Skip to content

Commit b10fe0d

Browse files
committed
Replacing stdlib N by our Nat
1 parent b485f65 commit b10fe0d

File tree

13 files changed

+104
-72
lines changed

13 files changed

+104
-72
lines changed

Lam.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ library
2525
exposed-modules:
2626
Lam.Context
2727
Lam.Data
28+
Lam.Nat
2829
Lam.Evaluator
2930
Lam.Handler
3031
Lam.Parser

src/Lam/Agda/Lam/Data.agda

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Lam.Data where
22

3+
open import Lam.Nat
4+
35
open import Data.Bool using (true; false)
46

57
open import Haskell.Prelude using (Int; Bool; String; _×_; Eq; _==_; _&&_)
@@ -9,18 +11,6 @@ Id = String
911

1012
{-# COMPILE AGDA2HS Id #-}
1113

12-
data Nat : Set where
13-
Z : Nat
14-
S : Nat Nat
15-
16-
instance
17-
iEqNat : Eq Nat
18-
iEqNat ._==_ Z Z = true
19-
iEqNat ._==_ (S x) (S y) = x == y
20-
iEqNat ._==_ _ _ = false
21-
22-
{-# COMPILE AGDA2HS Nat deriving (Eq, Show) #-}
23-
2414
data RawTypeL : Set where
2515
RawBoolT : RawTypeL
2616
RawIntT : RawTypeL

src/Lam/Agda/Lam/Evaluator.agda

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open import Haskell.Prelude hiding (Nat)
55
open import Data.Bool using (Bool; true; false)
66

77
open import Lam.Data
8+
open import Lam.Nat hiding (_<_)
89
open import Lam.UtilsAgda
910

1011
shiftUp' : Nat Expr Expr
@@ -62,7 +63,7 @@ shiftDown = shiftDown' Z
6263
substitute' : Nat Expr Expr Expr
6364
substitute' i s (App e1 e2) = App (substitute' i s e1) (substitute' i s e2)
6465
substitute' i s (Lam n t e) = Lam n t (substitute' (S i) (shiftUp s) e)
65-
substitute' i s (Var x) = if eqNat i x then s else Var x
66+
substitute' i s (Var x) = if i == x then s else Var x
6667
substitute' i s (Ite b t e) = Ite b' t' e'
6768
where b' = substitute' i s b
6869
t' = substitute' i s t

src/Lam/Agda/Lam/Export.agda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ module Lam.Export where
33
open import Lam.TypeChecker public
44
open import Lam.Evaluator public
55
open import Lam.Data public
6+
open import Lam.Nat public

src/Lam/Agda/Lam/FormalizationEvaluator.agda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Lam.FormalizationEvaluator where
22

33
open import Lam.Data
44
open import Lam.Evaluator
5+
open import Lam.Nat hiding (_<_)
56
open import Lam.UtilsAgda
67

78
open import Haskell.Prelude using (Just; Nothing; Int; Bool; _+_; _-_; _*_; _&&_; _||_; not; _<_)

src/Lam/Agda/Lam/FormalizationTypeChecker.agda

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
module Lam.FormalizationTypeChecker where
44

5-
open import Data.Fin.Base using (fromℕ<)
6-
open import Data.List
7-
open import Data.Nat using (ℕ; _<_)
5+
open import Data.List hiding (length; lookup)
86
open import Data.Product using (_×_) renaming (_,_ to ⟨_,_⟩)
97
open import Function.Base using (id)
108
open import Relation.Binary.PropositionalEquality using
@@ -16,6 +14,7 @@ open import Haskell.Prelude using
1614
(Bool; Int; Maybe; Just; Nothing; _>>=_; case_of_; if_then_else_; maybe; _==_)
1715

1816
open import Lam.Data
17+
open import Lam.Nat
1918
open import Lam.TypeChecker
2019
open import Lam.UtilsAgda
2120

@@ -69,9 +68,9 @@ data _⊢_∶_ : TypingContext → Expr → TypeL → Set where
6968
Γ ⊢ Ite b t e ∶ A
7069

7170
⊢v : : TypingContext} {i : Nat}
72-
{h : natToℕ i < length Γ}
71+
{h : i < length Γ}
7372
--------------------------------
74-
Γ ⊢ Var i ∶ lookup Γ (fromℕ< h)
73+
Γ ⊢ Var i ∶ lookup Γ i h
7574

7675
⊢l : : TypingContext} {name : Id} {body : Expr} {dom codom : TypeL}
7776
(dom ∷ Γ) ⊢ body ∶ codom

src/Lam/Agda/Lam/Nat.agda

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Lam.Nat where
2+
3+
open import Haskell.Prelude using (Eq; _==_; True; False; Bool; []; _∷_; List; Maybe; Nothing; Just)
4+
5+
data Nat : Set where
6+
Z : Nat
7+
S : Nat Nat
8+
9+
instance
10+
iEqNat : Eq Nat
11+
iEqNat ._==_ Z Z = True
12+
iEqNat ._==_ (S x) (S y) = x == y
13+
iEqNat ._==_ _ _ = False
14+
15+
{-# COMPILE AGDA2HS Nat deriving (Eq, Show) #-}
16+
17+
ltNat : Nat Nat Bool
18+
ltNat Z (S _) = True
19+
ltNat (S x) (S y) = ltNat x y
20+
ltNat _ _ = False
21+
22+
{-# COMPILE AGDA2HS ltNat #-}
23+
24+
inc : Nat Nat
25+
inc = S
26+
27+
{-# COMPILE AGDA2HS inc #-}
28+
29+
dec : Nat Nat
30+
dec Z = Z
31+
dec (S x) = x
32+
33+
{-# COMPILE AGDA2HS dec #-}
34+
35+
length : {A : Set} List A -> Nat
36+
length [] = Z
37+
length (_ ∷ xs) = S (length xs)
38+
39+
data _≤_ : Nat Nat Set where
40+
z≤ : {i} Z ≤ i
41+
s≤s : {i j} i ≤ j (S i) ≤ (S j)
42+
43+
_<_ : Nat -> Nat -> Set
44+
i < j = S i ≤ j
45+
46+
lookupMaybe : {t : Set} Nat List t Maybe t
47+
lookupMaybe _ [] = Nothing
48+
lookupMaybe Z (h ∷ _) = Just h
49+
lookupMaybe (S i) (_ ∷ t) = lookupMaybe i t
50+
51+
{-# COMPILE AGDA2HS lookupMaybe #-}
52+
53+
lookup : {A : Set} -> (l : List A) -> (i : Nat) -> (h : i < length l) -> A
54+
lookup (x ∷ l) Z h = x
55+
lookup (x ∷ l) (S i) (s≤s h) = lookup l i h

src/Lam/Agda/Lam/TypeChecker.agda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open import Data.List using (List)
55
open import Haskell.Prelude
66

77
open import Lam.Data
8+
open import Lam.Nat
89
open import Lam.UtilsAgda
910

1011
TypingContext : Set

src/Lam/Agda/Lam/UtilsAgda.agda

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,22 @@ module Lam.UtilsAgda where
33
import Agda.Builtin.Nat
44
open import Data.Empty using (⊥-elim)
55
open import Data.Fin.Base using (fromℕ<)
6-
open import Data.List
7-
open import Data.Nat using (ℕ; zero; suc; _<_)
6+
open import Data.List hiding (length; lookup)
87
open import Data.Product using (_×_) renaming (_,_ to ⟨_,_⟩)
98
open import Relation.Binary.PropositionalEquality using (_≡_; refl; cong; cong₂)
109
open import Relation.Nullary using (¬_)
1110

1211
open import Haskell.Prelude hiding (_<_; length; lookup; _×_; Nat; cong; cong₂; case_of_)
1312

1413
open import Lam.Data
14+
open import Lam.Nat
1515

1616
-- removing the equality proof of case_of_
1717
myCaseOf : {a b : Set} a (a b) b
1818
myCaseOf x f = f x
1919

2020
{-# COMPILE AGDA2HS myCaseOf #-}
2121

22-
-- Operations defined over our naturals
23-
24-
natToℕ : Nat
25-
natToℕ Z = zero
26-
natToℕ (S x) = suc (natToℕ x)
27-
28-
eqNat : Nat Nat Bool
29-
eqNat Z Z = True
30-
eqNat (S x) (S y) = eqNat x y
31-
eqNat _ _ = False
32-
33-
{-# COMPILE AGDA2HS eqNat #-}
34-
35-
ltNat : Nat Nat Bool
36-
ltNat Z (S _) = True
37-
ltNat (S x) (S y) = ltNat x y
38-
ltNat _ _ = False
39-
40-
{-# COMPILE AGDA2HS ltNat #-}
41-
42-
inc : Nat Nat
43-
inc = S
44-
45-
{-# COMPILE AGDA2HS inc #-}
46-
47-
dec : Nat Nat
48-
dec Z = Z
49-
dec (S x) = x
50-
51-
{-# COMPILE AGDA2HS dec #-}
52-
5322
eqType : TypeL TypeL Bool
5423
eqType BoolT BoolT = True
5524
eqType IntT IntT = True
@@ -61,16 +30,9 @@ eqType _ _ = False
6130

6231
{-# COMPILE AGDA2HS eqType #-}
6332

64-
lookupMaybe : {t : Set} Nat List t Maybe t
65-
lookupMaybe _ [] = Nothing
66-
lookupMaybe Z (h ∷ _) = Just h
67-
lookupMaybe (S i) (_ ∷ t) = lookupMaybe i t
68-
69-
{-# COMPILE AGDA2HS lookupMaybe #-}
70-
71-
lookup≡ : {t : Set} {l : List t} {i : Nat} (h : (natToℕ i) < length l) (lookupMaybe i l) ≡ Just (lookup l (fromℕ< h))
72-
lookup≡ {t} {x ∷ l} {Z} h = _≡_.refl
73-
lookup≡ {t} {x ∷ l} {S i} h = lookup≡ {t} {l} {i} (Data.Nat.≤-pred h)
33+
lookup≡ : {t : Set} {l : List t} {i : Nat} (h : i < length l) (lookupMaybe i l) ≡ Just (lookup l i h)
34+
lookup≡ {t} {x ∷ l} {Z} h = refl
35+
lookup≡ {t} {x ∷ l} {S i} (s≤s h) = lookup≡ h
7436

7537
&&to× : {a b : Bool} (a && b) ≡ True a ≡ True × b ≡ True
7638
&&to× {True} {True} h = ⟨ refl , refl ⟩
@@ -100,19 +62,15 @@ eqType-refl (Sum t1 t2) =
10062
injection-maybe : {t : Set} {a : t} ¬ (Nothing ≡ Just a)
10163
injection-maybe = λ ()
10264

103-
lookup?< : {t : Set} {l : List t} {i : Nat} {a : t} lookupMaybe i l ≡ Just a (natToℕ i) < length l
65+
lookup?< : {t : Set} {l : List t} {i : Nat} {a : t} lookupMaybe i l ≡ Just a i < length l
10466
lookup?< {_} {[]} {_} {_} eq = ⊥-elim (injection-maybe eq)
105-
lookup?< {t} {x ∷ l} {Z} eq = Data.Nat.s≤s Data.Nat.z≤n
106-
lookup?< {t} {x ∷ l} {S i} eq = Data.Nat.s≤s (lookup?< {t} {l} {i} eq)
67+
lookup?< {t} {x ∷ l} {Z} eq = s≤s z≤
68+
lookup?< {t} {x ∷ l} {S i} eq = s≤s (lookup?< eq)
10769

10870
iteAbs : {t : Set} {x y z : t} {b : Bool}
10971
¬ y ≡ z (if b then x else y) ≡ z b ≡ True × x ≡ z
11072
iteAbs {t} {x} {y} {z} {False} h₁ h₂ = ⊥-elim (h₁ h₂)
11173
iteAbs {t} {x} {y} {z} {True} h₁ h₂ = ⟨ refl , h₂ ⟩
11274

113-
liftEqNat : {n1 n2 : ℕ} Agda.Builtin.Nat._==_ n1 n2 ≡ Bool.true n1 ≡ n2
114-
liftEqNat {zero} {zero} h = refl
115-
liftEqNat {suc n1} {suc n2} h = cong suc (liftEqNat {n1} {n2} h)
116-
11775
Just-injective : {A : Set} {a b : A} (Just a) ≡ (Just b) _≡_ a b
11876
Just-injective refl = refl

src/Lam/Nat.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Lam.Nat where
2+
3+
data Nat = Z
4+
| S Nat
5+
deriving (Eq, Show)
6+
7+
ltNat :: Nat -> Nat -> Bool
8+
ltNat Z (S _) = True
9+
ltNat (S x) (S y) = ltNat x y
10+
ltNat _ _ = False
11+
12+
inc :: Nat -> Nat
13+
inc = S
14+
15+
dec :: Nat -> Nat
16+
dec Z = Z
17+
dec (S x) = x
18+
19+
lookupMaybe :: Nat -> [t] -> Maybe t
20+
lookupMaybe _ [] = Nothing
21+
lookupMaybe Z (h : _) = Just h
22+
lookupMaybe (S i) (_ : t) = lookupMaybe i t
23+

0 commit comments

Comments
 (0)