Skip to content

Commit 66dee97

Browse files
committed
add support for primitives
1 parent a6abb8e commit 66dee97

File tree

7 files changed

+96
-78
lines changed

7 files changed

+96
-78
lines changed

demo/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ <h1>agda2lambox</h1>
4646
<fieldset>
4747
<button id="run" disabled>Run</button>
4848
<button id="reload" disabled>Reload</button>
49+
<button id="clear">Clear</button>
4950
<select id="decoder">
5051
<option value="nat">Nat</option>
5152
<option value="bool">Bool</option>
@@ -69,6 +70,7 @@ <h1>agda2lambox</h1>
6970
on(file, "change", load)
7071
on(reload, "click", load)
7172
on(run, "click", exec)
73+
on(clear, "click", () => {output.innerHTML = ""})
7274

7375
async function load() {
7476
if (file.files.length == 0) return

src/Agda2Lambox/Compile.hs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import Data.Map qualified as Map
1515
import Data.Set qualified as Set
1616
import Agda.Compiler.Backend
1717
import Agda.Syntax.Internal ( QName )
18-
import Agda.Syntax.Common (Arg(..))
18+
import Agda.Syntax.Common (Arg(..), IsOpaque (TransparentDef))
1919
import Agda.Syntax.Common.Pretty ( prettyShow )
2020
import Agda.TypeChecking.Monad ( liftTCM, getConstInfo )
2121
import Agda.TypeChecking.Pretty
2222
import Agda.Utils.Monad ( whenM, ifM, unlessM, ifNotM, orM, forM_ )
23+
import Agda.Utils.SmallSet qualified as SmallSet
2324

2425
import Agda.Utils ( hasPragma, isDataOrRecDef, treeless, isArity )
2526

@@ -110,7 +111,33 @@ compileDefinition target defn@Defn{..} = setCurrentRange defName do
110111
reportSDoc "agda2lambox.compile" 5 $
111112
"Found primitive: " <> prettyTCM defName <> ". Compiling it as axiom."
112113

113-
typ <- whenTyped target $ compileTopLevelType defType
114-
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
114+
getBuiltinThing (PrimitiveName primName) >>= \case
115+
-- it's a primitive with an actual implementation
116+
-- we try to convert it to a function, manually
117+
Just (Prim (PrimFun{})) -> do
118+
let def = Function
119+
{ funClauses = primClauses
120+
, funCompiled = primCompiled
121+
, funSplitTree = Nothing
122+
, funTreeless = Nothing
123+
, funCovering = []
124+
, funInv = primInv
125+
, funMutual = Just [defName]
126+
, funProjection = Left NeverProjection
127+
, funFlags = SmallSet.empty
128+
, funTerminates = Just True
129+
, funExtLam = Nothing
130+
, funWith = Nothing
131+
, funIsKanOp = Nothing
132+
, funOpaque = TransparentDef
133+
}
134+
135+
let defn' = defn {theDef = def}
136+
liftTCM $ modifyGlobalDefinition defName $ const defn'
137+
compileFunction target defn'
138+
139+
_ -> do
140+
typ <- whenTyped target $ compileTopLevelType defType
141+
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
115142

116143
_ -> genericError $ "Cannot compile: " <> prettyShow defName

src/Main.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ agda2lambox = Backend backend
104104
, mayEraseType = \ _ -> return True
105105
}
106106

107-
108-
109-
110107
moduleSetup
111108
:: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath
112109
-> TCM (Recompile ModuleEnv ModuleRes)
@@ -131,7 +128,6 @@ writeModule Options{..} menv IsMain m defs = do
131128
let fileName = (outDir </>) . moduleNameToFileName m
132129
coqMod = CoqModule env (map qnameToKName programs)
133130

134-
135131
liftIO do
136132
putStrLn $ "Writing " <> fileName ".txt"
137133
pp coqMod <> "\n" & writeFile (fileName ".txt")

test/BigDemo.agda

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module _ where
22

3+
{-
34
-- - decision procedure (barebones)
45
module BAREBONES where
56
open import Agda.Primitive using (Level)
@@ -36,48 +37,56 @@ module BAREBONES where
3637
instance
3738
HasDecEq-Nat : HasDec (x ≡ y)
3839
HasDecEq-Nat {x}{y} .dec with x | y
39-
... | zero | zero = yes refl
40-
... | zero | suc n = no λ ()
40+
... | zero | zero = yes refl
41+
... | zero | suc n = no λ ()
4142
... | suc m | zero = no λ ()
4243
... | suc m | suc n with dec
43-
... | yes eq = yes (cong suc eq)
44+
... | yes eq = yes (cong suc eq)
4445
... | no m≢n = no λ where refl → m≢n refl
4546
4647
test : Bool
47-
test = isYes (dec {A = 4242})
48-
-- {-# COMPILE AGDA2LAMBOX test #-}
48+
test = isYes (dec {A = 4 ≡ 4})
49+
{-# COMPILE AGDA2LAMBOX test #-}
50+
-- -}
4951

52+
{-
5053
-- - decision procedure (importing stdlib)
5154
module STDLIB where
5255
open import Relation.Nullary using (isYes)
5356
5457
open import Data.Bool using (Bool; true; false)
5558
open import Data.Bool.Properties using (_≟_)
5659
test : Bool
57-
test = isYes (truefalse)
58-
-- {-# COMPILE AGDA2LAMBOX test #-}
60+
test = isYes (falsetrue)
61+
{-# COMPILE AGDA2LAMBOX test #-}
5962

6063
-- open import Data.Bool using (Bool)
6164
-- open import Data.Nat.Properties using (_≟_)
6265
-- test : Bool
6366
-- test = isYes (42 ≟ 42)
6467
-- {-# COMPILE AGDA2LAMBOX test #-}
6568

69+
-- -}
70+
71+
-- {-
6672
-- - sort
6773
module SORT where
68-
open import Data.List.Base using (List; _∷_; [])
69-
70-
open import Data.Bool.Properties using (≤-decTotalOrder)
71-
open import Data.Bool using (Bool; true; false)
72-
open import Data.List.Sort ≤-decTotalOrder using (sort)
73-
test : List Bool
74-
test = sort (true ∷ false ∷ true ∷ false ∷ [])
75-
{-# COMPILE AGDA2LAMBOX test #-}
76-
77-
-- open import Data.Nat.Properties using (≤-decTotalOrder)
78-
-- open import Data.Bool using (Bool; true; false)
7974
-- open import Data.List.Base using (List; _∷_; [])
75+
-- open import Data.Bool.Properties using (≤-decTotalOrder)
76+
-- open import Data.Bool using (Bool; true; false)
8077
-- open import Data.List.Sort ≤-decTotalOrder using (sort)
78+
8179
-- test : List Bool
82-
-- test = sort (42053 ∷ [])
80+
-- test = sort (truefalsetruefalse ∷ [])
8381
-- {-# COMPILE AGDA2LAMBOX test #-}
82+
83+
open import Data.Nat.Base
84+
open import Data.Nat.Properties using (≤-decTotalOrder)
85+
open import Data.List.Base using (List; _∷_; [])
86+
open import Data.List.Sort ≤-decTotalOrder using (sort)
87+
88+
test : List ℕ
89+
test = sort (42053 ∷ [])
90+
{-# COMPILE AGDA2LAMBOX test #-}
91+
92+
-- -}

test/Map.agda

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
open import Agda.Builtin.Nat
1+
open import Agda.Builtin.Nat using (Nat; suc; zero)
22
open import Agda.Builtin.List
33

4+
double : Nat Nat
5+
double zero = zero
6+
double (suc x) = suc (suc (double x))
7+
48
xs : List Nat
5-
xs = zero ∷ []
9+
xs = 135 ∷ []
610

711
map : {A B : Set} -> (A B) List A List B
812
map f [] = []
913
map f (x ∷ xs) = f x ∷ map f xs
1014

1115
ys : List Nat
12-
ys = map (2 +_) xs
16+
ys = map double xs
1317
{-# COMPILE AGDA2LAMBOX ys #-}

test/Nat.agda

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
open import Agda.Builtin.Nat
22

3-
one : Nat
4-
one = suc zero
3+
thing : Nat
4+
thing = 1 + 2
5+
{-# COMPILE AGDA2LAMBOX thing #-}
56

6-
{-
7-
add : Nat → Nat → Nat
8-
add zero y = y
9-
add (suc x) y = suc (add x y)
10-
11-
incr : Nat → Nat
12-
incr = suc
13-
14-
deux trois : Nat
15-
deux = suc (suc zero)
16-
trois = suc (suc (suc zero))
17-
18-
prog : Nat
19-
prog = add trois deux
20-
{-# COMPILE AGDA2LAMBOX prog #-}
21-
-}

test/STLC.agda

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,53 @@
1+
{-# OPTIONS --erasure #-}
12
open import Agda.Builtin.Nat
23

3-
-- we have to redefine addition on builtin nats
4-
-- because the builtin _+_ doesn't compile yet
5-
add : Nat Nat Nat
6-
add zero y = y
7-
add (suc x) y = suc (add x y)
8-
9-
variable n : Nat
4+
variable @0 n : Nat
105

116
data Type : Set where
127
: Type
138
_⇒_ : Type Type Type
149

15-
variable α β : Type
10+
variable @0 α β : Type
1611

1712
infixl 7 _▷_
18-
data Ctx : Nat Set where
13+
data Ctx : @0 Nat Set where
1914
[] : Ctx zero
2015
_▷_ : Ctx n Type Ctx (suc n)
2116

22-
variable Γ : Ctx n
17+
variable @0 Γ : Ctx n
2318

2419
infix 6 _∋_
25-
data _∋_ : Ctx n Type Set where
20+
data _∋_ : @0 Ctx n @0 Type Set where
2621
here : Γ ▷ α ∋ α
2722
there : Γ ∋ α Γ ▷ β ∋ α
2823

29-
data Tm: Ctx n) : Type Set where
30-
var : Γ ∋ α Tm Γ α
31-
lam : Tm (Γ ▷ α) β Tm Γ (α ⇒ β)
32-
app : Tm Γ (α ⇒ β) Tm Γ α Tm Γ β
33-
34-
lit : Nat Tm Γ ℕ
35-
_`+_ : Tm Γ ℕ Tm Γ ℕ Tm Γ ℕ
24+
data _⊢_ (@0 Γ : Ctx n) : @0 Type Set where
25+
var : Γ ∋ α Γ ⊢ α
26+
lam : (Γ ▷ α) ⊢ β Γ ⊢ (α ⇒ β)
27+
app : Γ ⊢ (α ⇒ β) Γ ⊢ α Γ ⊢ β
28+
lit : Nat Γ ⊢ ℕ
29+
plus : Γ ⊢ ℕ Γ ⊢ ℕ Γ ⊢ ℕ
3630

37-
⟦_⟧ : Type Set
38-
= Nat
39-
α ⇒ β= α ⟦ β ⟧
31+
Value : Type Set
32+
Value = Nat
33+
Value (α ⇒ β) = Value α Value β
4034

41-
data Env : Ctx n Set where
35+
data Env : @0 Ctx n Set where
4236
[] : Env []
43-
_▷_ : Env Γ ⟦ α ⟧ Env (Γ ▷ α)
37+
_▷_ : Env Γ Value α Env (Γ ▷ α)
4438

45-
lookupEnv : Env Γ Γ ∋ α ⟦ α ⟧
39+
lookupEnv : Env Γ Γ ∋ α Value α
4640
lookupEnv (env ▷ x) here = x
4741
lookupEnv (env ▷ _) (there k) = lookupEnv env k
4842

49-
eval : Env Γ Tm Γ α ⟦ α ⟧
50-
eval env (var k) = lookupEnv env k
51-
eval env (lam u) = λ x eval (env ▷ x) u
52-
eval env (app u v) = eval env u (eval env v)
53-
eval env (lit x) = x
54-
eval env (u `+ v) = add (eval env u) (eval env v)
43+
eval : Env Γ Γ ⊢ α Value α
44+
eval env (var k) = lookupEnv env k
45+
eval env (lam u) = λ x eval (env ▷ x) u
46+
eval env (app u v) = eval env u (eval env v)
47+
eval env (lit x) = x
48+
eval env (plus u v) = eval env u + eval env v
5549

50+
-- (λ x → x + 1) 1
5651
test : Nat
57-
test = eval [] (app (lam (lit 1 `+ var here)) (lit 1))
52+
test = eval [] (app (lam (plus (lit 1) (var here))) (lit 1))
5853
{-# COMPILE AGDA2LAMBOX test #-}

0 commit comments

Comments
 (0)