blob: b822e6cd25ccc5337ce5ca73a146e00ee26c1e33 [file] [log] [blame] [view]
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01001<!--
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 Lohuizendd5e5892018-11-22 23:29:16 +010017# The CUE Language Specification
18
19## Introduction
20
Marcel van Lohuizen5953c662019-01-26 13:26:04 +010021This is a reference manual for the CUE data constraint language.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010022CUE, pronounced cue or Q, is a general-purpose and strongly typed
Marcel van Lohuizen5953c662019-01-26 13:26:04 +010023constraint-based language.
24It can be used for data templating, data validation, code generation, scripting,
25and many other applications involving structured data.
26The CUE tooling, layered on top of CUE, provides
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010027a general purpose scripting language for creating scripts as well as
Marcel van Lohuizen5953c662019-01-26 13:26:04 +010028simple servers, also expressed in CUE.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010029
30CUE was designed with cloud configuration, and related systems, in mind,
31but is not limited to this domain.
32It derives its formalism from relational programming languages.
33This formalism allows for managing and reasoning over large amounts of
Marcel van Lohuizen5953c662019-01-26 13:26:04 +010034data in a straightforward manner.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010035
36The grammar is compact and regular, allowing for easy analysis by automatic
37tools such as integrated development environments.
38
39This document is maintained by mpvl@golang.org.
40CUE has a lot of similarities with the Go language. This document draws heavily
Marcel van Lohuizen73f14eb2019-01-30 17:11:17 +010041from the Go specification as a result.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010042
43CUE draws its influence from many languages.
44Its main influences were BCL/ GCL (internal to Google),
45LKB (LinGO), Go, and JSON.
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +020046Others are Swift, Typescript, Javascript, Prolog, NCL (internal to Google),
47Jsonnet, HCL, Flabbergast, JSONPath, Haskell, Objective-C, and Python.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010048
49
50## Notation
51
52The syntax is specified using Extended Backus-Naur Form (EBNF):
53
54```
55Production = production_name "=" [ Expression ] "." .
56Expression = Alternative { "|" Alternative } .
57Alternative = Term { Term } .
58Term = production_name | token [ "…" token ] | Group | Option | Repetition .
59Group = "(" Expression ")" .
60Option = "[" Expression "]" .
61Repetition = "{" Expression "}" .
62```
63
64Productions are expressions constructed from terms and the following operators,
65in increasing precedence:
66
67```
68| alternation
69() grouping
70[] option (0 or 1 times)
71{} repetition (0 to n times)
72```
73
74Lower-case production names are used to identify lexical tokens. Non-terminals
75are in CamelCase. Lexical tokens are enclosed in double quotes "" or back quotes
76``.
77
78The form a … b represents the set of characters from a through b as
79alternatives. The horizontal ellipsis … is also used elsewhere in the spec to
80informally denote various enumerations or code snippets that are not further
81specified. The character … (as opposed to the three characters ...) is not a
82token of the Go language.
83
84
85## Source code representation
86
87Source code is Unicode text encoded in UTF-8.
88Unless otherwise noted, the text is not canonicalized, so a single
89accented code point is distinct from the same character constructed from
90combining an accent and a letter; those are treated as two code points.
91For simplicity, this document will use the unqualified term character to refer
92to a Unicode code point in the source text.
93
94Each code point is distinct; for instance, upper and lower case letters are
95different characters.
96
97Implementation restriction: For compatibility with other tools, a compiler may
98disallow the NUL character (U+0000) in the source text.
99
100Implementation restriction: For compatibility with other tools, a compiler may
101ignore a UTF-8-encoded byte order mark (U+FEFF) if it is the first Unicode code
102point in the source text. A byte order mark may be disallowed anywhere else in
103the source.
104
105
106### Characters
107
108The following terms are used to denote specific Unicode character classes:
109
110```
111newline = /* the Unicode code point U+000A */ .
112unicode_char = /* an arbitrary Unicode code point except newline */ .
113unicode_letter = /* a Unicode code point classified as "Letter" */ .
114unicode_digit = /* a Unicode code point classified as "Number, decimal digit" */ .
115```
116
117In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of
118character categories.
119CUE treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo
120as Unicode letters, and those in the Number category Nd as Unicode digits.
121
122
123### Letters and digits
124
125The underscore character _ (U+005F) is considered a letter.
126
127```
128letter = unicode_letter | "_" .
129decimal_digit = "0" … "9" .
130octal_digit = "0" … "7" .
131hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
132```
133
134
135## Lexical elements
136
137### Comments
138Comments serve as program documentation. There are two forms:
139
1401. Line comments start with the character sequence // and stop at the end of the line.
1412. General comments start with the character sequence /* and stop with the first subsequent character sequence */.
142
143A comment cannot start inside string literal or inside a comment.
144A general comment containing no newlines acts like a space.
145Any other comment acts like a newline.
146
147
148### Tokens
149
150Tokens form the vocabulary of the CUE language. There are four classes:
151identifiers, keywords, operators and punctuation, and literals. White space,
152formed from spaces (U+0020), horizontal tabs (U+0009), carriage returns
153(U+000D), and newlines (U+000A), is ignored except as it separates tokens that
154would otherwise combine into a single token. Also, a newline or end of file may
155trigger the insertion of a comma. While breaking the input into tokens, the
156next token is the longest sequence of characters that form a valid token.
157
158
159### Commas
160
161The formal grammar uses commas "," as terminators in a number of productions.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500162CUE programs may omit most of these commas using the following two rules:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100163
164When the input is broken into tokens, a comma is automatically inserted into
165the token stream immediately after a line's final token if that token is
166
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500167- an identifier
168- null, true, false, bottom, or an integer, floating-point, or string literal
169- one of the characters ), ], or }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100170
171
172Although commas are automatically inserted, the parser will require
173explicit commas between two list elements.
174
175To reflect idiomatic use, examples in this document elide commas using
176these rules.
177
178
179### Identifiers
180
181Identifiers name entities such as fields and aliases.
182An identifier is a sequence of one or more letters and digits.
183It may not be `_`.
184The first character in an identifier must be a letter.
185
186<!--
187TODO: allow identifiers as defined in Unicode UAX #31
188(https://unicode.org/reports/tr31/).
189
190Identifiers are normalized using the NFC normal form.
191-->
192
193```
194identifier = letter { letter | unicode_digit } .
195```
196
197```
198a
199_x9
200fieldName
201αβ
202```
203
204<!-- TODO: Allow Unicode identifiers TR 32 http://unicode.org/reports/tr31/ -->
205
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500206Some identifiers are [predeclared](#predeclared-identifiers).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100207
208
209### Keywords
210
211CUE has a limited set of keywords.
212All keywords may be used as labels (field names).
213They cannot, however, be used as identifiers to refer to the same name.
214
215
216#### Values
217
218The following keywords are values.
219
220```
221null true false
222```
223
224These can never be used to refer to a field of the same name.
225This restriction is to ensure compatibility with JSON configuration files.
226
227
228#### Preamble
229
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100230The following keywords are used at the preamble of a CUE file.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100231After the preamble, they may be used as identifiers to refer to namesake fields.
232
233```
234package import
235```
236
237
238#### Comprehension clauses
239
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100240The following keywords are used in comprehensions.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100241
242```
243for in if let
244```
245
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100246The keywords `for`, `if` and `let` cannot be used as identifiers to
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100247refer to fields. All others can.
248
249<!--
250TODO:
251 reduce [to]
252 order [by]
253-->
254
255
256#### Arithmetic
257
258The following pseudo keywords can be used as operators in expressions.
259
260```
261div mod quo rem
262```
263
264These may be used as identifiers to refer to fields in all other contexts.
265
266
267### Operators and punctuation
268
269The following character sequences represent operators and punctuation:
270
271```
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +0100272+ div && == < . ( )
273- mod || != > : { }
274* quo & =~ <= = [ ]
275/ rem | !~ >= <- ... ,
276% _|_ ! ;
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100277```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100278<!-- :: for "is-a" definitions -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100279
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +0100280
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100281### Integer literals
282
283An integer literal is a sequence of digits representing an integer value.
284An optional prefix sets a non-decimal base: 0 for octal,
2850x or 0X for hexadecimal, and 0b for binary.
286In hexadecimal literals, letters a-f and A-F represent values 10 through 15.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500287All integers allow interstitial underscores "_";
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100288these have no meaning and are solely for readability.
289
290Decimal integers may have a SI or IEC multiplier.
291Multipliers can be used with fractional numbers.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500292When multiplying a fraction by a multiplier, the result is truncated
293towards zero if it is not an integer.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100294
295```
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200296int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit .
297decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } .
298decimals = decimal_digit { [ "_" ] decimal_digit } .
299si_it = decimals [ "." decimals ] multiplier |
300 "." decimals multiplier .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100301binary_lit = "0b" binary_digit { binary_digit } .
302hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } .
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200303octal_lit = "0" [ "o" ] octal_digit { [ "_" ] octal_digit } .
Jonathan Amsterdamabeffa42019-01-20 10:29:29 -0500304multiplier = ( "K" | "M" | "G" | "T" | "P" | "E" | "Y" | "Z" ) [ "i" ]
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200305
306float_lit = decimals "." [ decimals ] [ exponent ] |
307 decimals exponent |
308 "." decimals [ exponent ].
309exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100310```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100311
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100312```
31342
3141.5Gi
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100315170_141_183_460_469_231_731_687_303_715_884_105_727
Marcel van Lohuizenfc6303c2019-02-07 17:49:04 +01003160xBad_Face
3170o755
3180b0101_0001
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100319```
320
321### Decimal floating-point literals
322
323A decimal floating-point literal is a representation of
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500324a decimal floating-point value (a _float_).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100325It has an integer part, a decimal point, a fractional part, and an
326exponent part.
327The integer and fractional part comprise decimal digits; the
328exponent part is an `e` or `E` followed by an optionally signed decimal exponent.
329One of the integer part or the fractional part may be elided; one of the decimal
330point or the exponent may be elided.
331
332```
333decimal_lit = decimals "." [ decimals ] [ exponent ] |
334 decimals exponent |
335 "." decimals [ exponent ] .
336exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
337```
338
339```
3400.
34172.40
342072.40 // == 72.40
3432.71828
3441.e+0
3456.67428e-11
3461E6
347.25
348.12345E+5
349```
350
351
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100352### String and byte sequence literals
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100353
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100354A string literal represents a string constant obtained from concatenating a
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100355sequence of characters.
356Byte sequences are a sequence of bytes.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100357
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100358String and byte sequence literals are character sequences between,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100359respectively, double and single quotes, as in `"bar"` and `'bar'`.
360Within the quotes, any character may appear except newline and,
361respectively, unescaped double or single quote.
362String literals may only be valid UTF-8.
363Byte sequences may contain any sequence of bytes.
364
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400365Several escape sequences allow arbitrary values to be encoded as ASCII text.
366An escape sequence starts with an _escape delimiter_, which is `\` by default.
367The escape delimiter may be altered to be `\` plus a fixed number of
368hash symbols `#`
369by padding the start and end of a string or byte sequence literal
370with this number of hash symbols.
371
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100372There are four ways to represent the integer value as a numeric constant: `\x`
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400373followed by exactly two hexadecimal digits; `\u` followed by exactly four
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100374hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a
375plain backslash `\` followed by exactly three octal digits.
376In each case the value of the literal is the value represented by the
377digits in the corresponding base.
378Hexadecimal and octal escapes are only allowed within byte sequences
379(single quotes).
380
381Although these representations all result in an integer, they have different
382valid ranges.
383Octal escapes must represent a value between 0 and 255 inclusive.
384Hexadecimal escapes satisfy this condition by construction.
385The escapes `\u` and `\U` represent Unicode code points so within them
386some values are illegal, in particular those above `0x10FFFF`.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400387Surrogate halves are allowed,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100388but are translated into their non-surrogate equivalent internally.
389
390The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes
391represent individual bytes of the resulting string; all other escapes represent
392the (possibly multi-byte) UTF-8 encoding of individual characters.
393Thus inside a string literal `\377` and `\xFF` represent a single byte of
394value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent
395the two bytes `0xc3 0xbf` of the UTF-8
396encoding of character `U+00FF`.
397
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100398```
399\a U+0007 alert or bell
400\b U+0008 backspace
401\f U+000C form feed
402\n U+000A line feed or newline
403\r U+000D carriage return
404\t U+0009 horizontal tab
405\v U+000b vertical tab
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100406\/ U+002f slash (solidus)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100407\\ U+005c backslash
408\' U+0027 single quote (valid escape only within single quoted literals)
409\" U+0022 double quote (valid escape only within double quoted literals)
410```
411
412The escape `\(` is used as an escape for string interpolation.
413A `\(` must be followed by a valid CUE Expression, followed by a `)`.
414
415All other sequences starting with a backslash are illegal inside literals.
416
417```
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400418escaped_char = `\` { `#` } ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100419byte_value = octal_byte_value | hex_byte_value .
420octal_byte_value = `\` octal_digit octal_digit octal_digit .
421hex_byte_value = `\` "x" hex_digit hex_digit .
422little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
423big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
424 hex_digit hex_digit hex_digit hex_digit .
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400425unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
426interpolation = "\(" Expression ")" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100427
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400428string_lit = simple_string_lit |
429 multiline_string_lit |
430 simple_bytes_lit |
431 multiline_bytes_lit |
432 `#` string_lit `#` .
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100433
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400434simple_string_lit = `"` { unicode_value | interpolation } `"` .
435simple_bytes_lit = `"` { unicode_value | interpolation | byte_value } `"` .
436multiline_string_lit = `"""` newline
437 { unicode_value | interpolation | newline }
438 newline `"""` .
439multiline_bytes_lit = "'''" newline
440 { unicode_value | interpolation | byte_value | newline }
441 newline "'''" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100442```
443
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400444Carriage return characters (`\r`) inside string literals are discarded from
Marcel van Lohuizendb9d25a2019-02-21 23:54:43 +0100445the string value.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400446
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100447```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100448'a\000\xab'
449'\007'
450'\377'
451'\xa' // illegal: too few hexadecimal digits
452"\n"
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100453"\""
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100454'Hello, world!\n'
455"Hello, \( name )!"
456"日本語"
457"\u65e5本\U00008a9e"
458"\xff\u00FF"
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100459"\uD800" // illegal: surrogate half (TODO: probably should allow)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100460"\U00110000" // illegal: invalid Unicode code point
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400461
462#"This is not an \(interpolation)"#
463#"This is an \#(interpolation)"#
464#"The sequence "\U0001F604" renders as \#U0001F604."#
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100465```
466
467These examples all represent the same string:
468
469```
470"日本語" // UTF-8 input text
471'日本語' // UTF-8 input text as byte sequence
472`日本語` // UTF-8 input text as a raw literal
473"\u65e5\u672c\u8a9e" // the explicit Unicode code points
474"\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points
475"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes
476```
477
478If the source code represents a character as two code points, such as a
479combining form involving an accent and a letter, the result will appear as two
480code points if placed in a string literal.
481
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400482Strings and byte sequences have a multiline equivalent.
483Multiline strings are like their single-line equivalent,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100484but allow newline characters.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100485
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400486Multiline strings and byte sequences respectively start with
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100487a triple double quote (`"""`) or triple single quote (`'''`),
488immediately followed by a newline, which is discarded from the string contents.
489The string is closed by a matching triple quote, which must be by itself
490on a newline, preceded by optional whitespace.
491The whitespace before a closing triple quote must appear before any non-empty
492line after the opening quote and will be removed from each of these
493lines in the string literal.
494A closing triple quote may not appear in the string.
495To include it is suffices to escape one of the quotes.
496
497```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100498"""
499 lily:
500 out of the water
501 out of itself
502
503 bass
504 picking bugs
505 off the moon
506 — Nick Virgilio, Selected Haiku, 1988
507 """
508```
509
510This represents the same string as:
511
512```
513"lily:\nout of the water\nout of itself\n\n" +
514"bass\npicking bugs\noff the moon\n" +
515" — Nick Virgilio, Selected Haiku, 1988"
516```
517
518<!-- TODO: other values
519
520Support for other values:
521- Duration literals
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +0100522- regular expessions: `re("[a-z]")`
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100523-->
524
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500525
526## Values
527
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100528In addition to simple values like `"hello"` and `42.0`, CUE has _structs_.
529A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`.
530Structs are CUE's only way of building up complex values;
531lists, which we will see later,
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500532are defined in terms of structs.
533
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100534All possible values are ordered in a lattice,
535a partial order where every two elements have a single greatest lower bound.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500536A value `a` is an _instance_ of a value `b`,
537denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`,
538that is if `a` orders before `b` in the partial order
539(`⊑` is _not_ a CUE operator).
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100540We also say that `b` _subsumes_ `a` in this case.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500541In graphical terms, `b` is "above" `a` in the lattice.
542
543At the top of the lattice is the single ancestor of all values, called
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100544_top_, denoted `_` in CUE.
545Every value is an instance of top.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500546
547At the bottom of the lattice is the value called _bottom_, denoted `_|_`.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100548A bottom value usually indicates an error.
549Bottom is an instance of every value.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500550
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100551An _atom_ is any value whose only instances are itself and bottom.
552Examples of atoms are `42.0`, `"hello"`, `true`, `null`.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500553
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100554A value is _concrete_ if it is either an atom, or a struct all of whose
555field values are themselves concrete, recursively.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500556
557CUE's values also include what we normally think of as types, like `string` and
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100558`float`.
559But CUE does not distinguish between types and values; only the
560relationship of values in the lattice is important.
561Each CUE "type" subsumes the concrete values that one would normally think
562of as part of that type.
563For example, "hello" is an instance of `string`, and `42.0` is an instance of
564`float`.
565In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`.
566We informally call these CUE's "basic types".
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100567
568
569```
570false ⊑ bool
571true ⊑ bool
572true ⊑ true
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01005735.0 ⊑ float
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100574bool ⊑ _
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100575_|_ ⊑ _
576_|_ ⊑ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100577
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100578_ ⋢ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100579_ ⋢ bool
580int ⋢ bool
581bool ⋢ int
582false ⋢ true
583true ⋢ false
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100584float ⋢ 5.0
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01005855 ⋢ 6
586```
587
588
589### Unification
590
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500591The _unification_ of values `a` and `b`
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100592is defined as the greatest lower bound of `a` and `b`. (That is, the
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500593value `u` such that `u ⊑ a` and `u ⊑ b`,
594and for any other value `v` for which `v ⊑ a` and `v ⊑ b`
595it holds that `v ⊑ u`.)
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500596Since CUE values form a lattice, the unification of two CUE values is
597always unique.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100598
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500599These all follow from the definition of unification:
600- The unification of `a` with itself is always `a`.
601- The unification of values `a` and `b` where `a ⊑ b` is always `a`.
602- The unification of a value with bottom is always bottom.
603
604Unification in CUE is a [binary expression](#Operands), written `a & b`.
605It is commutative and associative.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100606As a consequence, order of evaluation is irrelevant, a property that is key
607to many of the constructs in the CUE language as well as the tooling layered
608on top of it.
609
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500610
611
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100612<!-- TODO: explicitly mention that disjunction is not a binary operation
613but a definition of a single value?-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100614
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100615
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100616### Disjunction
617
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500618The _disjunction_ of values `a` and `b`
619is defined as the least upper bound of `a` and `b`.
620(That is, the value `d` such that `a ⊑ d` and `b ⊑ d`,
621and for any other value `e` for which `a ⊑ e` and `b ⊑ e`,
622it holds that `d ⊑ e`.)
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100623This style of disjunctions is sometimes also referred to as sum types.
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500624Since CUE values form a lattice, the disjunction of two CUE values is always unique.
625
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100626
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500627These all follow from the definition of disjunction:
628- The disjunction of `a` with itself is always `a`.
629- The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`.
630- The disjunction of a value `a` with bottom is always `a`.
631- The disjunction of two bottom values is bottom.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100632
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500633Disjunction in CUE is a [binary expression](#Operands), written `a | b`.
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100634It is commutative, associative, and idempotent.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100635
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100636The unification of a disjunction with another value is equal to the disjunction
637composed of the unification of this value with all of the original elements
638of the disjunction.
639In other words, unification distributes over disjunction.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100640
641```
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100642(a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100643```
644
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100645```
646Expression Result
647({a:1} | {b:2}) & {c:3} {a:1, c:3} | {b:2, c:3}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100648(int | string) & "foo" "foo"
649("a" | "b") & "c" _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100650```
651
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100652A disjunction is _normalized_ if there is no element
653`a` for which there is an element `b` such that `a ⊑ b`.
654
655<!--
656Normalization is important, as we need to account for spurious elements
657For instance "tcp" | "tcp" should resolve to "tcp".
658
659Also consider
660
661 ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2},
662
663in this case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1} and thus
664this expression is logically equivalent to {a:1} and should therefore be
665considered to be unambiguous and resolve to {a:1} if a concrete value is needed.
666
667For instance, in
668
669 x: ({a:1} | {b:1}) & ({a:1} | {b:2}) // -> {a:1} | {a:1,b:1} | {a:1,b:2}
670 y: x.a // 1
671
672y should resolve to 1, and not an error.
673
674For comparison, in
675
676 x: ({a:1, b:1} | {b:2}) & {a:1} // -> {a:1,b:1} | {a:1,b:2}
677 y: x.a // _|_
678
679y should be an error as x is still ambiguous before the selector is applied,
680even though `a` resolves to 1 in all cases.
681-->
682
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500683
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100684#### Default values
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500685
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100686Any element of a disjunction can be marked as a default
687by prefixing it with an asterisk '*'.
688Intuitively, when an expression needs to be resolved for an operation other
689than unification or disjunctions,
690non-starred elements are dropped in favor of starred ones if the starred ones
691do not resolve to bottom.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500692
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100693More precisely, any value `v` may be associated with a default value `d`,
694denoted `(v, d)` (not CUE syntax),
695where `d` must be in instance of `v` (`d ⊑ v`).
696The rules for unifying and disjoining such values are as follows:
697
698```
699U1: (v1, d1) & v2 => (v1&v2, d1&v2)
700U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2)
701
702D1: (v1, d1) | v2 => (v1|v2, d1)
703D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2)
704```
705
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100706Default values may be introduced within disjunctions
707by _marking_ terms of a disjunction with an asterisk `*`
708([a unary expression](#Operators)).
709The default value of a disjunction with marked terms is the disjunction
710of those marked terms, applying the following rules for marks:
711
712```
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200713M1: *v => (v, v)
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100714M2: *(v1, d1) => (v1, d1)
715```
716
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200717In general, any operation in CUE involving default values proceeds along the
718following lines
719```
720O1: f((v1, d1), ..., (vn, dn)) => (fn(v1, ..., vn), fn(d1, ..., dn))
721```
722where, with the exception of disjunction, a value `v` without a default
723value is promoted to `(v, v)`.
724
725
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100726```
727Expression Value-default pair Rules applied
728*"tcp" | "udp" ("tcp"|"udp", "tcp") M1, D1
729string | *"foo" (string, "foo") M1, D1
730
731*1 | 2 | 3 (1|2|3, 1) M1, D1
732
733(*1|2|3) | (1|*2|3) (1|2|3, 1|2) M1, D1, D2
734(*1|2|3) | *(1|*2|3) (1|2|3, 1|2) M1, D1, M2, D2
735(*1|2|3) | (1|*2|3)&2 (1|2|3, 1|2) M1, D1, U1, D2
736
737(*1|2) & (1|*2) (1|2, _|_) M1, D1, U2
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200738
739(*1|2) + (1|*2) ((1|2)+(1|2), 3) M1, D1, O1
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100740```
741
742The rules of subsumption for defaults can be derived from the above definitions
743and are as follows.
744
745```
746(v2, d2) ⊑ (v1, d1) if v2 ⊑ v1 and d2 ⊑ d1
747(v1, d1) ⊑ v if v1 ⊑ v
748v ⊑ (v1, d1) if v ⊑ d1
749```
750
751<!--
752For the second rule, note that by definition d1 ⊑ v1, so d1 ⊑ v1 ⊑ v.
753
754The last one is so restrictive as v could still be made more specific by
755associating it with a default that is not subsumed by d1.
756
757Proof:
758 by definition for any d ⊑ v, it holds that (v, d) ⊑ v,
759 where the most general value is (v, v).
760 Given the subsumption rule for (v2, d2) ⊑ (v1, d1),
761 from (v, v) ⊑ v ⊑ (v1, d1) it follows that v ⊑ d1
762 exactly defines the boundary of this subsumption.
763-->
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100764
765<!--
766(non-normalized entries could also be implicitly marked, allowing writing
767int | 1, instead of int | *1, but that can be done in a backwards
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100768compatible way later if really desirable, as long as we require that
769disjunction literals be normalized).
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500770-->
771
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100772
773```
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100774Expression Resolves to
775"tcp" | "udp" "tcp" | "udp"
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100776*"tcp" | "udp" "tcp"
777float | *1 1
778*string | 1.0 string
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100779
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100780(*1|2|3) | (1|*2|3) 1|2
781(*1|2|3) & (1|*2|3) 1|2|3 // default is _|_
782
783(* >=5 | int) & (* <=5 | int) 5
784
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100785(*"tcp"|"udp") & ("udp"|*"tcp") "tcp"
786(*"tcp"|"udp") & ("udp"|"tcp") "tcp"
787(*"tcp"|"udp") & "tcp" "tcp"
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100788(*"tcp"|"udp") & (*"udp"|"tcp") "tcp" | "udp" // default is _|_
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100789
790(*true | false) & bool true
791(*true | false) & (true | false) true
792
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100793{a: 1} | {b: 1} {a: 1} | {b: 1}
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100794{a: 1} | *{b: 1} {b:1}
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100795*{a: 1} | *{b: 1} {a: 1} | {b: 1}
796({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} by normalization
797({a:1}|*{b:1}) & ({a:1}|*{b:1}) {b:1} // after eliminating {a:1,b:1} by normalization
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100798```
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500799
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100800
801### Bottom and errors
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100802
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100803Any evaluation error in CUE results in a bottom value, respresented by
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100804the token '_|_'.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100805Bottom is an instance of every other value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100806Any evaluation error is represented as bottom.
807
808Implementations may associate error strings with different instances of bottom;
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500809logically they all remain the same value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100810
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100811
812### Top
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100813
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100814Top is represented by the underscore character '_', lexically an identifier.
815Unifying any value `v` with top results `v` itself.
816
817```
818Expr Result
819_ & 5 5
820_ & _ _
821_ & _|_ _|_
822_ | _|_ _
823```
824
825
826### Null
827
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100828The _null value_ is represented with the keyword `null`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100829It has only one parent, top, and one child, bottom.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100830It is unordered with respect to any other value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100831
832```
833null_lit = "null"
834```
835
836```
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100837null & 8 _|_
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100838null & _ null
839null & _|_ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100840```
841
842
843### Boolean values
844
845A _boolean type_ represents the set of Boolean truth values denoted by
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100846the keywords `true` and `false`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100847The predeclared boolean type is `bool`; it is a defined type and a separate
848element in the lattice.
849
850```
851boolean_lit = "true" | "false"
852```
853
854```
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100855bool & true true
856true & true true
857true & false _|_
858bool & (false|true) false | true
859bool & (true|false) true | false
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100860```
861
862
863### Numeric values
864
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500865The _integer type_ represents the set of all integral numbers.
866The _decimal floating-point type_ represents the set of all decimal floating-point
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100867numbers.
868They are two distinct types.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500869The predeclared integer and decimal floating-point types are `int` and `float`;
870they are defined types.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100871
872A decimal floating-point literal always has type `float`;
873it is not an instance of `int` even if it is an integral number.
874
875An integer literal has both type `int` and `float`, with the integer variant
876being the default if no other constraints are applied.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500877Expressed in terms of disjunction and [type conversion](#conversions),
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100878the literal `1`, for instance, is defined as `int(1) | float(1)`.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100879Hexadecimal, octal, and binary integer literals are always of type `int`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100880
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100881Numeric literals are exact values of arbitrary precision.
882If the operation permits it, numbers should be kept in arbitrary precision.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100883
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100884Implementation restriction: although numeric values have arbitrary precision
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100885in the language, implementations may implement them using an internal
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100886representation with limited precision.
887That said, every implementation must:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100888
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500889- Represent integer values with at least 256 bits.
890- Represent floating-point values, with a mantissa of at least 256 bits and
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100891a signed binary exponent of at least 16 bits.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500892- Give an error if unable to represent an integer value precisely.
893- Give an error if unable to represent a floating-point value due to overflow.
894- Round to the nearest representable value if unable to represent
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100895a floating-point value due to limits on precision.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100896These requirements apply to the result of any expression except for builtin
897functions for which an unusual loss of precision must be explicitly documented.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100898
899
900### Strings
901
902The _string type_ represents the set of all possible UTF-8 strings,
903not allowing surrogates.
904The predeclared string type is `string`; it is a defined type.
905
906Strings are designed to be unicode-safe.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500907Comparison is done using canonical forms ("é" == "e\u0301").
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100908A string element is an
909[extended grapheme cluster](https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries),
910which is an approximation of a human-readable character.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100911
912The length of a string `s` (its size in bytes) can be discovered using
913the built-in function len.
914A string's extended grapheme cluster can be accessed by integer index
9150 through len(s)-1 for any byte that is part of that grapheme cluster.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100916
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100917To access the individual bytes of a string one should convert it to
918a sequence of bytes first.
919
920
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +0100921### Bounds
922
923A _bound_, syntactically_ a [unary expression](#Operands), defines
Marcel van Lohuizen62b87272019-02-01 10:07:49 +0100924an infinite disjunction of concrete values than can be represented
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +0100925as a single comparison.
926
927For any [comparison operator](#Comparison-operators) `op` except `==`,
928`op a` is the disjunction of every `x` such that `x op a`.
929
930```
9312 & >=2 & <=5 // 2, where 2 is either an int or float.
9322.5 & >=1 & <=5 // 2.5
9332 & >=1.0 & <3.0 // 2.0
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01009342 & >1 & <3.0 // 2.0
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01009352.5 & int & >1 & <5 // _|_
9362.5 & float & >1 & <5 // 2.5
937int & 2 & >1.0 & <3.0 // _|_
9382.5 & >=(int & 1) & <5 // _|_
939>=0 & <=7 & >=3 & <=10 // >=3 & <=7
940!=null & 1 // 1
941>=5 & <=5 // 5
942```
943
944
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100945### Structs
946
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500947A _struct_ is a set of elements called _fields_, each of
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100948which has a name, called a _label_, and value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100949
950We say a label is defined for a struct if the struct has a field with the
951corresponding label.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100952The value for a label `f` of struct `a` is denoted `f.a`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100953A struct `a` is an instance of `b`, or `a ⊑ b`, if for any label `f`
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100954defined for `b`, label `f` is also defined for `a` and `a.f ⊑ b.f`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100955Note that if `a` is an instance of `b` it may have fields with labels that
956are not defined for `b`.
957
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500958The (unique) struct with no fields, written `{}`, has every struct as an
959instance. It can be considered the type of all structs.
960
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100961The successful unification of structs `a` and `b` is a new struct `c` which
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100962has all fields of both `a` and `b`, where
963the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`,
964or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively.
965Any [references](#References) to `a` or `b`
966in their respective field values need to be replaced with references to `c`.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100967The result of a unification is bottom (`_|_`) if any of its fields evaluates
968to bottom, recursively.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100969
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100970A field name may also be an interpolated string.
971Identifiers used in such strings are evaluated within
972the scope of the struct in which the label is defined.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500973
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100974Syntactically, a struct literal may contain multiple fields with
975the same label, the result of which is a single field with a value
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500976that is the unification of the values of those fields.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100977
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100978A TemplateLabel indicates a template value that is to be unified with
979the values of all fields within a struct.
980The identifier of a template label binds to the field name of each
981field and is visible within the template value.
982
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100983```
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +0100984StructLit = "{" [ Declaration { "," Declaration } [ "," ] ] "}" .
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100985Declaration = FieldDecl | AliasDecl | ComprehensionDecl .
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +0100986FieldDecl = Label { Label } ":" Expression { attribute } .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100987
988AliasDecl = Label "=" Expression .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100989TemplateLabel = "<" identifier ">" .
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +0100990ConcreteLabel = identifier | simple_string_lit
991OptionalLabel = ConcreteLabel "?"
992Label = ConcreteLabel | OptionalLabel | TemplateLabel .
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +0100993
994attribute = "@" identifier "(" attr_elem { "," attr_elem } ")" .
995attr_elem = attr_string | identifier "=" attr_string .
996attr_string = { attr_char } | string_lit .
997attr_char = /* an arbitrary Unicode code point except newline, ',', '"', `'`, '#', '=', '(', and ')' */ .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100998```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100999
1000```
1001{a: 1} ⊑ {}
1002{a: 1, b: 1} ⊑ {a: 1}
1003{a: 1} ⊑ {a: int}
1004{a: 1, b: 1} ⊑ {a: int, b: float}
1005
1006{} ⋢ {a: 1}
1007{a: 2} ⋢ {a: 1}
1008{a: 1} ⋢ {b: 1}
1009```
1010
1011```
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001012Expression Result
1013{a: int, a: 1} {a: int(1)}
1014{a: int} & {a: 1} {a: int(1)}
1015{a: >=1 & <=7} & {a: >=5 & <=9} {a: >=5 & <=7}
1016{a: >=1 & <=7, a: >=5 & <=9} {a: >=5 & <=7}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001017
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001018{a: 1} & {b: 2} {a: 1, b: 2}
1019{a: 1, b: int} & {b: 2} {a: 1, b: int(2)}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001020
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001021{a: 1} & {a: 2} _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001022```
1023
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001024Fields may be associated with attributes.
1025Attributes define additional information about a field,
1026such as a mapping to a protobuf tag or alternative
1027name of the field when mapping to a different language.
1028
1029If a field has multiple attributes their identifiers must be unique.
1030Attributes accumulate when unifying two fields, removing duplicate entries.
1031It is an error for the resulting field to have two different attributes
1032with the same identifier.
1033
1034Attributes are not directly part of the data model, but may be
1035accessed through the API or other means of reflection.
1036The interpretation of the attribute value
1037(a comma-separated list of attribute elements) depends on the attribute.
1038Interpolations are not allowed in attribute strings.
1039
1040The recommended convention, however, is to interpret the first
1041`n` arguments as positional arguments,
1042where duplicate conflicting entries are an error,
1043and the remaining arguments as a combination of flags
1044(an identifier) and key value pairs, separated by a `=`.
1045
1046```
1047MyStruct1: {
1048 field: string @go(Field)
1049 attr: int @xml(,attr) @go(Attr)
1050}
1051
1052MyStruct2: {
1053 field: string @go(Field)
1054 attr: int @xml(a1,attr) @go(Attr)
1055}
1056
1057Combined: MyStruct1 & MyStruct2
1058// field: string @go(Field)
1059// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)
1060```
1061
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001062In addition to fields, a struct literal may also define aliases.
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001063Aliases name values that can be referred to
1064within the [scope](#declarations-and-scopes) of their
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001065definition, but are not part of the struct: aliases are irrelevant to
1066the partial ordering of values and are not emitted as part of any
1067generated data.
1068The name of an alias must be unique within the struct literal.
1069
1070```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001071// The empty struct.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001072{}
1073
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001074// A struct with 3 fields and 1 alias.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001075{
1076 alias = 3
1077
1078 foo: 2
1079 bar: "a string"
1080
1081 "not an ident": 4
1082}
1083```
1084
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001085A field whose value is a struct with a single field may be written as
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001086a sequence of the two field names,
1087followed by a colon and the value of that single field.
1088
1089```
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001090job myTask replicas: 2
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001091```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001092expands to
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001093```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001094job: {
1095 myTask: {
1096 replicas: 2
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001097 }
1098}
1099```
1100
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001101
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +01001102#### Optional fields
1103
1104An identifier or string label may be followed by a question mark `?`
1105to indicate a field is optional.
Marcel van Lohuizen8bc02e52019-04-01 13:14:07 +02001106The question mark is not part of the field name.
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +01001107Constraints defined by an optional field should only be applied when
1108a field is present.
Marcel van Lohuizen8bc02e52019-04-01 13:14:07 +02001109A field with such a marker may be omitted from output and should not cause
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +01001110an error when emitting a concrete configuration, even if its value is
1111not concrete or bottom.
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +01001112The result of unifying two fields only has an optional marker
1113if both fields have such a marker.
1114
1115<!--
1116The optional marker solves the issue of having to print large amounts of
1117boilerplate when dealing with large types with many optional or default
1118values (such as Kubernetes).
1119Writing such optional values in terms of *null | value is tedious,
1120unpleasant to read, and as it is not well defined what can be dropped or not,
1121all null values have to be emitted from the output, even if the user
1122doesn't override them.
1123Part of the issue is how null is defined. We could adopt a Typescript-like
1124approach of introducing "void" or "undefined" to mean "not defined and not
1125part of the output". But having all of null, undefined, and void can be
1126confusing. If these ever are introduced anyway, the ? operator could be
1127expressed along the lines of
1128 foo?: bar
1129being a shorthand for
1130 foo: void | bar
1131where void is the default if no other default is given.
1132
1133The current mechanical definition of "?" is straightforward, though, and
1134probably avoids the need for void, while solving a big issue.
1135
1136Caveats:
1137[1] this definition requires explicitly defined fields to be emitted, even
1138if they could be elided (for instance if the explicit value is the default
1139value defined an optional field). This is probably a good thing.
1140
1141[2] a default value may still need to be included in an output if it is not
1142the zero value for that field and it is not known if any outside system is
1143aware of defaults. For instance, which defaults are specified by the user
1144and which by the schema understood by the receiving system.
1145The use of "?" together with defaults should therefore be used carefully
1146in non-schema definitions.
1147Problematic cases should be easy to detect by a vet-like check, though.
1148
1149[3] It should be considered how this affects the trim command.
1150Should values implied by optional fields be allowed to be removed?
1151Probably not. This restriction is unlikely to limit the usefulness of trim,
1152though.
1153
1154[4] There should be an option to emit all concrete optional values.
1155```
1156-->
1157
1158```
1159Input Result
1160a: { foo?: string } {}
1161b: { foo: "bar" } { foo: "bar" }
1162c: { foo?: *"bar" | string } {}
1163
1164d: a & b { foo: "bar" }
1165e: b & c { foo: "bar" }
1166f: a & c {}
1167g: a & { foo?: number } _|_
1168```
1169
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001170
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001171### Lists
1172
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001173A list literal defines a new value of type list.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001174A list may be open or closed.
1175An open list is indicated with a `...` at the end of an element list,
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001176optionally followed by a value for the remaining elements.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001177
1178The length of a closed list is the number of elements it contains.
1179The length of an open list is the its number of elements as a lower bound
1180and an unlimited number of elements as its upper bound.
1181
1182```
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01001183ListLit = "[" [ ElementList [ "," [ "..." [ Expression ] ] ] "]" .
1184ElementList = Expression { "," Expression } .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001185```
1186<!---
1187KeyedElement = Element .
1188--->
1189
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001190Lists can be thought of as structs:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001191
1192```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001193List: *null | {
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001194 Elem: _
1195 Tail: List
1196}
1197```
1198
1199For closed lists, `Tail` is `null` for the last element, for open lists it is
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001200`*null | List`, defaulting to the shortest variant.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001201For instance, the open list [ 1, 2, ... ] can be represented as:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001202```
1203open: List & { Elem: 1, Tail: { Elem: 2 } }
1204```
1205and the closed version of this list, [ 1, 2 ], as
1206```
1207closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } }
1208```
1209
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001210Using this representation, the subsumption rule for lists can
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001211be derived from those of structs.
1212Implementations are not required to implement lists as structs.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001213The `Elem` and `Tail` fields are not special and `len` will not work as
1214expected in these cases.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001215
1216
1217## Declarations and Scopes
1218
1219
1220### Blocks
1221
1222A _block_ is a possibly empty sequence of declarations.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001223The braces of a struct literal `{ ... }` form a block, but there are
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001224others as well:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001225
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001226- The _universe block_ encompasses all CUE source text.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001227- Each [package](#modules-instances-and-packages) has a _package block_
1228 containing all CUE source text in that package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001229- Each file has a _file block_ containing all CUE source text in that file.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001230- Each `for` and `let` clause in a [comprehension](#comprehensions)
1231 is considered to be its own implicit block.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001232
1233Blocks nest and influence [scoping].
1234
1235
1236### Declarations and scope
1237
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001238A _declaration_ binds an identifier to a field, alias, or package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001239Every identifier in a program must be declared.
1240Other than for fields,
1241no identifier may be declared twice within the same block.
1242For fields an identifier may be declared more than once within the same block,
1243resulting in a field with a value that is the result of unifying the values
1244of all fields with the same identifier.
1245
1246```
1247TopLevelDecl = Declaration | Emit .
1248Emit = Operand .
1249```
1250
1251The _scope_ of a declared identifier is the extent of source text in which the
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001252identifier denotes the specified field, alias, or package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001253
1254CUE is lexically scoped using blocks:
1255
Jonathan Amsterdame4790382019-01-20 10:29:29 -050012561. The scope of a [predeclared identifier](#predeclared-identifiers) is the universe block.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010012571. The scope of an identifier denoting a field or alias
1258 declared at top level (outside any struct literal) is the file block.
12591. The scope of the package name of an imported package is the file block of the
1260 file containing the import declaration.
12611. The scope of a field or alias identifier declared inside a struct literal
1262 is the innermost containing block.
1263
1264An identifier declared in a block may be redeclared in an inner block.
1265While the identifier of the inner declaration is in scope, it denotes the entity
1266declared by the inner declaration.
1267
1268The package clause is not a declaration;
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001269the package name does not appear in any scope.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001270Its purpose is to identify the files belonging to the same package
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001271and to specify the default name for import declarations.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001272
1273
1274### Predeclared identifiers
1275
1276```
1277Functions
1278len required close open
1279
1280Types
1281null The null type and value
1282bool All boolean values
1283int All integral numbers
1284float All decimal floating-point numbers
1285string Any valid UTF-8 sequence
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001286bytes Any vallid byte sequence
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001287
1288Derived Value
1289number int | float
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001290uint >=0
1291uint8 >=0 & <=255
1292int8 >=-128 & <=127
1293uint16 >=0 & <=65536
1294int16 >=-32_768 & <=32_767
1295rune >=0 & <=0x10FFFF
1296uint32 >=0 & <=4_294_967_296
1297int32 >=-2_147_483_648 & <=2_147_483_647
1298uint64 >=0 & <=18_446_744_073_709_551_615
1299int64 >=-9_223_372_036_854_775_808 & <=9_223_372_036_854_775_807
1300uint128 >=0 & <=340_282_366_920_938_463_463_374_607_431_768_211_455
1301int128 >=-170_141_183_460_469_231_731_687_303_715_884_105_728 &
1302 <=170_141_183_460_469_231_731_687_303_715_884_105_727
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001303```
1304
1305
1306### Exported and manifested identifiers
1307
1308An identifier of a package may be exported to permit access to it
1309from another package.
1310An identifier is exported if both:
1311the first character of the identifier's name is not a Unicode lower case letter
1312(Unicode class "Ll") or the underscore "_"; and
1313the identifier is declared in the file block.
1314All other identifiers are not exported.
1315
1316An identifier that starts with the underscore "_" is not
1317emitted in any data output.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001318Quoted labels that start with an underscore are emitted, however.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001319
1320### Uniqueness of identifiers
1321
1322Given a set of identifiers, an identifier is called unique if it is different
1323from every other in the set, after applying normalization following
1324Unicode Annex #31.
1325Two identifiers are different if they are spelled differently.
1326<!--
1327or if they appear in different packages and are not exported.
1328--->
1329Otherwise, they are the same.
1330
1331
1332### Field declarations
1333
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001334A field declaration binds a label (the name of the field) to an expression.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001335The name for a quoted string used as label is the string it represents.
1336Tne name for an identifier used as a label is the identifier itself.
1337Quoted strings and identifiers can be used used interchangeably, with the
1338exception of identifiers starting with an underscore '_'.
1339The latter represent hidden fields and are treated in a different namespace.
1340
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001341If an expression may result in a value associated with a default value
1342as described in [default values](#default-values), the field binds to this
1343value-default pair.
1344
Marcel van Lohuizenbcf832f2019-04-03 22:50:44 +02001345<!-- TODO: disallow creating identifiers starting with __
1346...and reserve them for builtin values.
1347
1348The issue is with code generation. As no guarantee can be given that
1349a predeclared identifier is not overridden in one of the enclosing scopes,
1350code will have to handle detecting such cases and renaming them.
1351An alternative is to have the predeclared identifiers be aliases for namesake
1352equivalents starting with a double underscore (e.g. string -> __string),
1353allowing generated code (normal code would keep using `string`) to refer
1354to these directly.
1355-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001356
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001357
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001358### Alias declarations
1359
1360An alias declaration binds an identifier to the given expression.
1361
1362Within the scope of the identifier, it serves as an _alias_ for that
1363expression.
1364The expression is evaluated in the scope as it was declared.
1365
1366
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001367## Expressions
1368
1369An expression specifies the computation of a value by applying operators and
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001370built-in functions to operands.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001371
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001372Expressions that require concrete values are called _incomplete_ if any of
1373their operands are not concrete, but define a value that would be legal for
1374that expression.
1375Incomplete expressions may be left unevaluated until a concrete value is
1376requested at the application level.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001377
1378### Operands
1379
1380Operands denote the elementary values in an expression.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001381An operand may be a literal, a (possibly qualified) identifier denoting
1382field, alias, or a parenthesized expression.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001383
1384```
1385Operand = Literal | OperandName | ListComprehension | "(" Expression ")" .
1386Literal = BasicLit | ListLit | StructLit .
1387BasicLit = int_lit | float_lit | string_lit |
1388 null_lit | bool_lit | bottom_lit | top_lit .
1389OperandName = identifier | QualifiedIdent.
1390```
1391
1392### Qualified identifiers
1393
1394A qualified identifier is an identifier qualified with a package name prefix.
1395
1396```
1397QualifiedIdent = PackageName "." identifier .
1398```
1399
1400A qualified identifier accesses an identifier in a different package,
1401which must be [imported].
1402The identifier must be declared in the [package block] of that package.
1403
1404```
1405math.Sin // denotes the Sin function in package math
1406```
1407
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001408### References
1409
1410An identifier operand refers to a field and is called a reference.
1411The value of a reference is a copy of the expression associated with the field
1412that it is bound to,
1413with any references within that expression bound to the respective copies of
1414the fields they were originally bound to.
1415Implementations may use a different mechanism to evaluate as long as
1416these semantics are maintained.
1417
1418```
1419a: {
1420 place: string
1421 greeting: "Hello, \(place)!"
1422}
1423
1424b: a & { place: "world" }
1425c: a & { place: "you" }
1426
1427d: b.greeting // "Hello, world!"
1428e: c.greeting // "Hello, you!"
1429```
1430
1431
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001432
1433### Primary expressions
1434
1435Primary expressions are the operands for unary and binary expressions.
1436
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001437
1438```
1439
1440Slice: indices must be complete
1441([0, 1, 2, 3] | [2, 3])[0:2] => [0, 1] | [2, 3]
1442
1443([0, 1, 2, 3] | *[2, 3])[0:2] => [0, 1] | [2, 3]
1444([0,1,2,3]|[2,3], [2,3])[0:2] => ([0,1]|[2,3], [2,3])
1445
1446Index
1447a: (1|2, 1)
1448b: ([0,1,2,3]|[2,3], [2,3])[a] => ([0,1,2,3]|[2,3][a], 3)
1449
1450Binary operation
1451A binary is only evaluated if its operands are complete.
1452
1453Input Maximum allowed evaluation
1454a: string string
1455b: 2 2
1456c: a * b a * 2
1457
1458An error in a struct is if the evaluation of any expression results in
1459bottom, where an incomplete expression is not considered bottom.
1460```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01001461<!-- TODO(mpvl)
1462 Conversion |
1463-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001464```
1465PrimaryExpr =
1466 Operand |
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001467 PrimaryExpr Selector |
1468 PrimaryExpr Index |
1469 PrimaryExpr Slice |
1470 PrimaryExpr Arguments .
1471
1472Selector = "." identifier .
1473Index = "[" Expression "]" .
1474Slice = "[" [ Expression ] ":" [ Expression ] "]"
1475Argument = Expression .
1476Arguments = "(" [ ( Argument { "," Argument } ) [ "..." ] [ "," ] ] ")" .
1477```
1478<!---
1479Argument = Expression | ( identifer ":" Expression ).
1480--->
1481
1482```
1483x
14842
1485(s + ".txt")
1486f(3.1415, true)
1487m["foo"]
1488s[i : j + 1]
1489obj.color
1490f.p[i].x
1491```
1492
1493
1494### Selectors
1495
1496For a [primary expression] `x` that is not a [package name],
1497the selector expression
1498
1499```
1500x.f
1501```
1502
1503denotes the field `f` of the value `x`.
1504The identifier `f` is called the field selector.
1505The type of the selector expression is the type of `f`.
1506If `x` is a package name, see the section on [qualified identifiers].
1507
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001508<!--
1509TODO: consider allowing this and also for selectors. It needs to be considered
1510how defaults are corried forward in cases like:
1511
1512 x: { a: string | *"foo" } | *{ a: int | *4 }
1513 y: x.a & string
1514
1515What is y in this case?
1516 (x.a & string, _|_)
1517 (string|"foo", _|_)
1518 (string|"foo", "foo)
1519If the latter, then why?
1520
1521For a disjunction of the form `x1 | ... | xn`,
1522the selector is applied to each element `x1.f | ... | xn.f`.
1523-->
1524
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001525Otherwise, if `x` is not a struct, or if `f` does not exist in `x`,
1526the result of the expression is bottom (an error).
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001527In the latter case the expression is incomplete.
1528The operand of a selector may be associated with a default.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001529
1530```
1531T: {
1532 x: int
1533 y: 3
1534}
1535
1536a: T.x // int
1537b: T.y // 3
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01001538c: T.z // _|_ // field 'z' not found in T
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001539
1540e: {a: 1|*2} | *{a: 3|*4}
1541f: e.a // 4 (default value)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001542```
1543
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001544<!--
1545```
1546(v, d).f => (v.f, d.f)
1547
1548e: {a: 1|*2} | *{a: 3|*4}
1549f: e.a // 4 after selecting default from (({a: 1|*2} | {a: 3|*4}).a, 4)
1550
1551```
1552-->
1553
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001554
1555### Index expressions
1556
1557A primary expression of the form
1558
1559```
1560a[x]
1561```
1562
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001563denotes the element of the list, string, bytes, or struct `a` indexed by `x`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001564The value `x` is called the index or field name, respectively.
1565The following rules apply:
1566
1567If `a` is not a struct:
1568
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001569- `a` is a concrete string or bytes type or a list (which need not be complete)
1570- the index `x` unified with `int` must be concrete.
1571- the index `x` is in range if `0 <= x < len(a)`, where only the
1572 explicitly defined values of an open-ended list are considered,
1573 otherwise it is out of range
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001574
1575The result of `a[x]` is
1576
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001577for `a` of list or bytes type:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001578
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001579- the list or byte element at index `x`, if `x` is within range
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001580- bottom (an error), otherwise
1581
1582for `a` of string type:
1583
1584- the grapheme cluster at the `x`th byte (type string), if `x` is within range
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001585 where `x` may match any byte of the grapheme cluster
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001586- bottom (an error), otherwise
1587
1588for `a` of struct type:
1589
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001590- the index `x` unified with `string` must be concrete.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001591- the value of the field named `x` of struct `a`, if this field exists
1592- bottom (an error), otherwise
1593
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001594
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001595```
1596[ 1, 2 ][1] // 2
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01001597[ 1, 2 ][2] // _|_
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001598[ 1, 2, ...][2] // _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001599"He\u0300?"[0] // "H"
1600"He\u0300?"[1] // "e\u0300"
1601"He\u0300?"[2] // "e\u0300"
1602"He\u0300?"[3] // "e\u0300"
1603"He\u0300?"[4] // "?"
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01001604"He\u0300?"[5] // _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001605```
1606
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001607Both the operand and index value may be a value-default pair.
1608```
1609va[vi] => va[vi]
1610va[(vi, di)] => (va[vi], va[di])
1611(va, da)[vi] => (va[vi], da[vi])
1612(va, da)[(vi, di)] => (va[vi], da[di])
1613```
1614
1615```
1616Fields Result
1617x: [1, 2] | *[3, 4] ([1,2]|[3,4], [3,4])
1618i: int | *1 (int, 1)
1619
1620v: x[i] (x[i], 4)
1621```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001622
1623### Slice expressions
1624
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001625Slice expressions construct a substring or slice from a string, bytes,
1626or list value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001627
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001628For strings, bytes or lists, the primary expression
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001629```
1630a[low : high]
1631```
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001632constructs a substring or slice. The indices `low` and `high` must be
1633concrete integers and select
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001634which elements of operand `a` appear in the result.
1635The result has indices starting at 0 and length equal to `high` - `low`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001636After slicing the list `a`
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001637<!-- TODO(jba): how does slicing open lists work? -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001638
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001639<!-- TODO: consider this.
1640For `a` is a disjunction of the form `a1 | ... | an`, then the result is
1641`a1[low:high] | ... | an[low:high]` observing the above rules.
1642-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001643```
1644a := [1, 2, 3, 4, 5]
1645s := a[1:4]
1646```
1647the list s has length 3 and elements
1648```
1649s[0] == 2
1650s[1] == 3
1651s[2] == 4
1652```
1653For convenience, any of the indices may be omitted.
1654A missing `low` index defaults to zero; a missing `high` index defaults
1655to the length of the sliced operand:
1656```
1657a[2:] // same as a[2 : len(a)]
1658a[:3] // same as a[0 : 3]
1659a[:] // same as a[0 : len(a)]
1660```
1661
1662Indices are in range if `0 <= low <= high <= len(a)`,
1663otherwise they are out of range.
1664For strings, the indices selects the start of the extended grapheme cluster
1665at byte position indicated by the index.
1666If any of the slice values is out of range or if `low > high`, the result of
1667a slice is bottom (error).
1668
1669```
1670"He\u0300?"[:2] // "He\u0300"
1671"He\u0300?"[1:2] // "e\u0300"
1672"He\u0300?"[4:5] // "e\u0300?"
1673```
1674
1675
1676The result of a successful slice operation is a value of the same type
1677as the operand.
1678
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001679Both the slice operand and the slice indices may be associated with a default.
1680
1681<!--
1682```
1683va[vs:ve] => va[vs:ve]
1684va[vs:(ve, de)] => (va[vs:ve], va[vs:de])
1685va[(vs, ds):ve] => (va[vs:ve], va[ds:ve])
1686va[(vs, ds):(ve, de)] => (va[vs:ve], va[ds:de])
1687(va, da)[vs:ve] => (va[vs:ve], da[vs:ve])
1688(va, da)[vs:(ve, de)] => (va[vs:ve], da[vs:de])
1689(va, da)[(vs, ds):ve] => (va[vs:ve], da[ds:ve])
1690(va, da)[(vs, ds):(ve, de)] => (va[vs:ve], da[ds:de])
1691```
1692-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001693
1694### Operators
1695
1696Operators combine operands into expressions.
1697
1698```
1699Expression = UnaryExpr | Expression binary_op Expression .
1700UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
1701
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001702binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op .
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01001703rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001704add_op = "+" | "-" .
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01001705mul_op = "*" | "/" | "%" | "div" | "mod" | "quo" | "rem" .
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01001706unary_op = "+" | "-" | "!" | "*" | rel_op .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001707```
1708
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001709Comparisons are discussed [elsewhere](#Comparison-operators).
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01001710For any binary operators, the operand types must unify.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001711<!-- TODO: durations
1712 unless the operation involves durations.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001713
1714Except for duration operations, if one operand is an untyped [literal] and the
1715other operand is not, the constant is [converted] to the type of the other
1716operand.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001717-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001718
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001719Operands of unary and binary expressions may be associated with a default using
1720the following
1721<!--
1722```
1723O1: op (v1, d1) => (op v1, op d1)
1724
1725O2: (v1, d1) op (v2, d2) => (v1 op v2, d1 op d2)
1726and because v => (v, v)
1727O3: v1 op (v2, d2) => (v1 op v2, v1 op d2)
1728O4: (v1, d1) op v2 => (v1 op v2, d1 op v2)
1729```
1730-->
1731
1732```
1733Field Resulting Value-Default pair
1734a: *1|2 (1|2, 1)
1735b: -a (-a, -1)
1736
1737c: a + 2 (a+2, 3)
1738d: a + a (a+a, 2)
1739```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001740
1741#### Operator precedence
1742
1743Unary operators have the highest precedence.
1744
1745There are eight precedence levels for binary operators.
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001746Multiplication operators binds strongest, followed by
1747addition operators, comparison operators,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001748`&&` (logical AND), `||` (logical OR), `&` (unification),
1749and finally `|` (disjunction):
1750
1751```
1752Precedence Operator
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01001753 7 * / % div mod quo rem
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001754 6 + -
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01001755 5 == != < <= > >= =~ !~
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001756 4 &&
1757 3 ||
1758 2 &
1759 1 |
1760```
1761
1762Binary operators of the same precedence associate from left to right.
1763For instance, `x / y * z` is the same as `(x / y) * z`.
1764
1765```
1766+x
176723 + 3*x[i]
1768x <= f()
1769f() || g()
1770x == y+1 && y == z-1
17712 | int
1772{ a: 1 } & { b: 2 }
1773```
1774
1775#### Arithmetic operators
1776
1777Arithmetic operators apply to numeric values and yield a result of the same type
1778as the first operand. The three of the four standard arithmetic operators
1779`(+, -, *)` apply to integer and decimal floating-point types;
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01001780`+` and `*` also apply to lists and strings.
1781`/` and `%` only apply to decimal floating-point types and
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001782`div`, `mod`, `quo`, and `rem` only apply to integer types.
1783
1784```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001785+ sum integers, floats, lists, strings, bytes
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001786- difference integers, floats
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001787* product integers, floats, lists, strings, bytes
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001788/ quotient floats
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01001789% remainder floats
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001790div division integers
1791mod modulo integers
1792quo quotient integers
1793rem remainder integers
1794```
1795
1796#### Integer operators
1797
1798For two integer values `x` and `y`,
1799the integer quotient `q = x div y` and remainder `r = x mod y `
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001800implement Euclidean division and
1801satisfy the following relationship:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001802
1803```
1804r = x - y*q with 0 <= r < |y|
1805```
1806where `|y|` denotes the absolute value of `y`.
1807
1808```
1809 x y x div y x mod y
1810 5 3 1 2
1811-5 3 -2 1
1812 5 -3 -1 2
1813-5 -3 2 1
1814```
1815
1816For two integer values `x` and `y`,
1817the integer quotient `q = x quo y` and remainder `r = x rem y `
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001818implement truncated division and
1819satisfy the following relationship:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001820
1821```
1822x = q*y + r and |r| < |y|
1823```
1824
1825with `x quo y` truncated towards zero.
1826
1827```
1828 x y x quo y x rem y
1829 5 3 1 2
1830-5 3 -1 -2
1831 5 -3 -1 2
1832-5 -3 1 -2
1833```
1834
1835A zero divisor in either case results in bottom (an error).
1836
1837For integer operands, the unary operators `+` and `-` are defined as follows:
1838
1839```
1840+x is 0 + x
1841-x negation is 0 - x
1842```
1843
1844
1845#### Decimal floating-point operators
1846
1847For decimal floating-point numbers, `+x` is the same as `x`,
1848while -x is the negation of x.
1849The result of a floating-point division by zero is bottom (an error).
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001850<!-- TODO: consider making it +/- Inf -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001851
1852An implementation may combine multiple floating-point operations into a single
1853fused operation, possibly across statements, and produce a result that differs
1854from the value obtained by executing and rounding the instructions individually.
1855
1856
1857#### List operators
1858
1859Lists can be concatenated using the `+` operator.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001860For lists `a` and `b`,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001861```
1862a + b
1863```
1864will produce an open list if `b` is open.
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001865If list `a` is open, its default value, the shortest variant, is selected.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001866
1867```
1868[ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
1869[ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
1870[ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4, ... ]
Marcel van Lohuizen09d814d2019-02-22 19:14:33 +01001871[ 1, 2, ... ] + [ 3, 4, ... ] // [ 1, 2, 3, 4, ... ]
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001872```
1873
Jonathan Amsterdam0500c312019-02-16 18:04:09 -05001874Lists can be multiplied with a non-negative`int` using the `*` operator
Marcel van Lohuizen13e36bd2019-02-01 09:59:18 +01001875to create a repeated the list by the indicated number.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001876```
18773*[1,2] // [1, 2, 1, 2, 1, 2]
Jonathan Amsterdam0500c312019-02-16 18:04:09 -050018783*[1, 2, ...] // [1, 2, 1, 2, 1 ,2, ...]
Marcel van Lohuizen13e36bd2019-02-01 09:59:18 +01001879[byte]*4 // [byte, byte, byte, byte]
Jonathan Amsterdam0500c312019-02-16 18:04:09 -050018800*[1,2] // []
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001881```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001882
1883<!-- TODO(mpvl): should we allow multiplication with a range?
1884If so, how does one specify a list with a range of possible lengths?
1885
1886Suggestion from jba:
1887Multiplication should distribute over disjunction,
1888so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x].
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001889The hard part is figuring out what (>=1 & <=3) * [x] means,
1890since >=1 & <=3 includes many floats.
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001891(mpvl: could constrain arguments to parameter types, but needs to be
1892done consistently.)
1893-->
1894
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001895
1896#### String operators
1897
1898Strings can be concatenated using the `+` operator:
1899```
1900s := "hi " + name + " and good bye"
1901```
1902String addition creates a new string by concatenating the operands.
1903
1904A string can be repeated by multiplying it:
1905
1906```
1907s: "etc. "*3 // "etc. etc. etc. "
1908```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001909<!-- jba: Do these work for byte sequences? If not, why not? -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001910
1911##### Comparison operators
1912
1913Comparison operators compare two operands and yield an untyped boolean value.
1914
1915```
1916== equal
1917!= not equal
1918< less
1919<= less or equal
1920> greater
1921>= greater or equal
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001922=~ matches regular expression
1923!~ does not match regular expression
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001924```
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001925<!-- regular expression operator inspired by Bash, Perl, and Ruby. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001926
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001927In any comparison, the types of the two operands must unify or one of the
1928operands must be null.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001929
1930The equality operators `==` and `!=` apply to operands that are comparable.
1931The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered.
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001932The matching operators `=~` and `!~` apply to a string and regular
1933expression operand.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001934These terms and the result of the comparisons are defined as follows:
1935
Marcel van Lohuizen855243e2019-02-07 18:00:55 +01001936- Null is comparable with itself and any other type.
1937 Two null values are always equal, null is unequal with anything else.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001938- Boolean values are comparable.
1939 Two boolean values are equal if they are either both true or both false.
1940- Integer values are comparable and ordered, in the usual way.
1941- Floating-point values are comparable and ordered, as per the definitions
1942 for binary coded decimals in the IEEE-754-2008 standard.
Marcel van Lohuizen4a360992019-05-11 18:18:31 +02001943- Floating point numbers may be compared with integers.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001944- String values are comparable and ordered, lexically byte-wise after
1945 normalization to Unicode normal form NFC.
1946- Struct are not comparable.
Marcel van Lohuizen855243e2019-02-07 18:00:55 +01001947- Lists are not comparable.
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001948- The regular expression syntax is the one accepted by RE2,
1949 described in https://github.com/google/re2/wiki/Syntax,
1950 except for `\C`.
1951- `s =~ r` is true if `s` matches the regular expression `r`.
1952- `s !~ r` is true if `s` does not match regular expression `r`.
1953<!-- TODO: Implementations should adopt an algorithm that runs in linear time? -->
Marcel van Lohuizen88a8a5f2019-02-20 01:26:22 +01001954<!-- Consider implementing Level 2 of Unicode regular expression. -->
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001955
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001956```
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +010019573 < 4 // true
Marcel van Lohuizen4a360992019-05-11 18:18:31 +020019583 < 4.0 // true
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01001959null == 2 // false
1960null != {} // true
1961{} == {} // _|_: structs are not comparable against structs
1962
1963"Wild cats" =~ "cat" // true
1964"Wild cats" !~ "dog" // true
1965
1966"foo" =~ "^[a-z]{3}$" // true
1967"foo" =~ "^[a-z]{4}$" // false
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001968```
1969
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001970<!-- jba
1971I think I know what `3 < a` should mean if
1972
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001973 a: >=1 & <=5
1974
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001975It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely.
1976
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001977But what does `3 < (>=1 & <=5)` mean? We'll never get more information, so it must have a definite value.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001978-->
1979
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001980#### Logical operators
1981
1982Logical operators apply to boolean values and yield a result of the same type
1983as the operands. The right operand is evaluated conditionally.
1984
1985```
1986&& conditional AND p && q is "if p then q else false"
1987|| conditional OR p || q is "if p then true else q"
1988! NOT !p is "not p"
1989```
1990
1991
1992<!--
1993### TODO TODO TODO
1994
19953.14 / 0.0 // illegal: division by zero
1996Illegal conversions always apply to CUE.
1997
1998Implementation 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.
1999-->
2000
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002001<!--- TODO(mpvl): conversions
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002002### Conversions
2003Conversions are expressions of the form `T(x)` where `T` and `x` are
2004expressions.
2005The result is always an instance of `T`.
2006
2007```
2008Conversion = Expression "(" Expression [ "," ] ")" .
2009```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002010--->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002011<!---
2012
2013A literal value `x` can be converted to type T if `x` is representable by a
2014value of `T`.
2015
2016As a special case, an integer literal `x` can be converted to a string type
2017using the same rule as for non-constant x.
2018
2019Converting a literal yields a typed value as result.
2020
2021```
2022uint(iota) // iota value of type uint
2023float32(2.718281828) // 2.718281828 of type float32
2024complex128(1) // 1.0 + 0.0i of type complex128
2025float32(0.49999999) // 0.5 of type float32
2026float64(-1e-1000) // 0.0 of type float64
2027string('x') // "x" of type string
2028string(0x266c) // "♬" of type string
2029MyString("foo" + "bar") // "foobar" of type MyString
2030string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
2031(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
2032int(1.2) // illegal: 1.2 cannot be represented as an int
2033string(65.0) // illegal: 65.0 is not an integer constant
2034```
2035--->
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002036<!---
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002037
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002038A conversion is always allowed if `x` is an instance of `T`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002039
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002040If `T` and `x` of different underlying type, a conversion is allowed if
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002041`x` can be converted to a value `x'` of `T`'s type, and
2042`x'` is an instance of `T`.
2043A value `x` can be converted to the type of `T` in any of these cases:
2044
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002045- `x` is a struct and is subsumed by `T`.
2046- `x` and `T` are both integer or floating points.
2047- `x` is an integer or a byte sequence and `T` is a string.
2048- `x` is a string and `T` is a byte sequence.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002049
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002050Specific rules apply to conversions between numeric types, structs,
2051or to and from a string type. These conversions may change the representation
2052of `x`.
2053All other conversions only change the type but not the representation of x.
2054
2055
2056#### Conversions between numeric ranges
2057For the conversion of numeric values, the following rules apply:
2058
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +010020591. Any integer value can be converted into any other integer value
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002060 provided that it is within range.
20612. When converting a decimal floating-point number to an integer, the fraction
2062 is discarded (truncation towards zero). TODO: or disallow truncating?
2063
2064```
2065a: uint16(int(1000)) // uint16(1000)
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002066b: uint8(1000) // _|_ // overflow
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002067c: int(2.5) // 2 TODO: TBD
2068```
2069
2070
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002071#### Conversions to and from a string type
2072
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002073Converting a list of bytes to a string type yields a string whose successive
2074bytes are the elements of the slice.
2075Invalid UTF-8 is converted to `"\uFFFD"`.
2076
2077```
2078string('hell\xc3\xb8') // "hellø"
2079string(bytes([0x20])) // " "
2080```
2081
2082As string value is always convertible to a list of bytes.
2083
2084```
2085bytes("hellø") // 'hell\xc3\xb8'
2086bytes("") // ''
2087```
2088
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002089#### Conversions between list types
2090
2091Conversions between list types are possible only if `T` strictly subsumes `x`
2092and the result will be the unification of `T` and `x`.
2093
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002094If we introduce named types this would be different from IP & [10, ...]
2095
2096Consider removing this until it has a different meaning.
2097
2098```
2099IP: 4*[byte]
2100Private10: IP([10, ...]) // [10, byte, byte, byte]
2101```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002102
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002103#### Conversions between struct types
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002104
2105A conversion from `x` to `T`
2106is applied using the following rules:
2107
21081. `x` must be an instance of `T`,
21092. all fields defined for `x` that are not defined for `T` are removed from
2110 the result of the conversion, recursively.
2111
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002112<!-- jba: I don't think you say anywhere that the matching fields are unified.
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002113mpvl: they are not, x must be an instance of T, in which case x == T&x,
2114so unification would be unnecessary.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002115-->
Marcel van Lohuizena3f00972019-02-01 11:10:39 +01002116<!--
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002117```
2118T: {
2119 a: { b: 1..10 }
2120}
2121
2122x1: {
2123 a: { b: 8, c: 10 }
2124 d: 9
2125}
2126
2127c1: T(x1) // { a: { b: 8 } }
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002128c2: T({}) // _|_ // missing field 'a' in '{}'
2129c3: T({ a: {b: 0} }) // _|_ // field a.b does not unify (0 & 1..10)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002130```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002131-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002132
2133### Calls
2134
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002135Calls can be made to core library functions, called builtins.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002136Given an expression `f` of function type F,
2137```
2138f(a1, a2, … an)
2139```
2140calls `f` with arguments a1, a2, … an. Arguments must be expressions
2141of which the values are an instance of the parameter types of `F`
2142and are evaluated before the function is called.
2143
2144```
2145a: math.Atan2(x, y)
2146```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002147
2148In a function call, the function value and arguments are evaluated in the usual
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01002149order.
2150After they are evaluated, the parameters of the call are passed by value
2151to the function and the called function begins execution.
2152The return parameters
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002153of the function are passed by value back to the calling function when the
2154function returns.
2155
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002156
2157### Comprehensions
2158
Marcel van Lohuizen66db9202018-12-17 19:02:08 +01002159Lists and fields can be constructed using comprehensions.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002160
2161Each define a clause sequence that consists of a sequence of `for`, `if`, and
2162`let` clauses, nesting from left to right.
2163The `for` and `let` clauses each define a new scope in which new values are
2164bound to be available for the next clause.
2165
2166The `for` clause binds the defined identifiers, on each iteration, to the next
2167value of some iterable value in a new scope.
2168A `for` clause may bind one or two identifiers.
2169If there is one identifier, it binds it to the value, for instance
2170a list element, a struct field value or a range element.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002171If there are two identifiers, the first value will be the key or index,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002172if available, and the second will be the value.
2173
2174An `if` clause, or guard, specifies an expression that terminates the current
2175iteration if it evaluates to false.
2176
2177The `let` clause binds the result of an expression to the defined identifier
2178in a new scope.
2179
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002180A current iteration is said to complete if the innermost block of the clause
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002181sequence is reached.
2182
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002183_List comprehensions_ specify a single expression that is evaluated and included
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002184in the list for each completed iteration.
2185
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002186_Field comprehensions_ follow a `Field` with a clause sequence, where the
2187label and value of the field are evaluated for each iteration.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +04002188The label must be an identifier or simple_string_lit, where the
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002189later may be a string interpolation that refers to the identifiers defined
2190in the clauses.
2191Values of iterations that map to the same label unify into a single field.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002192
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01002193<!--
2194TODO: consider allowing multiple labels for comprehensions
2195(current implementation). Generally it is better to define comprehensions
2196in the current scope, though, as it may prevent surprises given the
2197restrictions on comprehensions.
2198-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002199```
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01002200ComprehensionDecl = Label ":" Expression [ "<-" ] Clauses .
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01002201ListComprehension = "[" Expression [ "<-" ] Clauses "]" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002202
2203Clauses = Clause { Clause } .
2204Clause = ForClause | GuardClause | LetClause .
2205ForClause = "for" identifier [ ", " identifier] "in" Expression .
2206GuardClause = "if" Expression .
2207LetClause = "let" identifier "=" Expression .
2208```
2209
2210```
2211a: [1, 2, 3, 4]
2212b: [ x+1 for x in a if x > 1] // [3, 4, 5]
2213
Marcel van Lohuizen66db9202018-12-17 19:02:08 +01002214c: { "\(x)": x + y for x in a if x < 4 let y = 1 }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002215d: { "1": 2, "2": 3, "3": 4 }
2216```
2217
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002218
2219### String interpolation
2220
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002221String interpolation allows constructing strings by replacing placeholder
2222expressions with their string representation.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002223String interpolation may be used in single- and double-quoted strings, as well
2224as their multiline equivalent.
2225
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002226A placeholder consists of "\(" followed by an expression and a ")". The
2227expression is evaluated within the scope within which the string is defined.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002228
2229```
2230a: "World"
2231b: "Hello \( a )!" // Hello World!
2232```
2233
2234
2235## Builtin Functions
2236
2237Built-in functions are predeclared. They are called like any other function.
2238
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002239
2240### `len`
2241
2242The built-in function `len` takes arguments of various types and return
2243a result of type int.
2244
2245```
2246Argument type Result
2247
2248string string length in bytes
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002249bytes length of byte sequence
2250list list length, smallest length for an open list
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002251struct number of distinct fields
2252```
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002253
2254```
2255Expression Result
2256len("Hellø") 6
2257len([1, 2, 3]) 3
2258len([1, 2, ...]) 2
2259len({a:1, b:2}) 2
2260```
2261
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002262### `and`
2263
2264The built-in function `and` takes a list and returns the result of applying
2265the `&` operator to all elements in the list.
2266It returns top for the empty list.
2267
2268Expression: Result
2269and([a, b]) a & b
2270and([a]) a
2271and([]) _
2272
2273### `or`
2274
2275The built-in function `or` takes a list and returns the result of applying
2276the `|` operator to all elements in the list.
2277It returns bottom for the empty list.
2278
2279Expression: Result
2280and([a, b]) a | b
2281and([a]) a
2282and([]) _|_
2283
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002284
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002285## Cycles
2286
2287Implementations are required to interpret or reject cycles encountered
2288during evaluation according to the rules in this section.
2289
2290
2291### Reference cycles
2292
2293A _reference cycle_ occurs if a field references itself, either directly or
2294indirectly.
2295
2296```
2297// x references itself
2298x: x
2299
2300// indirect cycles
2301b: c
2302c: d
2303d: b
2304```
2305
2306Implementations should report these as an error except in the following cases:
2307
2308
2309#### Expressions that unify an atom with an expression
2310
2311An expression of the form `a & e`, where `a` is an atom
2312and `e` is an expression, always evaluates to `a` or bottom.
2313As it does not matter how we fail, we can assume the result to be `a`
2314and validate after the field in which the expression occurs has been evaluated
2315that `a == e`.
2316
2317```
Marcel van Lohuizeneac8f9a2019-08-03 13:53:56 +02002318// Config Evaluates to (requiring concrete values)
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002319x: { x: {
2320 a: b + 100 a: _|_ // cycle detected
2321 b: a - 100 b: _|_ // cycle detected
2322} }
2323
2324y: x & { y: {
2325 a: 200 a: 200 // asserted that 200 == b + 100
2326 b: 100
2327} }
2328```
2329
2330
2331#### Field values
2332
2333A field value of the form `r & v`,
2334where `r` evaluates to a reference cycle and `v` is a value,
2335evaluates to `v`.
2336Unification is idempotent and unifying a value with itself ad infinitum,
2337which is what the cycle represents, results in this value.
2338Implementations should detect cycles of this kind, ignore `r`,
2339and take `v` as the result of unification.
2340<!-- Tomabechi's graph unification algorithm
2341can detect such cycles at near-zero cost. -->
2342
2343```
2344Configuration Evaluated
2345// c Cycles in nodes of type struct evaluate
2346// ↙︎ ↖ to the fixed point of unifying their
2347// a → b values ad infinitum.
2348
2349a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 }
2350b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 }
2351c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 }
2352
2353// resolve a b & {x:1}
2354// substitute b c & {y:2} & {x:1}
2355// substitute c a & {z:3} & {y:2} & {x:1}
2356// eliminate a (cycle) {z:3} & {y:2} & {x:1}
2357// simplify {x:1,y:2,z:3}
2358```
2359
2360This rule also applies to field values that are disjunctions of unification
2361operations of the above form.
2362
2363```
2364a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1}
2365b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2}
2366c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3}
2367
2368
2369// resolving a b&{x:1} | {y:1}
2370// substitute b ({x:2} | c&{z:2})&{x:1} | {y:1}
2371// simplify c&{z:2}&{x:1} | {y:1}
2372// substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1}
2373// simplify a&{y:3}&{z:2}&{x:1} | {y:1}
2374// eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1}
2375// expand {x:1,y:3,z:2} | {y:1}
2376```
2377
2378Note that all nodes that form a reference cycle to form a struct will evaluate
2379to the same value.
2380If a field value is a disjunction, any element that is part of a cycle will
2381evaluate to this value.
2382
2383
2384### Structural cycles
2385
2386CUE disallows infinite structures.
2387Implementations must report an error when encountering such declarations.
2388
2389<!-- for instance using an occurs check -->
2390
2391```
2392// Disallowed: a list of infinite length with all elements being 1.
2393list: {
2394 head: 1
2395 tail: list
2396}
2397
2398// Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...).
2399a: {
2400 b: c
2401}
2402c: {
2403 d: a
2404}
2405```
2406
2407It is allowed for a value to define an infinite set of possibilities
2408without evaluating to an infinite structure itself.
2409
2410```
2411// List defines a list of arbitrary length (default null).
2412List: *null | {
2413 head: _
2414 tail: List
2415}
2416```
2417
2418<!--
Marcel van Lohuizen7f48df72019-02-01 17:24:59 +01002419Consider banning any construct that makes CUE not having a linear
2420running time expressed in the number of nodes in the output.
2421
2422This would require restricting constructs like:
2423
2424(fib&{n:2}).out
2425
2426fib: {
2427 n: int
2428
2429 out: (fib&{n:n-2}).out + (fib&{n:n-1}).out if n >= 2
2430 out: fib({n:n-2}).out + fib({n:n-1}).out if n >= 2
2431 out: n if n < 2
2432}
2433
2434-->
2435<!--
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002436### Unused fields
2437
2438TODO: rules for detection of unused fields
2439
24401. Any alias value must be used
2441-->
2442
2443
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002444## Modules, instances, and packages
2445
2446CUE configurations are constructed combining _instances_.
2447An instance, in turn, is constructed from one or more source files belonging
2448to the same _package_ that together declare the data representation.
2449Elements of this data representation may be exported and used
2450in other instances.
2451
2452### Source file organization
2453
2454Each source file consists of an optional package clause defining collection
2455of files to which it belongs,
2456followed by a possibly empty set of import declarations that declare
2457packages whose contents it wishes to use, followed by a possibly empty set of
2458declarations.
2459
2460
2461```
2462SourceFile = [ PackageClause "," ] { ImportDecl "," } { TopLevelDecl "," } .
2463```
2464
2465### Package clause
2466
2467A package clause is an optional clause that defines the package to which
2468a source file the file belongs.
2469
2470```
2471PackageClause = "package" PackageName .
2472PackageName = identifier .
2473```
2474
2475The PackageName must not be the blank identifier.
2476
2477```
2478package math
2479```
2480
2481### Modules and instances
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002482A _module_ defines a tree of directories, rooted at the _module root_.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002483
2484All source files within a module with the same package belong to the same
2485package.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002486<!-- jba: I can't make sense of the above sentence. -->
2487A module may define multiple packages.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002488
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002489An _instance_ of a package is any subset of files belonging
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002490to the same package.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002491<!-- jba: Are you saying that -->
2492<!-- if I have a package with files a, b and c, then there are 8 instances of -->
2493<!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the -->
2494<!-- purpose of that definition? -->
2495It is interpreted as the concatenation of these files.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002496
2497An implementation may impose conventions on the layout of package files
2498to determine which files of a package belongs to an instance.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002499For example, an instance may be defined as the subset of package files
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002500belonging to a directory and all its ancestors.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002501<!-- jba: OK, that helps a little, but I still don't see what the purpose is. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002502
2503### Import declarations
2504
2505An import declaration states that the source file containing the declaration
2506depends on definitions of the _imported_ package (§Program initialization and
2507execution) and enables access to exported identifiers of that package.
2508The import names an identifier (PackageName) to be used for access and an
2509ImportPath that specifies the package to be imported.
2510
2511```
2512ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
2513ImportSpec = [ "." | PackageName ] ImportPath .
2514ImportPath = `"` { unicode_value } `"` .
2515```
2516
2517The PackageName is used in qualified identifiers to access exported identifiers
2518of the package within the importing source file.
2519It is declared in the file block.
2520If the PackageName is omitted, it defaults to the identifier specified in the
2521package clause of the imported instance.
2522If an explicit period (.) appears instead of a name, all the instances's
2523exported identifiers declared in that instances's package block will be declared
2524in the importing source file's file block
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002525and must be accessed without a qualifier.
2526<!-- jba: Can you omit this feature? It's likely to only decrease readability,
2527as we know from Go. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002528
2529The interpretation of the ImportPath is implementation-dependent but it is
2530typically either the path of a builtin package or a fully qualifying location
2531of an instance within a source code repository.
2532
2533Implementation restriction: An interpreter may restrict ImportPaths to non-empty
2534strings using only characters belonging to Unicode's L, M, N, P, and S general
2535categories (the Graphic characters without spaces) and may also exclude the
2536characters !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character
2537U+FFFD.
2538
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002539Assume we have package containing the package clause "package math",
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002540which exports function Sin at the path identified by "lib/math".
2541This table illustrates how Sin is accessed in files
2542that import the package after the various types of import declaration.
2543
2544```
2545Import declaration Local name of Sin
2546
2547import "lib/math" math.Sin
2548import m "lib/math" m.Sin
2549import . "lib/math" Sin
2550```
2551
2552An import declaration declares a dependency relation between the importing and
2553imported package. It is illegal for a package to import itself, directly or
2554indirectly, or to directly import a package without referring to any of its
2555exported identifiers.
2556
2557
2558### An example package
2559
2560TODO
2561
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002562
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002563
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002564
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002565