-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.pug
338 lines (312 loc) · 15 KB
/
index.pug
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
include header
html
head
meta(property='og:title' content='HENK')
meta(property='og:description' content='CoC Pure Type System')
meta(property='og:url' content='https://henk.groupoid.space/')
block title
title HENK
block content
+header('', 'Pure Type System', 'The minimal language with universal quantifier and infinity number of universes for consistent typechecking and normalization')
article.main
.exe
section
h1 Abstract
aside Namdak Tönpa
time DATE: 10 OCT 2016
section
p.
The <b>PTS<sup>∞</sup></b> programming language is a higher-order dependently typed lambda calculus,
an extension of Calculus of Constructions (Coquand, Huet) with the
predicative/impredicative hierarchy of indexed universes.
.semantics
section
h2#nat Universes
p.
The infinite hierarchy of universes allows to
avoid paradoxes (Girard, Russel, Hurkens) in type theory (Martin-Löf).
code.
U₀ : U₁ : U₂ : U₃ : …
U₀ — propositions
U₁ — values and sets
U₂ — types
U₃ — sorts
br
+tex(true).
$$
\begin{equation}
\tag{S}
\dfrac
{o : Nat}
{Type_o}
\end{equation}
$$
h2#axioms Predicative Universes
p.
All terms obey the <b>A</b> ranking inside the sequence of <b>S</b> universes,
and the complexity <b>R</b> of the dependent term is equal to a maximum of
the term's complexity and its dependency.
The universes system is completely described by the following
PTS notation (due to Barendregt):
code.
S (n : nat) = U n
A₁ (n m : nat) = U n : U m where m > n — cumulative
R₁ (m n : nat) = U m ⟶ U n : U (max m n) — predicative
br.
p.
Note that predicative universes are incompatible with Church lambda term encoding.
You can switch between predicative and impredicative uninverses using typecheker parameter.
+tex(true).
$$
\begin{equation}
\tag{A₁}
\dfrac
{i: Nat,\ \ \ \ j: Nat,\ \ \ \ i < j}
{Type_i : Type_{j}}
\end{equation}
$$
+tex(true).
$$
\begin{equation}
\tag{R₁}
\dfrac
{i : Nat,\ \ \ \ j : Nat}
{Type_i \rightarrow Type_{j} : Type_{max(i,j)}}
\end{equation}
$$
h2#axioms Impredicative Universes
p.
Propositional contractible bottom space is the only
available extension to predicative hierarchy that does not lead to inconsistency.
However, there is another option to have infinite
impredicative hierarchy.
code.
A₂ (n : nat) = U n : U (n + 1) — non-cumulative
R₂ (m n : nat) = U m ⟶ U n : U n — impredicative
br.
+tex(true).
$$
\begin{equation}
\tag{A₂}
\dfrac
{i: Nat}
{Type_i : Type_{i+1}}
\end{equation}
$$
+tex(true).
$$
\begin{equation}
\tag{R₂}
\dfrac
{i : Nat,\ \ \ \ j : Nat}
{Type_i \rightarrow Type_{j} : Type_{j}}
\end{equation}
$$
h2#ast Single Axiom Language
p.
This language is called one axiom language (or pure) as eliminator
and introduction adjoint functors inferred from type formation rule.
The only computation rule of Pi type is called beta-reduction.
code.
Pi (A: U) (B: A -> U): U = (x: A) -> B x
lambda (A: U) (B: A -> U) (b: Pi A B): Pi A B = \ (x: A) -> b x
app (A: U) (B: A -> U) (f: Pi A B) (a: A): B a = f a
beta (A: U) (B: A -> U) (a:A) (f: Pi A B): Equ (B a) (app A B (lambda A B f) a) (f a) =
br.
+tex(true).
\begin{equation}
\tag{$\Pi$-formation}
\dfrac
{x:A \vdash B : Type}
{\Pi\ (x:A) \rightarrow B : Type}
\end{equation}
+tex(true).
\begin{equation}
\tag{$\lambda$-intro}
\dfrac
{x:A \vdash b : B}
{\lambda\ (x:A) \rightarrow b : \Pi\ (x: A) \rightarrow B }
\end{equation}
br.
+tex(true).
$$
\begin{equation}
\tag{$App$-elim}
\dfrac
{f: (\Pi\ (x:A) \rightarrow B)\ \ \ a: A}
{f\ a : B\ [a/x]}
\end{equation}
$$
+tex(true).
$$
\begin{equation}
\tag{$\beta$-comp}
\dfrac
{x:A \vdash b: B\ \ \ a:A}
{(\lambda\ (x:A) \rightarrow b)\ a = b\ [a/x] : B\ [a/x]}
\end{equation}
$$
p.
This language could be embedded in itself and used
as Logical Framework for the Pi type:
code.
PTS (A: U): U
= (intro: (A -> U) -> U)
* (lambda: (B: A -> U) -> pi A B -> intro B)
* (app: (B: A -> U) -> intro B -> pi A B)
* ((B: A -> U) (f: pi A B) -> (a: A)
-> Path (B a) ((app B (lambda B f)) a) (f a))
br.
h1#impl Implementation
h2#ast Syntax
p.
The terms of PTS<sup>∞</sup> consist of <b>nat</b> indexed stars, variables, applications,
abstractions, and universal quantifications. This language is called Calculus
of Construction and exists in various syntaxes.
PTS<sup>∞</sup> supports <b>Morte's</b> syntax.
code.
<> = #option
I = #identifier
U = * < #number >
PTS = U | I | PTS → PTS | ∀ ( I : PTS ) → PTS
| PTS PTS | ( PTS ) | λ ( I : PTS ) → PTS
p.
Equivalent HOAS tree encoding for parsed terms is following:
code.
data pts (lang: U)
= star (n: nat)
| var (x: name) (l: nat)
| pi (x: name) (l: nat) (f: lang)
| lambda (x: name) (l: nat) (f: lang)
| app (f a: lang)
data PTS
= pure (_: pts PTS)
br.
h2 Universes
code.
star (:star,N) -> N
star _ -> (:error, "*")
h2 Functions
code.
func ((:forall,),(I,O)) -> true
func T -> (:error,(:forall,T))
h2 Variables
code.
var N B -> var N B (proplists:is_defined N B)
var N B true -> true
var N B false -> (:error,("free var",N,proplists:get_keys(B)))
h2 Shift
code.
sh (:var,(N,I)),N,P) when I>=P -> (:var,(N,I+1))
sh ((:forall,(N,0)),(I,O)),N,P) -> ((:forall,(N,0)),sh I N P,sh O N P+1)
sh ((:lambda,(N,0)),(I,O)),N,P) -> ((:lambda,(N,0)),sh I N P,sh O N P+1)
sh (Q,(L,R),N,P) -> (Q,sh L N P,sh R N P)
sh (T,N,P) -> T
h2 Substitution
code.
sub Term Name Value -> sub Term Name Value 0
sub (:arrow, (I,O)) N V L -> (:arrow, sub I N V L,sub O N V L);
sub ((:forall,(N,0)),(I,O)) N V L -> ((:forall,(N,0)),sub I N V L,sub O N(sh V N 0)L+1)
sub ((:forall,(F,X)),(I,O)) N V L -> ((:forall,(F,X)),sub I N V L,sub O N(sh V F 0)L)
sub ((:lambda,(N,0)),(I,O)) N V L -> ((:lambda,(N,0)),sub I N V L,sub O N(sh V N 0)L+1)
sub ((:lambda,(F,X)),(I,O)) N V L -> ((:lambda,(F,X)),sub I N V L,sub O N(sh V F 0)L)
sub (:app, (F,A)) N V L -> (:app,sub F N V L,sub A N V L)
sub (:var, (N,L)) N V L -> V
sub (:var, (N,I)) N V L when I>L -> (:var,(N,I-1))
sub T _ _ _ -> T.
h2 Normalization
code.
norm :none -> :none
norm :any -> :any
norm (:app,(F,A)) -> case norm F of
((:lambda,(N,0)),(I,O)) -> norm (sub O N A)
NF -> (:app,(NF,norm A)) end
norm (:remote,N) -> cache (norm N [])
norm (:arrow, (I,O)) -> ((:forall,("_",0)), (norm I,norm O))
norm ((:forall,(N,0)),(I,O)) -> ((:forall,(N,0)), (norm I,norm O))
norm ((:lambda,(N,0)),(I,O)) -> ((:lambda,(N,0)), (norm I,norm O))
norm T -> T
h2 Definitional Equality
code.
eq ((:forall,("_",0)), X) (:arrow,Y) -> eq X Y
eq (:app,(F1,A1)) (:app,(F2,A2)) -> let true = eq F1 F2 in eq A1 A2
eq (:star,N) (:star,N) -> true
eq (:var,(N,I)) (:var,(N,I)) -> true
eq (:remote,N) (:remote,N) -> true
eq ((:farall,(N1,0)),(I1,O1))
((:forall,(N2,0)),(I2,O2)) ->
let true = eq I1 I2 in eq O1 (sub (sh O2 N1 0) N2 (:var,(N1,0)) 0)
eq ((:lambda,(N1,0)),(I1,O1))
((:lambda,(N2,0)),(I2,O2)) ->
let true = eq I1 I2 in eq O1 (sub (sh O2 N1 0) N2 (:var,(N1,0)) 0)
eq (A,B) -> (:error,(:eq,A,B))
h2 Type Checker
code.
type (:star,N) _ -> (:star,N+1)
type (:var,(N,I)) D -> let true = var N D in keyget N D I
type (:remote,N) D -> cache type N D
type (:arrow,(I,O)) D -> (:star,h(star(type I D)),star(type O D))
type ((:forall,(N,0)),(I,O)) D -> (:star,h(star(type I D)),star(type O [(N,norm I)|D]))
type ((:lambda,(N,0)),(I,O)) D -> let star (type I D),
NI = norm I in ((:forall,(N,0)),(NI,type O [(N,NI)|D])))
type (:app,(F,A)) D -> let T = type(F,D),
true = func T,
((:forall,(N,0)),(I,O)) = T,
Q = type A D,
true = eq I Q in norm (sub O N A)
section
h1 USAGE
h2#normal Normalization (by Evaluation)
p Terms in PTS<sup>∞</sup> language.
code.
$ om show List/Cons
λ (A: *)
→ λ (Head: A)
→ λ (Tail:
∀ (List: *)
→ ∀ (Cons:
∀ (Head: A)
→ ∀ (Tail: List)
→ List)
→ ∀ (Nil: List)
→ List)
→ λ (List: *)
→ λ (Cons:
∀ (Head: A)
→ ∀ (Tail: List)
→ List)
→ λ (Nil: List)
→ Cons Head (Tail List Cons Nil)
h2#erased Type Erasure
p.
The untyped lambda language O is the simplest language used in
PTS<sup>∞</sup> to generate backend programs. This O is used as the output of type erasure.
code.
I = #identifier
O = I | ( O ) | O O | λ I -> O
br.
code.
Inductive O := Var: nat → O | App: O → O → O | Lambda: nat → O → O → O
p.
Terms in untyped lambda pure language.
code.
$ om print fst erase a "#List/Cons"
( λ Head
→ ( λ Tail
→ ( λ Cons
→ ( λ Nil
→ ((Cons Head) ((Tail Cons) Nil))))))
ok
h2#syntax Extraction
p.
Erlang extracted code O-BEAM. For other targets you may want to read about
lazy continuation-passing style <a href='../lang/cps/'>O-CPS</a> interpreter (Rust).
code.
'Cons'() -> fun (Head) -> fun (Tail) -> fun (Cons) -> fun (Nil) ->
((Cons(Head))((Tail(Cons))(Nil))) end end end end.
section
h1 Bibliography
p.
[1]. <a href="https://github.com/groupoid/groupoid.space/blob/master/articles/pts/pts.pdf">Addendum I: Pure Type System for Erlang</a><br>
[2]. <a href="https://www.cse.chalmers.se/~coquand/v1.pdf">Some remarks about Dependent Type Theory</a><br>
include footer