-
Notifications
You must be signed in to change notification settings - Fork 28
/
stlc.lean
167 lines (128 loc) · 6.16 KB
/
stlc.lean
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- Church-coded STLC
--------------------------------------------------------------------------------
def Ty : Type 1
:= ∀ (Ty : Type)
(nat top bot : Ty)
(arr prod sum : Ty → Ty → Ty)
, Ty
def nat : Ty := λ _ nat _ _ _ _ _ => nat
def top : Ty := λ _ _ top _ _ _ _ => top
def bot : Ty := λ _ _ _ bot _ _ _ => bot
def arr : Ty → Ty → Ty
:= λ A B Ty nat top bot arr prod sum =>
arr (A Ty nat top bot arr prod sum) (B Ty nat top bot arr prod sum)
def prod : Ty → Ty → Ty
:= λ A B Ty nat top bot arr prod sum =>
prod (A Ty nat top bot arr prod sum) (B Ty nat top bot arr prod sum)
def sum : Ty → Ty → Ty
:= λ A B Ty nat top bot arr prod sum =>
sum (A Ty nat top bot arr prod sum) (B Ty nat top bot arr prod sum)
def Con : Type 1
:= ∀ (Con : Type)
(nil : Con)
(snoc : Con → Ty → Con)
, Con
def nil : Con
:= λ Con nil snoc => nil
def snoc : Con → Ty → Con
:= λ Γ A Con nil snoc => snoc (Γ Con nil snoc) A
def Var : Con → Ty → Type 1
:= λ Γ A =>
∀ (Var : Con → Ty → Type)
(vz : ∀{Γ A}, Var (snoc Γ A) A)
(vs : ∀{Γ B A}, Var Γ A → Var (snoc Γ B) A)
, Var Γ A
def vz : ∀ {Γ A}, Var (snoc Γ A) A
:= λ Var vz vs => vz
def vs : ∀ {Γ B A}, Var Γ A → Var (snoc Γ B) A
:= λ x Var vz vs => vs (x Var vz vs)
def Tm : Con → Ty → Type 1
:= λ Γ A =>
∀ (Tm : Con → Ty → Type)
(var : ∀ {Γ A}, Var Γ A → Tm Γ A)
(lam : ∀ {Γ A B}, (Tm (snoc Γ A) B → Tm Γ (arr A B)))
(app : ∀ {Γ A B} , Tm Γ (arr A B) → Tm Γ A → Tm Γ B)
(tt : ∀ {Γ} , Tm Γ top)
(pair : ∀ {Γ A B} , Tm Γ A → Tm Γ B → Tm Γ (prod A B))
(fst : ∀ {Γ A B} , Tm Γ (prod A B) → Tm Γ A)
(snd : ∀ {Γ A B} , Tm Γ (prod A B) → Tm Γ B)
(left : ∀ {Γ A B} , Tm Γ A → Tm Γ (sum A B))
(right : ∀ {Γ A B} , Tm Γ B → Tm Γ (sum A B))
(case : ∀ {Γ A B C} , Tm Γ (sum A B) → Tm Γ (arr A C) → Tm Γ (arr B C) → Tm Γ C)
(zero : ∀ {Γ} , Tm Γ nat)
(suc : ∀ {Γ} , Tm Γ nat → Tm Γ nat)
(rec : ∀ {Γ A} , Tm Γ nat → Tm Γ (arr nat (arr A A)) → Tm Γ A → Tm Γ A)
, Tm Γ A
def var : ∀ {Γ A}, Var Γ A → Tm Γ A
:= λ x Tm var lam app tt pair fst snd left right case zero suc rec =>
var x
def lam : ∀ {Γ A B} , Tm (snoc Γ A) B → Tm Γ (arr A B)
:= λ t Tm var lam app tt pair fst snd left right case zero suc rec =>
lam (t Tm var lam app tt pair fst snd left right case zero suc rec)
def app : ∀ {Γ A B} , Tm Γ (arr A B) → Tm Γ A → Tm Γ B
:= λ t u Tm var lam app tt pair fst snd left right case zero suc rec =>
app (t Tm var lam app tt pair fst snd left right case zero suc rec)
(u Tm var lam app tt pair fst snd left right case zero suc rec)
def tt : ∀ {Γ} , Tm Γ top
:= λ Tm var lam app tt pair fst snd left right case zero suc rec => tt
def pair : ∀ {Γ A B} , Tm Γ A → Tm Γ B → Tm Γ (prod A B)
:= λ t u Tm var lam app tt pair fst snd left right case zero suc rec =>
pair (t Tm var lam app tt pair fst snd left right case zero suc rec)
(u Tm var lam app tt pair fst snd left right case zero suc rec)
def fst : ∀ {Γ A B} , Tm Γ (prod A B) → Tm Γ A
:= λ t Tm var lam app tt pair fst snd left right case zero suc rec =>
fst (t Tm var lam app tt pair fst snd left right case zero suc rec)
def snd : ∀ {Γ A B} , Tm Γ (prod A B) → Tm Γ B
:= λ t Tm var lam app tt pair fst snd left right case zero suc rec =>
snd (t Tm var lam app tt pair fst snd left right case zero suc rec)
def left : ∀ {Γ A B} , Tm Γ A → Tm Γ (sum A B)
:= λ t Tm var lam app tt pair fst snd left right case zero suc rec =>
left (t Tm var lam app tt pair fst snd left right case zero suc rec)
def right : ∀ {Γ A B} , Tm Γ B → Tm Γ (sum A B)
:= λ t Tm var lam app tt pair fst snd left right case zero suc rec =>
right (t Tm var lam app tt pair fst snd left right case zero suc rec)
def case : ∀ {Γ A B C} , Tm Γ (sum A B) → Tm Γ (arr A C) → Tm Γ (arr B C) → Tm Γ C
:= λ t u v Tm var lam app tt pair fst snd left right case zero suc rec =>
case (t Tm var lam app tt pair fst snd left right case zero suc rec)
(u Tm var lam app tt pair fst snd left right case zero suc rec)
(v Tm var lam app tt pair fst snd left right case zero suc rec)
def zero : ∀ {Γ} , Tm Γ nat
:= λ Tm var lam app tt pair fst snd left right case zero suc rec => zero
def suc : ∀ {Γ} , Tm Γ nat → Tm Γ nat
:= λ t Tm var lam app tt pair fst snd left right case zero suc rec =>
suc (t Tm var lam app tt pair fst snd left right case zero suc rec)
def rec : ∀ {Γ A} , Tm Γ nat → Tm Γ (arr nat (arr A A)) → Tm Γ A → Tm Γ A
:= λ t u v Tm var lam app tt pair fst snd left right case zero suc rec =>
rec (t Tm var lam app tt pair fst snd left right case zero suc rec)
(u Tm var lam app tt pair fst snd left right case zero suc rec)
(v Tm var lam app tt pair fst snd left right case zero suc rec)
--------------------------------------------------------------------------------
def v0 : ∀ {Γ A}, Tm (snoc Γ A) A
:= var vz
def v1 : ∀ {Γ A B}, Tm (snoc (snoc Γ A) B) A
:= var (vs vz)
def v2 : ∀ {Γ A B C}, Tm (snoc (snoc (snoc Γ A) B) C) A
:= var (vs (vs vz))
def v3 : ∀ {Γ A B C D}, Tm (snoc (snoc (snoc (snoc Γ A) B) C) D) A
:= var (vs (vs (vs vz)))
def tbool : Ty
:= sum top top
def true : ∀ {Γ}, Tm Γ tbool
:= left tt
def tfalse : ∀ {Γ}, Tm Γ tbool
:= right tt
def ifthenelse : ∀ {Γ A}, Tm Γ (arr tbool (arr A (arr A A)))
:= lam (lam (lam (case v2 (lam v2) (lam v1))))
def times4 : ∀ {Γ A}, Tm Γ (arr (arr A A) (arr A A))
:= lam (lam (app v1 (app v1 (app v1 (app v1 v0)))))
def add : ∀ {Γ}, Tm Γ (arr nat (arr nat nat))
:= lam (rec v0
(lam (lam (lam (suc (app v1 v0)))))
(lam v0))
def mul : ∀ {Γ}, Tm Γ (arr nat (arr nat nat))
:= lam (rec v0
(lam (lam (lam (app (app add (app v1 v0)) v0))))
(lam zero))
def fact : ∀ {Γ}, Tm Γ (arr nat nat)
:= lam (rec v0 (lam (lam (app (app mul (suc v1)) v0)))
(suc zero))