Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generation and evaluation of Nock isCell op #2602

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Juvix/Compiler/Nockma/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ eval inistack initerm =
OpAddress -> goOpAddress
OpQuote -> return goOpQuote
OpApply -> goOpApply
OpIsCell -> return goOpIsCell
OpIsCell -> goOpIsCell
OpInc -> goOpInc
OpEq -> goOpEq
OpIf -> goOpIf
Expand Down Expand Up @@ -231,10 +231,12 @@ eval inistack initerm =
goOpQuote :: Term a
goOpQuote = c ^. operatorCellTerm

goOpIsCell :: Term a
goOpIsCell = TermAtom $ case c ^. operatorCellTerm of
TermCell {} -> nockTrue
TermAtom {} -> nockFalse
goOpIsCell :: Sem r (Term a)
goOpIsCell = do
cr <- evalArg crumbEvalFirst stack (c ^. operatorCellTerm)
return . TermAtom $ case cr of
TermCell {} -> nockTrue
TermAtom {} -> nockFalse

goOpTrace :: Sem r (Term a)
goOpTrace = do
Expand Down
6 changes: 1 addition & 5 deletions src/Juvix/Compiler/Nockma/Translation/FromAsm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,6 @@ replaceSubterm obj relPath newVal = OpReplace # (relPath # newVal) # obj
evaluated :: Term Natural -> Term Natural
evaluated t = OpApply # (OpAddress # emptyPath) # t

-- | The IsCell op but the argument is evaluated first.
isCell' :: Term Natural -> Term Natural
isCell' t = evaluated $ (OpQuote # OpIsCell) # t

-- | The same as replaceSubterm but the path is a cell that is evaluated.
-- i.e. replaceSubterm a p b = replaceSubterm' a (quote p) b
replaceSubterm' :: Term Natural -> Term Natural -> Term Natural -> Term Natural
Expand Down Expand Up @@ -989,7 +985,7 @@ caseCmd defaultBranch = \case

goRepList :: NonEmpty (NockmaMemRepListConstr, Sem r ()) -> Sem r ()
goRepList ((c, b) :| bs) = do
push (isCell' (OpAddress # topOfStack ValueStack))
push (OpIsCell # (OpAddress # topOfStack ValueStack))
let otherBranch = fromJust (firstJust f bs <|> defaultBranch)
case c of
NockmaMemRepListConstrCons -> branch b otherBranch
Expand Down
4 changes: 2 additions & 2 deletions test/Nockma/Eval/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ tests =
Test "address nested" [nock| [0 1 2 3 4 5] |] [nock| [@ RRRRR] |] (eqNock [nock| 5 |]),
Test "quote" [nock| [0 1] |] [nock| [quote [1 0]] |] (eqNock [nock| [1 0] |]),
Test "apply" [nock| [0 1] |] [nock| [apply [@ S] [quote [@ R]]] |] (eqNock [nock| 1 |]),
Test "isCell atom" [nock| [0 1] |] [nock| [isCell 11] |] (eqNock [nock| false |]),
Test "isCell cell" [nock| [0 1] |] [nock| [isCell [1 0]] |] (eqNock [nock| true |]),
Test "isCell atom" [nock| [0 1] |] [nock| [isCell [@ L]] |] (eqNock [nock| false |]),
Test "isCell cell" [nock| [0 1] |] [nock| [isCell [@ S]] |] (eqNock [nock| true |]),
Test "suc" [nock| [0 1] |] [nock| [suc [quote 1]] |] (eqNock [nock| 2 |]),
Test "eq" [nock| [0 1] |] [nock| [= [1 0] [1 0]] |] (eqNock [nock| true |]),
Test "eq" [nock| [0 1] |] [nock| [= [1 0] [0 1]] |] (eqNock [nock| false |]),
Expand Down
Loading