blob: 22fc29c7772516243c22abfecc7819df54a1432d [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 Lohuizenc7791ac2019-10-07 11:29:28 +0200180An identifier is a sequence of one or more letters (which includes `_`) and digits.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100181It may not be `_`.
182The first character in an identifier must be a letter.
183
184<!--
185TODO: allow identifiers as defined in Unicode UAX #31
186(https://unicode.org/reports/tr31/).
187
188Identifiers are normalized using the NFC normal form.
189-->
190
191```
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +0200192identifier = letter { letter | unicode_digit } .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100193```
194
195```
196a
197_x9
198fieldName
199αβ
200```
201
202<!-- TODO: Allow Unicode identifiers TR 32 http://unicode.org/reports/tr31/ -->
203
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500204Some identifiers are [predeclared](#predeclared-identifiers).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100205
206
207### Keywords
208
209CUE has a limited set of keywords.
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200210In addition, CUE reserves all identifiers starting with `__`(double underscores)
211as keywords.
212These are typically targets of pre-declared identifiers.
213
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100214All keywords may be used as labels (field names).
215They cannot, however, be used as identifiers to refer to the same name.
216
217
218#### Values
219
220The following keywords are values.
221
222```
223null true false
224```
225
226These can never be used to refer to a field of the same name.
227This restriction is to ensure compatibility with JSON configuration files.
228
229
230#### Preamble
231
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100232The following keywords are used at the preamble of a CUE file.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100233After the preamble, they may be used as identifiers to refer to namesake fields.
234
235```
236package import
237```
238
239
240#### Comprehension clauses
241
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100242The following keywords are used in comprehensions.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100243
244```
245for in if let
246```
247
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100248The keywords `for`, `if` and `let` cannot be used as identifiers to
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200249refer to fields.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100250
251<!--
252TODO:
253 reduce [to]
254 order [by]
255-->
256
257
258#### Arithmetic
259
260The following pseudo keywords can be used as operators in expressions.
261
262```
263div mod quo rem
264```
265
266These may be used as identifiers to refer to fields in all other contexts.
267
268
269### Operators and punctuation
270
271The following character sequences represent operators and punctuation:
272
273```
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200274+ div && == < = ( )
275- mod || != > :: { }
276* quo & =~ <= : [ ]
277/ rem | !~ >= . ... ,
278 _|_ !
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100279```
Marcel van Lohuizen40178752019-08-25 19:17:56 +0200280<!--
281Free tokens: # ; ~ $ ^
282
283// To be used:
284 @ at: associative lists.
285
286// Idea: use # instead of @ for attributes and allow then at declaration level.
287// This will open up the possibility of defining #! at the start of a file
288// without requiring special syntax. Although probably not quite.
289 -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100290
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +0100291
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100292### Integer literals
293
294An integer literal is a sequence of digits representing an integer value.
Marcel van Lohuizenb2703c62019-09-29 18:20:01 +0200295An optional prefix sets a non-decimal base: 0o for octal,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002960x or 0X for hexadecimal, and 0b for binary.
297In hexadecimal literals, letters a-f and A-F represent values 10 through 15.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500298All integers allow interstitial underscores "_";
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100299these have no meaning and are solely for readability.
300
301Decimal integers may have a SI or IEC multiplier.
302Multipliers can be used with fractional numbers.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500303When multiplying a fraction by a multiplier, the result is truncated
304towards zero if it is not an integer.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100305
306```
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200307int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit .
308decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } .
309decimals = decimal_digit { [ "_" ] decimal_digit } .
310si_it = decimals [ "." decimals ] multiplier |
311 "." decimals multiplier .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100312binary_lit = "0b" binary_digit { binary_digit } .
313hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } .
Marcel van Lohuizenb2703c62019-09-29 18:20:01 +0200314octal_lit = "0o" octal_digit { [ "_" ] octal_digit } .
Marcel van Lohuizen6eefcd02019-10-04 13:32:06 +0200315multiplier = ( "K" | "M" | "G" | "T" | "P" ) [ "i" ]
Marcel van Lohuizenafb4db62019-05-31 00:23:24 +0200316
317float_lit = decimals "." [ decimals ] [ exponent ] |
318 decimals exponent |
319 "." decimals [ exponent ].
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +0200320exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100321```
Marcel van Lohuizen6eefcd02019-10-04 13:32:06 +0200322<!--
323TODO: consider allowing Exo (and up), if not followed by a sign
324or number. Alternatively one could only allow Ei, Yi, and Zi.
325-->
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100326
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100327```
32842
3291.5Gi
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100330170_141_183_460_469_231_731_687_303_715_884_105_727
Marcel van Lohuizenfc6303c2019-02-07 17:49:04 +01003310xBad_Face
3320o755
3330b0101_0001
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100334```
335
336### Decimal floating-point literals
337
338A decimal floating-point literal is a representation of
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500339a decimal floating-point value (a _float_).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100340It has an integer part, a decimal point, a fractional part, and an
341exponent part.
342The integer and fractional part comprise decimal digits; the
343exponent part is an `e` or `E` followed by an optionally signed decimal exponent.
344One of the integer part or the fractional part may be elided; one of the decimal
345point or the exponent may be elided.
346
347```
348decimal_lit = decimals "." [ decimals ] [ exponent ] |
349 decimals exponent |
350 "." decimals [ exponent ] .
351exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
352```
353
354```
3550.
35672.40
357072.40 // == 72.40
3582.71828
3591.e+0
3606.67428e-11
3611E6
362.25
363.12345E+5
364```
365
366
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100367### String and byte sequence literals
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100368
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100369A string literal represents a string constant obtained from concatenating a
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100370sequence of characters.
371Byte sequences are a sequence of bytes.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100372
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100373String and byte sequence literals are character sequences between,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100374respectively, double and single quotes, as in `"bar"` and `'bar'`.
375Within the quotes, any character may appear except newline and,
376respectively, unescaped double or single quote.
377String literals may only be valid UTF-8.
378Byte sequences may contain any sequence of bytes.
379
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400380Several escape sequences allow arbitrary values to be encoded as ASCII text.
381An escape sequence starts with an _escape delimiter_, which is `\` by default.
382The escape delimiter may be altered to be `\` plus a fixed number of
383hash symbols `#`
384by padding the start and end of a string or byte sequence literal
385with this number of hash symbols.
386
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100387There are four ways to represent the integer value as a numeric constant: `\x`
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400388followed by exactly two hexadecimal digits; `\u` followed by exactly four
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100389hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a
390plain backslash `\` followed by exactly three octal digits.
391In each case the value of the literal is the value represented by the
392digits in the corresponding base.
393Hexadecimal and octal escapes are only allowed within byte sequences
394(single quotes).
395
396Although these representations all result in an integer, they have different
397valid ranges.
398Octal escapes must represent a value between 0 and 255 inclusive.
399Hexadecimal escapes satisfy this condition by construction.
400The escapes `\u` and `\U` represent Unicode code points so within them
401some values are illegal, in particular those above `0x10FFFF`.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400402Surrogate halves are allowed,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100403but are translated into their non-surrogate equivalent internally.
404
405The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes
406represent individual bytes of the resulting string; all other escapes represent
407the (possibly multi-byte) UTF-8 encoding of individual characters.
408Thus inside a string literal `\377` and `\xFF` represent a single byte of
409value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent
410the two bytes `0xc3 0xbf` of the UTF-8
411encoding of character `U+00FF`.
412
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100413```
414\a U+0007 alert or bell
415\b U+0008 backspace
416\f U+000C form feed
417\n U+000A line feed or newline
418\r U+000D carriage return
419\t U+0009 horizontal tab
420\v U+000b vertical tab
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100421\/ U+002f slash (solidus)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100422\\ U+005c backslash
423\' U+0027 single quote (valid escape only within single quoted literals)
424\" U+0022 double quote (valid escape only within double quoted literals)
425```
426
427The escape `\(` is used as an escape for string interpolation.
428A `\(` must be followed by a valid CUE Expression, followed by a `)`.
429
430All other sequences starting with a backslash are illegal inside literals.
431
432```
Marcel van Lohuizen39df6c92019-10-25 20:16:26 +0200433escaped_char = `\` { `#` } ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "/" | `\` | "'" | `"` ) .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100434byte_value = octal_byte_value | hex_byte_value .
435octal_byte_value = `\` octal_digit octal_digit octal_digit .
436hex_byte_value = `\` "x" hex_digit hex_digit .
437little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
438big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
439 hex_digit hex_digit hex_digit hex_digit .
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400440unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
441interpolation = "\(" Expression ")" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100442
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400443string_lit = simple_string_lit |
444 multiline_string_lit |
445 simple_bytes_lit |
446 multiline_bytes_lit |
447 `#` string_lit `#` .
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100448
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400449simple_string_lit = `"` { unicode_value | interpolation } `"` .
450simple_bytes_lit = `"` { unicode_value | interpolation | byte_value } `"` .
451multiline_string_lit = `"""` newline
452 { unicode_value | interpolation | newline }
453 newline `"""` .
454multiline_bytes_lit = "'''" newline
455 { unicode_value | interpolation | byte_value | newline }
456 newline "'''" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100457```
458
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400459Carriage return characters (`\r`) inside string literals are discarded from
Marcel van Lohuizendb9d25a2019-02-21 23:54:43 +0100460the string value.
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400461
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100462```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100463'a\000\xab'
464'\007'
465'\377'
466'\xa' // illegal: too few hexadecimal digits
467"\n"
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100468"\""
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100469'Hello, world!\n'
470"Hello, \( name )!"
471"日本語"
472"\u65e5本\U00008a9e"
473"\xff\u00FF"
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +0100474"\uD800" // illegal: surrogate half (TODO: probably should allow)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100475"\U00110000" // illegal: invalid Unicode code point
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400476
477#"This is not an \(interpolation)"#
478#"This is an \#(interpolation)"#
479#"The sequence "\U0001F604" renders as \#U0001F604."#
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100480```
481
482These examples all represent the same string:
483
484```
485"日本語" // UTF-8 input text
486'日本語' // UTF-8 input text as byte sequence
487`日本語` // UTF-8 input text as a raw literal
488"\u65e5\u672c\u8a9e" // the explicit Unicode code points
489"\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points
490"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes
491```
492
493If the source code represents a character as two code points, such as a
494combining form involving an accent and a letter, the result will appear as two
495code points if placed in a string literal.
496
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400497Strings and byte sequences have a multiline equivalent.
498Multiline strings are like their single-line equivalent,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100499but allow newline characters.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100500
Marcel van Lohuizen369e4232019-02-15 10:59:29 +0400501Multiline strings and byte sequences respectively start with
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100502a triple double quote (`"""`) or triple single quote (`'''`),
503immediately followed by a newline, which is discarded from the string contents.
504The string is closed by a matching triple quote, which must be by itself
505on a newline, preceded by optional whitespace.
506The whitespace before a closing triple quote must appear before any non-empty
507line after the opening quote and will be removed from each of these
508lines in the string literal.
509A closing triple quote may not appear in the string.
510To include it is suffices to escape one of the quotes.
511
512```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100513"""
514 lily:
515 out of the water
516 out of itself
517
518 bass
519 picking bugs
520 off the moon
521 — Nick Virgilio, Selected Haiku, 1988
522 """
523```
524
525This represents the same string as:
526
527```
528"lily:\nout of the water\nout of itself\n\n" +
529"bass\npicking bugs\noff the moon\n" +
530" — Nick Virgilio, Selected Haiku, 1988"
531```
532
533<!-- TODO: other values
534
535Support for other values:
536- Duration literals
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +0100537- regular expessions: `re("[a-z]")`
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100538-->
539
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500540
541## Values
542
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100543In addition to simple values like `"hello"` and `42.0`, CUE has _structs_.
544A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`.
545Structs are CUE's only way of building up complex values;
546lists, which we will see later,
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500547are defined in terms of structs.
548
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100549All possible values are ordered in a lattice,
550a partial order where every two elements have a single greatest lower bound.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500551A value `a` is an _instance_ of a value `b`,
552denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`,
553that is if `a` orders before `b` in the partial order
554(`⊑` is _not_ a CUE operator).
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100555We also say that `b` _subsumes_ `a` in this case.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500556In graphical terms, `b` is "above" `a` in the lattice.
557
558At the top of the lattice is the single ancestor of all values, called
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100559_top_, denoted `_` in CUE.
560Every value is an instance of top.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500561
562At the bottom of the lattice is the value called _bottom_, denoted `_|_`.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100563A bottom value usually indicates an error.
564Bottom is an instance of every value.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500565
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100566An _atom_ is any value whose only instances are itself and bottom.
567Examples of atoms are `42.0`, `"hello"`, `true`, `null`.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500568
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100569A value is _concrete_ if it is either an atom, or a struct all of whose
570field values are themselves concrete, recursively.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500571
572CUE's values also include what we normally think of as types, like `string` and
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100573`float`.
574But CUE does not distinguish between types and values; only the
575relationship of values in the lattice is important.
576Each CUE "type" subsumes the concrete values that one would normally think
577of as part of that type.
578For example, "hello" is an instance of `string`, and `42.0` is an instance of
579`float`.
580In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`.
581We informally call these CUE's "basic types".
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100582
583
584```
585false ⊑ bool
586true ⊑ bool
587true ⊑ true
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01005885.0 ⊑ float
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100589bool ⊑ _
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100590_|_ ⊑ _
591_|_ ⊑ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100592
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100593_ ⋢ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100594_ ⋢ bool
595int ⋢ bool
596bool ⋢ int
597false ⋢ true
598true ⋢ false
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100599float ⋢ 5.0
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01006005 ⋢ 6
601```
602
603
604### Unification
605
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500606The _unification_ of values `a` and `b`
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100607is defined as the greatest lower bound of `a` and `b`. (That is, the
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500608value `u` such that `u ⊑ a` and `u ⊑ b`,
609and for any other value `v` for which `v ⊑ a` and `v ⊑ b`
610it holds that `v ⊑ u`.)
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500611Since CUE values form a lattice, the unification of two CUE values is
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400612always unique.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100613
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500614These all follow from the definition of unification:
615- The unification of `a` with itself is always `a`.
616- The unification of values `a` and `b` where `a ⊑ b` is always `a`.
617- The unification of a value with bottom is always bottom.
618
619Unification in CUE is a [binary expression](#Operands), written `a & b`.
620It is commutative and associative.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100621As a consequence, order of evaluation is irrelevant, a property that is key
622to many of the constructs in the CUE language as well as the tooling layered
623on top of it.
624
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500625
626
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100627<!-- TODO: explicitly mention that disjunction is not a binary operation
628but a definition of a single value?-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100629
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100630
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100631### Disjunction
632
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500633The _disjunction_ of values `a` and `b`
634is defined as the least upper bound of `a` and `b`.
635(That is, the value `d` such that `a ⊑ d` and `b ⊑ d`,
636and for any other value `e` for which `a ⊑ e` and `b ⊑ e`,
637it holds that `d ⊑ e`.)
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100638This style of disjunctions is sometimes also referred to as sum types.
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500639Since CUE values form a lattice, the disjunction of two CUE values is always unique.
640
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100641
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500642These all follow from the definition of disjunction:
643- The disjunction of `a` with itself is always `a`.
644- The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`.
645- The disjunction of a value `a` with bottom is always `a`.
646- The disjunction of two bottom values is bottom.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100647
Jonathan Amsterdama8d8a3c2019-02-03 07:53:55 -0500648Disjunction in CUE is a [binary expression](#Operands), written `a | b`.
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100649It is commutative, associative, and idempotent.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100650
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100651The unification of a disjunction with another value is equal to the disjunction
652composed of the unification of this value with all of the original elements
653of the disjunction.
654In other words, unification distributes over disjunction.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100655
656```
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100657(a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100658```
659
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100660```
661Expression Result
662({a:1} | {b:2}) & {c:3} {a:1, c:3} | {b:2, c:3}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100663(int | string) & "foo" "foo"
664("a" | "b") & "c" _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100665```
666
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100667A disjunction is _normalized_ if there is no element
668`a` for which there is an element `b` such that `a ⊑ b`.
669
670<!--
671Normalization is important, as we need to account for spurious elements
672For instance "tcp" | "tcp" should resolve to "tcp".
673
674Also consider
675
676 ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2},
677
678in this case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1} and thus
679this expression is logically equivalent to {a:1} and should therefore be
680considered to be unambiguous and resolve to {a:1} if a concrete value is needed.
681
682For instance, in
683
684 x: ({a:1} | {b:1}) & ({a:1} | {b:2}) // -> {a:1} | {a:1,b:1} | {a:1,b:2}
685 y: x.a // 1
686
687y should resolve to 1, and not an error.
688
689For comparison, in
690
691 x: ({a:1, b:1} | {b:2}) & {a:1} // -> {a:1,b:1} | {a:1,b:2}
692 y: x.a // _|_
693
694y should be an error as x is still ambiguous before the selector is applied,
695even though `a` resolves to 1 in all cases.
696-->
697
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500698
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100699#### Default values
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500700
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100701Any element of a disjunction can be marked as a default
Axel Wagner8529d772019-09-24 18:27:12 +0000702by prefixing it with an asterisk `*`.
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100703Intuitively, when an expression needs to be resolved for an operation other
704than unification or disjunctions,
705non-starred elements are dropped in favor of starred ones if the starred ones
706do not resolve to bottom.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500707
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100708More precisely, any value `v` may be associated with a default value `d`,
709denoted `(v, d)` (not CUE syntax),
710where `d` must be in instance of `v` (`d ⊑ v`).
711The rules for unifying and disjoining such values are as follows:
712
713```
714U1: (v1, d1) & v2 => (v1&v2, d1&v2)
715U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2)
716
717D1: (v1, d1) | v2 => (v1|v2, d1)
718D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2)
719```
720
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100721Default values may be introduced within disjunctions
722by _marking_ terms of a disjunction with an asterisk `*`
723([a unary expression](#Operators)).
724The default value of a disjunction with marked terms is the disjunction
725of those marked terms, applying the following rules for marks:
726
727```
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200728M1: *v => (v, v)
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100729M2: *(v1, d1) => (v1, d1)
730```
731
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400732In general, any operation `f` in CUE involving default values proceeds along the
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200733following lines
734```
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400735O1: f((v1, d1), ..., (vn, dn)) => (f(v1, ..., vn), f(d1, ..., dn))
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200736```
737where, with the exception of disjunction, a value `v` without a default
738value is promoted to `(v, v)`.
739
740
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100741```
742Expression Value-default pair Rules applied
743*"tcp" | "udp" ("tcp"|"udp", "tcp") M1, D1
744string | *"foo" (string, "foo") M1, D1
745
746*1 | 2 | 3 (1|2|3, 1) M1, D1
747
748(*1|2|3) | (1|*2|3) (1|2|3, 1|2) M1, D1, D2
749(*1|2|3) | *(1|*2|3) (1|2|3, 1|2) M1, D1, M2, D2
750(*1|2|3) | (1|*2|3)&2 (1|2|3, 1|2) M1, D1, U1, D2
751
752(*1|2) & (1|*2) (1|2, _|_) M1, D1, U2
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +0200753
754(*1|2) + (1|*2) ((1|2)+(1|2), 3) M1, D1, O1
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100755```
756
757The rules of subsumption for defaults can be derived from the above definitions
758and are as follows.
759
760```
761(v2, d2) ⊑ (v1, d1) if v2 ⊑ v1 and d2 ⊑ d1
762(v1, d1) ⊑ v if v1 ⊑ v
763v ⊑ (v1, d1) if v ⊑ d1
764```
765
766<!--
767For the second rule, note that by definition d1 ⊑ v1, so d1 ⊑ v1 ⊑ v.
768
769The last one is so restrictive as v could still be made more specific by
770associating it with a default that is not subsumed by d1.
771
772Proof:
773 by definition for any d ⊑ v, it holds that (v, d) ⊑ v,
774 where the most general value is (v, v).
775 Given the subsumption rule for (v2, d2) ⊑ (v1, d1),
776 from (v, v) ⊑ v ⊑ (v1, d1) it follows that v ⊑ d1
777 exactly defines the boundary of this subsumption.
778-->
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100779
780<!--
781(non-normalized entries could also be implicitly marked, allowing writing
782int | 1, instead of int | *1, but that can be done in a backwards
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100783compatible way later if really desirable, as long as we require that
784disjunction literals be normalized).
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500785-->
786
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100787
788```
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100789Expression Resolves to
790"tcp" | "udp" "tcp" | "udp"
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100791*"tcp" | "udp" "tcp"
792float | *1 1
793*string | 1.0 string
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100794
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100795(*1|2|3) | (1|*2|3) 1|2
796(*1|2|3) & (1|*2|3) 1|2|3 // default is _|_
797
798(* >=5 | int) & (* <=5 | int) 5
799
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100800(*"tcp"|"udp") & ("udp"|*"tcp") "tcp"
801(*"tcp"|"udp") & ("udp"|"tcp") "tcp"
802(*"tcp"|"udp") & "tcp" "tcp"
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100803(*"tcp"|"udp") & (*"udp"|"tcp") "tcp" | "udp" // default is _|_
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100804
805(*true | false) & bool true
806(*true | false) & (true | false) true
807
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100808{a: 1} | {b: 1} {a: 1} | {b: 1}
Marcel van Lohuizen69139d62019-01-24 13:46:51 +0100809{a: 1} | *{b: 1} {b:1}
Marcel van Lohuizen6e5d9932019-03-14 15:52:48 +0100810*{a: 1} | *{b: 1} {a: 1} | {b: 1}
811({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} by normalization
812({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 +0100813```
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500814
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100815
816### Bottom and errors
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100817
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100818Any evaluation error in CUE results in a bottom value, respresented by
Axel Wagner8529d772019-09-24 18:27:12 +0000819the token `_|_`.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100820Bottom is an instance of every other value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100821Any evaluation error is represented as bottom.
822
823Implementations may associate error strings with different instances of bottom;
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500824logically they all remain the same value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100825
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100826
827### Top
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100828
Axel Wagner8529d772019-09-24 18:27:12 +0000829Top is represented by the underscore character `_`, lexically an identifier.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100830Unifying any value `v` with top results `v` itself.
831
832```
833Expr Result
834_ & 5 5
835_ & _ _
836_ & _|_ _|_
837_ | _|_ _
838```
839
840
841### Null
842
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100843The _null value_ is represented with the keyword `null`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100844It has only one parent, top, and one child, bottom.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100845It is unordered with respect to any other value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100846
847```
848null_lit = "null"
849```
850
851```
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +0100852null & 8 _|_
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100853null & _ null
854null & _|_ _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100855```
856
857
858### Boolean values
859
860A _boolean type_ represents the set of Boolean truth values denoted by
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100861the keywords `true` and `false`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100862The predeclared boolean type is `bool`; it is a defined type and a separate
863element in the lattice.
864
865```
866boolean_lit = "true" | "false"
867```
868
869```
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100870bool & true true
871true & true true
872true & false _|_
873bool & (false|true) false | true
874bool & (true|false) true | false
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100875```
876
877
878### Numeric values
879
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500880The _integer type_ represents the set of all integral numbers.
881The _decimal floating-point type_ represents the set of all decimal floating-point
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100882numbers.
883They are two distinct types.
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +0200884Both are instances instances of a generic `number` type.
885
886<!--
887 number
888 / \
889 int float
890-->
891
892The predeclared number, integer, decimal floating-point types are
893`number`, `int` and `float`; they are defined types.
894<!--
895TODO: should we drop float? It is somewhat preciser and probably a good idea
896to have it in the programmatic API, but it may be confusing to have to deal
897with it in the language.
898-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100899
900A decimal floating-point literal always has type `float`;
901it is not an instance of `int` even if it is an integral number.
902
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400903Integer literals are always of type `int` and don't match type `float`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100904
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100905Numeric literals are exact values of arbitrary precision.
906If the operation permits it, numbers should be kept in arbitrary precision.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100907
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100908Implementation restriction: although numeric values have arbitrary precision
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100909in the language, implementations may implement them using an internal
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100910representation with limited precision.
911That said, every implementation must:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100912
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500913- Represent integer values with at least 256 bits.
914- Represent floating-point values, with a mantissa of at least 256 bits and
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100915a signed binary exponent of at least 16 bits.
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500916- Give an error if unable to represent an integer value precisely.
917- Give an error if unable to represent a floating-point value due to overflow.
918- Round to the nearest representable value if unable to represent
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100919a floating-point value due to limits on precision.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100920These requirements apply to the result of any expression except for builtin
921functions for which an unusual loss of precision must be explicitly documented.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100922
923
924### Strings
925
Marcel van Lohuizen4108f802019-08-13 18:30:25 +0200926The _string type_ represents the set of UTF-8 strings,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100927not allowing surrogates.
928The predeclared string type is `string`; it is a defined type.
929
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100930The length of a string `s` (its size in bytes) can be discovered using
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400931the built-in function `len`.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100932
Marcel van Lohuizen4108f802019-08-13 18:30:25 +0200933
934### Bytes
935
936The _bytes type_ represents the set of byte sequences.
937A byte sequence value is a (possibly empty) sequence of bytes.
938The number of bytes is called the length of the byte sequence
939and is never negative.
940The predeclared byte sequence type is `bytes`; it is a defined type.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100941
942
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +0100943### Bounds
944
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400945A _bound_, syntactically a [unary expression](#Operands), defines
Marcel van Lohuizen62b87272019-02-01 10:07:49 +0100946an infinite disjunction of concrete values than can be represented
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +0100947as a single comparison.
948
949For any [comparison operator](#Comparison-operators) `op` except `==`,
950`op a` is the disjunction of every `x` such that `x op a`.
951
952```
9532 & >=2 & <=5 // 2, where 2 is either an int or float.
9542.5 & >=1 & <=5 // 2.5
9552 & >=1.0 & <3.0 // 2.0
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01009562 & >1 & <3.0 // 2.0
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01009572.5 & int & >1 & <5 // _|_
9582.5 & float & >1 & <5 // 2.5
959int & 2 & >1.0 & <3.0 // _|_
9602.5 & >=(int & 1) & <5 // _|_
961>=0 & <=7 & >=3 & <=10 // >=3 & <=7
962!=null & 1 // 1
963>=5 & <=5 // 5
964```
965
966
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100967### Structs
968
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500969A _struct_ is a set of elements called _fields_, each of
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100970which has a name, called a _label_, and value.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100971
972We say a label is defined for a struct if the struct has a field with the
973corresponding label.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +0200974The value for a label `f` of struct `a` is denoted `a.f`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100975A struct `a` is an instance of `b`, or `a ⊑ b`, if for any label `f`
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100976defined for `b`, label `f` is also defined for `a` and `a.f ⊑ b.f`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +0100977Note that if `a` is an instance of `b` it may have fields with labels that
978are not defined for `b`.
979
Jonathan Amsterdame4790382019-01-20 10:29:29 -0500980The (unique) struct with no fields, written `{}`, has every struct as an
981instance. It can be considered the type of all structs.
982
Jonathan Amsterdam061bde12019-09-03 08:28:10 -0400983```
984{a: 1} ⊑ {}
985{a: 1, b: 1} ⊑ {a: 1}
986{a: 1} ⊑ {a: int}
987{a: 1, b: 1} ⊑ {a: int, b: float}
988
989{} ⋢ {a: 1}
990{a: 2} ⋢ {a: 1}
991{a: 1} ⋢ {b: 1}
992```
993
Marcel van Lohuizen62658a82019-06-16 12:18:47 +0200994A field may be required or optional.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +0100995The successful unification of structs `a` and `b` is a new struct `c` which
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +0100996has all fields of both `a` and `b`, where
997the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`,
998or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +0200999If a field `f` is in both `a` and `b`, `c.f` is optional only if both
1000`a.f` and `b.f` are optional.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001001Any [references](#References) to `a` or `b`
1002in their respective field values need to be replaced with references to `c`.
Marcel van Lohuizen3022ae92019-10-15 13:35:58 +02001003The result of a unification is bottom (`_|_`) if any of its non-optional
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001004fields evaluates to bottom, recursively.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02001005
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001006<!--NOTE: About bottom values for optional fields being okay.
1007
1008The proposition ¬P is a close cousin of P → ⊥ and is often used
1009as an approximation to avoid the issues of using not.
1010Bottom (⊥) is also frequently used to mean undefined. This makes sense.
1011Consider `{a?: 2} & {a?: 3}`.
1012Both structs say `a` is optional; in other words, it may be omitted.
1013So we can still get a valid result by omitting `a`, even in
1014case of a conflict.
1015
1016Granted, this definition may lead to confusing results, especially in
1017definitions, when tightening an optional field leads to unintentionally
1018discarding it.
1019It could be a role of vet checkers to identify such cases (and suggest users
1020to explicitly use `_|_` to discard a field, for instance).
1021-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001022
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001023Syntactically, a struct literal may contain multiple fields with
1024the same label, the result of which is a single field with the same properties
1025as defined as the unification of two fields resulting from unifying two structs.
1026
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001027These examples illustrate required fields only.
1028Examples with optional fields follow below.
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001029
1030```
1031Expression Result (without optional fields)
1032{a: int, a: 1} {a: 1}
1033{a: int} & {a: 1} {a: 1}
1034{a: >=1 & <=7} & {a: >=5 & <=9} {a: >=5 & <=7}
1035{a: >=1 & <=7, a: >=5 & <=9} {a: >=5 & <=7}
1036
1037{a: 1} & {b: 2} {a: 1, b: 2}
1038{a: 1, b: int} & {b: 2} {a: 1, b: 2}
1039
1040{a: 1} & {a: 2} _|_
1041```
1042
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001043Optional labels are defined in sets with an expression to select all
1044labels to which to apply a given constraint.
1045Syntactically, the label of an optional field set is an expression in square
1046brackets indicating the matching labels.
1047The value `string` matches all fields, while a concrete string matches a
1048single field.
1049As the latter case is common, a concrete label followed by
1050a question mark `?` may be used as a shorthand.
1051So `foo?: bar` is a shorthand for `["foo"]: bar`.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001052The question mark is not part of the field name.
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001053The token `...` may be used as the last declaration in a struct
1054and is a shorthand for `[_]: _`.
1055
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001056Concrete field labels may be an identifier or string, the latter of which may be
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001057interpolated.
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001058Fields with identifier labels can be referred to within the scope they are
1059defined, string labels cannot.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001060References within such interpolated strings are resolved within
1061the scope of the struct in which the label sequence is
1062defined and can reference concrete labels lexically preceding
1063the label within a label sequence.
1064<!-- We allow this so that rewriting a CUE file to collapse or expand
1065field sequences has no impact on semantics.
1066-->
1067
1068<!--TODO: first implementation round will not yet have expression labels
1069
1070An ExpressionLabel sets a collection of optional fields to a field value.
1071By default it defines this value for all possible string labels.
1072An optional expression limits this to the set of optional fields which
1073labels match the expression.
1074-->
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001075
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001076
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001077<!-- NOTE: if we allow ...Expr, as in list, it would mean something different. -->
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001078
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001079
1080<!-- NOTE:
1081A DefinitionDecl does not allow repeated labels. This is to avoid
1082any ambiguity or confusion about whether earlier path components
1083are to be interpreted as declarations or normal fields (they should
1084always be normal fields.)
1085-->
1086
1087<!--NOTE:
1088The syntax has been deliberately restricted to allow for the following
1089future extensions and relaxations:
1090 - Allow omitting a "?" in an expression label to indicate a concrete
1091 string value (but maybe we want to use () for that).
1092 - Make the "?" in expression label optional if expression labels
1093 are always optional.
1094 - Or allow eliding the "?" if the expression has no references and
1095 is obviously not concrete (such as `[string]`).
1096 - The expression of an expression label may also indicate a struct with
1097 integer or even number labels
1098 (beware of imprecise computation in the latter).
1099 e.g. `{ [int]: string }` is a map of integers to strings.
1100 - Allow for associative lists (`foo [@.field]: {field: string}`)
1101 - The `...` notation can be extended analogously to that of a ListList,
1102 by allowing it to follow with an expression for the remaining properties.
1103 In that case it is no longer a shorthand for `[string]: _`, but rather
1104 would define the value for any other value for which there is no field
1105 defined.
1106 Like the definition with List, this is somewhat odd, but it allows the
1107 encoding of JSON schema's and (non-structural) OpenAPI's
1108 additionalProperties and additionalItems.
1109-->
1110
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001111```
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02001112StructLit = "{" { Declaration "," } [ "..." ] "}" .
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001113Declaration = Field | Comprehension | AliasExpr .
1114Field = LabelSpec { LabelSpec } Expression .
1115LabelSpec = Label ( ":" | "::" ) .
1116Label = LabelName [ "?" ] | "[" AliasExpr "]".
1117LabelName = identifier | simple_string_lit .
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001118
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001119attribute = "@" identifier "(" attr_elems ")" .
1120attr_elems = attr_elem { "," attr_elem }
1121attr_elem = attr_string | attr_label | attr_nest .
1122attr_label = identifier "=" attr_string .
1123attr_nest = identifier "(" attr_elems ")" .
1124attr_string = { attr_char } | string_lit .
1125attr_char = /* an arbitrary Unicode code point except newline, ',', '"', `'`, '#', '=', '(', and ')' */ .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001126```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001127
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001128<!--
1129 TODO: Label = LabelName [ "?" ] | "[" AliasExpr "]" | "(" AliasExpr ")"
1130-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001131
1132```
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001133Expression Result (without optional fields)
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001134a: { foo?: string } {}
1135b: { foo: "bar" } { foo: "bar" }
1136c: { foo?: *"bar" | string } {}
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001137
1138d: a & b { foo: "bar" }
1139e: b & c { foo: "bar" }
1140f: a & c {}
1141g: a & { foo?: number } {}
1142h: b & { foo?: number } _|_
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001143i: c & { foo: string } { foo: "bar" }
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001144
1145intMap: [string]: int
1146intMap: {
1147 t1: 43
1148 t2: 2.4 // error: 2.4 is not an integer
1149}
1150
1151nameMap: [string]: {
1152 firstName: string
1153 nickName: *firstName | string
1154}
1155
1156nameMap: hank: { firstName: "Hank" }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001157```
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001158The optional field set defined by `nameMap` matches every field,
1159in this case just `hank`, and unifies the associated constraint
1160with the matched field, resulting in:
1161```
1162nameMap: hank: {
1163 firstName: "Hank"
1164 nickName: "Hank"
1165}
1166```
1167
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001168
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001169#### Closed structs
1170
1171By default, structs are open to adding fields.
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001172Instances of an open struct `p` may contain fields not defined in `p`.
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001173This is makes it easy to add fields, but can lead to bugs:
1174
1175```
1176S: {
1177 field1: string
1178}
1179
1180S1: S & { field2: "foo" }
1181
1182// S1 is { field1: string, field2: "foo" }
1183
1184
1185A: {
1186 field1: string
1187 field2: string
1188}
1189
1190A1: A & {
1191 feild1: "foo" // "field1" was accidentally misspelled
1192}
1193
1194// A1 is
1195// { field1: string, field2: string, feild1: "foo" }
1196// not the intended
1197// { field1: "foo", field2: string }
1198```
1199
Marcel van Lohuizen18637db2019-09-03 11:48:25 +02001200A _closed struct_ `c` is a struct whose instances may not have regular fields
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001201not defined in `c`.
Marcel van Lohuizen4245fb42019-09-09 11:22:12 +02001202Closing a struct is equivalent to adding an optional field with value `_|_`
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001203for all undefined fields.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001204
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001205Syntactically, closed structs can be explicitly created with the `close` builtin
1206or implicitly by [definitions](#Definitions).
1207
1208
1209```
1210A: close({
1211 field1: string
1212 field2: string
1213})
1214
1215A1: A & {
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001216 feild1: string
1217} // _|_ feild1 not defined for A
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001218
1219A2: A & {
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001220 for k,v in { feild1: string } {
1221 k: v
1222 }
1223} // _|_ feild1 not defined for A
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001224
1225C: close({
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001226 [_]: _
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001227})
1228
1229C2: C & {
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001230 for k,v in { thisIsFine: string } {
1231 "\(k)": v
1232 }
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001233}
1234
1235D: close({
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001236 // Values generated by comprehensions are treated as embeddings.
1237 for k,v in { x: string } {
1238 "\(k)": v
1239 }
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001240})
1241```
1242
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001243<!-- (jba) Somewhere it should be said that optional fields are only
1244 interesting inside closed structs. -->
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001245
1246#### Embedding
1247
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001248A struct may contain an _embedded value_, an operand used
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001249as a declaration, which must evaluate to a struct.
1250An embedded value of type struct is unified with the struct in which it is
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001251embedded, but disregarding the restrictions imposed by closed structs.
1252A struct resulting from such a unification is closed if either of the involved
1253structs were closed.
1254
Marcel van Lohuizena3c7bef2019-10-10 21:50:58 +02001255At the top level, an embedded value may be any type.
1256In this case, a CUE program will evaluate to the embedded value
1257and the CUE program may not have top-level regular or optional
1258fields (definitions and aliases are allowed).
1259
Marcel van Lohuizene53305e2019-09-13 10:10:31 +02001260Syntactically, embeddings may be any expression, except that `<`
1261is eagerly interpreted as a bind label.
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02001262
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001263```
1264S1: {
1265 a: 1
1266 b: 2
1267 {
1268 c: 3
1269 }
1270}
1271// S1 is { a: 1, b: 2, c: 3 }
1272
1273S2: close({
1274 a: 1
1275 b: 2
1276 {
1277 c: 3
1278 }
1279})
1280// same as close(S1)
1281
1282S3: {
1283 a: 1
1284 b: 2
1285 close({
1286 c: 3
1287 })
1288}
1289// same as S2
1290```
1291
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001292
1293#### Definitions
1294
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001295A field of a struct may be declared as a regular field (using `:`)
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001296or as a _definition_ (using `::`).
1297Definitions are not emitted as part of the model and are never required
1298to be concrete when emitting data.
Marcel van Lohuizen18637db2019-09-03 11:48:25 +02001299It is illegal to have a regular field and a definition with the same name
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001300within the same struct.
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001301Literal structs that are part of a definition's value are implicitly closed,
1302but may unify unrestricted with other structs within the field's declaration.
Marcel van Lohuizen5e8c3912019-09-03 15:46:26 +02001303This excludes literals structs in embeddings and aliases.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02001304
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001305<!--
1306This may be a more intuitive definition:
1307 Literal structs that are part of a definition's value are implicitly closed.
1308 Implicitly closed literal structs that are unified within
1309 a single field declaration are considered to be a single literal struct.
1310However, this would make unification non-commutative, unless one imposes an
1311ordering where literal structs are unified before unifying them with others.
1312Imposing such an ordering is complex and error prone.
1313-->
Marcel van Lohuizen5134dee2019-07-21 14:41:44 +02001314An ellipsis `...` in such literal structs keeps them open,
1315as it defines `_` for all labels.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02001316
Marcel van Lohuizen5e8c3912019-09-03 15:46:26 +02001317<!--
1318Excluding embeddings from recursive closing allows comprehensions to be
1319interpreted as embeddings without some exception. For instance,
1320 if x > 2 {
1321 foo: string
1322 }
1323should not cause any failure. It is also consistent with embeddings being
1324opened when included in a closed struct.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001325
Marcel van Lohuizen5e8c3912019-09-03 15:46:26 +02001326Finally, excluding embeddings from recursive closing allows for
1327a mechanism to not recursively close, without needing an additional language
1328construct, such as a triple colon or something else:
1329foo :: {
1330 {
1331 // not recursively closed
1332 }
1333 ... // include this to not close outer struct
1334}
1335
1336Including aliases from this exclusion, which are more a separate definition
1337than embedding seems sensible, and allows for an easy mechanism to avoid
1338closing, aside from embedding.
1339-->
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001340
1341```
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001342MyStruct :: {
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001343 sub field: string
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001344}
1345
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001346MyStruct :: {
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001347 sub enabled?: bool
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001348}
1349
1350myValue: MyStruct & {
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001351 sub feild: 2 // error, feild not defined in MyStruct
1352 sub enabled: true // okay
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001353}
1354
1355D :: {
1356 OneOf
1357
1358 c: int // adds this field.
1359}
1360
1361OneOf :: { a: int } | { b: int }
1362
1363
1364D1: D & { a: 12, c: 22 } // { a: 12, c: 22 }
1365D2: D & { a: 12, b: 33 } // _|_ // cannot define both `a` and `b`
1366```
1367
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001368
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001369<!---
1370JSON fields are usual camelCase. Clashes can be avoided by adopting the
1371convention that definitions be TitleCase. Unexported definitions are still
1372subject to clashes, but those are likely easier to resolve because they are
1373package internal.
1374--->
1375
1376
1377#### Field attributes
1378
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001379Fields may be associated with attributes.
1380Attributes define additional information about a field,
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001381such as a mapping to a protocol buffer <!-- TODO: add link --> tag or alternative
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001382name of the field when mapping to a different language.
1383
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001384<!-- TODO define attribute syntax here, before getting into semantics. -->
1385
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001386If a field has multiple attributes their identifiers must be unique.
1387Attributes accumulate when unifying two fields, removing duplicate entries.
1388It is an error for the resulting field to have two different attributes
1389with the same identifier.
1390
1391Attributes are not directly part of the data model, but may be
1392accessed through the API or other means of reflection.
1393The interpretation of the attribute value
1394(a comma-separated list of attribute elements) depends on the attribute.
1395Interpolations are not allowed in attribute strings.
1396
1397The recommended convention, however, is to interpret the first
1398`n` arguments as positional arguments,
1399where duplicate conflicting entries are an error,
1400and the remaining arguments as a combination of flags
1401(an identifier) and key value pairs, separated by a `=`.
1402
1403```
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001404myStruct1: {
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001405 field: string @go(Field)
1406 attr: int @xml(,attr) @go(Attr)
1407}
1408
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001409myStruct2: {
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001410 field: string @go(Field)
1411 attr: int @xml(a1,attr) @go(Attr)
1412}
1413
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001414Combined: myStruct1 & myStruct2
Marcel van Lohuizenb9b62d32019-03-14 23:50:15 +01001415// field: string @go(Field)
1416// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)
1417```
1418
Marcel van Lohuizenfa7e3ce2019-10-10 15:43:34 +02001419
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001420#### Aliases
1421
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001422Aliases name values that can be referred to
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001423within the [scope](#declarations-and-scopes) in which they are declared.
1424The name of an alias must be unique within its scope.
1425
1426```
1427AliasExpr = identifier "=" Expression | Expression .
1428```
1429
1430Aliases can appear in several positions:
1431
1432As a declaration in a struct (`X=expr`):
1433
1434- binds the value to an identifier without including it in the struct.
1435
1436In front of a Label (`X=label: value`):
1437
1438- binds the identifier to the same value as `label` would be bound
1439 to if it were a valid identifier.
1440- for optional fields (`foo?: bar` and `[foo]: bar`),
1441 the bound identifier is only visible within the field value (`value`).
1442
1443Inside a bracketed label (`[X=expr]: value`):
1444
1445- binds the identifier to the the concrete label that matches `expr`
1446 within the instances of the field value (`value`).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001447
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001448<!-- TODO: explain the difference between aliases and definitions.
1449 Now that you have definitions, are aliases really necessary?
1450 Consider removing.
1451-->
1452
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001453```
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001454// An alias declaration
1455Alias = 3
1456a: Alias // 3
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001457
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001458// A field alias
1459foo: X // 4
1460X="not an identifier": 4
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001461
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001462// A label alias
1463[Y=string]: { name: Y }
1464foo: { value: 1 } // outputs: foo: { name: "foo", value: 1 }
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001465```
1466
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001467<!-- TODO: also allow aliases as lists -->
1468
1469
Jonathan Amsterdam061bde12019-09-03 08:28:10 -04001470#### Shorthand notation for nested structs
1471
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001472A field whose value is a struct with a single field may be written as
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001473a colon-separated sequence of the two field names,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001474followed by a colon and the value of that single field.
1475
1476```
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001477job: myTask: replicas: 2
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001478```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001479expands to
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001480```
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001481job: {
1482 myTask: {
1483 replicas: 2
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001484 }
1485}
1486```
1487
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001488<!-- OPTIONAL FIELDS:
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001489
Marcel van Lohuizen08a0ef22019-03-28 09:12:19 +01001490The optional marker solves the issue of having to print large amounts of
1491boilerplate when dealing with large types with many optional or default
1492values (such as Kubernetes).
1493Writing such optional values in terms of *null | value is tedious,
1494unpleasant to read, and as it is not well defined what can be dropped or not,
1495all null values have to be emitted from the output, even if the user
1496doesn't override them.
1497Part of the issue is how null is defined. We could adopt a Typescript-like
1498approach of introducing "void" or "undefined" to mean "not defined and not
1499part of the output". But having all of null, undefined, and void can be
1500confusing. If these ever are introduced anyway, the ? operator could be
1501expressed along the lines of
1502 foo?: bar
1503being a shorthand for
1504 foo: void | bar
1505where void is the default if no other default is given.
1506
1507The current mechanical definition of "?" is straightforward, though, and
1508probably avoids the need for void, while solving a big issue.
1509
1510Caveats:
1511[1] this definition requires explicitly defined fields to be emitted, even
1512if they could be elided (for instance if the explicit value is the default
1513value defined an optional field). This is probably a good thing.
1514
1515[2] a default value may still need to be included in an output if it is not
1516the zero value for that field and it is not known if any outside system is
1517aware of defaults. For instance, which defaults are specified by the user
1518and which by the schema understood by the receiving system.
1519The use of "?" together with defaults should therefore be used carefully
1520in non-schema definitions.
1521Problematic cases should be easy to detect by a vet-like check, though.
1522
1523[3] It should be considered how this affects the trim command.
1524Should values implied by optional fields be allowed to be removed?
1525Probably not. This restriction is unlikely to limit the usefulness of trim,
1526though.
1527
1528[4] There should be an option to emit all concrete optional values.
1529```
1530-->
1531
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001532### Lists
1533
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001534A list literal defines a new value of type list.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001535A list may be open or closed.
1536An open list is indicated with a `...` at the end of an element list,
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01001537optionally followed by a value for the remaining elements.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001538
1539The length of a closed list is the number of elements it contains.
1540The length of an open list is the its number of elements as a lower bound
1541and an unlimited number of elements as its upper bound.
1542
1543```
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01001544ListLit = "[" [ ElementList [ "," [ "..." [ Expression ] ] ] "]" .
1545ElementList = Expression { "," Expression } .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001546```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001547
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001548Lists can be thought of as structs:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001549
1550```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001551List: *null | {
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001552 Elem: _
1553 Tail: List
1554}
1555```
1556
1557For closed lists, `Tail` is `null` for the last element, for open lists it is
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01001558`*null | List`, defaulting to the shortest variant.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001559For instance, the open list [ 1, 2, ... ] can be represented as:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001560```
1561open: List & { Elem: 1, Tail: { Elem: 2 } }
1562```
1563and the closed version of this list, [ 1, 2 ], as
1564```
1565closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } }
1566```
1567
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001568Using this representation, the subsumption rule for lists can
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001569be derived from those of structs.
1570Implementations are not required to implement lists as structs.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001571The `Elem` and `Tail` fields are not special and `len` will not work as
1572expected in these cases.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001573
1574
1575## Declarations and Scopes
1576
1577
1578### Blocks
1579
1580A _block_ is a possibly empty sequence of declarations.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001581The braces of a struct literal `{ ... }` form a block, but there are
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001582others as well:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001583
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001584- The _universe block_ encompasses all CUE source text.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001585- Each [package](#modules-instances-and-packages) has a _package block_
1586 containing all CUE source text in that package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001587- Each file has a _file block_ containing all CUE source text in that file.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001588- Each `for` and `let` clause in a [comprehension](#comprehensions)
1589 is considered to be its own implicit block.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001590
1591Blocks nest and influence [scoping].
1592
1593
1594### Declarations and scope
1595
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001596A _declaration_ may bind an identifier to a field, alias, or package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001597Every identifier in a program must be declared.
1598Other than for fields,
1599no identifier may be declared twice within the same block.
1600For fields an identifier may be declared more than once within the same block,
1601resulting in a field with a value that is the result of unifying the values
1602of all fields with the same identifier.
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001603String labels do not bind an identifier to the respective field.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001604
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001605The _scope_ of a declared identifier is the extent of source text in which the
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001606identifier denotes the specified field, alias, or package.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001607
1608CUE is lexically scoped using blocks:
1609
Jonathan Amsterdame4790382019-01-20 10:29:29 -050016101. The scope of a [predeclared identifier](#predeclared-identifiers) is the universe block.
Marcel van Lohuizen21f6c442019-09-26 14:55:23 +020016111. The scope of an identifier denoting a field
1612 declared at top level (outside any struct literal) is the package block.
16131. The scope of an identifier denoting an alias
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001614 declared at top level (outside any struct literal) is the file block.
16151. The scope of the package name of an imported package is the file block of the
1616 file containing the import declaration.
16171. The scope of a field or alias identifier declared inside a struct literal
1618 is the innermost containing block.
1619
1620An identifier declared in a block may be redeclared in an inner block.
1621While the identifier of the inner declaration is in scope, it denotes the entity
1622declared by the inner declaration.
1623
1624The package clause is not a declaration;
Jonathan Amsterdame4790382019-01-20 10:29:29 -05001625the package name does not appear in any scope.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001626Its purpose is to identify the files belonging to the same package
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01001627and to specify the default name for import declarations.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001628
1629
1630### Predeclared identifiers
1631
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001632CUE predefines a set of types and builtin functions.
1633For each of these there is a corresponding keyword which is the name
1634of the predefined identifier, prefixed with `__`.
1635
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001636```
1637Functions
1638len required close open
1639
1640Types
1641null The null type and value
1642bool All boolean values
1643int All integral numbers
1644float All decimal floating-point numbers
1645string Any valid UTF-8 sequence
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02001646bytes Any valid byte sequence
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001647
1648Derived Value
1649number int | float
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01001650uint >=0
1651uint8 >=0 & <=255
1652int8 >=-128 & <=127
1653uint16 >=0 & <=65536
1654int16 >=-32_768 & <=32_767
1655rune >=0 & <=0x10FFFF
1656uint32 >=0 & <=4_294_967_296
1657int32 >=-2_147_483_648 & <=2_147_483_647
1658uint64 >=0 & <=18_446_744_073_709_551_615
1659int64 >=-9_223_372_036_854_775_808 & <=9_223_372_036_854_775_807
1660uint128 >=0 & <=340_282_366_920_938_463_463_374_607_431_768_211_455
1661int128 >=-170_141_183_460_469_231_731_687_303_715_884_105_728 &
1662 <=170_141_183_460_469_231_731_687_303_715_884_105_727
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02001663float32 >=-3.40282346638528859811704183484516925440e+38 &
1664 <=3.40282346638528859811704183484516925440e+38
1665float64 >=-1.797693134862315708145274237317043567981e+308 &
1666 <=1.797693134862315708145274237317043567981e+308
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001667```
1668
1669
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001670### Exported identifiers
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001671
1672An identifier of a package may be exported to permit access to it
1673from another package.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001674An identifier is exported if
1675the first character of the identifier's name is a Unicode upper case letter
1676(Unicode class "Lu"); and
1677the identifier is declared in the file block.
1678All other top-level identifiers used for fields not exported.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001679
1680In addition, any definition declared anywhere within a package of which
1681the first character of the identifier's name is a Unicode upper case letter
1682(Unicode class "Lu") is visible outside this package.
1683Any other defintion is not visible outside the package and resides
1684in a separate namespace than namesake identifiers of other packages.
1685This is in contrast to ordinary field declarations that do not begin with
1686an upper-case letter, which are visible outside the package.
1687
1688```
1689package mypackage
1690
1691foo: string // not visible outside mypackage
1692
Marcel van Lohuizen21f6c442019-09-26 14:55:23 +02001693Foo :: { // visible outside mypackage
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001694 a: 1 // visible outside mypackage
1695 B: 2 // visible outside mypackage
1696
1697 C :: { // visible outside mypackage
1698 d: 4 // visible outside mypackage
1699 }
1700 e :: foo // not visible outside mypackage
1701}
1702```
1703
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001704
1705### Uniqueness of identifiers
1706
1707Given a set of identifiers, an identifier is called unique if it is different
1708from every other in the set, after applying normalization following
1709Unicode Annex #31.
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001710Two identifiers are different if they are spelled differently
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001711or if they appear in different packages and are not exported.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001712Otherwise, they are the same.
1713
1714
1715### Field declarations
1716
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001717A field associates the value of an expression to a label within a struct.
1718If this label is an identifier, it binds the field to that identifier,
1719so the field's value can be referenced by writing the identifier.
1720String labels are not bound to fields.
1721```
1722a: {
1723 b: 2
1724 "s": 3
1725
1726 c: b // 2
1727 d: s // _|_ unresolved identifier "s"
1728 e: a.s // 3
1729}
1730```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001731
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001732If an expression may result in a value associated with a default value
1733as described in [default values](#default-values), the field binds to this
1734value-default pair.
1735
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001736
Marcel van Lohuizenbcf832f2019-04-03 22:50:44 +02001737<!-- TODO: disallow creating identifiers starting with __
1738...and reserve them for builtin values.
1739
1740The issue is with code generation. As no guarantee can be given that
1741a predeclared identifier is not overridden in one of the enclosing scopes,
1742code will have to handle detecting such cases and renaming them.
1743An alternative is to have the predeclared identifiers be aliases for namesake
1744equivalents starting with a double underscore (e.g. string -> __string),
1745allowing generated code (normal code would keep using `string`) to refer
1746to these directly.
1747-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001748
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001749
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001750### Alias declarations
1751
1752An alias declaration binds an identifier to the given expression.
1753
1754Within the scope of the identifier, it serves as an _alias_ for that
1755expression.
Marcel van Lohuizen40178752019-08-25 19:17:56 +02001756The expression is evaluated in the scope it was declared.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001757
1758
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001759## Expressions
1760
1761An expression specifies the computation of a value by applying operators and
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001762built-in functions to operands.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001763
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001764Expressions that require concrete values are called _incomplete_ if any of
1765their operands are not concrete, but define a value that would be legal for
1766that expression.
1767Incomplete expressions may be left unevaluated until a concrete value is
1768requested at the application level.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001769
1770### Operands
1771
1772Operands denote the elementary values in an expression.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01001773An operand may be a literal, a (possibly qualified) identifier denoting
1774field, alias, or a parenthesized expression.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001775
1776```
1777Operand = Literal | OperandName | ListComprehension | "(" Expression ")" .
1778Literal = BasicLit | ListLit | StructLit .
1779BasicLit = int_lit | float_lit | string_lit |
1780 null_lit | bool_lit | bottom_lit | top_lit .
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001781OperandName = identifier | QualifiedIdent .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001782```
1783
1784### Qualified identifiers
1785
1786A qualified identifier is an identifier qualified with a package name prefix.
1787
1788```
1789QualifiedIdent = PackageName "." identifier .
1790```
1791
1792A qualified identifier accesses an identifier in a different package,
1793which must be [imported].
1794The identifier must be declared in the [package block] of that package.
1795
1796```
1797math.Sin // denotes the Sin function in package math
1798```
1799
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001800### References
1801
1802An identifier operand refers to a field and is called a reference.
1803The value of a reference is a copy of the expression associated with the field
1804that it is bound to,
1805with any references within that expression bound to the respective copies of
1806the fields they were originally bound to.
1807Implementations may use a different mechanism to evaluate as long as
1808these semantics are maintained.
1809
1810```
1811a: {
1812 place: string
1813 greeting: "Hello, \(place)!"
1814}
1815
1816b: a & { place: "world" }
1817c: a & { place: "you" }
1818
1819d: b.greeting // "Hello, world!"
1820e: c.greeting // "Hello, you!"
1821```
1822
1823
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001824
1825### Primary expressions
1826
1827Primary expressions are the operands for unary and binary expressions.
1828
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001829
1830```
1831
1832Slice: indices must be complete
1833([0, 1, 2, 3] | [2, 3])[0:2] => [0, 1] | [2, 3]
1834
1835([0, 1, 2, 3] | *[2, 3])[0:2] => [0, 1] | [2, 3]
1836([0,1,2,3]|[2,3], [2,3])[0:2] => ([0,1]|[2,3], [2,3])
1837
1838Index
1839a: (1|2, 1)
1840b: ([0,1,2,3]|[2,3], [2,3])[a] => ([0,1,2,3]|[2,3][a], 3)
1841
1842Binary operation
1843A binary is only evaluated if its operands are complete.
1844
1845Input Maximum allowed evaluation
1846a: string string
1847b: 2 2
1848c: a * b a * 2
1849
1850An error in a struct is if the evaluation of any expression results in
1851bottom, where an incomplete expression is not considered bottom.
1852```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01001853<!-- TODO(mpvl)
1854 Conversion |
1855-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001856```
1857PrimaryExpr =
1858 Operand |
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001859 PrimaryExpr Selector |
1860 PrimaryExpr Index |
1861 PrimaryExpr Slice |
1862 PrimaryExpr Arguments .
1863
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001864Selector = "." (identifier | simple_string_lit) .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001865Index = "[" Expression "]" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001866Argument = Expression .
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02001867Arguments = "(" [ ( Argument { "," Argument } ) [ "," ] ] ")" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001868```
1869<!---
Marcel van Lohuizen9ffcbbc2019-10-23 18:05:05 +02001870TODO:
1871 PrimaryExpr Query |
1872Query = "." Filters .
1873Filters = Filter { Filter } .
1874Filter = "[" [ "?" ] AliasExpr "]" .
1875
1876TODO: maybe reintroduce slices, as they are useful in queries, probably this
1877time with Python semantics.
1878Slice = "[" [ Expression ] ":" [ Expression ] [ ":" [Expression] ] "]" .
1879
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001880Argument = Expression | ( identifer ":" Expression ).
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001881
1882// & expression type
1883// string_lit: same as label. Arguments is current node.
1884// If selector is applied to list, it performs the operation for each
1885// element.
1886
1887TODO: considering allowing decimal_lit for selectors.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001888--->
1889
1890```
1891x
18922
1893(s + ".txt")
1894f(3.1415, true)
1895m["foo"]
1896s[i : j + 1]
1897obj.color
1898f.p[i].x
1899```
1900
1901
1902### Selectors
1903
Roger Peppeded0e1d2019-09-24 16:39:36 +01001904For a [primary expression](#primary-expressions) `x` that is not a [package name](#package-clause),
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001905the selector expression
1906
1907```
1908x.f
1909```
1910
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001911denotes the element of a <!--list or -->struct `x` identified by `f`.
1912<!--For structs, -->`f` must be an identifier or a string literal identifying
1913any definition or regular non-optional field.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001914The identifier `f` is called the field selector.
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001915
1916<!--
1917Allowing strings to be used as field selectors obviates the need for
1918backquoted identifiers. Note that some standards use names for structs that
1919are not standard identifiers (such "Fn::Foo"). Note that indexing does not
1920allow access to identifiers.
1921-->
1922
1923<!--
1924For lists, `f` must be an integer and follows the same lookup rules as
1925for the index operation.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001926The type of the selector expression is the type of `f`.
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001927-->
1928
Roger Peppeded0e1d2019-09-24 16:39:36 +01001929If `x` is a package name, see the section on [qualified identifiers](#qualified-identifiers).
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001930
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001931<!--
1932TODO: consider allowing this and also for selectors. It needs to be considered
1933how defaults are corried forward in cases like:
1934
1935 x: { a: string | *"foo" } | *{ a: int | *4 }
1936 y: x.a & string
1937
1938What is y in this case?
1939 (x.a & string, _|_)
1940 (string|"foo", _|_)
1941 (string|"foo", "foo)
1942If the latter, then why?
1943
1944For a disjunction of the form `x1 | ... | xn`,
1945the selector is applied to each element `x1.f | ... | xn.f`.
1946-->
1947
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001948Otherwise, if `x` is not a <!--list or -->struct,
1949or if `f` does not exist in `x`,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001950the result of the expression is bottom (an error).
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001951In the latter case the expression is incomplete.
1952The operand of a selector may be associated with a default.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001953
1954```
1955T: {
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001956 x: int
1957 y: 3
1958 "x-y": 4
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001959}
1960
Marcel van Lohuizenc7791ac2019-10-07 11:29:28 +02001961a: T.x // int
1962b: T.y // 3
1963c: T.z // _|_ // field 'z' not found in T
1964d: T."x-y" // 4
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001965
1966e: {a: 1|*2} | *{a: 3|*4}
1967f: e.a // 4 (default value)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001968```
1969
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001970<!--
1971```
1972(v, d).f => (v.f, d.f)
1973
1974e: {a: 1|*2} | *{a: 3|*4}
1975f: e.a // 4 after selecting default from (({a: 1|*2} | {a: 3|*4}).a, 4)
1976
1977```
1978-->
1979
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001980
1981### Index expressions
1982
1983A primary expression of the form
1984
1985```
1986a[x]
1987```
1988
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02001989denotes the element of a list or struct `a` indexed by `x`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01001990The value `x` is called the index or field name, respectively.
1991The following rules apply:
1992
1993If `a` is not a struct:
1994
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02001995- `a` is a list (which need not be complete)
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02001996- the index `x` unified with `int` must be concrete.
1997- the index `x` is in range if `0 <= x < len(a)`, where only the
1998 explicitly defined values of an open-ended list are considered,
1999 otherwise it is out of range
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002000
2001The result of `a[x]` is
2002
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02002003for `a` of list type:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002004
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02002005- the list element at index `x`, if `x` is within range
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002006- bottom (an error), otherwise
2007
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002008
2009for `a` of struct type:
2010
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002011- the index `x` unified with `string` must be concrete.
Marcel van Lohuizend2825532019-09-23 12:44:01 +01002012- the value of the regular and non-optional field named `x` of struct `a`,
2013 if this field exists
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002014- bottom (an error), otherwise
2015
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002016
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002017```
2018[ 1, 2 ][1] // 2
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002019[ 1, 2 ][2] // _|_
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002020[ 1, 2, ...][2] // _|_
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002021```
2022
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002023Both the operand and index value may be a value-default pair.
2024```
2025va[vi] => va[vi]
2026va[(vi, di)] => (va[vi], va[di])
2027(va, da)[vi] => (va[vi], da[vi])
2028(va, da)[(vi, di)] => (va[vi], da[di])
2029```
2030
2031```
2032Fields Result
2033x: [1, 2] | *[3, 4] ([1,2]|[3,4], [3,4])
2034i: int | *1 (int, 1)
2035
2036v: x[i] (x[i], 4)
2037```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002038
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002039### Operators
2040
2041Operators combine operands into expressions.
2042
2043```
2044Expression = UnaryExpr | Expression binary_op Expression .
2045UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
2046
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002047binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op .
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01002048rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002049add_op = "+" | "-" .
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002050mul_op = "*" | "/" | "div" | "mod" | "quo" | "rem" .
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01002051unary_op = "+" | "-" | "!" | "*" | rel_op .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002052```
2053
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002054Comparisons are discussed [elsewhere](#Comparison-operators).
Marcel van Lohuizen7da140a2019-02-01 09:35:00 +01002055For any binary operators, the operand types must unify.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002056
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002057<!-- TODO: durations
2058 unless the operation involves durations.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002059
2060Except for duration operations, if one operand is an untyped [literal] and the
2061other operand is not, the constant is [converted] to the type of the other
2062operand.
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002063-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002064
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002065Operands of unary and binary expressions may be associated with a default using
2066the following
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002067
Marcel van Lohuizenfe4abac2019-04-06 17:19:03 +02002068<!--
2069```
2070O1: op (v1, d1) => (op v1, op d1)
2071
2072O2: (v1, d1) op (v2, d2) => (v1 op v2, d1 op d2)
2073and because v => (v, v)
2074O3: v1 op (v2, d2) => (v1 op v2, v1 op d2)
2075O4: (v1, d1) op v2 => (v1 op v2, d1 op v2)
2076```
2077-->
2078
2079```
2080Field Resulting Value-Default pair
2081a: *1|2 (1|2, 1)
2082b: -a (-a, -1)
2083
2084c: a + 2 (a+2, 3)
2085d: a + a (a+a, 2)
2086```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002087
2088#### Operator precedence
2089
2090Unary operators have the highest precedence.
2091
2092There are eight precedence levels for binary operators.
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002093Multiplication operators binds strongest, followed by
2094addition operators, comparison operators,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002095`&&` (logical AND), `||` (logical OR), `&` (unification),
2096and finally `|` (disjunction):
2097
2098```
2099Precedence Operator
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002100 7 * / div mod quo rem
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002101 6 + -
Marcel van Lohuizen2b0e7cd2019-03-25 08:28:41 +01002102 5 == != < <= > >= =~ !~
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002103 4 &&
2104 3 ||
2105 2 &
2106 1 |
2107```
2108
2109Binary operators of the same precedence associate from left to right.
2110For instance, `x / y * z` is the same as `(x / y) * z`.
2111
2112```
2113+x
211423 + 3*x[i]
2115x <= f()
2116f() || g()
2117x == y+1 && y == z-1
21182 | int
2119{ a: 1 } & { b: 2 }
2120```
2121
2122#### Arithmetic operators
2123
2124Arithmetic operators apply to numeric values and yield a result of the same type
2125as the first operand. The three of the four standard arithmetic operators
2126`(+, -, *)` apply to integer and decimal floating-point types;
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01002127`+` and `*` also apply to lists and strings.
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002128`/` only applies to decimal floating-point types and
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002129`div`, `mod`, `quo`, and `rem` only apply to integer types.
2130
2131```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002132+ sum integers, floats, lists, strings, bytes
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002133- difference integers, floats
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002134* product integers, floats, lists, strings, bytes
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002135/ quotient floats
2136div division integers
2137mod modulo integers
2138quo quotient integers
2139rem remainder integers
2140```
2141
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002142For any operator that accepts operands of type `float`, any operand may be
2143of type `int` or `float`, in which case the result will be `float` if any
2144of the operands is `float` or `int` otherwise.
2145For `/` the result is always `float`.
2146
2147
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002148#### Integer operators
2149
2150For two integer values `x` and `y`,
2151the integer quotient `q = x div y` and remainder `r = x mod y `
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002152implement Euclidean division and
2153satisfy the following relationship:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002154
2155```
2156r = x - y*q with 0 <= r < |y|
2157```
2158where `|y|` denotes the absolute value of `y`.
2159
2160```
2161 x y x div y x mod y
2162 5 3 1 2
2163-5 3 -2 1
2164 5 -3 -1 2
2165-5 -3 2 1
2166```
2167
2168For two integer values `x` and `y`,
2169the integer quotient `q = x quo y` and remainder `r = x rem y `
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002170implement truncated division and
2171satisfy the following relationship:
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002172
2173```
2174x = q*y + r and |r| < |y|
2175```
2176
2177with `x quo y` truncated towards zero.
2178
2179```
2180 x y x quo y x rem y
2181 5 3 1 2
2182-5 3 -1 -2
2183 5 -3 -1 2
2184-5 -3 1 -2
2185```
2186
2187A zero divisor in either case results in bottom (an error).
2188
2189For integer operands, the unary operators `+` and `-` are defined as follows:
2190
2191```
2192+x is 0 + x
2193-x negation is 0 - x
2194```
2195
2196
2197#### Decimal floating-point operators
2198
2199For decimal floating-point numbers, `+x` is the same as `x`,
2200while -x is the negation of x.
2201The result of a floating-point division by zero is bottom (an error).
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002202
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002203<!-- TODO: consider making it +/- Inf -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002204
2205An implementation may combine multiple floating-point operations into a single
2206fused operation, possibly across statements, and produce a result that differs
2207from the value obtained by executing and rounding the instructions individually.
2208
2209
2210#### List operators
2211
2212Lists can be concatenated using the `+` operator.
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002213Opens list are closed to their default value beforehand.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002214
2215```
2216[ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
2217[ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002218[ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4 ]
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002219```
2220
Jonathan Amsterdam0500c312019-02-16 18:04:09 -05002221Lists can be multiplied with a non-negative`int` using the `*` operator
Marcel van Lohuizen13e36bd2019-02-01 09:59:18 +01002222to create a repeated the list by the indicated number.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002223```
22243*[1,2] // [1, 2, 1, 2, 1, 2]
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +020022253*[1, 2, ...] // [1, 2, 1, 2, 1 ,2]
Marcel van Lohuizen13e36bd2019-02-01 09:59:18 +01002226[byte]*4 // [byte, byte, byte, byte]
Jonathan Amsterdam0500c312019-02-16 18:04:09 -050022270*[1,2] // []
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002228```
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002229
2230<!-- TODO(mpvl): should we allow multiplication with a range?
2231If so, how does one specify a list with a range of possible lengths?
2232
2233Suggestion from jba:
2234Multiplication should distribute over disjunction,
2235so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x].
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002236The hard part is figuring out what (>=1 & <=3) * [x] means,
2237since >=1 & <=3 includes many floats.
Marcel van Lohuizen08466f82019-02-01 09:09:09 +01002238(mpvl: could constrain arguments to parameter types, but needs to be
2239done consistently.)
2240-->
2241
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002242
2243#### String operators
2244
2245Strings can be concatenated using the `+` operator:
2246```
2247s := "hi " + name + " and good bye"
2248```
2249String addition creates a new string by concatenating the operands.
2250
2251A string can be repeated by multiplying it:
2252
2253```
2254s: "etc. "*3 // "etc. etc. etc. "
2255```
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002256
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002257<!-- jba: Do these work for byte sequences? If not, why not? -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002258
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002259
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002260##### Comparison operators
2261
2262Comparison operators compare two operands and yield an untyped boolean value.
2263
2264```
2265== equal
2266!= not equal
2267< less
2268<= less or equal
2269> greater
2270>= greater or equal
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002271=~ matches regular expression
2272!~ does not match regular expression
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002273```
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002274
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002275<!-- regular expression operator inspired by Bash, Perl, and Ruby. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002276
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002277In any comparison, the types of the two operands must unify or one of the
2278operands must be null.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002279
2280The equality operators `==` and `!=` apply to operands that are comparable.
2281The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered.
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002282The matching operators `=~` and `!~` apply to a string and regular
2283expression operand.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002284These terms and the result of the comparisons are defined as follows:
2285
Marcel van Lohuizen855243e2019-02-07 18:00:55 +01002286- Null is comparable with itself and any other type.
2287 Two null values are always equal, null is unequal with anything else.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002288- Boolean values are comparable.
2289 Two boolean values are equal if they are either both true or both false.
2290- Integer values are comparable and ordered, in the usual way.
2291- Floating-point values are comparable and ordered, as per the definitions
2292 for binary coded decimals in the IEEE-754-2008 standard.
Marcel van Lohuizen4a360992019-05-11 18:18:31 +02002293- Floating point numbers may be compared with integers.
Marcel van Lohuizen4108f802019-08-13 18:30:25 +02002294- String and bytes values are comparable and ordered lexically byte-wise.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002295- Struct are not comparable.
Marcel van Lohuizen855243e2019-02-07 18:00:55 +01002296- Lists are not comparable.
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002297- The regular expression syntax is the one accepted by RE2,
2298 described in https://github.com/google/re2/wiki/Syntax,
2299 except for `\C`.
2300- `s =~ r` is true if `s` matches the regular expression `r`.
2301- `s !~ r` is true if `s` does not match regular expression `r`.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002302
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02002303<!--- TODO: consider the following
2304- For regular expression, named capture groups are interpreted as CUE references
2305 that must unify with the strings matching this capture group.
2306--->
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002307<!-- TODO: Implementations should adopt an algorithm that runs in linear time? -->
Marcel van Lohuizen88a8a5f2019-02-20 01:26:22 +01002308<!-- Consider implementing Level 2 of Unicode regular expression. -->
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002309
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002310```
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +010023113 < 4 // true
Marcel van Lohuizen4a360992019-05-11 18:18:31 +020023123 < 4.0 // true
Marcel van Lohuizen0a0a3ac2019-02-10 16:48:53 +01002313null == 2 // false
2314null != {} // true
2315{} == {} // _|_: structs are not comparable against structs
2316
2317"Wild cats" =~ "cat" // true
2318"Wild cats" !~ "dog" // true
2319
2320"foo" =~ "^[a-z]{3}$" // true
2321"foo" =~ "^[a-z]{4}$" // false
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002322```
2323
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002324<!-- jba
2325I think I know what `3 < a` should mean if
2326
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002327 a: >=1 & <=5
2328
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002329It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely.
2330
Marcel van Lohuizen62b87272019-02-01 10:07:49 +01002331But 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 -05002332-->
2333
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002334#### Logical operators
2335
2336Logical operators apply to boolean values and yield a result of the same type
2337as the operands. The right operand is evaluated conditionally.
2338
2339```
2340&& conditional AND p && q is "if p then q else false"
2341|| conditional OR p || q is "if p then true else q"
2342! NOT !p is "not p"
2343```
2344
2345
2346<!--
2347### TODO TODO TODO
2348
23493.14 / 0.0 // illegal: division by zero
2350Illegal conversions always apply to CUE.
2351
2352Implementation 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.
2353-->
2354
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002355<!--- TODO(mpvl): conversions
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002356### Conversions
2357Conversions are expressions of the form `T(x)` where `T` and `x` are
2358expressions.
2359The result is always an instance of `T`.
2360
2361```
2362Conversion = Expression "(" Expression [ "," ] ")" .
2363```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002364--->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002365<!---
2366
2367A literal value `x` can be converted to type T if `x` is representable by a
2368value of `T`.
2369
2370As a special case, an integer literal `x` can be converted to a string type
2371using the same rule as for non-constant x.
2372
2373Converting a literal yields a typed value as result.
2374
2375```
2376uint(iota) // iota value of type uint
2377float32(2.718281828) // 2.718281828 of type float32
2378complex128(1) // 1.0 + 0.0i of type complex128
2379float32(0.49999999) // 0.5 of type float32
2380float64(-1e-1000) // 0.0 of type float64
2381string('x') // "x" of type string
2382string(0x266c) // "♬" of type string
2383MyString("foo" + "bar") // "foobar" of type MyString
2384string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
2385(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
2386int(1.2) // illegal: 1.2 cannot be represented as an int
2387string(65.0) // illegal: 65.0 is not an integer constant
2388```
2389--->
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002390<!---
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002391
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002392A conversion is always allowed if `x` is an instance of `T`.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002393
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002394If `T` and `x` of different underlying type, a conversion is allowed if
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002395`x` can be converted to a value `x'` of `T`'s type, and
2396`x'` is an instance of `T`.
2397A value `x` can be converted to the type of `T` in any of these cases:
2398
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002399- `x` is a struct and is subsumed by `T`.
2400- `x` and `T` are both integer or floating points.
2401- `x` is an integer or a byte sequence and `T` is a string.
2402- `x` is a string and `T` is a byte sequence.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002403
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002404Specific rules apply to conversions between numeric types, structs,
2405or to and from a string type. These conversions may change the representation
2406of `x`.
2407All other conversions only change the type but not the representation of x.
2408
2409
2410#### Conversions between numeric ranges
2411For the conversion of numeric values, the following rules apply:
2412
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +010024131. Any integer value can be converted into any other integer value
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002414 provided that it is within range.
24152. When converting a decimal floating-point number to an integer, the fraction
2416 is discarded (truncation towards zero). TODO: or disallow truncating?
2417
2418```
2419a: uint16(int(1000)) // uint16(1000)
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002420b: uint8(1000) // _|_ // overflow
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002421c: int(2.5) // 2 TODO: TBD
2422```
2423
2424
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002425#### Conversions to and from a string type
2426
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002427Converting a list of bytes to a string type yields a string whose successive
2428bytes are the elements of the slice.
2429Invalid UTF-8 is converted to `"\uFFFD"`.
2430
2431```
2432string('hell\xc3\xb8') // "hellø"
2433string(bytes([0x20])) // " "
2434```
2435
2436As string value is always convertible to a list of bytes.
2437
2438```
2439bytes("hellø") // 'hell\xc3\xb8'
2440bytes("") // ''
2441```
2442
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002443#### Conversions between list types
2444
2445Conversions between list types are possible only if `T` strictly subsumes `x`
2446and the result will be the unification of `T` and `x`.
2447
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002448If we introduce named types this would be different from IP & [10, ...]
2449
2450Consider removing this until it has a different meaning.
2451
2452```
2453IP: 4*[byte]
2454Private10: IP([10, ...]) // [10, byte, byte, byte]
2455```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002456
Marcel van Lohuizen75cb0032019-01-11 12:10:48 +01002457#### Conversions between struct types
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002458
2459A conversion from `x` to `T`
2460is applied using the following rules:
2461
24621. `x` must be an instance of `T`,
24632. all fields defined for `x` that are not defined for `T` are removed from
2464 the result of the conversion, recursively.
2465
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002466<!-- jba: I don't think you say anywhere that the matching fields are unified.
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002467mpvl: they are not, x must be an instance of T, in which case x == T&x,
2468so unification would be unnecessary.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002469-->
Marcel van Lohuizena3f00972019-02-01 11:10:39 +01002470<!--
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002471```
2472T: {
2473 a: { b: 1..10 }
2474}
2475
2476x1: {
2477 a: { b: 8, c: 10 }
2478 d: 9
2479}
2480
2481c1: T(x1) // { a: { b: 8 } }
Marcel van Lohuizen6f0faec2018-12-16 10:42:42 +01002482c2: T({}) // _|_ // missing field 'a' in '{}'
2483c3: T({ a: {b: 0} }) // _|_ // field a.b does not unify (0 & 1..10)
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002484```
Marcel van Lohuizend340e8d2019-01-30 16:57:39 +01002485-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002486
2487### Calls
2488
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002489Calls can be made to core library functions, called builtins.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002490Given an expression `f` of function type F,
2491```
2492f(a1, a2, … an)
2493```
2494calls `f` with arguments a1, a2, … an. Arguments must be expressions
2495of which the values are an instance of the parameter types of `F`
2496and are evaluated before the function is called.
2497
2498```
2499a: math.Atan2(x, y)
2500```
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002501
2502In a function call, the function value and arguments are evaluated in the usual
Marcel van Lohuizen1e0fe9c2018-12-21 00:17:06 +01002503order.
2504After they are evaluated, the parameters of the call are passed by value
2505to the function and the called function begins execution.
2506The return parameters
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002507of the function are passed by value back to the calling function when the
2508function returns.
2509
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002510
2511### Comprehensions
2512
Marcel van Lohuizen66db9202018-12-17 19:02:08 +01002513Lists and fields can be constructed using comprehensions.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002514
2515Each define a clause sequence that consists of a sequence of `for`, `if`, and
2516`let` clauses, nesting from left to right.
2517The `for` and `let` clauses each define a new scope in which new values are
2518bound to be available for the next clause.
2519
2520The `for` clause binds the defined identifiers, on each iteration, to the next
2521value of some iterable value in a new scope.
2522A `for` clause may bind one or two identifiers.
Marcel van Lohuizen4245fb42019-09-09 11:22:12 +02002523If there is one identifier, it binds it to the value of
2524a list element or struct field value.
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002525If there are two identifiers, the first value will be the key or index,
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002526if available, and the second will be the value.
2527
Marcel van Lohuizen4245fb42019-09-09 11:22:12 +02002528For lists, `for` iterates over all elements in the list after closing it.
2529For structs, `for` iterates over all non-optional regular fields.
2530
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002531An `if` clause, or guard, specifies an expression that terminates the current
2532iteration if it evaluates to false.
2533
2534The `let` clause binds the result of an expression to the defined identifier
2535in a new scope.
2536
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002537A current iteration is said to complete if the innermost block of the clause
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002538sequence is reached.
2539
Marcel van Lohuizen5fee32f2019-01-21 22:18:48 +01002540_List comprehensions_ specify a single expression that is evaluated and included
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002541in the list for each completed iteration.
2542
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002543_Field comprehensions_ follow a clause sequence with a struct literal,
2544where the struct literal is evaluated and embedded at the point of
2545declaration of the comprehension for each complete iteration.
2546As usual, fields in the struct may evaluate to the same label,
2547resulting in the unification of their values.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002548
2549```
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02002550Comprehension = Clauses StructLit .
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002551ListComprehension = "[" Expression Clauses "]" .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002552
2553Clauses = Clause { Clause } .
2554Clause = ForClause | GuardClause | LetClause .
2555ForClause = "for" identifier [ ", " identifier] "in" Expression .
2556GuardClause = "if" Expression .
2557LetClause = "let" identifier "=" Expression .
2558```
2559
2560```
2561a: [1, 2, 3, 4]
2562b: [ x+1 for x in a if x > 1] // [3, 4, 5]
2563
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002564c: {
2565 for x in a
2566 if x < 4
2567 let y = 1 {
2568 "\(x)": x + y
2569 }
2570}
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002571d: { "1": 2, "2": 3, "3": 4 }
2572```
2573
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002574
2575### String interpolation
2576
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002577String interpolation allows constructing strings by replacing placeholder
2578expressions with their string representation.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002579String interpolation may be used in single- and double-quoted strings, as well
2580as their multiline equivalent.
2581
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002582A placeholder consists of "\(" followed by an expression and a ")". The
2583expression is evaluated within the scope within which the string is defined.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002584
2585```
2586a: "World"
2587b: "Hello \( a )!" // Hello World!
2588```
2589
2590
2591## Builtin Functions
2592
2593Built-in functions are predeclared. They are called like any other function.
2594
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002595
2596### `len`
2597
2598The built-in function `len` takes arguments of various types and return
2599a result of type int.
2600
2601```
2602Argument type Result
2603
2604string string length in bytes
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002605bytes length of byte sequence
2606list list length, smallest length for an open list
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002607struct number of distinct data fields, including optional
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002608```
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002609<!-- TODO: consider not supporting len, but instead rely on more
2610precisely named builtin functions:
2611 - strings.RuneLen(x)
2612 - bytes.Len(x) // x may be a string
2613 - struct.NumFooFields(x)
2614 - list.Len(x)
2615-->
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002616
2617```
2618Expression Result
2619len("Hellø") 6
2620len([1, 2, 3]) 3
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002621len([1, 2, ...]) >=2
Marcel van Lohuizen45163fa2019-01-22 15:53:32 +01002622```
2623
Marcel van Lohuizen62658a82019-06-16 12:18:47 +02002624
2625### `close`
2626
2627The builtin function `close` converts a partially defined, or open, struct
2628to a fully defined, or closed, struct.
2629
2630
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002631### `and`
2632
2633The built-in function `and` takes a list and returns the result of applying
2634the `&` operator to all elements in the list.
2635It returns top for the empty list.
2636
2637Expression: Result
2638and([a, b]) a & b
2639and([a]) a
2640and([]) _
2641
2642### `or`
2643
2644The built-in function `or` takes a list and returns the result of applying
2645the `|` operator to all elements in the list.
2646It returns bottom for the empty list.
2647
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002648```
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002649Expression: Result
2650and([a, b]) a | b
2651and([a]) a
2652and([]) _|_
Marcel van Lohuizen6c35af62019-05-06 10:50:57 +02002653```
Marcel van Lohuizena460fe82019-04-26 10:20:51 +02002654
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002655
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002656## Cycles
2657
2658Implementations are required to interpret or reject cycles encountered
2659during evaluation according to the rules in this section.
2660
2661
2662### Reference cycles
2663
2664A _reference cycle_ occurs if a field references itself, either directly or
2665indirectly.
2666
2667```
2668// x references itself
2669x: x
2670
2671// indirect cycles
2672b: c
2673c: d
2674d: b
2675```
2676
2677Implementations should report these as an error except in the following cases:
2678
2679
2680#### Expressions that unify an atom with an expression
2681
2682An expression of the form `a & e`, where `a` is an atom
2683and `e` is an expression, always evaluates to `a` or bottom.
2684As it does not matter how we fail, we can assume the result to be `a`
2685and validate after the field in which the expression occurs has been evaluated
2686that `a == e`.
2687
2688```
Marcel van Lohuizeneac8f9a2019-08-03 13:53:56 +02002689// Config Evaluates to (requiring concrete values)
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002690x: { x: {
2691 a: b + 100 a: _|_ // cycle detected
2692 b: a - 100 b: _|_ // cycle detected
2693} }
2694
2695y: x & { y: {
2696 a: 200 a: 200 // asserted that 200 == b + 100
2697 b: 100
2698} }
2699```
2700
2701
2702#### Field values
2703
2704A field value of the form `r & v`,
2705where `r` evaluates to a reference cycle and `v` is a value,
2706evaluates to `v`.
2707Unification is idempotent and unifying a value with itself ad infinitum,
2708which is what the cycle represents, results in this value.
2709Implementations should detect cycles of this kind, ignore `r`,
2710and take `v` as the result of unification.
Marcel van Lohuizen0d0b9ad2019-10-10 18:19:28 +02002711
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002712<!-- Tomabechi's graph unification algorithm
2713can detect such cycles at near-zero cost. -->
2714
2715```
2716Configuration Evaluated
2717// c Cycles in nodes of type struct evaluate
2718// ↙︎ ↖ to the fixed point of unifying their
2719// a → b values ad infinitum.
2720
2721a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 }
2722b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 }
2723c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 }
2724
2725// resolve a b & {x:1}
2726// substitute b c & {y:2} & {x:1}
2727// substitute c a & {z:3} & {y:2} & {x:1}
2728// eliminate a (cycle) {z:3} & {y:2} & {x:1}
2729// simplify {x:1,y:2,z:3}
2730```
2731
2732This rule also applies to field values that are disjunctions of unification
2733operations of the above form.
2734
2735```
2736a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1}
2737b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2}
2738c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3}
2739
2740
2741// resolving a b&{x:1} | {y:1}
2742// substitute b ({x:2} | c&{z:2})&{x:1} | {y:1}
2743// simplify c&{z:2}&{x:1} | {y:1}
2744// substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1}
2745// simplify a&{y:3}&{z:2}&{x:1} | {y:1}
2746// eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1}
2747// expand {x:1,y:3,z:2} | {y:1}
2748```
2749
2750Note that all nodes that form a reference cycle to form a struct will evaluate
2751to the same value.
2752If a field value is a disjunction, any element that is part of a cycle will
2753evaluate to this value.
2754
2755
2756### Structural cycles
2757
2758CUE disallows infinite structures.
2759Implementations must report an error when encountering such declarations.
2760
2761<!-- for instance using an occurs check -->
2762
2763```
2764// Disallowed: a list of infinite length with all elements being 1.
2765list: {
2766 head: 1
2767 tail: list
2768}
2769
2770// Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...).
2771a: {
2772 b: c
2773}
2774c: {
2775 d: a
2776}
2777```
2778
2779It is allowed for a value to define an infinite set of possibilities
2780without evaluating to an infinite structure itself.
2781
2782```
2783// List defines a list of arbitrary length (default null).
2784List: *null | {
2785 head: _
2786 tail: List
2787}
2788```
2789
2790<!--
Marcel van Lohuizen7f48df72019-02-01 17:24:59 +01002791Consider banning any construct that makes CUE not having a linear
2792running time expressed in the number of nodes in the output.
2793
2794This would require restricting constructs like:
2795
2796(fib&{n:2}).out
2797
2798fib: {
2799 n: int
2800
2801 out: (fib&{n:n-2}).out + (fib&{n:n-1}).out if n >= 2
2802 out: fib({n:n-2}).out + fib({n:n-1}).out if n >= 2
2803 out: n if n < 2
2804}
2805
2806-->
2807<!--
Marcel van Lohuizen6713ae22019-01-26 14:42:25 +01002808### Unused fields
2809
2810TODO: rules for detection of unused fields
2811
28121. Any alias value must be used
2813-->
2814
2815
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002816## Modules, instances, and packages
2817
2818CUE configurations are constructed combining _instances_.
2819An instance, in turn, is constructed from one or more source files belonging
2820to the same _package_ that together declare the data representation.
2821Elements of this data representation may be exported and used
2822in other instances.
2823
2824### Source file organization
2825
2826Each source file consists of an optional package clause defining collection
2827of files to which it belongs,
2828followed by a possibly empty set of import declarations that declare
2829packages whose contents it wishes to use, followed by a possibly empty set of
2830declarations.
2831
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02002832Like with a struct, a source file may contain embeddings.
2833Unlike with a struct, the embedded expressions may be any value.
2834If the result of the unification of all embedded values is not a struct,
2835it will be output instead of its enclosing file when exporting CUE
2836to a data format
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002837
2838```
Marcel van Lohuizen1f5a9032019-09-09 23:53:42 +02002839SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
2840```
2841
2842```
2843"Hello \(place)!"
2844
2845place: "world"
2846
2847// Outputs "Hello world!"
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002848```
2849
2850### Package clause
2851
2852A package clause is an optional clause that defines the package to which
2853a source file the file belongs.
2854
2855```
2856PackageClause = "package" PackageName .
2857PackageName = identifier .
2858```
2859
2860The PackageName must not be the blank identifier.
2861
2862```
2863package math
2864```
2865
2866### Modules and instances
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002867A _module_ defines a tree of directories, rooted at the _module root_.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002868
2869All source files within a module with the same package belong to the same
2870package.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002871<!-- jba: I can't make sense of the above sentence. -->
2872A module may define multiple packages.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002873
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002874An _instance_ of a package is any subset of files belonging
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002875to the same package.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002876<!-- jba: Are you saying that -->
2877<!-- if I have a package with files a, b and c, then there are 8 instances of -->
2878<!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the -->
2879<!-- purpose of that definition? -->
2880It is interpreted as the concatenation of these files.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002881
2882An implementation may impose conventions on the layout of package files
2883to determine which files of a package belongs to an instance.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002884For example, an instance may be defined as the subset of package files
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002885belonging to a directory and all its ancestors.
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002886<!-- jba: OK, that helps a little, but I still don't see what the purpose is. -->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002887
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002888
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002889### Import declarations
2890
2891An import declaration states that the source file containing the declaration
2892depends on definitions of the _imported_ package (§Program initialization and
2893execution) and enables access to exported identifiers of that package.
2894The import names an identifier (PackageName) to be used for access and an
2895ImportPath that specifies the package to be imported.
2896
2897```
Marcel van Lohuizen40178752019-08-25 19:17:56 +02002898ImportDecl = "import" ( ImportSpec | "(" { ImportSpec "," } ")" ) .
Marcel van Lohuizenfbab65d2019-08-13 16:51:15 +02002899ImportSpec = [ PackageName ] ImportPath .
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002900ImportLocation = { unicode_value } .
2901ImportPath = `"` ImportLocation [ ":" identifier ] `"` .
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002902```
2903
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002904The PackageName is used in qualified identifiers to access
2905exported identifiers of the package within the importing source file.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002906It is declared in the file block.
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002907It defaults to the identifier specified in the package clause of the imported
2908package, which must match either the last path component of ImportLocation
2909or the identifier following it.
2910
2911<!--
2912Note: this deviates from the Go spec where there is no such restriction.
2913This restriction has the benefit of being to determine the identifiers
2914for packages from within the file itself. But for CUE it is has another benefit:
2915when using package hiearchies, one is more likely to want to include multiple
2916packages within the same directory structure. This mechanism allows
2917disambiguation in these cases.
2918-->
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002919
2920The interpretation of the ImportPath is implementation-dependent but it is
2921typically either the path of a builtin package or a fully qualifying location
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002922of a package within a source code repository.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002923
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002924An ImportLocation must be a non-empty strings using only characters belonging
2925Unicode's L, M, N, P, and S general categories
2926(the Graphic characters without spaces)
2927and may not include the characters !"#$%&'()*,:;<=>?[\]^`{|}
2928or the Unicode replacement character U+FFFD.
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002929
Jonathan Amsterdame4790382019-01-20 10:29:29 -05002930Assume we have package containing the package clause "package math",
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002931which exports function Sin at the path identified by "lib/math".
2932This table illustrates how Sin is accessed in files
2933that import the package after the various types of import declaration.
2934
2935```
2936Import declaration Local name of Sin
2937
2938import "lib/math" math.Sin
Marcel van Lohuizen7414fae2019-08-13 17:26:35 +02002939import "lib/math:math" math.Sin
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002940import m "lib/math" m.Sin
Marcel van Lohuizendd5e5892018-11-22 23:29:16 +01002941```
2942
2943An import declaration declares a dependency relation between the importing and
2944imported package. It is illegal for a package to import itself, directly or
2945indirectly, or to directly import a package without referring to any of its
2946exported identifiers.
2947
2948
2949### An example package
2950
2951TODO