Skip to content

Commit b36ad4f

Browse files
committed
hide term compilation monad
1 parent 1107ae3 commit b36ad4f

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

src/Agda2Lambox/Compile/Function.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Agda.Utils.Monad (guardWithError)
2020

2121
import Agda.Utils ( etaExpandCtor )
2222
import Agda2Lambox.Compile.Utils
23-
import Agda2Lambox.Compile.Term ( runC, compileTerm, withMutuals )
23+
import Agda2Lambox.Compile.Term ( compileTerm )
2424
import LambdaBox qualified as LBox
2525

2626

@@ -32,9 +32,9 @@ isFunction _ = False
3232

3333
-- | Convert a function body to a Lambdabox term.
3434
compileFunctionBody :: [QName] -> Definition -> TCM LBox.Term
35-
compileFunctionBody fixpoints Defn{defName} = do
35+
compileFunctionBody ms Defn{defName} = do
3636
Just t <-toTreeless EagerEvaluation defName
37-
runC . withMutuals fixpoints . compileTerm =<< etaExpandCtor t
37+
compileTerm ms =<< etaExpandCtor t
3838

3939

4040
-- | Whether to compile a function definition to λ□.

src/Agda2Lambox/Compile/Term.hs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{-# LANGUAGE NamedFieldPuns, DerivingVia, GeneralizedNewtypeDeriving #-}
22
module Agda2Lambox.Compile.Term
3-
( C
4-
, runC
5-
, withMutuals
6-
, compileTerm
3+
( compileTerm
74
) where
85

96
import Control.Monad ( mapM )
@@ -74,8 +71,16 @@ withMutuals ms = local \e -> e { mutuals = reverse ms }
7471
-- * Term conversion
7572

7673
-- | Convert a treeless term to its λ□ equivalent.
77-
compileTerm :: TTerm -> C LBox.Term
78-
compileTerm = \case
74+
compileTerm
75+
:: [QName]
76+
-- ^ Local fixpoints.
77+
-> TTerm
78+
-> TCM LBox.Term
79+
compileTerm ms = runC . withMutuals ms . compileTermC
80+
81+
-- | Convert a treeless term to its λ□ equivalent.
82+
compileTermC :: TTerm -> C LBox.Term
83+
compileTermC = \case
7984
TVar n -> pure $ LRel n
8085
TPrim p -> fail "primitives not supported"
8186

@@ -86,20 +91,20 @@ compileTerm = \case
8691
Just i -> LRel $ i + boundVars
8792

8893
TCon q -> liftTCM $ toConApp q []
89-
TApp (TCon q) args -> liftTCM . toConApp q =<< mapM compileTerm args
94+
TApp (TCon q) args -> liftTCM . toConApp q =<< mapM compileTermC args
9095
-- ^ For dealing with fully-applied constructors
9196

9297
TApp u es -> do
93-
cu <- compileTerm u
94-
ces <- mapM compileTerm es
98+
cu <- compileTermC u
99+
ces <- mapM compileTermC es
95100
pure $ foldl' LApp cu ces
96101

97-
TLam t -> underBinder $ LLam <$> compileTerm t
102+
TLam t -> underBinder $ LLam <$> compileTermC t
98103

99104
TLit l -> fail "literals not supported"
100105

101-
TLet u v -> LLet <$> compileTerm u
102-
<*> underBinder (compileTerm v)
106+
TLet u v -> LLet <$> compileTermC u
107+
<*> underBinder (compileTermC v)
103108

104109
TCase n CaseInfo{..} dt talts ->
105110
case caseErased of
@@ -125,6 +130,6 @@ compileCaseType = \case
125130
compileAlt :: TAlt -> C ([LBox.Name], LBox.Term)
126131
compileAlt = \case
127132
TACon{..} -> let names = take aArity $ repeat LBox.Anon
128-
in (names,) <$> underBinders aArity (compileTerm aBody)
133+
in (names,) <$> underBinders aArity (compileTermC aBody)
129134
TALit{..} -> fail "literal patterns not supported"
130135
TAGuard{..} -> fail "case guards not supported"

theories/Eval.v

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,42 @@ From MetaCoq.Erasure Require Import EAst.
66
From CertiCoq.Common Require Import Common.
77
From CertiCoq.LambdaBoxMut Require Import compile term program wcbvEval.
88

9+
910
(* TODO: eta-expand here *)
1011

1112
(* convert a lambda box program to certicoq lambda box mut, and run it *)
1213
Definition eval_program (p : EAst.program) : exception Term :=
1314
let prog := {| env := LambdaBoxMut.compile.compile_ctx (fst p);
1415
main := compile (snd p)
1516
|}
16-
in wcbvEval (env prog) (2 ^ 14) (main prog).
17+
in wcbvEval (env prog) (2 ^ 14) (main prog).
18+
19+
20+
(* Courtesy of Eske *)
21+
22+
From CertiCoq Require Import LambdaBoxLocal.toplevel.
23+
From CertiCoq Require Import LambdaANF.toplevel.
24+
From CertiCoq Require Import Compiler.pipeline.
25+
Require Import ExtLib.Structures.Monad.
26+
Import MonadNotation.
27+
From CertiCoq Require Import Common.Common Common.compM Common.Pipeline_utils.
28+
29+
Definition box_to_wasm (p : EAst.program) :=
30+
(* For simplicity we assume that the program contains no primitives *)
31+
let prims := [] in
32+
let next_id := 100%positive in
33+
let opts := default_opts in
34+
(* Translate lambda_box -> lambda_boxmut *)
35+
let p_mut := {| CertiCoq.Common.AstCommon.main := LambdaBoxMut.compile.compile (snd p) ; CertiCoq.Common.AstCommon.env := LambdaBoxMut.compile.compile_ctx (fst p) |} in
36+
check_axioms prims p_mut;;
37+
(* Translate lambda_boxmut -> lambda_boxlocal *)
38+
p_local <- compile_LambdaBoxLocal prims p_mut;;
39+
(* Translate lambda_boxlocal -> lambda_anf *)
40+
p_anf <- compile_LambdaANF_ANF next_id prims p_local;;
41+
(* Translate lambda_anf -> lambda_anf *)
42+
p_anf <- compile_LambdaANF next_id p_anf;;
43+
(* Compile lambda_anf -> WASM *)
44+
compile_LambdaANF_to_Wasm prims p_anf.
45+
46+
Definition test (p : EAst.program) :=
47+
run_pipeline _ _ default_opts p box_to_wasm.

0 commit comments

Comments
 (0)