Skip to content

Commit 54ed5b8

Browse files
committed
evaluate programs in coq
1 parent cc197d2 commit 54ed5b8

File tree

9 files changed

+66
-21
lines changed

9 files changed

+66
-21
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ The backend builds off Agda 2.7.0.1.
1212
Compatible with Coq 8.19.0, MetaCoq 1.3.1 and CertiCoq 0.9.
1313

1414
```
15-
opam pin add coq 8.19.0
1615
opam pin add certicoq 0.9+8.19
1716
coq_makefile -f _CoqProject -o CoqMakefile
1817
make -f CoqMakefile
@@ -29,9 +28,8 @@ cabal run agda2lambox -- --out-dir build -itest test/Nat.agda
2928
- [x] Make pretty-printer prettier
3029
- [x] Ensure well-formedness of generated programs inside Coq
3130
- [x] Make well-formedness check faster by splitting it into boolean and propositional.
32-
- [ ] Evaluate λ□ programs from inside Coq to start testing
33-
- [ ] Possibly salvage an existing interpreter
34-
- [ ] Or implement our own...
31+
- [x] Evaluate λ□ programs from inside Coq to start testing
32+
- [x] Using the λ□-Mut from CertiCoq
3533
- [ ] Support mutual inductives
3634
- [x] Support (one-inductive) records
3735
- [ ] Properly translate projections in terms.

_CoqProject

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-R theories Agda2Lambox
2-
-R build Build
32
theories/CheckWF.v
3+
theories/Eval.v

src/Agda2Lambox/Convert/Data.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ convertDatatype defn@Defn{defName, theDef} =
5959
-- NOTE(flupe): Agda's datatypes are *always* finite?
6060
-- Co-induction is restricted to records.
6161
-- We may want to set BiFinite for non-recursive datatypes, but I don't know yet.
62-
, indPars = dataPars
63-
-- TODO(flupe):
64-
-- ^ double-check, but I don't think we'll ever share parameters.
65-
-- unless, perhaps, when defined inside a parametrized module (?).
62+
, indPars = 0
6663
, indBodies = [inductive]
6764
}

src/Agda2Lambox/Convert/Term.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import qualified LambdaBox as L
2222
import Agda2Lambox.Monad
2323
import Agda2Lambox.Convert.Class
2424

25+
-- quick fix, remove this asap
26+
app :: L.Term -> L.Term -> L.Term
27+
app (LCtor ind k es) arg = LCtor ind k (es ++ [arg])
28+
app t arg = LApp t arg
2529

2630
-- | Compiling (treeless) Agda terms into Lambox expressions.
2731
instance A.TTerm ~> L.Term where
@@ -36,7 +40,7 @@ instance A.TTerm ~> L.Term where
3640
A.TApp t args -> do
3741
ct <- go t
3842
cargs <- mapM go args
39-
return $ foldl LApp ct cargs
43+
return $ foldl app ct cargs
4044
A.TLam t -> inBoundVar $ LLam <$> go t
4145
A.TLit l -> go l
4246
A.TCon qn -> do
@@ -87,7 +91,7 @@ instance A.Literal ~> L.Term where
8791

8892
let indnat = L.Inductive (qnameToKerName qnat) 0
8993

90-
return $ (!! fromInteger n) $ iterate (LApp (LCtor indnat 1 [])) (LCtor indnat 0 [])
94+
return $ (!! fromInteger n) $ iterate (app (LCtor indnat 1 [])) (LCtor indnat 0 [])
9195

9296
LitWord64 w -> fail "Literal int64 not supported"
9397
LitFloat f -> fail "Literal float not supported"

src/CoqGen.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ instance Pretty (ToCoq CoqModule) where
173173
, "From MetaCoq.Common Require Import BasicAst Kernames Universes."
174174
, "From MetaCoq.Utils Require Import bytestring."
175175
, "From MetaCoq.Erasure Require Import EAst."
176-
, "From Agda2Lambox Require Import CheckWF."
176+
, "From Agda2Lambox Require Import CheckWF Eval."
177177
, "Import ListNotations."
178178
]
179179

@@ -182,9 +182,11 @@ instance Pretty (ToCoq CoqModule) where
182182

183183
, "Compute @check_wf_glob eflags env."
184184

185-
, vsep $ flip map (zip [1..] coqPrograms) \(i :: Int, kn) -> vsep
186-
[ hang ("Definition prog" <> pretty i <> " : program :=") 2 $
185+
, vsep $ flip map (zip [1..] $ reverse coqPrograms) \(i :: Int, kn) ->
186+
let progname = "prog" <> pretty i in vsep
187+
[ hang ("Definition " <> progname <> " : program :=") 2 $
187188
pcoq (text "env" :: Doc, LConst kn)
188189
<> "."
190+
, "Compute eval_program " <> progname <> "."
189191
]
190192
]

test/Map.agda

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
data Nat : Set where
2+
zero : Nat
3+
succ : Nat Nat
4+
5+
add : Nat -> Nat -> Nat
6+
add zero y = y
7+
add (succ x) y = succ (add x y)
8+
9+
infixr 7 _::_
10+
data List (A : Set) : Set where
11+
[] : List A
12+
_::_ : A -> List A -> List A
13+
14+
xs : List Nat
15+
xs = zero :: succ zero :: succ (succ zero) :: []
16+
17+
map : {A B : Set} -> (A -> B) -> List A -> List B
18+
map f [] = []
19+
map f (x :: xs) = f x :: map f xs
20+
21+
ys : List Nat
22+
ys = map (\x -> add x x) xs
23+
{-# COMPILE AGDA2LAMBOX ys #-}

test/Nat.agda

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
data Nat : Set where
22
zero : Nat
33
succ : Nat Nat
4-
{-# BUILTIN NATURAL Nat #-}
4+
5+
one : Nat
6+
one = succ zero
57

68
add : Nat Nat Nat
79
add zero y = y
810
add (succ x) y = succ (add x y)
911

12+
deux trois : Nat
13+
deux = succ (succ zero)
14+
trois = succ (succ (succ zero))
15+
1016
prog : Nat
11-
prog = add 3 2
17+
prog = add trois deux
1218
{-# COMPILE AGDA2LAMBOX prog #-}
13-
14-
-- 5
15-
res : Nat
16-
res = 5

theories/CheckWF.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ Notation "x 'eqn:' p" := (exist _ x p) (only parsing, at level 20).
2323
try auto.
2424
*)
2525

26-
Definition eflags : EEnvFlags := all_env_flags.
26+
Definition eflags : EEnvFlags :=
27+
{| has_axioms := true;
28+
term_switches := all_term_flags;
29+
has_cstr_params := false; (* CertiCoq doesn't want params in ctors *)
30+
cstr_as_blocks := true (* CertiCoq seem to require fully-applied ctors *)
31+
|}.
2732

2833
Fixpoint check_fresh_global (k : kername) (decls : global_declarations) : bool :=
2934
match decls with

theories/Eval.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(* This file provides utilises to evaluate lambda box programs *)
2+
3+
From Coq Require Import Nat.
4+
From MetaCoq.Utils Require Import utils.
5+
From MetaCoq.Erasure Require Import EAst.
6+
From CertiCoq.Common Require Import Common.
7+
From CertiCoq.LambdaBoxMut Require Import compile term program wcbvEval.
8+
9+
(* convert a lambda box program to certicoq lambda box mut, and run it *)
10+
Definition eval_program (p : EAst.program) : exception Term :=
11+
let prog := {| env := LambdaBoxMut.compile.compile_ctx (fst p);
12+
main := compile (snd p)
13+
|}
14+
in wcbvEval (env prog) (2 ^ 14) (main prog).

0 commit comments

Comments
 (0)