-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTTT.rkt
250 lines (210 loc) · 7.24 KB
/
TTT.rkt
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
#lang racket
(require (rename-in redex/reduction-semantics
[define-judgment-form define-judgement-form]
[test-judgment-holds test-judgement-holds]
[judgment-holds judgement-holds])
(rename-in redex/pict
[render-judgment-form render-judgement-form])
pict
file/convertible)
;; Truncated Type Theory
(define-language TTT
(x y f g ::= variable-not-otherwise-mentioned)
(U ::= ★ □ △)
(e t u ::= x (Π (x : t) t) (λ (x : t) e) (e e) (let [x e] e) U)
(* ::= : =)
(Γ ::= · (· (x * t) ...))
#:binding-forms
(Π (x : t_1) t_2 #:refers-to x)
(λ (x : t) e #:refers-to x)
(let [x e_1] e_2 #:refers-to x)
#;(· (x * t) #:...bind (dom x (shadow dom x))))
(default-language TTT)
(define-syntax-rule (test-term-equal term1 term2)
(test-equal (term term1) (term term2)))
(define-syntax-rule (test-relation-holds rel)
(test-predicate identity (term rel)))
;; ★ is impredicative, the others are not
(define-metafunction TTT
rule : U U -> U
[(rule U ★) ★]
[(rule ★ U) U]
[(rule □ □) □]
[(rule U △) △]
[(rule △ U) △])
;; Extend and extract from the environment
(define-relation TTT
:∈ ⊆ x × t × Γ
[(:∈ x t Γ)
(where (· _ ... (x : t) _ ...) Γ)])
(define-relation TTT
=∈ ⊆ x × e × Γ
[(=∈ x e Γ)
(where (· _ ... (x = e) _ ...) Γ)])
(define-metafunction TTT
+: : Γ x t -> Γ
[(+: · x t) (· (x : t))]
[(+: (· any ...) x t)
(· any ... (x : t))])
(define-metafunction TTT
+= : Γ x e -> Γ
[(+= · x t) (· (x = e))]
[(+= (· any ...) x e)
(· any ... (x = e))])
;; Other metafunctions for sequential versions of terms
(define-metafunction TTT
Π* : (x : t) ... t -> t
[(Π* e) e]
[(Π* (x : t) any ... t)
(Π (x : t) (Π* any ... t))])
(define-metafunction TTT
λ* : (x : t) ... e -> e
[(λ* e) e]
[(λ* (x : t) any ... e)
(λ (x : t) (λ* any ... e))])
(define-metafunction TTT
@* : e e ... -> e
[(@* e) e]
[(@* e e_hd e_tl ...)
(@* (e e_hd) e_tl ...)])
(define-metafunction TTT
let* : [x e] ... e -> e
[(let* e) e]
[(let* [x e] any ... e_body)
(let [x e] (let* any ... e_body))])
;; TTT is truncated because:
;; - There are only three universes, and △ itself is not a term.
;; - There is only a typed definitional equality judgement, no typing judgement.
;; - There is cumulativity for universes but no subtyping for function types.
;; Note also that in rules Π, λ, and let, the second premise context and the type
;; favour the left-hand side for the bound type or term,
;; but the right-hand version should also be derivable by symmetry.
(define-judgement-form TTT
#:contract (⊢ Γ e e t)
[------------ "★"
(⊢ Γ ★ ★ □)]
[------------ "□"
(⊢ Γ □ □ △)]
[(⊢ Γ t_1 t_2 ★)
--------------- "★⊆U"
(⊢ Γ t_1 t_2 U)]
[(⊢ Γ t_1 t_2 U)
---------------- "U⊆△"
(⊢ Γ t_1 t_2 △)]
[(⊢ Γ e_2 e_1 t)
--------------- "sym"
(⊢ Γ e_1 e_2 t)]
[(⊢ Γ e_1 e_2 t)
(⊢ Γ e_2 e_3 t)
--------------- "trans"
(⊢ Γ e_1 e_3 t)]
[(⊢ Γ e_1 e_2 t_2)
(⊢ Γ t_1 t_2 U)
----------------- "conv"
(⊢ Γ e_1 e_2 t_1)]
[(:∈ x t Γ)
----------- "∈"
(⊢ Γ x x t)]
[(⊢ Γ t_1 t_2 U_1)
(⊢ (+: Γ x t_1) u_1 u_2 U_2)
-------------------------------------------------------- "Π"
(⊢ Γ (Π (x : t_1) u_1) (Π (x : t_2) u_2) (rule U_1 U_2))]
[(⊢ Γ t_1 t_2 U)
(⊢ (+: Γ x t_1) e_1 e_2 u)
--------------------------------------------------------- "λ"
(⊢ Γ (λ (x : t_1) e_1) (λ (x : t_2) e_2) (Π (x : t_1) u))]
[(⊢ Γ e_11 e_21 (Π (x : t) u))
(⊢ Γ e_12 e_22 t)
--------------------------------------------------- "@"
(⊢ Γ (e_11 e_12) (e_21 e_22) (substitute u x e_12))]
[(⊢ Γ e_11 e_21 t)
(⊢ (+= (+: Γ x t) x e_11) e_12 e_22 u)
------------------------------------------------------------------- "let"
(⊢ Γ (let [x e_11] e_12) (let [x e_21] e_22) (substitute u x e_11))]
[(⊢ Γ t t U)
(⊢ Γ e_2 e_2 t)
(⊢ (+: Γ x t) e_1 e_1 u)
----------------------------------------------------------------------- "β"
(⊢ Γ ((λ (x : t) e_1) e_2) (substitute e_1 x e_2) (substitute u x e_2))]
[(⊢ Γ t t U)
(⊢ (+: Γ x t) (e x) (e x) u)
--------------------------------------- "η"
(⊢ Γ (λ (x : t) (e x)) e (Π (x : t) u))]
[(⊢ Γ e_1 e_1 t)
(⊢ (+= (+: Γ x t) x e_1) e_2 e_2 u)
------------------------------------------------------------------- "ζ"
(⊢ Γ (let [x e_1] e_2) (substitute e_2 x e_1) (substitute u x e_1))]
[(:∈ x t Γ)
(=∈ x e Γ)
----------- "δ"
(⊢ Γ x e t)])
;; Examples
(define D-★
(derivation
'(⊢ · ★ ★ □) "★" '()))
(define D-★-weak
(derivation
'(⊢ (· (X : ★)) ★ ★ □) "★" '()))
(define D-□
(derivation
'(⊢ · □ □ △) "□" '()))
(define D-★△
(derivation
'(⊢ · ★ ★ △) "U⊆△"
(list D-★)))
(define-term ⊥
(Π (X : ★) X))
(test-term-equal (rule □ ★) ★)
(test-term-equal (+: · X ★) (· (X : ★)))
(test-relation-holds (:∈ X ★ (· (X : ★))))
(define D-∈
(derivation
'(⊢ (· (X : ★)) X X ★) "∈" '()))
(define D-⊥
(derivation
`(⊢ · ,(term ⊥) ,(term ⊥) ★) "Π"
(list D-★ D-∈)))
(define D-★→★
(derivation
'(⊢ · (Π (X : ★) ★) (Π (X : ★) ★) □) "Π"
(list D-★
D-★-weak)))
(test-judgement-holds ⊢ D-★)
(test-judgement-holds ⊢ D-□)
(test-judgement-holds ⊢ D-★△)
(test-judgement-holds ⊢ D-∈)
(test-judgement-holds ⊢ D-⊥)
(test-judgement-holds ⊢ D-★→★)
(test-results)
;; Render judgment rules as image
(define (render-pretty)
(default-style 'swiss)
(define rules
(with-compound-rewriters
(['substitute (match-lambda [`(,substitute ,< ,e_body ,x ,e ,>)
(list "" e_body "[" x " ↦ " e "]")])]
['rule (match-lambda [`(,< ,rule ,U₁ ,U₂ ,>)
(list rule "(" U₁ ", " U₂ ")")])]
[':∈ (match-lambda [`(,< ,:∈ ,x ,t ,Γ ,>)
(list "(" x " : " t ") ∈ " Γ)])]
['=∈ (match-lambda [`(,< ,=∈ ,x ,e ,Γ ,>)
(list "(" x " ≔ " e ") ∈ " Γ)])]
['+: (match-lambda [`(,< ,+: ,Γ ,x ,t ,>)
(list Γ ", " x " : " t)])]
['+= (match-lambda [`(,< ,+= ,Γ ,x ,t ,>)
(list Γ ", " x " ≔ " t)])]
['let (match-lambda [`(,< ,let ,(struct* lw ([e `(,<< ,x ,e₁ ,>>)])) ,e₂ ,>)
(list "(let " x " ≔ " e₁ " in " e₂ ")")])]
['⊢ (match-lambda [`(,< ,⊢ ,Γ ,e₁ ,e₂ ,t ,>)
(list Γ " ⊢ " e₁ " ≡ " e₂ " : " t)])])
#;(render-metafunction rule #:contract? #t)
(render-judgement-form ⊢)))
(cc-superimpose (colorize (filled-rectangle (pict-width rules) (pict-height rules)) "white") rules))
(define (render-file filename #:type type)
(define req
(match type
['png 'png@2x-bytes]
['svg 'svg-bytes]))
(with-output-to-file filename
(thunk (write-bytes (convert (render-pretty) req)))
#:exists 'replace))