Skip to content

Commit

Permalink
Fix caseCmd for RepConstr (#2666)
Browse files Browse the repository at this point in the history
See #2670 for an example which triggers the bug.

The nockma case compilation did not correctly compile case expressions
for standard (i.e not list or tuple) constructors.

Existing compilation tests (e.g Tree, Lambda Calculus) did not fail due
to the relevant `fromJust` never being evaluated due to lazy evaluation.
The tests now write out the resulting nockma file to force full
evaluation.

* Closes #2670

---------

Co-authored-by: Paul Cadman <[email protected]>
  • Loading branch information
janmasrovira and paulcadman authored Feb 26, 2024
1 parent be24abb commit 1413f8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Juvix/Compiler/Nockma/Translation/FromTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,13 @@ caseCmd arg defaultBranch = \case
OpEq
# constructorTagToTerm tag
# (getConstructorField ConstructorTag arg)
elseBr <- caseCmd arg defaultBranch bs
return (branch cond b elseBr)
case nonEmpty bs of
Nothing -> case defaultBranch of
Nothing -> return b
Just defbr -> return (branch cond b defbr)
Just ((t', b') :| bs') -> do
elseBr <- goRepConstr t' b' bs'
return (branch cond b elseBr)

asNockmaMemRepListConstr :: Tree.Tag -> Sem r NockmaMemRepListConstr
asNockmaMemRepListConstr tag = case tag of
Expand Down
7 changes: 6 additions & 1 deletion test/Anoma/Compilation/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Base
import Juvix.Compiler.Backend (Target (TargetAnoma))
import Juvix.Compiler.Nockma.Evaluator
import Juvix.Compiler.Nockma.Language
import Juvix.Compiler.Nockma.Pretty
import Juvix.Compiler.Nockma.Translation.FromSource.QQ
import Juvix.Compiler.Nockma.Translation.FromTree
import Juvix.Prelude qualified as Prelude
Expand All @@ -19,7 +20,11 @@ mkAnomaCallTest' enableDebug _testName relRoot mainFile args _testCheck =
where
mkTestIO :: IO Test
mkTestIO = do
_testProgramSubject <- withRootCopy compileMain
_testProgramSubject <- withRootCopy $ \tmpDir -> do
compiledMain <- compileMain tmpDir
-- Write out the nockma function to force full evaluation of the compiler
writeFileEnsureLn (tmpDir <//> $(mkRelFile "test.nockma")) (ppSerialize compiledMain)
return compiledMain
let _testProgramFormula = anomaCall args
_testEvalOptions = defaultEvalOptions
return Test {..}
Expand Down

0 comments on commit 1413f8f

Please sign in to comment.