-- SPDX-FileCopyrightText: 2020 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-TQ

-- | Duplication of Backend functions, but without input and output stack.

module Indigo.Frontend.Language
  ( -- * Assignment and modifications
    new
  , setVar
  , setField
  , (+=)
  , (-=)
  , (*=)
  , (<<<=)
  , (>>>=)
  , (&&=)
  , (||=)
  , (^=)
  , (=:)

  -- * Storage Fields
  , getStorageField
  , setStorageField
  , updateStorageField

  -- * Conditional
  , if_
  , when
  , unless
  , ifSome
  , ifNone
  , whenSome
  , whenNone
  , ifRight
  , ifLeft
  , whenRight
  , whenLeft
  , ifCons

  -- * Case
  , case_
  , caseRec
  , entryCase
  , entryCaseRec
  , entryCaseSimple
  , (//->)
  , (#=)

  -- * Scope
  , scope
  , defFunction
  , defContract
  , defNamedPureLambda1
  , defNamedLambda1
  , defNamedLambda0
  , defNamedEffLambda1

  -- * Loop
  , while
  , whileLeft
  , forEach

  -- * Contract call
  , selfCalling
  , contractCalling

  -- * Documentation
  , doc
  , docGroup
  , docStorage
  , contractName
  , contractGeneral
  , contractGeneralDefault
  , finalizeParamCallingDoc

  -- * Short-handed doc item
  , anchor
  , description
  , example

  -- * Side-effects operations
  , transferTokens
  , setDelegate
  , createContract
  , createLorentzContract

  -- * Failures
  , failWith
  , assert
  , failCustom
  , failCustom_
  , failUnexpected_
  , assertCustom
  , assertCustom_

  -- * Comments
  , comment
  , justComment
  , commentAroundFun
  , commentAroundStmt

  -- * Blocks
  , IndigoFunction
  , IndigoProcedure
  , IndigoEntrypoint

  -- * Helpers
  , liftIndigoState
  ) where

import qualified Indigo.Backend as B
import Indigo.Backend.Case hiding (caseRec, entryCaseRec)
import Indigo.Backend.Lambda
import Indigo.Backend.Scope
import Indigo.Compilation (compileIndigoContract)
import Indigo.Frontend.Program
import Indigo.Frontend.Statement
import Indigo.Internal hiding (SetField, return, (>>), (>>=))
import Indigo.Lorentz
import Indigo.Prelude
import Lorentz.Entrypoints.Helpers (RequireSumType)
import qualified Lorentz.Instr as L
import qualified Lorentz.Run as L
import qualified Michelson.Typed as MT
import qualified Michelson.Typed.Arith as M
import Michelson.Typed.Haskell.Instr.Sum (CaseClauseParam(..), CtorField(..))
import Util.Markdown (toAnchor)
import Util.TypeLits (AppendSymbol)
import Util.TypeTuple.Class

oneIndigoM :: StatementF IndigoM a -> IndigoM a
oneIndigoM :: StatementF IndigoM a -> IndigoM a
oneIndigoM st :: StatementF IndigoM a
st = Program (StatementF IndigoM) a -> IndigoM a
forall a. Program (StatementF IndigoM) a -> IndigoM a
IndigoM (StatementF IndigoM a -> Program (StatementF IndigoM) a
forall (instr :: * -> *) a. instr a -> Program instr a
Instr StatementF IndigoM a
st)

liftIndigoState :: (forall inp. SomeIndigoState inp a) -> IndigoM a
liftIndigoState :: (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState code :: forall (inp :: [*]). SomeIndigoState inp a
code = Program (StatementF IndigoM) a -> IndigoM a
forall a. Program (StatementF IndigoM) a -> IndigoM a
IndigoM (StatementF IndigoM a -> Program (StatementF IndigoM) a
forall (instr :: * -> *) a. instr a -> Program instr a
Instr (StatementF IndigoM a -> Program (StatementF IndigoM) a)
-> StatementF IndigoM a -> Program (StatementF IndigoM) a
forall a b. (a -> b) -> a -> b
$ (forall (inp :: [*]). SomeIndigoState inp a)
-> StatementF IndigoM a
forall a (freer :: * -> *).
(forall (inp :: [*]). SomeIndigoState inp a) -> StatementF freer a
LiftIndigoState forall (inp :: [*]). SomeIndigoState inp a
code)

varModification
  :: (IsExpr ey y, IsObject x)
  => ([y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification :: ('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification act :: '[y, x] :-> '[x]
act v :: Var x
v = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (ey -> StatementF IndigoM ()) -> ey -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ('[y, x] :-> '[x]) -> Var x -> Expr y -> StatementF IndigoM ()
forall x y (freer :: * -> *).
(IsObject x, KnownValue y) =>
('[y, x] :-> '[x]) -> Var x -> Expr y -> StatementF freer ()
VarModification '[y, x] :-> '[x]
act Var x
v (Expr y -> StatementF IndigoM ())
-> (ey -> Expr y) -> ey -> StatementF IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ey -> Expr y
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

----------------------------------------------------------------------------
-- Var creation and assignment
----------------------------------------------------------------------------

-- | Create a new variable with the result of the given expression as its initial value.
new :: IsExpr ex x => ex -> IndigoM (Var x)
new :: ex -> IndigoM (Var x)
new = StatementF IndigoM (Var x) -> IndigoM (Var x)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (Var x) -> IndigoM (Var x))
-> (ex -> StatementF IndigoM (Var x)) -> ex -> IndigoM (Var x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr x -> StatementF IndigoM (Var x)
forall x (freer :: * -> *).
KnownValue x =>
Expr x -> StatementF freer (Var x)
NewVar (Expr x -> StatementF IndigoM (Var x))
-> (ex -> Expr x) -> ex -> StatementF IndigoM (Var x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr x
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

-- | Set the given variable to the result of the given expression.
setVar :: (IsExpr ex x) => Var x -> ex -> IndigoM ()
setVar :: Var x -> ex -> IndigoM ()
setVar v :: Var x
v = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (ex -> StatementF IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var x -> Expr x -> StatementF IndigoM ()
forall x (freer :: * -> *). Var x -> Expr x -> StatementF freer ()
SetVar Var x
v (Expr x -> StatementF IndigoM ())
-> (ex -> Expr x) -> ex -> StatementF IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr x
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

infixr 0 =:
(=:) :: IsExpr ex x => Var x -> ex -> IndigoM ()
v :: Var x
v =: :: Var x -> ex -> IndigoM ()
=: e :: ex
e = Var x -> ex -> IndigoM ()
forall ex x. IsExpr ex x => Var x -> ex -> IndigoM ()
setVar Var x
v ex
e

setField
  :: ( ex :~> ftype
     , IsObject dt
     , IsObject ftype
     , HasField dt fname ftype
     )
  => Var dt -> Label fname -> ex -> IndigoM ()
setField :: Var dt -> Label fname -> ex -> IndigoM ()
setField v :: Var dt
v fName :: Label fname
fName = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (ex -> StatementF IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var dt -> Label fname -> Expr ftype -> StatementF IndigoM ()
forall dt ftype (arg :: Symbol) (cont :: * -> *).
(IsObject dt, IsObject ftype, HasField dt arg ftype) =>
Var dt -> Label arg -> Expr ftype -> StatementF cont ()
SetField Var dt
v Label fname
fName (Expr ftype -> StatementF IndigoM ())
-> (ex -> Expr ftype) -> ex -> StatementF IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr ftype
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

(+=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Add n m, ArithResHs M.Add n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
+= :: Var m -> ex1 -> IndigoM ()
(+=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Add n m =>
(n & (m & s)) :-> (ArithResHs Add n m & s)
L.add

(-=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Sub n m, ArithResHs M.Sub n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
-= :: Var m -> ex1 -> IndigoM ()
(-=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Sub n m =>
(n & (m & s)) :-> (ArithResHs Sub n m & s)
L.sub

(*=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Mul n m, ArithResHs M.Mul n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
*= :: Var m -> ex1 -> IndigoM ()
(*=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Mul n m =>
(n & (m & s)) :-> (ArithResHs Mul n m & s)
L.mul

(||=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Or n m, ArithResHs M.Or n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
||= :: Var m -> ex1 -> IndigoM ()
(||=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Or n m =>
(n & (m & s)) :-> (ArithResHs Or n m & s)
L.or

(&&=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.And n m, ArithResHs M.And n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
&&= :: Var m -> ex1 -> IndigoM ()
(&&=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs And n m =>
(n & (m & s)) :-> (ArithResHs And n m & s)
L.and

(^=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Xor n m, ArithResHs M.Xor n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
^= :: Var m -> ex1 -> IndigoM ()
(^=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Xor n m =>
(n & (m & s)) :-> (ArithResHs Xor n m & s)
L.xor

(<<<=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Lsl n m, ArithResHs M.Lsl n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
<<<= :: Var m -> ex1 -> IndigoM ()
(<<<=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Lsl n m =>
(n & (m & s)) :-> (ArithResHs Lsl n m & s)
L.lsl

(>>>=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Lsr n m, ArithResHs M.Lsr n m ~ m
     ) => Var m -> ex1 -> IndigoM ()
>>>= :: Var m -> ex1 -> IndigoM ()
(>>>=) = ('[n, m] :-> '[m]) -> Var m -> ex1 -> IndigoM ()
forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[n, m] :-> '[m]
forall n m (s :: [*]).
ArithOpHs Lsr n m =>
(n & (m & s)) :-> (ArithResHs Lsr n m & s)
L.lsr

----------------------------------------------------------------------------
-- Storage Fields
----------------------------------------------------------------------------

-- | Sets a storage field to a new value.
setStorageField
  :: forall store name ftype ex.
     ( HasStorage store
     , ex :~> ftype
     , IsObject store
     , IsObject ftype
     , HasField store name ftype
     )
  => Label name -> ex -> IndigoM ()
setStorageField :: Label name -> ex -> IndigoM ()
setStorageField field :: Label name
field expr :: ex
expr = Var store -> Label name -> ex -> IndigoM ()
forall ex ftype dt (fname :: Symbol).
(ex :~> ftype, IsObject dt, IsObject ftype,
 HasField dt fname ftype) =>
Var dt -> Label fname -> ex -> IndigoM ()
setField (HasStorage store => Var store
forall st. HasStorage st => Var st
storageVar @store) Label name
field ex
expr

-- | Updates a storage field by using an updating 'IndigoM'.
updateStorageField
  :: forall store ftype fname fex.
     ( HasStorage store
     , fex :~> ftype
     , HasField store fname ftype
     , IsObject store
     , IsObject ftype
     )
  => Label fname
  -> (Var ftype -> IndigoM fex)
  -> IndigoM ()
updateStorageField :: Label fname -> (Var ftype -> IndigoM fex) -> IndigoM ()
updateStorageField field :: Label fname
field upd :: Var ftype -> IndigoM fex
upd = IndigoM () -> IndigoM ()
forall a. ScopeCodeGen a => IndigoM a -> IndigoFunction a
scope (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ do
  let storage :: Var store
storage = HasStorage store => Var store
forall st. HasStorage st => Var st
storageVar @store
  Var ftype
fieldVar <- Expr ftype -> IndigoM (Var ftype)
forall ex x. IsExpr ex x => ex -> IndigoM (Var x)
new(Expr ftype -> IndigoM (Var ftype))
-> Expr ftype -> IndigoM (Var ftype)
forall a b. (a -> b) -> a -> b
$ Var store
storage Var store -> Label fname -> Expr ftype
forall dt (name :: Symbol) ftype exDt.
(HasField dt name ftype, exDt :~> dt) =>
exDt -> Label name -> Expr ftype
#! Label fname
field
  fex
expr <- Var ftype -> IndigoM fex
upd Var ftype
fieldVar
  Var store -> Label fname -> fex -> IndigoM ()
forall ex ftype dt (fname :: Symbol).
(ex :~> ftype, IsObject dt, IsObject ftype,
 HasField dt fname ftype) =>
Var dt -> Label fname -> ex -> IndigoM ()
setField Var store
storage Label fname
field fex
expr

-- | Get a field from the storage, returns a variable.
--
-- Note that the storage type almost always needs to be specified.
getStorageField
  :: forall store ftype fname .
     ( HasStorage store
     , HasField store fname ftype
     )
  => Label fname -> IndigoM (Var ftype)
getStorageField :: Label fname -> IndigoM (Var ftype)
getStorageField field :: Label fname
field = Expr ftype -> IndigoM (Var ftype)
forall ex x. IsExpr ex x => ex -> IndigoM (Var x)
new(Expr ftype -> IndigoM (Var ftype))
-> Expr ftype -> IndigoM (Var ftype)
forall a b. (a -> b) -> a -> b
$ HasStorage store => Var store
forall st. HasStorage st => Var st
storageVar @store Var store -> Label fname -> Expr ftype
forall dt (name :: Symbol) ftype exDt.
(HasField dt name ftype, exDt :~> dt) =>
exDt -> Label name -> Expr ftype
#! Label fname
field

----------------------------------------------------------------------------
-- Conditional
----------------------------------------------------------------------------

if_
  :: forall a b ex . (IfConstraint a b, ex :~> Bool)
  => ex
  -> IndigoM a
  -> IndigoM b
  -> IndigoM (RetVars a)
if_ :: ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ ex :: ex
ex tb :: IndigoM a
tb fb :: IndigoM b
fb = StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
 -> IndigoM (RetVars a))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a b. (a -> b) -> a -> b
$ Expr Bool
-> IndigoM a -> IndigoM b -> StatementF IndigoM (RetVars a)
forall a b (freer :: * -> *).
IfConstraint a b =>
Expr Bool -> freer a -> freer b -> StatementF freer (RetVars a)
If (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
ex) IndigoM a
tb IndigoM b
fb

-- | Run the instruction when the condition is met, do nothing otherwise.
when :: (exc :~> Bool) => exc -> IndigoM () -> IndigoM ()
when :: exc -> IndigoM () -> IndigoM ()
when cond :: exc
cond expr :: IndigoM ()
expr = exc -> IndigoM () -> IndigoM () -> IndigoM (RetVars ())
forall a b ex.
(IfConstraint a b, ex :~> Bool) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ exc
cond IndigoM ()
expr (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

-- | Reverse of 'when'.
unless :: (exc :~> Bool) => exc -> IndigoM () -> IndigoM ()
unless :: exc -> IndigoM () -> IndigoM ()
unless cond :: exc
cond expr :: IndigoM ()
expr = exc -> IndigoM () -> IndigoM () -> IndigoM (RetVars ())
forall a b ex.
(IfConstraint a b, ex :~> Bool) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ exc
cond (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) IndigoM ()
expr

ifSome
  :: forall x a b ex . (KnownValue x, ex :~> Maybe x, IfConstraint a b)
  => ex
  -> (Var x -> IndigoM a)
  -> IndigoM b
  -> IndigoM (RetVars a)
ifSome :: ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome ex :: ex
ex tb :: Var x -> IndigoM a
tb fb :: IndigoM b
fb = StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
 -> IndigoM (RetVars a))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a b. (a -> b) -> a -> b
$ Expr (Maybe x)
-> (Var x -> IndigoM a)
-> IndigoM b
-> StatementF IndigoM (RetVars a)
forall a b a (freer :: * -> *).
(IfConstraint a b, KnownValue a) =>
Expr (Maybe a)
-> (Var a -> freer a) -> freer b -> StatementF freer (RetVars a)
IfSome (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
ex) Var x -> IndigoM a
tb IndigoM b
fb

ifNone
  :: forall x a b ex . (KnownValue x, ex :~> Maybe x, IfConstraint a b)
  => ex
  -> IndigoM b
  -> (Var x -> IndigoM a)
  -> IndigoM (RetVars a)
ifNone :: ex -> IndigoM b -> (Var x -> IndigoM a) -> IndigoM (RetVars a)
ifNone ex :: ex
ex fb :: IndigoM b
fb tb :: Var x -> IndigoM a
tb = Expr (Maybe x)
-> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
ex) Var x -> IndigoM a
tb IndigoM b
fb

-- | Run the instruction when the given expression returns 'Just' a value,
-- do nothing otherwise.
whenSome
  :: forall x exa .
     ( KnownValue x
     , exa :~> Maybe x
     )
  => exa
  -> (Var x -> IndigoM ())
  -> IndigoM ()
whenSome :: exa -> (Var x -> IndigoM ()) -> IndigoM ()
whenSome c :: exa
c f :: Var x -> IndigoM ()
f = exa -> (Var x -> IndigoM ()) -> IndigoM () -> IndigoM (RetVars ())
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome exa
c Var x -> IndigoM ()
f (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

-- | Run the instruction when the given expression returns 'Nothing',
-- do nothing otherwise.
whenNone
  :: forall x exa .
     ( KnownValue x
     , exa :~> Maybe x
     )
  => exa
  -> IndigoM ()
  -> IndigoM ()
whenNone :: exa -> IndigoM () -> IndigoM ()
whenNone c :: exa
c f :: IndigoM ()
f = exa -> (Var x -> IndigoM ()) -> IndigoM () -> IndigoM (RetVars ())
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome exa
c (\_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) IndigoM ()
f

ifRight
  :: forall x y a b ex .
     ( KnownValue x
     , KnownValue y
     , ex :~> Either y x
     , IfConstraint a b
     )
  => ex
  -> (Var x -> IndigoM a)
  -> (Var y -> IndigoM b)
  -> IndigoM (RetVars a)
ifRight :: ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex :: ex
ex rb :: Var x -> IndigoM a
rb lb :: Var y -> IndigoM b
lb = StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
 -> IndigoM (RetVars a))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a b. (a -> b) -> a -> b
$ Expr (Either y x)
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> StatementF IndigoM (RetVars a)
forall a b x y (freer :: * -> *).
(IfConstraint a b, KnownValue x, KnownValue y) =>
Expr (Either y x)
-> (Var x -> freer a)
-> (Var y -> freer b)
-> StatementF freer (RetVars a)
IfRight (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
ex) Var x -> IndigoM a
rb Var y -> IndigoM b
lb

ifLeft
  :: forall x y a b ex .
     ( KnownValue x
     , KnownValue y
     , ex :~> Either y x
     , IfConstraint a b
     )
  => ex
  -> (Var y -> IndigoM b)
  -> (Var x -> IndigoM a)
  -> IndigoM (RetVars a)
ifLeft :: ex
-> (Var y -> IndigoM b)
-> (Var x -> IndigoM a)
-> IndigoM (RetVars a)
ifLeft ex :: ex
ex lb :: Var y -> IndigoM b
lb rb :: Var x -> IndigoM a
rb = ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x,
 IfConstraint a b) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
ex Var x -> IndigoM a
rb Var y -> IndigoM b
lb

whenRight
  :: forall x y ex .
     ( KnownValue x
     , KnownValue y
     , ex :~> Either y x
     )
  => ex
  -> (Var x -> IndigoM ())
  -> IndigoM ()
whenRight :: ex -> (Var x -> IndigoM ()) -> IndigoM ()
whenRight c :: ex
c f :: Var x -> IndigoM ()
f = ex
-> (Var x -> IndigoM ())
-> (Var y -> IndigoM ())
-> IndigoM (RetVars ())
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x,
 IfConstraint a b) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
c Var x -> IndigoM ()
f (\_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

whenLeft
  :: forall x y ex .
     ( KnownValue x
     , KnownValue y
     , ex :~> Either y x
     )
  => ex
  -> (Var y -> IndigoM ())
  -> IndigoM ()
whenLeft :: ex -> (Var y -> IndigoM ()) -> IndigoM ()
whenLeft c :: ex
c f :: Var y -> IndigoM ()
f = ex
-> (Var x -> IndigoM ())
-> (Var y -> IndigoM ())
-> IndigoM (RetVars ())
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x,
 IfConstraint a b) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
c (\_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) Var y -> IndigoM ()
f

ifCons
  :: forall x a b ex . (KnownValue x, ex :~> List x, IfConstraint a b)
  => ex
  -> (Var x -> Var (List x) -> IndigoM a)
  -> IndigoM b
  -> IndigoM (RetVars a)
ifCons :: ex
-> (Var x -> Var (List x) -> IndigoM a)
-> IndigoM b
-> IndigoM (RetVars a)
ifCons ex :: ex
ex tb :: Var x -> Var (List x) -> IndigoM a
tb fb :: IndigoM b
fb = StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
 -> IndigoM (RetVars a))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue b) b)
-> IndigoM (RetVars a)
forall a b. (a -> b) -> a -> b
$ Expr (List x)
-> (Var x -> Var (List x) -> IndigoM a)
-> IndigoM b
-> StatementF IndigoM (RetVars a)
forall a b x (freer :: * -> *).
(IfConstraint a b, KnownValue x) =>
Expr (List x)
-> (Var x -> Var (List x) -> freer a)
-> freer b
-> StatementF freer (RetVars a)
IfCons (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
ex) Var x -> Var (List x) -> IndigoM a
tb IndigoM b
fb

----------------------------------------------------------------------------
-- Case
----------------------------------------------------------------------------

-- | A case statement for indigo. See examples for a sample usage.
caseRec
  :: forall dt guard ret clauses .
     ( CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses
     , guard :~> dt
     )
  => guard
  -> clauses
  -> IndigoM (RetVars ret)
caseRec :: guard -> clauses -> IndigoM (RetVars ret)
caseRec g :: guard
g = StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret))
-> (clauses -> StatementF IndigoM (RetVars ret))
-> clauses
-> IndigoM (RetVars ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr dt -> clauses -> StatementF IndigoM (RetVars ret)
forall (freer :: * -> *) dt ret dt.
CaseCommonF (IndigoMCaseClauseL freer) dt ret dt =>
Expr dt -> dt -> StatementF freer (RetVars ret)
Case (guard -> Expr (ExprType guard)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr guard
g)

-- | 'caseRec' for tuples.
case_
  :: forall dt guard ret clauses.
     ( CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses
     , RecFromTuple clauses
     , guard :~> dt
     )
  => guard
  -> IsoRecTuple clauses
  -> IndigoM (RetVars ret)
case_ :: guard -> IsoRecTuple clauses -> IndigoM (RetVars ret)
case_ g :: guard
g = Expr dt -> clauses -> IndigoM (RetVars ret)
forall dt guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 guard :~> dt) =>
guard -> clauses -> IndigoM (RetVars ret)
caseRec (guard -> Expr (ExprType guard)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr guard
g) (clauses -> IndigoM (RetVars ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep dt)))
    -> clauses)
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep dt)))
-> IndigoM (RetVars ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecFromTuple clauses => IsoRecTuple clauses -> clauses
forall r. RecFromTuple r => IsoRecTuple r -> r
recFromTuple @clauses


-- | 'caseRec' for pattern-matching on parameter.
entryCaseRec
  :: forall dt entrypointKind guard ret clauses .
     ( CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses
     , DocumentEntrypoints entrypointKind dt
     , guard :~> dt
     )
  => Proxy entrypointKind
  -> guard
  -> clauses
  -> IndigoM (RetVars ret)
entryCaseRec :: Proxy entrypointKind -> guard -> clauses -> IndigoM (RetVars ret)
entryCaseRec proxy :: Proxy entrypointKind
proxy g :: guard
g cls :: clauses
cls = StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret))
-> StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret)
forall a b. (a -> b) -> a -> b
$ Proxy entrypointKind
-> Expr dt -> clauses -> StatementF IndigoM (RetVars ret)
forall (freer :: * -> *) dt ret clauses entrypointKind.
(CaseCommonF (IndigoMCaseClauseL freer) dt ret clauses,
 DocumentEntrypoints entrypointKind dt) =>
Proxy entrypointKind
-> Expr dt -> clauses -> StatementF freer (RetVars ret)
EntryCase Proxy entrypointKind
proxy (guard -> Expr (ExprType guard)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr guard
g) clauses
cls

-- | 'entryCaseRec' for tuples.
entryCase
  :: forall dt entrypointKind guard ret clauses .
     ( CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses
     , RecFromTuple clauses
     , DocumentEntrypoints entrypointKind dt
     , guard :~> dt
     )
  => Proxy entrypointKind
  -> guard
  -> IsoRecTuple clauses
  -> IndigoM (RetVars ret)
entryCase :: Proxy entrypointKind
-> guard -> IsoRecTuple clauses -> IndigoM (RetVars ret)
entryCase proxy :: Proxy entrypointKind
proxy g :: guard
g = Proxy entrypointKind -> guard -> clauses -> IndigoM (RetVars ret)
forall dt entrypointKind guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 DocumentEntrypoints entrypointKind dt, guard :~> dt) =>
Proxy entrypointKind -> guard -> clauses -> IndigoM (RetVars ret)
entryCaseRec Proxy entrypointKind
proxy guard
g (clauses -> IndigoM (RetVars ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep dt)))
    -> clauses)
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep dt)))
-> IndigoM (RetVars ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecFromTuple clauses => IsoRecTuple clauses -> clauses
forall r. RecFromTuple r => IsoRecTuple r -> r
recFromTuple @clauses

entryCaseSimple
  :: forall cp guard ret clauses .
     ( CaseCommonF (IndigoMCaseClauseL IndigoM) cp ret clauses
     , RecFromTuple clauses
     , DocumentEntrypoints PlainEntrypointsKind cp
     , NiceParameterFull cp
     , RequireFlatParamEps cp
     , guard :~> cp
     )
  => guard
  -> IsoRecTuple clauses
  -> IndigoM (RetVars ret)
entryCaseSimple :: guard -> IsoRecTuple clauses -> IndigoM (RetVars ret)
entryCaseSimple g :: guard
g = StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars ret) -> IndigoM (RetVars ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep cp)))
    -> StatementF IndigoM (RetVars ret))
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep cp)))
-> IndigoM (RetVars ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr cp -> clauses -> StatementF IndigoM (RetVars ret)
forall (freer :: * -> *) cp ret x.
(CaseCommonF (IndigoMCaseClauseL freer) cp ret x,
 DocumentEntrypoints PlainEntrypointsKind cp, NiceParameterFull cp,
 RequireFlatParamEps cp) =>
Expr cp -> x -> StatementF freer (RetVars ret)
EntryCaseSimple (guard -> Expr (ExprType guard)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr guard
g) (clauses -> StatementF IndigoM (RetVars ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep cp)))
    -> clauses)
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (GCaseClauses (Rep cp)))
-> StatementF IndigoM (RetVars ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecFromTuple clauses => IsoRecTuple clauses -> clauses
forall r. RecFromTuple r => IsoRecTuple r -> r
recFromTuple @clauses

{-# DEPRECATED (//->) "use '#=' instead" #-}
-- | An alias for '#=' kept only for backward compatibility.
(//->)
  :: ( CaseArrow name (Var x -> IndigoAnyOut x ret)
                      (IndigoCaseClauseL ret ('CaseClauseParam ctor ('OneField x)))
     , ScopeCodeGen retBr
     , ret ~ RetExprs retBr
     , RetOutStack ret ~ RetOutStack retBr
     , KnownValue x
     , name ~ (AppendSymbol "c" ctor)
     )
  => Label name
  -> (Var x -> IndigoM retBr)
  -> IndigoMCaseClauseL IndigoM ret ('CaseClauseParam ctor ('OneField x))
//-> :: Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
(//->) cName :: Label name
cName b :: Var x -> IndigoM retBr
b = Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
forall (name :: Symbol) x ret (ctor :: Symbol) retBr
       (freer :: * -> *).
(CaseArrow
   name
   (Var x -> IndigoAnyOut x ret)
   (IndigoCaseClauseL ret ('CaseClauseParam ctor ('OneField x))),
 name ~ AppendSymbol "c" ctor, KnownValue x, ScopeCodeGen retBr,
 ret ~ RetExprs retBr, RetOutStack ret ~ RetOutStack retBr) =>
Label name
-> (Var x -> freer retBr)
-> IndigoMCaseClauseL
     freer ret ('CaseClauseParam ctor ('OneField x))
OneFieldIndigoMCaseClauseL Label name
cName Var x -> IndigoM retBr
b
infixr 0 //->

-- | Use this instead of '/->'.
--
-- This operator is like '/->' but wraps a body into 'IndigoAnyOut',
-- which is needed for two reasons: to allow having any output stack
-- and to allow returning not exactly the same values.
--
-- It has the added benefit of not being an arrow, so in case the body of the
-- clause is a lambda there won't be several.
(#=)
  :: ( CaseArrow name (Var x -> IndigoAnyOut x ret)
                      (IndigoCaseClauseL ret ('CaseClauseParam ctor ('OneField x)))
     , ScopeCodeGen retBr
     , ret ~ RetExprs retBr
     , RetOutStack ret ~ RetOutStack retBr
     , KnownValue x
     , name ~ (AppendSymbol "c" ctor)
     )
  => Label name
  -> (Var x -> IndigoM retBr)
  -> IndigoMCaseClauseL IndigoM ret ('CaseClauseParam ctor ('OneField x))
#= :: Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
(#=) cName :: Label name
cName b :: Var x -> IndigoM retBr
b = Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
forall (name :: Symbol) x ret (ctor :: Symbol) retBr
       (freer :: * -> *).
(CaseArrow
   name
   (Var x -> IndigoAnyOut x ret)
   (IndigoCaseClauseL ret ('CaseClauseParam ctor ('OneField x))),
 name ~ AppendSymbol "c" ctor, KnownValue x, ScopeCodeGen retBr,
 ret ~ RetExprs retBr, RetOutStack ret ~ RetOutStack retBr) =>
Label name
-> (Var x -> freer retBr)
-> IndigoMCaseClauseL
     freer ret ('CaseClauseParam ctor ('OneField x))
OneFieldIndigoMCaseClauseL Label name
cName Var x -> IndigoM retBr
b
infixr 0 #=

----------------------------------------------------------------------------
-- Scope & Functions
----------------------------------------------------------------------------

-- | Utility type for an 'IndigoM' that adds one element to the stack and returns
-- a variable pointing at it.
type IndigoFunction ret = IndigoM (RetVars ret)

-- | Utility type for an 'IndigoM' that does not modify the stack (only the
-- values in it) and returns nothing.
type IndigoProcedure = IndigoM ()

type IndigoEntrypoint param = param -> IndigoProcedure

scope
  :: forall a . ScopeCodeGen a
  => IndigoM a
  -> IndigoFunction a
scope :: IndigoM a -> IndigoFunction a
scope = StatementF IndigoM (RetVars' (ClassifyReturnValue a) a)
-> IndigoFunction a
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue a) a)
 -> IndigoFunction a)
-> (IndigoM a
    -> StatementF IndigoM (RetVars' (ClassifyReturnValue a) a))
-> IndigoM a
-> IndigoFunction a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IndigoM a
-> StatementF IndigoM (RetVars' (ClassifyReturnValue a) a)
forall res (freer :: * -> *).
ScopeCodeGen res =>
freer res -> StatementF freer (RetVars res)
Scope

-- | Alias for 'scope' we use in the tutorial.
defFunction
  :: forall a . ScopeCodeGen a
  => IndigoM a
  -> IndigoFunction a
defFunction :: IndigoM a -> IndigoFunction a
defFunction = IndigoM a -> IndigoFunction a
forall a. ScopeCodeGen a => IndigoM a -> IndigoFunction a
scope

-- | A more specific version of 'defFunction' meant to more easily create
-- 'IndigoContract's.
--
-- Used in the tutorial. The 'HasSideEffects' constraint is
-- specified to avoid the warning for redundant constraints.
defContract
  :: (HasSideEffects => IndigoM ())
  -> (HasSideEffects => IndigoProcedure)
defContract :: (HasSideEffects => IndigoM ()) -> HasSideEffects => IndigoM ()
defContract = (HasSideEffects => IndigoM ()) -> IndigoM ()
forall a. ScopeCodeGen a => IndigoM a -> IndigoFunction a
scope

-- | Family of @defNamed*LambdaN@ functions put an Indigo computation
-- on the stack to later call it avoiding code duplication.
-- @defNamed*LambdaN@ takes a computation with N arguments.
-- This family of functions add some overhead to contract byte size
-- for every call of the function,
-- therefore, DON'T use @defNamed*LambdaN@ if:
-- * Your computation is pretty small.
--   It would be cheaper just to inline it, so use 'defFunction'.
-- * Your computation is called only once, in this case also use 'defFunction'.
--
-- Also, pay attention that @defNamed*LambdaN@ accepts a string that is
-- a name of the passed computation. Be careful and make sure that all
-- declared computations have different names.
-- Later the name will be removed.
--
-- Pay attention, that lambda argument will be evaluated
-- to variable before lambda calling.
--
-- TODO Approach with lambda names has critical pitfall:
-- in case if a function takes @Label name@, lambda body
-- won't be regenerated for every different label.
-- So be carefully, this will be fixed in a following issue.
defNamedEffLambda1
  :: forall st argExpr res .
  ( ToExpr argExpr
  , Typeable res
  , ExecuteLambdaEff1C st (ExprType argExpr) res
  , CreateLambdaEff1C st (ExprType argExpr) res)
  => String
  -> (Var (ExprType argExpr) -> IndigoM res)
  -> (argExpr -> IndigoM (RetVars res))
defNamedEffLambda1 :: String
-> (Var (ExprType argExpr) -> IndigoM res)
-> argExpr
-> IndigoM (RetVars res)
defNamedEffLambda1 lName :: String
lName body :: Var (ExprType argExpr) -> IndigoM res
body = \ex :: argExpr
ex ->
  StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars res) -> IndigoM (RetVars res))
-> StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ Proxy st
-> String
-> (Var (ExprType argExpr) -> IndigoM res)
-> Expr (ExprType argExpr)
-> StatementF IndigoM (RetVars res)
forall res arg res (freer :: * -> *).
(ExecuteLambdaEff1C res arg res, CreateLambdaEff1C res arg res,
 Typeable res) =>
Proxy res
-> String
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars res)
LambdaEff1Call (Proxy st
forall k (t :: k). Proxy t
Proxy @st) String
lName Var (ExprType argExpr) -> IndigoM res
body (argExpr -> Expr (ExprType argExpr)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr argExpr
ex)

-- | Like defNamedEffLambda1 but doesn't make side effects.
defNamedLambda1
  :: forall st argExpr res .
  ( ToExpr argExpr
  , Typeable res
  , ExecuteLambda1C st (ExprType argExpr) res
  , CreateLambda1C st (ExprType argExpr) res)
  => String
  -> (Var (ExprType argExpr) -> IndigoM res)
  -> (argExpr -> IndigoM (RetVars res))
defNamedLambda1 :: String
-> (Var (ExprType argExpr) -> IndigoM res)
-> argExpr
-> IndigoM (RetVars res)
defNamedLambda1 lName :: String
lName body :: Var (ExprType argExpr) -> IndigoM res
body = \ex :: argExpr
ex ->
  StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars res) -> IndigoM (RetVars res))
-> StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ Proxy st
-> String
-> (Var (ExprType argExpr) -> IndigoM res)
-> Expr (ExprType argExpr)
-> StatementF IndigoM (RetVars res)
forall st arg res (freer :: * -> *).
(ExecuteLambda1C st arg res, CreateLambda1C st arg res,
 Typeable res) =>
Proxy st
-> String
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars res)
Lambda1Call (Proxy st
forall k (t :: k). Proxy t
Proxy @st) String
lName Var (ExprType argExpr) -> IndigoM res
body (argExpr -> Expr (ExprType argExpr)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr argExpr
ex)

-- | Like defNamedLambda1 but doesn't take an argument.
defNamedLambda0
  :: forall st res .
  ( Typeable res
  , ExecuteLambda1C st () res
  , CreateLambda1C st () res)
  => String
  -> IndigoM res
  -> IndigoM (RetVars res)
defNamedLambda0 :: String -> IndigoM res -> IndigoM (RetVars res)
defNamedLambda0 lName :: String
lName body :: IndigoM res
body = StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars res) -> IndigoM (RetVars res))
-> StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ Proxy st
-> String
-> (Var () -> IndigoM res)
-> Expr ()
-> StatementF IndigoM (RetVars res)
forall st arg res (freer :: * -> *).
(ExecuteLambda1C st arg res, CreateLambda1C st arg res,
 Typeable res) =>
Proxy st
-> String
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars res)
Lambda1Call (Proxy st
forall k (t :: k). Proxy t
Proxy @st) String
lName (\(Var ()
_ :: Var ()) -> IndigoM res
body) (() -> Expr ()
forall a. NiceConstant a => a -> Expr a
C ())

-- | Like defNamedEffLambda1 but doesn't modify storage and doesn't make side effects.
defNamedPureLambda1
  :: forall argExpr res .
  ( ToExpr argExpr
  , Typeable res
  , ExecuteLambdaPure1C (ExprType argExpr) res
  , CreateLambdaPure1C (ExprType argExpr) res)
  => String
  -> (Var (ExprType argExpr) -> IndigoM res)
  -> (argExpr -> IndigoM (RetVars res))
defNamedPureLambda1 :: String
-> (Var (ExprType argExpr) -> IndigoM res)
-> argExpr
-> IndigoM (RetVars res)
defNamedPureLambda1 lName :: String
lName body :: Var (ExprType argExpr) -> IndigoM res
body = \ex :: argExpr
ex ->
  StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars res) -> IndigoM (RetVars res))
-> StatementF IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ String
-> (Var (ExprType argExpr) -> IndigoM res)
-> Expr (ExprType argExpr)
-> StatementF IndigoM (RetVars res)
forall arg res (freer :: * -> *).
(ExecuteLambdaPure1C arg res, CreateLambdaPure1C arg res,
 Typeable res) =>
String
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars res)
LambdaPure1Call String
lName Var (ExprType argExpr) -> IndigoM res
body (argExpr -> Expr (ExprType argExpr)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr argExpr
ex)

----------------------------------------------------------------------------
-- Loop
----------------------------------------------------------------------------

-- | While statement.
while :: forall ex . ex :~> Bool => ex -> IndigoM () -> IndigoM ()
while :: ex -> IndigoM () -> IndigoM ()
while e :: ex
e body :: IndigoM ()
body = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> StatementF IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ Expr Bool -> IndigoM () -> StatementF IndigoM ()
forall (freer :: * -> *).
Expr Bool -> freer () -> StatementF freer ()
While (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
e) IndigoM ()
body

whileLeft
  :: forall x y ex .
     ( ex :~> Either y x
     , KnownValue y
     , KnownValue x
     )
  => ex
  -> (Var y -> IndigoM ())
  -> IndigoM (Var x)
whileLeft :: ex -> (Var y -> IndigoM ()) -> IndigoM (Var x)
whileLeft e :: ex
e body :: Var y -> IndigoM ()
body = StatementF IndigoM (Var x) -> IndigoM (Var x)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (Var x) -> IndigoM (Var x))
-> StatementF IndigoM (Var x) -> IndigoM (Var x)
forall a b. (a -> b) -> a -> b
$ Expr (Either y x)
-> (Var y -> IndigoM ()) -> StatementF IndigoM (Var x)
forall x y (freer :: * -> *).
(KnownValue x, KnownValue y) =>
Expr (Either y x)
-> (Var y -> freer ()) -> StatementF freer (Var x)
WhileLeft (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
e) Var y -> IndigoM ()
body

-- | For statements to iterate over a container.
forEach
  :: forall a e . (IterOpHs a, KnownValue (IterOpElHs a), e :~> a)
  => e -> (Var (IterOpElHs a) -> IndigoM ())
  -> IndigoM ()
forEach :: e -> (Var (IterOpElHs a) -> IndigoM ()) -> IndigoM ()
forEach container :: e
container body :: Var (IterOpElHs a) -> IndigoM ()
body = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> StatementF IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ Expr a
-> (Var (IterOpElHs a) -> IndigoM ()) -> StatementF IndigoM ()
forall a (freer :: * -> *).
(IterOpHs a, KnownValue (IterOpElHs a)) =>
Expr a -> (Var (IterOpElHs a) -> freer ()) -> StatementF freer ()
ForEach (e -> Expr (ExprType e)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr e
container) Var (IterOpElHs a) -> IndigoM ()
body

----------------------------------------------------------------------------
-- Documentation
----------------------------------------------------------------------------

-- | Put a document item.
doc :: DocItem di => di -> IndigoM ()
doc :: di -> IndigoM ()
doc di :: di
di = (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ())
-> (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ IndigoState inp inp () -> SomeIndigoState inp ()
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (IndigoState inp inp () -> SomeIndigoState inp ())
-> IndigoState inp inp () -> SomeIndigoState inp ()
forall a b. (a -> b) -> a -> b
$ di -> IndigoState inp inp ()
forall di (s :: [*]). DocItem di => di -> IndigoState s s ()
B.doc di
di

-- | Group documentation built in the given piece of code
-- into a block dedicated to one thing, e.g. to one entrypoint.
docGroup :: DocGrouping -> IndigoM () -> IndigoM ()
docGroup :: DocGrouping -> IndigoM () -> IndigoM ()
docGroup = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (DocGrouping -> IndigoM () -> StatementF IndigoM ())
-> DocGrouping
-> IndigoM ()
-> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... DocGrouping -> IndigoM () -> StatementF IndigoM ()
forall (freer :: * -> *).
DocGrouping -> freer () -> StatementF freer ()
DocGroup

-- | Insert documentation of the contract's storage type. The type
-- should be passed using type applications.
docStorage :: forall storage. TypeHasDoc storage => IndigoM ()
docStorage :: IndigoM ()
docStorage = (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ())
-> (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ IndigoState inp inp () -> SomeIndigoState inp ()
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (IndigoState inp inp () -> SomeIndigoState inp ())
-> IndigoState inp inp () -> SomeIndigoState inp ()
forall a b. (a -> b) -> a -> b
$ forall (s :: [*]). TypeHasDoc storage => IndigoState s s ()
forall storage (s :: [*]). TypeHasDoc storage => IndigoState s s ()
B.docStorage @storage

-- | Give a name to the given contract. Apply it to the whole contract code.
contractName :: Text -> IndigoM () -> IndigoM ()
contractName :: Text -> IndigoM () -> IndigoM ()
contractName = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (Text -> IndigoM () -> StatementF IndigoM ())
-> Text
-> IndigoM ()
-> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... Text -> IndigoM () -> StatementF IndigoM ()
forall (freer :: * -> *). Text -> freer () -> StatementF freer ()
ContractName

-- | Attach general info to the given contract.
contractGeneral :: IndigoM () -> IndigoM ()
contractGeneral :: IndigoM () -> IndigoM ()
contractGeneral = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (IndigoM () -> StatementF IndigoM ())
-> IndigoM ()
-> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IndigoM () -> StatementF IndigoM ()
forall (freer :: * -> *). freer () -> StatementF freer ()
ContractGeneral

-- | Attach default general info to the contract documentation.
contractGeneralDefault :: IndigoM ()
contractGeneralDefault :: IndigoM ()
contractGeneralDefault = (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ())
-> (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ IndigoState inp inp () -> SomeIndigoState inp ()
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (IndigoState inp inp () -> SomeIndigoState inp ())
-> IndigoState inp inp () -> SomeIndigoState inp ()
forall a b. (a -> b) -> a -> b
$ IndigoState inp inp ()
forall (s :: [*]). IndigoState s s ()
B.contractGeneralDefault

-- | Indigo version for the homonym Lorentz function.
finalizeParamCallingDoc
  :: forall param x.
     ( ToExpr param
     , NiceParameterFull (ExprType param)
     , RequireSumType (ExprType param)
     , HasCallStack
     )
  => (Var (ExprType param) -> IndigoM x) -> param -> IndigoM x
finalizeParamCallingDoc :: (Var (ExprType param) -> IndigoM x) -> param -> IndigoM x
finalizeParamCallingDoc i :: Var (ExprType param) -> IndigoM x
i = StatementF IndigoM x -> IndigoM x
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM x -> IndigoM x)
-> (param -> StatementF IndigoM x) -> param -> IndigoM x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var (ExprType param) -> IndigoM x)
-> Expr (ExprType param) -> StatementF IndigoM x
forall cp (freer :: * -> *) x.
(NiceParameterFull cp, RequireSumType cp, HasCallStack) =>
(Var cp -> freer x) -> Expr cp -> StatementF freer x
FinalizeParamCallingDoc Var (ExprType param) -> IndigoM x
i (Expr (ExprType param) -> StatementF IndigoM x)
-> (param -> Expr (ExprType param))
-> param
-> StatementF IndigoM x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. param -> Expr (ExprType param)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

-- | Put a 'DDescription' doc item.
description :: Markdown -> IndigoM ()
description :: Markdown -> IndigoM ()
description = DDescription -> IndigoM ()
forall di. DocItem di => di -> IndigoM ()
doc (DDescription -> IndigoM ())
-> (Markdown -> DDescription) -> Markdown -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Markdown -> DDescription
DDescription

-- | Put a 'DAnchor' doc item.
anchor :: Text -> IndigoM ()
anchor :: Text -> IndigoM ()
anchor = DAnchor -> IndigoM ()
forall di. DocItem di => di -> IndigoM ()
doc (DAnchor -> IndigoM ()) -> (Text -> DAnchor) -> Text -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Anchor -> DAnchor
DAnchor (Anchor -> DAnchor) -> (Text -> Anchor) -> Text -> DAnchor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Anchor
forall anchor. ToAnchor anchor => anchor -> Anchor
toAnchor

-- | Put a 'DEntrypointExample' doc item.
example :: forall a. NiceParameter a => a -> IndigoM ()
example :: a -> IndigoM ()
example = DEntrypointExample -> IndigoM ()
forall di. DocItem di => di -> IndigoM ()
doc (DEntrypointExample -> IndigoM ())
-> (a -> DEntrypointExample) -> a -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DEntrypointExample
forall a. NiceParameter a => a -> DEntrypointExample
mkDEntrypointExample

----------------------------------------------------------------------------
-- Contract call
----------------------------------------------------------------------------

selfCalling
  :: forall p mname.
     ( NiceParameterFull p
     , KnownValue (GetEntrypointArgCustom p mname)
     )
  => EntrypointRef mname
  -> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
selfCalling :: EntrypointRef mname
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
selfCalling ep :: EntrypointRef mname
ep = (forall (inp :: [*]).
 SomeIndigoState
   inp (Var (ContractRef (GetEntrypointArgCustom p mname))))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]).
  SomeIndigoState
    inp (Var (ContractRef (GetEntrypointArgCustom p mname))))
 -> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname))))
-> (forall (inp :: [*]).
    SomeIndigoState
      inp (Var (ContractRef (GetEntrypointArgCustom p mname))))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a b. (a -> b) -> a -> b
$ IndigoState
  inp
  (ContractRef (GetEntrypointArgCustom p mname) & inp)
  (Var (ContractRef (GetEntrypointArgCustom p mname)))
-> SomeIndigoState
     inp (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (IndigoState
   inp
   (ContractRef (GetEntrypointArgCustom p mname) & inp)
   (Var (ContractRef (GetEntrypointArgCustom p mname)))
 -> SomeIndigoState
      inp (Var (ContractRef (GetEntrypointArgCustom p mname))))
-> IndigoState
     inp
     (ContractRef (GetEntrypointArgCustom p mname) & inp)
     (Var (ContractRef (GetEntrypointArgCustom p mname)))
-> SomeIndigoState
     inp (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a b. (a -> b) -> a -> b
$ EntrypointRef mname
-> IndigoState
     inp
     (ContractRef (GetEntrypointArgCustom p mname) & inp)
     (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall p (inp :: [*]) (mname :: Maybe Symbol).
(NiceParameterFull p,
 KnownValue (GetEntrypointArgCustom p mname)) =>
EntrypointRef mname
-> IndigoState
     inp
     (ContractRef (GetEntrypointArgCustom p mname) & inp)
     (Var (ContractRef (GetEntrypointArgCustom p mname)))
B.selfCalling @p EntrypointRef mname
ep

contractCalling
  :: forall cp epRef epArg addr exAddr.
     ( HasEntrypointArg cp epRef epArg
     , ToTAddress cp addr
     , ToT addr ~ ToT Address
     , exAddr :~> addr
     , KnownValue epArg
     )
  => epRef -> exAddr -> IndigoM (Var (Maybe (ContractRef epArg)))
contractCalling :: epRef -> exAddr -> IndigoM (Var (Maybe (ContractRef epArg)))
contractCalling epRef :: epRef
epRef = StatementF IndigoM (Var (Maybe (ContractRef epArg)))
-> IndigoM (Var (Maybe (ContractRef epArg)))
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (Var (Maybe (ContractRef epArg)))
 -> IndigoM (Var (Maybe (ContractRef epArg))))
-> (exAddr -> StatementF IndigoM (Var (Maybe (ContractRef epArg))))
-> exAddr
-> IndigoM (Var (Maybe (ContractRef epArg)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy cp
-> epRef
-> Expr addr
-> StatementF IndigoM (Var (Maybe (ContractRef epArg)))
forall cp epRef epArg addr (freer :: * -> *).
(HasEntrypointArg cp epRef epArg, ToTAddress cp addr,
 ToT addr ~ ToT Address, KnownValue epArg) =>
Proxy cp
-> epRef
-> Expr addr
-> StatementF freer (Var (Maybe (ContractRef epArg)))
ContractCalling (Proxy cp
forall k (t :: k). Proxy t
Proxy @cp) epRef
epRef (Expr addr -> StatementF IndigoM (Var (Maybe (ContractRef epArg))))
-> (exAddr -> Expr addr)
-> exAddr
-> StatementF IndigoM (Var (Maybe (ContractRef epArg)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. exAddr -> Expr addr
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

----------------------------------------------------------------------------
-- Side-effects operations
----------------------------------------------------------------------------

transferTokens
  :: (IsExpr exp p, IsExpr exm Mutez, IsExpr exc (ContractRef p), NiceParameter p, HasSideEffects)
  => exp -> exm -> exc -> IndigoM ()
transferTokens :: exp -> exm -> exc -> IndigoM ()
transferTokens ep :: exp
ep em :: exm
em ec :: exc
ec = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> StatementF IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$
  Expr p
-> Expr Mutez -> Expr (ContractRef p) -> StatementF IndigoM ()
forall p (freer :: * -> *).
(NiceParameter p, HasSideEffects) =>
Expr p -> Expr Mutez -> Expr (ContractRef p) -> StatementF freer ()
TransferTokens (exp -> Expr (ExprType exp)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exp
ep) (exm -> Expr (ExprType exm)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exm
em) (exc -> Expr (ExprType exc)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exc
ec)

setDelegate :: (HasSideEffects, IsExpr ex (Maybe KeyHash)) => ex -> IndigoM ()
setDelegate :: ex -> IndigoM ()
setDelegate =  StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (ex -> StatementF IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr (Maybe KeyHash) -> StatementF IndigoM ()
forall (freer :: * -> *).
HasSideEffects =>
Expr (Maybe KeyHash) -> StatementF freer ()
SetDelegate (Expr (Maybe KeyHash) -> StatementF IndigoM ())
-> (ex -> Expr (Maybe KeyHash)) -> ex -> StatementF IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr (Maybe KeyHash)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

-- | Create contract using default compilation options for Lorentz compiler.
--
-- See "Lorentz.Run".
createContract
  :: ( IsObject st
     , IsExpr exk (Maybe KeyHash), IsExpr exm Mutez, IsExpr exs st
     , NiceStorage st, NiceParameterFull param
     , HasSideEffects
     )
  => (HasStorage st => Var param -> IndigoM ())
  -> exk
  -> exm
  -> exs
  -> IndigoM (Var Address)
createContract :: (HasStorage st => Var param -> IndigoM ())
-> exk -> exm -> exs -> IndigoM (Var Address)
createContract iCtr :: HasStorage st => Var param -> IndigoM ()
iCtr ek :: exk
ek em :: exm
em es :: exs
es = StatementF IndigoM (Var Address) -> IndigoM (Var Address)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (Var Address) -> IndigoM (Var Address))
-> StatementF IndigoM (Var Address) -> IndigoM (Var Address)
forall a b. (a -> b) -> a -> b
$
  Contract param st
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF IndigoM (Var Address)
forall st param (freer :: * -> *).
(IsObject st, NiceStorage st, NiceParameterFull param,
 HasSideEffects) =>
Contract param st
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF freer (Var Address)
CreateContract (ContractCode param st -> Contract param st
forall cp st. ContractCode cp st -> Contract cp st
defaultContract (ContractCode param st -> Contract param st)
-> ContractCode param st -> Contract param st
forall a b. (a -> b) -> a -> b
$ IndigoContract param st -> ContractCode param st
forall param st.
(KnownValue param, IsObject st) =>
IndigoContract param st -> ContractCode param st
compileIndigoContract IndigoContract param st
HasStorage st => Var param -> IndigoM ()
iCtr) (exk -> Expr (ExprType exk)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exk
ek) (exm -> Expr (ExprType exm)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exm
em) (exs -> Expr (ExprType exs)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exs
es)

-- | Create contract from raw Lorentz 'L.Contract'.
createLorentzContract
  :: ( IsObject st
     , IsExpr exk (Maybe KeyHash), IsExpr exm Mutez, IsExpr exs st
     , NiceStorage st, NiceParameterFull param
     , HasSideEffects
     )
  => L.Contract param st
  -> exk
  -> exm
  -> exs
  -> IndigoM (Var Address)
createLorentzContract :: Contract param st -> exk -> exm -> exs -> IndigoM (Var Address)
createLorentzContract lCtr :: Contract param st
lCtr ek :: exk
ek em :: exm
em es :: exs
es = StatementF IndigoM (Var Address) -> IndigoM (Var Address)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (Var Address) -> IndigoM (Var Address))
-> StatementF IndigoM (Var Address) -> IndigoM (Var Address)
forall a b. (a -> b) -> a -> b
$
  Contract param st
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF IndigoM (Var Address)
forall st param (freer :: * -> *).
(IsObject st, NiceStorage st, NiceParameterFull param,
 HasSideEffects) =>
Contract param st
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF freer (Var Address)
CreateContract Contract param st
lCtr (exk -> Expr (ExprType exk)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exk
ek) (exm -> Expr (ExprType exm)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exm
em) (exs -> Expr (ExprType exs)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr exs
es)

----------------------------------------------------------------------------
-- Error
----------------------------------------------------------------------------

assert
  :: forall x ex.
     ( IsError x
     , IsExpr ex Bool
     )
  => x -> ex -> IndigoM ()
assert :: x -> ex -> IndigoM ()
assert x :: x
x = StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (ex -> StatementF IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Expr Bool -> StatementF IndigoM ()
forall x (freer :: * -> *).
IsError x =>
x -> Expr Bool -> StatementF freer ()
Assert x
x (Expr Bool -> StatementF IndigoM ())
-> (ex -> Expr Bool) -> ex -> StatementF IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr Bool
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

failWith
  :: forall r a ex . IsExpr ex a
  => ex -> IndigoM r
failWith :: ex -> IndigoM r
failWith = StatementF IndigoM r -> IndigoM r
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM r -> IndigoM r)
-> (ex -> StatementF IndigoM r) -> ex -> IndigoM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr a -> StatementF IndigoM r
forall a (freer :: * -> *) r.
KnownValue a =>
Expr a -> StatementF freer r
FailWith (Expr a -> StatementF IndigoM r)
-> (ex -> Expr a) -> ex -> StatementF IndigoM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr a
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

failCustom
  :: forall r tag err ex.
     ( err ~ ErrorArg tag
     , CustomErrorHasDoc tag
     , NiceConstant err
     , ex :~> err
     )
  => Label tag -> ex -> IndigoM r
failCustom :: Label tag -> ex -> IndigoM r
failCustom l :: Label tag
l = StatementF IndigoM r -> IndigoM r
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM r -> IndigoM r)
-> (ex -> StatementF IndigoM r) -> ex -> IndigoM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label tag -> Expr err -> StatementF IndigoM r
forall err (tag :: Symbol) (freer :: * -> *) r.
(err ~ ErrorArg tag, CustomErrorHasDoc tag, NiceConstant err) =>
Label tag -> Expr err -> StatementF freer r
FailCustom Label tag
l (Expr err -> StatementF IndigoM r)
-> (ex -> Expr err) -> ex -> StatementF IndigoM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr err
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

failCustom_
  :: forall r tag notVoidErrorMsg.
     ( RequireNoArgError tag notVoidErrorMsg
     , CustomErrorHasDoc tag
     )
  => Label tag -> IndigoM r
failCustom_ :: Label tag -> IndigoM r
failCustom_ lab :: Label tag
lab = (forall (inp :: [*]). SomeIndigoState inp r) -> IndigoM r
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]). SomeIndigoState inp r) -> IndigoM r)
-> (forall (inp :: [*]). SomeIndigoState inp r) -> IndigoM r
forall a b. (a -> b) -> a -> b
$ IndigoState inp Any r -> SomeIndigoState inp r
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (IndigoState inp Any r -> SomeIndigoState inp r)
-> IndigoState inp Any r -> SomeIndigoState inp r
forall a b. (a -> b) -> a -> b
$ Label tag -> IndigoState inp Any r
forall (tag :: Symbol) (s :: [*]) (t :: [*]) r
       (notVoidErrorMsg :: ErrorMessage).
(RequireNoArgError tag notVoidErrorMsg, CustomErrorHasDoc tag) =>
Label tag -> IndigoState s t r
B.failCustom_ Label tag
lab

failUnexpected_ :: MText -> IndigoM r
failUnexpected_ :: MText -> IndigoM r
failUnexpected_ tx :: MText
tx = (forall (inp :: [*]). SomeIndigoState inp r) -> IndigoM r
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]). SomeIndigoState inp r) -> IndigoM r)
-> (forall (inp :: [*]). SomeIndigoState inp r) -> IndigoM r
forall a b. (a -> b) -> a -> b
$ IndigoState inp Any r -> SomeIndigoState inp r
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (IndigoState inp Any r -> SomeIndigoState inp r)
-> IndigoState inp Any r -> SomeIndigoState inp r
forall a b. (a -> b) -> a -> b
$ MText -> IndigoState inp Any r
forall (s :: [*]) (t :: [*]) r. MText -> IndigoState s t r
B.failUnexpected_ MText
tx

assertCustom
  :: forall tag err errEx ex .
     ( err ~ ErrorArg tag
     , CustomErrorHasDoc tag
     , NiceConstant err
     , IsExpr errEx err
     , IsExpr ex Bool
     )
  => Label tag -> errEx -> ex -> IndigoM ()
assertCustom :: Label tag -> errEx -> ex -> IndigoM ()
assertCustom tag :: Label tag
tag errEx :: errEx
errEx e :: ex
e = Expr Bool -> IndigoM () -> IndigoM () -> IndigoM (RetVars ())
forall a b ex.
(IfConstraint a b, ex :~> Bool) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
e) (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (Label tag -> errEx -> IndigoM ()
forall r (tag :: Symbol) err ex.
(err ~ ErrorArg tag, CustomErrorHasDoc tag, NiceConstant err,
 ex :~> err) =>
Label tag -> ex -> IndigoM r
failCustom Label tag
tag errEx
errEx :: IndigoM ())

assertCustom_
  :: forall tag notVoidErrorMsg ex.
     ( RequireNoArgError tag notVoidErrorMsg
     , CustomErrorHasDoc tag
     , IsExpr ex Bool
     )
  => Label tag -> ex -> IndigoM ()
assertCustom_ :: Label tag -> ex -> IndigoM ()
assertCustom_ tag :: Label tag
tag e :: ex
e = Expr Bool -> IndigoM () -> IndigoM () -> IndigoM (RetVars ())
forall a b ex.
(IfConstraint a b, ex :~> Bool) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ (ex -> Expr (ExprType ex)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr ex
e) (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (Label tag -> IndigoM ()
forall r (tag :: Symbol) (notVoidErrorMsg :: ErrorMessage).
(RequireNoArgError tag notVoidErrorMsg, CustomErrorHasDoc tag) =>
Label tag -> IndigoM r
failCustom_ Label tag
tag :: IndigoM ())

----------------------------------------------------------------------------
-- Comments
----------------------------------------------------------------------------

-- | Add a comment in a generated Michelson code
justComment :: Text -> IndigoM ()
justComment :: Text -> IndigoM ()
justComment = CommentType -> IndigoM ()
comment (CommentType -> IndigoM ())
-> (Text -> CommentType) -> Text -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> CommentType
MT.JustComment

-- | Add a comment in a generated Michelson code
comment :: MT.CommentType -> IndigoM ()
comment :: CommentType -> IndigoM ()
comment t :: CommentType
t = (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a. (forall (inp :: [*]). SomeIndigoState inp a) -> IndigoM a
liftIndigoState ((forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ())
-> (forall (inp :: [*]). SomeIndigoState inp ()) -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ IndigoState inp inp () -> SomeIndigoState inp ()
forall (inp :: [*]) (out :: [*]) a.
IndigoState inp out a -> SomeIndigoState inp a
toSIS (CommentType -> IndigoState inp inp ()
forall (i :: [*]). CommentType -> IndigoState i i ()
B.comment CommentType
t)

-- | Add a comment before and after the given Indigo function code.
-- The first argument is the name of the function.
commentAroundFun :: Text -> IndigoM a -> IndigoM a
commentAroundFun :: Text -> IndigoM a -> IndigoM a
commentAroundFun fName :: Text
fName body :: IndigoM a
body =
  CommentType -> IndigoM ()
comment (Text -> CommentType
MT.FunctionStarts Text
fName) IndigoM () -> IndigoM a -> IndigoM a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
  IndigoM a
body IndigoM a -> (a -> IndigoM a) -> IndigoM a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
  \res :: a
res -> a
res a -> IndigoM () -> IndigoM a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ CommentType -> IndigoM ()
comment (Text -> CommentType
MT.FunctionEnds Text
fName)

-- | Add a comment before and after the given Indigo statement code.
-- The first argument is the name of the statement.
commentAroundStmt :: Text -> IndigoM a -> IndigoM a
commentAroundStmt :: Text -> IndigoM a -> IndigoM a
commentAroundStmt sName :: Text
sName body :: IndigoM a
body =
  CommentType -> IndigoM ()
comment (Text -> CommentType
MT.StatementStarts Text
sName) IndigoM () -> IndigoM a -> IndigoM a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
  IndigoM a
body IndigoM a -> (a -> IndigoM a) -> IndigoM a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
  \res :: a
res -> a
res a -> IndigoM () -> IndigoM a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ CommentType -> IndigoM ()
comment (Text -> CommentType
MT.StatementEnds Text
sName)