Skip to content

Commit

Permalink
Remove mainFunctionWrapper in nock backend
Browse files Browse the repository at this point in the history
THe mainFunctionWrapper was used to capture the argument of main to use
as the `id` field of a anomaGet (scry) call.

The `id` argument is no longer passed into the main function in all
cases. anomaGet must support being called with the `id` argument
explicitly, it is now part of the scry key.
  • Loading branch information
paulcadman committed Jan 27, 2025
1 parent 8ac9112 commit 623edf2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 63 deletions.
68 changes: 11 additions & 57 deletions src/Juvix/Compiler/Nockma/Translation/FromTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ constructorPath = pathFromEnum
closurePath :: AnomaCallablePathId -> Path
closurePath = pathFromEnum

anomaGetPath :: Path
anomaGetPath = [L]

data IndexTupleArgs = IndexTupleArgs
{ _indexTupleArgsLength :: Natural,
_indexTupleArgsIndex :: Natural
Expand Down Expand Up @@ -412,27 +409,6 @@ addressTempRef tr = do
p <- tempRefPath tr
return $ opAddress "tempRef" p

-- `funsLib` is being quoted in this function
mainFunctionWrapper :: (Member (Reader CompilerCtx) r) => Term Natural -> Term Natural -> Sem r (Term Natural)
mainFunctionWrapper funslib funCode = do
-- 1. The Anoma system expects to receive a function of type `ScryId -> Transaction`
--
-- 2. The ScryId is only used to construct the argument to the Scry operation
-- (i.e the anomaGet builtin in the Juvix frontend),
--
-- 3. When the Juvix developer writes a function to submit to Anoma they use
-- type `() -> Transaction`, this wrapper is used to capture the ScryId
-- argument into the subject which is then used to construct OpScry arguments
-- when anomaGet is compiled.
--
-- 4. If the Anoma system expectation changes then this code must be changed.
anomaGet <- getFieldInSubject ArgsTuple
captureAnomaGetOrder <- replaceSubject $ \case
FunCode -> Just (OpQuote # funCode)
FunctionsLibrary -> Just (OpReplace # (anomaGetPath # anomaGet) # OpQuote # funslib)
_ -> Nothing
return $ opCall "mainFunctionWrapper" (closurePath FunCode) captureAnomaGetOrder

compile :: forall r. (Members '[Reader FunctionCtx, Reader CompilerCtx] r) => Tree.Node -> Sem r (Term Natural)
compile = \case
Tree.Binop b -> goBinop b
Expand Down Expand Up @@ -561,7 +537,7 @@ compile = \case
goAnomaOp Tree.NodeAnoma {..} = do
args <- mapM compile _nodeAnomaArgs
case _nodeAnomaOpcode of
Tree.OpAnomaGet -> goAnomaGet args
Tree.OpAnomaGet -> return (goAnomaGet args)
Tree.OpAnomaEncode -> goAnomaEncode args
Tree.OpAnomaDecode -> goAnomaDecode args
Tree.OpAnomaVerifyDetached -> goAnomaVerifyDetached args
Expand Down Expand Up @@ -629,12 +605,8 @@ compile = \case
Tree.OpIntToUInt8 -> compile arg >>= intToUInt8
Tree.OpUInt8ToInt -> compile arg

goAnomaGet :: [Term Natural] -> Sem r (Term Natural)
goAnomaGet key = do
funlibPath <- stackPath FunctionsLibrary
let anomaGet = opAddress "anomaGet" (funlibPath <> anomaGetPath)
let arg = remakeList [anomaGet, foldTermsOrQuotedNil key]
return (OpScry # (OpQuote # nockNilTagged "OpScry-typehint") # arg)
goAnomaGet :: [Term Natural] -> Term Natural
goAnomaGet key = OpScry # (OpQuote # nockNilTagged "OpScry-typehint") # (foldTermsOrQuotedNil key)

goAnomaEncode :: [Term Natural] -> Sem r (Term Natural)
goAnomaEncode = callStdlib StdlibEncode
Expand Down Expand Up @@ -1009,13 +981,6 @@ runCompilerWith _opts constrs moduleFuns mainFun =
libFuns :: [CompilerFunction]
libFuns = moduleFuns ++ (builtinFunction <$> allElements)

-- The number of "extra" functions at the front of the functions library
-- list which are not defined by the user. Currently, the only such function
-- is anomaGet (the `main` function and the functions from `libFuns` are
-- defined by the user).
libFunShift :: Natural
libFunShift = 1

allFuns :: NonEmpty CompilerFunction
allFuns = mainFun :| libFuns

Expand All @@ -1035,8 +1000,7 @@ runCompilerWith _opts constrs moduleFuns mainFun =
where
compiledFuns :: [Term Natural]
compiledFuns =
(nockNilTagged "anomaGetPlaceholder")
: (nockNilTagged "mainFunctionPlaceholder")
(nockNilTagged "mainFunctionPlaceholder")
: ( makeLibraryFunction
<$> [(f ^. compilerFunctionName, f ^. compilerFunctionArity, runCompilerFunction compilerCtx f) | f <- libFuns]
)
Expand All @@ -1058,14 +1022,12 @@ runCompilerWith _opts constrs moduleFuns mainFun =

-- The result is not quoted and cannot be evaluated directly.
makeMainFunction :: Term Natural -> Term Natural
makeMainFunction c = makeRawClosure $ \p ->
let nockNilHere = nockNilTagged ("makeMainFunction-" <> show p)
in case p of
FunCode -> run . runReader compilerCtx $ mainFunctionWrapper funcsLib c
ArgsTuple -> argsTuplePlaceholder "mainFunction" (mainFun ^. compilerFunctionArity)
ClosureRemainingArgsNum -> nockNilHere
FunctionsLibrary -> functionsLibraryPlaceHolder
AnomaLibrary -> anomaLib
makeMainFunction funCode = makeRawClosure $ \case
FunCode -> funCode
ArgsTuple -> argsTuplePlaceholder "mainFunction" (mainFun ^. compilerFunctionArity)
ClosureRemainingArgsNum -> nockNilTagged ("makeMainFunction-ClosureRemainingArgsNum")
FunctionsLibrary -> funcsLib
AnomaLibrary -> anomaLib

functionInfos :: HashMap FunctionId FunctionInfo
functionInfos = hashMap (run (runStreamOfNaturals (toList <$> userFunctions)))
Expand All @@ -1076,7 +1038,7 @@ runCompilerWith _opts constrs moduleFuns mainFun =
return
( _compilerFunctionId,
FunctionInfo
{ _functionInfoPath = pathFromEnum FunctionsLibrary ++ indexStack (i + libFunShift),
{ _functionInfoPath = pathFromEnum FunctionsLibrary ++ indexStack i,
_functionInfoArity = _compilerFunctionArity,
_functionInfoName = _compilerFunctionName
}
Expand Down Expand Up @@ -1154,9 +1116,6 @@ curryClosure f args newArity = do
FunctionsLibrary -> OpQuote # functionsLibraryPlaceHolder
AnomaLibrary -> OpQuote # anomaLibPlaceholder

replaceSubject :: (Member (Reader CompilerCtx) r) => (AnomaCallablePathId -> Maybe (Term Natural)) -> Sem r (Term Natural)
replaceSubject = replaceSubject' "replaceSubject"

replaceSubject' :: (Member (Reader CompilerCtx) r) => Text -> (AnomaCallablePathId -> Maybe (Term Natural)) -> Sem r (Term Natural)
replaceSubject' tag f = do
lst <- forM allElements $ \s -> do
Expand Down Expand Up @@ -1324,11 +1283,6 @@ getConstructorField = getField
getField :: (Enum field) => field -> Term Natural -> Term Natural
getField field t = t >># opAddress "getField" (pathFromEnum field)

getFieldInSubject :: (Member (Reader CompilerCtx) r) => (Enum field) => field -> Sem r (Term Natural)
getFieldInSubject field = do
path <- stackPath field
return $ opAddress "getFieldInSubject" path

getConstructorMemRep :: (Members '[Reader CompilerCtx] r) => Tree.Tag -> Sem r NockmaMemRep
getConstructorMemRep tag = (^. constructorInfoMemRep) <$> getConstructorInfo tag

Expand Down
8 changes: 2 additions & 6 deletions test/Anoma/Compilation/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -865,16 +865,12 @@ allTests =
v1 :: Term Natural = [nock| 222 |]
k2 :: Term Natural = [nock| [1 2 3 nil] |]
v2 :: Term Natural = [nock| [4 5 6 nil] |]
-- The keys of the storage are of the form [id key nil].
-- The id is captured from the arguments tuple of the function.
sk1 :: Term Natural = [nock| [[333 1 2 3 nil] 333 nil] |]
sk2 :: Term Natural = [nock| [[333 1 2 3 nil] [1 2 3 nil] nil] |]
in mkAnomaTest'
AnomaTestModeDebugOnly
( Storage
( hashMap
[ (StorageKey sk1, v1),
(StorageKey sk2, v2)
[ (StorageKey k1, v1),
(StorageKey k2, v2)
]
)
)
Expand Down

0 comments on commit 623edf2

Please sign in to comment.