This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.rkt
138 lines (125 loc) · 5.07 KB
/
core.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
#lang racket
(provide unifier
check-type
subst
typeof
typeof-expanded
local-expand-expr
syntax-property*
free-identifier?
constructor?
normalize)
(require syntax/parse
syntax/stx
racket/dict
"helper/stx-util.rkt"
"helper/id-hash.rkt")
(define (unifier subst-map)
(define (unify? t1 t2)
(cond
[(and (syntax? t1) (syntax? t2)) (void)]
[(syntax? t1) (raise-syntax-error 'bad-unification
(format "~a is not syntax" t2)
t1)]
[(syntax? t2) (raise-syntax-error 'bad-unification
(format "~a is not syntax" t1)
t2)]
[else (error 'not-syntax "~a or ~a" t1 t2)])
(syntax-parse (list t1 t2)
[(a b) #:when (and (free-identifier? t1)
(free-identifier? t2))
(define bounded? (hash-ref subst-map (syntax->datum #'a) #f))
(if bounded?
(unify? t2 bounded?)
#t)]
[(a b) #:when (free-identifier? t2)
(unify? t2 t1)]
[(a b) #:when (free-identifier? t1)
(define bounded? (hash-ref subst-map (syntax->datum #'a) #f))
(if bounded?
(if (unify? (hash-ref subst-map (syntax->datum #'a)) t2)
#t
(raise-syntax-error 'cannot-unified
(format "`~a` with `~a`"
(syntax->datum (hash-ref subst-map (syntax->datum #'a)))
(syntax->datum t2))
t2))
(begin
(hash-set! subst-map (syntax->datum #'a) #'b)
#t))]
[((a ...) (b ...))
(define (recheck a b)
(unless (stx-length=? a b)
(raise-syntax-error 'bad-unification
(format "mismatched length for two term, ~a and ~a"
t1 t2)
t2)))
(cond
[(stx-length=? #'(a ...) #'(b ...))
(stx-andmap unify? #'(a ...) #'(b ...))]
; have to try expand when length mismatched, this is because sometimes one of them is implicit applied form
[(stx-length<=? #'(a ...) #'(b ...))
(define ta (normalize #'(a ...)))
(recheck ta #'(b ...))
(stx-andmap unify? ta #'(b ...))]
[(stx-length>=? #'(a ...) #'(b ...))
(define tb (normalize #'(b ...)))
(recheck #'(a ...) tb)
(stx-andmap unify? #'(a ...) tb)])]
[(a b) (equal? (syntax->datum t1) (syntax->datum t2))]))
unify?)
(define (normalize stx)
(datum->syntax stx (eval (local-expand-expr stx)) stx))
(define (check-type term type
[subst-map (make-hash)]
[locals (make-mutable-id-hash)])
(define unify? (unifier subst-map))
(unless (unify? (typeof term locals) type)
(raise-syntax-error 'type-mismatch
(format "expect: `~a`, get: `~a`"
(syntax->datum (subst type subst-map))
(syntax->datum (subst (typeof term locals) subst-map)))
term)))
(define (subst stx m)
(syntax-parse stx
[(A a ...)
; The source location of first syntax takes by quasisyntax/loc will be the second constructed syntax's source location
(quasisyntax/loc stx
(A #,@(stx-map (λ (b) (subst b m)) #'(a ...))))]
[name:id (hash-ref m (syntax->datum #'name) stx)]))
(define (typeof stx [locals (make-mutable-id-hash)])
(syntax-parse stx
[x:id #:when (free-identifier? #'x)
(dict-ref locals stx)]
[x:id #:when (syntax-property (local-expand-expr stx) 'type)
(syntax-property (local-expand-expr stx) 'type)]
[x:id (raise-syntax-error 'no-type (format "~a has no type" stx) stx)]
[(x:id a* ...)
(syntax-parse (typeof #'x locals)
[(Pi ([x* : typ*] ...) result-ty) #'result-ty])]))
(define (typeof-expanded stx)
(syntax->datum (typeof stx)))
(define (local-expand-expr stx [identifiers '()])
(local-expand stx 'expression identifiers))
(define (syntax-property* stx . pairs)
(match pairs
[(cons key (cons value '()))
(syntax-property stx key value)]
[(cons key (cons value rest))
(syntax-property (apply syntax-property* (cons stx rest))
key value)]))
(define (constructor? stx)
(and (bounded-identifier? stx)
(syntax-property (local-expand-expr stx)
'constructor)))
(define (free-identifier? id-stx)
(and (identifier? id-stx)
(not (identifier-binding id-stx))))
(define (bounded-identifier? id-stx)
(and (identifier? id-stx)
(identifier-binding id-stx (syntax-local-phase-level) #t)))
(module+ test
(require rackunit)
(check-equal? (syntax->datum
(subst #'(List A) (make-hash '((A . Nat)))))
'(List Nat)))