MyNixOS website logo
Description

EDSL for the Michelson Language.

Lorentz is a powerful meta-programming tool which allows one to write Michelson contracts directly in Haskell. It has the same instructions as Michelson, but operates on Haskell values and allows one to use Haskell features.

:warning: Note: this project is deprecated.

It is no longer maintained since the activation of protocol "Nairobi" on the Tezos mainnet (June 24th, 2023).

Morley Lorentz EDSL

Hackage

Table of contents

Overview

Lorentz is a powerful meta-programming tool which allows one to write Michelson contracts directly in Haskell.

Haskell's type checker and automatic type inference facilitate contracts implementation and reduce boilerplate related to types. Adoption of Algebraic Data Types makes work with complex objects safe and convenient. Later Lorentz contract can be dumped as a plain textual Michelson contract using functions from Morley.Michelson.Printer.

As an addition, you can optimize the transpiled Michelson contract before printing using functions from Morley.Michelson.Optimizer. E.g. this optimizer will replace IF {} {} with DROP. For more possible optimizations please refer to the module mentioned earlier.

You can find Lorentz instructions in Lorentz modules.

Examples of using Lorentz eDSL reside in the morley-ledgers or tezos-btc repos. For more information, refer to that README's in the corresponding repositories.

Also, to get more information about Lorentz you can read our blogpost.

Writing contracts in Lorentz

Basically, Lorentz function is just the haskell function that transforms one stack to another, this can be presented as the following operator:

(inp :: [Type]) :-> (out :: [Type])

In order to list types on the stack, we will use : operator. For example, Natural : Integer : Bool : '[].

Such design provides a nice code decomposition ability.

Contract code in Lorentz is Lorentz function with specific type:

type ContractOut storage = '[([Operation], storage)]
type ContractCode parameter storage = '[(parameter, storage)] :-> ContractOut storage

Lorentz reimplements all existing instructions and macros from Michelson.

Apart from reimplementing existing Michelson functionality, Lorentz provides bunch of additional features:

  • Pattern-matching on ADTs.
data TrafficLight
  = Red
  | Yellow
  | Green
  deriving stock Generic
  deriving anyclass IsoValue

showTrafficLight :: TrafficLight : s :-> MText : s
showTrafficLight = caseT
  ( #cRed /-> push [mt|Red|]
  , #cYellow /-> push [mt|Yellow|]
  , #cGreen /-> push [mt|Green|]
  )
  • RebindableSyntax for Lorentz instructions, including do notation and if operator
sumIsGt10 :: Integer : Integer : s :-> Bool : s
sumIsGt10 = do
  add
  dip $ push @Integer 10
  gt

foo :: Integer : s :-> Integer : s
foo = do
  dup
  sumIsGt10
  if Holds
  then do push @Integer 1
  else do push @Integer -1
  • Records with getters.
data UserInfo = UserInfo
  { firstName :: MText
  , secondName :: MText
  , userId :: Natural
  } deriving stock Generic
    deriving anyclass IsoValue

getFullName :: UserInfo : s :-> MText : s
getFullName = do
  getField #firstName
  dip $ toField #secondName
  concat
  • Automatic contracts documentation generation.

    Lorentz provides primitives for embedding documentation in the contract code and functions to produce documentation in Markdown, they can be found in Lorentz.Doc and Morley.Michelson.Doc modules. Documentation examples can be found here.

Sample smart contract written in Lorentz

Example contract with autodoc and custom data-type tree:

data MeasurementMethod
  = ParrotStep
  | MonkeyStep
  | ElephantStep

$(customGeneric "MeasurementMethod" rightBalanced)

deriving anyclass instance IsoValue MeasurementMethod
deriving anyclass instance HasAnnotation MeasurementMethod

instance TypeHasDoc MeasurementMethod where
  typeDocName _ = "MeasurementMethod"
  typeDocMdDescription =
    "This type defines the way of measuring boa length. \
    \Single boa constrictor corresponds to 38 parrot steps, 31 monkey step \
    \and 9 elephant steps."

data Parameter
  = MeasureBoaConstrictor ("method" :! MeasurementMethod)
  | Zero ()
  deriving stock Generic
  deriving anyclass IsoValue

instance ParameterHasEntrypoints Parameter where
  type ParameterEntrypointsDerivation Parameter = EpdPlain

type Storage = Natural

measureBoaConstrictor :: ContractCode Parameter Storage
measureBoaConstrictor = docGroup (DName "Boa constrictor measurement") $ do
  doc $ DDescription "This contract measures boa constrictor."
  unpair
  dip drop
  entryCase @Parameter (Proxy @PlainEntrypointsKind)
    ( #cMeasureBoaConstrictor /-> do
        doc $ DDescription "Measure the boa constrictor with given method."
        fromNamed #method
        caseT @MeasurementMethod
          ( #cParrotStep /-> push @Natural 38
          , #cMonkeyStep /-> push @Natural 31
          , #cElephantStep /-> push @Natural 9
          )
    , #cZero /-> do
        doc $ DDescription "Put zero to the storage."
        drop; push @Natural 0
    )
  nil; pair

Generated documentation

Generated documentation for this contract can be found here.

Transpiled Michelson contract

parameter (or (or :method %measureBoaConstrictor unit
                                                 (or unit
                                                     unit))
              (unit %zero));
storage nat;
code { CAST (pair (or (or unit (or unit unit)) unit) nat);
       DUP;
       CAR;
       DIP { CDR };
       DIP { DROP };
       IF_LEFT { IF_LEFT { DROP;
                           PUSH nat 38 }
                         { IF_LEFT { DROP;
                                     PUSH nat 31 }
                                   { DROP;
                                     PUSH nat 9 } } }
               { DROP;
                 PUSH nat 0 };
       NIL operation;
       PAIR };

FAQ

  • Q: I added a new parameter case to contract and GHC went mad.

    A: Ensure that your number of entry points does not exceed the limit set in Morley.Util.TypeTuple.Instances.

  • Q: I added one more datatype that is used in the contract and GHC reports with errors related to Rep type family.

    A: Make sure your datatype derives Generic instance and all primitive types used in it have IsPrimitiveValue set to True.

Metadata

Version

0.16.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows