Skip to content

Commit cc197d2

Browse files
committed
basic support for lit naturals
1 parent 3bd489c commit cc197d2

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ The backend builds off Agda 2.7.0.1.
99

1010
## Setup
1111

12-
Compatible with Coq 8.20.0 and MetaCoq 1.3.2.
12+
Compatible with Coq 8.19.0, MetaCoq 1.3.1 and CertiCoq 0.9.
13+
14+
```
15+
opam pin add coq 8.19.0
16+
opam pin add certicoq 0.9+8.19
17+
coq_makefile -f _CoqProject -o CoqMakefile
18+
make -f CoqMakefile
19+
cabal run agda2lambox -- --out-dir build -itest test/Nat.agda
20+
```
1321

1422
## TODO
1523

src/Agda2Lambox/Convert/Term.hs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import Agda.Utils
1313
import Agda.Syntax.Literal
1414
import Agda.Syntax.Abstract.Name ( QName(..), ModuleName(..) )
1515
import Agda.Syntax.Common.Pretty ( prettyShow )
16+
import Agda.TypeChecking.Primitive.Base ( getBuiltinName )
17+
import Agda.Compiler.Backend ( builtinNat, builtinZero, builtinSuc )
1618

1719
import LambdaBox
1820
import qualified LambdaBox as L
@@ -50,7 +52,7 @@ instance A.TTerm ~> L.Term where
5052
A.Erased _ -> fail "Erased matches are not supported."
5153
A.NotErased _ -> do
5254
calts <- traverse go talts
53-
cind <- go caseType
55+
cind <- go caseType
5456
return $ LCase cind 0 (LRel n) calts
5557
A.TUnit -> return LBox
5658
A.TSort -> return LBox
@@ -68,12 +70,25 @@ instance A.CaseType ~> L.Inductive where
6870
go = \case
6971
A.CTData qn -> return $ L.Inductive (qnameToKerName qn) 0
7072
-- TODO(flupe): handle mutual inductive
73+
74+
-- Builtin Nat
75+
A.CTNat -> do
76+
liftTCM (getBuiltinName builtinNat) >>= \case
77+
Nothing -> fail "Builtin Nat not bound."
78+
Just qn -> return $ L.Inductive (qnameToKerName qn) 0
79+
7180
_ -> fail "Not supported case type"
7281

7382
-- TODO(flupe): handle using MetaCoq tPrim and prim_val
7483
instance A.Literal ~> L.Term where
7584
go = \case
76-
LitNat n -> fail "Literal natural numbers not supported"
85+
LitNat n -> do
86+
Just qnat <- liftTCM $ getBuiltinName builtinNat
87+
88+
let indnat = L.Inductive (qnameToKerName qnat) 0
89+
90+
return $ (!! fromInteger n) $ iterate (LApp (LCtor indnat 1 [])) (LCtor indnat 0 [])
91+
7792
LitWord64 w -> fail "Literal int64 not supported"
7893
LitFloat f -> fail "Literal float not supported"
7994
LitString s -> fail "Literal string not supported"

test/Nat.agda

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
data Nat : Set where
2+
zero : Nat
3+
succ : Nat Nat
4+
{-# BUILTIN NATURAL Nat #-}
5+
6+
add : Nat Nat Nat
7+
add zero y = y
8+
add (succ x) y = succ (add x y)
9+
10+
prog : Nat
11+
prog = add 3 2
12+
{-# COMPILE AGDA2LAMBOX prog #-}
13+
14+
-- 5
15+
res : Nat
16+
res = 5

0 commit comments

Comments
 (0)