Skip to content

Commit

Permalink
Use pow2 function from Anoma/Nock stdlib (#2629)
Browse files Browse the repository at this point in the history
This PR introduces the `pow2` function from the Anoma/Nock stdlib,
replacing the 'Builtin function' version we wrote in the Tree->Nockma
translation. This will be more efficient because it will be jetted in
Anoma.

## New Anoma/nockma stdlib locations

The Nock stdlib now has multiple layers. We now use a Nock term to
to fetch each stdlib symbol's code because this can be obtained from
Urbit's dojo,

for example:

    > =>  anoma  !=(add)
    [9 20 0 15]

where `anoma` is the symbol where the anoma stdlib is loaded.

The stdlib is the Nock code associated with
https://github.com/anoma/anoma/blob/6a4e15fe9c356225034f38445ce0eace2b43ab5e/hoon/anoma.hoon

## Tests

This commit also adds unit tests for the stdlib / appendRights
functions. The tests use the evaluator with 'interceptStdlibCalls' both
enabled and disabled.
  • Loading branch information
paulcadman authored Feb 8, 2024
1 parent 8dfe2ba commit 1edddfb
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 352 deletions.
7 changes: 4 additions & 3 deletions src/Juvix/Compiler/Nockma/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ eval inistack initerm =
ParsedAutoConsCell a -> goAutoConsCell a
ParsedOperatorCell o -> goOperatorCell o
ParsedStdlibCallCell o -> do
ignore <- asks (^. evalIgnoreStdlibCalls)
intercept' <- asks (^. evalInterceptStdlibCalls)
if
| ignore -> goOperatorCell (o ^. stdlibCallRaw)
| otherwise -> goStdlibCall (o ^. stdlibCallCell)
| intercept' -> goStdlibCall (o ^. stdlibCallCell)
| otherwise -> goOperatorCell (o ^. stdlibCallRaw)
where
loc :: Maybe Interval
loc = term ^. termLoc
Expand Down Expand Up @@ -182,6 +182,7 @@ eval inistack initerm =
StdlibMod -> binArith mod
StdlibLt -> binCmp (<)
StdlibLe -> binCmp (<=)
StdlibPow2 -> unaArith (2 ^)

goAutoConsCell :: AutoConsCell a -> Sem r (Term a)
goAutoConsCell c = do
Expand Down
7 changes: 5 additions & 2 deletions src/Juvix/Compiler/Nockma/Evaluator/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module Juvix.Compiler.Nockma.Evaluator.Options where
import Juvix.Prelude.Base

newtype EvalOptions = EvalOptions
{ _evalIgnoreStdlibCalls :: Bool
{ _evalInterceptStdlibCalls :: Bool
}

defaultEvalOptions :: EvalOptions
defaultEvalOptions = EvalOptions False
defaultEvalOptions =
EvalOptions
{ _evalInterceptStdlibCalls = True
}

makeLenses ''EvalOptions
15 changes: 2 additions & 13 deletions src/Juvix/Compiler/Nockma/Language.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Juvix.Compiler.Nockma.Language
( module Juvix.Compiler.Nockma.Language,
module Juvix.Compiler.Core.Language.Base,
module Juvix.Compiler.Nockma.StdlibFunction,
module Juvix.Compiler.Nockma.StdlibFunction.Base,
module Juvix.Compiler.Nockma.Language.Path,
)
where
Expand All @@ -10,7 +10,7 @@ import Data.HashMap.Strict qualified as HashMap
import GHC.Base (Type)
import Juvix.Compiler.Core.Language.Base (Symbol)
import Juvix.Compiler.Nockma.Language.Path
import Juvix.Compiler.Nockma.StdlibFunction
import Juvix.Compiler.Nockma.StdlibFunction.Base
import Juvix.Prelude hiding (Atom, Path)
import Juvix.Prelude.Pretty

Expand Down Expand Up @@ -332,17 +332,6 @@ infixl 1 >>#
(>>#) :: (IsNock x, IsNock y) => x -> y -> Term Natural
a >># b = TermCell (a >>#. b)

stdlibNumArgs :: StdlibFunction -> Natural
stdlibNumArgs = \case
StdlibDec -> 1
StdlibAdd -> 2
StdlibSub -> 2
StdlibMul -> 2
StdlibMod -> 2
StdlibDiv -> 2
StdlibLe -> 2
StdlibLt -> 2

{-# COMPLETE Cell #-}

pattern Cell :: Term a -> Term a -> Cell a
Expand Down
Loading

0 comments on commit 1edddfb

Please sign in to comment.