-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

-- | Frontend statements's functions of the Indigo Language.

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
  , emit

  -- * Failures
  , failWith
  , assert
  , failCustom
  , failCustom_
  , failCustomNoArg
  , failUnexpected_
  , assertCustom
  , assertCustom_
  , assertCustomNoArg
  , assertSome
  , assertNone
  , assertRight
  , assertLeft
  -- * Re-exports
  , ReturnableValue
  , RetVars
  , FieldAnn
  , annQ

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

  -- * Blocks
  , IndigoFunction
  , IndigoProcedure
  , IndigoEntrypoint

  -- * Helpers
  , liftIndigoState
  ) where

import Fmt (Buildable)
import GHC.Stack (popCallStack)
import GHC.Stack.Types (SrcLoc(..))

import Indigo.Backend qualified as B
import Indigo.Backend.Case hiding (caseRec, entryCaseRec)
import Indigo.Backend.Lambda
import Indigo.Backend.Scope
import Indigo.Common.Expr (Expr(C), ExprType, IsExpr, ToExpr, toExpr, type (:~>))
import Indigo.Common.Field (HasField)
import Indigo.Common.Object (IsObject)
import Indigo.Common.SIS (SomeIndigoState, toSIS)
import Indigo.Common.Var (HasSideEffects, HasStorage, Var, storageVar)
import Indigo.Compilation (compileIndigoContract)
import Indigo.Frontend.Expr ((#!))
import Indigo.Frontend.Internal.Statement
import Indigo.Frontend.Program
import Indigo.Lorentz hiding (comment, commentAroundFun, commentAroundStmt, justComment)
import Indigo.Prelude
import Lorentz.Entrypoints.Helpers (RequireSumType)
import Lorentz.Instr qualified as L
import Lorentz.Run qualified as L
import Morley.Michelson.Typed qualified as MT
import Morley.Michelson.Typed.Arith qualified as M
import Morley.Michelson.Typed.Haskell.Instr.Sum (CaseClauseParam(..), CtorField(..))
import Morley.Michelson.Untyped.Annotation (FieldAnn, annQ)
import Morley.Util.Markdown (toAnchor)
import Morley.Util.TypeLits (AppendSymbol)
import Morley.Util.TypeTuple.Class
import Prelude ((==))

oneIndigoM :: StatementF IndigoM a -> IndigoM a
oneIndigoM :: forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM 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)

calledFrom :: HasCallStack => IndigoM a -> IndigoM a
calledFrom :: forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom IndigoM a
iM = case CallStack -> [([Char], SrcLoc)]
getCallStack CallStack
HasCallStack => CallStack
callStack of
  [] -> Text -> IndigoM a
forall a. HasCallStack => Text -> a
error Text
"impossible: calledFrom has HasCallStack constraint, so at least one element has to be at the callStack"
  (([Char]
_, SrcLoc
loc):[([Char], SrcLoc)]
_)
    | SrcLoc -> [Char]
srcLocModule SrcLoc
loc [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"Indigo.Frontend.Language" ->
        Program (StatementF IndigoM) a -> IndigoM a
forall a. Program (StatementF IndigoM) a -> IndigoM a
IndigoM (Program (StatementF IndigoM) a -> IndigoM a)
-> (IndigoM a -> Program (StatementF IndigoM) a)
-> IndigoM a
-> IndigoM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM a -> Program (StatementF IndigoM) a
forall (instr :: * -> *) a. instr a -> Program instr a
Instr (StatementF IndigoM a -> Program (StatementF IndigoM) a)
-> (IndigoM a -> StatementF IndigoM a)
-> IndigoM a
-> Program (StatementF IndigoM) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallStack -> IndigoM a -> StatementF IndigoM a
forall (freer :: * -> *) a.
CallStack -> freer a -> StatementF freer a
CalledFrom (CallStack -> CallStack
popCallStack CallStack
HasCallStack => CallStack
callStack) (IndigoM a -> IndigoM a) -> IndigoM a -> IndigoM a
forall a b. (a -> b) -> a -> b
$ IndigoM a
iM
    | Bool
otherwise -> Text -> IndigoM a
forall a. HasCallStack => Text -> a
error (Text -> IndigoM a) -> Text -> IndigoM a
forall a b. (a -> b) -> a -> b
$ [Char] -> Text
forall a. IsString a => [Char] -> a
fromString ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$
                    [Char]
"Misuse of calledFrom: the call made from " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
srcLocModule SrcLoc
loc [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
". " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                    [Char]
"You've either forgotten to specify HasCallStack constraint for exported Indigo frontend function or " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                    [Char]
"exported calledFrom and called outside of Indigo.Frontend.Language module. " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                    [Char]
"Please, report this issue to Indigo developers."

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

varModification
  :: (IsExpr ey y, IsObject x)
  => ([y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification :: forall ey y x.
(IsExpr ey y, IsObject x) =>
('[y, x] :-> '[x]) -> Var x -> ey -> IndigoM ()
varModification '[y, x] :-> '[x]
act 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, HasCallStack) => ex -> IndigoM (Var x)
new :: forall ex x. (IsExpr ex x, HasCallStack) => ex -> IndigoM (Var x)
new = IndigoM (Var x) -> IndigoM (Var x)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var x) -> IndigoM (Var x))
-> (ex -> IndigoM (Var x)) -> ex -> IndigoM (Var x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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, HasCallStack) => Var x -> ex -> IndigoM ()
setVar :: forall ex x.
(IsExpr ex x, HasCallStack) =>
Var x -> ex -> IndigoM ()
setVar Var x
v = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (ex -> IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: * -> *).
KnownValue x =>
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, HasCallStack) => Var x -> ex -> IndigoM ()
Var x
v =: :: forall ex x.
(IsExpr ex x, HasCallStack) =>
Var x -> ex -> IndigoM ()
=: ex
e = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ Var x -> ex -> IndigoM ()
forall ex x.
(IsExpr ex x, HasCallStack) =>
Var x -> ex -> IndigoM ()
setVar Var x
v ex
e

setField
  :: ( ex :~> ftype
     , IsObject dt
     , IsObject ftype
     , HasField dt fname ftype
     , HasCallStack
     )
  => Var dt -> Label fname -> ex -> IndigoM ()
setField :: forall ex ftype dt (fname :: Symbol).
(ex :~> ftype, IsObject dt, IsObject ftype,
 HasField dt fname ftype, HasCallStack) =>
Var dt -> Label fname -> ex -> IndigoM ()
setField Var dt
v Label fname
fName = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (ex -> IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 (fname :: Symbol) (freer :: * -> *).
(IsObject dt, IsObject ftype, HasField dt fname ftype) =>
Var dt -> Label fname -> Expr ftype -> StatementF freer ()
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 m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
+= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Add n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(+=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Add n m r =>
(n : m : s) :-> (r : s)
L.add

(-=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Sub n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
-= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Sub n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(-=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Sub n m r =>
(n : m : s) :-> (r : s)
L.sub

(*=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Mul n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
*= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Mul n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(*=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Mul n m r =>
(n : m : s) :-> (r : s)
L.mul

(||=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Or n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
||= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Or n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(||=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Or n m r =>
(n : m : s) :-> (r : s)
L.or

(&&=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.And n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
&&= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs And n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(&&=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs And n m r =>
(n : m : s) :-> (r : s)
L.and

(^=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Xor n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
^= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Xor n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(^=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Xor n m r =>
(n : m : s) :-> (r : s)
L.xor

(<<<=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Lsl n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
<<<= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Lsl n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(<<<=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Lsl n m r =>
(n : m : s) :-> (r : s)
L.lsl

(>>>=)
  :: ( IsExpr ex1 n, IsObject m
     , ArithOpHs M.Lsr n m m
     , HasCallStack
     ) => Var m -> ex1 -> IndigoM ()
>>>= :: forall ex1 n m.
(IsExpr ex1 n, IsObject m, ArithOpHs Lsr n m m, HasCallStack) =>
Var m -> ex1 -> IndigoM ()
(>>>=) = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Var m -> ex1 -> IndigoM ()) -> Var m -> ex1 -> IndigoM ()
forall a b c. SuperComposition a b c => a -> b -> c
... ('[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 r (s :: [*]).
ArithOpHs Lsr n m r =>
(n : m : s) :-> (r : 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
     , HasCallStack
     )
  => Label name -> ex -> IndigoM ()
setStorageField :: forall store (name :: Symbol) ftype ex.
(HasStorage store, ex :~> ftype, IsObject store, IsObject ftype,
 HasField store name ftype, HasCallStack) =>
Label name -> ex -> IndigoM ()
setStorageField Label name
field ex
expr = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ Var store -> Label name -> ex -> IndigoM ()
forall ex ftype dt (fname :: Symbol).
(ex :~> ftype, IsObject dt, IsObject ftype,
 HasField dt fname ftype, HasCallStack) =>
Var dt -> Label fname -> ex -> IndigoM ()
setField (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
     , HasCallStack
     )
  => Label fname
  -> (Var ftype -> IndigoM fex)
  -> IndigoM ()
updateStorageField :: forall store ftype (fname :: Symbol) fex.
(HasStorage store, fex :~> ftype, HasField store fname ftype,
 IsObject store, IsObject ftype, HasCallStack) =>
Label fname -> (Var ftype -> IndigoM fex) -> IndigoM ()
updateStorageField Label fname
field Var ftype -> IndigoM fex
upd = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ IndigoM () -> IndigoFunction ()
forall a.
(ScopeCodeGen a, HasCallStack) =>
IndigoM a -> IndigoFunction a
scope (IndigoM () -> IndigoFunction ())
-> IndigoM () -> IndigoFunction ()
forall a b. (a -> b) -> a -> b
$ do
  let storage :: Var store
storage = forall st. HasStorage st => Var st
storageVar @store
  Var ftype
fieldVar <- Expr ftype -> IndigoM (Var ftype)
forall ex x. (IsExpr ex x, HasCallStack) => 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, HasCallStack) =>
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
     , HasCallStack
     )
  => Label fname -> IndigoM (Var ftype)
getStorageField :: forall store ftype (fname :: Symbol).
(HasStorage store, HasField store fname ftype, HasCallStack) =>
Label fname -> IndigoM (Var ftype)
getStorageField Label fname
field = IndigoM (Var ftype) -> IndigoM (Var ftype)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var ftype) -> IndigoM (Var ftype))
-> IndigoM (Var ftype) -> IndigoM (Var ftype)
forall a b. (a -> b) -> a -> b
$ Expr ftype -> IndigoM (Var ftype)
forall ex x. (IsExpr ex x, HasCallStack) => ex -> IndigoM (Var x)
new(Expr ftype -> IndigoM (Var ftype))
-> Expr ftype -> IndigoM (Var ftype)
forall a b. (a -> b) -> a -> b
$ 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, HasCallStack)
  => ex
  -> IndigoM a
  -> IndigoM b
  -> IndigoM (RetVars a)
if_ :: forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ ex
ex IndigoM a
tb IndigoM b
fb = IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars b) -> IndigoM (RetVars b))
-> IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars b) -> IndigoM (RetVars b))
-> StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ Expr Bool
-> IndigoM a -> IndigoM b -> StatementF IndigoM (RetVars a)
forall a1 b (freer :: * -> *).
IfConstraint a1 b =>
Expr Bool
-> freer a1
-> freer b
-> StatementF freer (RetVars' (ClassifyReturnValue a1) a1)
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, HasCallStack) => exc -> IndigoM () -> IndigoM ()
when :: forall exc.
(exc :~> Bool, HasCallStack) =>
exc -> IndigoM () -> IndigoM ()
when exc
cond IndigoM ()
expr = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ exc -> IndigoM () -> IndigoM () -> IndigoFunction ()
forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
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, HasCallStack) => exc -> IndigoM () -> IndigoM ()
unless :: forall exc.
(exc :~> Bool, HasCallStack) =>
exc -> IndigoM () -> IndigoM ()
unless exc
cond IndigoM ()
expr = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ exc -> IndigoM () -> IndigoM () -> IndigoFunction ()
forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
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, HasCallStack)
  => ex
  -> (Var x -> IndigoM a)
  -> IndigoM b
  -> IndigoM (RetVars a)
ifSome :: forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b, HasCallStack) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome ex
ex Var x -> IndigoM a
tb IndigoM b
fb = IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars b) -> IndigoM (RetVars b))
-> IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars b) -> IndigoM (RetVars b))
-> StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ Expr (Maybe x)
-> (Var x -> IndigoM a)
-> IndigoM b
-> StatementF IndigoM (RetVars a)
forall a1 b x (freer :: * -> *).
(IfConstraint a1 b, KnownValue x) =>
Expr (Maybe x)
-> (Var x -> freer a1)
-> freer b
-> StatementF freer (RetVars' (ClassifyReturnValue a1) a1)
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, HasCallStack)
  => ex
  -> IndigoM b
  -> (Var x -> IndigoM a)
  -> IndigoM (RetVars a)
ifNone :: forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b, HasCallStack) =>
ex -> IndigoM b -> (Var x -> IndigoM a) -> IndigoM (RetVars a)
ifNone ex
ex IndigoM b
fb Var x -> IndigoM a
tb = IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars b) -> IndigoM (RetVars b))
-> IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ 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, HasCallStack) =>
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
     , HasCallStack
     )
  => exa
  -> (Var x -> IndigoM ())
  -> IndigoM ()
whenSome :: forall x exa.
(KnownValue x, exa :~> Maybe x, HasCallStack) =>
exa -> (Var x -> IndigoM ()) -> IndigoM ()
whenSome exa
c Var x -> IndigoM ()
f = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ exa -> (Var x -> IndigoM ()) -> IndigoM () -> IndigoFunction ()
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b, HasCallStack) =>
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
     , HasCallStack
     )
  => exa
  -> IndigoM ()
  -> IndigoM ()
whenNone :: forall x exa.
(KnownValue x, exa :~> Maybe x, HasCallStack) =>
exa -> IndigoM () -> IndigoM ()
whenNone exa
c IndigoM ()
f = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ exa -> (Var x -> IndigoM ()) -> IndigoM () -> IndigoFunction ()
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b, HasCallStack) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome exa
c (\Var x
_ -> () -> 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
     , HasCallStack
     )
  => ex
  -> (Var x -> IndigoM a)
  -> (Var y -> IndigoM b)
  -> IndigoM (RetVars a)
ifRight :: forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x, IfConstraint a b,
 HasCallStack) =>
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 = IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars b) -> IndigoM (RetVars b))
-> IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars b) -> IndigoM (RetVars b))
-> StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ Expr (Either y x)
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> StatementF IndigoM (RetVars a)
forall a1 b x y (freer :: * -> *).
(IfConstraint a1 b, KnownValue x, KnownValue y) =>
Expr (Either y x)
-> (Var x -> freer a1)
-> (Var y -> freer b)
-> StatementF freer (RetVars' (ClassifyReturnValue a1) a1)
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
     , HasCallStack
     )
  => ex
  -> (Var y -> IndigoM b)
  -> (Var x -> IndigoM a)
  -> IndigoM (RetVars a)
ifLeft :: forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x, IfConstraint a b,
 HasCallStack) =>
ex
-> (Var y -> IndigoM b)
-> (Var x -> IndigoM a)
-> IndigoM (RetVars a)
ifLeft ex
ex Var y -> IndigoM b
lb Var x -> IndigoM a
rb = IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars b) -> IndigoM (RetVars b))
-> IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ 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,
 HasCallStack) =>
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
     , HasCallStack
     )
  => ex
  -> (Var x -> IndigoM ())
  -> IndigoM ()
whenRight :: forall x y ex.
(KnownValue x, KnownValue y, ex :~> Either y x, HasCallStack) =>
ex -> (Var x -> IndigoM ()) -> IndigoM ()
whenRight ex
c Var x -> IndigoM ()
f = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex
-> (Var x -> IndigoM ())
-> (Var y -> IndigoM ())
-> IndigoFunction ()
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x, IfConstraint a b,
 HasCallStack) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
c Var x -> IndigoM ()
f (\Var y
_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

whenLeft
  :: forall x y ex .
     ( KnownValue x
     , KnownValue y
     , ex :~> Either y x
     , HasCallStack
     )
  => ex
  -> (Var y -> IndigoM ())
  -> IndigoM ()
whenLeft :: forall x y ex.
(KnownValue x, KnownValue y, ex :~> Either y x, HasCallStack) =>
ex -> (Var y -> IndigoM ()) -> IndigoM ()
whenLeft ex
c Var y -> IndigoM ()
f = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex
-> (Var x -> IndigoM ())
-> (Var y -> IndigoM ())
-> IndigoFunction ()
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x, IfConstraint a b,
 HasCallStack) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
c (\Var x
_ -> () -> 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, HasCallStack)
  => ex
  -> (Var x -> Var (List x) -> IndigoM a)
  -> IndigoM b
  -> IndigoM (RetVars a)
ifCons :: forall x a b ex.
(KnownValue x, ex :~> List x, IfConstraint a b, HasCallStack) =>
ex
-> (Var x -> Var (List x) -> IndigoM a)
-> IndigoM b
-> IndigoM (RetVars a)
ifCons ex
ex Var x -> Var (List x) -> IndigoM a
tb IndigoM b
fb = IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars b) -> IndigoM (RetVars b))
-> IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars b) -> IndigoM (RetVars b))
-> StatementF IndigoM (RetVars b) -> IndigoM (RetVars b)
forall a b. (a -> b) -> a -> b
$ Expr (List x)
-> (Var x -> Var (List x) -> IndigoM a)
-> IndigoM b
-> StatementF IndigoM (RetVars a)
forall a1 b x (freer :: * -> *).
(IfConstraint a1 b, KnownValue x) =>
Expr (List x)
-> (Var x -> Var (List x) -> freer a1)
-> freer b
-> StatementF freer (RetVars' (ClassifyReturnValue a1) a1)
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
     , HasCallStack
     )
  => guard
  -> clauses
  -> IndigoM (RetVars ret)
caseRec :: forall dt guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 guard :~> dt, HasCallStack) =>
guard -> clauses -> IndigoM (RetVars ret)
caseRec guard
g = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (clauses -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> clauses
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (clauses
    -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> clauses
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr dt
-> clauses
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall (freer :: * -> *) dt ret clauses.
CaseCommonF (IndigoMCaseClauseL freer) dt ret clauses =>
Expr dt
-> clauses
-> StatementF freer (RetVars' (ClassifyReturnValue ret) 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
     , HasCallStack
     )
  => guard
  -> IsoRecTuple clauses
  -> IndigoM (RetVars ret)
case_ :: forall dt guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 RecFromTuple clauses, guard :~> dt, HasCallStack) =>
guard -> IsoRecTuple clauses -> IndigoM (RetVars ret)
case_ guard
g = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
    -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr dt
-> clauses -> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall dt guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 guard :~> dt, HasCallStack) =>
guard -> clauses -> IndigoM (RetVars ret)
caseRec (guard -> Expr (ExprType guard)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr guard
g) (clauses -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
    -> clauses)
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
     , HasCallStack
     )
  => Proxy entrypointKind
  -> guard
  -> clauses
  -> IndigoM (RetVars ret)
entryCaseRec :: forall dt entrypointKind guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 DocumentEntrypoints entrypointKind dt, guard :~> dt,
 HasCallStack) =>
Proxy entrypointKind -> guard -> clauses -> IndigoM (RetVars ret)
entryCaseRec Proxy entrypointKind
proxy guard
g clauses
cls = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
    -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ Proxy entrypointKind
-> Expr dt
-> clauses
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall (freer :: * -> *) dt ret clauses entrypointKind.
(CaseCommonF (IndigoMCaseClauseL freer) dt ret clauses,
 DocumentEntrypoints entrypointKind dt) =>
Proxy entrypointKind
-> Expr dt
-> clauses
-> StatementF freer (RetVars' (ClassifyReturnValue ret) 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
     , HasCallStack
     )
  => Proxy entrypointKind
  -> guard
  -> IsoRecTuple clauses
  -> IndigoM (RetVars ret)
entryCase :: forall dt entrypointKind guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 RecFromTuple clauses, DocumentEntrypoints entrypointKind dt,
 guard :~> dt, HasCallStack) =>
Proxy entrypointKind
-> guard -> IsoRecTuple clauses -> IndigoM (RetVars ret)
entryCase Proxy entrypointKind
proxy guard
g = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
    -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy entrypointKind
-> guard
-> clauses
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall dt entrypointKind guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) dt ret clauses,
 DocumentEntrypoints entrypointKind dt, guard :~> dt,
 HasCallStack) =>
Proxy entrypointKind -> guard -> clauses -> IndigoM (RetVars ret)
entryCaseRec Proxy entrypointKind
proxy guard
g (clauses -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
    -> clauses)
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses dt))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
     , HasCallStack
     )
  => guard
  -> IsoRecTuple clauses
  -> IndigoM (RetVars ret)
entryCaseSimple :: forall cp guard ret clauses.
(CaseCommonF (IndigoMCaseClauseL IndigoM) cp ret clauses,
 RecFromTuple clauses, DocumentEntrypoints PlainEntrypointsKind cp,
 NiceParameterFull cp, RequireFlatParamEps cp, guard :~> cp,
 HasCallStack) =>
guard -> IsoRecTuple clauses -> IndigoM (RetVars ret)
entryCaseSimple guard
g = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses cp))
    -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses cp))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses cp))
    -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses cp))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr cp
-> clauses
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall (freer :: * -> *) cp ret clauses.
(CaseCommonF (IndigoMCaseClauseL freer) cp ret clauses,
 DocumentEntrypoints PlainEntrypointsKind cp, NiceParameterFull cp,
 RequireFlatParamEps cp) =>
Expr cp
-> clauses
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
EntryCaseSimple (guard -> Expr (ExprType guard)
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr guard
g) (clauses
 -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (IsoRecTuple
      (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses cp))
    -> clauses)
-> IsoRecTuple
     (Rec (IndigoMCaseClauseL IndigoM ret) (CaseClauses cp))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. RecFromTuple r => IsoRecTuple r -> r
recFromTuple @clauses

{-# DEPRECATED (//->) "use '#=' instead" #-}
-- | An alias for '#=' kept only for backward compatibility.
(//->)
  :: ( name ~ (AppendSymbol "c" ctor)
     , KnownValue x
     , ScopeCodeGen retBr
     , ret ~ RetExprs retBr
     , RetOutStack ret ~ RetOutStack retBr
     )
  => Label name
  -> (Var x -> IndigoM retBr)
  -> IndigoMCaseClauseL IndigoM ret ('CaseClauseParam ctor ('OneField x))
//-> :: forall (name :: Symbol) (ctor :: Symbol) x retBr ret.
(name ~ AppendSymbol "c" ctor, KnownValue x, ScopeCodeGen retBr,
 ret ~ RetExprs retBr, RetOutStack ret ~ RetOutStack retBr) =>
Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
(//->) Label name
cName Var x -> IndigoM retBr
b = Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
forall (name :: Symbol) (ctor :: Symbol) x retBr ret
       (freer :: * -> *).
(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.
(#=)
  :: ( name ~ (AppendSymbol "c" ctor)
     , KnownValue x
     , ScopeCodeGen retBr
     , ret ~ RetExprs retBr
     , RetOutStack ret ~ RetOutStack retBr
     )
  => Label name
  -> (Var x -> IndigoM retBr)
  -> IndigoMCaseClauseL IndigoM ret ('CaseClauseParam ctor ('OneField x))
#= :: forall (name :: Symbol) (ctor :: Symbol) x retBr ret.
(name ~ AppendSymbol "c" ctor, KnownValue x, ScopeCodeGen retBr,
 ret ~ RetExprs retBr, RetOutStack ret ~ RetOutStack retBr) =>
Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
(#=) Label name
cName Var x -> IndigoM retBr
b = Label name
-> (Var x -> IndigoM retBr)
-> IndigoMCaseClauseL
     IndigoM ret ('CaseClauseParam ctor ('OneField x))
forall (name :: Symbol) (ctor :: Symbol) x retBr ret
       (freer :: * -> *).
(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, HasCallStack)
  => IndigoM a
  -> IndigoFunction a
scope :: forall a.
(ScopeCodeGen a, HasCallStack) =>
IndigoM a -> IndigoFunction a
scope = IndigoM (RetVars' (ClassifyReturnValue a) a)
-> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue a) a)
 -> IndigoM (RetVars' (ClassifyReturnValue a) a))
-> (IndigoM a -> IndigoM (RetVars' (ClassifyReturnValue a) a))
-> IndigoM a
-> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM (RetVars' (ClassifyReturnValue a) a)
-> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue a) a)
 -> IndigoM (RetVars' (ClassifyReturnValue a) a))
-> (IndigoM a
    -> StatementF IndigoM (RetVars' (ClassifyReturnValue a) a))
-> IndigoM a
-> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IndigoM a
-> StatementF IndigoM (RetVars' (ClassifyReturnValue a) a)
forall a1 (freer :: * -> *).
ScopeCodeGen a1 =>
freer a1 -> StatementF freer (RetVars' (ClassifyReturnValue a1) a1)
Scope

-- | Alias for 'scope' we use in the tutorial.
defFunction
  :: forall a . (ScopeCodeGen a, HasCallStack)
  => IndigoM a
  -> IndigoFunction a
defFunction :: forall a.
(ScopeCodeGen a, HasCallStack) =>
IndigoM a -> IndigoFunction a
defFunction = IndigoM (RetVars' (ClassifyReturnValue a) a)
-> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue a) a)
 -> IndigoM (RetVars' (ClassifyReturnValue a) a))
-> (IndigoM a -> IndigoM (RetVars' (ClassifyReturnValue a) a))
-> IndigoM a
-> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IndigoM a -> IndigoM (RetVars' (ClassifyReturnValue a) a)
forall a.
(ScopeCodeGen a, HasCallStack) =>
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
  :: HasCallStack
  => ((HasSideEffects, IsNotInView) => IndigoM ())
  -> ((HasSideEffects, IsNotInView) => IndigoProcedure)
defContract :: HasCallStack =>
((HasSideEffects, IsNotInView) => IndigoM ())
-> (HasSideEffects, IsNotInView) => IndigoM ()
defContract (HasSideEffects, IsNotInView) => IndigoM ()
c = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoFunction ()
forall a.
(ScopeCodeGen a, HasCallStack) =>
IndigoM a -> IndigoFunction a
scope IndigoM ()
(HasSideEffects, IsNotInView) => IndigoM ()
c)

-- | 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
  , HasCallStack
  )
  => String
  -> (Var (ExprType argExpr) -> IndigoM res)
  -> (argExpr -> IndigoM (RetVars res))
defNamedEffLambda1 :: forall st argExpr res.
(ToExpr argExpr, Typeable res,
 ExecuteLambdaEff1C st (ExprType argExpr) res,
 CreateLambdaEff1C st (ExprType argExpr) res, HasCallStack) =>
[Char]
-> (Var (ExprType argExpr) -> IndigoM res)
-> argExpr
-> IndigoM (RetVars res)
defNamedEffLambda1 [Char]
lName Var (ExprType argExpr) -> IndigoM res
body = \argExpr
ex ->
  IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars res) -> IndigoM (RetVars res))
-> IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ 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
$ LambdaKind st (ExprType argExpr) res '[st, Ops]
-> [Char]
-> (Var (ExprType argExpr) -> IndigoM res)
-> Expr (ExprType argExpr)
-> StatementF IndigoM (RetVars res)
forall st arg res (extra :: [*]) (freer :: * -> *).
LambdaKind st arg res extra
-> [Char]
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars' (ClassifyReturnValue res) res)
LambdaCall1 (Proxy st -> LambdaKind st (ExprType argExpr) res '[st, Ops]
forall st arg res.
(ExecuteLambdaEff1C st arg res,
 CreateLambda1CGeneric '[st, Ops] arg res, Typeable res) =>
Proxy st -> LambdaKind st arg res '[st, Ops]
EffLambda (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @st)) [Char]
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
  , HasCallStack
  )
  => String
  -> (Var (ExprType argExpr) -> IndigoM res)
  -> (argExpr -> IndigoM (RetVars res))
defNamedLambda1 :: forall st argExpr res.
(ToExpr argExpr, Typeable res,
 ExecuteLambda1C st (ExprType argExpr) res,
 CreateLambda1C st (ExprType argExpr) res, HasCallStack) =>
[Char]
-> (Var (ExprType argExpr) -> IndigoM res)
-> argExpr
-> IndigoM (RetVars res)
defNamedLambda1 [Char]
lName Var (ExprType argExpr) -> IndigoM res
body = \argExpr
ex ->
  IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars res) -> IndigoM (RetVars res))
-> IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ 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
$ LambdaKind st (ExprType argExpr) res '[st]
-> [Char]
-> (Var (ExprType argExpr) -> IndigoM res)
-> Expr (ExprType argExpr)
-> StatementF IndigoM (RetVars res)
forall st arg res (extra :: [*]) (freer :: * -> *).
LambdaKind st arg res extra
-> [Char]
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars' (ClassifyReturnValue res) res)
LambdaCall1 (Proxy st -> LambdaKind st (ExprType argExpr) res '[st]
forall st arg res.
(ExecuteLambda1C st arg res, CreateLambda1CGeneric '[st] arg res,
 Typeable res) =>
Proxy st -> LambdaKind st arg res '[st]
StorageLambda (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @st)) [Char]
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
  , HasCallStack
  )
  => String
  -> IndigoM res
  -> IndigoM (RetVars res)
defNamedLambda0 :: forall st res.
(Typeable res, ExecuteLambda1C st () res, CreateLambda1C st () res,
 HasCallStack) =>
[Char] -> IndigoM res -> IndigoM (RetVars res)
defNamedLambda0 [Char]
lName IndigoM res
body =
  IndigoM (RetVars' (ClassifyReturnValue res) res)
-> IndigoM (RetVars' (ClassifyReturnValue res) res)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue res) res)
 -> IndigoM (RetVars' (ClassifyReturnValue res) res))
-> IndigoM (RetVars' (ClassifyReturnValue res) res)
-> IndigoM (RetVars' (ClassifyReturnValue res) res)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars' (ClassifyReturnValue res) res)
-> IndigoM (RetVars' (ClassifyReturnValue res) res)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue res) res)
 -> IndigoM (RetVars' (ClassifyReturnValue res) res))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue res) res)
-> IndigoM (RetVars' (ClassifyReturnValue res) res)
forall a b. (a -> b) -> a -> b
$ LambdaKind st () res '[st]
-> [Char]
-> (Var () -> IndigoM res)
-> Expr ()
-> StatementF IndigoM (RetVars' (ClassifyReturnValue res) res)
forall st arg res (extra :: [*]) (freer :: * -> *).
LambdaKind st arg res extra
-> [Char]
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars' (ClassifyReturnValue res) res)
LambdaCall1 (Proxy st -> LambdaKind st () res '[st]
forall st arg res.
(ExecuteLambda1C st arg res, CreateLambda1CGeneric '[st] arg res,
 Typeable res) =>
Proxy st -> LambdaKind st arg res '[st]
StorageLambda (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @st)) [Char]
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
  , HasCallStack
  )
  => String
  -> (Var (ExprType argExpr) -> IndigoM res)
  -> (argExpr -> IndigoM (RetVars res))
defNamedPureLambda1 :: forall argExpr res.
(ToExpr argExpr, Typeable res,
 ExecuteLambdaPure1C (ExprType argExpr) res,
 CreateLambdaPure1C (ExprType argExpr) res, HasCallStack) =>
[Char]
-> (Var (ExprType argExpr) -> IndigoM res)
-> argExpr
-> IndigoM (RetVars res)
defNamedPureLambda1 [Char]
lName Var (ExprType argExpr) -> IndigoM res
body = \argExpr
ex ->
  IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars res) -> IndigoM (RetVars res))
-> IndigoM (RetVars res) -> IndigoM (RetVars res)
forall a b. (a -> b) -> a -> b
$ 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
$ LambdaKind Any (ExprType argExpr) res '[]
-> [Char]
-> (Var (ExprType argExpr) -> IndigoM res)
-> Expr (ExprType argExpr)
-> StatementF IndigoM (RetVars res)
forall st arg res (extra :: [*]) (freer :: * -> *).
LambdaKind st arg res extra
-> [Char]
-> (Var arg -> freer res)
-> Expr arg
-> StatementF freer (RetVars' (ClassifyReturnValue res) res)
LambdaCall1 LambdaKind Any (ExprType argExpr) res '[]
forall arg res st.
(ExecuteLambdaPure1C arg res, CreateLambda1CGeneric '[] arg res,
 Typeable res) =>
LambdaKind st arg res '[]
PureLambda [Char]
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, HasCallStack) => ex -> IndigoM () -> IndigoM ()
while :: forall exc.
(exc :~> Bool, HasCallStack) =>
exc -> IndigoM () -> IndigoM ()
while ex
e IndigoM ()
body = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ 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
     , HasCallStack
     )
  => ex
  -> (Var y -> IndigoM ())
  -> IndigoM (Var x)
whileLeft :: forall x y ex.
(ex :~> Either y x, KnownValue y, KnownValue x, HasCallStack) =>
ex -> (Var y -> IndigoM ()) -> IndigoM (Var x)
whileLeft ex
e Var y -> IndigoM ()
body = IndigoM (Var x) -> IndigoM (Var x)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var x) -> IndigoM (Var x))
-> IndigoM (Var x) -> IndigoM (Var x)
forall a b. (a -> b) -> a -> b
$ 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, HasCallStack)
  => e -> (Var (IterOpElHs a) -> IndigoM ())
  -> IndigoM ()
forEach :: forall a e.
(IterOpHs a, KnownValue (IterOpElHs a), e :~> a, HasCallStack) =>
e -> (Var (IterOpElHs a) -> IndigoM ()) -> IndigoM ()
forEach e
container Var (IterOpElHs a) -> IndigoM ()
body = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ 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 a1 (freer :: * -> *).
(IterOpHs a1, KnownValue (IterOpElHs a1)) =>
Expr a1 -> (Var (IterOpElHs a1) -> 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, HasCallStack) => di -> IndigoM ()
doc :: forall di. (DocItem di, HasCallStack) => di -> IndigoM ()
doc di
di = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ (forall (inp :: [*]). SomeIndigoState inp) -> IndigoM ()
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 :: [*]).
IndigoState inp out -> SomeIndigoState inp
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 :: (DocItem di, HasCallStack) => (SubDoc -> di) -> IndigoM () -> IndigoM ()
docGroup :: forall di.
(DocItem di, HasCallStack) =>
(SubDoc -> di) -> IndigoM () -> IndigoM ()
docGroup SubDoc -> di
f IndigoM ()
i = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> StatementF IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ (SubDoc -> di) -> IndigoM () -> StatementF IndigoM ()
forall di (freer :: * -> *).
DocItem di =>
(SubDoc -> di) -> freer () -> StatementF freer ()
DocGroup SubDoc -> di
f IndigoM ()
i

-- | Insert documentation of the contract's storage type. The type
-- should be passed using type applications.
{-# DEPRECATED docStorage "Use `doc (dStorage @storage)` instead." #-}
docStorage :: forall storage. (TypeHasDoc storage, HasCallStack) => IndigoM ()
docStorage :: forall storage. (TypeHasDoc storage, HasCallStack) => IndigoM ()
docStorage = DStorageType -> IndigoM ()
forall di. (DocItem di, HasCallStack) => di -> IndigoM ()
doc (DStorageType -> IndigoM ()) -> DStorageType -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ forall store. TypeHasDoc store => DStorageType
dStorage @storage

-- | Give a name to the given contract. Apply it to the whole contract code.
{-# DEPRECATED contractName "Use `docGroup name` instead." #-}
contractName :: HasCallStack => Text -> IndigoM () -> IndigoM ()
contractName :: HasCallStack => Text -> IndigoM () -> IndigoM ()
contractName = (SubDoc -> DName) -> IndigoM () -> IndigoM ()
forall di.
(DocItem di, HasCallStack) =>
(SubDoc -> di) -> IndigoM () -> IndigoM ()
docGroup ((SubDoc -> DName) -> IndigoM () -> IndigoM ())
-> (Text -> SubDoc -> DName) -> Text -> IndigoM () -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> SubDoc -> DName
DName

-- | Attach general info to the given contract.
{-# DEPRECATED contractGeneral "Use `docGroup DGeneralInfoSection` instead." #-}
contractGeneral :: HasCallStack => IndigoM () -> IndigoM ()
contractGeneral :: HasCallStack => IndigoM () -> IndigoM ()
contractGeneral = (SubDoc -> DGeneralInfoSection) -> IndigoM () -> IndigoM ()
forall di.
(DocItem di, HasCallStack) =>
(SubDoc -> di) -> IndigoM () -> IndigoM ()
docGroup SubDoc -> DGeneralInfoSection
DGeneralInfoSection

-- | Attach default general info to the contract documentation.
contractGeneralDefault :: HasCallStack => IndigoM ()
contractGeneralDefault :: HasCallStack => IndigoM ()
contractGeneralDefault = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ (forall (inp :: [*]). SomeIndigoState inp) -> IndigoM ()
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 :: [*]).
IndigoState inp out -> SomeIndigoState inp
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.
     ( ToExpr param
     , NiceParameterFull (ExprType param)
     , RequireSumType (ExprType param)
     , HasCallStack
     )
  => (Var (ExprType param) -> IndigoM ()) -> param -> IndigoM ()
finalizeParamCallingDoc :: forall param.
(ToExpr param, NiceParameterFull (ExprType param),
 RequireSumType (ExprType param), HasCallStack) =>
(Var (ExprType param) -> IndigoM ()) -> param -> IndigoM ()
finalizeParamCallingDoc Var (ExprType param) -> IndigoM ()
i = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (param -> IndigoM ()) -> param -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> (param -> StatementF IndigoM ()) -> param -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var (ExprType param) -> IndigoM ())
-> Expr (ExprType param) -> StatementF IndigoM ()
forall cp (freer :: * -> *).
(NiceParameterFull cp, RequireSumType cp, HasCallStack) =>
(Var cp -> freer ()) -> Expr cp -> StatementF freer ()
FinalizeParamCallingDoc Var (ExprType param) -> IndigoM ()
i (Expr (ExprType param) -> StatementF IndigoM ())
-> (param -> Expr (ExprType param))
-> param
-> StatementF IndigoM ()
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 :: HasCallStack => Markdown -> IndigoM ()
description :: HasCallStack => Markdown -> IndigoM ()
description = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Markdown -> IndigoM ()) -> Markdown -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DDescription -> IndigoM ()
forall di. (DocItem di, HasCallStack) => 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 :: HasCallStack => Text -> IndigoM ()
anchor :: HasCallStack => Text -> IndigoM ()
anchor = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Text -> IndigoM ()) -> Text -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DAnchor -> IndigoM ()
forall di. (DocItem di, HasCallStack) => 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, HasCallStack) => a -> IndigoM ()
example :: forall a. (NiceParameter a, HasCallStack) => a -> IndigoM ()
example = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> (a -> IndigoM ()) -> a -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DEntrypointExample -> IndigoM ()
forall di. (DocItem di, HasCallStack) => 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)
     , IsoValue (ContractRef (GetEntrypointArgCustom p mname))
     , HasCallStack, IsNotInView
     )
  => EntrypointRef mname
  -> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
selfCalling :: forall p (mname :: Maybe Symbol).
(NiceParameterFull p, KnownValue (GetEntrypointArgCustom p mname),
 IsoValue (ContractRef (GetEntrypointArgCustom p mname)),
 HasCallStack, IsNotInView) =>
EntrypointRef mname
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
selfCalling EntrypointRef mname
ep = IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
 -> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname))))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a b. (a -> b) -> a -> b
$ StatementF
  IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF
   IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
 -> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname))))
-> StatementF
     IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
-> IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall a b. (a -> b) -> a -> b
$ Proxy p
-> EntrypointRef mname
-> StatementF
     IndigoM (Var (ContractRef (GetEntrypointArgCustom p mname)))
forall p (mname :: Maybe Symbol) (freer :: * -> *).
(NiceParameterFull p, KnownValue (GetEntrypointArgCustom p mname),
 IsoValue (ContractRef (GetEntrypointArgCustom p mname)),
 IsNotInView) =>
Proxy p
-> EntrypointRef mname
-> StatementF
     freer (Var (ContractRef (GetEntrypointArgCustom p mname)))
SelfCalling (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @p) EntrypointRef mname
ep

contractCalling
  :: forall cp vd epRef epArg addr exAddr.
     ( HasEntrypointArg cp epRef epArg
     , ToTAddress cp vd addr
     , ToT addr ~ ToT Address
     , exAddr :~> addr
     , KnownValue epArg
     , IsoValue (ContractRef epArg)
     , HasCallStack
     )
  => epRef -> exAddr -> IndigoM (Var (Maybe (ContractRef epArg)))
contractCalling :: forall cp vd epRef epArg addr exAddr.
(HasEntrypointArg cp epRef epArg, ToTAddress cp vd addr,
 ToT addr ~ ToT Address, exAddr :~> addr, KnownValue epArg,
 IsoValue (ContractRef epArg), HasCallStack) =>
epRef -> exAddr -> IndigoM (Var (Maybe (ContractRef epArg)))
contractCalling epRef
epRef = IndigoM (Var (Maybe (ContractRef epArg)))
-> IndigoM (Var (Maybe (ContractRef epArg)))
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var (Maybe (ContractRef epArg)))
 -> IndigoM (Var (Maybe (ContractRef epArg))))
-> (exAddr -> IndigoM (Var (Maybe (ContractRef epArg))))
-> exAddr
-> IndigoM (Var (Maybe (ContractRef epArg)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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, vd)
-> epRef
-> Expr addr
-> StatementF IndigoM (Var (Maybe (ContractRef epArg)))
forall cp epRef epArg vd addr (freer :: * -> *).
(HasEntrypointArg cp epRef epArg, ToTAddress cp vd addr,
 ToT addr ~ ToT Address, KnownValue epArg,
 IsoValue (ContractRef epArg)) =>
Proxy (cp, vd)
-> epRef
-> Expr addr
-> StatementF freer (Var (Maybe (ContractRef epArg)))
ContractCalling (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @(cp, vd)) 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
     , HasCallStack
     , IsNotInView
     )
  => exp -> exm -> exc -> IndigoM ()
transferTokens :: forall exp p exm exc.
(IsExpr exp p, IsExpr exm Mutez, IsExpr exc (ContractRef p),
 NiceParameter p, HasSideEffects, HasCallStack, IsNotInView) =>
exp -> exm -> exc -> IndigoM ()
transferTokens exp
ep exm
em exc
ec = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ 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, IsNotInView) =>
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), HasCallStack, IsNotInView)
  => ex -> IndigoM ()
setDelegate :: forall ex.
(HasSideEffects, IsExpr ex (Maybe KeyHash), HasCallStack,
 IsNotInView) =>
ex -> IndigoM ()
setDelegate = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (ex -> IndigoM ()) -> ex -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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, IsNotInView) =>
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
  :: forall st exk exm exs param.
     ( IsObject st
     , IsExpr exk (Maybe KeyHash), IsExpr exm Mutez, IsExpr exs st
     , NiceStorageFull st, NiceParameterFull param
     , HasSideEffects
     , HasCallStack
     , IsNotInView
     )
  => (HasStorage st => Var param -> IndigoM ())
  -> exk
  -> exm
  -> exs
  -> IndigoM (Var Address)
createContract :: forall st exk exm exs param.
(IsObject st, IsExpr exk (Maybe KeyHash), IsExpr exm Mutez,
 IsExpr exs st, NiceStorageFull st, NiceParameterFull param,
 HasSideEffects, HasCallStack, IsNotInView) =>
(HasStorage st => Var param -> IndigoM ())
-> exk -> exm -> exs -> IndigoM (Var Address)
createContract HasStorage st => Var param -> IndigoM ()
iCtr exk
ek exm
em exs
es = IndigoM (Var Address) -> IndigoM (Var Address)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var Address) -> IndigoM (Var Address))
-> IndigoM (Var Address) -> IndigoM (Var Address)
forall a b. (a -> b) -> a -> b
$ 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
$
  -- pva701: we don't have GenCodeHooks at this point so we just pass empty ones
  -- Maybe we should pass this hooks via 'given'
  Contract param st ()
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF IndigoM (Var Address)
forall st param vd (freer :: * -> *).
(IsObject st, NiceStorage st, NiceParameterFull param,
 HasSideEffects, NiceViewsDescriptor vd, Typeable vd,
 IsNotInView) =>
Contract param st vd
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF freer (Var Address)
CreateContract (ContractCode param st -> Contract param st ()
forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
ContractCode cp st -> Contract cp st ()
mkContract (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, NiceViewsDescriptor vd, Typeable vd
     , HasSideEffects
     , HasCallStack
     , IsNotInView
     )
  => L.Contract param st vd
  -> exk
  -> exm
  -> exs
  -> IndigoM (Var Address)
createLorentzContract :: forall st exk exm exs param vd.
(IsObject st, IsExpr exk (Maybe KeyHash), IsExpr exm Mutez,
 IsExpr exs st, NiceStorage st, NiceParameterFull param,
 NiceViewsDescriptor vd, Typeable vd, HasSideEffects, HasCallStack,
 IsNotInView) =>
Contract param st vd -> exk -> exm -> exs -> IndigoM (Var Address)
createLorentzContract Contract param st vd
lCtr exk
ek exm
em exs
es = IndigoM (Var Address) -> IndigoM (Var Address)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (Var Address) -> IndigoM (Var Address))
-> IndigoM (Var Address) -> IndigoM (Var Address)
forall a b. (a -> b) -> a -> b
$ 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 vd
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF IndigoM (Var Address)
forall st param vd (freer :: * -> *).
(IsObject st, NiceStorage st, NiceParameterFull param,
 HasSideEffects, NiceViewsDescriptor vd, Typeable vd,
 IsNotInView) =>
Contract param st vd
-> Expr (Maybe KeyHash)
-> Expr Mutez
-> Expr st
-> StatementF freer (Var Address)
CreateContract Contract param st vd
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)

-- | @emit tag expression@ emits a contract event with textual tag @tag@ and
-- payload @expression@.
--
-- The tag must be a field annotation. Use 'annQ' quoter to construct it from a
-- literal, for example:
--
-- > emit [annQ|tag|] ()
emit
  :: (HasSideEffects, NicePackedValue a, HasAnnotation a, HasCallStack)
  => FieldAnn -> Expr a -> IndigoM ()
emit :: forall a.
(HasSideEffects, NicePackedValue a, HasAnnotation a,
 HasCallStack) =>
FieldAnn -> Expr a -> IndigoM ()
emit FieldAnn
tag Expr a
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM () -> IndigoM ()
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM () -> IndigoM ())
-> StatementF IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ FieldAnn -> Expr a -> StatementF IndigoM ()
forall a1 (freer :: * -> *).
(HasSideEffects, NicePackedValue a1, HasAnnotation a1) =>
FieldAnn -> Expr a1 -> StatementF freer ()
Emit FieldAnn
tag Expr a
ex

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

failWith
  :: forall ret a ex.
     (NiceConstant a, IsExpr ex a, ReturnableValue ret, HasCallStack)
  => ex -> IndigoM (RetVars ret)
failWith :: forall ret a ex.
(NiceConstant a, IsExpr ex a, ReturnableValue ret, HasCallStack) =>
ex -> IndigoM (RetVars ret)
failWith = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (ex -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> ex
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (ex
    -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> ex
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy ret
-> (forall (inp :: [*]). Expr a -> SomeIndigoState inp)
-> Expr a
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall ret a1 (freer :: * -> *).
ReturnableValue ret =>
Proxy ret
-> (forall (inp :: [*]). Expr a1 -> SomeIndigoState inp)
-> Expr a1
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
FailOver (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret) (IndigoState inp Any -> SomeIndigoState inp
forall (inp :: [*]) (out :: [*]).
IndigoState inp out -> SomeIndigoState inp
toSIS (IndigoState inp Any -> SomeIndigoState inp)
-> (Expr a -> IndigoState inp Any) -> Expr a -> SomeIndigoState inp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr a -> IndigoState inp Any
forall a (s :: [*]) (t :: [*]).
NiceConstant a =>
Expr a -> IndigoState s t
B.failWith) (Expr a
 -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (ex -> Expr a)
-> ex
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr a
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

failUsing_
  :: forall ret x. (IsError x, Buildable x, ReturnableValue ret, HasCallStack)
  => x -> IndigoM (RetVars ret)
failUsing_ :: forall ret x.
(IsError x, Buildable x, ReturnableValue ret, HasCallStack) =>
x -> IndigoM (RetVars ret)
failUsing_ x
x = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall ret (freer :: * -> *).
ReturnableValue ret =>
Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
Fail (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret) (IndigoState inp Any -> SomeIndigoState inp
forall (inp :: [*]) (out :: [*]).
IndigoState inp out -> SomeIndigoState inp
toSIS (IndigoState inp Any -> SomeIndigoState inp)
-> IndigoState inp Any -> SomeIndigoState inp
forall a b. (a -> b) -> a -> b
$ x -> IndigoState inp Any
forall x (s :: [*]) (t :: [*]).
(IsError x, Buildable x) =>
x -> IndigoState s t
B.failUsing_ x
x)

failCustom
  :: forall ret tag err ex.
     ( ReturnableValue ret
     , MustHaveErrorArg tag (MText, err)
     , CustomErrorHasDoc tag
     , NiceConstant err
     , ex :~> err
     , HasCallStack
     )
  => Label tag -> ex -> IndigoM (RetVars ret)
failCustom :: forall ret (tag :: Symbol) err ex.
(ReturnableValue ret, MustHaveErrorArg tag (MText, err),
 CustomErrorHasDoc tag, NiceConstant err, ex :~> err,
 HasCallStack) =>
Label tag -> ex -> IndigoM (RetVars ret)
failCustom Label tag
l = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (ex -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> ex
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (ex
    -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> ex
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy ret
-> (forall (inp :: [*]). Expr err -> SomeIndigoState inp)
-> Expr err
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall ret a1 (freer :: * -> *).
ReturnableValue ret =>
Proxy ret
-> (forall (inp :: [*]). Expr a1 -> SomeIndigoState inp)
-> Expr a1
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
FailOver (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret) (IndigoState inp Any -> SomeIndigoState inp
forall (inp :: [*]) (out :: [*]).
IndigoState inp out -> SomeIndigoState inp
toSIS (IndigoState inp Any -> SomeIndigoState inp)
-> (Expr err -> IndigoState inp Any)
-> Expr err
-> SomeIndigoState inp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label tag -> Expr err -> IndigoState inp Any
forall (tag :: Symbol) err (s :: [*]) (t :: [*]).
(MustHaveErrorArg tag (MText, err), CustomErrorHasDoc tag,
 NiceConstant err) =>
Label tag -> Expr err -> IndigoState s t
B.failCustom Label tag
l) (Expr err
 -> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> (ex -> Expr err)
-> ex
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ex -> Expr err
forall a. ToExpr a => a -> Expr (ExprType a)
toExpr

failCustom_
  :: forall ret tag.
     ( ReturnableValue ret
     , MustHaveErrorArg tag (MText, ())
     , CustomErrorHasDoc tag
     , HasCallStack
     )
  => Label tag -> IndigoM (RetVars ret)
failCustom_ :: forall ret (tag :: Symbol).
(ReturnableValue ret, MustHaveErrorArg tag (MText, ()),
 CustomErrorHasDoc tag, HasCallStack) =>
Label tag -> IndigoM (RetVars ret)
failCustom_ Label tag
tag = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall ret (freer :: * -> *).
ReturnableValue ret =>
Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
Fail (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret) (IndigoState inp Any -> SomeIndigoState inp
forall (inp :: [*]) (out :: [*]).
IndigoState inp out -> SomeIndigoState inp
toSIS (IndigoState inp Any -> SomeIndigoState inp)
-> IndigoState inp Any -> SomeIndigoState inp
forall a b. (a -> b) -> a -> b
$ Label tag -> IndigoState inp Any
forall (tag :: Symbol) (s :: [*]) (t :: [*]).
(MustHaveErrorArg tag (MText, ()), CustomErrorHasDoc tag) =>
Label tag -> IndigoState s t
B.failCustom_ Label tag
tag)

failCustomNoArg
  :: forall ret tag.
     ( ReturnableValue ret
     , MustHaveErrorArg tag MText
     , CustomErrorHasDoc tag
     , HasCallStack
     )
  => Label tag -> IndigoM (RetVars ret)
failCustomNoArg :: forall ret (tag :: Symbol).
(ReturnableValue ret, MustHaveErrorArg tag MText,
 CustomErrorHasDoc tag, HasCallStack) =>
Label tag -> IndigoM (RetVars ret)
failCustomNoArg Label tag
tag = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall ret (freer :: * -> *).
ReturnableValue ret =>
Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
Fail (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret) (IndigoState inp Any -> SomeIndigoState inp
forall (inp :: [*]) (out :: [*]).
IndigoState inp out -> SomeIndigoState inp
toSIS (IndigoState inp Any -> SomeIndigoState inp)
-> IndigoState inp Any -> SomeIndigoState inp
forall a b. (a -> b) -> a -> b
$ Label tag -> IndigoState inp Any
forall (tag :: Symbol) (s :: [*]) (t :: [*]).
(MustHaveErrorArg tag MText, CustomErrorHasDoc tag) =>
Label tag -> IndigoState s t
B.failCustomNoArg Label tag
tag)

failUnexpected_
  :: forall ret. (ReturnableValue ret, HasCallStack)
  => MText -> IndigoM (RetVars ret)
failUnexpected_ :: forall ret.
(ReturnableValue ret, HasCallStack) =>
MText -> IndigoM (RetVars ret)
failUnexpected_ MText
tx = IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a. StatementF IndigoM a -> IndigoM a
oneIndigoM (StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
 -> IndigoM (RetVars' (ClassifyReturnValue ret) ret))
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
-> IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall a b. (a -> b) -> a -> b
$ Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF IndigoM (RetVars' (ClassifyReturnValue ret) ret)
forall ret (freer :: * -> *).
ReturnableValue ret =>
Proxy ret
-> (forall (inp :: [*]). SomeIndigoState inp)
-> StatementF freer (RetVars' (ClassifyReturnValue ret) ret)
Fail (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret) (IndigoState inp Any -> SomeIndigoState inp
forall (inp :: [*]) (out :: [*]).
IndigoState inp out -> SomeIndigoState inp
toSIS (IndigoState inp Any -> SomeIndigoState inp)
-> IndigoState inp Any -> SomeIndigoState inp
forall a b. (a -> b) -> a -> b
$ MText -> IndigoState inp Any
forall (s :: [*]) (t :: [*]). MText -> IndigoState s t
B.failUnexpected_ MText
tx)

assert
  :: forall x ex.
     ( IsError x
     , Buildable x
     , IsExpr ex Bool
     , HasCallStack
     )
  => x -> ex -> IndigoM ()
assert :: forall x ex.
(IsError x, Buildable x, IsExpr ex Bool, HasCallStack) =>
x -> ex -> IndigoM ()
assert x
err ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex -> IndigoM () -> IndigoM () -> IndigoFunction ()
forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ ex
ex (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (forall ret x.
(IsError x, Buildable x, ReturnableValue ret, HasCallStack) =>
x -> IndigoM (RetVars ret)
failUsing_ @() x
err)

assertCustom
  :: forall tag err errEx ex.
     ( MustHaveErrorArg tag (MText, err)
     , CustomErrorHasDoc tag
     , NiceConstant err
     , IsExpr errEx err
     , IsExpr ex Bool
     , HasCallStack
     )
  => Label tag -> errEx -> ex -> IndigoM ()
assertCustom :: forall (tag :: Symbol) err errEx ex.
(MustHaveErrorArg tag (MText, err), CustomErrorHasDoc tag,
 NiceConstant err, IsExpr errEx err, IsExpr ex Bool,
 HasCallStack) =>
Label tag -> errEx -> ex -> IndigoM ()
assertCustom Label tag
tag errEx
errEx ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex -> IndigoM () -> IndigoM () -> IndigoFunction ()
forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ ex
ex (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (forall ret (tag :: Symbol) err ex.
(ReturnableValue ret, MustHaveErrorArg tag (MText, err),
 CustomErrorHasDoc tag, NiceConstant err, ex :~> err,
 HasCallStack) =>
Label tag -> ex -> IndigoM (RetVars ret)
failCustom @() Label tag
tag errEx
errEx)

assertCustom_
  :: forall tag ex.
     ( MustHaveErrorArg tag (MText, ())
     , CustomErrorHasDoc tag
     , IsExpr ex Bool
     , HasCallStack
     )
  => Label tag -> ex -> IndigoM ()
assertCustom_ :: forall (tag :: Symbol) ex.
(MustHaveErrorArg tag (MText, ()), CustomErrorHasDoc tag,
 IsExpr ex Bool, HasCallStack) =>
Label tag -> ex -> IndigoM ()
assertCustom_ Label tag
tag ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex -> IndigoM () -> IndigoM () -> IndigoFunction ()
forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ ex
ex (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (forall ret (tag :: Symbol).
(ReturnableValue ret, MustHaveErrorArg tag (MText, ()),
 CustomErrorHasDoc tag, HasCallStack) =>
Label tag -> IndigoM (RetVars ret)
failCustom_ @() Label tag
tag)

assertCustomNoArg
  :: forall tag ex.
     ( MustHaveErrorArg tag MText
     , CustomErrorHasDoc tag
     , IsExpr ex Bool
     , HasCallStack
     )
  => Label tag -> ex -> IndigoM ()
assertCustomNoArg :: forall (tag :: Symbol) ex.
(MustHaveErrorArg tag MText, CustomErrorHasDoc tag, IsExpr ex Bool,
 HasCallStack) =>
Label tag -> ex -> IndigoM ()
assertCustomNoArg Label tag
tag ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex -> IndigoM () -> IndigoM () -> IndigoFunction ()
forall a b ex.
(IfConstraint a b, ex :~> Bool, HasCallStack) =>
ex -> IndigoM a -> IndigoM b -> IndigoM (RetVars a)
if_ ex
ex (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (forall ret (tag :: Symbol).
(ReturnableValue ret, MustHaveErrorArg tag MText,
 CustomErrorHasDoc tag, HasCallStack) =>
Label tag -> IndigoM (RetVars ret)
failCustomNoArg @() Label tag
tag)

assertSome
  :: forall x err ex.
  ( IsError err
  , Buildable err
  , KnownValue x
  , ex :~> Maybe x
  , HasCallStack
  )
  => err -> ex -> IndigoM ()
assertSome :: forall x err ex.
(IsError err, Buildable err, KnownValue x, ex :~> Maybe x,
 HasCallStack) =>
err -> ex -> IndigoM ()
assertSome err
err ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex -> (Var x -> IndigoM ()) -> IndigoM () -> IndigoFunction ()
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b, HasCallStack) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome ex
ex (\Var x
_ -> forall ret x.
(IsError x, Buildable x, ReturnableValue ret, HasCallStack) =>
x -> IndigoM (RetVars ret)
failUsing_ @() err
err) (() -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

assertNone
  :: forall x err ex.
  ( IsError err
  , Buildable err
  , KnownValue x
  , ex :~> Maybe x
  , HasCallStack
  )
  => err -> ex -> IndigoM ()
assertNone :: forall x err ex.
(IsError err, Buildable err, KnownValue x, ex :~> Maybe x,
 HasCallStack) =>
err -> ex -> IndigoM ()
assertNone err
err ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex -> (Var x -> IndigoM ()) -> IndigoM () -> IndigoFunction ()
forall x a b ex.
(KnownValue x, ex :~> Maybe x, IfConstraint a b, HasCallStack) =>
ex -> (Var x -> IndigoM a) -> IndigoM b -> IndigoM (RetVars a)
ifSome ex
ex (\Var x
_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (forall ret x.
(IsError x, Buildable x, ReturnableValue ret, HasCallStack) =>
x -> IndigoM (RetVars ret)
failUsing_ @() err
err)

assertRight
  :: forall x y err ex.
  ( IsError err
  , Buildable err
  , KnownValue x
  , KnownValue y
  , ex :~> Either y x
  , HasCallStack
  )
  => err -> ex -> IndigoM ()
assertRight :: forall x y err ex.
(IsError err, Buildable err, KnownValue x, KnownValue y,
 ex :~> Either y x, HasCallStack) =>
err -> ex -> IndigoM ()
assertRight err
err ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex
-> (Var x -> IndigoM ())
-> (Var y -> IndigoM ())
-> IndigoFunction ()
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x, IfConstraint a b,
 HasCallStack) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
ex (\Var x
_ -> forall ret x.
(IsError x, Buildable x, ReturnableValue ret, HasCallStack) =>
x -> IndigoM (RetVars ret)
failUsing_ @() err
err) (\Var y
_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

assertLeft
  :: forall x y err ex.
  ( IsError err
  , Buildable err
  , KnownValue x
  , KnownValue y
  , ex :~> Either y x
  , HasCallStack
  )
  => err -> ex -> IndigoM ()
assertLeft :: forall x y err ex.
(IsError err, Buildable err, KnownValue x, KnownValue y,
 ex :~> Either y x, HasCallStack) =>
err -> ex -> IndigoM ()
assertLeft err
err ex
ex = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ ex
-> (Var x -> IndigoM ())
-> (Var y -> IndigoM ())
-> IndigoFunction ()
forall x y a b ex.
(KnownValue x, KnownValue y, ex :~> Either y x, IfConstraint a b,
 HasCallStack) =>
ex
-> (Var x -> IndigoM a)
-> (Var y -> IndigoM b)
-> IndigoM (RetVars a)
ifRight ex
ex (\Var x
_ -> () -> IndigoM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (\Var y
_ -> forall ret x.
(IsError x, Buildable x, ReturnableValue ret, HasCallStack) =>
x -> IndigoM (RetVars ret)
failUsing_ @() err
err)

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

-- | Add a comment in a generated Michelson code
justComment :: HasCallStack => Text -> IndigoM ()
justComment :: HasCallStack => Text -> IndigoM ()
justComment = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ())
-> (Text -> IndigoM ()) -> Text -> IndigoM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => CommentType -> IndigoM ()
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 :: HasCallStack => MT.CommentType -> IndigoM ()
comment :: HasCallStack => CommentType -> IndigoM ()
comment CommentType
t = IndigoM () -> IndigoM ()
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM () -> IndigoM ()) -> IndigoM () -> IndigoM ()
forall a b. (a -> b) -> a -> b
$ (forall (inp :: [*]). SomeIndigoState inp) -> IndigoM ()
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 :: [*]).
IndigoState inp out -> SomeIndigoState inp
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 :: HasCallStack => Text -> IndigoM a -> IndigoM a
commentAroundFun :: forall a. HasCallStack => Text -> IndigoM a -> IndigoM a
commentAroundFun Text
fName IndigoM a
body = IndigoM a -> IndigoM a
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM a -> IndigoM a) -> IndigoM a -> IndigoM a
forall a b. (a -> b) -> a -> b
$
  HasCallStack => CommentType -> IndigoM ()
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
>>=
  \a
res -> a
res a -> IndigoM () -> IndigoM a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ HasCallStack => CommentType -> IndigoM ()
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 :: HasCallStack => Text -> IndigoM a -> IndigoM a
commentAroundStmt :: forall a. HasCallStack => Text -> IndigoM a -> IndigoM a
commentAroundStmt Text
sName IndigoM a
body = IndigoM a -> IndigoM a
forall a. HasCallStack => IndigoM a -> IndigoM a
calledFrom (IndigoM a -> IndigoM a) -> IndigoM a -> IndigoM a
forall a b. (a -> b) -> a -> b
$
  HasCallStack => CommentType -> IndigoM ()
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
>>=
  \a
res -> a
res a -> IndigoM () -> IndigoM a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ HasCallStack => CommentType -> IndigoM ()
CommentType -> IndigoM ()
comment (Text -> CommentType
MT.StatementEnds Text
sName)