/
fig10-dep.rkt
66 lines (57 loc) · 2.25 KB
/
fig10-dep.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
#lang turnstile+/quicklang
(require (only-in turnstile+/eval define-red [reflect/m ⇑])
(only-in turnstile+/typedefs define-type)
"type-n.rkt") ; import Type
;; Figure 10: dependently typed core calculus
(provide Type (for-syntax ~Type) TypeTop
Π (for-syntax ~Π)
λ (rename-out [app #%app] [β app/eval/1])
ann define provide for-syntax)
;; (Type 99) just for demo purposes
;; (see Cur for proper hierarchy implementation)
(define-type Π [#:bind (Type 99)] (Type 99) : Type)
;; lambda and #%app -----------------------------------------------------------
(define-typed-syntax λ
;; expected ty only
[(_ y:id e) ⇐ (~Π [x:id : τ_in] τ_out) ≫
[[x ≫ x- : τ_in] ⊢ ($subst x y e) ≫ e- ⇐ τ_out]
---------
[⊢ (λ- (x-) e-)]]
;; both expected ty and annotations
[(_ [y:id (~datum :) τ_in*] e) ⇐ (~Π [x:id : τ_in] τ_out) ≫
[⊢ τ_in* ≫ τ_in** ⇐ Type]
#:when (typecheck? #'τ_in** #'τ_in)
[[x ≫ x- : τ_in] ⊢ ($subst x y e) ≫ e- ⇐ τ_out]
-------
[⊢ (λ- (x-) e-)]]
;; annotations only
[(_ [x:id (~datum :) τ_in] e) ≫
[⊢ τ_in ≫ τ_in- ⇒ (~Type _)]
[[x ≫ x- : τ_in-] ⊢ e ≫ e- ⇒ τ_out]
-------
[⊢ (λ- (x-) e-) ⇒ (Π [x- : τ_in-] τ_out)]])
(define-typerule (app e_fn e_arg) ≫
[⊢ e_fn ≫ e_fn- ⇒ (~Π [X : τ_in] τ_out)]
[⊢ e_arg ≫ e_arg- ⇐ τ_in]
-----------------------------
[⊢ (β e_fn- e_arg-) ⇒ (⇑ ($subst e_arg- X τ_out))])
;; fig 11: β as a plain macro (does not cooperate with other reduction rules)
#;(define-for-syntax (⇑v1 e) (subst #'β #'#%plain-app e))
#;(define-syntax β
(syntax-parser
[(_ f e)
(syntax-parse (list (expand/df #'f) (expand/df #'e))
[(((~literal #%plain-lambda) (x-) body-) e-)
(⇑v1 (subst #'e- #'x- #'body-))]
[(f- e-) #'(#%plain-app f- e-)])]))
(define-red β
(#%plain-app ((~literal #%plain-lambda) (x) body) e)
~> ($subst e x body))
(define-typed-syntax (ann e (~datum :) τ) ≫
[⊢ e ≫ e- ⇐ τ]
--------
[⊢ e- ⇒ τ])
;; top-level ------------------------------------------------------------------
(define-syntax define
(syntax-parser
[(_ alias:id τ) #'(define-syntax- alias (make-variable-like-transformer #'τ))]))