CUE is a language for configuration, schemas and validation. Its concept page The Logic of CUE explains the one idea most of the language rests on: types are values, and all values are ordered. This series takes that page apart one idea at a time, in nine videos of about a minute each. Every example on screen was run with cue v0.17.1.
Made with the Video Curriculum Guide; the unit cards, recorded sources and the independent review are in the The Logic of CUE Curriculum.
1. A type is a value
In CUE a type such as bool is a value: the same kind of thing as true, only less specific.
Most languages keep two worlds. Types, like bool. And values, like true.
A type describes values. A value is the thing itself. They are kept apart.
CUE removes the wall. To CUE, bool is a value too.
Think of places. Spain is a place. So is one street address in Madrid. One is just less specific.
Bool is the less specific one. It says: true or false, not decided yet. True is decided.
So a description of data, and the data, are written the same way. One language for both.
Check yourself. In CUE, what is the difference between bool and true?
2. Every value has a place in one order
All CUE values sit in one order, from top, which is any value, down to bottom, which is an error; lower means more specific, and two values can sit side by side.
CUE puts every value in one order. Number is above int. Int is above seven.
Lower means more specific. We say seven is an instance of int.
Eight is an instance of int too. Seven and eight sit side by side. Neither is above the other.
It works like a guessing game. Before any answer, it could be anything. Each answer narrows it down.
The very top is written as an underscore. It means any value at all.
Below everything is bottom. It is how CUE writes an error. So an error is not outside the order. It is the lowest point.
Check yourself. What sits at the very top of CUE's order, and what sits at the very bottom?
3. Unifying two values gives exactly one result
The & operator, unification, gives the highest value that is an instance of both sides; there is exactly one, so it does not matter which side comes first.
The ampersand combines two values. CUE calls this unifying.
The result is the highest value that is an instance of both. Unify bool with true, and you get true.
Picture two stencils, one on top of the other. Paint only gets through where both are cut.
Swap them. The result is the same. There is exactly one answer, so it does not matter which comes first.
What if nothing is an instance of both? Unify true with false, and you get bottom. An error.
Whole files unify the same way. No file wins. Both hold, or you get an error.
Check yourself. What does true & false give, and why does a & b equal b & a?
4. A constraint is a value too
A constraint such as <10 is a value that sits between a type and a single number.
Take a comparison. X is less than ten.
Drop the left side. What is left is a value, called a constraint. It means every number below ten.
It is like the height bar at a ride. The bar is not anyone's height. But it is real, and it sorts heights.
Less than ten sits below number, and above three. Between a type and a single number.
Constraints unify like any other value. Three to ten, with five to twenty, gives five to ten.
Unify less than ten with three, and you get three. The constraint and the number are the same kind of thing.
Check yourself. Where does <10 sit in the order, relative to number and to 3?
5. One operator for either-or
The | operator means "one of these", and it is the same operation whether the alternatives are values, an enum, or types, a sum type.
The pipe means: one of these. Info, or warn, or error. Other languages call this an enum.
Int or string. Other languages call this a sum type: a value that may be one of several types.
To CUE they are the same operation. Types are values, so there is nothing to tell apart.
It is a menu. A menu can list single dishes, or whole kinds of dish. Either way, it is a list of choices.
Unify a menu with a choice, and you get the choice.
Choose something that is not on it, and you get bottom.
Check yourself. Why does CUE not need separate features for enums and sum types?
6. Structs are ordered by their fields
One struct is an instance of another when it has at least the same fields, with constraints at least as strict; nobody has to declare the relationship.
A struct is a set of named fields. A municipality has a name, and a population.
A big city is stricter. Its population is an int over a million.
London fills in both fields. Its population is eight million. London is a big city, which is a municipality.
Nobody declared that. It follows from the fields. Loosely: an instance has at least the same fields, with constraints at least as strict.
Think of a paper form. Each box you fill in makes it more specific.
Adding a field does the same. A struct with a name and a population is an instance of one with only a name.
Check yourself. What two things make one struct an instance of another?
7. An optional field can fail without breaking the struct
An optional field constrains a field without demanding it, so when its constraint becomes impossible only that field is ruled out; a required field in the same state makes the whole struct an error.
A question mark makes a field optional. If port is there, it must be an int. It may also be absent.
A field without the mark is required. It must be there.
Make two required fields disagree. Port cannot be eighty and ninety. The whole struct is an error.
Make two optional fields disagree. No value is an instance of both, so the field can never be filled in. But the struct is fine without it.
It is an optional box on a form, with rules nobody can meet. You leave it blank, and hand the form in.
Fill it in anyway, and then it is an error.
Check yourself. What is the result of unifying port?: 80 with port?: 90, and what changes if the fields are required?
8. A concrete value is final
In CUE, once a field has a concrete value nothing elsewhere can change it; a different value is an error, not an override.
Many configuration tools work in layers. A layer says replicas is one. A later layer says three, and overrides it.
It is a stack of sticky notes. You read the last one. But is it really the last?
So when you read a value, it may not be the final one. Something elsewhere may override it.
In CUE, a concrete value is written in ink. Concrete means a single value, like three. It is final.
Write five somewhere else, and nothing is overridden. CUE reports a conflict, and points at both places.
And a value that is not concrete, like int, tells you it is not settled yet.
Check yourself. In CUE, what happens if one file says replicas: 3 and another says replicas: 5?
9. A default is a preference, not an override
A default, a starred alternative, is a preference: it is used when the rest of the configuration leaves the choice undecided, a concrete value replaces it without conflict, and if two defaults for one field disagree neither is used.
Without overrides, how do you say: replicas is one, unless told otherwise? With a default. A star marks the preferred choice.
A default is written in pencil. It is not a concrete value.
If nothing else is said, the default is used.
If someone writes three in ink, it is three. No conflict. Three is an int.
Now two defaults that disagree. The first says one. The second says two. Two pencil marks settle nothing. CUE does not pick.
The field stays undecided, until someone writes a concrete value. Nothing hidden decides for you.
Check yourself. What does CUE do when one place says int | 1 and another says int | 2 for the same field?
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime