CLVM is the compiled, minimal version of ChiaLisp that is used by the Chia network.
Chialisp compiles into CLVM so it's important to understand how it works.
The full set of operators is documented here.
This guide will cover the basics of the language and act as an introduction to the structure of programs.
You should be able to follow along by running a version of clvm_tools.
Follow the instructions in the README to install it.
CLVM is built out of cons boxes and atoms. These are referred to as CLVM Objects.
A cons box is a pair of CLVM Objects. The items in a cons box can either be an atom or another cons box.
An atom is a string of bytes. These bytes can be interpreted both as a signed big-endian integer and a byte string, depending on the operator using it.
All atoms in CLVM are immutable. All operators that perform computations on atoms create new atoms for the result.
Atoms can be printed in three different ways, decimal, hexadecimal and as a string. Hexadecimal values are prefixed by 0x, and strings are quoted in ".
The way the integer is printed does not affect its underlying value.
The atom 100 printed in decimal is the same as 0x64 printed in hexadecimal. Likewise the value 0x68656c6c6f is the same as "hello".
When interpreting atoms as integers, it's important to remember that they are signed. In order to represent a positive integer, the most significant bit may not be set. Because of this, positive integers have a 0 byte prepended to them, in case the most significant bit in the next byte is set.
Lists are enclosed by parentheses and each entry in the list is single spaced with no period between values.
Lists are much more commonly used than cons boxes as they are more versatile.
(200300"hello""world")
You can also nest lists.
("hello"("nested""list")("world"))
Remember a list is a representation of consecutive cons boxes terminated in a null atom ().
The following expressions are equal:
To interpret an atom as a value, rather than a program, it needs to be quoted with q. Quoted values form a cons box where the first item is the q operator.
For example, this program is just the value 100:
(q.100)
Note that in the higher level Chialisp language, values do not need to be quoted.
A list is any space-separated, ordered group of one or more elements inside brackets.
For example: (70 80 90 100), (0xf00dbabe 48 "hello"), and (90) are all valid lists.
Lists can even contain other lists, such as ("list" "list" ("sublist" "sublist" ("sub-sublist")) "list").
Programs are a subset of lists which can be evaluated using CLVM. A program is actually just a list in polish notation.
In order for a list to be a valid program:
1. The first item in the list must be a valid operator
2. Every item after the first must be a valid program
Rule 2 is why literal values and non-program lists must be quoted using q . .
$ brun'(q.(8090100))' (8090100)
And now that we know we can have programs inside programs we can create programs such as:
Programs in CLVM tend to get built in this fashion.
Smaller programs are assembled together to create a larger program.
It is recommended that you create your programs in an editor with brackets matching!
There are no support for floating point numbers in CLVM, only integers. There is no hard size limit on integers in CLVM. There is also support for negative values.
Note that / returns theflooredquotient. CLVM is also different from most languages in that it floors to negative infinity rather than zero.
This can create some unexpected results when trying to divide negative numbers.
You may have noticed that the multiplication example above takes more than two parameters in the list.
This is because many operators can take a variable number of parameters.
+ and * are commutative so the order of parameters does not matter.
For non-commutative operations, (- 100 30 20 5) is equivalent to (- 100 (+ 30 20 5)).
Similarly, (/ 120 5 4 2) is equivalent to (/ 120 (* 5 4 2)).
Note that both B and C are evaluated eagerly, just like all subexpressions.
To defer evaluation until after the condition, B and C must be quoted (with
q), and then evaluated with (a).
Up until now our programs have not had any input or variables, however CLVM does have support for this.
An environment is a list of values passed to the puzzle.
You can only pass a single CLVM object as the environment, but this object can be an arbitrarily long list.
The entire environment can be referenced with an unquoted 1.
This marks the end of this section of the guide.
In this section we have covered many of the basics of using CLVM.
It is recommended you play with using the information presented here for a bit before moving on.
This guide has not covered all of the operators available in CLVM - try using some of the other ones listed here!