The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or clickhereto continue anyway

tezos/language.md at master tezos/tezos GitHub

Michelson: the language of Smart Contracts in Tezos

The language is stack based, with high level data types and primitives and strict static type checking. Its design cherry picks traits from several language families. Vigilant readers will notice direct references to Forth, Scheme, ML and Cat.

A Michelson program is a series of instructions that are run in sequence: each instruction receives as input the stack resulting of the previous instruction, and rewrites it for the next one. The stack contains both immediate values and heap allocated structures. All values are immutable and garbage collected.

A Michelson program receives as input a single element stack containing an input value and the contents of a storage space. It must return a single element stack containing an output value and the new contents of the storage space. Alternatively, a Michelson program can fail, explicitly using a specific opcode, or because something went wrong that could not be caught by the type system (e.g. division by zero, gas exhaustion).

The types of the input, output and storage are fixed and monomorphic, and the program is typechecked before being introduced into the system. No smart contract execution can fail because an instruction has been executed on a stack of unexpected length or contents.

This specification gives the complete instruction set, type system and semantics of the language. It is meant as a precise reference manual, not an easy introduction. Even though, some examples are provided at the end of the document and can be read first or at the same time as the specification.

Table of contents

I - Semantics

This specification gives a detailed formal semantics of the Michelson language. It explains in a symbolic way the computation performed by the Michelson interpreter on a given program and initial stack to produce the corresponding resulting stack. The Michelson interpreter is a pure function: it only builds a result stack from the elements of an initial one, without affecting its environment. This semantics is then naturally given in what is called a big step form: a symbolic definition of a recursive reference interpreter. This definition takes the form of a list of rules that cover all the possible inputs of the interpreter (program and stack), and describe the computation of the corresponding resulting stacks.

Rules form and selection

The rules have the main following form.

> (syntax pattern) / (initial stack pattern) => (result stack pattern)
  iff (conditions)
  where (recursions)

The left hand side of the => sign is used for selecting the rule. Given a program and an initial stack, one (and only one) rule can be selected using the following process. First, the toplevel structure of the program must match the syntax pattern. This is quite simple since there is only a few non trivial patterns to deal with instruction sequences, and the rest is made of trivial pattern that match one specific instruction. Then, the initial stack must match the initial stack pattern. Finally, some rules add extra conditions over the values in the stack that follow the iff keyword. Sometimes, several rules may apply in a given context. In this case, the one that appears first in this specification is to be selected. If no rule applies, the result is equivalent to the one for the explicit FAIL instruction. This case does not happen on well-typed programs, as explained in the next section.

The right hand side describes the result of the interpreter if the rule applies. It consists in a stack pattern, whose part are either constants, or elements of the context (program and initial stack) that have been named on the left hand side of the => sign.

Recursive rules (big step form)

Sometimes, the result of interpreting a program is derived from the result of interpreting another one (as in conditionals or function calls). In these cases, the rule contains a clause of the following form.

where (intermediate program) / (intermediate stack) => (partial result)

This means that this rules applies in case interpreting the intermediate state on the left gives the pattern on the right.

The left hand sign of the => sign is constructed from elements of the initial state or other partial results, and the right hand side identify parts that can be used to build the result stack of the rule.

If the partial result pattern does not actually match the result of the interpretation, then the result of the whole rule is equivalent to the one for the explicit FAIL instruction. Again, this case does not happen on well-typed programs, as explained in the next section.

Format of patterns

Code patterns are of one of the following syntactical forms.

Stack patterns are of one of the following syntactical forms.

Data patterns are of one of the following syntactical forms.

The domain of instruction names, symbolic constants and data constructors is fixed by this specification. Michelson does not let the programmer introduce its own types.

Be aware that the syntax used in the specification may differ a bit from the concrete syntax, which is presented in Section IX. In particular, some instructions are annotated with types that are not present in the concrete language because they are synthesized by the typechecker.

Shortcuts

Sometimes, it is easier to think (and shorter to write) in terms of program rewriting than in terms of big step semantics. When it is the case, and when both are equivalents, we write rules of the form:

p / S   =>   S''
where   p' / S'   =>   S''

using the following shortcut:

p / S   =>   p' / S'

The concrete language also has some syntax sugar to group some common sequences of operations as one. This is described in this specification using a simple regular expression style recursive instruction rewriting.

II - Introduction to the type system and notations

This specification describes a type system for Michelson. To make things clear, in particular to readers that are not accustomed to reading formal programming language specifications, it does not give a typechecking or inference algorithm. It only gives an intentional definition of what we consider to be well-typed programs. For each syntactical form, it describes the stacks that are considered well-typed inputs, and the resulting outputs.

The type system is sound, meaning that if a program can be given a type, then if run on a well-typed input stack, the interpreter will never apply an interpretation rule on a stack of unexpected length or contents. Also, it will never reach a state where it cannot select an appropriate rule to continue the execution. Well-typed programs do not block, and do not go wrong.

Type notations

The specification introduces notations for the types of values, terms and stacks. Apart from a subset of value types that appear in the form of type annotations in some places throughout the language, it is important to understand that this type language only exists in the specification.

A stack type can be written:

Instructions, programs and primitives of the language are also typed, their types are written:

(type of stack before) -> (type of stack after)

The types of values in the stack are written:

Meta type variables

The typing rules introduce meta type variables. To be clear, this has nothing to do with polymorphism, which Michelson does not have. These variables only live at the specification level, and are used to express the consistency between the parts of the program. For instance, the typing rule for the IF construct introduces meta variables to express that both branches must have the same type.

Here are the notations for meta type variables:

Typing rules

The system is syntax directed, which means here that it defines a single typing rule for each syntax construct. A typing rule restricts the type of input stacks that are authorized for this syntax construct, links the output type to the input type, and links both of them to the subexpressions when needed, using meta type variables.

Typing rules are of the form:

(syntax pattern)
:: (type of stack before) -> (type of stack after) [rule-name]
   iff (premises)

Where premises are typing requirements over subprograms or values in the stack, both of the form (x) :: (type), meaning that value (x) must have type (type).

A program is shown well-typed if one can find an instance of a rule that applies to the toplevel program expression, with all meta type variables replaced by non variable type expressions, and of which all type requirements in the premises can be proven well-typed in the same manner. For the reader unfamiliar with formal type systems, this is called building a typing derivation.

Here is an example typing derivation on a small program that computes (x+5)*10 for a given input x, obtained by instantiating the typing rules for instructions PUSH, ADD and for the sequence, as found in the next sections. When instantiating, we replace the iff with by.

{ PUSH nat 5 ; ADD ; PUSH nat 10 ; SWAP ; MUL }
:: [ nat : [] -> nat : [] ]
   by { PUSH nat 5 ; ADD }
      :: [ nat : [] -> nat : [] ]
         by PUSH nat 5
            :: [ nat : [] -> nat : nat : [] ]
               by 5 :: nat
        and ADD
            :: [ nat : nat : [] -> nat : [] ]
  and { PUSH nat 10 ; SWAP ; MUL }
      :: [ nat : [] -> nat : [] ]
         by PUSH nat 10
            :: [ nat : [] -> nat : nat : [] ]
               by 10 :: nat
        and { SWAP ; MUL }
            :: [ nat : nat : [] -> nat : [] ]
               by SWAP
                  :: [ nat : nat : [] -> nat : nat : [] ]
              and MUL
                  :: [ nat : nat : [] -> nat : [] ]

Producing such a typing derivation can be done in a number of manners, such as unification or abstract interpretation. In the implementation of Michelson, this is done by performing a recursive symbolic evaluation of the program on an abstract stack representing the input type provided by the programmer, and checking that the resulting symbolic stack is consistent with the expected result, also provided by the programmer.

Annotations

Most Instructions in the language can optionally take an annotation. Annotations allow you to better track data, on the stack and within pairs and unions.

If added on the components of a type, the annotation will be propagated by the typechecker througout access instructions.

Annotating an instruction that produces a value on the stack will rewrite the annotation an the toplevel of its type.

Trying to annotate an instruction that does not produce a value will result in a typechecking error.

At join points in the program (IF, IF_LEFT, IF_CONS, IF_NONE, LOOP), annotations must be compatible. Annotations are compatible if both elements are annotated with the same annotation or if at least one of the values/types is unannotated.

Stack visualization tools like the Michelson's Emacs mode print annotations associated with each type in the program, as propagated by the typechecker. This is useful as a debugging aid.

Side note

As with most type systems, it is incomplete. There are programs that cannot be given a type in this type system, yet that would not go wrong if executed. This is a necessary compromise to make the type system usable. Also, it is important to remember that the implementation of Michelson does not accept as many programs as the type system describes as well-typed. This is because the implementation uses a simple single pass typechecking algorithm, and does not handle any form of polymorphism.

III - Core data types and notations

IV - Core instructions

Control structures

Stack operations

Generic comparison

Comparison only works on a class of types that we call comparable. A COMPARE operation is defined in an ad hoc way for each comparable type, but the result of compare is always an int, which can in turn be checked in a generic manner using the following combinators. The result of COMPARE is 0 if the top two elements of the stack are equal, negative if the first element in the stack is less than the second, and positive otherwise.

V - Operations

Operations on booleans

Operations on integers and natural numbers

Integers and naturals are arbitrary-precision, meaning the only size limit is fuel.

Bitwise logical operators are also available on unsigned integers.

Operations on strings

Strings are mostly used for naming things without having to rely on external ID databases. So what can be done is basically use string constants as is, concatenate them and use them as keys.

Operations on pairs

Operations on sets

Operations on maps

Operations on optional values

Operations on unions

Operations on lists

VI - Domain specific data types

VII - Domain specific operations

Operations on timestamps

Timestamp immediates can be obtained by the NOW operation, or retrieved from script parameters or globals.

Operations on Tez

Tez are internally represented by a 64 bit signed integer. There are restrictions to prevent creating a negative amount of tez. Operations are limited to prevent overflow and mixing them with other numerical types by mistake. They are also mandatorily checked for under/overflows.

Operations on contracts

Special operations

Cryptographic primitives

VIII - Macros

In addition to the operations above, several extensions have been added to the language's concreate syntax. If you are interacting with the node via RPC, bypassing the client, which expands away these macros, you will need to desurgar them yourself.

These macros are designed to be unambiguous and reversable, meaning that errors are reported in terms of resugared syntax. Below you'll see these macros defined in terms of other syntactic forms. That is how these macros are seen by the node.

Compare

Syntactic sugar exists for merging COMPARE and comparison combinators, and also for branching.

Assertion Macros

All assertion operations are syntactic sugar for conditionals with a FAIL instruction in the appropriate branch. When possible, use them to increase clarity about illegal states.

Syntactic Conveniences

These are macros are simply more convenient syntax for various common operations.

IX - Concrete syntax

The concrete language is very close to the formal notation of the specification. Its structure is extremely simple: an expression in the language can only be one of the four following constructs.

  1. An integer.
  2. A character string.
  3. The application of a primitive to a sequence of expressions.
  4. A sequence of expressions.

This simple four cases notation is called Micheline.

Constants

There are two kinds of constants:

  1. Integers or naturals in decimal (no prefix), hexadecimal (0x prefix), octal (0o prefix) or binary (0b prefix).
  2. Strings with usual escapes \n, \t, \b, \r, \\, \". The encoding of a Michelson source file must be UTF-8, and non-ASCII characters can only appear in comments. No line break can appear in a string. Any non-printable characters must be escaped using two hexadecimal characters, as in '\xHH' or the predefine escape sequences above..

Primitive applications

A primitive application is a name followed by arguments

prim arg1 arg2

When a primitive application is the argument to another primitive application, it must be wrapped with parentheses.

prim (prim1 arg11 arg12) (prim2 arg21 arg22)

Sequences

Successive expression can be grouped as a single sequence expression using curly braces as delimiters and semicolon as separators.

{ expr1 ; expr2 ; expr3 ; expr4 }

A sequence can be passed as argument to a primitive.

prim arg1 arg2 { arg3_expr1 ; arg3_expr2 }

Primitive applications right inside a sequence cannot be wrapped.

{ (prim arg1 arg2) } # is not ok

Indentation

To remove ambiguities for human readers, the parser enforces some indentation rules.

Annotations

Sequences and primitive applications can receive an annotation.

An annotation is a lowercase identifier that starts with an @ sign. It comes after the opening curly brace for sequence, and after the primitive name for primitive applications.

{ @annot
  expr ;
  expr ;
  ... }

(prim @annot arg arg ...)

Differences with the formal notation

The concrete syntax follows the same lexical conventions as the specification: instructions are represented by uppercase identifiers, type constructors by lowercase identifiers, and constant constructors are Capitalized.

Lists can be written in a single shot instead of a succession of Cons

(List 1 2 3) = (Cons 1 (Cons 2 (Cons 3 Nil)))

All domain specific constants are Micheline strings with specific formats:

To prevent errors, control flow primitives that take instructions as parameters require sequences in the concrete syntax.

IF { instr1_true ; instr2_true ; ... }
   { instr1_false ; instr2_false ; ... }

Main program structure

The toplevel of a smart contract file must be an undelimited sequence of four primitive applications (in no particular order) that provide its parameter, return and storage types, as well as its code.

See the next section for a concrete example.

Comments

A hash sign (#) anywhere outside of a string literal will make the rest of the line (and itself) completely ignored, as in the following example.

{ PUSH nat 1 ; # pushes 1
  PUSH nat 2 ; # pushes 2
  ADD }        # computes 2 + 1

Comments that span on multiple lines or that stop before the end of the line can also be written, using C-like delimiters (/* ... */).

X - JSON syntax

Micheline expressions are encoded in JSON like this:

As in the concrete syntax, all domain specific constants are encoded as strings.

XI - Examples

Contracts in the system are stored as a piece of code and a global data storage. The type of the global data of the storage is fixed for each contract at origination time. This is ensured statically by checking on origination that the code preserves the type of the global data. For this, the code of the contract is checked to be of the following type lambda (pair 'arg 'global) -> (pair 'ret 'global) where 'global is the type of the original global store given on origination. The contract also takes a parameter and returns a value, hence the complete calling convention above.

Empty contract

Any contract with the same parameter and return types may be written with an empty sequence in its code section. The simplest contract is the contract for which the parameter, storage, and return are all of type unit. This contract is as follows:

code {};
storage unit;
parameter unit;
return unit;

Reservoir contract

We want to create a contract that stores tez until a timestamp T or a maximum amount N is reached. Whenever N is reached before T, all tokens are reversed to an account B (and the contract is automatically deleted). Any call to the contract's code performed after T will otherwise transfer the tokens to another account A.

We want to build this contract in a reusable manner, so we do not hard-code the parameters. Instead, we assume that the global data of the contract are (Pair (Pair T N) (Pair A B)).

Hence, the global data of the contract has the following type

'g =
  pair
    (pair timestamp tez)
    (pair (contract unit unit) (contract unit unit))

Following the contract calling convention, the code is a lambda of type

lambda
  (pair unit 'g)
  (pair unit 'g)

written as

lambda
  (pair
     unit
     (pair
       (pair timestamp tez)
       (pair (contract unit unit) (contract unit unit))))
  (pair
     unit
     (pair
        (pair timestamp tez)
        (pair (contract unit unit) (contract unit unit))))

The complete source reservoir.tz is:

parameter timestamp ;
storage
  (pair
     (pair timestamp tez) # T N
     (pair (contract unit unit) (contract unit unit))) ; # A B
return unit ;
code
  { DUP ; CDAAR ; # T
    NOW ;
    COMPARE ; LE ;
    IF { DUP ; CDADR ; # N
         BALANCE ;
         COMPARE ; LE ;
         IF { CDR ; UNIT ; PAIR }
            { DUP ; CDDDR ; # B
              BALANCE ; UNIT ;
              DIIIP { CDR } ;
              TRANSFER_TOKENS ;
              PAIR } }
       { DUP ; CDDAR ; # A
         BALANCE ;
         UNIT ;
         DIIIP { CDR } ;
         TRANSFER_TOKENS ;
         PAIR } }

Reservoir contract (variant with broker and status)

We basically want the same contract as the previous one, but instead of destroying it, we want to keep it alive, storing a flag S so that we can tell afterwards if the tokens have been transferred to A or B. We also want a broker X to get some fee P in any case.

We thus add variables P and S and X to the global data of the contract, now (Pair (S, Pair (T, Pair (Pair P N) (Pair X (Pair A B))))). P is the fee for broker A, S is the state, as a string "open", "timeout" or "success".

At the beginning of the transaction:

 S is accessible via a CDAR
 T               via a CDDAR
 P               via a CDDDAAR
 N               via a CDDDADR
 X               via a CDDDDAR
 A               via a CDDDDDAR
 B               via a CDDDDDDR

For the contract to stay alive, we test that all least (Tez "1.00") is still available after each transaction. This value is given as an example and must be updated according to the actual Tezos minimal value for contract balance.

The complete source scrutable_reservoir.tz is:

parameter timestamp ;
storage
  (pair
     string # S
     (pair
        timestamp # T
        (pair
           (pair tez tez) ; # P N
           (pair
              (contract unit unit) # X
              (pair (contract unit unit) (contract unit unit)))))) ; # A B
return unit ;
code
  { DUP ; CDAR # S
    PUSH string "open" ;
    COMPARE ; NEQ ;
    IF { FAIL } # on "success", "timeout" or a bad init value
       { DUP ; CDDAR ; # T
         NOW ;
         COMPARE ; LT ;
         IF { # Before timeout
              # We compute ((1 + P) + N) tez for keeping the contract alive
              PUSH tez "1.00" ;
              DIP { DUP ; CDDDAAR } ; ADD ; # P
              DIP { DUP ; CDDDADR } ; ADD ; # N
              # We compare to the cumulated amount
              BALANCE ;
              COMPARE; LT ;
              IF { # Not enough cash, we just accept the transaction
                   # and leave the global untouched
                   CDR }
                 { # Enough cash, successful ending
                   # We update the global
                   CDDR ; PUSH string "success" ; PAIR ;
                   # We transfer the fee to the broker
                   DUP ; CDDAAR ; # P
                   DIP { DUP ; CDDDAR } # X
                   UNIT ; TRANSFER_TOKENS ; DROP ;
                   # We transfer the rest to A
                   DUP ; CDDADR ; # N
                   DIP { DUP ; CDDDDAR } # A
                   UNIT ; TRANSFER_TOKENS ; DROP } }
            { # After timeout, we refund
              # We update the global
              CDDR ; PUSH string "timeout" ; PAIR ;
              # We try to transfer the fee to the broker
              PUSH tez "1.00" ; BALANCE ; SUB ; # available
              DIP { DUP ; CDDAAR } ; # P
              COMPARE ; LT ; # available < P
              IF { PUSH tez "1.00" ; BALANCE ; SUB ; # available
                   DIP { DUP ; CDDDAR } # X
                   UNIT ; TRANSFER_TOKENS ; DROP }
                 { DUP ; CDDAAR ; # P
                   DIP { DUP ; CDDDAR } # X
                   UNIT ; TRANSFER_TOKENS ; DROP }
              # We transfer the rest to B
              PUSH tez "1.00" ; BALANCE ; SUB ; # available
              DIP { DUP ; CDDDDDR } # B
              UNIT ; TRANSFER_TOKENS ; DROP } }
    # return Unit
    UNIT ; PAIR }

Forward contract

We want to write a forward contract on dried peas. The contract takes as global data the tons of peas Q, the expected delivery date T, the contract agreement date Z, a strike K, a collateral C per ton of dried peas, and the accounts of the buyer B, the seller S and the warehouse W.

These parameters as grouped in the global storage as follows:

Pair
  (Pair (Pair Q (Pair T Z)))
  (Pair
     (Pair K C)
     (Pair (Pair B S) W))

of type

pair
  (pair nat (pair timestamp timestamp))
  (pair
     (pair tez tez)
     (pair (pair account account) account))

The 24 hours after timestamp Z are for the buyer and seller to store their collateral (Q * C). For this, the contract takes a string as parameter, matching "buyer" or "seller" indicating the party for which the tokens are transferred. At the end of this day, each of them can send a transaction to send its tokens back. For this, we need to store who already paid and how much, as a (pair tez tez) where the left component is the buyer and the right one the seller.

After the first day, nothing cam happen until T.

During the 24 hours after T, the buyer must pay (Q * K) to the contract, minus the amount already sent.

After this day, if the buyer didn't pay enough then any transaction will send all the tokens to the seller.

Otherwise, the seller must deliver at least Q tons of dried peas to the warehouse, in the next 24 hours. When the amount is equal to or exceeds Q, all the tokens are transferred to the seller and the contract is destroyed. For storing the quantity of peas already delivered, we add a counter of type nat in the global storage. For knowing this quantity, we accept messages from W with a partial amount of delivered peas as argument.

After this day, any transaction will send all the tokens to the buyer (not enough peas have been delivered in time).

Hence, the global storage is a pair, with the counters on the left, and the constant parameters on the right, initially as follows.

Pair
  (Pair 0 (Pair 0_00 0_00))
  (Pair
     (Pair (Pair Q (Pair T Z)))
     (Pair
        (Pair K C)
        (Pair (Pair B S) W)))

of type

pair
  (pair nat (pair tez tez))
  (pair
     (pair nat (pair timestamp timestamp))
     (pair
        (pair tez tez)
        (pair (pair account account) account)))

The parameter of the transaction will be either a transfer from the buyer or the seller or a delivery notification from the warehouse of type (or string nat).

At the beginning of the transaction:

Q is accessible via a CDDAAR
T               via a CDDADAR
Z               via a CDDADDR
K               via a CDDDAAR
C               via a CDDDADR
B               via a CDDDDAAR
S               via a CDDDDADR
W               via a CDDDDDR
the delivery counter via a CDAAR
the amount versed by the seller via a CDADDR
the argument via a CAR

The contract returns a unit value, and we assume that it is created with the minimum amount, set to (Tez "1.00").

The complete source forward.tz is:

parameter (or string nat) ;
return unit ;
storage
  (pair
     (pair nat (pair tez tez)) # counter from_buyer from_seller
     (pair
        (pair nat (pair timestamp timestamp)) # Q T Z
        (pair
           (pair tez tez) # K C
           (pair
              (pair (contract unit unit) (contract unit unit)) # B S
              (contract unit unit))))) ; # W
code
  { DUP ; CDDADDR ; # Z
    PUSH nat 86400 ; SWAP ; ADD ; # one day in second
    NOW ; COMPARE ; LT ;
    IF { # Before Z + 24
         DUP ; CAR ; # we must receive (Left "buyer") or (Left "seller")
         IF_LEFT
           { DUP ; PUSH string "buyer" ; COMPARE ; EQ ;
             IF { DROP ;
                  DUP ; CDADAR ; # amount already versed by the buyer
                  DIP { AMOUNT } ; ADD ; # transaction
                  #  then we rebuild the globals
                  DIP { DUP ; CDADDR } ; PAIR ; # seller amount
                  PUSH nat 0 ; PAIR ; # delivery counter at 0
                  DIP { CDDR } ; PAIR ; # parameters
                  # and return Unit
                  UNIT ; PAIR }
                { PUSH string "seller" ; COMPARE ; EQ ;
                  IF { DUP ; CDADDR ; # amount already versed by the seller
                       DIP { AMOUNT } ; ADD ; # transaction
                       #  then we rebuild the globals
                       DIP { DUP ; CDADAR } ; SWAP ; PAIR ; # buyer amount
                       PUSH nat 0 ; PAIR ; # delivery counter at 0
                       DIP { CDDR } ; PAIR ; # parameters
                       # and return Unit
                       UNIT ; PAIR }
                     { FAIL } } } # (Left _)
           { FAIL } } # (Right _)
       { # After Z + 24
         # test if the required amount is reached
         DUP ; CDDAAR ; # Q
         DIP { DUP ; CDDDADR } ; MUL ; # C
         PUSH nat 2 ; MUL ;
         PUSH tez "1.00" ; ADD ;
         BALANCE ; COMPARE ; LT ; # balance < 2 * (Q * C) + 1
         IF { # refund the parties
              CDR ; DUP ; CADAR ; # amount versed by the buyer
              DIP { DUP ; CDDDAAR } # B
              UNIT ; TRANSFER_TOKENS ; DROP
              DUP ; CADDR ; # amount versed by the seller
              DIP { DUP ; CDDDADR } # S
              UNIT ; TRANSFER_TOKENS ; DROP
              BALANCE ; # bonus to the warehouse to destroy the account
              DIP { DUP ; CDDDDR } # W
              UNIT ; TRANSFER_TOKENS ; DROP
              # return unit, don't change the global
              # since the contract will be destroyed
              UNIT ; PAIR }
            { # otherwise continue
              DUP ; CDDADAR # T
              NOW ; COMPARE ; LT
              IF { FAIL } # Between Z + 24 and T
                 { # after T
                   DUP ; CDDADAR # T
                   PUSH nat 86400 ; ADD # one day in second
                   NOW ; COMPARE ; LT
                   IF { # Between T and T + 24
                        # we only accept transactions from the buyer
                        DUP ; CAR ; # we must receive (Left "buyer")
                        IF_LEFT
                          { PUSH string "buyer" ; COMPARE ; EQ ;
                            IF { DUP ; CDADAR ; # amount already versed by the buyer
                                 DIP { AMOUNT } ; ADD ; # transaction
                                 # The amount must not exceed Q * K
                                 DUP ;
                                 DIIP { DUP ; CDDAAR ; # Q
                                        DIP { DUP ; CDDDAAR } ; MUL ; } ; # K
                                 DIP { COMPARE ; GT ; # new amount > Q * K
                                       IF { FAIL } { } } ; # abort or continue
                                 #  then we rebuild the globals
                                 DIP { DUP ; CDADDR } ; PAIR ; # seller amount
                                 PUSH nat 0 ; PAIR ; # delivery counter at 0
                                 DIP { CDDR } ; PAIR ; # parameters
                                 # and return Unit
                                 UNIT ; PAIR }
                               { FAIL } } # (Left _)
                          { FAIL } } # (Right _)
                      { # After T + 24
                        # test if the required payment is reached
                        DUP ; CDDAAR ; # Q
                        DIP { DUP ; CDDDAAR } ; MUL ; # K
                        DIP { DUP ; CDADAR } ; # amount already versed by the buyer
                        COMPARE ; NEQ ;
                        IF { # not reached, pay the seller and destroy the contract
                             BALANCE ;
                             DIP { DUP ; CDDDDADR } # S
                             DIIP { CDR } ;
                             UNIT ; TRANSFER_TOKENS ; DROP ;
                             # and return Unit
                             UNIT ; PAIR }
                           { # otherwise continue
                             DUP ; CDDADAR # T
                             PUSH nat 86400 ; ADD ;
                             PUSH nat 86400 ; ADD ; # two days in second
                             NOW ; COMPARE ; LT
                             IF { # Between T + 24 and T + 48
                                  # We accept only delivery notifications, from W
                                  DUP ; CDDDDDR ; MANAGER ; # W
                                  SOURCE unit unit ; MANAGER ;
                                  COMPARE ; NEQ ;
                                  IF { FAIL } {} # fail if not the warehouse
                                  DUP ; CAR ; # we must receive (Right amount)
                                  IF_LEFT
                                    { FAIL } # (Left _)
                                    { # We increment the counter
                                      DIP { DUP ; CDAAR } ; ADD ;
                                      # And rebuild the globals in advance
                                      DIP { DUP ; CDADR } ; PAIR ;
                                      DIP { CDDR } ; PAIR ;
                                      UNIT ; PAIR ;
                                      # We test if enough have been delivered
                                      DUP ; CDAAR ;
                                      DIP { DUP ; CDDAAR } ;
                                      COMPARE ; LT ; # counter < Q
                                      IF { CDR } # wait for more
                                         { # Transfer all the money to the seller
                                           BALANCE ; # and destroy the contract
                                           DIP { DUP ; CDDDDADR } # S
                                           DIIP { CDR } ;
                                           UNIT ; TRANSFER_TOKENS ; DROP } } ;
                                  UNIT ; PAIR }
                                { # after T + 48, transfer everything to the buyer
                                  BALANCE ; # and destroy the contract
                                  DIP { DUP ; CDDDDAAR } # B
                                  DIIP { CDR } ;
                                  UNIT ; TRANSFER_TOKENS ; DROP ;
                                  # and return unit
                                  UNIT ; PAIR } } } } } } }

XII - Full grammar

<data> ::=
  | <int constant>
  | <natural number constant>
  | <string constant>
  | <timestamp string constant>
  | <signature string constant>
  | <key string constant>
  | <key_hash string constant>
  | <tez string constant>
  | <contract string constant>
  | Unit
  | True
  | False
  | Pair <data> <data>
  | Left <data>
  | Right <data>
  | Some <data>
  | None
  | List <data> ...
  | Set <data> ...
  | Map (Item <data> <data>) ...
  | instruction
<instruction> ::=
  | { <instruction> ... }
  | DROP
  | DUP
  | SWAP
  | PUSH <type> <data>
  | SOME
  | NONE <type>
  | UNIT
  | IF_NONE { <instruction> ... } { <instruction> ... }
  | PAIR
  | CAR
  | CDR
  | LEFT <type>
  | RIGHT <type>
  | IF_LEFT { <instruction> ... } { <instruction> ... }
  | NIL <type>
  | CONS
  | IF_CONS { <instruction> ... } { <instruction> ... }
  | EMPTY_SET <type>
  | EMPTY_MAP <comparable type> <type>
  | MAP
  | MAP { <instruction> ... }
  | REDUCE
  | ITER { <instruction> ... }
  | MEM
  | GET
  | UPDATE
  | IF { <instruction> ... } { <instruction> ... }
  | LOOP { <instruction> ... }
  | LOOP_LEFT { <instruction> ... }
  | LAMBDA <type> <type> { <instruction> ... }
  | EXEC
  | DIP { <instruction> ... }
  | FAIL
  | CONCAT
  | ADD
  | SUB
  | MUL
  | DIV
  | ABS
  | NEG
  | MOD
  | LSL
  | LSR
  | OR
  | AND
  | XOR
  | NOT
  | COMPARE
  | EQ
  | NEQ
  | LT
  | GT
  | LE
  | GE
  | INT
  | MANAGER
  | SELF
  | TRANSFER_TOKENS
  | CREATE_ACCOUNT
  | CREATE_CONTRACT
  | DEFAULT_ACCOUNT
  | NOW
  | AMOUNT
  | BALANCE
  | CHECK_SIGNATURE
  | H
  | HASH_KEY
  | STEPS_TO_QUOTA
  | SOURCE <type> <type>
<type> ::=
  | <comparable type>
  | key
  | unit
  | signature
  | option <type>
  | list <type>
  | set <comparable type>
  | contract <type> <type>
  | pair <type> <type>
  | or <type> <type>
  | lambda <type> <type>
  | map <comparable type> <type>
<comparable type> ::=
  | int
  | nat
  | string
  | tez
  | bool
  | key_hash
  | timestamp

XIII - Reference implementation

The language is implemented in OCaml as follows:

Continue reading on github.com