This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
generated from dannypsnl-fork/racket-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
180 lines (166 loc) · 5.57 KB
/
main.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
#lang racket
(require "core.rkt"
"substmap.rkt")
(struct env (parent map)
#:transparent)
(define (make-env [parent #f])
(env parent (make-hash)))
(define cur-Γ (make-parameter (make-env)))
(define (bind id val)
(hash-set! (env-map (cur-Γ)) id val))
(define (lookup id)
(let ([env (cur-Γ)])
(if env
(hash-ref (env-map env) id
(parameterize ([cur-Γ (env-parent env)])
(lookup id)))
#f)))
(define (<-type val)
(match val
[(Value _ ty) ty]))
(define (parse-constructor c [dependencies #f])
(match-let ([`(,name : ,typ) c])
(match typ
[`(→ ,t* ...)
#:when dependencies
(bind name (Constructor `(→ ,@(map (λ (t) (replace-occur #:in t #:occur dependencies)) t*))))]
[ty #:when dependencies
(bind name (Constructor (replace-occur #:in ty #:occur dependencies)))]
[else (bind name (Constructor typ))])))
(define (run statement)
(match statement
[`(,v : ,t)
(let ([v (eval v)])
(unify t (Value-typ v))
v)]
[`(data (,typ-name ,typ-dependency* ...) ,constructors* ...)
typ-name
(define deps (make-hash))
(for ([d typ-dependency*])
(match d
[`(,d ,dt)
(hash-set! deps d (FreeVar d dt))]
[d (hash-set! deps d (FreeVar d 'U))]))
(for ([c constructors*])
(parse-constructor c deps))]
[`(data ,typ-name ,constructors* ...)
typ-name
(for ([c constructors*])
(parse-constructor c))]
[`(define (,name [,param-name* : ,param-typ*] ...) : ,typ
,exp)
(bind name (Function param-name* param-typ* typ exp))]
[`(define (,name param* ...) : ,typ ,exp)
(error 'todo)]
[`(define ,name : ,typ ,exp)
(let ([s (make-subst)]
[e (eval exp)])
(unify typ (<-type e) #:subst s)
(bind name e))]
[exp (displayln (eval exp))]))
(define (eval exp)
(match exp
[`(match ,e
[,pat* => ,pat-e*] ...)
(match-let ([(Value val typ) (eval e)])
(define (pattern-equal? pat val)
(match pat
[`(,first ,rest ...)
(and (pattern-equal? first (car val))
(pattern-equal? rest (cdr val)))]
['_ #t]
[n (equal? n val)]))
(define result
(ormap (λ (pat pat-e)
(if (pattern-equal? pat val)
(eval pat-e)
#f))
pat* pat-e*))
(unless result
(error 'semantic "match fail ~a" exp))
result)]
[`(,app ,arg* ...)
(let ([appliable (eval app)]
[arg* (map (λ (a) (eval a)) arg*)])
(match appliable
[(Constructor typ)
(match typ
[`(→ ,t* ... ,t)
(let ([subst (make-subst)])
; TODO: currying fix
(for ([expect t*]
[arg arg*])
(unify expect (Value-typ arg) #:subst subst))
(Value `(,app ,@arg*)
(replace-occur #:in t #:occur (subst-resolve subst))))])]
[(Function param-name* param-typ* typ expr)
(let ([env (make-env (cur-Γ))]
[subst (make-subst)])
(parameterize ([cur-Γ env])
; TODO: currying fix
(for ([param param-name*]
[expect param-typ*]
[arg arg*])
(unify expect (Value-typ arg) #:subst subst)
(bind param arg))
(Value (eval expr)
(replace-occur #:in typ #:occur (subst-resolve subst)))))]
[(Value _ _) appliable]
[else (error 'semantic "not appliable: ~a" app)]))]
[name
(let ([v? (lookup name)])
(unless v? (error 'semantic "no identifier: ~a" name))
(cond
[(Constructor? v?)
(match (Constructor-typ v?)
[`(→ ,t* ... ,t)
v?]
[t (Value name t)])]
[(Function? v?) v?]
[(Value? v?) v?]
[else (error 'unknown "unknown: ~a => ~a" name v?)]))]))
(define pre-defined-Γ (make-env))
(parameterize ([cur-Γ pre-defined-Γ])
(run '(data Nat
[Zero : Nat]
[Suc : (→ Nat Nat)]))
(run '(data Bool
[True : Bool]
[False : Bool])))
(module+ test
(parameterize ([cur-Γ pre-defined-Γ])
(run 'Zero)
(run '(Suc Zero))
(run '((Suc (Suc Zero)) : Nat))
;; error case: semantic: type mismatched, expected: Nat, but got: Bool
#;(run '(Suc True)))
(parameterize ([cur-Γ pre-defined-Γ])
(run '(data (List T)
[Nil : (List T)]
[Cons : (→ T (List T) (List T))]))
(run '(Cons Zero Nil))
;; error case: semantic: type mismatched, expected: `Bool`, but got: `Nat`
#;(run '(Cons Zero (Cons True Nil))))
(parameterize ([cur-Γ pre-defined-Γ])
(run '(data (Vec T [N Nat])
[Nil : (Vec T Zero)]
[Cons : (→ T (Vec T N) (Vec T (Suc N)))]))
(run '(Cons Zero Nil))
(run '(Cons (Suc Zero) (Cons Zero Nil)))
;; error case: semantic: type mismatched, expected: `Nat`, but got: `Bool`
#;(run '(Cons True (Cons Zero Nil))))
(parameterize ([cur-Γ pre-defined-Γ])
(run '(define one : Nat
(Suc Zero)))
(run 'one)
(run '(define (add2 [n : Nat]) : Nat
(Suc (Suc n))))
(run '(add2 Zero))
(run '(add2 (Suc Zero))))
(parameterize ([cur-Γ pre-defined-Γ])
(run '(define (is-zero? [n : Nat]) : Bool
(match n
[Zero => True]
[(Suc _) => False])))
(run '(is-zero? Zero))
(run '(is-zero? (Suc Zero)))))