blob: 2fe48642acae4293c0619b3c02261076268dc226 [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),
Marcel van Lohuizen62658a82019-06-16 12:18:47 +020047Jsonnet, HCL, Flabbergast, Nix, 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
Roger Peppeded0e1d2019-09-24 16:39:36 +010082token of the CUE language.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +010083
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
Marcel van Lohuizen7fc421b2019-09-11 09:24:03 +0200138Comments serve as program documentation.
139CUE supports line comments that start with the character sequence //
140and stop at the end of the line.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100141
Marcel van Lohuizen7fc421b2019-09-11 09:24:03 +0200142A comment cannot start inside a string literal or inside a comment.
143A comment acts like a newline.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100144
145
146### Tokens
147
148Tokens form the vocabulary of the CUE language. There are four classes:
149identifiers, keywords, operators and punctuation, and literals. White space,
150formed from spaces (U+0020), horizontal tabs (U+0009), carriage returns
151(U+000D), and newlines (U+000A), is ignored except as it separates tokens that
152would otherwise combine into a single token. Also, a newline or end of file may
153trigger the insertion of a comma. While breaking the input into tokens, the
154next token is the longest sequence of characters that form a valid token.
155
156
157### Commas
158
159The formal grammar uses commas "," as terminators in a number of productions.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500160CUE programs may omit most of these commas using the following two rules:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100161
162When the input is broken into tokens, a comma is automatically inserted into
163the token stream immediately after a line's final token if that token is
164
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500165- an identifier
166- null, true, false, bottom, or an integer, floating-point, or string literal
167- one of the characters ), ], or }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100168
169
170Although commas are automatically inserted, the parser will require
171explicit commas between two list elements.
172
173To reflect idiomatic use, examples in this document elide commas using
174these rules.
175
176
177### Identifiers
178
179Identifiers name entities such as fields and aliases.
Marcel van Lohuizen8a2df962019-11-10 00:14:24 +0100180An identifier is a sequence of one or more letters (which includes `_` and `$`)
181and digits.
182It may not be `_` or `$`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100183The first character in an identifier must be a letter.
184
185<!--
186TODO: allow identifiers as defined in Unicode UAX #31
187(https://unicode.org/reports/tr31/).
188
189Identifiers are normalized using the NFC normal form.
190-->
191
192```
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +0200193identifier = letter { letter | unicode_digit } .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100194```
195
196```
197a
198_x9
199fieldName
200αβ
201```
202
203<!-- TODO: Allow Unicode identifiers TR 32 http://unicode.org/reports/tr31/ -->
204
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500205Some identifiers are [predeclared](#predeclared-identifiers).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100206
207
208### Keywords
209
210CUE has a limited set of keywords.
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200211In addition, CUE reserves all identifiers starting with `__`(double underscores)
212as keywords.
213These are typically targets of pre-declared identifiers.
214
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100215All keywords may be used as labels (field names).
216They cannot, however, be used as identifiers to refer to the same name.
217
218
219#### Values
220
221The following keywords are values.
222
223```
224null true false
225```
226
227These can never be used to refer to a field of the same name.
228This restriction is to ensure compatibility with JSON configuration files.
229
230
231#### Preamble
232
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100233The following keywords are used at the preamble of a CUE file.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100234After the preamble, they may be used as identifiers to refer to namesake fields.
235
236```
237package import
238```
239
240
241#### Comprehension clauses
242
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100243The following keywords are used in comprehensions.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100244
245```
246for in if let
247```
248
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100249The keywords `for`, `if` and `let` cannot be used as identifiers to
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200250refer to fields.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100251
252<!--
253TODO:
254 reduce [to]
255 order [by]
256-->
257
258
259#### Arithmetic
260
261The following pseudo keywords can be used as operators in expressions.
262
263```
264div mod quo rem
265```
266
267These may be used as identifiers to refer to fields in all other contexts.
268
269
270### Operators and punctuation
271
272The following character sequences represent operators and punctuation:
273
274```
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200275+ div && == < = ( )
276- mod || != > :: { }
277* quo & =~ <= : [ ]
278/ rem | !~ >= . ... ,
279 _|_ !
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100280```
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200281<!--
282Free tokens: # ; ~ $ ^
283
284// To be used:
285 @ at: associative lists.
286
287// Idea: use # instead of @ for attributes and allow then at declaration level.
288// This will open up the possibility of defining #! at the start of a file
289// without requiring special syntax. Although probably not quite.
290 -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100291
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +0100292
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100293### Integer literals
294
295An integer literal is a sequence of digits representing an integer value.
Marcel van Lohuizenb2703c62019-09-29 18:20:01 +0200296An optional prefix sets a non-decimal base: 0o for octal,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002970x or 0X for hexadecimal, and 0b for binary.
298In hexadecimal literals, letters a-f and A-F represent values 10 through 15.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500299All integers allow interstitial underscores "_";
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100300these have no meaning and are solely for readability.
301
302Decimal integers may have a SI or IEC multiplier.
303Multipliers can be used with fractional numbers.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500304When multiplying a fraction by a multiplier, the result is truncated
305towards zero if it is not an integer.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100306
307```
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200308int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit .
309decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } .
310decimals = decimal_digit { [ "_" ] decimal_digit } .
311si_it = decimals [ "." decimals ] multiplier |
312 "." decimals multiplier .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100313binary_lit = "0b" binary_digit { binary_digit } .
314hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } .
Marcel van Lohuizenb2703c62019-09-29 18:20:01 +0200315octal_lit = "0o" octal_digit { [ "_" ] octal_digit } .
Marcel van Lohuizen6eefcd02019-10-04 13:32:06 +0200316multiplier = ( "K" | "M" | "G" | "T" | "P" ) [ "i" ]
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200317
318float_lit = decimals "." [ decimals ] [ exponent ] |
319 decimals exponent |
320 "." decimals [ exponent ].
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +0200321exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100322```
Marcel van Lohuizen6eefcd02019-10-04 13:32:06 +0200323<!--
324TODO: consider allowing Exo (and up), if not followed by a sign
325or number. Alternatively one could only allow Ei, Yi, and Zi.
326-->
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100327
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100328```
32942
3301.5Gi
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100331170_141_183_460_469_231_731_687_303_715_884_105_727
Marcel van Lohuizenfc6303c2019-02-07 17:49:04 +01003320xBad_Face
3330o755
3340b0101_0001
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100335```
336
337### Decimal floating-point literals
338
339A decimal floating-point literal is a representation of
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500340a decimal floating-point value (a _float_).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100341It has an integer part, a decimal point, a fractional part, and an
342exponent part.
343The integer and fractional part comprise decimal digits; the
344exponent part is an `e` or `E` followed by an optionally signed decimal exponent.
345One of the integer part or the fractional part may be elided; one of the decimal
346point or the exponent may be elided.
347
348```
349decimal_lit = decimals "." [ decimals ] [ exponent ] |
350 decimals exponent |
351 "." decimals [ exponent ] .
352exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
353```
354
355```
3560.
35772.40
358072.40 // == 72.40
3592.71828
3601.e+0
3616.67428e-11
3621E6
363.25
364.12345E+5
365```
366
367
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100368### String and byte sequence literals
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100369
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100370A string literal represents a string constant obtained from concatenating a
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100371sequence of characters.
372Byte sequences are a sequence of bytes.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100373
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100374String and byte sequence literals are character sequences between,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100375respectively, double and single quotes, as in `"bar"` and `'bar'`.
376Within the quotes, any character may appear except newline and,
377respectively, unescaped double or single quote.
378String literals may only be valid UTF-8.
379Byte sequences may contain any sequence of bytes.
380
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400381Several escape sequences allow arbitrary values to be encoded as ASCII text.
382An escape sequence starts with an _escape delimiter_, which is `\` by default.
383The escape delimiter may be altered to be `\` plus a fixed number of
384hash symbols `#`
385by padding the start and end of a string or byte sequence literal
386with this number of hash symbols.
387
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100388There are four ways to represent the integer value as a numeric constant: `\x`
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400389followed by exactly two hexadecimal digits; `\u` followed by exactly four
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100390hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a
391plain backslash `\` followed by exactly three octal digits.
392In each case the value of the literal is the value represented by the
393digits in the corresponding base.
394Hexadecimal and octal escapes are only allowed within byte sequences
395(single quotes).
396
397Although these representations all result in an integer, they have different
398valid ranges.
399Octal escapes must represent a value between 0 and 255 inclusive.
400Hexadecimal escapes satisfy this condition by construction.
401The escapes `\u` and `\U` represent Unicode code points so within them
402some values are illegal, in particular those above `0x10FFFF`.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400403Surrogate halves are allowed,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100404but are translated into their non-surrogate equivalent internally.
405
406The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes
407represent individual bytes of the resulting string; all other escapes represent
408the (possibly multi-byte) UTF-8 encoding of individual characters.
409Thus inside a string literal `\377` and `\xFF` represent a single byte of
410value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent
411the two bytes `0xc3 0xbf` of the UTF-8
412encoding of character `U+00FF`.
413
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100414```
415\a U+0007 alert or bell
416\b U+0008 backspace
417\f U+000C form feed
418\n U+000A line feed or newline
419\r U+000D carriage return
420\t U+0009 horizontal tab
421\v U+000b vertical tab
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100422\/ U+002f slash (solidus)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100423\\ U+005c backslash
424\' U+0027 single quote (valid escape only within single quoted literals)
425\" U+0022 double quote (valid escape only within double quoted literals)
426```
427
428The escape `\(` is used as an escape for string interpolation.
429A `\(` must be followed by a valid CUE Expression, followed by a `)`.
430
431All other sequences starting with a backslash are illegal inside literals.
432
433```
Marcel van Lohuizen39df6c92019-10-25 20:16:26 +0200434escaped_char = `\` { `#` } ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "/" | `\` | "'" | `"` ) .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100435byte_value = octal_byte_value | hex_byte_value .
436octal_byte_value = `\` octal_digit octal_digit octal_digit .
437hex_byte_value = `\` "x" hex_digit hex_digit .
438little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
439big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
440 hex_digit hex_digit hex_digit hex_digit .
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400441unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
442interpolation = "\(" Expression ")" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100443
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400444string_lit = simple_string_lit |
445 multiline_string_lit |
446 simple_bytes_lit |
447 multiline_bytes_lit |
448 `#` string_lit `#` .
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100449
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400450simple_string_lit = `"` { unicode_value | interpolation } `"` .
451simple_bytes_lit = `"` { unicode_value | interpolation | byte_value } `"` .
452multiline_string_lit = `"""` newline
453 { unicode_value | interpolation | newline }
454 newline `"""` .
455multiline_bytes_lit = "'''" newline
456 { unicode_value | interpolation | byte_value | newline }
457 newline "'''" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100458```
459
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400460Carriage return characters (`\r`) inside string literals are discarded from
Marcel van Lohuizendb9d25a2019-02-21 23:54:43 +0100461the string value.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400462
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100463```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100464'a\000\xab'
465'\007'
466'\377'
467'\xa' // illegal: too few hexadecimal digits
468"\n"
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100469"\""
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100470'Hello, world!\n'
471"Hello, \( name )!"
472"日本語"
473"\u65e5本\U00008a9e"
474"\xff\u00FF"
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100475"\uD800" // illegal: surrogate half (TODO: probably should allow)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100476"\U00110000" // illegal: invalid Unicode code point
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400477
478#"This is not an \(interpolation)"#
479#"This is an \#(interpolation)"#
480#"The sequence "\U0001F604" renders as \#U0001F604."#
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100481```
482
483These examples all represent the same string:
484
485```
486"日本語" // UTF-8 input text
487'日本語' // UTF-8 input text as byte sequence
488`日本語` // UTF-8 input text as a raw literal
489"\u65e5\u672c\u8a9e" // the explicit Unicode code points
490"\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points
491"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes
492```
493
494If the source code represents a character as two code points, such as a
495combining form involving an accent and a letter, the result will appear as two
496code points if placed in a string literal.
497
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400498Strings and byte sequences have a multiline equivalent.
499Multiline strings are like their single-line equivalent,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100500but allow newline characters.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100501
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400502Multiline strings and byte sequences respectively start with
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100503a triple double quote (`"""`) or triple single quote (`'''`),
504immediately followed by a newline, which is discarded from the string contents.
505The string is closed by a matching triple quote, which must be by itself
506on a newline, preceded by optional whitespace.
507The whitespace before a closing triple quote must appear before any non-empty
508line after the opening quote and will be removed from each of these
509lines in the string literal.
510A closing triple quote may not appear in the string.
511To include it is suffices to escape one of the quotes.
512
513```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100514"""
515 lily:
516 out of the water
517 out of itself
518
519 bass
520 picking bugs
521 off the moon
522 — Nick Virgilio, Selected Haiku, 1988
523 """
524```
525
526This represents the same string as:
527
528```
529"lily:\nout of the water\nout of itself\n\n" +
530"bass\npicking bugs\noff the moon\n" +
531" — Nick Virgilio, Selected Haiku, 1988"
532```
533
534<!-- TODO: other values
535
536Support for other values:
537- Duration literals
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +0100538- regular expessions: `re("[a-z]")`
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100539-->
540
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500541
542## Values
543
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100544In addition to simple values like `"hello"` and `42.0`, CUE has _structs_.
545A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`.
546Structs are CUE's only way of building up complex values;
547lists, which we will see later,
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500548are defined in terms of structs.
549
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100550All possible values are ordered in a lattice,
551a partial order where every two elements have a single greatest lower bound.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500552A value `a` is an _instance_ of a value `b`,
553denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`,
554that is if `a` orders before `b` in the partial order
555(`⊑` is _not_ a CUE operator).
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100556We also say that `b` _subsumes_ `a` in this case.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500557In graphical terms, `b` is "above" `a` in the lattice.
558
559At the top of the lattice is the single ancestor of all values, called
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100560_top_, denoted `_` in CUE.
561Every value is an instance of top.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500562
563At the bottom of the lattice is the value called _bottom_, denoted `_|_`.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100564A bottom value usually indicates an error.
565Bottom is an instance of every value.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500566
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100567An _atom_ is any value whose only instances are itself and bottom.
568Examples of atoms are `42.0`, `"hello"`, `true`, `null`.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500569
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100570A value is _concrete_ if it is either an atom, or a struct all of whose
571field values are themselves concrete, recursively.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500572
573CUE's values also include what we normally think of as types, like `string` and
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100574`float`.
575But CUE does not distinguish between types and values; only the
576relationship of values in the lattice is important.
577Each CUE "type" subsumes the concrete values that one would normally think
578of as part of that type.
579For example, "hello" is an instance of `string`, and `42.0` is an instance of
580`float`.
581In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`.
582We informally call these CUE's "basic types".
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100583
584
585```
586false ⊑ bool
587true ⊑ bool
588true ⊑ true
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01005895.0 ⊑ float
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100590bool ⊑ _
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100591_|_ ⊑ _
592_|_ ⊑ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100593
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100594_ ⋢ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100595_ ⋢ bool
596int ⋢ bool
597bool ⋢ int
598false ⋢ true
599true ⋢ false
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100600float ⋢ 5.0
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01006015 ⋢ 6
602```
603
604
605### Unification
606
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500607The _unification_ of values `a` and `b`
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100608is defined as the greatest lower bound of `a` and `b`. (That is, the
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500609value `u` such that `u ⊑ a` and `u ⊑ b`,
610and for any other value `v` for which `v ⊑ a` and `v ⊑ b`
611it holds that `v ⊑ u`.)
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500612Since CUE values form a lattice, the unification of two CUE values is
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400613always unique.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100614
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500615These all follow from the definition of unification:
616- The unification of `a` with itself is always `a`.
617- The unification of values `a` and `b` where `a ⊑ b` is always `a`.
618- The unification of a value with bottom is always bottom.
619
620Unification in CUE is a [binary expression](#Operands), written `a & b`.
621It is commutative and associative.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100622As a consequence, order of evaluation is irrelevant, a property that is key
623to many of the constructs in the CUE language as well as the tooling layered
624on top of it.
625
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500626
627
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100628<!-- TODO: explicitly mention that disjunction is not a binary operation
629but a definition of a single value?-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100630
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100631
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100632### Disjunction
633
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500634The _disjunction_ of values `a` and `b`
635is defined as the least upper bound of `a` and `b`.
636(That is, the value `d` such that `a ⊑ d` and `b ⊑ d`,
637and for any other value `e` for which `a ⊑ e` and `b ⊑ e`,
638it holds that `d ⊑ e`.)
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100639This style of disjunctions is sometimes also referred to as sum types.
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500640Since CUE values form a lattice, the disjunction of two CUE values is always unique.
641
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100642
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500643These all follow from the definition of disjunction:
644- The disjunction of `a` with itself is always `a`.
645- The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`.
646- The disjunction of a value `a` with bottom is always `a`.
647- The disjunction of two bottom values is bottom.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100648
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500649Disjunction in CUE is a [binary expression](#Operands), written `a | b`.
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100650It is commutative, associative, and idempotent.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100651
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100652The unification of a disjunction with another value is equal to the disjunction
653composed of the unification of this value with all of the original elements
654of the disjunction.
655In other words, unification distributes over disjunction.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100656
657```
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100658(a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100659```
660
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100661```
662Expression Result
663({a:1} | {b:2}) & {c:3} {a:1, c:3} | {b:2, c:3}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100664(int | string) & "foo" "foo"
665("a" | "b") & "c" _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100666```
667
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100668A disjunction is _normalized_ if there is no element
669`a` for which there is an element `b` such that `a ⊑ b`.
670
671<!--
672Normalization is important, as we need to account for spurious elements
673For instance "tcp" | "tcp" should resolve to "tcp".
674
675Also consider
676
677 ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2},
678
679in this case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1} and thus
680this expression is logically equivalent to {a:1} and should therefore be
681considered to be unambiguous and resolve to {a:1} if a concrete value is needed.
682
683For instance, in
684
685 x: ({a:1} | {b:1}) & ({a:1} | {b:2}) // -> {a:1} | {a:1,b:1} | {a:1,b:2}
686 y: x.a // 1
687
688y should resolve to 1, and not an error.
689
690For comparison, in
691
692 x: ({a:1, b:1} | {b:2}) & {a:1} // -> {a:1,b:1} | {a:1,b:2}
693 y: x.a // _|_
694
695y should be an error as x is still ambiguous before the selector is applied,
696even though `a` resolves to 1 in all cases.
697-->
698
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500699
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100700#### Default values
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500701
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100702Any element of a disjunction can be marked as a default
Axel Wagner8529d772019-09-24 18:27:12 +0000703by prefixing it with an asterisk `*`.
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100704Intuitively, when an expression needs to be resolved for an operation other
705than unification or disjunctions,
706non-starred elements are dropped in favor of starred ones if the starred ones
707do not resolve to bottom.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500708
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100709More precisely, any value `v` may be associated with a default value `d`,
710denoted `(v, d)` (not CUE syntax),
711where `d` must be in instance of `v` (`d ⊑ v`).
712The rules for unifying and disjoining such values are as follows:
713
714```
715U1: (v1, d1) & v2 => (v1&v2, d1&v2)
716U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2)
717
718D1: (v1, d1) | v2 => (v1|v2, d1)
719D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2)
720```
721
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100722Default values may be introduced within disjunctions
723by _marking_ terms of a disjunction with an asterisk `*`
724([a unary expression](#Operators)).
725The default value of a disjunction with marked terms is the disjunction
726of those marked terms, applying the following rules for marks:
727
728```
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200729M1: *v => (v, v)
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100730M2: *(v1, d1) => (v1, d1)
731```
732
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400733In general, any operation `f` in CUE involving default values proceeds along the
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200734following lines
735```
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400736O1: f((v1, d1), ..., (vn, dn)) => (f(v1, ..., vn), f(d1, ..., dn))
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200737```
738where, with the exception of disjunction, a value `v` without a default
739value is promoted to `(v, v)`.
740
741
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100742```
743Expression Value-default pair Rules applied
744*"tcp" | "udp" ("tcp"|"udp", "tcp") M1, D1
745string | *"foo" (string, "foo") M1, D1
746
747*1 | 2 | 3 (1|2|3, 1) M1, D1
748
749(*1|2|3) | (1|*2|3) (1|2|3, 1|2) M1, D1, D2
750(*1|2|3) | *(1|*2|3) (1|2|3, 1|2) M1, D1, M2, D2
751(*1|2|3) | (1|*2|3)&2 (1|2|3, 1|2) M1, D1, U1, D2
752
753(*1|2) & (1|*2) (1|2, _|_) M1, D1, U2
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200754
755(*1|2) + (1|*2) ((1|2)+(1|2), 3) M1, D1, O1
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100756```
757
758The rules of subsumption for defaults can be derived from the above definitions
759and are as follows.
760
761```
762(v2, d2) ⊑ (v1, d1) if v2 ⊑ v1 and d2 ⊑ d1
763(v1, d1) ⊑ v if v1 ⊑ v
764v ⊑ (v1, d1) if v ⊑ d1
765```
766
767<!--
768For the second rule, note that by definition d1 ⊑ v1, so d1 ⊑ v1 ⊑ v.
769
770The last one is so restrictive as v could still be made more specific by
771associating it with a default that is not subsumed by d1.
772
773Proof:
774 by definition for any d ⊑ v, it holds that (v, d) ⊑ v,
775 where the most general value is (v, v).
776 Given the subsumption rule for (v2, d2) ⊑ (v1, d1),
777 from (v, v) ⊑ v ⊑ (v1, d1) it follows that v ⊑ d1
778 exactly defines the boundary of this subsumption.
779-->
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100780
781<!--
782(non-normalized entries could also be implicitly marked, allowing writing
783int | 1, instead of int | *1, but that can be done in a backwards
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100784compatible way later if really desirable, as long as we require that
785disjunction literals be normalized).
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500786-->
787
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100788
789```
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100790Expression Resolves to
791"tcp" | "udp" "tcp" | "udp"
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100792*"tcp" | "udp" "tcp"
793float | *1 1
794*string | 1.0 string
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100795
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100796(*1|2|3) | (1|*2|3) 1|2
797(*1|2|3) & (1|*2|3) 1|2|3 // default is _|_
798
799(* >=5 | int) & (* <=5 | int) 5
800
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100801(*"tcp"|"udp") & ("udp"|*"tcp") "tcp"
802(*"tcp"|"udp") & ("udp"|"tcp") "tcp"
803(*"tcp"|"udp") & "tcp" "tcp"
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100804(*"tcp"|"udp") & (*"udp"|"tcp") "tcp" | "udp" // default is _|_
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100805
806(*true | false) & bool true
807(*true | false) & (true | false) true
808
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100809{a: 1} | {b: 1} {a: 1} | {b: 1}
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100810{a: 1} | *{b: 1} {b:1}
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100811*{a: 1} | *{b: 1} {a: 1} | {b: 1}
812({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} by normalization
813({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 +0100814```
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500815
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100816
817### Bottom and errors
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100818
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100819Any evaluation error in CUE results in a bottom value, respresented by
Axel Wagner8529d772019-09-24 18:27:12 +0000820the token `_|_`.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100821Bottom is an instance of every other value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100822Any evaluation error is represented as bottom.
823
824Implementations may associate error strings with different instances of bottom;
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500825logically they all remain the same value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100826
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100827
828### Top
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100829
Axel Wagner8529d772019-09-24 18:27:12 +0000830Top is represented by the underscore character `_`, lexically an identifier.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100831Unifying any value `v` with top results `v` itself.
832
833```
834Expr Result
835_ & 5 5
836_ & _ _
837_ & _|_ _|_
838_ | _|_ _
839```
840
841
842### Null
843
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100844The _null value_ is represented with the keyword `null`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100845It has only one parent, top, and one child, bottom.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100846It is unordered with respect to any other value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100847
848```
849null_lit = "null"
850```
851
852```
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100853null & 8 _|_
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100854null & _ null
855null & _|_ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100856```
857
858
859### Boolean values
860
861A _boolean type_ represents the set of Boolean truth values denoted by
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100862the keywords `true` and `false`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100863The predeclared boolean type is `bool`; it is a defined type and a separate
864element in the lattice.
865
866```
867boolean_lit = "true" | "false"
868```
869
870```
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100871bool & true true
872true & true true
873true & false _|_
874bool & (false|true) false | true
875bool & (true|false) true | false
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100876```
877
878
879### Numeric values
880
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500881The _integer type_ represents the set of all integral numbers.
882The _decimal floating-point type_ represents the set of all decimal floating-point
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100883numbers.
884They are two distinct types.
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +0200885Both are instances instances of a generic `number` type.
886
887<!--
888 number
889 / \
890 int float
891-->
892
893The predeclared number, integer, decimal floating-point types are
894`number`, `int` and `float`; they are defined types.
895<!--
896TODO: should we drop float? It is somewhat preciser and probably a good idea
897to have it in the programmatic API, but it may be confusing to have to deal
898with it in the language.
899-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100900
901A decimal floating-point literal always has type `float`;
902it is not an instance of `int` even if it is an integral number.
903
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400904Integer literals are always of type `int` and don't match type `float`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100905
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100906Numeric literals are exact values of arbitrary precision.
907If the operation permits it, numbers should be kept in arbitrary precision.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100908
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100909Implementation restriction: although numeric values have arbitrary precision
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100910in the language, implementations may implement them using an internal
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100911representation with limited precision.
912That said, every implementation must:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100913
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500914- Represent integer values with at least 256 bits.
915- Represent floating-point values, with a mantissa of at least 256 bits and
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100916a signed binary exponent of at least 16 bits.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500917- Give an error if unable to represent an integer value precisely.
918- Give an error if unable to represent a floating-point value due to overflow.
919- Round to the nearest representable value if unable to represent
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100920a floating-point value due to limits on precision.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100921These requirements apply to the result of any expression except for builtin
922functions for which an unusual loss of precision must be explicitly documented.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100923
924
925### Strings
926
Marcel van Lohuizen4108f802019-08-13 18:30:25 +0200927The _string type_ represents the set of UTF-8 strings,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100928not allowing surrogates.
929The predeclared string type is `string`; it is a defined type.
930
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100931The length of a string `s` (its size in bytes) can be discovered using
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400932the built-in function `len`.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100933
Marcel van Lohuizen4108f802019-08-13 18:30:25 +0200934
935### Bytes
936
937The _bytes type_ represents the set of byte sequences.
938A byte sequence value is a (possibly empty) sequence of bytes.
939The number of bytes is called the length of the byte sequence
940and is never negative.
941The predeclared byte sequence type is `bytes`; it is a defined type.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100942
943
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +0100944### Bounds
945
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400946A _bound_, syntactically a [unary expression](#Operands), defines
Marcel van Lohuizen62b87272019-02-01 10:07:49 +0100947an infinite disjunction of concrete values than can be represented
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +0100948as a single comparison.
949
950For any [comparison operator](#Comparison-operators) `op` except `==`,
951`op a` is the disjunction of every `x` such that `x op a`.
952
953```
9542 & >=2 & <=5 // 2, where 2 is either an int or float.
9552.5 & >=1 & <=5 // 2.5
9562 & >=1.0 & <3.0 // 2.0
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01009572 & >1 & <3.0 // 2.0
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01009582.5 & int & >1 & <5 // _|_
9592.5 & float & >1 & <5 // 2.5
960int & 2 & >1.0 & <3.0 // _|_
9612.5 & >=(int & 1) & <5 // _|_
962>=0 & <=7 & >=3 & <=10 // >=3 & <=7
963!=null & 1 // 1
964>=5 & <=5 // 5
965```
966
967
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100968### Structs
969
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500970A _struct_ is a set of elements called _fields_, each of
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100971which has a name, called a _label_, and value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100972
973We say a label is defined for a struct if the struct has a field with the
974corresponding label.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +0200975The value for a label `f` of struct `a` is denoted `a.f`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100976A struct `a` is an instance of `b`, or `a ⊑ b`, if for any label `f`
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100977defined for `b`, label `f` is also defined for `a` and `a.f ⊑ b.f`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100978Note that if `a` is an instance of `b` it may have fields with labels that
979are not defined for `b`.
980
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500981The (unique) struct with no fields, written `{}`, has every struct as an
982instance. It can be considered the type of all structs.
983
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400984```
985{a: 1} ⊑ {}
986{a: 1, b: 1} ⊑ {a: 1}
987{a: 1} ⊑ {a: int}
988{a: 1, b: 1} ⊑ {a: int, b: float}
989
990{} ⋢ {a: 1}
991{a: 2} ⋢ {a: 1}
992{a: 1} ⋢ {b: 1}
993```
994
Marcel van Lohuizen62658a82019-06-16 12:18:47 +0200995A field may be required or optional.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100996The successful unification of structs `a` and `b` is a new struct `c` which
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100997has all fields of both `a` and `b`, where
998the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`,
999or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001000If a field `f` is in both `a` and `b`, `c.f` is optional only if both
1001`a.f` and `b.f` are optional.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001002Any [references](#References) to `a` or `b`
1003in their respective field values need to be replaced with references to `c`.
Marcel van Lohuizen3022ae92019-10-15 13:35:58 +02001004The result of a unification is bottom (`_|_`) if any of its non-optional
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001005fields evaluates to bottom, recursively.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02001006
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001007<!--NOTE: About bottom values for optional fields being okay.
1008
1009The proposition ¬P is a close cousin of P → ⊥ and is often used
1010as an approximation to avoid the issues of using not.
1011Bottom (⊥) is also frequently used to mean undefined. This makes sense.
1012Consider `{a?: 2} & {a?: 3}`.
1013Both structs say `a` is optional; in other words, it may be omitted.
1014So we can still get a valid result by omitting `a`, even in
1015case of a conflict.
1016
1017Granted, this definition may lead to confusing results, especially in
1018definitions, when tightening an optional field leads to unintentionally
1019discarding it.
1020It could be a role of vet checkers to identify such cases (and suggest users
1021to explicitly use `_|_` to discard a field, for instance).
1022-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001023
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001024Syntactically, a struct literal may contain multiple fields with
1025the same label, the result of which is a single field with the same properties
1026as defined as the unification of two fields resulting from unifying two structs.
1027
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001028These examples illustrate required fields only.
1029Examples with optional fields follow below.
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001030
1031```
1032Expression Result (without optional fields)
1033{a: int, a: 1} {a: 1}
1034{a: int} & {a: 1} {a: 1}
1035{a: >=1 & <=7} & {a: >=5 & <=9} {a: >=5 & <=7}
1036{a: >=1 & <=7, a: >=5 & <=9} {a: >=5 & <=7}
1037
1038{a: 1} & {b: 2} {a: 1, b: 2}
1039{a: 1, b: int} & {b: 2} {a: 1, b: 2}
1040
1041{a: 1} & {a: 2} _|_
1042```
1043
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001044Optional labels are defined in sets with an expression to select all
1045labels to which to apply a given constraint.
1046Syntactically, the label of an optional field set is an expression in square
1047brackets indicating the matching labels.
1048The value `string` matches all fields, while a concrete string matches a
1049single field.
1050As the latter case is common, a concrete label followed by
1051a question mark `?` may be used as a shorthand.
1052So `foo?: bar` is a shorthand for `["foo"]: bar`.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001053The question mark is not part of the field name.
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001054The token `...` may be used as the last declaration in a struct
1055and is a shorthand for `[_]: _`.
1056
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001057Concrete field labels may be an identifier or string, the latter of which may be
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001058interpolated.
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001059Fields with identifier labels can be referred to within the scope they are
1060defined, string labels cannot.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001061References within such interpolated strings are resolved within
1062the scope of the struct in which the label sequence is
1063defined and can reference concrete labels lexically preceding
1064the label within a label sequence.
1065<!-- We allow this so that rewriting a CUE file to collapse or expand
1066field sequences has no impact on semantics.
1067-->
1068
1069<!--TODO: first implementation round will not yet have expression labels
1070
1071An ExpressionLabel sets a collection of optional fields to a field value.
1072By default it defines this value for all possible string labels.
1073An optional expression limits this to the set of optional fields which
1074labels match the expression.
1075-->
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001076
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001077
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001078<!-- NOTE: if we allow ...Expr, as in list, it would mean something different. -->
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001079
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001080
1081<!-- NOTE:
1082A DefinitionDecl does not allow repeated labels. This is to avoid
1083any ambiguity or confusion about whether earlier path components
1084are to be interpreted as declarations or normal fields (they should
1085always be normal fields.)
1086-->
1087
1088<!--NOTE:
1089The syntax has been deliberately restricted to allow for the following
1090future extensions and relaxations:
1091 - Allow omitting a "?" in an expression label to indicate a concrete
1092 string value (but maybe we want to use () for that).
1093 - Make the "?" in expression label optional if expression labels
1094 are always optional.
1095 - Or allow eliding the "?" if the expression has no references and
1096 is obviously not concrete (such as `[string]`).
1097 - The expression of an expression label may also indicate a struct with
1098 integer or even number labels
1099 (beware of imprecise computation in the latter).
1100 e.g. `{ [int]: string }` is a map of integers to strings.
1101 - Allow for associative lists (`foo [@.field]: {field: string}`)
1102 - The `...` notation can be extended analogously to that of a ListList,
1103 by allowing it to follow with an expression for the remaining properties.
1104 In that case it is no longer a shorthand for `[string]: _`, but rather
1105 would define the value for any other value for which there is no field
1106 defined.
1107 Like the definition with List, this is somewhat odd, but it allows the
1108 encoding of JSON schema's and (non-structural) OpenAPI's
1109 additionalProperties and additionalItems.
1110-->
1111
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001112```
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02001113StructLit = "{" { Declaration "," } [ "..." ] "}" .
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001114Declaration = Field | Comprehension | AliasExpr .
1115Field = LabelSpec { LabelSpec } Expression .
1116LabelSpec = Label ( ":" | "::" ) .
1117Label = LabelName [ "?" ] | "[" AliasExpr "]".
1118LabelName = identifier | simple_string_lit .
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001119
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001120attribute = "@" identifier "(" attr_elems ")" .
1121attr_elems = attr_elem { "," attr_elem }
1122attr_elem = attr_string | attr_label | attr_nest .
1123attr_label = identifier "=" attr_string .
1124attr_nest = identifier "(" attr_elems ")" .
1125attr_string = { attr_char } | string_lit .
1126attr_char = /* an arbitrary Unicode code point except newline, ',', '"', `'`, '#', '=', '(', and ')' */ .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001127```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001128
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001129<!--
1130 TODO: Label = LabelName [ "?" ] | "[" AliasExpr "]" | "(" AliasExpr ")"
1131-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001132
1133```
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001134Expression Result (without optional fields)
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001135a: { foo?: string } {}
1136b: { foo: "bar" } { foo: "bar" }
1137c: { foo?: *"bar" | string } {}
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001138
1139d: a & b { foo: "bar" }
1140e: b & c { foo: "bar" }
1141f: a & c {}
1142g: a & { foo?: number } {}
1143h: b & { foo?: number } _|_
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001144i: c & { foo: string } { foo: "bar" }
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001145
1146intMap: [string]: int
1147intMap: {
1148 t1: 43
1149 t2: 2.4 // error: 2.4 is not an integer
1150}
1151
1152nameMap: [string]: {
1153 firstName: string
1154 nickName: *firstName | string
1155}
1156
1157nameMap: hank: { firstName: "Hank" }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001158```
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001159The optional field set defined by `nameMap` matches every field,
1160in this case just `hank`, and unifies the associated constraint
1161with the matched field, resulting in:
1162```
1163nameMap: hank: {
1164 firstName: "Hank"
1165 nickName: "Hank"
1166}
1167```
1168
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001169
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001170#### Closed structs
1171
1172By default, structs are open to adding fields.
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001173Instances of an open struct `p` may contain fields not defined in `p`.
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001174This is makes it easy to add fields, but can lead to bugs:
1175
1176```
1177S: {
1178 field1: string
1179}
1180
1181S1: S & { field2: "foo" }
1182
1183// S1 is { field1: string, field2: "foo" }
1184
1185
1186A: {
1187 field1: string
1188 field2: string
1189}
1190
1191A1: A & {
1192 feild1: "foo" // "field1" was accidentally misspelled
1193}
1194
1195// A1 is
1196// { field1: string, field2: string, feild1: "foo" }
1197// not the intended
1198// { field1: "foo", field2: string }
1199```
1200
Marcel van Lohuizen18637db2019-09-03 11:48:25 +02001201A _closed struct_ `c` is a struct whose instances may not have regular fields
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001202not defined in `c`.
Marcel van Lohuizen4245fb42019-09-09 11:22:12 +02001203Closing a struct is equivalent to adding an optional field with value `_|_`
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001204for all undefined fields.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001205
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001206Syntactically, closed structs can be explicitly created with the `close` builtin
1207or implicitly by [definitions](#Definitions).
1208
1209
1210```
1211A: close({
1212 field1: string
1213 field2: string
1214})
1215
1216A1: A & {
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001217 feild1: string
1218} // _|_ feild1 not defined for A
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001219
1220A2: A & {
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001221 for k,v in { feild1: string } {
1222 k: v
1223 }
1224} // _|_ feild1 not defined for A
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001225
1226C: close({
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001227 [_]: _
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001228})
1229
1230C2: C & {
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001231 for k,v in { thisIsFine: string } {
1232 "\(k)": v
1233 }
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001234}
1235
1236D: close({
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001237 // Values generated by comprehensions are treated as embeddings.
1238 for k,v in { x: string } {
1239 "\(k)": v
1240 }
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001241})
1242```
1243
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001244<!-- (jba) Somewhere it should be said that optional fields are only
1245 interesting inside closed structs. -->
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001246
1247#### Embedding
1248
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001249A struct may contain an _embedded value_, an operand used
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001250as a declaration, which must evaluate to a struct.
1251An embedded value of type struct is unified with the struct in which it is
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001252embedded, but disregarding the restrictions imposed by closed structs.
1253A struct resulting from such a unification is closed if either of the involved
1254structs were closed.
1255
Marcel van Lohuizena3c7bef2019-10-10 21:50:58 +02001256At the top level, an embedded value may be any type.
1257In this case, a CUE program will evaluate to the embedded value
1258and the CUE program may not have top-level regular or optional
1259fields (definitions and aliases are allowed).
1260
Marcel van Lohuizene53305e2019-09-13 10:10:31 +02001261Syntactically, embeddings may be any expression, except that `<`
1262is eagerly interpreted as a bind label.
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02001263
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001264```
1265S1: {
1266 a: 1
1267 b: 2
1268 {
1269 c: 3
1270 }
1271}
1272// S1 is { a: 1, b: 2, c: 3 }
1273
1274S2: close({
1275 a: 1
1276 b: 2
1277 {
1278 c: 3
1279 }
1280})
1281// same as close(S1)
1282
1283S3: {
1284 a: 1
1285 b: 2
1286 close({
1287 c: 3
1288 })
1289}
1290// same as S2
1291```
1292
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001293
1294#### Definitions
1295
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001296A field of a struct may be declared as a regular field (using `:`)
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001297or as a _definition_ (using `::`).
1298Definitions are not emitted as part of the model and are never required
1299to be concrete when emitting data.
Marcel van Lohuizen18637db2019-09-03 11:48:25 +02001300It is illegal to have a regular field and a definition with the same name
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001301within the same struct.
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001302Literal structs that are part of a definition's value are implicitly closed,
1303but may unify unrestricted with other structs within the field's declaration.
Marcel van Lohuizen5e8c3912019-09-03 15:46:26 +02001304This excludes literals structs in embeddings and aliases.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02001305
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001306<!--
1307This may be a more intuitive definition:
1308 Literal structs that are part of a definition's value are implicitly closed.
1309 Implicitly closed literal structs that are unified within
1310 a single field declaration are considered to be a single literal struct.
1311However, this would make unification non-commutative, unless one imposes an
1312ordering where literal structs are unified before unifying them with others.
1313Imposing such an ordering is complex and error prone.
1314-->
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001315An ellipsis `...` in such literal structs keeps them open,
1316as it defines `_` for all labels.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02001317
Marcel van Lohuizen5e8c3912019-09-03 15:46:26 +02001318<!--
1319Excluding embeddings from recursive closing allows comprehensions to be
1320interpreted as embeddings without some exception. For instance,
1321 if x > 2 {
1322 foo: string
1323 }
1324should not cause any failure. It is also consistent with embeddings being
1325opened when included in a closed struct.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001326
Marcel van Lohuizen5e8c3912019-09-03 15:46:26 +02001327Finally, excluding embeddings from recursive closing allows for
1328a mechanism to not recursively close, without needing an additional language
1329construct, such as a triple colon or something else:
1330foo :: {
1331 {
1332 // not recursively closed
1333 }
1334 ... // include this to not close outer struct
1335}
1336
1337Including aliases from this exclusion, which are more a separate definition
1338than embedding seems sensible, and allows for an easy mechanism to avoid
1339closing, aside from embedding.
1340-->
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001341
1342```
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001343MyStruct :: {
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001344 sub field: string
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001345}
1346
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001347MyStruct :: {
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001348 sub enabled?: bool
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001349}
1350
1351myValue: MyStruct & {
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001352 sub feild: 2 // error, feild not defined in MyStruct
1353 sub enabled: true // okay
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001354}
1355
1356D :: {
1357 OneOf
1358
1359 c: int // adds this field.
1360}
1361
1362OneOf :: { a: int } | { b: int }
1363
1364
1365D1: D & { a: 12, c: 22 } // { a: 12, c: 22 }
1366D2: D & { a: 12, b: 33 } // _|_ // cannot define both `a` and `b`
1367```
1368
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001369
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001370<!---
1371JSON fields are usual camelCase. Clashes can be avoided by adopting the
1372convention that definitions be TitleCase. Unexported definitions are still
1373subject to clashes, but those are likely easier to resolve because they are
1374package internal.
1375--->
1376
1377
1378#### Field attributes
1379
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001380Fields may be associated with attributes.
1381Attributes define additional information about a field,
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001382such as a mapping to a protocol buffer <!-- TODO: add link --> tag or alternative
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001383name of the field when mapping to a different language.
1384
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001385<!-- TODO define attribute syntax here, before getting into semantics. -->
1386
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001387If a field has multiple attributes their identifiers must be unique.
1388Attributes accumulate when unifying two fields, removing duplicate entries.
1389It is an error for the resulting field to have two different attributes
1390with the same identifier.
1391
1392Attributes are not directly part of the data model, but may be
1393accessed through the API or other means of reflection.
1394The interpretation of the attribute value
1395(a comma-separated list of attribute elements) depends on the attribute.
1396Interpolations are not allowed in attribute strings.
1397
1398The recommended convention, however, is to interpret the first
1399`n` arguments as positional arguments,
1400where duplicate conflicting entries are an error,
1401and the remaining arguments as a combination of flags
1402(an identifier) and key value pairs, separated by a `=`.
1403
1404```
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001405myStruct1: {
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001406 field: string @go(Field)
1407 attr: int @xml(,attr) @go(Attr)
1408}
1409
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001410myStruct2: {
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001411 field: string @go(Field)
1412 attr: int @xml(a1,attr) @go(Attr)
1413}
1414
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001415Combined: myStruct1 & myStruct2
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001416// field: string @go(Field)
1417// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)
1418```
1419
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001420
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001421#### Aliases
1422
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001423Aliases name values that can be referred to
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001424within the [scope](#declarations-and-scopes) in which they are declared.
1425The name of an alias must be unique within its scope.
1426
1427```
1428AliasExpr = identifier "=" Expression | Expression .
1429```
1430
1431Aliases can appear in several positions:
1432
1433As a declaration in a struct (`X=expr`):
1434
1435- binds the value to an identifier without including it in the struct.
1436
1437In front of a Label (`X=label: value`):
1438
1439- binds the identifier to the same value as `label` would be bound
1440 to if it were a valid identifier.
1441- for optional fields (`foo?: bar` and `[foo]: bar`),
1442 the bound identifier is only visible within the field value (`value`).
1443
1444Inside a bracketed label (`[X=expr]: value`):
1445
1446- binds the identifier to the the concrete label that matches `expr`
1447 within the instances of the field value (`value`).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001448
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001449<!-- TODO: explain the difference between aliases and definitions.
1450 Now that you have definitions, are aliases really necessary?
1451 Consider removing.
1452-->
1453
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001454```
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001455// An alias declaration
1456Alias = 3
1457a: Alias // 3
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001458
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001459// A field alias
1460foo: X // 4
1461X="not an identifier": 4
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001462
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001463// A label alias
1464[Y=string]: { name: Y }
1465foo: { value: 1 } // outputs: foo: { name: "foo", value: 1 }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001466```
1467
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001468<!-- TODO: also allow aliases as lists -->
1469
1470
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001471#### Shorthand notation for nested structs
1472
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001473A field whose value is a struct with a single field may be written as
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001474a colon-separated sequence of the two field names,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001475followed by a colon and the value of that single field.
1476
1477```
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001478job: myTask: replicas: 2
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001479```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001480expands to
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001481```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001482job: {
1483 myTask: {
1484 replicas: 2
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001485 }
1486}
1487```
1488
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001489<!-- OPTIONAL FIELDS:
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001490
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +01001491The optional marker solves the issue of having to print large amounts of
1492boilerplate when dealing with large types with many optional or default
1493values (such as Kubernetes).
1494Writing such optional values in terms of *null | value is tedious,
1495unpleasant to read, and as it is not well defined what can be dropped or not,
1496all null values have to be emitted from the output, even if the user
1497doesn't override them.
1498Part of the issue is how null is defined. We could adopt a Typescript-like
1499approach of introducing "void" or "undefined" to mean "not defined and not
1500part of the output". But having all of null, undefined, and void can be
1501confusing. If these ever are introduced anyway, the ? operator could be
1502expressed along the lines of
1503 foo?: bar
1504being a shorthand for
1505 foo: void | bar
1506where void is the default if no other default is given.
1507
1508The current mechanical definition of "?" is straightforward, though, and
1509probably avoids the need for void, while solving a big issue.
1510
1511Caveats:
1512[1] this definition requires explicitly defined fields to be emitted, even
1513if they could be elided (for instance if the explicit value is the default
1514value defined an optional field). This is probably a good thing.
1515
1516[2] a default value may still need to be included in an output if it is not
1517the zero value for that field and it is not known if any outside system is
1518aware of defaults. For instance, which defaults are specified by the user
1519and which by the schema understood by the receiving system.
1520The use of "?" together with defaults should therefore be used carefully
1521in non-schema definitions.
1522Problematic cases should be easy to detect by a vet-like check, though.
1523
1524[3] It should be considered how this affects the trim command.
1525Should values implied by optional fields be allowed to be removed?
1526Probably not. This restriction is unlikely to limit the usefulness of trim,
1527though.
1528
1529[4] There should be an option to emit all concrete optional values.
1530```
1531-->
1532
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001533### Lists
1534
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001535A list literal defines a new value of type list.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001536A list may be open or closed.
1537An open list is indicated with a `...` at the end of an element list,
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001538optionally followed by a value for the remaining elements.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001539
1540The length of a closed list is the number of elements it contains.
1541The length of an open list is the its number of elements as a lower bound
1542and an unlimited number of elements as its upper bound.
1543
1544```
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01001545ListLit = "[" [ ElementList [ "," [ "..." [ Expression ] ] ] "]" .
1546ElementList = Expression { "," Expression } .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001547```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001548
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001549Lists can be thought of as structs:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001550
1551```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001552List: *null | {
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001553 Elem: _
1554 Tail: List
1555}
1556```
1557
1558For closed lists, `Tail` is `null` for the last element, for open lists it is
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001559`*null | List`, defaulting to the shortest variant.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001560For instance, the open list [ 1, 2, ... ] can be represented as:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001561```
1562open: List & { Elem: 1, Tail: { Elem: 2 } }
1563```
1564and the closed version of this list, [ 1, 2 ], as
1565```
1566closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } }
1567```
1568
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001569Using this representation, the subsumption rule for lists can
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001570be derived from those of structs.
1571Implementations are not required to implement lists as structs.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001572The `Elem` and `Tail` fields are not special and `len` will not work as
1573expected in these cases.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001574
1575
1576## Declarations and Scopes
1577
1578
1579### Blocks
1580
1581A _block_ is a possibly empty sequence of declarations.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001582The braces of a struct literal `{ ... }` form a block, but there are
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001583others as well:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001584
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001585- The _universe block_ encompasses all CUE source text.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001586- Each [package](#modules-instances-and-packages) has a _package block_
1587 containing all CUE source text in that package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001588- Each file has a _file block_ containing all CUE source text in that file.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001589- Each `for` and `let` clause in a [comprehension](#comprehensions)
1590 is considered to be its own implicit block.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001591
1592Blocks nest and influence [scoping].
1593
1594
1595### Declarations and scope
1596
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001597A _declaration_ may bind an identifier to a field, alias, or package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001598Every identifier in a program must be declared.
1599Other than for fields,
1600no identifier may be declared twice within the same block.
1601For fields an identifier may be declared more than once within the same block,
1602resulting in a field with a value that is the result of unifying the values
1603of all fields with the same identifier.
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001604String labels do not bind an identifier to the respective field.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001605
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001606The _scope_ of a declared identifier is the extent of source text in which the
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001607identifier denotes the specified field, alias, or package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001608
1609CUE is lexically scoped using blocks:
1610
Jonathan Amsterdame4790382019-01-20 10:29:29 -050016111. The scope of a [predeclared identifier](#predeclared-identifiers) is the universe block.
Marcel van Lohuizen21f6c442019-09-26 14:55:23 +020016121. The scope of an identifier denoting a field
1613 declared at top level (outside any struct literal) is the package block.
16141. The scope of an identifier denoting an alias
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001615 declared at top level (outside any struct literal) is the file block.
16161. The scope of the package name of an imported package is the file block of the
1617 file containing the import declaration.
16181. The scope of a field or alias identifier declared inside a struct literal
1619 is the innermost containing block.
1620
1621An identifier declared in a block may be redeclared in an inner block.
1622While the identifier of the inner declaration is in scope, it denotes the entity
1623declared by the inner declaration.
1624
1625The package clause is not a declaration;
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001626the package name does not appear in any scope.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001627Its purpose is to identify the files belonging to the same package
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001628and to specify the default name for import declarations.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001629
1630
1631### Predeclared identifiers
1632
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001633CUE predefines a set of types and builtin functions.
1634For each of these there is a corresponding keyword which is the name
1635of the predefined identifier, prefixed with `__`.
1636
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001637```
1638Functions
1639len required close open
1640
1641Types
1642null The null type and value
1643bool All boolean values
1644int All integral numbers
1645float All decimal floating-point numbers
1646string Any valid UTF-8 sequence
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02001647bytes Any valid byte sequence
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001648
1649Derived Value
1650number int | float
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001651uint >=0
1652uint8 >=0 & <=255
1653int8 >=-128 & <=127
1654uint16 >=0 & <=65536
1655int16 >=-32_768 & <=32_767
1656rune >=0 & <=0x10FFFF
1657uint32 >=0 & <=4_294_967_296
1658int32 >=-2_147_483_648 & <=2_147_483_647
1659uint64 >=0 & <=18_446_744_073_709_551_615
1660int64 >=-9_223_372_036_854_775_808 & <=9_223_372_036_854_775_807
1661uint128 >=0 & <=340_282_366_920_938_463_463_374_607_431_768_211_455
1662int128 >=-170_141_183_460_469_231_731_687_303_715_884_105_728 &
1663 <=170_141_183_460_469_231_731_687_303_715_884_105_727
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02001664float32 >=-3.40282346638528859811704183484516925440e+38 &
1665 <=3.40282346638528859811704183484516925440e+38
1666float64 >=-1.797693134862315708145274237317043567981e+308 &
1667 <=1.797693134862315708145274237317043567981e+308
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001668```
1669
1670
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001671### Exported identifiers
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001672
1673An identifier of a package may be exported to permit access to it
1674from another package.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001675An identifier is exported if
1676the first character of the identifier's name is a Unicode upper case letter
1677(Unicode class "Lu"); and
1678the identifier is declared in the file block.
1679All other top-level identifiers used for fields not exported.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001680
1681In addition, any definition declared anywhere within a package of which
1682the first character of the identifier's name is a Unicode upper case letter
1683(Unicode class "Lu") is visible outside this package.
1684Any other defintion is not visible outside the package and resides
1685in a separate namespace than namesake identifiers of other packages.
1686This is in contrast to ordinary field declarations that do not begin with
1687an upper-case letter, which are visible outside the package.
1688
1689```
1690package mypackage
1691
1692foo: string // not visible outside mypackage
1693
Marcel van Lohuizen21f6c442019-09-26 14:55:23 +02001694Foo :: { // visible outside mypackage
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001695 a: 1 // visible outside mypackage
1696 B: 2 // visible outside mypackage
1697
1698 C :: { // visible outside mypackage
1699 d: 4 // visible outside mypackage
1700 }
1701 e :: foo // not visible outside mypackage
1702}
1703```
1704
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001705
1706### Uniqueness of identifiers
1707
1708Given a set of identifiers, an identifier is called unique if it is different
1709from every other in the set, after applying normalization following
1710Unicode Annex #31.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001711Two identifiers are different if they are spelled differently
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001712or if they appear in different packages and are not exported.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001713Otherwise, they are the same.
1714
1715
1716### Field declarations
1717
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001718A field associates the value of an expression to a label within a struct.
1719If this label is an identifier, it binds the field to that identifier,
1720so the field's value can be referenced by writing the identifier.
1721String labels are not bound to fields.
1722```
1723a: {
1724 b: 2
1725 "s": 3
1726
1727 c: b // 2
1728 d: s // _|_ unresolved identifier "s"
1729 e: a.s // 3
1730}
1731```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001732
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001733If an expression may result in a value associated with a default value
1734as described in [default values](#default-values), the field binds to this
1735value-default pair.
1736
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001737
Marcel van Lohuizenbcf832f2019-04-03 22:50:44 +02001738<!-- TODO: disallow creating identifiers starting with __
1739...and reserve them for builtin values.
1740
1741The issue is with code generation. As no guarantee can be given that
1742a predeclared identifier is not overridden in one of the enclosing scopes,
1743code will have to handle detecting such cases and renaming them.
1744An alternative is to have the predeclared identifiers be aliases for namesake
1745equivalents starting with a double underscore (e.g. string -> __string),
1746allowing generated code (normal code would keep using `string`) to refer
1747to these directly.
1748-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001749
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001750
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001751### Alias declarations
1752
1753An alias declaration binds an identifier to the given expression.
1754
1755Within the scope of the identifier, it serves as an _alias_ for that
1756expression.
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001757The expression is evaluated in the scope it was declared.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001758
1759
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001760## Expressions
1761
1762An expression specifies the computation of a value by applying operators and
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001763built-in functions to operands.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001764
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001765Expressions that require concrete values are called _incomplete_ if any of
1766their operands are not concrete, but define a value that would be legal for
1767that expression.
1768Incomplete expressions may be left unevaluated until a concrete value is
1769requested at the application level.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001770
1771### Operands
1772
1773Operands denote the elementary values in an expression.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001774An operand may be a literal, a (possibly qualified) identifier denoting
1775field, alias, or a parenthesized expression.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001776
1777```
1778Operand = Literal | OperandName | ListComprehension | "(" Expression ")" .
1779Literal = BasicLit | ListLit | StructLit .
1780BasicLit = int_lit | float_lit | string_lit |
1781 null_lit | bool_lit | bottom_lit | top_lit .
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001782OperandName = identifier | QualifiedIdent .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001783```
1784
1785### Qualified identifiers
1786
1787A qualified identifier is an identifier qualified with a package name prefix.
1788
1789```
1790QualifiedIdent = PackageName "." identifier .
1791```
1792
1793A qualified identifier accesses an identifier in a different package,
1794which must be [imported].
1795The identifier must be declared in the [package block] of that package.
1796
1797```
1798math.Sin // denotes the Sin function in package math
1799```
1800
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001801### References
1802
1803An identifier operand refers to a field and is called a reference.
1804The value of a reference is a copy of the expression associated with the field
1805that it is bound to,
1806with any references within that expression bound to the respective copies of
1807the fields they were originally bound to.
1808Implementations may use a different mechanism to evaluate as long as
1809these semantics are maintained.
1810
1811```
1812a: {
1813 place: string
1814 greeting: "Hello, \(place)!"
1815}
1816
1817b: a & { place: "world" }
1818c: a & { place: "you" }
1819
1820d: b.greeting // "Hello, world!"
1821e: c.greeting // "Hello, you!"
1822```
1823
1824
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001825
1826### Primary expressions
1827
1828Primary expressions are the operands for unary and binary expressions.
1829
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001830
1831```
1832
1833Slice: indices must be complete
1834([0, 1, 2, 3] | [2, 3])[0:2] => [0, 1] | [2, 3]
1835
1836([0, 1, 2, 3] | *[2, 3])[0:2] => [0, 1] | [2, 3]
1837([0,1,2,3]|[2,3], [2,3])[0:2] => ([0,1]|[2,3], [2,3])
1838
1839Index
1840a: (1|2, 1)
1841b: ([0,1,2,3]|[2,3], [2,3])[a] => ([0,1,2,3]|[2,3][a], 3)
1842
1843Binary operation
1844A binary is only evaluated if its operands are complete.
1845
1846Input Maximum allowed evaluation
1847a: string string
1848b: 2 2
1849c: a * b a * 2
1850
1851An error in a struct is if the evaluation of any expression results in
1852bottom, where an incomplete expression is not considered bottom.
1853```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01001854<!-- TODO(mpvl)
1855 Conversion |
1856-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001857```
1858PrimaryExpr =
1859 Operand |
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001860 PrimaryExpr Selector |
1861 PrimaryExpr Index |
1862 PrimaryExpr Slice |
1863 PrimaryExpr Arguments .
1864
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001865Selector = "." (identifier | simple_string_lit) .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001866Index = "[" Expression "]" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001867Argument = Expression .
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001868Arguments = "(" [ ( Argument { "," Argument } ) [ "," ] ] ")" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001869```
1870<!---
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001871TODO:
1872 PrimaryExpr Query |
1873Query = "." Filters .
1874Filters = Filter { Filter } .
1875Filter = "[" [ "?" ] AliasExpr "]" .
1876
1877TODO: maybe reintroduce slices, as they are useful in queries, probably this
1878time with Python semantics.
1879Slice = "[" [ Expression ] ":" [ Expression ] [ ":" [Expression] ] "]" .
1880
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001881Argument = Expression | ( identifer ":" Expression ).
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001882
1883// & expression type
1884// string_lit: same as label. Arguments is current node.
1885// If selector is applied to list, it performs the operation for each
1886// element.
1887
1888TODO: considering allowing decimal_lit for selectors.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001889--->
1890
1891```
1892x
18932
1894(s + ".txt")
1895f(3.1415, true)
1896m["foo"]
1897s[i : j + 1]
1898obj.color
1899f.p[i].x
1900```
1901
1902
1903### Selectors
1904
Roger Peppeded0e1d2019-09-24 16:39:36 +01001905For a [primary expression](#primary-expressions) `x` that is not a [package name](#package-clause),
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001906the selector expression
1907
1908```
1909x.f
1910```
1911
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001912denotes the element of a <!--list or -->struct `x` identified by `f`.
1913<!--For structs, -->`f` must be an identifier or a string literal identifying
1914any definition or regular non-optional field.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001915The identifier `f` is called the field selector.
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001916
1917<!--
1918Allowing strings to be used as field selectors obviates the need for
1919backquoted identifiers. Note that some standards use names for structs that
1920are not standard identifiers (such "Fn::Foo"). Note that indexing does not
1921allow access to identifiers.
1922-->
1923
1924<!--
1925For lists, `f` must be an integer and follows the same lookup rules as
1926for the index operation.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001927The type of the selector expression is the type of `f`.
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001928-->
1929
Roger Peppeded0e1d2019-09-24 16:39:36 +01001930If `x` is a package name, see the section on [qualified identifiers](#qualified-identifiers).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001931
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001932<!--
1933TODO: consider allowing this and also for selectors. It needs to be considered
1934how defaults are corried forward in cases like:
1935
1936 x: { a: string | *"foo" } | *{ a: int | *4 }
1937 y: x.a & string
1938
1939What is y in this case?
1940 (x.a & string, _|_)
1941 (string|"foo", _|_)
1942 (string|"foo", "foo)
1943If the latter, then why?
1944
1945For a disjunction of the form `x1 | ... | xn`,
1946the selector is applied to each element `x1.f | ... | xn.f`.
1947-->
1948
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001949Otherwise, if `x` is not a <!--list or -->struct,
1950or if `f` does not exist in `x`,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001951the result of the expression is bottom (an error).
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001952In the latter case the expression is incomplete.
1953The operand of a selector may be associated with a default.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001954
1955```
1956T: {
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001957 x: int
1958 y: 3
1959 "x-y": 4
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001960}
1961
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001962a: T.x // int
1963b: T.y // 3
1964c: T.z // _|_ // field 'z' not found in T
1965d: T."x-y" // 4
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001966
1967e: {a: 1|*2} | *{a: 3|*4}
1968f: e.a // 4 (default value)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001969```
1970
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001971<!--
1972```
1973(v, d).f => (v.f, d.f)
1974
1975e: {a: 1|*2} | *{a: 3|*4}
1976f: e.a // 4 after selecting default from (({a: 1|*2} | {a: 3|*4}).a, 4)
1977
1978```
1979-->
1980
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001981
1982### Index expressions
1983
1984A primary expression of the form
1985
1986```
1987a[x]
1988```
1989
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02001990denotes the element of a list or struct `a` indexed by `x`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001991The value `x` is called the index or field name, respectively.
1992The following rules apply:
1993
1994If `a` is not a struct:
1995
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02001996- `a` is a list (which need not be complete)
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001997- the index `x` unified with `int` must be concrete.
1998- the index `x` is in range if `0 <= x < len(a)`, where only the
1999 explicitly defined values of an open-ended list are considered,
2000 otherwise it is out of range
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002001
2002The result of `a[x]` is
2003
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02002004for `a` of list type:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002005
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02002006- the list element at index `x`, if `x` is within range
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002007- bottom (an error), otherwise
2008
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002009
2010for `a` of struct type:
2011
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002012- the index `x` unified with `string` must be concrete.
Marcel van Lohuizend2825532019-09-23 12:44:01 +01002013- the value of the regular and non-optional field named `x` of struct `a`,
2014 if this field exists
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002015- bottom (an error), otherwise
2016
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002017
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002018```
2019[ 1, 2 ][1] // 2
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002020[ 1, 2 ][2] // _|_
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002021[ 1, 2, ...][2] // _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002022```
2023
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002024Both the operand and index value may be a value-default pair.
2025```
2026va[vi] => va[vi]
2027va[(vi, di)] => (va[vi], va[di])
2028(va, da)[vi] => (va[vi], da[vi])
2029(va, da)[(vi, di)] => (va[vi], da[di])
2030```
2031
2032```
2033Fields Result
2034x: [1, 2] | *[3, 4] ([1,2]|[3,4], [3,4])
2035i: int | *1 (int, 1)
2036
2037v: x[i] (x[i], 4)
2038```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002039
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002040### Operators
2041
2042Operators combine operands into expressions.
2043
2044```
2045Expression = UnaryExpr | Expression binary_op Expression .
2046UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
2047
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002048binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op .
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01002049rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002050add_op = "+" | "-" .
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002051mul_op = "*" | "/" | "div" | "mod" | "quo" | "rem" .
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01002052unary_op = "+" | "-" | "!" | "*" | rel_op .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002053```
2054
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002055Comparisons are discussed [elsewhere](#Comparison-operators).
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01002056For any binary operators, the operand types must unify.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002057
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002058<!-- TODO: durations
2059 unless the operation involves durations.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002060
2061Except for duration operations, if one operand is an untyped [literal] and the
2062other operand is not, the constant is [converted] to the type of the other
2063operand.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002064-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002065
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002066Operands of unary and binary expressions may be associated with a default using
2067the following
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002068
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002069<!--
2070```
2071O1: op (v1, d1) => (op v1, op d1)
2072
2073O2: (v1, d1) op (v2, d2) => (v1 op v2, d1 op d2)
2074and because v => (v, v)
2075O3: v1 op (v2, d2) => (v1 op v2, v1 op d2)
2076O4: (v1, d1) op v2 => (v1 op v2, d1 op v2)
2077```
2078-->
2079
2080```
2081Field Resulting Value-Default pair
2082a: *1|2 (1|2, 1)
2083b: -a (-a, -1)
2084
2085c: a + 2 (a+2, 3)
2086d: a + a (a+a, 2)
2087```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002088
2089#### Operator precedence
2090
2091Unary operators have the highest precedence.
2092
2093There are eight precedence levels for binary operators.
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002094Multiplication operators binds strongest, followed by
2095addition operators, comparison operators,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002096`&&` (logical AND), `||` (logical OR), `&` (unification),
2097and finally `|` (disjunction):
2098
2099```
2100Precedence Operator
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002101 7 * / div mod quo rem
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002102 6 + -
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01002103 5 == != < <= > >= =~ !~
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002104 4 &&
2105 3 ||
2106 2 &
2107 1 |
2108```
2109
2110Binary operators of the same precedence associate from left to right.
2111For instance, `x / y * z` is the same as `(x / y) * z`.
2112
2113```
2114+x
211523 + 3*x[i]
2116x <= f()
2117f() || g()
2118x == y+1 && y == z-1
21192 | int
2120{ a: 1 } & { b: 2 }
2121```
2122
2123#### Arithmetic operators
2124
2125Arithmetic operators apply to numeric values and yield a result of the same type
2126as the first operand. The three of the four standard arithmetic operators
2127`(+, -, *)` apply to integer and decimal floating-point types;
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01002128`+` and `*` also apply to lists and strings.
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002129`/` only applies to decimal floating-point types and
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002130`div`, `mod`, `quo`, and `rem` only apply to integer types.
2131
2132```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002133+ sum integers, floats, lists, strings, bytes
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002134- difference integers, floats
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002135* product integers, floats, lists, strings, bytes
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002136/ quotient floats
2137div division integers
2138mod modulo integers
2139quo quotient integers
2140rem remainder integers
2141```
2142
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002143For any operator that accepts operands of type `float`, any operand may be
2144of type `int` or `float`, in which case the result will be `float` if any
2145of the operands is `float` or `int` otherwise.
2146For `/` the result is always `float`.
2147
2148
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002149#### Integer operators
2150
2151For two integer values `x` and `y`,
2152the integer quotient `q = x div y` and remainder `r = x mod y `
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002153implement Euclidean division and
2154satisfy the following relationship:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002155
2156```
2157r = x - y*q with 0 <= r < |y|
2158```
2159where `|y|` denotes the absolute value of `y`.
2160
2161```
2162 x y x div y x mod y
2163 5 3 1 2
2164-5 3 -2 1
2165 5 -3 -1 2
2166-5 -3 2 1
2167```
2168
2169For two integer values `x` and `y`,
2170the integer quotient `q = x quo y` and remainder `r = x rem y `
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002171implement truncated division and
2172satisfy the following relationship:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002173
2174```
2175x = q*y + r and |r| < |y|
2176```
2177
2178with `x quo y` truncated towards zero.
2179
2180```
2181 x y x quo y x rem y
2182 5 3 1 2
2183-5 3 -1 -2
2184 5 -3 -1 2
2185-5 -3 1 -2
2186```
2187
2188A zero divisor in either case results in bottom (an error).
2189
2190For integer operands, the unary operators `+` and `-` are defined as follows:
2191
2192```
2193+x is 0 + x
2194-x negation is 0 - x
2195```
2196
2197
2198#### Decimal floating-point operators
2199
2200For decimal floating-point numbers, `+x` is the same as `x`,
2201while -x is the negation of x.
2202The result of a floating-point division by zero is bottom (an error).
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002203
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002204<!-- TODO: consider making it +/- Inf -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002205
2206An implementation may combine multiple floating-point operations into a single
2207fused operation, possibly across statements, and produce a result that differs
2208from the value obtained by executing and rounding the instructions individually.
2209
2210
2211#### List operators
2212
2213Lists can be concatenated using the `+` operator.
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002214Opens list are closed to their default value beforehand.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002215
2216```
2217[ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
2218[ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002219[ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4 ]
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002220```
2221
Jonathan Amsterdam0500c312019-02-16 18:04:09 -05002222Lists can be multiplied with a non-negative`int` using the `*` operator
Marcel van Lohuizen13e36bd2019-02-01 09:59:18 +01002223to create a repeated the list by the indicated number.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002224```
22253*[1,2] // [1, 2, 1, 2, 1, 2]
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +020022263*[1, 2, ...] // [1, 2, 1, 2, 1 ,2]
Marcel van Lohuizen13e36bd2019-02-01 09:59:18 +01002227[byte]*4 // [byte, byte, byte, byte]
Jonathan Amsterdam0500c312019-02-16 18:04:09 -050022280*[1,2] // []
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002229```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002230
2231<!-- TODO(mpvl): should we allow multiplication with a range?
2232If so, how does one specify a list with a range of possible lengths?
2233
2234Suggestion from jba:
2235Multiplication should distribute over disjunction,
2236so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x].
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002237The hard part is figuring out what (>=1 & <=3) * [x] means,
2238since >=1 & <=3 includes many floats.
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002239(mpvl: could constrain arguments to parameter types, but needs to be
2240done consistently.)
2241-->
2242
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002243
2244#### String operators
2245
2246Strings can be concatenated using the `+` operator:
2247```
2248s := "hi " + name + " and good bye"
2249```
2250String addition creates a new string by concatenating the operands.
2251
2252A string can be repeated by multiplying it:
2253
2254```
2255s: "etc. "*3 // "etc. etc. etc. "
2256```
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002257
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002258<!-- jba: Do these work for byte sequences? If not, why not? -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002259
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002260
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002261##### Comparison operators
2262
2263Comparison operators compare two operands and yield an untyped boolean value.
2264
2265```
2266== equal
2267!= not equal
2268< less
2269<= less or equal
2270> greater
2271>= greater or equal
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002272=~ matches regular expression
2273!~ does not match regular expression
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002274```
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002275
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002276<!-- regular expression operator inspired by Bash, Perl, and Ruby. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002277
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002278In any comparison, the types of the two operands must unify or one of the
2279operands must be null.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002280
2281The equality operators `==` and `!=` apply to operands that are comparable.
2282The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered.
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002283The matching operators `=~` and `!~` apply to a string and regular
2284expression operand.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002285These terms and the result of the comparisons are defined as follows:
2286
Marcel van Lohuizen855243e2019-02-07 18:00:55 +01002287- Null is comparable with itself and any other type.
2288 Two null values are always equal, null is unequal with anything else.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002289- Boolean values are comparable.
2290 Two boolean values are equal if they are either both true or both false.
2291- Integer values are comparable and ordered, in the usual way.
2292- Floating-point values are comparable and ordered, as per the definitions
2293 for binary coded decimals in the IEEE-754-2008 standard.
Marcel van Lohuizen4a360992019-05-11 18:18:31 +02002294- Floating point numbers may be compared with integers.
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02002295- String and bytes values are comparable and ordered lexically byte-wise.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002296- Struct are not comparable.
Marcel van Lohuizen855243e2019-02-07 18:00:55 +01002297- Lists are not comparable.
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002298- The regular expression syntax is the one accepted by RE2,
2299 described in https://github.com/google/re2/wiki/Syntax,
2300 except for `\C`.
2301- `s =~ r` is true if `s` matches the regular expression `r`.
2302- `s !~ r` is true if `s` does not match regular expression `r`.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002303
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02002304<!--- TODO: consider the following
2305- For regular expression, named capture groups are interpreted as CUE references
2306 that must unify with the strings matching this capture group.
2307--->
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002308<!-- TODO: Implementations should adopt an algorithm that runs in linear time? -->
Marcel van Lohuizen88a8a5f2019-02-20 01:26:22 +01002309<!-- Consider implementing Level 2 of Unicode regular expression. -->
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002310
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002311```
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +010023123 < 4 // true
Marcel van Lohuizen4a360992019-05-11 18:18:31 +020023133 < 4.0 // true
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002314null == 2 // false
2315null != {} // true
2316{} == {} // _|_: structs are not comparable against structs
2317
2318"Wild cats" =~ "cat" // true
2319"Wild cats" !~ "dog" // true
2320
2321"foo" =~ "^[a-z]{3}$" // true
2322"foo" =~ "^[a-z]{4}$" // false
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002323```
2324
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002325<!-- jba
2326I think I know what `3 < a` should mean if
2327
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002328 a: >=1 & <=5
2329
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002330It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely.
2331
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002332But 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 -05002333-->
2334
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002335#### Logical operators
2336
2337Logical operators apply to boolean values and yield a result of the same type
2338as the operands. The right operand is evaluated conditionally.
2339
2340```
2341&& conditional AND p && q is "if p then q else false"
2342|| conditional OR p || q is "if p then true else q"
2343! NOT !p is "not p"
2344```
2345
2346
2347<!--
2348### TODO TODO TODO
2349
23503.14 / 0.0 // illegal: division by zero
2351Illegal conversions always apply to CUE.
2352
2353Implementation 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.
2354-->
2355
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002356<!--- TODO(mpvl): conversions
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002357### Conversions
2358Conversions are expressions of the form `T(x)` where `T` and `x` are
2359expressions.
2360The result is always an instance of `T`.
2361
2362```
2363Conversion = Expression "(" Expression [ "," ] ")" .
2364```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002365--->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002366<!---
2367
2368A literal value `x` can be converted to type T if `x` is representable by a
2369value of `T`.
2370
2371As a special case, an integer literal `x` can be converted to a string type
2372using the same rule as for non-constant x.
2373
2374Converting a literal yields a typed value as result.
2375
2376```
2377uint(iota) // iota value of type uint
2378float32(2.718281828) // 2.718281828 of type float32
2379complex128(1) // 1.0 + 0.0i of type complex128
2380float32(0.49999999) // 0.5 of type float32
2381float64(-1e-1000) // 0.0 of type float64
2382string('x') // "x" of type string
2383string(0x266c) // "♬" of type string
2384MyString("foo" + "bar") // "foobar" of type MyString
2385string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
2386(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
2387int(1.2) // illegal: 1.2 cannot be represented as an int
2388string(65.0) // illegal: 65.0 is not an integer constant
2389```
2390--->
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002391<!---
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002392
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002393A conversion is always allowed if `x` is an instance of `T`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002394
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002395If `T` and `x` of different underlying type, a conversion is allowed if
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002396`x` can be converted to a value `x'` of `T`'s type, and
2397`x'` is an instance of `T`.
2398A value `x` can be converted to the type of `T` in any of these cases:
2399
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002400- `x` is a struct and is subsumed by `T`.
2401- `x` and `T` are both integer or floating points.
2402- `x` is an integer or a byte sequence and `T` is a string.
2403- `x` is a string and `T` is a byte sequence.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002404
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002405Specific rules apply to conversions between numeric types, structs,
2406or to and from a string type. These conversions may change the representation
2407of `x`.
2408All other conversions only change the type but not the representation of x.
2409
2410
2411#### Conversions between numeric ranges
2412For the conversion of numeric values, the following rules apply:
2413
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +010024141. Any integer value can be converted into any other integer value
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002415 provided that it is within range.
24162. When converting a decimal floating-point number to an integer, the fraction
2417 is discarded (truncation towards zero). TODO: or disallow truncating?
2418
2419```
2420a: uint16(int(1000)) // uint16(1000)
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002421b: uint8(1000) // _|_ // overflow
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002422c: int(2.5) // 2 TODO: TBD
2423```
2424
2425
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002426#### Conversions to and from a string type
2427
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002428Converting a list of bytes to a string type yields a string whose successive
2429bytes are the elements of the slice.
2430Invalid UTF-8 is converted to `"\uFFFD"`.
2431
2432```
2433string('hell\xc3\xb8') // "hellø"
2434string(bytes([0x20])) // " "
2435```
2436
2437As string value is always convertible to a list of bytes.
2438
2439```
2440bytes("hellø") // 'hell\xc3\xb8'
2441bytes("") // ''
2442```
2443
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002444#### Conversions between list types
2445
2446Conversions between list types are possible only if `T` strictly subsumes `x`
2447and the result will be the unification of `T` and `x`.
2448
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002449If we introduce named types this would be different from IP & [10, ...]
2450
2451Consider removing this until it has a different meaning.
2452
2453```
2454IP: 4*[byte]
2455Private10: IP([10, ...]) // [10, byte, byte, byte]
2456```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002457
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002458#### Conversions between struct types
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002459
2460A conversion from `x` to `T`
2461is applied using the following rules:
2462
24631. `x` must be an instance of `T`,
24642. all fields defined for `x` that are not defined for `T` are removed from
2465 the result of the conversion, recursively.
2466
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002467<!-- jba: I don't think you say anywhere that the matching fields are unified.
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002468mpvl: they are not, x must be an instance of T, in which case x == T&x,
2469so unification would be unnecessary.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002470-->
Marcel van Lohuizena3f00972019-02-01 11:10:39 +01002471<!--
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002472```
2473T: {
2474 a: { b: 1..10 }
2475}
2476
2477x1: {
2478 a: { b: 8, c: 10 }
2479 d: 9
2480}
2481
2482c1: T(x1) // { a: { b: 8 } }
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002483c2: T({}) // _|_ // missing field 'a' in '{}'
2484c3: T({ a: {b: 0} }) // _|_ // field a.b does not unify (0 & 1..10)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002485```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002486-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002487
2488### Calls
2489
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002490Calls can be made to core library functions, called builtins.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002491Given an expression `f` of function type F,
2492```
2493f(a1, a2, … an)
2494```
2495calls `f` with arguments a1, a2, … an. Arguments must be expressions
2496of which the values are an instance of the parameter types of `F`
2497and are evaluated before the function is called.
2498
2499```
2500a: math.Atan2(x, y)
2501```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002502
2503In a function call, the function value and arguments are evaluated in the usual
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01002504order.
2505After they are evaluated, the parameters of the call are passed by value
2506to the function and the called function begins execution.
2507The return parameters
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002508of the function are passed by value back to the calling function when the
2509function returns.
2510
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002511
2512### Comprehensions
2513
Marcel van Lohuizen66db9202018-12-17 19:02:08 +01002514Lists and fields can be constructed using comprehensions.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002515
2516Each define a clause sequence that consists of a sequence of `for`, `if`, and
2517`let` clauses, nesting from left to right.
2518The `for` and `let` clauses each define a new scope in which new values are
2519bound to be available for the next clause.
2520
2521The `for` clause binds the defined identifiers, on each iteration, to the next
2522value of some iterable value in a new scope.
2523A `for` clause may bind one or two identifiers.
Marcel van Lohuizen4245fb42019-09-09 11:22:12 +02002524If there is one identifier, it binds it to the value of
2525a list element or struct field value.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002526If there are two identifiers, the first value will be the key or index,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002527if available, and the second will be the value.
2528
Marcel van Lohuizen4245fb42019-09-09 11:22:12 +02002529For lists, `for` iterates over all elements in the list after closing it.
2530For structs, `for` iterates over all non-optional regular fields.
2531
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002532An `if` clause, or guard, specifies an expression that terminates the current
2533iteration if it evaluates to false.
2534
2535The `let` clause binds the result of an expression to the defined identifier
2536in a new scope.
2537
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002538A current iteration is said to complete if the innermost block of the clause
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002539sequence is reached.
2540
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002541_List comprehensions_ specify a single expression that is evaluated and included
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002542in the list for each completed iteration.
2543
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002544_Field comprehensions_ follow a clause sequence with a struct literal,
2545where the struct literal is evaluated and embedded at the point of
2546declaration of the comprehension for each complete iteration.
2547As usual, fields in the struct may evaluate to the same label,
2548resulting in the unification of their values.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002549
2550```
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02002551Comprehension = Clauses StructLit .
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002552ListComprehension = "[" Expression Clauses "]" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002553
2554Clauses = Clause { Clause } .
2555Clause = ForClause | GuardClause | LetClause .
2556ForClause = "for" identifier [ ", " identifier] "in" Expression .
2557GuardClause = "if" Expression .
2558LetClause = "let" identifier "=" Expression .
2559```
2560
2561```
2562a: [1, 2, 3, 4]
2563b: [ x+1 for x in a if x > 1] // [3, 4, 5]
2564
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002565c: {
2566 for x in a
2567 if x < 4
2568 let y = 1 {
2569 "\(x)": x + y
2570 }
2571}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002572d: { "1": 2, "2": 3, "3": 4 }
2573```
2574
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002575
2576### String interpolation
2577
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002578String interpolation allows constructing strings by replacing placeholder
2579expressions with their string representation.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002580String interpolation may be used in single- and double-quoted strings, as well
2581as their multiline equivalent.
2582
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002583A placeholder consists of "\(" followed by an expression and a ")". The
2584expression is evaluated within the scope within which the string is defined.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002585
2586```
2587a: "World"
2588b: "Hello \( a )!" // Hello World!
2589```
2590
2591
2592## Builtin Functions
2593
2594Built-in functions are predeclared. They are called like any other function.
2595
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002596
2597### `len`
2598
2599The built-in function `len` takes arguments of various types and return
2600a result of type int.
2601
2602```
2603Argument type Result
2604
2605string string length in bytes
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002606bytes length of byte sequence
2607list list length, smallest length for an open list
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002608struct number of distinct data fields, including optional
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002609```
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002610<!-- TODO: consider not supporting len, but instead rely on more
2611precisely named builtin functions:
2612 - strings.RuneLen(x)
2613 - bytes.Len(x) // x may be a string
2614 - struct.NumFooFields(x)
2615 - list.Len(x)
2616-->
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002617
2618```
2619Expression Result
2620len("Hellø") 6
2621len([1, 2, 3]) 3
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002622len([1, 2, ...]) >=2
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002623```
2624
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02002625
2626### `close`
2627
2628The builtin function `close` converts a partially defined, or open, struct
2629to a fully defined, or closed, struct.
2630
2631
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002632### `and`
2633
2634The built-in function `and` takes a list and returns the result of applying
2635the `&` operator to all elements in the list.
2636It returns top for the empty list.
2637
2638Expression: Result
2639and([a, b]) a & b
2640and([a]) a
2641and([]) _
2642
2643### `or`
2644
2645The built-in function `or` takes a list and returns the result of applying
2646the `|` operator to all elements in the list.
2647It returns bottom for the empty list.
2648
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002649```
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002650Expression: Result
2651and([a, b]) a | b
2652and([a]) a
2653and([]) _|_
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002654```
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002655
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002656
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002657## Cycles
2658
2659Implementations are required to interpret or reject cycles encountered
2660during evaluation according to the rules in this section.
2661
2662
2663### Reference cycles
2664
2665A _reference cycle_ occurs if a field references itself, either directly or
2666indirectly.
2667
2668```
2669// x references itself
2670x: x
2671
2672// indirect cycles
2673b: c
2674c: d
2675d: b
2676```
2677
2678Implementations should report these as an error except in the following cases:
2679
2680
2681#### Expressions that unify an atom with an expression
2682
2683An expression of the form `a & e`, where `a` is an atom
2684and `e` is an expression, always evaluates to `a` or bottom.
2685As it does not matter how we fail, we can assume the result to be `a`
2686and validate after the field in which the expression occurs has been evaluated
2687that `a == e`.
2688
2689```
Marcel van Lohuizeneac8f9a2019-08-03 13:53:56 +02002690// Config Evaluates to (requiring concrete values)
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002691x: { x: {
2692 a: b + 100 a: _|_ // cycle detected
2693 b: a - 100 b: _|_ // cycle detected
2694} }
2695
2696y: x & { y: {
2697 a: 200 a: 200 // asserted that 200 == b + 100
2698 b: 100
2699} }
2700```
2701
2702
2703#### Field values
2704
2705A field value of the form `r & v`,
2706where `r` evaluates to a reference cycle and `v` is a value,
2707evaluates to `v`.
2708Unification is idempotent and unifying a value with itself ad infinitum,
2709which is what the cycle represents, results in this value.
2710Implementations should detect cycles of this kind, ignore `r`,
2711and take `v` as the result of unification.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002712
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002713<!-- Tomabechi's graph unification algorithm
2714can detect such cycles at near-zero cost. -->
2715
2716```
2717Configuration Evaluated
2718// c Cycles in nodes of type struct evaluate
2719// ↙︎ ↖ to the fixed point of unifying their
2720// a → b values ad infinitum.
2721
2722a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 }
2723b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 }
2724c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 }
2725
2726// resolve a b & {x:1}
2727// substitute b c & {y:2} & {x:1}
2728// substitute c a & {z:3} & {y:2} & {x:1}
2729// eliminate a (cycle) {z:3} & {y:2} & {x:1}
2730// simplify {x:1,y:2,z:3}
2731```
2732
2733This rule also applies to field values that are disjunctions of unification
2734operations of the above form.
2735
2736```
2737a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1}
2738b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2}
2739c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3}
2740
2741
2742// resolving a b&{x:1} | {y:1}
2743// substitute b ({x:2} | c&{z:2})&{x:1} | {y:1}
2744// simplify c&{z:2}&{x:1} | {y:1}
2745// substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1}
2746// simplify a&{y:3}&{z:2}&{x:1} | {y:1}
2747// eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1}
2748// expand {x:1,y:3,z:2} | {y:1}
2749```
2750
2751Note that all nodes that form a reference cycle to form a struct will evaluate
2752to the same value.
2753If a field value is a disjunction, any element that is part of a cycle will
2754evaluate to this value.
2755
2756
2757### Structural cycles
2758
2759CUE disallows infinite structures.
2760Implementations must report an error when encountering such declarations.
2761
2762<!-- for instance using an occurs check -->
2763
2764```
2765// Disallowed: a list of infinite length with all elements being 1.
2766list: {
2767 head: 1
2768 tail: list
2769}
2770
2771// Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...).
2772a: {
2773 b: c
2774}
2775c: {
2776 d: a
2777}
2778```
2779
2780It is allowed for a value to define an infinite set of possibilities
2781without evaluating to an infinite structure itself.
2782
2783```
2784// List defines a list of arbitrary length (default null).
2785List: *null | {
2786 head: _
2787 tail: List
2788}
2789```
2790
2791<!--
Marcel van Lohuizen7f48df72019-02-01 17:24:59 +01002792Consider banning any construct that makes CUE not having a linear
2793running time expressed in the number of nodes in the output.
2794
2795This would require restricting constructs like:
2796
2797(fib&{n:2}).out
2798
2799fib: {
2800 n: int
2801
2802 out: (fib&{n:n-2}).out + (fib&{n:n-1}).out if n >= 2
2803 out: fib({n:n-2}).out + fib({n:n-1}).out if n >= 2
2804 out: n if n < 2
2805}
2806
2807-->
2808<!--
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002809### Unused fields
2810
2811TODO: rules for detection of unused fields
2812
28131. Any alias value must be used
2814-->
2815
2816
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002817## Modules, instances, and packages
2818
2819CUE configurations are constructed combining _instances_.
2820An instance, in turn, is constructed from one or more source files belonging
2821to the same _package_ that together declare the data representation.
2822Elements of this data representation may be exported and used
2823in other instances.
2824
2825### Source file organization
2826
2827Each source file consists of an optional package clause defining collection
2828of files to which it belongs,
2829followed by a possibly empty set of import declarations that declare
2830packages whose contents it wishes to use, followed by a possibly empty set of
2831declarations.
2832
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02002833Like with a struct, a source file may contain embeddings.
2834Unlike with a struct, the embedded expressions may be any value.
2835If the result of the unification of all embedded values is not a struct,
2836it will be output instead of its enclosing file when exporting CUE
2837to a data format
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002838
2839```
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02002840SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
2841```
2842
2843```
2844"Hello \(place)!"
2845
2846place: "world"
2847
2848// Outputs "Hello world!"
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002849```
2850
2851### Package clause
2852
2853A package clause is an optional clause that defines the package to which
2854a source file the file belongs.
2855
2856```
2857PackageClause = "package" PackageName .
2858PackageName = identifier .
2859```
2860
2861The PackageName must not be the blank identifier.
2862
2863```
2864package math
2865```
2866
2867### Modules and instances
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002868A _module_ defines a tree of directories, rooted at the _module root_.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002869
2870All source files within a module with the same package belong to the same
2871package.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002872<!-- jba: I can't make sense of the above sentence. -->
2873A module may define multiple packages.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002874
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002875An _instance_ of a package is any subset of files belonging
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002876to the same package.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002877<!-- jba: Are you saying that -->
2878<!-- if I have a package with files a, b and c, then there are 8 instances of -->
2879<!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the -->
2880<!-- purpose of that definition? -->
2881It is interpreted as the concatenation of these files.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002882
2883An implementation may impose conventions on the layout of package files
2884to determine which files of a package belongs to an instance.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002885For example, an instance may be defined as the subset of package files
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002886belonging to a directory and all its ancestors.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002887<!-- jba: OK, that helps a little, but I still don't see what the purpose is. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002888
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002889
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002890### Import declarations
2891
2892An import declaration states that the source file containing the declaration
2893depends on definitions of the _imported_ package (§Program initialization and
2894execution) and enables access to exported identifiers of that package.
2895The import names an identifier (PackageName) to be used for access and an
2896ImportPath that specifies the package to be imported.
2897
2898```
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002899ImportDecl = "import" ( ImportSpec | "(" { ImportSpec "," } ")" ) .
Marcel van Lohuizenfbab65d2019-08-13 16:51:15 +02002900ImportSpec = [ PackageName ] ImportPath .
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002901ImportLocation = { unicode_value } .
2902ImportPath = `"` ImportLocation [ ":" identifier ] `"` .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002903```
2904
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002905The PackageName is used in qualified identifiers to access
2906exported identifiers of the package within the importing source file.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002907It is declared in the file block.
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002908It defaults to the identifier specified in the package clause of the imported
2909package, which must match either the last path component of ImportLocation
2910or the identifier following it.
2911
2912<!--
2913Note: this deviates from the Go spec where there is no such restriction.
2914This restriction has the benefit of being to determine the identifiers
2915for packages from within the file itself. But for CUE it is has another benefit:
2916when using package hiearchies, one is more likely to want to include multiple
2917packages within the same directory structure. This mechanism allows
2918disambiguation in these cases.
2919-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002920
2921The interpretation of the ImportPath is implementation-dependent but it is
2922typically either the path of a builtin package or a fully qualifying location
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002923of a package within a source code repository.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002924
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002925An ImportLocation must be a non-empty strings using only characters belonging
2926Unicode's L, M, N, P, and S general categories
2927(the Graphic characters without spaces)
2928and may not include the characters !"#$%&'()*,:;<=>?[\]^`{|}
2929or the Unicode replacement character U+FFFD.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002930
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002931Assume we have package containing the package clause "package math",
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002932which exports function Sin at the path identified by "lib/math".
2933This table illustrates how Sin is accessed in files
2934that import the package after the various types of import declaration.
2935
2936```
2937Import declaration Local name of Sin
2938
2939import "lib/math" math.Sin
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002940import "lib/math:math" math.Sin
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002941import m "lib/math" m.Sin
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002942```
2943
2944An import declaration declares a dependency relation between the importing and
2945imported package. It is illegal for a package to import itself, directly or
2946indirectly, or to directly import a package without referring to any of its
2947exported identifiers.
2948
2949
2950### An example package
2951
2952TODO