/
2019-05-09-untyped.agda
133 lines (106 loc) · 2.87 KB
/
2019-05-09-untyped.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
-- untyped lambda calculus
module _ where
module _ where
-- function
module _ where
_▹_ : {A B C : Set} → (A → B) → (B → C) → (A → C)
(f ▹ g) x = g (f x)
_#_ : {A B : Set} → A → (A → B) → B
x # f = f x
-- dependendent pair
module _ where
record Σ (A : Set) (B : A → Set) : Set where
field
π₁ : A
π₂ : B π₁
∃ : {A : Set} → (B : A → Set) → Set
∃ {A} B = Σ A B
-- finite product
module _ where
record _×_ (A B : Set) : Set where
field
fst : A
snd : B
-- bool
module _ where
data Bool : Set where
false true : Bool
-- natural
module _ where
data ℕ : Set where
zero : ℕ
succ : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_=?_ : ℕ → ℕ → Bool
zero =? zero = true
zero =? succ j = false
succ i =? zero = false
succ i =? succ j = i =? j
infix 5 ƛ_
infixr 10 _^_
data Lam : Set where
$ : ℕ → Lam
_^_ : Lam → Lam → Lam
ƛ_ : Lam → Lam
-- conversion
module _ where
subst : Lam → ℕ → Lam → Lam
subst ($ j) i g = (i =? j) # \{ false → $ j ; true → g }
subst (f₁ ^ f₂) i g = subst f₁ i g ^ subst f₂ i g
subst (ƛ f) i g = ƛ subst f (succ i) g
infix 4 _⇒^_
data _⇒^_ : Lam → Lam → Set where
β-step : ∀ {f g} → (ƛ f) ^ g ⇒^ subst f zero g
infix 4 _⇒_
data _⇒_ : Lam → Lam → Set where
here : ∀ {f f'} → f ⇒^ f' → f ⇒ f'
appL : ∀ {f f' g} → f ⇒ f' → f ^ g ⇒ f' ^ g
appR : ∀ {f g g'} → g ⇒ g' → f ^ g ⇒ f ^ g'
abs : ∀ {f f'} → f ⇒ f' → ƛ f ⇒ ƛ f'
infix 4 _⇒*_
data _⇒*_ : Lam → Lam → Set where
pure : ∀ {f f'} → f ⇒ f' → f ⇒* f'
refl : ∀ {f} → f ⇒* f
trans : ∀ {f g h} → f ⇒ g → g ⇒* h → f ⇒* h
infix 4 _=β_
_=β_ : Lam → Lam → Set
_=β_ = {!!}
record normal-form (f : Lam) : Set where
field
nf : Lam
reduction : f ⇒* nf
normal : {h : Lam} → f ⇒* h → h ⇒* nf
church-rosser : ∀ {f g₁ g₂} → f ⇒* g₁ → f ⇒* g₂ → ∃ \(h : Lam) → (g₁ ⇒* h) × (g₂ ⇒* h)
church-rosser = {!!}
module _ where
$0 : Lam
$0 = $ 0
$1 : Lam
$1 = $ 1
-- terms
module _ where
-- infinite reduction chain
module _ where
eΩ : Lam
eΩ = eω ^ eω where eω = ƛ $0 ^ $0
_ : eΩ ⇒ eΩ
_ = here β-step
-- general recursion
module _ where
eY : Lam
eY = ƛ (eY' ^ eY') where eY' = ƛ $1 ^ ($0 ^ $0)
_ : {f : Lam} → eY ^ f =β f ^ (eY ^ f)
_ = {!!}
-- naturals
module _ where
-- zero : 1 ⇛ ℕ
eZero : Lam
eZero = ƛ ƛ $1
-- succ : ℕ ⇛ ℕ
eSucc : Lam → Lam
eSucc k = ƛ ƛ ($0 ^ k)
{-
case : A, (A ⇛ A) → ℕ ⇛ A
case (f, g) zero = f
case (f, g) (succ k) = g (case (f, g) k)
-}