Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1 | <!-- |
| 2 | Copyright 2018 The CUE Authors |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | --> |
| 16 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 17 | # The CUE Language Specification |
| 18 | |
| 19 | ## Introduction |
| 20 | |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 21 | This is a reference manual for the CUE data constraint language. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 22 | CUE, pronounced cue or Q, is a general-purpose and strongly typed |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 23 | constraint-based language. |
| 24 | It can be used for data templating, data validation, code generation, scripting, |
| 25 | and many other applications involving structured data. |
| 26 | The CUE tooling, layered on top of CUE, provides |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 27 | a general purpose scripting language for creating scripts as well as |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 28 | simple servers, also expressed in CUE. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 29 | |
| 30 | CUE was designed with cloud configuration, and related systems, in mind, |
| 31 | but is not limited to this domain. |
| 32 | It derives its formalism from relational programming languages. |
| 33 | This formalism allows for managing and reasoning over large amounts of |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 34 | data in a straightforward manner. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 35 | |
| 36 | The grammar is compact and regular, allowing for easy analysis by automatic |
| 37 | tools such as integrated development environments. |
| 38 | |
| 39 | This document is maintained by mpvl@golang.org. |
| 40 | CUE has a lot of similarities with the Go language. This document draws heavily |
Marcel van Lohuizen | 73f14eb | 2019-01-30 17:11:17 +0100 | [diff] [blame] | 41 | from the Go specification as a result. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 42 | |
| 43 | CUE draws its influence from many languages. |
| 44 | Its main influences were BCL/ GCL (internal to Google), |
| 45 | LKB (LinGO), Go, and JSON. |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 46 | Others are Swift, Typescript, Javascript, Prolog, NCL (internal to Google), |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 47 | Jsonnet, HCL, Flabbergast, Nix, JSONPath, Haskell, Objective-C, and Python. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 48 | |
| 49 | |
| 50 | ## Notation |
| 51 | |
| 52 | The syntax is specified using Extended Backus-Naur Form (EBNF): |
| 53 | |
| 54 | ``` |
| 55 | Production = production_name "=" [ Expression ] "." . |
| 56 | Expression = Alternative { "|" Alternative } . |
| 57 | Alternative = Term { Term } . |
| 58 | Term = production_name | token [ "…" token ] | Group | Option | Repetition . |
| 59 | Group = "(" Expression ")" . |
| 60 | Option = "[" Expression "]" . |
| 61 | Repetition = "{" Expression "}" . |
| 62 | ``` |
| 63 | |
| 64 | Productions are expressions constructed from terms and the following operators, |
| 65 | in increasing precedence: |
| 66 | |
| 67 | ``` |
| 68 | | alternation |
| 69 | () grouping |
| 70 | [] option (0 or 1 times) |
| 71 | {} repetition (0 to n times) |
| 72 | ``` |
| 73 | |
| 74 | Lower-case production names are used to identify lexical tokens. Non-terminals |
| 75 | are in CamelCase. Lexical tokens are enclosed in double quotes "" or back quotes |
| 76 | ``. |
| 77 | |
| 78 | The form a … b represents the set of characters from a through b as |
| 79 | alternatives. The horizontal ellipsis … is also used elsewhere in the spec to |
| 80 | informally denote various enumerations or code snippets that are not further |
| 81 | specified. The character … (as opposed to the three characters ...) is not a |
Roger Peppe | ded0e1d | 2019-09-24 16:39:36 +0100 | [diff] [blame] | 82 | token of the CUE language. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 83 | |
| 84 | |
| 85 | ## Source code representation |
| 86 | |
| 87 | Source code is Unicode text encoded in UTF-8. |
| 88 | Unless otherwise noted, the text is not canonicalized, so a single |
| 89 | accented code point is distinct from the same character constructed from |
| 90 | combining an accent and a letter; those are treated as two code points. |
| 91 | For simplicity, this document will use the unqualified term character to refer |
| 92 | to a Unicode code point in the source text. |
| 93 | |
| 94 | Each code point is distinct; for instance, upper and lower case letters are |
| 95 | different characters. |
| 96 | |
| 97 | Implementation restriction: For compatibility with other tools, a compiler may |
| 98 | disallow the NUL character (U+0000) in the source text. |
| 99 | |
| 100 | Implementation restriction: For compatibility with other tools, a compiler may |
| 101 | ignore a UTF-8-encoded byte order mark (U+FEFF) if it is the first Unicode code |
| 102 | point in the source text. A byte order mark may be disallowed anywhere else in |
| 103 | the source. |
| 104 | |
| 105 | |
| 106 | ### Characters |
| 107 | |
| 108 | The following terms are used to denote specific Unicode character classes: |
| 109 | |
| 110 | ``` |
| 111 | newline = /* the Unicode code point U+000A */ . |
| 112 | unicode_char = /* an arbitrary Unicode code point except newline */ . |
| 113 | unicode_letter = /* a Unicode code point classified as "Letter" */ . |
| 114 | unicode_digit = /* a Unicode code point classified as "Number, decimal digit" */ . |
| 115 | ``` |
| 116 | |
| 117 | In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of |
| 118 | character categories. |
| 119 | CUE treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo |
| 120 | as Unicode letters, and those in the Number category Nd as Unicode digits. |
| 121 | |
| 122 | |
| 123 | ### Letters and digits |
| 124 | |
| 125 | The underscore character _ (U+005F) is considered a letter. |
| 126 | |
| 127 | ``` |
| 128 | letter = unicode_letter | "_" . |
| 129 | decimal_digit = "0" … "9" . |
| 130 | octal_digit = "0" … "7" . |
| 131 | hex_digit = "0" … "9" | "A" … "F" | "a" … "f" . |
| 132 | ``` |
| 133 | |
| 134 | |
| 135 | ## Lexical elements |
| 136 | |
| 137 | ### Comments |
Marcel van Lohuizen | 7fc421b | 2019-09-11 09:24:03 +0200 | [diff] [blame] | 138 | Comments serve as program documentation. |
| 139 | CUE supports line comments that start with the character sequence // |
| 140 | and stop at the end of the line. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 141 | |
Marcel van Lohuizen | 7fc421b | 2019-09-11 09:24:03 +0200 | [diff] [blame] | 142 | A comment cannot start inside a string literal or inside a comment. |
| 143 | A comment acts like a newline. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 144 | |
| 145 | |
| 146 | ### Tokens |
| 147 | |
| 148 | Tokens form the vocabulary of the CUE language. There are four classes: |
| 149 | identifiers, keywords, operators and punctuation, and literals. White space, |
| 150 | formed from spaces (U+0020), horizontal tabs (U+0009), carriage returns |
| 151 | (U+000D), and newlines (U+000A), is ignored except as it separates tokens that |
| 152 | would otherwise combine into a single token. Also, a newline or end of file may |
| 153 | trigger the insertion of a comma. While breaking the input into tokens, the |
| 154 | next token is the longest sequence of characters that form a valid token. |
| 155 | |
| 156 | |
| 157 | ### Commas |
| 158 | |
| 159 | The formal grammar uses commas "," as terminators in a number of productions. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 160 | CUE programs may omit most of these commas using the following two rules: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 161 | |
| 162 | When the input is broken into tokens, a comma is automatically inserted into |
| 163 | the token stream immediately after a line's final token if that token is |
| 164 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 165 | - an identifier |
| 166 | - null, true, false, bottom, or an integer, floating-point, or string literal |
| 167 | - one of the characters ), ], or } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 168 | |
| 169 | |
| 170 | Although commas are automatically inserted, the parser will require |
| 171 | explicit commas between two list elements. |
| 172 | |
| 173 | To reflect idiomatic use, examples in this document elide commas using |
| 174 | these rules. |
| 175 | |
| 176 | |
| 177 | ### Identifiers |
| 178 | |
| 179 | Identifiers name entities such as fields and aliases. |
Marcel van Lohuizen | 8a2df96 | 2019-11-10 00:14:24 +0100 | [diff] [blame] | 180 | An identifier is a sequence of one or more letters (which includes `_` and `$`) |
| 181 | and digits. |
| 182 | It may not be `_` or `$`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 183 | The first character in an identifier must be a letter. |
| 184 | |
| 185 | <!-- |
| 186 | TODO: allow identifiers as defined in Unicode UAX #31 |
| 187 | (https://unicode.org/reports/tr31/). |
| 188 | |
| 189 | Identifiers are normalized using the NFC normal form. |
| 190 | --> |
| 191 | |
| 192 | ``` |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 193 | identifier = letter { letter | unicode_digit } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 194 | ``` |
| 195 | |
| 196 | ``` |
| 197 | a |
| 198 | _x9 |
| 199 | fieldName |
| 200 | αβ |
| 201 | ``` |
| 202 | |
| 203 | <!-- TODO: Allow Unicode identifiers TR 32 http://unicode.org/reports/tr31/ --> |
| 204 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 205 | Some identifiers are [predeclared](#predeclared-identifiers). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 206 | |
| 207 | |
| 208 | ### Keywords |
| 209 | |
| 210 | CUE has a limited set of keywords. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 211 | In addition, CUE reserves all identifiers starting with `__`(double underscores) |
| 212 | as keywords. |
| 213 | These are typically targets of pre-declared identifiers. |
| 214 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 215 | All keywords may be used as labels (field names). |
| 216 | They cannot, however, be used as identifiers to refer to the same name. |
| 217 | |
| 218 | |
| 219 | #### Values |
| 220 | |
| 221 | The following keywords are values. |
| 222 | |
| 223 | ``` |
| 224 | null true false |
| 225 | ``` |
| 226 | |
| 227 | These can never be used to refer to a field of the same name. |
| 228 | This restriction is to ensure compatibility with JSON configuration files. |
| 229 | |
| 230 | |
| 231 | #### Preamble |
| 232 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 233 | The following keywords are used at the preamble of a CUE file. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 234 | After the preamble, they may be used as identifiers to refer to namesake fields. |
| 235 | |
| 236 | ``` |
| 237 | package import |
| 238 | ``` |
| 239 | |
| 240 | |
| 241 | #### Comprehension clauses |
| 242 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 243 | The following keywords are used in comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 244 | |
| 245 | ``` |
| 246 | for in if let |
| 247 | ``` |
| 248 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 249 | The keywords `for`, `if` and `let` cannot be used as identifiers to |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 250 | refer to fields. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 251 | |
| 252 | <!-- |
| 253 | TODO: |
| 254 | reduce [to] |
| 255 | order [by] |
| 256 | --> |
| 257 | |
| 258 | |
| 259 | #### Arithmetic |
| 260 | |
| 261 | The following pseudo keywords can be used as operators in expressions. |
| 262 | |
| 263 | ``` |
| 264 | div mod quo rem |
| 265 | ``` |
| 266 | |
| 267 | These may be used as identifiers to refer to fields in all other contexts. |
| 268 | |
| 269 | |
| 270 | ### Operators and punctuation |
| 271 | |
| 272 | The following character sequences represent operators and punctuation: |
| 273 | |
| 274 | ``` |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 275 | + div && == < = ( ) |
| 276 | - mod || != > :: { } |
| 277 | * quo & =~ <= : [ ] |
| 278 | / rem | !~ >= . ... , |
| 279 | _|_ ! |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 280 | ``` |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 281 | <!-- |
| 282 | Free tokens: # ; ~ $ ^ |
| 283 | |
| 284 | // To be used: |
| 285 | @ at: associative lists. |
| 286 | |
| 287 | // Idea: use # instead of @ for attributes and allow then at declaration level. |
| 288 | // This will open up the possibility of defining #! at the start of a file |
| 289 | // without requiring special syntax. Although probably not quite. |
| 290 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 291 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 292 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 293 | ### Integer literals |
| 294 | |
| 295 | An integer literal is a sequence of digits representing an integer value. |
Marcel van Lohuizen | b2703c6 | 2019-09-29 18:20:01 +0200 | [diff] [blame] | 296 | An optional prefix sets a non-decimal base: 0o for octal, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 297 | 0x or 0X for hexadecimal, and 0b for binary. |
| 298 | In hexadecimal literals, letters a-f and A-F represent values 10 through 15. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 299 | All integers allow interstitial underscores "_"; |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 300 | these have no meaning and are solely for readability. |
| 301 | |
| 302 | Decimal integers may have a SI or IEC multiplier. |
| 303 | Multipliers can be used with fractional numbers. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 304 | When multiplying a fraction by a multiplier, the result is truncated |
| 305 | towards zero if it is not an integer. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 306 | |
| 307 | ``` |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 308 | int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit . |
| 309 | decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } . |
| 310 | decimals = decimal_digit { [ "_" ] decimal_digit } . |
| 311 | si_it = decimals [ "." decimals ] multiplier | |
| 312 | "." decimals multiplier . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 313 | binary_lit = "0b" binary_digit { binary_digit } . |
| 314 | hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } . |
Marcel van Lohuizen | b2703c6 | 2019-09-29 18:20:01 +0200 | [diff] [blame] | 315 | octal_lit = "0o" octal_digit { [ "_" ] octal_digit } . |
Marcel van Lohuizen | 6eefcd0 | 2019-10-04 13:32:06 +0200 | [diff] [blame] | 316 | multiplier = ( "K" | "M" | "G" | "T" | "P" ) [ "i" ] |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 317 | |
| 318 | float_lit = decimals "." [ decimals ] [ exponent ] | |
| 319 | decimals exponent | |
| 320 | "." decimals [ exponent ]. |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 321 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 322 | ``` |
Marcel van Lohuizen | 6eefcd0 | 2019-10-04 13:32:06 +0200 | [diff] [blame] | 323 | <!-- |
| 324 | TODO: consider allowing Exo (and up), if not followed by a sign |
| 325 | or number. Alternatively one could only allow Ei, Yi, and Zi. |
| 326 | --> |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 327 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 328 | ``` |
| 329 | 42 |
| 330 | 1.5Gi |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 331 | 170_141_183_460_469_231_731_687_303_715_884_105_727 |
Marcel van Lohuizen | fc6303c | 2019-02-07 17:49:04 +0100 | [diff] [blame] | 332 | 0xBad_Face |
| 333 | 0o755 |
| 334 | 0b0101_0001 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 335 | ``` |
| 336 | |
| 337 | ### Decimal floating-point literals |
| 338 | |
| 339 | A decimal floating-point literal is a representation of |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 340 | a decimal floating-point value (a _float_). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 341 | It has an integer part, a decimal point, a fractional part, and an |
| 342 | exponent part. |
| 343 | The integer and fractional part comprise decimal digits; the |
| 344 | exponent part is an `e` or `E` followed by an optionally signed decimal exponent. |
| 345 | One of the integer part or the fractional part may be elided; one of the decimal |
| 346 | point or the exponent may be elided. |
| 347 | |
| 348 | ``` |
| 349 | decimal_lit = decimals "." [ decimals ] [ exponent ] | |
| 350 | decimals exponent | |
| 351 | "." decimals [ exponent ] . |
| 352 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
| 353 | ``` |
| 354 | |
| 355 | ``` |
| 356 | 0. |
| 357 | 72.40 |
| 358 | 072.40 // == 72.40 |
| 359 | 2.71828 |
| 360 | 1.e+0 |
| 361 | 6.67428e-11 |
| 362 | 1E6 |
| 363 | .25 |
| 364 | .12345E+5 |
| 365 | ``` |
| 366 | |
| 367 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 368 | ### String and byte sequence literals |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 369 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 370 | A string literal represents a string constant obtained from concatenating a |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 371 | sequence of characters. |
| 372 | Byte sequences are a sequence of bytes. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 373 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 374 | String and byte sequence literals are character sequences between, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 375 | respectively, double and single quotes, as in `"bar"` and `'bar'`. |
| 376 | Within the quotes, any character may appear except newline and, |
| 377 | respectively, unescaped double or single quote. |
| 378 | String literals may only be valid UTF-8. |
| 379 | Byte sequences may contain any sequence of bytes. |
| 380 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 381 | Several escape sequences allow arbitrary values to be encoded as ASCII text. |
| 382 | An escape sequence starts with an _escape delimiter_, which is `\` by default. |
| 383 | The escape delimiter may be altered to be `\` plus a fixed number of |
| 384 | hash symbols `#` |
| 385 | by padding the start and end of a string or byte sequence literal |
| 386 | with this number of hash symbols. |
| 387 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 388 | There are four ways to represent the integer value as a numeric constant: `\x` |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 389 | followed by exactly two hexadecimal digits; `\u` followed by exactly four |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 390 | hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a |
| 391 | plain backslash `\` followed by exactly three octal digits. |
| 392 | In each case the value of the literal is the value represented by the |
| 393 | digits in the corresponding base. |
| 394 | Hexadecimal and octal escapes are only allowed within byte sequences |
| 395 | (single quotes). |
| 396 | |
| 397 | Although these representations all result in an integer, they have different |
| 398 | valid ranges. |
| 399 | Octal escapes must represent a value between 0 and 255 inclusive. |
| 400 | Hexadecimal escapes satisfy this condition by construction. |
| 401 | The escapes `\u` and `\U` represent Unicode code points so within them |
| 402 | some values are illegal, in particular those above `0x10FFFF`. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 403 | Surrogate halves are allowed, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 404 | but are translated into their non-surrogate equivalent internally. |
| 405 | |
| 406 | The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes |
| 407 | represent individual bytes of the resulting string; all other escapes represent |
| 408 | the (possibly multi-byte) UTF-8 encoding of individual characters. |
| 409 | Thus inside a string literal `\377` and `\xFF` represent a single byte of |
| 410 | value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent |
| 411 | the two bytes `0xc3 0xbf` of the UTF-8 |
| 412 | encoding of character `U+00FF`. |
| 413 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 414 | ``` |
| 415 | \a U+0007 alert or bell |
| 416 | \b U+0008 backspace |
| 417 | \f U+000C form feed |
| 418 | \n U+000A line feed or newline |
| 419 | \r U+000D carriage return |
| 420 | \t U+0009 horizontal tab |
| 421 | \v U+000b vertical tab |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 422 | \/ U+002f slash (solidus) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 423 | \\ U+005c backslash |
| 424 | \' U+0027 single quote (valid escape only within single quoted literals) |
| 425 | \" U+0022 double quote (valid escape only within double quoted literals) |
| 426 | ``` |
| 427 | |
| 428 | The escape `\(` is used as an escape for string interpolation. |
| 429 | A `\(` must be followed by a valid CUE Expression, followed by a `)`. |
| 430 | |
| 431 | All other sequences starting with a backslash are illegal inside literals. |
| 432 | |
| 433 | ``` |
Marcel van Lohuizen | 39df6c9 | 2019-10-25 20:16:26 +0200 | [diff] [blame] | 434 | escaped_char = `\` { `#` } ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "/" | `\` | "'" | `"` ) . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 435 | byte_value = octal_byte_value | hex_byte_value . |
| 436 | octal_byte_value = `\` octal_digit octal_digit octal_digit . |
| 437 | hex_byte_value = `\` "x" hex_digit hex_digit . |
| 438 | little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit . |
| 439 | big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit |
| 440 | hex_digit hex_digit hex_digit hex_digit . |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 441 | unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . |
| 442 | interpolation = "\(" Expression ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 443 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 444 | string_lit = simple_string_lit | |
| 445 | multiline_string_lit | |
| 446 | simple_bytes_lit | |
| 447 | multiline_bytes_lit | |
| 448 | `#` string_lit `#` . |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 449 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 450 | simple_string_lit = `"` { unicode_value | interpolation } `"` . |
Marcel van Lohuizen | c6e5d17 | 2019-11-22 12:09:25 -0800 | [diff] [blame] | 451 | simple_bytes_lit = `'` { unicode_value | interpolation | byte_value } `'` . |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 452 | multiline_string_lit = `"""` newline |
| 453 | { unicode_value | interpolation | newline } |
| 454 | newline `"""` . |
| 455 | multiline_bytes_lit = "'''" newline |
| 456 | { unicode_value | interpolation | byte_value | newline } |
| 457 | newline "'''" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 458 | ``` |
| 459 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 460 | Carriage return characters (`\r`) inside string literals are discarded from |
Marcel van Lohuizen | db9d25a | 2019-02-21 23:54:43 +0100 | [diff] [blame] | 461 | the string value. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 462 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 463 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 464 | 'a\000\xab' |
| 465 | '\007' |
| 466 | '\377' |
| 467 | '\xa' // illegal: too few hexadecimal digits |
| 468 | "\n" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 469 | "\"" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 470 | 'Hello, world!\n' |
| 471 | "Hello, \( name )!" |
| 472 | "日本語" |
| 473 | "\u65e5本\U00008a9e" |
| 474 | "\xff\u00FF" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 475 | "\uD800" // illegal: surrogate half (TODO: probably should allow) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 476 | "\U00110000" // illegal: invalid Unicode code point |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 477 | |
| 478 | #"This is not an \(interpolation)"# |
| 479 | #"This is an \#(interpolation)"# |
| 480 | #"The sequence "\U0001F604" renders as \#U0001F604."# |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 481 | ``` |
| 482 | |
| 483 | These examples all represent the same string: |
| 484 | |
| 485 | ``` |
| 486 | "日本語" // UTF-8 input text |
| 487 | '日本語' // UTF-8 input text as byte sequence |
| 488 | `日本語` // UTF-8 input text as a raw literal |
| 489 | "\u65e5\u672c\u8a9e" // the explicit Unicode code points |
| 490 | "\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points |
| 491 | "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes |
| 492 | ``` |
| 493 | |
| 494 | If the source code represents a character as two code points, such as a |
| 495 | combining form involving an accent and a letter, the result will appear as two |
| 496 | code points if placed in a string literal. |
| 497 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 498 | Strings and byte sequences have a multiline equivalent. |
| 499 | Multiline strings are like their single-line equivalent, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 500 | but allow newline characters. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 501 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 502 | Multiline strings and byte sequences respectively start with |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 503 | a triple double quote (`"""`) or triple single quote (`'''`), |
| 504 | immediately followed by a newline, which is discarded from the string contents. |
| 505 | The string is closed by a matching triple quote, which must be by itself |
| 506 | on a newline, preceded by optional whitespace. |
Marcel van Lohuizen | c8d6c39 | 2019-12-02 13:30:47 +0100 | [diff] [blame] | 507 | The newline preceding the closing quote is discarded from the string contents. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 508 | The whitespace before a closing triple quote must appear before any non-empty |
| 509 | line after the opening quote and will be removed from each of these |
| 510 | lines in the string literal. |
| 511 | A closing triple quote may not appear in the string. |
| 512 | To include it is suffices to escape one of the quotes. |
| 513 | |
| 514 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 515 | """ |
| 516 | lily: |
| 517 | out of the water |
| 518 | out of itself |
| 519 | |
| 520 | bass |
| 521 | picking bugs |
| 522 | off the moon |
| 523 | — Nick Virgilio, Selected Haiku, 1988 |
| 524 | """ |
| 525 | ``` |
| 526 | |
| 527 | This represents the same string as: |
| 528 | |
| 529 | ``` |
| 530 | "lily:\nout of the water\nout of itself\n\n" + |
| 531 | "bass\npicking bugs\noff the moon\n" + |
| 532 | " — Nick Virgilio, Selected Haiku, 1988" |
| 533 | ``` |
| 534 | |
| 535 | <!-- TODO: other values |
| 536 | |
| 537 | Support for other values: |
| 538 | - Duration literals |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 539 | - regular expessions: `re("[a-z]")` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 540 | --> |
| 541 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 542 | |
| 543 | ## Values |
| 544 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 545 | In addition to simple values like `"hello"` and `42.0`, CUE has _structs_. |
| 546 | A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`. |
| 547 | Structs are CUE's only way of building up complex values; |
| 548 | lists, which we will see later, |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 549 | are defined in terms of structs. |
| 550 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 551 | All possible values are ordered in a lattice, |
| 552 | a partial order where every two elements have a single greatest lower bound. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 553 | A value `a` is an _instance_ of a value `b`, |
| 554 | denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`, |
| 555 | that is if `a` orders before `b` in the partial order |
| 556 | (`⊑` is _not_ a CUE operator). |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 557 | We also say that `b` _subsumes_ `a` in this case. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 558 | In graphical terms, `b` is "above" `a` in the lattice. |
| 559 | |
| 560 | At the top of the lattice is the single ancestor of all values, called |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 561 | _top_, denoted `_` in CUE. |
| 562 | Every value is an instance of top. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 563 | |
| 564 | At the bottom of the lattice is the value called _bottom_, denoted `_|_`. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 565 | A bottom value usually indicates an error. |
| 566 | Bottom is an instance of every value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 567 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 568 | An _atom_ is any value whose only instances are itself and bottom. |
| 569 | Examples of atoms are `42.0`, `"hello"`, `true`, `null`. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 570 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 571 | A value is _concrete_ if it is either an atom, or a struct all of whose |
| 572 | field values are themselves concrete, recursively. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 573 | |
| 574 | CUE's values also include what we normally think of as types, like `string` and |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 575 | `float`. |
| 576 | But CUE does not distinguish between types and values; only the |
| 577 | relationship of values in the lattice is important. |
| 578 | Each CUE "type" subsumes the concrete values that one would normally think |
| 579 | of as part of that type. |
| 580 | For example, "hello" is an instance of `string`, and `42.0` is an instance of |
| 581 | `float`. |
| 582 | In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`. |
| 583 | We informally call these CUE's "basic types". |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 584 | |
| 585 | |
| 586 | ``` |
| 587 | false ⊑ bool |
| 588 | true ⊑ bool |
| 589 | true ⊑ true |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 590 | 5.0 ⊑ float |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 591 | bool ⊑ _ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 592 | _|_ ⊑ _ |
| 593 | _|_ ⊑ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 594 | |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 595 | _ ⋢ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 596 | _ ⋢ bool |
| 597 | int ⋢ bool |
| 598 | bool ⋢ int |
| 599 | false ⋢ true |
| 600 | true ⋢ false |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 601 | float ⋢ 5.0 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 602 | 5 ⋢ 6 |
| 603 | ``` |
| 604 | |
| 605 | |
| 606 | ### Unification |
| 607 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 608 | The _unification_ of values `a` and `b` |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 609 | is defined as the greatest lower bound of `a` and `b`. (That is, the |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 610 | value `u` such that `u ⊑ a` and `u ⊑ b`, |
| 611 | and for any other value `v` for which `v ⊑ a` and `v ⊑ b` |
| 612 | it holds that `v ⊑ u`.) |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 613 | Since CUE values form a lattice, the unification of two CUE values is |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 614 | always unique. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 615 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 616 | These all follow from the definition of unification: |
| 617 | - The unification of `a` with itself is always `a`. |
| 618 | - The unification of values `a` and `b` where `a ⊑ b` is always `a`. |
| 619 | - The unification of a value with bottom is always bottom. |
| 620 | |
| 621 | Unification in CUE is a [binary expression](#Operands), written `a & b`. |
| 622 | It is commutative and associative. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 623 | As a consequence, order of evaluation is irrelevant, a property that is key |
| 624 | to many of the constructs in the CUE language as well as the tooling layered |
| 625 | on top of it. |
| 626 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 627 | |
| 628 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 629 | <!-- TODO: explicitly mention that disjunction is not a binary operation |
| 630 | but a definition of a single value?--> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 631 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 632 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 633 | ### Disjunction |
| 634 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 635 | The _disjunction_ of values `a` and `b` |
| 636 | is defined as the least upper bound of `a` and `b`. |
| 637 | (That is, the value `d` such that `a ⊑ d` and `b ⊑ d`, |
| 638 | and for any other value `e` for which `a ⊑ e` and `b ⊑ e`, |
| 639 | it holds that `d ⊑ e`.) |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 640 | This style of disjunctions is sometimes also referred to as sum types. |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 641 | Since CUE values form a lattice, the disjunction of two CUE values is always unique. |
| 642 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 643 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 644 | These all follow from the definition of disjunction: |
| 645 | - The disjunction of `a` with itself is always `a`. |
| 646 | - The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`. |
| 647 | - The disjunction of a value `a` with bottom is always `a`. |
| 648 | - The disjunction of two bottom values is bottom. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 649 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 650 | Disjunction in CUE is a [binary expression](#Operands), written `a | b`. |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 651 | It is commutative, associative, and idempotent. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 652 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 653 | The unification of a disjunction with another value is equal to the disjunction |
| 654 | composed of the unification of this value with all of the original elements |
| 655 | of the disjunction. |
| 656 | In other words, unification distributes over disjunction. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 657 | |
| 658 | ``` |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 659 | (a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 660 | ``` |
| 661 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 662 | ``` |
| 663 | Expression Result |
| 664 | ({a:1} | {b:2}) & {c:3} {a:1, c:3} | {b:2, c:3} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 665 | (int | string) & "foo" "foo" |
| 666 | ("a" | "b") & "c" _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 667 | ``` |
| 668 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 669 | A disjunction is _normalized_ if there is no element |
| 670 | `a` for which there is an element `b` such that `a ⊑ b`. |
| 671 | |
| 672 | <!-- |
| 673 | Normalization is important, as we need to account for spurious elements |
| 674 | For instance "tcp" | "tcp" should resolve to "tcp". |
| 675 | |
| 676 | Also consider |
| 677 | |
| 678 | ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2}, |
| 679 | |
| 680 | in this case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1} and thus |
| 681 | this expression is logically equivalent to {a:1} and should therefore be |
| 682 | considered to be unambiguous and resolve to {a:1} if a concrete value is needed. |
| 683 | |
| 684 | For instance, in |
| 685 | |
| 686 | x: ({a:1} | {b:1}) & ({a:1} | {b:2}) // -> {a:1} | {a:1,b:1} | {a:1,b:2} |
| 687 | y: x.a // 1 |
| 688 | |
| 689 | y should resolve to 1, and not an error. |
| 690 | |
| 691 | For comparison, in |
| 692 | |
| 693 | x: ({a:1, b:1} | {b:2}) & {a:1} // -> {a:1,b:1} | {a:1,b:2} |
| 694 | y: x.a // _|_ |
| 695 | |
| 696 | y should be an error as x is still ambiguous before the selector is applied, |
| 697 | even though `a` resolves to 1 in all cases. |
| 698 | --> |
| 699 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 700 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 701 | #### Default values |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 702 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 703 | Any element of a disjunction can be marked as a default |
Axel Wagner | 8529d77 | 2019-09-24 18:27:12 +0000 | [diff] [blame] | 704 | by prefixing it with an asterisk `*`. |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 705 | Intuitively, when an expression needs to be resolved for an operation other |
| 706 | than unification or disjunctions, |
| 707 | non-starred elements are dropped in favor of starred ones if the starred ones |
| 708 | do not resolve to bottom. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 709 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 710 | More precisely, any value `v` may be associated with a default value `d`, |
| 711 | denoted `(v, d)` (not CUE syntax), |
| 712 | where `d` must be in instance of `v` (`d ⊑ v`). |
| 713 | The rules for unifying and disjoining such values are as follows: |
| 714 | |
| 715 | ``` |
| 716 | U1: (v1, d1) & v2 => (v1&v2, d1&v2) |
| 717 | U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2) |
| 718 | |
| 719 | D1: (v1, d1) | v2 => (v1|v2, d1) |
| 720 | D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2) |
| 721 | ``` |
| 722 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 723 | Default values may be introduced within disjunctions |
| 724 | by _marking_ terms of a disjunction with an asterisk `*` |
| 725 | ([a unary expression](#Operators)). |
| 726 | The default value of a disjunction with marked terms is the disjunction |
| 727 | of those marked terms, applying the following rules for marks: |
| 728 | |
| 729 | ``` |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 730 | M1: *v => (v, v) |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 731 | M2: *(v1, d1) => (v1, d1) |
| 732 | ``` |
| 733 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 734 | In general, any operation `f` in CUE involving default values proceeds along the |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 735 | following lines |
| 736 | ``` |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 737 | O1: f((v1, d1), ..., (vn, dn)) => (f(v1, ..., vn), f(d1, ..., dn)) |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 738 | ``` |
| 739 | where, with the exception of disjunction, a value `v` without a default |
| 740 | value is promoted to `(v, v)`. |
| 741 | |
| 742 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 743 | ``` |
| 744 | Expression Value-default pair Rules applied |
| 745 | *"tcp" | "udp" ("tcp"|"udp", "tcp") M1, D1 |
| 746 | string | *"foo" (string, "foo") M1, D1 |
| 747 | |
| 748 | *1 | 2 | 3 (1|2|3, 1) M1, D1 |
| 749 | |
| 750 | (*1|2|3) | (1|*2|3) (1|2|3, 1|2) M1, D1, D2 |
| 751 | (*1|2|3) | *(1|*2|3) (1|2|3, 1|2) M1, D1, M2, D2 |
| 752 | (*1|2|3) | (1|*2|3)&2 (1|2|3, 1|2) M1, D1, U1, D2 |
| 753 | |
| 754 | (*1|2) & (1|*2) (1|2, _|_) M1, D1, U2 |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 755 | |
| 756 | (*1|2) + (1|*2) ((1|2)+(1|2), 3) M1, D1, O1 |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 757 | ``` |
| 758 | |
| 759 | The rules of subsumption for defaults can be derived from the above definitions |
| 760 | and are as follows. |
| 761 | |
| 762 | ``` |
| 763 | (v2, d2) ⊑ (v1, d1) if v2 ⊑ v1 and d2 ⊑ d1 |
| 764 | (v1, d1) ⊑ v if v1 ⊑ v |
| 765 | v ⊑ (v1, d1) if v ⊑ d1 |
| 766 | ``` |
| 767 | |
| 768 | <!-- |
| 769 | For the second rule, note that by definition d1 ⊑ v1, so d1 ⊑ v1 ⊑ v. |
| 770 | |
| 771 | The last one is so restrictive as v could still be made more specific by |
| 772 | associating it with a default that is not subsumed by d1. |
| 773 | |
| 774 | Proof: |
| 775 | by definition for any d ⊑ v, it holds that (v, d) ⊑ v, |
| 776 | where the most general value is (v, v). |
| 777 | Given the subsumption rule for (v2, d2) ⊑ (v1, d1), |
| 778 | from (v, v) ⊑ v ⊑ (v1, d1) it follows that v ⊑ d1 |
| 779 | exactly defines the boundary of this subsumption. |
| 780 | --> |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 781 | |
| 782 | <!-- |
| 783 | (non-normalized entries could also be implicitly marked, allowing writing |
| 784 | int | 1, instead of int | *1, but that can be done in a backwards |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 785 | compatible way later if really desirable, as long as we require that |
| 786 | disjunction literals be normalized). |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 787 | --> |
| 788 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 789 | |
| 790 | ``` |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 791 | Expression Resolves to |
| 792 | "tcp" | "udp" "tcp" | "udp" |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 793 | *"tcp" | "udp" "tcp" |
| 794 | float | *1 1 |
| 795 | *string | 1.0 string |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 796 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 797 | (*1|2|3) | (1|*2|3) 1|2 |
| 798 | (*1|2|3) & (1|*2|3) 1|2|3 // default is _|_ |
| 799 | |
| 800 | (* >=5 | int) & (* <=5 | int) 5 |
| 801 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 802 | (*"tcp"|"udp") & ("udp"|*"tcp") "tcp" |
| 803 | (*"tcp"|"udp") & ("udp"|"tcp") "tcp" |
| 804 | (*"tcp"|"udp") & "tcp" "tcp" |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 805 | (*"tcp"|"udp") & (*"udp"|"tcp") "tcp" | "udp" // default is _|_ |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 806 | |
| 807 | (*true | false) & bool true |
| 808 | (*true | false) & (true | false) true |
| 809 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 810 | {a: 1} | {b: 1} {a: 1} | {b: 1} |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 811 | {a: 1} | *{b: 1} {b:1} |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 812 | *{a: 1} | *{b: 1} {a: 1} | {b: 1} |
| 813 | ({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} by normalization |
| 814 | ({a:1}|*{b:1}) & ({a:1}|*{b:1}) {b:1} // after eliminating {a:1,b:1} by normalization |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 815 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 816 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 817 | |
| 818 | ### Bottom and errors |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 819 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 820 | Any evaluation error in CUE results in a bottom value, respresented by |
Axel Wagner | 8529d77 | 2019-09-24 18:27:12 +0000 | [diff] [blame] | 821 | the token `_|_`. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 822 | Bottom is an instance of every other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 823 | Any evaluation error is represented as bottom. |
| 824 | |
| 825 | Implementations may associate error strings with different instances of bottom; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 826 | logically they all remain the same value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 827 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 828 | |
| 829 | ### Top |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 830 | |
Axel Wagner | 8529d77 | 2019-09-24 18:27:12 +0000 | [diff] [blame] | 831 | Top is represented by the underscore character `_`, lexically an identifier. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 832 | Unifying any value `v` with top results `v` itself. |
| 833 | |
| 834 | ``` |
| 835 | Expr Result |
| 836 | _ & 5 5 |
| 837 | _ & _ _ |
| 838 | _ & _|_ _|_ |
| 839 | _ | _|_ _ |
| 840 | ``` |
| 841 | |
| 842 | |
| 843 | ### Null |
| 844 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 845 | The _null value_ is represented with the keyword `null`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 846 | It has only one parent, top, and one child, bottom. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 847 | It is unordered with respect to any other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 848 | |
| 849 | ``` |
| 850 | null_lit = "null" |
| 851 | ``` |
| 852 | |
| 853 | ``` |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 854 | null & 8 _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 855 | null & _ null |
| 856 | null & _|_ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 857 | ``` |
| 858 | |
| 859 | |
| 860 | ### Boolean values |
| 861 | |
| 862 | A _boolean type_ represents the set of Boolean truth values denoted by |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 863 | the keywords `true` and `false`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 864 | The predeclared boolean type is `bool`; it is a defined type and a separate |
| 865 | element in the lattice. |
| 866 | |
| 867 | ``` |
| 868 | boolean_lit = "true" | "false" |
| 869 | ``` |
| 870 | |
| 871 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 872 | bool & true true |
| 873 | true & true true |
| 874 | true & false _|_ |
| 875 | bool & (false|true) false | true |
| 876 | bool & (true|false) true | false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 877 | ``` |
| 878 | |
| 879 | |
| 880 | ### Numeric values |
| 881 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 882 | The _integer type_ represents the set of all integral numbers. |
| 883 | The _decimal floating-point type_ represents the set of all decimal floating-point |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 884 | numbers. |
| 885 | They are two distinct types. |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 886 | Both are instances instances of a generic `number` type. |
| 887 | |
| 888 | <!-- |
| 889 | number |
| 890 | / \ |
| 891 | int float |
| 892 | --> |
| 893 | |
| 894 | The predeclared number, integer, decimal floating-point types are |
| 895 | `number`, `int` and `float`; they are defined types. |
| 896 | <!-- |
| 897 | TODO: should we drop float? It is somewhat preciser and probably a good idea |
| 898 | to have it in the programmatic API, but it may be confusing to have to deal |
| 899 | with it in the language. |
| 900 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 901 | |
| 902 | A decimal floating-point literal always has type `float`; |
| 903 | it is not an instance of `int` even if it is an integral number. |
| 904 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 905 | Integer literals are always of type `int` and don't match type `float`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 906 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 907 | Numeric literals are exact values of arbitrary precision. |
| 908 | If the operation permits it, numbers should be kept in arbitrary precision. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 909 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 910 | Implementation restriction: although numeric values have arbitrary precision |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 911 | in the language, implementations may implement them using an internal |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 912 | representation with limited precision. |
| 913 | That said, every implementation must: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 914 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 915 | - Represent integer values with at least 256 bits. |
| 916 | - Represent floating-point values, with a mantissa of at least 256 bits and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 917 | a signed binary exponent of at least 16 bits. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 918 | - Give an error if unable to represent an integer value precisely. |
| 919 | - Give an error if unable to represent a floating-point value due to overflow. |
| 920 | - Round to the nearest representable value if unable to represent |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 921 | a floating-point value due to limits on precision. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 922 | These requirements apply to the result of any expression except for builtin |
| 923 | functions for which an unusual loss of precision must be explicitly documented. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 924 | |
| 925 | |
| 926 | ### Strings |
| 927 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 928 | The _string type_ represents the set of UTF-8 strings, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 929 | not allowing surrogates. |
| 930 | The predeclared string type is `string`; it is a defined type. |
| 931 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 932 | The length of a string `s` (its size in bytes) can be discovered using |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 933 | the built-in function `len`. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 934 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 935 | |
| 936 | ### Bytes |
| 937 | |
| 938 | The _bytes type_ represents the set of byte sequences. |
| 939 | A byte sequence value is a (possibly empty) sequence of bytes. |
| 940 | The number of bytes is called the length of the byte sequence |
| 941 | and is never negative. |
| 942 | The predeclared byte sequence type is `bytes`; it is a defined type. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 943 | |
| 944 | |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 945 | ### Bounds |
| 946 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 947 | A _bound_, syntactically a [unary expression](#Operands), defines |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 948 | an infinite disjunction of concrete values than can be represented |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 949 | as a single comparison. |
| 950 | |
| 951 | For any [comparison operator](#Comparison-operators) `op` except `==`, |
| 952 | `op a` is the disjunction of every `x` such that `x op a`. |
| 953 | |
| 954 | ``` |
| 955 | 2 & >=2 & <=5 // 2, where 2 is either an int or float. |
| 956 | 2.5 & >=1 & <=5 // 2.5 |
| 957 | 2 & >=1.0 & <3.0 // 2.0 |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 958 | 2 & >1 & <3.0 // 2.0 |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 959 | 2.5 & int & >1 & <5 // _|_ |
| 960 | 2.5 & float & >1 & <5 // 2.5 |
| 961 | int & 2 & >1.0 & <3.0 // _|_ |
| 962 | 2.5 & >=(int & 1) & <5 // _|_ |
| 963 | >=0 & <=7 & >=3 & <=10 // >=3 & <=7 |
| 964 | !=null & 1 // 1 |
| 965 | >=5 & <=5 // 5 |
| 966 | ``` |
| 967 | |
| 968 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 969 | ### Structs |
| 970 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 971 | A _struct_ is a set of elements called _fields_, each of |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 972 | which has a name, called a _label_, and value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 973 | |
| 974 | We say a label is defined for a struct if the struct has a field with the |
| 975 | corresponding label. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 976 | The value for a label `f` of struct `a` is denoted `a.f`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 977 | A struct `a` is an instance of `b`, or `a ⊑ b`, if for any label `f` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 978 | defined for `b`, label `f` is also defined for `a` and `a.f ⊑ b.f`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 979 | Note that if `a` is an instance of `b` it may have fields with labels that |
| 980 | are not defined for `b`. |
| 981 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 982 | The (unique) struct with no fields, written `{}`, has every struct as an |
| 983 | instance. It can be considered the type of all structs. |
| 984 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 985 | ``` |
| 986 | {a: 1} ⊑ {} |
| 987 | {a: 1, b: 1} ⊑ {a: 1} |
| 988 | {a: 1} ⊑ {a: int} |
| 989 | {a: 1, b: 1} ⊑ {a: int, b: float} |
| 990 | |
| 991 | {} ⋢ {a: 1} |
| 992 | {a: 2} ⋢ {a: 1} |
| 993 | {a: 1} ⋢ {b: 1} |
| 994 | ``` |
| 995 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 996 | A field may be required or optional. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 997 | The successful unification of structs `a` and `b` is a new struct `c` which |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 998 | has all fields of both `a` and `b`, where |
| 999 | the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`, |
| 1000 | or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1001 | If a field `f` is in both `a` and `b`, `c.f` is optional only if both |
| 1002 | `a.f` and `b.f` are optional. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1003 | Any [references](#References) to `a` or `b` |
| 1004 | in their respective field values need to be replaced with references to `c`. |
Marcel van Lohuizen | 3022ae9 | 2019-10-15 13:35:58 +0200 | [diff] [blame] | 1005 | The result of a unification is bottom (`_|_`) if any of its non-optional |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1006 | fields evaluates to bottom, recursively. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 1007 | |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1008 | <!--NOTE: About bottom values for optional fields being okay. |
| 1009 | |
| 1010 | The proposition ¬P is a close cousin of P → ⊥ and is often used |
| 1011 | as an approximation to avoid the issues of using not. |
| 1012 | Bottom (⊥) is also frequently used to mean undefined. This makes sense. |
| 1013 | Consider `{a?: 2} & {a?: 3}`. |
| 1014 | Both structs say `a` is optional; in other words, it may be omitted. |
| 1015 | So we can still get a valid result by omitting `a`, even in |
| 1016 | case of a conflict. |
| 1017 | |
| 1018 | Granted, this definition may lead to confusing results, especially in |
| 1019 | definitions, when tightening an optional field leads to unintentionally |
| 1020 | discarding it. |
| 1021 | It could be a role of vet checkers to identify such cases (and suggest users |
| 1022 | to explicitly use `_|_` to discard a field, for instance). |
| 1023 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1024 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1025 | Syntactically, a struct literal may contain multiple fields with |
| 1026 | the same label, the result of which is a single field with the same properties |
| 1027 | as defined as the unification of two fields resulting from unifying two structs. |
| 1028 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1029 | These examples illustrate required fields only. |
| 1030 | Examples with optional fields follow below. |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1031 | |
| 1032 | ``` |
| 1033 | Expression Result (without optional fields) |
| 1034 | {a: int, a: 1} {a: 1} |
| 1035 | {a: int} & {a: 1} {a: 1} |
| 1036 | {a: >=1 & <=7} & {a: >=5 & <=9} {a: >=5 & <=7} |
| 1037 | {a: >=1 & <=7, a: >=5 & <=9} {a: >=5 & <=7} |
| 1038 | |
| 1039 | {a: 1} & {b: 2} {a: 1, b: 2} |
| 1040 | {a: 1, b: int} & {b: 2} {a: 1, b: 2} |
| 1041 | |
| 1042 | {a: 1} & {a: 2} _|_ |
| 1043 | ``` |
| 1044 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1045 | Optional labels are defined in sets with an expression to select all |
| 1046 | labels to which to apply a given constraint. |
| 1047 | Syntactically, the label of an optional field set is an expression in square |
| 1048 | brackets indicating the matching labels. |
| 1049 | The value `string` matches all fields, while a concrete string matches a |
| 1050 | single field. |
| 1051 | As the latter case is common, a concrete label followed by |
| 1052 | a question mark `?` may be used as a shorthand. |
Marcel van Lohuizen | 0cb140e | 2020-02-10 09:09:43 +0100 | [diff] [blame] | 1053 | So |
| 1054 | ``` |
| 1055 | foo?: bar |
| 1056 | ``` |
| 1057 | is a shorthand for |
| 1058 | ``` |
| 1059 | ["foo"]: bar |
| 1060 | ``` |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1061 | The question mark is not part of the field name. |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1062 | The token `...` may be used as the last declaration in a struct |
Marcel van Lohuizen | 0cb140e | 2020-02-10 09:09:43 +0100 | [diff] [blame] | 1063 | and is a shorthand for |
| 1064 | ``` |
| 1065 | [_]: _ |
| 1066 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1067 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1068 | Concrete field labels may be an identifier or string, the latter of which may be |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1069 | interpolated. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1070 | Fields with identifier labels can be referred to within the scope they are |
| 1071 | defined, string labels cannot. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1072 | References within such interpolated strings are resolved within |
| 1073 | the scope of the struct in which the label sequence is |
| 1074 | defined and can reference concrete labels lexically preceding |
| 1075 | the label within a label sequence. |
| 1076 | <!-- We allow this so that rewriting a CUE file to collapse or expand |
| 1077 | field sequences has no impact on semantics. |
| 1078 | --> |
| 1079 | |
| 1080 | <!--TODO: first implementation round will not yet have expression labels |
| 1081 | |
| 1082 | An ExpressionLabel sets a collection of optional fields to a field value. |
| 1083 | By default it defines this value for all possible string labels. |
| 1084 | An optional expression limits this to the set of optional fields which |
| 1085 | labels match the expression. |
| 1086 | --> |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1087 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1088 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1089 | <!-- NOTE: if we allow ...Expr, as in list, it would mean something different. --> |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1090 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1091 | |
| 1092 | <!-- NOTE: |
| 1093 | A DefinitionDecl does not allow repeated labels. This is to avoid |
| 1094 | any ambiguity or confusion about whether earlier path components |
| 1095 | are to be interpreted as declarations or normal fields (they should |
| 1096 | always be normal fields.) |
| 1097 | --> |
| 1098 | |
| 1099 | <!--NOTE: |
| 1100 | The syntax has been deliberately restricted to allow for the following |
| 1101 | future extensions and relaxations: |
| 1102 | - Allow omitting a "?" in an expression label to indicate a concrete |
| 1103 | string value (but maybe we want to use () for that). |
| 1104 | - Make the "?" in expression label optional if expression labels |
| 1105 | are always optional. |
| 1106 | - Or allow eliding the "?" if the expression has no references and |
| 1107 | is obviously not concrete (such as `[string]`). |
| 1108 | - The expression of an expression label may also indicate a struct with |
| 1109 | integer or even number labels |
| 1110 | (beware of imprecise computation in the latter). |
| 1111 | e.g. `{ [int]: string }` is a map of integers to strings. |
| 1112 | - Allow for associative lists (`foo [@.field]: {field: string}`) |
| 1113 | - The `...` notation can be extended analogously to that of a ListList, |
| 1114 | by allowing it to follow with an expression for the remaining properties. |
| 1115 | In that case it is no longer a shorthand for `[string]: _`, but rather |
| 1116 | would define the value for any other value for which there is no field |
| 1117 | defined. |
| 1118 | Like the definition with List, this is somewhat odd, but it allows the |
| 1119 | encoding of JSON schema's and (non-structural) OpenAPI's |
| 1120 | additionalProperties and additionalItems. |
| 1121 | --> |
| 1122 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1123 | ``` |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 1124 | StructLit = "{" { Declaration "," } [ "..." ] "}" . |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1125 | Declaration = Field | Comprehension | AliasExpr | attribute . |
| 1126 | Field = LabelSpec { LabelSpec } Expression { attribute } . |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1127 | LabelSpec = Label ( ":" | "::" ) . |
| 1128 | Label = LabelName [ "?" ] | "[" AliasExpr "]". |
| 1129 | LabelName = identifier | simple_string_lit . |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1130 | |
Marcel van Lohuizen | 4d29dde | 2019-12-02 23:11:30 +0100 | [diff] [blame] | 1131 | attribute = "@" identifier "(" attr_tokens ")" . |
| 1132 | attr_tokens = { attr_token | |
| 1133 | "(" attr_tokens ")" | |
| 1134 | "[" attr_tokens "]" | |
| 1135 | "{" attr_tokens "}" } . |
| 1136 | attr_token = /* any token except '(', ')', '[', ']', '{', or '}' */ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1137 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1138 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1139 | <!-- |
| 1140 | TODO: Label = LabelName [ "?" ] | "[" AliasExpr "]" | "(" AliasExpr ")" |
| 1141 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1142 | |
| 1143 | ``` |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1144 | Expression Result (without optional fields) |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1145 | a: { foo?: string } {} |
| 1146 | b: { foo: "bar" } { foo: "bar" } |
| 1147 | c: { foo?: *"bar" | string } {} |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1148 | |
| 1149 | d: a & b { foo: "bar" } |
| 1150 | e: b & c { foo: "bar" } |
| 1151 | f: a & c {} |
| 1152 | g: a & { foo?: number } {} |
| 1153 | h: b & { foo?: number } _|_ |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1154 | i: c & { foo: string } { foo: "bar" } |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1155 | |
| 1156 | intMap: [string]: int |
| 1157 | intMap: { |
| 1158 | t1: 43 |
| 1159 | t2: 2.4 // error: 2.4 is not an integer |
| 1160 | } |
| 1161 | |
| 1162 | nameMap: [string]: { |
| 1163 | firstName: string |
| 1164 | nickName: *firstName | string |
| 1165 | } |
| 1166 | |
| 1167 | nameMap: hank: { firstName: "Hank" } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1168 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1169 | The optional field set defined by `nameMap` matches every field, |
| 1170 | in this case just `hank`, and unifies the associated constraint |
| 1171 | with the matched field, resulting in: |
| 1172 | ``` |
| 1173 | nameMap: hank: { |
| 1174 | firstName: "Hank" |
| 1175 | nickName: "Hank" |
| 1176 | } |
| 1177 | ``` |
| 1178 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1179 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1180 | #### Closed structs |
| 1181 | |
| 1182 | By default, structs are open to adding fields. |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1183 | Instances of an open struct `p` may contain fields not defined in `p`. |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1184 | This is makes it easy to add fields, but can lead to bugs: |
| 1185 | |
| 1186 | ``` |
| 1187 | S: { |
| 1188 | field1: string |
| 1189 | } |
| 1190 | |
| 1191 | S1: S & { field2: "foo" } |
| 1192 | |
| 1193 | // S1 is { field1: string, field2: "foo" } |
| 1194 | |
| 1195 | |
| 1196 | A: { |
| 1197 | field1: string |
| 1198 | field2: string |
| 1199 | } |
| 1200 | |
| 1201 | A1: A & { |
| 1202 | feild1: "foo" // "field1" was accidentally misspelled |
| 1203 | } |
| 1204 | |
| 1205 | // A1 is |
| 1206 | // { field1: string, field2: string, feild1: "foo" } |
| 1207 | // not the intended |
| 1208 | // { field1: "foo", field2: string } |
| 1209 | ``` |
| 1210 | |
Marcel van Lohuizen | 18637db | 2019-09-03 11:48:25 +0200 | [diff] [blame] | 1211 | A _closed struct_ `c` is a struct whose instances may not have regular fields |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1212 | not defined in `c`. |
Marcel van Lohuizen | 4245fb4 | 2019-09-09 11:22:12 +0200 | [diff] [blame] | 1213 | Closing a struct is equivalent to adding an optional field with value `_|_` |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1214 | for all undefined fields. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1215 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1216 | Syntactically, closed structs can be explicitly created with the `close` builtin |
| 1217 | or implicitly by [definitions](#Definitions). |
| 1218 | |
| 1219 | |
| 1220 | ``` |
| 1221 | A: close({ |
| 1222 | field1: string |
| 1223 | field2: string |
| 1224 | }) |
| 1225 | |
| 1226 | A1: A & { |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1227 | feild1: string |
| 1228 | } // _|_ feild1 not defined for A |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1229 | |
| 1230 | A2: A & { |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1231 | for k,v in { feild1: string } { |
| 1232 | k: v |
| 1233 | } |
| 1234 | } // _|_ feild1 not defined for A |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1235 | |
| 1236 | C: close({ |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1237 | [_]: _ |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1238 | }) |
| 1239 | |
| 1240 | C2: C & { |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1241 | for k,v in { thisIsFine: string } { |
| 1242 | "\(k)": v |
| 1243 | } |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | D: close({ |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1247 | // Values generated by comprehensions are treated as embeddings. |
| 1248 | for k,v in { x: string } { |
| 1249 | "\(k)": v |
| 1250 | } |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1251 | }) |
| 1252 | ``` |
| 1253 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1254 | <!-- (jba) Somewhere it should be said that optional fields are only |
| 1255 | interesting inside closed structs. --> |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1256 | |
| 1257 | #### Embedding |
| 1258 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1259 | A struct may contain an _embedded value_, an operand used |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1260 | as a declaration, which must evaluate to a struct. |
| 1261 | An embedded value of type struct is unified with the struct in which it is |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1262 | embedded, but disregarding the restrictions imposed by closed structs. |
| 1263 | A struct resulting from such a unification is closed if either of the involved |
| 1264 | structs were closed. |
| 1265 | |
Marcel van Lohuizen | a3c7bef | 2019-10-10 21:50:58 +0200 | [diff] [blame] | 1266 | At the top level, an embedded value may be any type. |
| 1267 | In this case, a CUE program will evaluate to the embedded value |
| 1268 | and the CUE program may not have top-level regular or optional |
| 1269 | fields (definitions and aliases are allowed). |
| 1270 | |
Marcel van Lohuizen | e53305e | 2019-09-13 10:10:31 +0200 | [diff] [blame] | 1271 | Syntactically, embeddings may be any expression, except that `<` |
| 1272 | is eagerly interpreted as a bind label. |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 1273 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1274 | ``` |
| 1275 | S1: { |
| 1276 | a: 1 |
| 1277 | b: 2 |
| 1278 | { |
| 1279 | c: 3 |
| 1280 | } |
| 1281 | } |
| 1282 | // S1 is { a: 1, b: 2, c: 3 } |
| 1283 | |
| 1284 | S2: close({ |
| 1285 | a: 1 |
| 1286 | b: 2 |
| 1287 | { |
| 1288 | c: 3 |
| 1289 | } |
| 1290 | }) |
| 1291 | // same as close(S1) |
| 1292 | |
| 1293 | S3: { |
| 1294 | a: 1 |
| 1295 | b: 2 |
| 1296 | close({ |
| 1297 | c: 3 |
| 1298 | }) |
| 1299 | } |
| 1300 | // same as S2 |
| 1301 | ``` |
| 1302 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1303 | |
| 1304 | #### Definitions |
| 1305 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1306 | A field of a struct may be declared as a regular field (using `:`) |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1307 | or as a _definition_ (using `::`). |
| 1308 | Definitions are not emitted as part of the model and are never required |
| 1309 | to be concrete when emitting data. |
Marcel van Lohuizen | 18637db | 2019-09-03 11:48:25 +0200 | [diff] [blame] | 1310 | It is illegal to have a regular field and a definition with the same name |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1311 | within the same struct. |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1312 | Literal structs that are part of a definition's value are implicitly closed, |
| 1313 | but may unify unrestricted with other structs within the field's declaration. |
Marcel van Lohuizen | 5e8c391 | 2019-09-03 15:46:26 +0200 | [diff] [blame] | 1314 | This excludes literals structs in embeddings and aliases. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 1315 | |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1316 | <!-- |
| 1317 | This may be a more intuitive definition: |
| 1318 | Literal structs that are part of a definition's value are implicitly closed. |
| 1319 | Implicitly closed literal structs that are unified within |
| 1320 | a single field declaration are considered to be a single literal struct. |
| 1321 | However, this would make unification non-commutative, unless one imposes an |
| 1322 | ordering where literal structs are unified before unifying them with others. |
| 1323 | Imposing such an ordering is complex and error prone. |
| 1324 | --> |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1325 | An ellipsis `...` in such literal structs keeps them open, |
| 1326 | as it defines `_` for all labels. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 1327 | |
Marcel van Lohuizen | 5e8c391 | 2019-09-03 15:46:26 +0200 | [diff] [blame] | 1328 | <!-- |
| 1329 | Excluding embeddings from recursive closing allows comprehensions to be |
| 1330 | interpreted as embeddings without some exception. For instance, |
| 1331 | if x > 2 { |
| 1332 | foo: string |
| 1333 | } |
| 1334 | should not cause any failure. It is also consistent with embeddings being |
| 1335 | opened when included in a closed struct. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1336 | |
Marcel van Lohuizen | 5e8c391 | 2019-09-03 15:46:26 +0200 | [diff] [blame] | 1337 | Finally, excluding embeddings from recursive closing allows for |
| 1338 | a mechanism to not recursively close, without needing an additional language |
| 1339 | construct, such as a triple colon or something else: |
| 1340 | foo :: { |
| 1341 | { |
| 1342 | // not recursively closed |
| 1343 | } |
| 1344 | ... // include this to not close outer struct |
| 1345 | } |
| 1346 | |
| 1347 | Including aliases from this exclusion, which are more a separate definition |
| 1348 | than embedding seems sensible, and allows for an easy mechanism to avoid |
| 1349 | closing, aside from embedding. |
| 1350 | --> |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1351 | |
| 1352 | ``` |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1353 | MyStruct :: { |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1354 | sub field: string |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1357 | MyStruct :: { |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1358 | sub enabled?: bool |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | myValue: MyStruct & { |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1362 | sub feild: 2 // error, feild not defined in MyStruct |
| 1363 | sub enabled: true // okay |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | D :: { |
| 1367 | OneOf |
| 1368 | |
| 1369 | c: int // adds this field. |
| 1370 | } |
| 1371 | |
| 1372 | OneOf :: { a: int } | { b: int } |
| 1373 | |
| 1374 | |
| 1375 | D1: D & { a: 12, c: 22 } // { a: 12, c: 22 } |
| 1376 | D2: D & { a: 12, b: 33 } // _|_ // cannot define both `a` and `b` |
| 1377 | ``` |
| 1378 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1379 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1380 | <!--- |
| 1381 | JSON fields are usual camelCase. Clashes can be avoided by adopting the |
| 1382 | convention that definitions be TitleCase. Unexported definitions are still |
| 1383 | subject to clashes, but those are likely easier to resolve because they are |
| 1384 | package internal. |
| 1385 | ---> |
| 1386 | |
| 1387 | |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1388 | #### Attributes |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1389 | |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1390 | Attributes allow associating meta information with values. |
| 1391 | Their primary purpose is to define mappings between CUE and |
| 1392 | other representations. |
| 1393 | Attributes do not influence the evaluation of CUE. |
| 1394 | |
| 1395 | An attribute associates an identifier with a value, a balanced token sequence, |
| 1396 | which is a sequence of CUE tokens with balanced brackets (`()`, `[]`, and `{}`). |
| 1397 | The sequence may not contain interpolations. |
| 1398 | |
| 1399 | Fields, structs and packages can be associated with a set of attributes. |
| 1400 | Attributes accumulate during unification, but implementations may remove |
| 1401 | duplicates that have the same source string representation. |
| 1402 | The interpretation of an attribute, including the handling of multiple |
| 1403 | attributes for a given identifier, is up to the consumer of the attribute. |
| 1404 | |
| 1405 | Field attributes define additional information about a field, |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1406 | such as a mapping to a protocol buffer <!-- TODO: add link --> tag or alternative |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1407 | name of the field when mapping to a different language. |
| 1408 | |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1409 | |
| 1410 | ``` |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1411 | // Package attribute |
| 1412 | @protobuf(proto3) |
| 1413 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1414 | myStruct1: { |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1415 | // Struct attribute: |
| 1416 | @jsonschema(id="https://example.org/mystruct1.json") |
| 1417 | |
| 1418 | // Field attributes |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1419 | field: string @go(Field) |
| 1420 | attr: int @xml(,attr) @go(Attr) |
| 1421 | } |
| 1422 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1423 | myStruct2: { |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1424 | field: string @go(Field) |
| 1425 | attr: int @xml(a1,attr) @go(Attr) |
| 1426 | } |
| 1427 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1428 | Combined: myStruct1 & myStruct2 |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1429 | // field: string @go(Field) |
| 1430 | // attr: int @xml(,attr) @xml(a1,attr) @go(Attr) |
| 1431 | ``` |
| 1432 | |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1433 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1434 | #### Aliases |
| 1435 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1436 | Aliases name values that can be referred to |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1437 | within the [scope](#declarations-and-scopes) in which they are declared. |
| 1438 | The name of an alias must be unique within its scope. |
| 1439 | |
| 1440 | ``` |
| 1441 | AliasExpr = identifier "=" Expression | Expression . |
| 1442 | ``` |
| 1443 | |
| 1444 | Aliases can appear in several positions: |
| 1445 | |
| 1446 | As a declaration in a struct (`X=expr`): |
| 1447 | |
| 1448 | - binds the value to an identifier without including it in the struct. |
| 1449 | |
| 1450 | In front of a Label (`X=label: value`): |
| 1451 | |
| 1452 | - binds the identifier to the same value as `label` would be bound |
| 1453 | to if it were a valid identifier. |
| 1454 | - for optional fields (`foo?: bar` and `[foo]: bar`), |
| 1455 | the bound identifier is only visible within the field value (`value`). |
| 1456 | |
| 1457 | Inside a bracketed label (`[X=expr]: value`): |
| 1458 | |
| 1459 | - binds the identifier to the the concrete label that matches `expr` |
| 1460 | within the instances of the field value (`value`). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1461 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1462 | <!-- TODO: explain the difference between aliases and definitions. |
| 1463 | Now that you have definitions, are aliases really necessary? |
| 1464 | Consider removing. |
| 1465 | --> |
| 1466 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1467 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1468 | // An alias declaration |
| 1469 | Alias = 3 |
| 1470 | a: Alias // 3 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1471 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1472 | // A field alias |
| 1473 | foo: X // 4 |
| 1474 | X="not an identifier": 4 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1475 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1476 | // A label alias |
| 1477 | [Y=string]: { name: Y } |
| 1478 | foo: { value: 1 } // outputs: foo: { name: "foo", value: 1 } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1479 | ``` |
| 1480 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1481 | <!-- TODO: also allow aliases as lists --> |
| 1482 | |
| 1483 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1484 | #### Shorthand notation for nested structs |
| 1485 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1486 | A field whose value is a struct with a single field may be written as |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1487 | a colon-separated sequence of the two field names, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1488 | followed by a colon and the value of that single field. |
| 1489 | |
| 1490 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1491 | job: myTask: replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1492 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1493 | expands to |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1494 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1495 | job: { |
| 1496 | myTask: { |
| 1497 | replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | ``` |
| 1501 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1502 | <!-- OPTIONAL FIELDS: |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1503 | |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 1504 | The optional marker solves the issue of having to print large amounts of |
| 1505 | boilerplate when dealing with large types with many optional or default |
| 1506 | values (such as Kubernetes). |
| 1507 | Writing such optional values in terms of *null | value is tedious, |
| 1508 | unpleasant to read, and as it is not well defined what can be dropped or not, |
| 1509 | all null values have to be emitted from the output, even if the user |
| 1510 | doesn't override them. |
| 1511 | Part of the issue is how null is defined. We could adopt a Typescript-like |
| 1512 | approach of introducing "void" or "undefined" to mean "not defined and not |
| 1513 | part of the output". But having all of null, undefined, and void can be |
| 1514 | confusing. If these ever are introduced anyway, the ? operator could be |
| 1515 | expressed along the lines of |
| 1516 | foo?: bar |
| 1517 | being a shorthand for |
| 1518 | foo: void | bar |
| 1519 | where void is the default if no other default is given. |
| 1520 | |
| 1521 | The current mechanical definition of "?" is straightforward, though, and |
| 1522 | probably avoids the need for void, while solving a big issue. |
| 1523 | |
| 1524 | Caveats: |
| 1525 | [1] this definition requires explicitly defined fields to be emitted, even |
| 1526 | if they could be elided (for instance if the explicit value is the default |
| 1527 | value defined an optional field). This is probably a good thing. |
| 1528 | |
| 1529 | [2] a default value may still need to be included in an output if it is not |
| 1530 | the zero value for that field and it is not known if any outside system is |
| 1531 | aware of defaults. For instance, which defaults are specified by the user |
| 1532 | and which by the schema understood by the receiving system. |
| 1533 | The use of "?" together with defaults should therefore be used carefully |
| 1534 | in non-schema definitions. |
| 1535 | Problematic cases should be easy to detect by a vet-like check, though. |
| 1536 | |
| 1537 | [3] It should be considered how this affects the trim command. |
| 1538 | Should values implied by optional fields be allowed to be removed? |
| 1539 | Probably not. This restriction is unlikely to limit the usefulness of trim, |
| 1540 | though. |
| 1541 | |
| 1542 | [4] There should be an option to emit all concrete optional values. |
| 1543 | ``` |
| 1544 | --> |
| 1545 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1546 | ### Lists |
| 1547 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1548 | A list literal defines a new value of type list. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1549 | A list may be open or closed. |
| 1550 | An open list is indicated with a `...` at the end of an element list, |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1551 | optionally followed by a value for the remaining elements. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1552 | |
| 1553 | The length of a closed list is the number of elements it contains. |
| 1554 | The length of an open list is the its number of elements as a lower bound |
| 1555 | and an unlimited number of elements as its upper bound. |
| 1556 | |
| 1557 | ``` |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 1558 | ListLit = "[" [ ElementList [ "," [ "..." [ Expression ] ] ] "]" . |
| 1559 | ElementList = Expression { "," Expression } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1560 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1561 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1562 | Lists can be thought of as structs: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1563 | |
| 1564 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1565 | List: *null | { |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1566 | Elem: _ |
| 1567 | Tail: List |
| 1568 | } |
| 1569 | ``` |
| 1570 | |
| 1571 | For closed lists, `Tail` is `null` for the last element, for open lists it is |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1572 | `*null | List`, defaulting to the shortest variant. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1573 | For instance, the open list [ 1, 2, ... ] can be represented as: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1574 | ``` |
| 1575 | open: List & { Elem: 1, Tail: { Elem: 2 } } |
| 1576 | ``` |
| 1577 | and the closed version of this list, [ 1, 2 ], as |
| 1578 | ``` |
| 1579 | closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } } |
| 1580 | ``` |
| 1581 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1582 | Using this representation, the subsumption rule for lists can |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1583 | be derived from those of structs. |
| 1584 | Implementations are not required to implement lists as structs. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1585 | The `Elem` and `Tail` fields are not special and `len` will not work as |
| 1586 | expected in these cases. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1587 | |
| 1588 | |
| 1589 | ## Declarations and Scopes |
| 1590 | |
| 1591 | |
| 1592 | ### Blocks |
| 1593 | |
| 1594 | A _block_ is a possibly empty sequence of declarations. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1595 | The braces of a struct literal `{ ... }` form a block, but there are |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1596 | others as well: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1597 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1598 | - The _universe block_ encompasses all CUE source text. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1599 | - Each [package](#modules-instances-and-packages) has a _package block_ |
| 1600 | containing all CUE source text in that package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1601 | - Each file has a _file block_ containing all CUE source text in that file. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1602 | - Each `for` and `let` clause in a [comprehension](#comprehensions) |
| 1603 | is considered to be its own implicit block. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1604 | |
| 1605 | Blocks nest and influence [scoping]. |
| 1606 | |
| 1607 | |
| 1608 | ### Declarations and scope |
| 1609 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1610 | A _declaration_ may bind an identifier to a field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1611 | Every identifier in a program must be declared. |
| 1612 | Other than for fields, |
| 1613 | no identifier may be declared twice within the same block. |
| 1614 | For fields an identifier may be declared more than once within the same block, |
| 1615 | resulting in a field with a value that is the result of unifying the values |
| 1616 | of all fields with the same identifier. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1617 | String labels do not bind an identifier to the respective field. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1618 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1619 | The _scope_ of a declared identifier is the extent of source text in which the |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1620 | identifier denotes the specified field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1621 | |
| 1622 | CUE is lexically scoped using blocks: |
| 1623 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1624 | 1. The scope of a [predeclared identifier](#predeclared-identifiers) is the universe block. |
Marcel van Lohuizen | 21f6c44 | 2019-09-26 14:55:23 +0200 | [diff] [blame] | 1625 | 1. The scope of an identifier denoting a field |
| 1626 | declared at top level (outside any struct literal) is the package block. |
| 1627 | 1. The scope of an identifier denoting an alias |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1628 | declared at top level (outside any struct literal) is the file block. |
| 1629 | 1. The scope of the package name of an imported package is the file block of the |
| 1630 | file containing the import declaration. |
| 1631 | 1. The scope of a field or alias identifier declared inside a struct literal |
| 1632 | is the innermost containing block. |
| 1633 | |
| 1634 | An identifier declared in a block may be redeclared in an inner block. |
| 1635 | While the identifier of the inner declaration is in scope, it denotes the entity |
| 1636 | declared by the inner declaration. |
| 1637 | |
| 1638 | The package clause is not a declaration; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1639 | the package name does not appear in any scope. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1640 | Its purpose is to identify the files belonging to the same package |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1641 | and to specify the default name for import declarations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1642 | |
| 1643 | |
| 1644 | ### Predeclared identifiers |
| 1645 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1646 | CUE predefines a set of types and builtin functions. |
| 1647 | For each of these there is a corresponding keyword which is the name |
| 1648 | of the predefined identifier, prefixed with `__`. |
| 1649 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1650 | ``` |
| 1651 | Functions |
| 1652 | len required close open |
| 1653 | |
| 1654 | Types |
| 1655 | null The null type and value |
| 1656 | bool All boolean values |
| 1657 | int All integral numbers |
| 1658 | float All decimal floating-point numbers |
| 1659 | string Any valid UTF-8 sequence |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 1660 | bytes Any valid byte sequence |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1661 | |
| 1662 | Derived Value |
| 1663 | number int | float |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1664 | uint >=0 |
| 1665 | uint8 >=0 & <=255 |
| 1666 | int8 >=-128 & <=127 |
| 1667 | uint16 >=0 & <=65536 |
| 1668 | int16 >=-32_768 & <=32_767 |
| 1669 | rune >=0 & <=0x10FFFF |
| 1670 | uint32 >=0 & <=4_294_967_296 |
| 1671 | int32 >=-2_147_483_648 & <=2_147_483_647 |
| 1672 | uint64 >=0 & <=18_446_744_073_709_551_615 |
| 1673 | int64 >=-9_223_372_036_854_775_808 & <=9_223_372_036_854_775_807 |
| 1674 | uint128 >=0 & <=340_282_366_920_938_463_463_374_607_431_768_211_455 |
| 1675 | int128 >=-170_141_183_460_469_231_731_687_303_715_884_105_728 & |
| 1676 | <=170_141_183_460_469_231_731_687_303_715_884_105_727 |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 1677 | float32 >=-3.40282346638528859811704183484516925440e+38 & |
| 1678 | <=3.40282346638528859811704183484516925440e+38 |
| 1679 | float64 >=-1.797693134862315708145274237317043567981e+308 & |
| 1680 | <=1.797693134862315708145274237317043567981e+308 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1681 | ``` |
| 1682 | |
| 1683 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1684 | ### Exported identifiers |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1685 | |
| 1686 | An identifier of a package may be exported to permit access to it |
| 1687 | from another package. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1688 | An identifier is exported if |
| 1689 | the first character of the identifier's name is a Unicode upper case letter |
| 1690 | (Unicode class "Lu"); and |
| 1691 | the identifier is declared in the file block. |
| 1692 | All other top-level identifiers used for fields not exported. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1693 | |
| 1694 | In addition, any definition declared anywhere within a package of which |
| 1695 | the first character of the identifier's name is a Unicode upper case letter |
| 1696 | (Unicode class "Lu") is visible outside this package. |
| 1697 | Any other defintion is not visible outside the package and resides |
| 1698 | in a separate namespace than namesake identifiers of other packages. |
| 1699 | This is in contrast to ordinary field declarations that do not begin with |
| 1700 | an upper-case letter, which are visible outside the package. |
| 1701 | |
| 1702 | ``` |
| 1703 | package mypackage |
| 1704 | |
| 1705 | foo: string // not visible outside mypackage |
| 1706 | |
Marcel van Lohuizen | 21f6c44 | 2019-09-26 14:55:23 +0200 | [diff] [blame] | 1707 | Foo :: { // visible outside mypackage |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1708 | a: 1 // visible outside mypackage |
| 1709 | B: 2 // visible outside mypackage |
| 1710 | |
| 1711 | C :: { // visible outside mypackage |
| 1712 | d: 4 // visible outside mypackage |
| 1713 | } |
| 1714 | e :: foo // not visible outside mypackage |
| 1715 | } |
| 1716 | ``` |
| 1717 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1718 | |
| 1719 | ### Uniqueness of identifiers |
| 1720 | |
| 1721 | Given a set of identifiers, an identifier is called unique if it is different |
| 1722 | from every other in the set, after applying normalization following |
| 1723 | Unicode Annex #31. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1724 | Two identifiers are different if they are spelled differently |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1725 | or if they appear in different packages and are not exported. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1726 | Otherwise, they are the same. |
| 1727 | |
| 1728 | |
| 1729 | ### Field declarations |
| 1730 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1731 | A field associates the value of an expression to a label within a struct. |
| 1732 | If this label is an identifier, it binds the field to that identifier, |
| 1733 | so the field's value can be referenced by writing the identifier. |
| 1734 | String labels are not bound to fields. |
| 1735 | ``` |
| 1736 | a: { |
| 1737 | b: 2 |
| 1738 | "s": 3 |
| 1739 | |
| 1740 | c: b // 2 |
| 1741 | d: s // _|_ unresolved identifier "s" |
| 1742 | e: a.s // 3 |
| 1743 | } |
| 1744 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1745 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1746 | If an expression may result in a value associated with a default value |
| 1747 | as described in [default values](#default-values), the field binds to this |
| 1748 | value-default pair. |
| 1749 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1750 | |
Marcel van Lohuizen | bcf832f | 2019-04-03 22:50:44 +0200 | [diff] [blame] | 1751 | <!-- TODO: disallow creating identifiers starting with __ |
| 1752 | ...and reserve them for builtin values. |
| 1753 | |
| 1754 | The issue is with code generation. As no guarantee can be given that |
| 1755 | a predeclared identifier is not overridden in one of the enclosing scopes, |
| 1756 | code will have to handle detecting such cases and renaming them. |
| 1757 | An alternative is to have the predeclared identifiers be aliases for namesake |
| 1758 | equivalents starting with a double underscore (e.g. string -> __string), |
| 1759 | allowing generated code (normal code would keep using `string`) to refer |
| 1760 | to these directly. |
| 1761 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1762 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1763 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1764 | ### Alias declarations |
| 1765 | |
| 1766 | An alias declaration binds an identifier to the given expression. |
| 1767 | |
| 1768 | Within the scope of the identifier, it serves as an _alias_ for that |
| 1769 | expression. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1770 | The expression is evaluated in the scope it was declared. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1771 | |
| 1772 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1773 | ## Expressions |
| 1774 | |
| 1775 | An expression specifies the computation of a value by applying operators and |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1776 | built-in functions to operands. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1777 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1778 | Expressions that require concrete values are called _incomplete_ if any of |
| 1779 | their operands are not concrete, but define a value that would be legal for |
| 1780 | that expression. |
| 1781 | Incomplete expressions may be left unevaluated until a concrete value is |
| 1782 | requested at the application level. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1783 | |
| 1784 | ### Operands |
| 1785 | |
| 1786 | Operands denote the elementary values in an expression. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1787 | An operand may be a literal, a (possibly qualified) identifier denoting |
| 1788 | field, alias, or a parenthesized expression. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1789 | |
| 1790 | ``` |
| 1791 | Operand = Literal | OperandName | ListComprehension | "(" Expression ")" . |
| 1792 | Literal = BasicLit | ListLit | StructLit . |
| 1793 | BasicLit = int_lit | float_lit | string_lit | |
| 1794 | null_lit | bool_lit | bottom_lit | top_lit . |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1795 | OperandName = identifier | QualifiedIdent . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1796 | ``` |
| 1797 | |
| 1798 | ### Qualified identifiers |
| 1799 | |
| 1800 | A qualified identifier is an identifier qualified with a package name prefix. |
| 1801 | |
| 1802 | ``` |
| 1803 | QualifiedIdent = PackageName "." identifier . |
| 1804 | ``` |
| 1805 | |
| 1806 | A qualified identifier accesses an identifier in a different package, |
| 1807 | which must be [imported]. |
| 1808 | The identifier must be declared in the [package block] of that package. |
| 1809 | |
| 1810 | ``` |
| 1811 | math.Sin // denotes the Sin function in package math |
| 1812 | ``` |
| 1813 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1814 | ### References |
| 1815 | |
| 1816 | An identifier operand refers to a field and is called a reference. |
| 1817 | The value of a reference is a copy of the expression associated with the field |
| 1818 | that it is bound to, |
| 1819 | with any references within that expression bound to the respective copies of |
| 1820 | the fields they were originally bound to. |
| 1821 | Implementations may use a different mechanism to evaluate as long as |
| 1822 | these semantics are maintained. |
| 1823 | |
| 1824 | ``` |
| 1825 | a: { |
| 1826 | place: string |
| 1827 | greeting: "Hello, \(place)!" |
| 1828 | } |
| 1829 | |
| 1830 | b: a & { place: "world" } |
| 1831 | c: a & { place: "you" } |
| 1832 | |
| 1833 | d: b.greeting // "Hello, world!" |
| 1834 | e: c.greeting // "Hello, you!" |
| 1835 | ``` |
| 1836 | |
| 1837 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1838 | |
| 1839 | ### Primary expressions |
| 1840 | |
| 1841 | Primary expressions are the operands for unary and binary expressions. |
| 1842 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1843 | |
| 1844 | ``` |
| 1845 | |
| 1846 | Slice: indices must be complete |
| 1847 | ([0, 1, 2, 3] | [2, 3])[0:2] => [0, 1] | [2, 3] |
| 1848 | |
| 1849 | ([0, 1, 2, 3] | *[2, 3])[0:2] => [0, 1] | [2, 3] |
| 1850 | ([0,1,2,3]|[2,3], [2,3])[0:2] => ([0,1]|[2,3], [2,3]) |
| 1851 | |
| 1852 | Index |
| 1853 | a: (1|2, 1) |
| 1854 | b: ([0,1,2,3]|[2,3], [2,3])[a] => ([0,1,2,3]|[2,3][a], 3) |
| 1855 | |
| 1856 | Binary operation |
| 1857 | A binary is only evaluated if its operands are complete. |
| 1858 | |
| 1859 | Input Maximum allowed evaluation |
| 1860 | a: string string |
| 1861 | b: 2 2 |
| 1862 | c: a * b a * 2 |
| 1863 | |
| 1864 | An error in a struct is if the evaluation of any expression results in |
| 1865 | bottom, where an incomplete expression is not considered bottom. |
| 1866 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1867 | <!-- TODO(mpvl) |
| 1868 | Conversion | |
| 1869 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1870 | ``` |
| 1871 | PrimaryExpr = |
| 1872 | Operand | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1873 | PrimaryExpr Selector | |
| 1874 | PrimaryExpr Index | |
| 1875 | PrimaryExpr Slice | |
| 1876 | PrimaryExpr Arguments . |
| 1877 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1878 | Selector = "." (identifier | simple_string_lit) . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1879 | Index = "[" Expression "]" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1880 | Argument = Expression . |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1881 | Arguments = "(" [ ( Argument { "," Argument } ) [ "," ] ] ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1882 | ``` |
| 1883 | <!--- |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1884 | TODO: |
| 1885 | PrimaryExpr Query | |
| 1886 | Query = "." Filters . |
| 1887 | Filters = Filter { Filter } . |
| 1888 | Filter = "[" [ "?" ] AliasExpr "]" . |
| 1889 | |
| 1890 | TODO: maybe reintroduce slices, as they are useful in queries, probably this |
| 1891 | time with Python semantics. |
| 1892 | Slice = "[" [ Expression ] ":" [ Expression ] [ ":" [Expression] ] "]" . |
| 1893 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1894 | Argument = Expression | ( identifer ":" Expression ). |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1895 | |
| 1896 | // & expression type |
| 1897 | // string_lit: same as label. Arguments is current node. |
| 1898 | // If selector is applied to list, it performs the operation for each |
| 1899 | // element. |
| 1900 | |
| 1901 | TODO: considering allowing decimal_lit for selectors. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1902 | ---> |
| 1903 | |
| 1904 | ``` |
| 1905 | x |
| 1906 | 2 |
| 1907 | (s + ".txt") |
| 1908 | f(3.1415, true) |
| 1909 | m["foo"] |
| 1910 | s[i : j + 1] |
| 1911 | obj.color |
| 1912 | f.p[i].x |
| 1913 | ``` |
| 1914 | |
| 1915 | |
| 1916 | ### Selectors |
| 1917 | |
Roger Peppe | ded0e1d | 2019-09-24 16:39:36 +0100 | [diff] [blame] | 1918 | For a [primary expression](#primary-expressions) `x` that is not a [package name](#package-clause), |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1919 | the selector expression |
| 1920 | |
| 1921 | ``` |
| 1922 | x.f |
| 1923 | ``` |
| 1924 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1925 | denotes the element of a <!--list or -->struct `x` identified by `f`. |
| 1926 | <!--For structs, -->`f` must be an identifier or a string literal identifying |
| 1927 | any definition or regular non-optional field. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1928 | The identifier `f` is called the field selector. |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1929 | |
| 1930 | <!-- |
| 1931 | Allowing strings to be used as field selectors obviates the need for |
| 1932 | backquoted identifiers. Note that some standards use names for structs that |
| 1933 | are not standard identifiers (such "Fn::Foo"). Note that indexing does not |
| 1934 | allow access to identifiers. |
| 1935 | --> |
| 1936 | |
| 1937 | <!-- |
| 1938 | For lists, `f` must be an integer and follows the same lookup rules as |
| 1939 | for the index operation. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1940 | The type of the selector expression is the type of `f`. |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1941 | --> |
| 1942 | |
Roger Peppe | ded0e1d | 2019-09-24 16:39:36 +0100 | [diff] [blame] | 1943 | If `x` is a package name, see the section on [qualified identifiers](#qualified-identifiers). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1944 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1945 | <!-- |
| 1946 | TODO: consider allowing this and also for selectors. It needs to be considered |
| 1947 | how defaults are corried forward in cases like: |
| 1948 | |
| 1949 | x: { a: string | *"foo" } | *{ a: int | *4 } |
| 1950 | y: x.a & string |
| 1951 | |
| 1952 | What is y in this case? |
| 1953 | (x.a & string, _|_) |
| 1954 | (string|"foo", _|_) |
| 1955 | (string|"foo", "foo) |
| 1956 | If the latter, then why? |
| 1957 | |
| 1958 | For a disjunction of the form `x1 | ... | xn`, |
| 1959 | the selector is applied to each element `x1.f | ... | xn.f`. |
| 1960 | --> |
| 1961 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1962 | Otherwise, if `x` is not a <!--list or -->struct, |
| 1963 | or if `f` does not exist in `x`, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1964 | the result of the expression is bottom (an error). |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1965 | In the latter case the expression is incomplete. |
| 1966 | The operand of a selector may be associated with a default. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1967 | |
| 1968 | ``` |
| 1969 | T: { |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1970 | x: int |
| 1971 | y: 3 |
| 1972 | "x-y": 4 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1973 | } |
| 1974 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1975 | a: T.x // int |
| 1976 | b: T.y // 3 |
| 1977 | c: T.z // _|_ // field 'z' not found in T |
| 1978 | d: T."x-y" // 4 |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1979 | |
| 1980 | e: {a: 1|*2} | *{a: 3|*4} |
| 1981 | f: e.a // 4 (default value) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1982 | ``` |
| 1983 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1984 | <!-- |
| 1985 | ``` |
| 1986 | (v, d).f => (v.f, d.f) |
| 1987 | |
| 1988 | e: {a: 1|*2} | *{a: 3|*4} |
| 1989 | f: e.a // 4 after selecting default from (({a: 1|*2} | {a: 3|*4}).a, 4) |
| 1990 | |
| 1991 | ``` |
| 1992 | --> |
| 1993 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1994 | |
| 1995 | ### Index expressions |
| 1996 | |
| 1997 | A primary expression of the form |
| 1998 | |
| 1999 | ``` |
| 2000 | a[x] |
| 2001 | ``` |
| 2002 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2003 | denotes the element of a list or struct `a` indexed by `x`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2004 | The value `x` is called the index or field name, respectively. |
| 2005 | The following rules apply: |
| 2006 | |
| 2007 | If `a` is not a struct: |
| 2008 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2009 | - `a` is a list (which need not be complete) |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2010 | - the index `x` unified with `int` must be concrete. |
| 2011 | - the index `x` is in range if `0 <= x < len(a)`, where only the |
| 2012 | explicitly defined values of an open-ended list are considered, |
| 2013 | otherwise it is out of range |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2014 | |
| 2015 | The result of `a[x]` is |
| 2016 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2017 | for `a` of list type: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2018 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2019 | - the list element at index `x`, if `x` is within range |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2020 | - bottom (an error), otherwise |
| 2021 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2022 | |
| 2023 | for `a` of struct type: |
| 2024 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2025 | - the index `x` unified with `string` must be concrete. |
Marcel van Lohuizen | d282553 | 2019-09-23 12:44:01 +0100 | [diff] [blame] | 2026 | - the value of the regular and non-optional field named `x` of struct `a`, |
| 2027 | if this field exists |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2028 | - bottom (an error), otherwise |
| 2029 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2030 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2031 | ``` |
| 2032 | [ 1, 2 ][1] // 2 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2033 | [ 1, 2 ][2] // _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2034 | [ 1, 2, ...][2] // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2035 | ``` |
| 2036 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2037 | Both the operand and index value may be a value-default pair. |
| 2038 | ``` |
| 2039 | va[vi] => va[vi] |
| 2040 | va[(vi, di)] => (va[vi], va[di]) |
| 2041 | (va, da)[vi] => (va[vi], da[vi]) |
| 2042 | (va, da)[(vi, di)] => (va[vi], da[di]) |
| 2043 | ``` |
| 2044 | |
| 2045 | ``` |
| 2046 | Fields Result |
| 2047 | x: [1, 2] | *[3, 4] ([1,2]|[3,4], [3,4]) |
| 2048 | i: int | *1 (int, 1) |
| 2049 | |
| 2050 | v: x[i] (x[i], 4) |
| 2051 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2052 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2053 | ### Operators |
| 2054 | |
| 2055 | Operators combine operands into expressions. |
| 2056 | |
| 2057 | ``` |
| 2058 | Expression = UnaryExpr | Expression binary_op Expression . |
| 2059 | UnaryExpr = PrimaryExpr | unary_op UnaryExpr . |
| 2060 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2061 | binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op . |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 2062 | rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2063 | add_op = "+" | "-" . |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2064 | mul_op = "*" | "/" | "div" | "mod" | "quo" | "rem" . |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 2065 | unary_op = "+" | "-" | "!" | "*" | rel_op . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2066 | ``` |
| 2067 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2068 | Comparisons are discussed [elsewhere](#Comparison-operators). |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 2069 | For any binary operators, the operand types must unify. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2070 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2071 | <!-- TODO: durations |
| 2072 | unless the operation involves durations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | Except for duration operations, if one operand is an untyped [literal] and the |
| 2075 | other operand is not, the constant is [converted] to the type of the other |
| 2076 | operand. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2077 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2078 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2079 | Operands of unary and binary expressions may be associated with a default using |
| 2080 | the following |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2081 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2082 | <!-- |
| 2083 | ``` |
| 2084 | O1: op (v1, d1) => (op v1, op d1) |
| 2085 | |
| 2086 | O2: (v1, d1) op (v2, d2) => (v1 op v2, d1 op d2) |
| 2087 | and because v => (v, v) |
| 2088 | O3: v1 op (v2, d2) => (v1 op v2, v1 op d2) |
| 2089 | O4: (v1, d1) op v2 => (v1 op v2, d1 op v2) |
| 2090 | ``` |
| 2091 | --> |
| 2092 | |
| 2093 | ``` |
| 2094 | Field Resulting Value-Default pair |
| 2095 | a: *1|2 (1|2, 1) |
| 2096 | b: -a (-a, -1) |
| 2097 | |
| 2098 | c: a + 2 (a+2, 3) |
| 2099 | d: a + a (a+a, 2) |
| 2100 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2101 | |
| 2102 | #### Operator precedence |
| 2103 | |
| 2104 | Unary operators have the highest precedence. |
| 2105 | |
| 2106 | There are eight precedence levels for binary operators. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2107 | Multiplication operators binds strongest, followed by |
| 2108 | addition operators, comparison operators, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2109 | `&&` (logical AND), `||` (logical OR), `&` (unification), |
| 2110 | and finally `|` (disjunction): |
| 2111 | |
| 2112 | ``` |
| 2113 | Precedence Operator |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2114 | 7 * / div mod quo rem |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2115 | 6 + - |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 2116 | 5 == != < <= > >= =~ !~ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2117 | 4 && |
| 2118 | 3 || |
| 2119 | 2 & |
| 2120 | 1 | |
| 2121 | ``` |
| 2122 | |
| 2123 | Binary operators of the same precedence associate from left to right. |
| 2124 | For instance, `x / y * z` is the same as `(x / y) * z`. |
| 2125 | |
| 2126 | ``` |
| 2127 | +x |
| 2128 | 23 + 3*x[i] |
| 2129 | x <= f() |
| 2130 | f() || g() |
| 2131 | x == y+1 && y == z-1 |
| 2132 | 2 | int |
| 2133 | { a: 1 } & { b: 2 } |
| 2134 | ``` |
| 2135 | |
| 2136 | #### Arithmetic operators |
| 2137 | |
| 2138 | Arithmetic operators apply to numeric values and yield a result of the same type |
| 2139 | as the first operand. The three of the four standard arithmetic operators |
| 2140 | `(+, -, *)` apply to integer and decimal floating-point types; |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 2141 | `+` and `*` also apply to lists and strings. |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2142 | `/` only applies to decimal floating-point types and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2143 | `div`, `mod`, `quo`, and `rem` only apply to integer types. |
| 2144 | |
| 2145 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2146 | + sum integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2147 | - difference integers, floats |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2148 | * product integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2149 | / quotient floats |
| 2150 | div division integers |
| 2151 | mod modulo integers |
| 2152 | quo quotient integers |
| 2153 | rem remainder integers |
| 2154 | ``` |
| 2155 | |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2156 | For any operator that accepts operands of type `float`, any operand may be |
| 2157 | of type `int` or `float`, in which case the result will be `float` if any |
| 2158 | of the operands is `float` or `int` otherwise. |
| 2159 | For `/` the result is always `float`. |
| 2160 | |
| 2161 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2162 | #### Integer operators |
| 2163 | |
| 2164 | For two integer values `x` and `y`, |
| 2165 | the integer quotient `q = x div y` and remainder `r = x mod y ` |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2166 | implement Euclidean division and |
| 2167 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2168 | |
| 2169 | ``` |
| 2170 | r = x - y*q with 0 <= r < |y| |
| 2171 | ``` |
| 2172 | where `|y|` denotes the absolute value of `y`. |
| 2173 | |
| 2174 | ``` |
| 2175 | x y x div y x mod y |
| 2176 | 5 3 1 2 |
| 2177 | -5 3 -2 1 |
| 2178 | 5 -3 -1 2 |
| 2179 | -5 -3 2 1 |
| 2180 | ``` |
| 2181 | |
| 2182 | For two integer values `x` and `y`, |
| 2183 | the integer quotient `q = x quo y` and remainder `r = x rem y ` |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2184 | implement truncated division and |
| 2185 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2186 | |
| 2187 | ``` |
| 2188 | x = q*y + r and |r| < |y| |
| 2189 | ``` |
| 2190 | |
| 2191 | with `x quo y` truncated towards zero. |
| 2192 | |
| 2193 | ``` |
| 2194 | x y x quo y x rem y |
| 2195 | 5 3 1 2 |
| 2196 | -5 3 -1 -2 |
| 2197 | 5 -3 -1 2 |
| 2198 | -5 -3 1 -2 |
| 2199 | ``` |
| 2200 | |
| 2201 | A zero divisor in either case results in bottom (an error). |
| 2202 | |
| 2203 | For integer operands, the unary operators `+` and `-` are defined as follows: |
| 2204 | |
| 2205 | ``` |
| 2206 | +x is 0 + x |
| 2207 | -x negation is 0 - x |
| 2208 | ``` |
| 2209 | |
| 2210 | |
| 2211 | #### Decimal floating-point operators |
| 2212 | |
| 2213 | For decimal floating-point numbers, `+x` is the same as `x`, |
| 2214 | while -x is the negation of x. |
| 2215 | The result of a floating-point division by zero is bottom (an error). |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2216 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2217 | <!-- TODO: consider making it +/- Inf --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2218 | |
| 2219 | An implementation may combine multiple floating-point operations into a single |
| 2220 | fused operation, possibly across statements, and produce a result that differs |
| 2221 | from the value obtained by executing and rounding the instructions individually. |
| 2222 | |
| 2223 | |
| 2224 | #### List operators |
| 2225 | |
| 2226 | Lists can be concatenated using the `+` operator. |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2227 | Opens list are closed to their default value beforehand. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2228 | |
| 2229 | ``` |
| 2230 | [ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
| 2231 | [ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2232 | [ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4 ] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2233 | ``` |
| 2234 | |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 2235 | Lists can be multiplied with a non-negative`int` using the `*` operator |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 2236 | to create a repeated the list by the indicated number. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2237 | ``` |
| 2238 | 3*[1,2] // [1, 2, 1, 2, 1, 2] |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2239 | 3*[1, 2, ...] // [1, 2, 1, 2, 1 ,2] |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 2240 | [byte]*4 // [byte, byte, byte, byte] |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 2241 | 0*[1,2] // [] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2242 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2243 | |
| 2244 | <!-- TODO(mpvl): should we allow multiplication with a range? |
| 2245 | If so, how does one specify a list with a range of possible lengths? |
| 2246 | |
| 2247 | Suggestion from jba: |
| 2248 | Multiplication should distribute over disjunction, |
| 2249 | so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x]. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2250 | The hard part is figuring out what (>=1 & <=3) * [x] means, |
| 2251 | since >=1 & <=3 includes many floats. |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2252 | (mpvl: could constrain arguments to parameter types, but needs to be |
| 2253 | done consistently.) |
| 2254 | --> |
| 2255 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2256 | |
| 2257 | #### String operators |
| 2258 | |
| 2259 | Strings can be concatenated using the `+` operator: |
| 2260 | ``` |
Daniel Martí | 107863a | 2020-02-11 15:00:50 +0000 | [diff] [blame] | 2261 | s: "hi " + name + " and good bye" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2262 | ``` |
| 2263 | String addition creates a new string by concatenating the operands. |
| 2264 | |
| 2265 | A string can be repeated by multiplying it: |
| 2266 | |
| 2267 | ``` |
| 2268 | s: "etc. "*3 // "etc. etc. etc. " |
| 2269 | ``` |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2270 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2271 | <!-- jba: Do these work for byte sequences? If not, why not? --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2272 | |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2273 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2274 | ##### Comparison operators |
| 2275 | |
| 2276 | Comparison operators compare two operands and yield an untyped boolean value. |
| 2277 | |
| 2278 | ``` |
| 2279 | == equal |
| 2280 | != not equal |
| 2281 | < less |
| 2282 | <= less or equal |
| 2283 | > greater |
| 2284 | >= greater or equal |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2285 | =~ matches regular expression |
| 2286 | !~ does not match regular expression |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2287 | ``` |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2288 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2289 | <!-- regular expression operator inspired by Bash, Perl, and Ruby. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2290 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2291 | In any comparison, the types of the two operands must unify or one of the |
| 2292 | operands must be null. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2293 | |
| 2294 | The equality operators `==` and `!=` apply to operands that are comparable. |
| 2295 | The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered. |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2296 | The matching operators `=~` and `!~` apply to a string and regular |
| 2297 | expression operand. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2298 | These terms and the result of the comparisons are defined as follows: |
| 2299 | |
Marcel van Lohuizen | 855243e | 2019-02-07 18:00:55 +0100 | [diff] [blame] | 2300 | - Null is comparable with itself and any other type. |
| 2301 | Two null values are always equal, null is unequal with anything else. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2302 | - Boolean values are comparable. |
| 2303 | Two boolean values are equal if they are either both true or both false. |
| 2304 | - Integer values are comparable and ordered, in the usual way. |
| 2305 | - Floating-point values are comparable and ordered, as per the definitions |
| 2306 | for binary coded decimals in the IEEE-754-2008 standard. |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 2307 | - Floating point numbers may be compared with integers. |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2308 | - String and bytes values are comparable and ordered lexically byte-wise. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2309 | - Struct are not comparable. |
Marcel van Lohuizen | 855243e | 2019-02-07 18:00:55 +0100 | [diff] [blame] | 2310 | - Lists are not comparable. |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2311 | - The regular expression syntax is the one accepted by RE2, |
| 2312 | described in https://github.com/google/re2/wiki/Syntax, |
| 2313 | except for `\C`. |
| 2314 | - `s =~ r` is true if `s` matches the regular expression `r`. |
| 2315 | - `s !~ r` is true if `s` does not match regular expression `r`. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2316 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 2317 | <!--- TODO: consider the following |
| 2318 | - For regular expression, named capture groups are interpreted as CUE references |
| 2319 | that must unify with the strings matching this capture group. |
| 2320 | ---> |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2321 | <!-- TODO: Implementations should adopt an algorithm that runs in linear time? --> |
Marcel van Lohuizen | 88a8a5f | 2019-02-20 01:26:22 +0100 | [diff] [blame] | 2322 | <!-- Consider implementing Level 2 of Unicode regular expression. --> |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2323 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2324 | ``` |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2325 | 3 < 4 // true |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 2326 | 3 < 4.0 // true |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2327 | null == 2 // false |
| 2328 | null != {} // true |
| 2329 | {} == {} // _|_: structs are not comparable against structs |
| 2330 | |
| 2331 | "Wild cats" =~ "cat" // true |
| 2332 | "Wild cats" !~ "dog" // true |
| 2333 | |
| 2334 | "foo" =~ "^[a-z]{3}$" // true |
| 2335 | "foo" =~ "^[a-z]{4}$" // false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2336 | ``` |
| 2337 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2338 | <!-- jba |
| 2339 | I think I know what `3 < a` should mean if |
| 2340 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2341 | a: >=1 & <=5 |
| 2342 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2343 | It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely. |
| 2344 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2345 | But what does `3 < (>=1 & <=5)` mean? We'll never get more information, so it must have a definite value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2346 | --> |
| 2347 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2348 | #### Logical operators |
| 2349 | |
| 2350 | Logical operators apply to boolean values and yield a result of the same type |
| 2351 | as the operands. The right operand is evaluated conditionally. |
| 2352 | |
| 2353 | ``` |
| 2354 | && conditional AND p && q is "if p then q else false" |
| 2355 | || conditional OR p || q is "if p then true else q" |
| 2356 | ! NOT !p is "not p" |
| 2357 | ``` |
| 2358 | |
| 2359 | |
| 2360 | <!-- |
| 2361 | ### TODO TODO TODO |
| 2362 | |
| 2363 | 3.14 / 0.0 // illegal: division by zero |
| 2364 | Illegal conversions always apply to CUE. |
| 2365 | |
| 2366 | Implementation restriction: A compiler may use rounding while computing untyped floating-point or complex constant expressions; see the implementation restriction in the section on constants. This rounding may cause a floating-point constant expression to be invalid in an integer context, even if it would be integral when calculated using infinite precision, and vice versa. |
| 2367 | --> |
| 2368 | |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2369 | <!--- TODO(mpvl): conversions |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2370 | ### Conversions |
| 2371 | Conversions are expressions of the form `T(x)` where `T` and `x` are |
| 2372 | expressions. |
| 2373 | The result is always an instance of `T`. |
| 2374 | |
| 2375 | ``` |
| 2376 | Conversion = Expression "(" Expression [ "," ] ")" . |
| 2377 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2378 | ---> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2379 | <!--- |
| 2380 | |
| 2381 | A literal value `x` can be converted to type T if `x` is representable by a |
| 2382 | value of `T`. |
| 2383 | |
| 2384 | As a special case, an integer literal `x` can be converted to a string type |
| 2385 | using the same rule as for non-constant x. |
| 2386 | |
| 2387 | Converting a literal yields a typed value as result. |
| 2388 | |
| 2389 | ``` |
| 2390 | uint(iota) // iota value of type uint |
| 2391 | float32(2.718281828) // 2.718281828 of type float32 |
| 2392 | complex128(1) // 1.0 + 0.0i of type complex128 |
| 2393 | float32(0.49999999) // 0.5 of type float32 |
| 2394 | float64(-1e-1000) // 0.0 of type float64 |
| 2395 | string('x') // "x" of type string |
| 2396 | string(0x266c) // "♬" of type string |
| 2397 | MyString("foo" + "bar") // "foobar" of type MyString |
| 2398 | string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant |
| 2399 | (*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type |
| 2400 | int(1.2) // illegal: 1.2 cannot be represented as an int |
| 2401 | string(65.0) // illegal: 65.0 is not an integer constant |
| 2402 | ``` |
| 2403 | ---> |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2404 | <!--- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2405 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2406 | A conversion is always allowed if `x` is an instance of `T`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2407 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2408 | If `T` and `x` of different underlying type, a conversion is allowed if |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2409 | `x` can be converted to a value `x'` of `T`'s type, and |
| 2410 | `x'` is an instance of `T`. |
| 2411 | A value `x` can be converted to the type of `T` in any of these cases: |
| 2412 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2413 | - `x` is a struct and is subsumed by `T`. |
| 2414 | - `x` and `T` are both integer or floating points. |
| 2415 | - `x` is an integer or a byte sequence and `T` is a string. |
| 2416 | - `x` is a string and `T` is a byte sequence. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2417 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2418 | Specific rules apply to conversions between numeric types, structs, |
| 2419 | or to and from a string type. These conversions may change the representation |
| 2420 | of `x`. |
| 2421 | All other conversions only change the type but not the representation of x. |
| 2422 | |
| 2423 | |
| 2424 | #### Conversions between numeric ranges |
| 2425 | For the conversion of numeric values, the following rules apply: |
| 2426 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2427 | 1. Any integer value can be converted into any other integer value |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2428 | provided that it is within range. |
| 2429 | 2. When converting a decimal floating-point number to an integer, the fraction |
| 2430 | is discarded (truncation towards zero). TODO: or disallow truncating? |
| 2431 | |
| 2432 | ``` |
| 2433 | a: uint16(int(1000)) // uint16(1000) |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2434 | b: uint8(1000) // _|_ // overflow |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2435 | c: int(2.5) // 2 TODO: TBD |
| 2436 | ``` |
| 2437 | |
| 2438 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2439 | #### Conversions to and from a string type |
| 2440 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2441 | Converting a list of bytes to a string type yields a string whose successive |
| 2442 | bytes are the elements of the slice. |
| 2443 | Invalid UTF-8 is converted to `"\uFFFD"`. |
| 2444 | |
| 2445 | ``` |
| 2446 | string('hell\xc3\xb8') // "hellø" |
| 2447 | string(bytes([0x20])) // " " |
| 2448 | ``` |
| 2449 | |
| 2450 | As string value is always convertible to a list of bytes. |
| 2451 | |
| 2452 | ``` |
| 2453 | bytes("hellø") // 'hell\xc3\xb8' |
| 2454 | bytes("") // '' |
| 2455 | ``` |
| 2456 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2457 | #### Conversions between list types |
| 2458 | |
| 2459 | Conversions between list types are possible only if `T` strictly subsumes `x` |
| 2460 | and the result will be the unification of `T` and `x`. |
| 2461 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2462 | If we introduce named types this would be different from IP & [10, ...] |
| 2463 | |
| 2464 | Consider removing this until it has a different meaning. |
| 2465 | |
| 2466 | ``` |
| 2467 | IP: 4*[byte] |
| 2468 | Private10: IP([10, ...]) // [10, byte, byte, byte] |
| 2469 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2470 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2471 | #### Conversions between struct types |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2472 | |
| 2473 | A conversion from `x` to `T` |
| 2474 | is applied using the following rules: |
| 2475 | |
| 2476 | 1. `x` must be an instance of `T`, |
| 2477 | 2. all fields defined for `x` that are not defined for `T` are removed from |
| 2478 | the result of the conversion, recursively. |
| 2479 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2480 | <!-- jba: I don't think you say anywhere that the matching fields are unified. |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2481 | mpvl: they are not, x must be an instance of T, in which case x == T&x, |
| 2482 | so unification would be unnecessary. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2483 | --> |
Marcel van Lohuizen | a3f0097 | 2019-02-01 11:10:39 +0100 | [diff] [blame] | 2484 | <!-- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2485 | ``` |
| 2486 | T: { |
| 2487 | a: { b: 1..10 } |
| 2488 | } |
| 2489 | |
| 2490 | x1: { |
| 2491 | a: { b: 8, c: 10 } |
| 2492 | d: 9 |
| 2493 | } |
| 2494 | |
| 2495 | c1: T(x1) // { a: { b: 8 } } |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2496 | c2: T({}) // _|_ // missing field 'a' in '{}' |
| 2497 | c3: T({ a: {b: 0} }) // _|_ // field a.b does not unify (0 & 1..10) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2498 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2499 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2500 | |
| 2501 | ### Calls |
| 2502 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2503 | Calls can be made to core library functions, called builtins. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2504 | Given an expression `f` of function type F, |
| 2505 | ``` |
| 2506 | f(a1, a2, … an) |
| 2507 | ``` |
| 2508 | calls `f` with arguments a1, a2, … an. Arguments must be expressions |
| 2509 | of which the values are an instance of the parameter types of `F` |
| 2510 | and are evaluated before the function is called. |
| 2511 | |
| 2512 | ``` |
| 2513 | a: math.Atan2(x, y) |
| 2514 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2515 | |
| 2516 | In a function call, the function value and arguments are evaluated in the usual |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 2517 | order. |
| 2518 | After they are evaluated, the parameters of the call are passed by value |
| 2519 | to the function and the called function begins execution. |
| 2520 | The return parameters |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2521 | of the function are passed by value back to the calling function when the |
| 2522 | function returns. |
| 2523 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2524 | |
| 2525 | ### Comprehensions |
| 2526 | |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 2527 | Lists and fields can be constructed using comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2528 | |
| 2529 | Each define a clause sequence that consists of a sequence of `for`, `if`, and |
| 2530 | `let` clauses, nesting from left to right. |
| 2531 | The `for` and `let` clauses each define a new scope in which new values are |
| 2532 | bound to be available for the next clause. |
| 2533 | |
| 2534 | The `for` clause binds the defined identifiers, on each iteration, to the next |
| 2535 | value of some iterable value in a new scope. |
| 2536 | A `for` clause may bind one or two identifiers. |
Marcel van Lohuizen | 4245fb4 | 2019-09-09 11:22:12 +0200 | [diff] [blame] | 2537 | If there is one identifier, it binds it to the value of |
| 2538 | a list element or struct field value. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2539 | If there are two identifiers, the first value will be the key or index, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2540 | if available, and the second will be the value. |
| 2541 | |
Marcel van Lohuizen | 4245fb4 | 2019-09-09 11:22:12 +0200 | [diff] [blame] | 2542 | For lists, `for` iterates over all elements in the list after closing it. |
| 2543 | For structs, `for` iterates over all non-optional regular fields. |
| 2544 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2545 | An `if` clause, or guard, specifies an expression that terminates the current |
| 2546 | iteration if it evaluates to false. |
| 2547 | |
| 2548 | The `let` clause binds the result of an expression to the defined identifier |
| 2549 | in a new scope. |
| 2550 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2551 | A current iteration is said to complete if the innermost block of the clause |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2552 | sequence is reached. |
| 2553 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2554 | _List comprehensions_ specify a single expression that is evaluated and included |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2555 | in the list for each completed iteration. |
| 2556 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 2557 | _Field comprehensions_ follow a clause sequence with a struct literal, |
| 2558 | where the struct literal is evaluated and embedded at the point of |
| 2559 | declaration of the comprehension for each complete iteration. |
| 2560 | As usual, fields in the struct may evaluate to the same label, |
| 2561 | resulting in the unification of their values. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2562 | |
| 2563 | ``` |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 2564 | Comprehension = Clauses StructLit . |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 2565 | ListComprehension = "[" Expression Clauses "]" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2566 | |
| 2567 | Clauses = Clause { Clause } . |
| 2568 | Clause = ForClause | GuardClause | LetClause . |
| 2569 | ForClause = "for" identifier [ ", " identifier] "in" Expression . |
| 2570 | GuardClause = "if" Expression . |
| 2571 | LetClause = "let" identifier "=" Expression . |
| 2572 | ``` |
| 2573 | |
| 2574 | ``` |
| 2575 | a: [1, 2, 3, 4] |
| 2576 | b: [ x+1 for x in a if x > 1] // [3, 4, 5] |
| 2577 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 2578 | c: { |
| 2579 | for x in a |
| 2580 | if x < 4 |
| 2581 | let y = 1 { |
| 2582 | "\(x)": x + y |
| 2583 | } |
| 2584 | } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2585 | d: { "1": 2, "2": 3, "3": 4 } |
| 2586 | ``` |
| 2587 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2588 | |
| 2589 | ### String interpolation |
| 2590 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2591 | String interpolation allows constructing strings by replacing placeholder |
| 2592 | expressions with their string representation. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2593 | String interpolation may be used in single- and double-quoted strings, as well |
| 2594 | as their multiline equivalent. |
| 2595 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2596 | A placeholder consists of "\(" followed by an expression and a ")". The |
| 2597 | expression is evaluated within the scope within which the string is defined. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2598 | |
| 2599 | ``` |
| 2600 | a: "World" |
| 2601 | b: "Hello \( a )!" // Hello World! |
| 2602 | ``` |
| 2603 | |
| 2604 | |
| 2605 | ## Builtin Functions |
| 2606 | |
| 2607 | Built-in functions are predeclared. They are called like any other function. |
| 2608 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2609 | |
| 2610 | ### `len` |
| 2611 | |
| 2612 | The built-in function `len` takes arguments of various types and return |
| 2613 | a result of type int. |
| 2614 | |
| 2615 | ``` |
| 2616 | Argument type Result |
| 2617 | |
| 2618 | string string length in bytes |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2619 | bytes length of byte sequence |
| 2620 | list list length, smallest length for an open list |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2621 | struct number of distinct data fields, including optional |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2622 | ``` |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2623 | <!-- TODO: consider not supporting len, but instead rely on more |
| 2624 | precisely named builtin functions: |
| 2625 | - strings.RuneLen(x) |
| 2626 | - bytes.Len(x) // x may be a string |
| 2627 | - struct.NumFooFields(x) |
| 2628 | - list.Len(x) |
| 2629 | --> |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2630 | |
| 2631 | ``` |
| 2632 | Expression Result |
| 2633 | len("Hellø") 6 |
| 2634 | len([1, 2, 3]) 3 |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2635 | len([1, 2, ...]) >=2 |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2636 | ``` |
| 2637 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 2638 | |
| 2639 | ### `close` |
| 2640 | |
| 2641 | The builtin function `close` converts a partially defined, or open, struct |
| 2642 | to a fully defined, or closed, struct. |
| 2643 | |
| 2644 | |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2645 | ### `and` |
| 2646 | |
| 2647 | The built-in function `and` takes a list and returns the result of applying |
| 2648 | the `&` operator to all elements in the list. |
| 2649 | It returns top for the empty list. |
| 2650 | |
Adieu | 5b4fa8b | 2019-12-03 19:20:58 +0100 | [diff] [blame] | 2651 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2652 | Expression: Result |
| 2653 | and([a, b]) a & b |
| 2654 | and([a]) a |
| 2655 | and([]) _ |
Adieu | 5b4fa8b | 2019-12-03 19:20:58 +0100 | [diff] [blame] | 2656 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2657 | |
| 2658 | ### `or` |
| 2659 | |
| 2660 | The built-in function `or` takes a list and returns the result of applying |
| 2661 | the `|` operator to all elements in the list. |
| 2662 | It returns bottom for the empty list. |
| 2663 | |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2664 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2665 | Expression: Result |
Adieu | 5b4fa8b | 2019-12-03 19:20:58 +0100 | [diff] [blame] | 2666 | or([a, b]) a | b |
| 2667 | or([a]) a |
| 2668 | or([]) _|_ |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2669 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2670 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2671 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2672 | ## Cycles |
| 2673 | |
| 2674 | Implementations are required to interpret or reject cycles encountered |
| 2675 | during evaluation according to the rules in this section. |
| 2676 | |
| 2677 | |
| 2678 | ### Reference cycles |
| 2679 | |
| 2680 | A _reference cycle_ occurs if a field references itself, either directly or |
| 2681 | indirectly. |
| 2682 | |
| 2683 | ``` |
| 2684 | // x references itself |
| 2685 | x: x |
| 2686 | |
| 2687 | // indirect cycles |
| 2688 | b: c |
| 2689 | c: d |
| 2690 | d: b |
| 2691 | ``` |
| 2692 | |
| 2693 | Implementations should report these as an error except in the following cases: |
| 2694 | |
| 2695 | |
| 2696 | #### Expressions that unify an atom with an expression |
| 2697 | |
| 2698 | An expression of the form `a & e`, where `a` is an atom |
| 2699 | and `e` is an expression, always evaluates to `a` or bottom. |
| 2700 | As it does not matter how we fail, we can assume the result to be `a` |
| 2701 | and validate after the field in which the expression occurs has been evaluated |
| 2702 | that `a == e`. |
| 2703 | |
| 2704 | ``` |
Marcel van Lohuizen | eac8f9a | 2019-08-03 13:53:56 +0200 | [diff] [blame] | 2705 | // Config Evaluates to (requiring concrete values) |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2706 | x: { x: { |
| 2707 | a: b + 100 a: _|_ // cycle detected |
| 2708 | b: a - 100 b: _|_ // cycle detected |
| 2709 | } } |
| 2710 | |
| 2711 | y: x & { y: { |
| 2712 | a: 200 a: 200 // asserted that 200 == b + 100 |
| 2713 | b: 100 |
| 2714 | } } |
| 2715 | ``` |
| 2716 | |
| 2717 | |
| 2718 | #### Field values |
| 2719 | |
| 2720 | A field value of the form `r & v`, |
| 2721 | where `r` evaluates to a reference cycle and `v` is a value, |
| 2722 | evaluates to `v`. |
| 2723 | Unification is idempotent and unifying a value with itself ad infinitum, |
| 2724 | which is what the cycle represents, results in this value. |
| 2725 | Implementations should detect cycles of this kind, ignore `r`, |
| 2726 | and take `v` as the result of unification. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2727 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2728 | <!-- Tomabechi's graph unification algorithm |
| 2729 | can detect such cycles at near-zero cost. --> |
| 2730 | |
| 2731 | ``` |
| 2732 | Configuration Evaluated |
| 2733 | // c Cycles in nodes of type struct evaluate |
| 2734 | // ↙︎ ↖ to the fixed point of unifying their |
| 2735 | // a → b values ad infinitum. |
| 2736 | |
| 2737 | a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 } |
| 2738 | b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 } |
| 2739 | c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 } |
| 2740 | |
| 2741 | // resolve a b & {x:1} |
| 2742 | // substitute b c & {y:2} & {x:1} |
| 2743 | // substitute c a & {z:3} & {y:2} & {x:1} |
| 2744 | // eliminate a (cycle) {z:3} & {y:2} & {x:1} |
| 2745 | // simplify {x:1,y:2,z:3} |
| 2746 | ``` |
| 2747 | |
| 2748 | This rule also applies to field values that are disjunctions of unification |
| 2749 | operations of the above form. |
| 2750 | |
| 2751 | ``` |
| 2752 | a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1} |
| 2753 | b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2} |
| 2754 | c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3} |
| 2755 | |
| 2756 | |
| 2757 | // resolving a b&{x:1} | {y:1} |
| 2758 | // substitute b ({x:2} | c&{z:2})&{x:1} | {y:1} |
| 2759 | // simplify c&{z:2}&{x:1} | {y:1} |
| 2760 | // substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1} |
| 2761 | // simplify a&{y:3}&{z:2}&{x:1} | {y:1} |
| 2762 | // eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1} |
| 2763 | // expand {x:1,y:3,z:2} | {y:1} |
| 2764 | ``` |
| 2765 | |
| 2766 | Note that all nodes that form a reference cycle to form a struct will evaluate |
| 2767 | to the same value. |
| 2768 | If a field value is a disjunction, any element that is part of a cycle will |
| 2769 | evaluate to this value. |
| 2770 | |
| 2771 | |
| 2772 | ### Structural cycles |
| 2773 | |
| 2774 | CUE disallows infinite structures. |
| 2775 | Implementations must report an error when encountering such declarations. |
| 2776 | |
| 2777 | <!-- for instance using an occurs check --> |
| 2778 | |
| 2779 | ``` |
| 2780 | // Disallowed: a list of infinite length with all elements being 1. |
| 2781 | list: { |
| 2782 | head: 1 |
| 2783 | tail: list |
| 2784 | } |
| 2785 | |
| 2786 | // Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...). |
| 2787 | a: { |
| 2788 | b: c |
| 2789 | } |
| 2790 | c: { |
| 2791 | d: a |
| 2792 | } |
| 2793 | ``` |
| 2794 | |
| 2795 | It is allowed for a value to define an infinite set of possibilities |
| 2796 | without evaluating to an infinite structure itself. |
| 2797 | |
| 2798 | ``` |
| 2799 | // List defines a list of arbitrary length (default null). |
| 2800 | List: *null | { |
| 2801 | head: _ |
| 2802 | tail: List |
| 2803 | } |
| 2804 | ``` |
| 2805 | |
| 2806 | <!-- |
Marcel van Lohuizen | 7f48df7 | 2019-02-01 17:24:59 +0100 | [diff] [blame] | 2807 | Consider banning any construct that makes CUE not having a linear |
| 2808 | running time expressed in the number of nodes in the output. |
| 2809 | |
| 2810 | This would require restricting constructs like: |
| 2811 | |
| 2812 | (fib&{n:2}).out |
| 2813 | |
| 2814 | fib: { |
| 2815 | n: int |
| 2816 | |
| 2817 | out: (fib&{n:n-2}).out + (fib&{n:n-1}).out if n >= 2 |
| 2818 | out: fib({n:n-2}).out + fib({n:n-1}).out if n >= 2 |
| 2819 | out: n if n < 2 |
| 2820 | } |
| 2821 | |
| 2822 | --> |
| 2823 | <!-- |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2824 | ### Unused fields |
| 2825 | |
| 2826 | TODO: rules for detection of unused fields |
| 2827 | |
| 2828 | 1. Any alias value must be used |
| 2829 | --> |
| 2830 | |
| 2831 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2832 | ## Modules, instances, and packages |
| 2833 | |
| 2834 | CUE configurations are constructed combining _instances_. |
| 2835 | An instance, in turn, is constructed from one or more source files belonging |
| 2836 | to the same _package_ that together declare the data representation. |
| 2837 | Elements of this data representation may be exported and used |
| 2838 | in other instances. |
| 2839 | |
| 2840 | ### Source file organization |
| 2841 | |
| 2842 | Each source file consists of an optional package clause defining collection |
| 2843 | of files to which it belongs, |
| 2844 | followed by a possibly empty set of import declarations that declare |
| 2845 | packages whose contents it wishes to use, followed by a possibly empty set of |
| 2846 | declarations. |
| 2847 | |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 2848 | Like with a struct, a source file may contain embeddings. |
| 2849 | Unlike with a struct, the embedded expressions may be any value. |
| 2850 | If the result of the unification of all embedded values is not a struct, |
| 2851 | it will be output instead of its enclosing file when exporting CUE |
| 2852 | to a data format |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2853 | |
| 2854 | ``` |
Marcel van Lohuizen | 0a6f7c9 | 2020-01-28 11:10:38 +0100 | [diff] [blame] | 2855 | SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } . |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 2856 | ``` |
| 2857 | |
| 2858 | ``` |
| 2859 | "Hello \(place)!" |
| 2860 | |
| 2861 | place: "world" |
| 2862 | |
| 2863 | // Outputs "Hello world!" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2864 | ``` |
| 2865 | |
| 2866 | ### Package clause |
| 2867 | |
| 2868 | A package clause is an optional clause that defines the package to which |
| 2869 | a source file the file belongs. |
| 2870 | |
| 2871 | ``` |
| 2872 | PackageClause = "package" PackageName . |
| 2873 | PackageName = identifier . |
| 2874 | ``` |
| 2875 | |
| 2876 | The PackageName must not be the blank identifier. |
| 2877 | |
| 2878 | ``` |
| 2879 | package math |
| 2880 | ``` |
| 2881 | |
| 2882 | ### Modules and instances |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2883 | A _module_ defines a tree of directories, rooted at the _module root_. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2884 | |
| 2885 | All source files within a module with the same package belong to the same |
| 2886 | package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2887 | <!-- jba: I can't make sense of the above sentence. --> |
| 2888 | A module may define multiple packages. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2889 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2890 | An _instance_ of a package is any subset of files belonging |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2891 | to the same package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2892 | <!-- jba: Are you saying that --> |
| 2893 | <!-- if I have a package with files a, b and c, then there are 8 instances of --> |
| 2894 | <!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the --> |
| 2895 | <!-- purpose of that definition? --> |
| 2896 | It is interpreted as the concatenation of these files. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2897 | |
| 2898 | An implementation may impose conventions on the layout of package files |
| 2899 | to determine which files of a package belongs to an instance. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2900 | For example, an instance may be defined as the subset of package files |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2901 | belonging to a directory and all its ancestors. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2902 | <!-- jba: OK, that helps a little, but I still don't see what the purpose is. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2903 | |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2904 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2905 | ### Import declarations |
| 2906 | |
| 2907 | An import declaration states that the source file containing the declaration |
| 2908 | depends on definitions of the _imported_ package (§Program initialization and |
| 2909 | execution) and enables access to exported identifiers of that package. |
| 2910 | The import names an identifier (PackageName) to be used for access and an |
| 2911 | ImportPath that specifies the package to be imported. |
| 2912 | |
| 2913 | ``` |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 2914 | ImportDecl = "import" ( ImportSpec | "(" { ImportSpec "," } ")" ) . |
Marcel van Lohuizen | fbab65d | 2019-08-13 16:51:15 +0200 | [diff] [blame] | 2915 | ImportSpec = [ PackageName ] ImportPath . |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2916 | ImportLocation = { unicode_value } . |
| 2917 | ImportPath = `"` ImportLocation [ ":" identifier ] `"` . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2918 | ``` |
| 2919 | |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2920 | The PackageName is used in qualified identifiers to access |
| 2921 | exported identifiers of the package within the importing source file. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2922 | It is declared in the file block. |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2923 | It defaults to the identifier specified in the package clause of the imported |
| 2924 | package, which must match either the last path component of ImportLocation |
| 2925 | or the identifier following it. |
| 2926 | |
| 2927 | <!-- |
| 2928 | Note: this deviates from the Go spec where there is no such restriction. |
| 2929 | This restriction has the benefit of being to determine the identifiers |
| 2930 | for packages from within the file itself. But for CUE it is has another benefit: |
| 2931 | when using package hiearchies, one is more likely to want to include multiple |
| 2932 | packages within the same directory structure. This mechanism allows |
| 2933 | disambiguation in these cases. |
| 2934 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2935 | |
| 2936 | The interpretation of the ImportPath is implementation-dependent but it is |
| 2937 | typically either the path of a builtin package or a fully qualifying location |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2938 | of a package within a source code repository. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2939 | |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2940 | An ImportLocation must be a non-empty strings using only characters belonging |
| 2941 | Unicode's L, M, N, P, and S general categories |
| 2942 | (the Graphic characters without spaces) |
| 2943 | and may not include the characters !"#$%&'()*,:;<=>?[\]^`{|} |
| 2944 | or the Unicode replacement character U+FFFD. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2945 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2946 | Assume we have package containing the package clause "package math", |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2947 | which exports function Sin at the path identified by "lib/math". |
| 2948 | This table illustrates how Sin is accessed in files |
| 2949 | that import the package after the various types of import declaration. |
| 2950 | |
| 2951 | ``` |
| 2952 | Import declaration Local name of Sin |
| 2953 | |
| 2954 | import "lib/math" math.Sin |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2955 | import "lib/math:math" math.Sin |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2956 | import m "lib/math" m.Sin |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2957 | ``` |
| 2958 | |
| 2959 | An import declaration declares a dependency relation between the importing and |
| 2960 | imported package. It is illegal for a package to import itself, directly or |
| 2961 | indirectly, or to directly import a package without referring to any of its |
| 2962 | exported identifiers. |
| 2963 | |
| 2964 | |
| 2965 | ### An example package |
| 2966 | |
| 2967 | TODO |