[#169] Create an infix version of setVar and rename newVar

Problem: To resemble more other imperative languages,
it would be nice to rename `newVar` to a shorter word and add infix operator for `setVar`

Solution: Rename `newVar` to just `new` and add `(=:)` as infix operator for `setVar`
parent f4a79b75
Pipeline #147785984 passed with stages
in 6 minutes and 18 seconds
......@@ -6,7 +6,7 @@
module Indigo.Frontend.Language
( -- * Assignment and modifications
newVar
new
, setVar
, setField
, (+=)
......@@ -17,6 +17,7 @@ module Indigo.Frontend.Language
, (&&=)
, (||=)
, (^=)
, (=:)
-- * Storage Fields
, getStorageField
......@@ -134,13 +135,17 @@ varModification act v ex = oneIndigoM $ VarModification act v ex
----------------------------------------------------------------------------
-- | Create a new variable with the result of the given expression as its initial value.
newVar :: (IsExpr ex x, TypeValue x) => ex -> IndigoM (Var x)
newVar e = oneIndigoM $ NewVar e
new :: (IsExpr ex x, TypeValue x) => ex -> IndigoM (Var x)
new e = oneIndigoM $ NewVar e
-- | Set the given variable to the result of the given expression.
setVar :: (IsExpr ex x, IsObject x) => Var x -> ex -> IndigoM ()
setVar v e = oneIndigoM $ SetVar v e
infixr 0 =:
(=:) :: (IsExpr ex x, IsObject x) => Var x -> ex -> IndigoM ()
v =: e = setVar v e
setField
:: ( ex :~> ftype
, IsObject dt
......@@ -228,7 +233,7 @@ updateStorageField
-> IndigoM ()
updateStorageField field upd = scope $ do
let storage = storageVar @store
fieldVar <- newVar (storage %! field)
fieldVar <- new (storage %! field)
expr <- upd fieldVar
setField (storageVar @store) field expr
......@@ -241,7 +246,7 @@ getStorageField
, HasField store fname ftype
)
=> Label fname -> IndigoM (Var ftype)
getStorageField field = newVar (storageVar @store %! field)
getStorageField field = new (storageVar @store %! field)
----------------------------------------------------------------------------
-- Conditional
......
......@@ -134,7 +134,7 @@ subGt0 minuend subtrahend onNegative = do
when (diff <. zero) onNegative
resVar <- newVar Nothing
resVar <- new Nothing
if diff ==. zero then setVar resVar $ C Nothing else setVar resVar $ isNat diff
......
......@@ -91,7 +91,7 @@ setDecomposedVariable = compileIndigoContract $ \param -> do
~. (#y, snd (snd param))
~. (#color, fst param)
~. (#weight, (0 :: Integer))
setVar storage newStorageVal
storage =: newStorageVal
------------------------------
......@@ -114,7 +114,7 @@ setMaterializedVariable = compileIndigoContract $ \param -> do
, constExpr [mt|"hello, Ivan!"|]
)
)
setVar storage newStorageVal
storage =: newStorageVal
------------------------------
......
......@@ -90,16 +90,16 @@ assertCheck param st
contractVarLorentz :: ContractCode Integer Integer
contractVarLorentz = compileIndigoContract $ \_param ->
when (5 int <. 10 int) $ do
_a <- newVar $ 10 int
_a <- new $ 10 int
return ()
ifTest :: L.Contract Integer Integer
ifTest = noOptimizationContract $ compileIndigoContract $ \param -> do
a <- newVar $ 7 int +. param
a <- new $ 7 int +. param
when (param <. a) $ do
_c <- newVar (storageVar @Integer)
_c <- new (storageVar @Integer)
return ()
_c <- newVar $ param <. storageVar @Integer
_c <- new $ param <. storageVar @Integer
return ()
contractIfLorentz :: ContractCode Integer Integer
......@@ -107,29 +107,29 @@ contractIfLorentz = L.cCode ifTest
contractIfValueLorentz :: ContractCode Integer Integer
contractIfValueLorentz = compileIndigoContract $ \param -> do
a <- newVar $ 7 int +. param
a <- new $ 7 int +. param
when (param <. a) $ do
_c <- newVar (storageVar @Integer)
_c <- new (storageVar @Integer)
return ()
_c <- newVar $ param <. storageVar @Integer
_c <- new $ param <. storageVar @Integer
return ()
contractWhileLorentz :: ContractCode Integer Integer
contractWhileLorentz = compileIndigoContract $ \param -> do
i <- newVar $ 0 int
s <- newVar $ 0 int
i <- new $ 0 int
s <- new $ 0 int
while (i <. storageVar @Integer) $ do
when (i %. param ==. 0 nat) $
s += i
i += 1 int
setVar storageVar s
storageVar =: s
contractForEachLorentz :: ContractCode [Integer] Integer
contractForEachLorentz = compileIndigoContract $ \param -> do
s <- newVar $ 0 int
s <- new $ 0 int
forEach param $ \it -> do
setVar s (s +. it)
setVar storageVar s
s =: s +. it
storageVar =: s
contractCaseLorentz :: ContractCode DummyOp Integer
contractCaseLorentz = compileIndigoContract $ \param -> scope $ do
......@@ -169,44 +169,44 @@ doSub :: Var Integer -> Var (Integer, Integer) -> IndigoM (Var Bool)
doSub storage p = do
-- Create a variable to demostrate that branches of case
-- are cleaned automatically
testVar <- newVar True
setVar storage (Fst p -. Snd p)
testVar <- new True
storage =: Fst p -. Snd p
return testVar
doAdd :: Var Integer -> Var (Integer, Integer) -> IndigoM Bool
doAdd storage p = do
setVar storage (Fst p +. Snd p)
storage =: Fst p +. Snd p
return False
contractDocLorentz :: ContractCode Integer Integer
contractDocLorentz = compileIndigoContract $ \param -> do
doc (DDescription "x")
contractName "aaa" (doc $ DDescription "a")
i <- newVar $ 10 int
i <- new $ 10 int
docGroup (SomeDocItem . DName "bbb") (doc $ DDescription "b")
setVar storageVar (param +. i)
storageVar =: param +. i
contractOpsLorentz :: ContractCode (Maybe KeyHash) Address
contractOpsLorentz = compileIndigoContract $ \param -> do
setDelegate param
m <- newVar $ 0 mutez
is <- newVar $ 0 int
m <- new $ 0 mutez
is <- new $ 0 int
addr <- createLorentzContract ifTest param m is
setVar storageVar addr
storageVar =: addr
contractAssertLorentz :: ContractCode Integer Integer
contractAssertLorentz = compileIndigoContract $ \param -> do
s <- newVar (param +. storageVar @Integer)
z <- newVar $ 0 int
s <- new (param +. storageVar @Integer)
z <- new $ 0 int
assert negativeResM (s >. z)
setVar storageVar s
storageVar =: s
contractCommentLorentz :: ContractCode Integer Integer
contractCommentLorentz = compileIndigoContract $ \param -> do
s <- commentAroundStmt "param plus storage" $ newVar (param +. storageVar @Integer)
_z <- newVar $ 0 int
s <- commentAroundStmt "param plus storage" $ new (param +. storageVar @Integer)
_z <- new $ 0 int
justComment "just comment"
setVar storageVar s
storageVar =: s
--------------------------------------------------------------------------------
-- Expected Contracts
......@@ -226,7 +226,7 @@ expectedDocContract =
L.doc (DDescription "x") #
-- contractName "aaa" (doc $ DDescription "a")
L.contractName "aaa" (L.doc $ DDescription "a") #
-- i <- newVar $ 10 int
-- i <- new $ 10 int
L.push (10 int) #
-- docGroup (SomeDocItem . DName "bbb") (doc $ DDescription "b")
L.docGroup (SomeDocItem . DName "bbb") (L.doc $ DDescription "b") #
......
......@@ -195,96 +195,96 @@ cryptoCheck param _st = Right (C.sha512 param, C.blake2b param)
exprNullary
:: forall s. IsObject s => Expr s -> '[(), s] :-> '[(), s]
exprNullary expr = compileIndigo @2 $ \st _param -> setVar st expr
exprNullary expr = compileIndigo @2 $ \st _param -> st =: expr
exprUnary
:: forall s. IsObject s => (Var s -> Expr s) -> '[s, s] :-> '[s, s]
exprUnary expr = compileIndigo @2 $ \st param -> setVar st (expr param)
exprUnary expr = compileIndigo @2 $ \st param -> st =: expr param
exprBinary
:: forall s. IsObject s => (Var s -> Var s -> Expr s) -> '[s, s] :-> '[s, s]
exprBinary expr = compileIndigo @2 $ \st param -> setVar st (expr param st)
exprBinary expr = compileIndigo @2 $ \st param -> st =: expr param st
exprAbs :: '[Integer, Natural] :-> '[Integer, Natural]
exprAbs = compileIndigo @2 $ \st param -> setVar st (abs param)
exprAbs = compileIndigo @2 $ \st param -> st =: abs param
exprDivEq :: '[Integer, Integer] :-> '[Integer, Integer]
exprDivEq = compileIndigo @2 $ \st param -> setVar st (st /. param)
exprDivEq = compileIndigo @2 $ \st param -> st =: st /. param
exprModNeq :: '[Integer, Integer] :-> '[Integer, Integer]
exprModNeq = compileIndigo @2 $ \st param -> do
if_ ((st %. param) /=. (0 :: Natural))
(setVar st (0 :: Integer))
(setVar st (1 :: Integer))
(st =: (0 :: Integer))
(st =: (1 :: Integer))
exprLe3 :: '[Integer, Bool] :-> '[Integer, Bool]
exprLe3 = compileIndigo @2 $ \st param ->
setVar st (param <=. (3 :: Integer))
st =: param <=. (3 :: Integer)
exprLt3OrGt10 :: '[Integer, Bool] :-> '[Integer, Bool]
exprLt3OrGt10 = compileIndigo @2 $ \st param ->
setVar st ((param <. (3 :: Integer)) ||. (param >. (10 :: Integer)))
st =: (param <. (3 :: Integer)) ||. (param >. (10 :: Integer))
exprGe3AndNotGe10 :: '[Integer, Bool] :-> '[Integer, Bool]
exprGe3AndNotGe10 = compileIndigo @2 $ \st param ->
setVar st ((param >=. (3 :: Integer)) &&. (Not (param >=. (10 :: Integer))))
st =: (param >=. (3 :: Integer)) &&. (Not (param >=. (10 :: Integer)))
exprGe4OrNeq5AndEq6 :: '[Integer, Bool] :-> '[Integer, Bool]
exprGe4OrNeq5AndEq6 = compileIndigo @2 $ \st param ->
setVar st ((param >=. (4 :: Integer)) ||. (param /=. (5 :: Integer)) &&. (param ==. (6 :: Integer)))
st =: (param >=. (4 :: Integer)) ||. (param /=. (5 :: Integer)) &&. (param ==. (6 :: Integer))
exprNot :: '[Bool, Bool] :-> '[Bool, Bool]
exprNot = compileIndigo @2 $ \st param ->
setVar st (not param)
st =: not param
exprIsNat :: '[Integer, Maybe Natural] :-> '[Integer, Maybe Natural]
exprIsNat = compileIndigo @2 $ \st param ->
setVar st (isNat param)
st =: isNat param
exprSome :: TypeValue a => '[a, Maybe a] :-> '[a, Maybe a]
exprSome = compileIndigo @2 $ \st param ->
setVar st (some param)
st =: some param
exprNone :: (Typeable a, KnownValue a) => '[a, Maybe a] :-> '[a, Maybe a]
exprNone = compileIndigo @2 $ \st _ ->
setVar st none
st =: none
exprFst :: '[(Integer, Integer), Integer] :-> '[(Integer, Integer), Integer]
exprFst = compileIndigo @2 $ \st param -> setVar st (fst param)
exprFst = compileIndigo @2 $ \st param -> st =: fst param
exprSnd :: '[(Integer, Integer), Integer] :-> '[(Integer, Integer), Integer]
exprSnd = compileIndigo @2 $ \st param -> setVar st (snd param)
exprSnd = compileIndigo @2 $ \st param -> st =: snd param
exprPack :: '[Signature, ByteString] :-> '[Signature, ByteString]
exprPack = compileIndigo @2 $ \st param -> setVar st (pack param)
exprPack = compileIndigo @2 $ \st param -> st =: pack param
exprUnpack :: '[ByteString, Maybe Signature] :-> '[ByteString, Maybe Signature]
exprUnpack= compileIndigo @2 $ \st param -> setVar st (unpack param)
exprUnpack= compileIndigo @2 $ \st param -> st =: unpack param
exprSet :: '[(Set Integer), Integer] :-> '[(Set Integer), Integer]
exprSet = compileIndigo @2 $ \st param -> do
z <- newVar (0 :: Integer)
z <- new (0 :: Integer)
if_ (param #? z &&. mem z param) -- Same, but also checks `mem` expression
(setVar param (param #- z))
(setVar param (param #~ (1 :: Integer)))
(param =: param #- z)
(param =: param #~ (1 :: Integer))
if_ (size param ==. (1 :: Natural))
(setVar st z)
(setVar st (1 :: Integer))
(st =: z)
(st =: (1 :: Integer))
exprSize :: '[[Integer], Natural] :-> '[[Integer], Natural]
exprSize = compileIndigo @2 $ \st param -> do
setVar st (size param)
st =: size param
exprEmptySet :: '[(), Set Integer] :-> '[(), Set Integer]
exprEmptySet = compileIndigo @2 $ \st _param -> setVar st emptySet
exprEmptySet = compileIndigo @2 $ \st _param -> st =: emptySet
exprCons :: '[Integer, List Integer] :-> '[Integer, List Integer]
exprCons = compileIndigo @2 $ \st param -> do
setVar st (param .: st)
st =: param .: st
exprConcat :: '[MText, MText] :-> '[MText, MText]
exprConcat = compileIndigo @2 $ \st param -> do
setVar st (param <>. st)
st =: param <>. st
exprSlice :: '[Natural, Maybe MText] :-> '[Natural, Maybe MText]
exprSlice = compileIndigo @2 $ \(st' :: Var (Maybe MText)) param -> do
......@@ -292,38 +292,38 @@ exprSlice = compileIndigo @2 $ \(st' :: Var (Maybe MText)) param -> do
(\st -> do
ifSome (slice (0 :: Natural, param) st)
(\r -> do
setVar st r
setVar st' (some st))
st =: r
st' =: some st)
(return ()))
(return ())
exprBigMapLookup :: '[BigMap Integer Integer, Maybe Integer] :-> '[BigMap Integer Integer, Maybe Integer]
exprBigMapLookup = compileIndigo @2 $ \st param -> do
setVar st $ (param &? (2 :: Integer))
st =: param &? (2 :: Integer)
exprBigMapDelete :: '[Integer, BigMap Integer Integer] :-> '[Integer, BigMap Integer Integer]
exprBigMapDelete = compileIndigo @2 $ \st param -> do
setVar st $ st &- param
st =: st &- param
exprBigMapInsert :: '[Integer, BigMap Integer Integer] :-> '[Integer, BigMap Integer Integer]
exprBigMapInsert = compileIndigo @2 $ \st param -> do
setVar st $ st &~ (param, param)
st =: st &~ (param, param)
exprUStore
:: '[Integer, MyUstore]
:-> '[Integer, MyUstore]
exprUStore = compileIndigo @2 $ \st param -> do
st1 <- newVar $ st %~ (#ints, param, ())
st2 <- newVar $ st1 %~~ (#ints, notNewKeyM, 0 :: Integer, ())
st1 <- new $ st %~ (#ints, param, ())
st2 <- new $ st1 %~~ (#ints, notNewKeyM, 0 :: Integer, ())
ifSome (st2 %@ (#ints, -1 :: Integer))
(\_v -> setVar st st)
(setVar st st2)
(\_v -> st =: st)
(st =: st2)
exprCheckSignature :: '[Bool, Bool] :-> '[Bool, Bool]
exprCheckSignature = compileIndigo @2 (phi sampleSignature)
where
phi SignatureData{..} st _param = do
setVar st $ checkSignature
st =: checkSignature
(constExpr $ partialParse C.parsePublicKey sdPublicKey)
(constExpr $ partialParse C.parseSignature sdSignature)
(constExpr sdBytes)
......@@ -332,13 +332,13 @@ exprCrypto
:: '[ByteString, ByteString]
:-> '[ByteString, ByteString]
exprCrypto = compileIndigo @2 $ \st param -> do
_sha256var <- newVar $ sha256 param
setVar st (blake2b param)
setVar param (sha512 param)
_sha256var <- new $ sha256 param
st =: blake2b param
param =: sha512 param
exprHashKey :: '[PublicKey, KeyHash] :-> '[PublicKey, KeyHash]
exprHashKey = compileIndigo @2 $ \st param -> do
setVar st (hashKey param)
st =: hashKey param
----------------------------------------------------------------------------
-- Contract specified tests
......@@ -355,7 +355,7 @@ exprHashKey = compileIndigo @2 $ \st param -> do
-- )
-- => ContractCode p st
-- contractCodeLorentz = compileIndigoContract $ \param st -> do
-- setVar st $ contract (constExpr genesisAddress)
-- st =: contract (constExpr genesisAddress)
-- expectedContractCode
-- :: forall p st.
......
......@@ -48,19 +48,19 @@ sumLambdaCheck param _st = Right $ sum param
-- In this case code has to be just inlined.
sumLambdaCalledOnce :: ContractCode [Integer] Integer
sumLambdaCalledOnce = compileIndigoContract $ \lst -> do
s <- newVar @Integer 0
s <- new @Integer 0
forEach lst $
setVar s <=< sumLambda . pair s
setVar storageVar s
storageVar =: s
-- | Pure lambda called twice.
-- In this case lambda has to be pushed on the stack and then called using @[email protected]
sumLambdaCalledTwice :: ContractCode [Integer] Integer
sumLambdaCalledTwice = compileIndigoContract $ \lst -> do
s <- newVar @Integer 0 >>= sumLambda . pair (0 :: Integer)
s <- new @Integer 0 >>= sumLambda . pair (0 :: Integer)
forEach lst $
setVar s <=< sumLambda . pair s
setVar storageVar s
storageVar =: s
lambdasSideEffectsCheck :: Maybe KeyHash -> Integer -> Either MichelsonFailed ([Operation], Integer)
......@@ -73,11 +73,11 @@ lambdasSideEffectsCheck param st = Right ([crConOp 1, crConOp 0], st + 2)
dummyContract
:: L.Contract Integer Integer
dummyContract = noOptimizationContract $ compileIndigoContract $ \param -> do
a <- newVar $ (7 :: Integer) +. param
a <- new $ (7 :: Integer) +. param
when (param <. a) $ do
_c <- newVar (storageVar @Integer)
_c <- new (storageVar @Integer)
return ()
_c <- newVar $ param <. storageVar @Integer
_c <- new $ param <. storageVar @Integer
return ()
lambdaCreateContract ::
......@@ -85,7 +85,7 @@ lambdaCreateContract ::
, HasSideEffects, HasStorage Integer
) => ex -> IndigoM ()
lambdaCreateContract = defNamedEffLambda1 @Integer "create storage" $ \paramSt -> do
m <- newVar (toMutez 0)
m <- new (toMutez 0)
_addr <- createLorentzContract dummyContract (fst paramSt) m (snd paramSt)
storageVar @Integer += (1 :: Integer)
......@@ -147,7 +147,7 @@ lambdaInLambda2 = compileIndigoContract $ \matrix -> do
-- * a pure lambda uses @[email protected] or @[email protected]
lambdaOuterVarClosure :: ContractCode Integer Integer
lambdaOuterVarClosure = compileIndigoContract $ \param -> do
plusTwo <- newVar (param +. (2 :: Integer))
plusTwo <- new (param +. (2 :: Integer))
let lam :: pr :~> Integer => pr -> IndigoM (Var Integer)
lam = defNamedPureLambda1 "lambda" $ \innerPar -> pure (plusTwo +. innerPar)
_v1 <- lam (3 :: Integer)
......
......@@ -187,13 +187,13 @@ prefixing the name with a `#`.
For example, this is some Indigo code interacting with `TransferParams`s:
```haskell
-- just a variable of type 'Address'
a <- newVar sender
a <- new sender
-- creation of a variable of type TransferParams
tp <- newVar $ constructT (a !~ #source, a !~ #receiver, 10 !~ #amount)
tp <- new $ constructT (a !~ #source, a !~ #receiver, 10 !~ #amount)
-- extracting the "amount" from a TransferParams
amount <- newVar $ tp !. #amount
amount <- new $ tp !. #amount
-- updating the "amount" in a TransferParams
setVar tp $ tp ~. (#amount, 20)
tp =: tp ~. (#amount, 20)
```
Named types are often useful in making clear what is the use of a given type,
......
......@@ -62,10 +62,10 @@ For example:
```haskell
1 int += 2 int -- is invalid, this will not compile
a <- newVar (1 int)
a <- new (1 int)
a += 2 int -- "a" will have value of "3" after this expression
b <- newVar (a += 1 int) -- is also invalid, because "(a += b)" is not an expression
b <- new (a += 1 int) -- is also invalid, because "(a += b)" is not an expression
```
*Type variables* are used in the types of these tables as well, you can recognize
......@@ -435,7 +435,7 @@ For example, these are valid statements:
```haskell
if 1 int <= 2 int
then do
setVar storage (1 int) -- this will be executed
storage =: 1 int -- this will be executed
return ()
else do
return () -- this will not
......@@ -444,7 +444,7 @@ resVal <- if 1 int ==. 2 int
then do
return False
else do
a <- newVar (1 int ==. 1 int)
a <- new (1 int ==. 1 int)
return a
-- we can now use the `resVal` `Bool` variable
```
......@@ -456,7 +456,7 @@ be executed only if the condition is `True`.
The first statement above could so be rewritten as:
```haskell
when (1 int <= 2 int) $ do
setVar storage (1 int)
storage =: 1 int
```
In other cases we might want to do exactly the opposite and execute some code
......@@ -465,7 +465,7 @@ Indigo has a convenience statement for this too, called `unless`.
The previous example for instance could also be written as:
```haskell
unless (1 int > 2 int) $ do
setVar storage (1 int)
storage =: 1 int
```
When we have to deal with `Maybe a`s we often want to know if they are `some a`
......@@ -478,7 +478,7 @@ For example, let's suppose to have a `val` variable containing `some 1` of type
`Maybe Integer`, these would be a valid statement:
```haskell
ifSome val
(\n -> setVar storage n)
(\n -> storage =: n)
(return ())
```
......@@ -495,12 +495,12 @@ We also two have statements similar to `when` for convenience:
`while` is the statement that will execute a piece of code as long as a `Bool`
condition holds. For example, this loop would execute twice:
```haskell
i <- newVar (0 int)
i <- new (0 int)
while (i int < 2 int) $ do
-- more code can be here
i += 1 int
-- the code following will get executed after the loop
setVar i (0 int)
i =: 0 int
```
`forEach` is instead a way to execute a piece of code over each of the elements
......@@ -508,7 +508,7 @@ of a container (a list, set or map).
For example, given a `numList` variable of type `[Integer]` this will compute
the sum of all its elements:
```haskell
sum <- newVar (0 int)
sum <- new (0 int)
forEach numList $ \number -> do
sum += number
```
......