forked from LuKuangChen/stacker-on-racket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatypes.rkt
420 lines (408 loc) · 19 KB
/
datatypes.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#lang plait
(require "io.rkt")
(require "error.rkt")
(require "utilities.rkt")
(require (typed-in "show.rkt" [string-of-o : (Obs -> String)]))
(require (typed-in racket
[random : (Number -> Number)]
[list->vector : ((Listof 'a) -> (Vectorof 'a))]
[vector->list : ((Vectorof 'a) -> (Listof 'a))]
[vector-map : (('a -> 'b) (Vectorof 'a) -> (Vectorof 'b))]
[remove-duplicates : ((Listof 'a) -> (Listof 'a))]))
(define-type-alias CompiledProgram ((Listof (Id * Term)) * (Listof Term)))
(define-type Term
(t-quote [v : Val])
(t-var [x : Id])
(t-fun [name : (Optionof Symbol)]
[arg* : (Listof Id)]
[def* : (Listof (Id * Term))]
[body : Term])
(t-app [fun : Term] [arg* : (Listof Term)])
(t-let [bind* : (Listof (Id * Term))] [body : Term])
(t-letrec [bind* : (Listof (Id * Term))] [body : Term])
(t-set! [var : Id] [val : Term])
(t-begin [prelude* : (Listof Term)] [result : Term])
(t-if [cnd : Term] [thn : Term] [els : Term])
(t-cond [cnd-thn* : (Listof (Term * Term))] [els : (Optionof Term)]))
(define (t-or t1 t2)
(t-if t1
(t-quote (v-bool #t))
t2))
(define (t-and t1 t2)
(t-if t1
t2
(t-quote (v-bool #f))))
(define-type-alias (Result 'a) 'a)
(define-type Heap
(heap-heap [it : (Hashof HeapAddress (Number * HeapValue))]))
(define (empty-heap) : Heap
(heap-heap (hash (list))))
(define the-heap-size 1000)
(define (find-heap-addr the-heap [base : Number]) : Number
(let ([propose (+ base (random the-heap-size))])
(type-case (Optionof (Number * HeapValue)) (hash-ref the-heap (ha-user propose))
[(none) propose]
[(some hv) (find-heap-addr the-heap base)])))
(define (base-addr [hv : HeapValue])
(type-case HeapValue hv
[(h-env env map) the-heap-size]
[else 0]))
(define (allocate! [a-heap : Heap] [hv : HeapValue]) : (Heap * HeapAddress)
(let* ([a-heap (heap-heap-it a-heap)]
[addr (ha-user (find-heap-addr a-heap (base-addr hv)))]
[a-heap (hash-set a-heap addr (pair (length (hash-keys a-heap)) hv))])
(values (heap-heap a-heap) addr)))
(define (heap-ref a-heap a-heap-address) : HeapValue
(type-case (Optionof (Number * HeapValue)) (hash-ref (heap-heap-it a-heap) a-heap-address)
[(none)
(raise (exn-internal 'heap-ref (format "Invalid address ~a" a-heap-address)))]
[(some hv)
(snd hv)]))
(define (heap-set [h : Heap] [ha : HeapAddress] [hv : HeapValue]) : Heap
(let* ([h (heap-heap-it h)]
[timestamp
(type-case (Optionof (Number * HeapValue)) (hash-ref h ha)
[(none) (length (hash-keys h))]
[(some v) (fst v)])])
(heap-heap (hash-set h ha (pair timestamp hv)))))
(define-type HeapAddress
(ha-prim [it : PrimitiveHeapAddress])
(ha-user [it : Number]))
(define-type PrimitiveHeapAddress
(pa-empty)
(pa-map)
(pa-filter)
(pa-memberp)
(pa-foldl)
(pa-foldr)
(pa-andmap)
(pa-ormap)
(pa-base-env))
(define-type HeapValue
(h-vec [it : (Vectorof Val)])
(h-cons [it : (Val * Val)])
(h-fun [env : Env] [name : (Optionof Symbol)] [arg* : (Listof Id)] [def* : (Listof (Id * Term))] [body : Term])
(h-env [parent : Env] [map : (Hashof Id (Optionof Val))]))
(define-type Val
(v-addr [it : HeapAddress])
(v-prim [name : PrimitiveOp])
(v-str [it : String])
(v-num [it : Number])
(v-char [it : Char])
(v-bool [it : Boolean])
(v-empty)
(v-void))
(define (v-fun [the-heap : Heap] [env : Env] name arg* def* body)
(let-values (((the-heap addr) (allocate! the-heap (h-fun env name arg* def* body))))
(values the-heap (v-addr addr))))
(define (v-vec [the-heap : Heap] it)
(let-values (((the-heap addr) (allocate! the-heap (h-vec it))))
(values the-heap (v-addr addr))))
(define (v-cons [the-heap : Heap] it)
(let-values (((the-heap addr) (allocate! the-heap (h-cons it))))
(values the-heap (v-addr addr))))
(define (v-list [the-heap : Heap] it)
(if (empty? it)
(values the-heap (v-empty))
(let-values (((the-heap v) (v-list the-heap (rest it))))
(let-values (((the-heap addr) (allocate! the-heap (h-cons (pair (first it) v)))))
(values the-heap (v-addr addr))))))
(define-type PrimitiveOp
(po-not)
(po-left)
(po-right)
(po-vlen)
(po-check-expect)
(po-string-length)
(po-string-append)
(po-string->list)
(po-list->string)
(po-eqp)
(po-equalp)
(po-zerop)
(po-+)
(po--)
(po-*)
(po-/)
(po-<)
(po->)
(po-<=)
(po->=)
(po-=)
(po-emptyp)
(po-pairp)
(po-mpair)
(po-set-left!)
(po-set-right!)
(po-vref)
(po-consp)
(po-cons)
(po-sub1)
(po-add1)
(po-vset!)
(po-mvec)
(po-first)
(po-rest)
(po-list))
(define-type-alias Env (Optionof HeapAddress))
(define (env-declare the-heap [env : Env] x*)
(env-extend/declare the-heap env (map (lambda (x) (values x (none))) x*)))
(define (env-extend the-heap [env : Env] [x&v* : (Listof (Id * Val))])
(env-extend/declare the-heap env (map2 pair (map fst x&v*) (map some (map snd x&v*)))))
(define (env-extend/declare the-heap [env : Env] [x&v* : (Listof (Id * (Optionof Val)))]): (Heap * Env)
(let ((x* (map fst x&v*)))
(if (no-duplicates x*)
(let-values (((the-heap addr) (allocate! the-heap (h-env env (hash x&v*)))))
(values the-heap (some addr)))
(raise (exn-rt "redeclare")))))
(define (no-duplicates x*)
(= (length x*)
(length (remove-duplicates x*))))
(define (env-set the-heap [env : Env] x v)
(let ((addr (some-v env)))
(type-case HeapValue (heap-ref the-heap addr)
((h-env env map)
(type-case (Optionof 'a) (hash-ref map x)
((none)
(env-set the-heap env x v))
((some _)
(heap-set
the-heap
addr
(h-env env (hash-set map x (some v))) ))))
(else
(raise (exn-internal 'env-set "This is impossible. The address is not an env."))))))
(define (env-lookup the-heap [env : Env] x)
(type-case (Optionof HeapAddress) env
((none)
(none))
((some addr)
(env-lookup-1 the-heap addr x))))
(define (env-lookup-1 the-heap addr x)
(type-case HeapValue (heap-ref the-heap addr)
((h-env env map)
(type-case
(Optionof (Optionof Val))
(hash-ref map x)
((none) (env-lookup the-heap env x))
((some v)
(type-case
(Optionof Val)
v
((none) (raise (exn-rt (format "use-before-set ~a" x))))
((some v) (some v))))))
(else
(raise (exn-internal 'env-lookup "Not an env.")))))
(define-type ECFrame
(F-begin [e* : (Listof Term)] [e : Term])
(F-app [v* : (Listof Val)] [e* : (Listof Term)])
(F-let [xv* : (Listof (Id * Val))] [x : Id] [xe* : (Listof (Id * Term))] [body : Term])
(F-if [thn : Term] [els : Term])
(F-set! [var : Id])
(P-def [x : Id] [d* : (Listof (Id * Term))] [e* : (Listof Term)])
(P-exp [v* : (Listof Val)] [e* : (Listof Term)]))
(define-type-alias ECtx (Listof ECFrame))
(define-type CtxAnn
(ca-app [v : Val] [v* : (Listof Val)])
(ca-let)
(ca-letrec))
(define-type-alias Ctx (Env * ECtx * CtxAnn))
(define-type-alias Stack (Listof Ctx))
(define (uninteresting-variable? x)
(member x builtins))
(define-values (base-heap base-env builtins)
(let* ((the-heap (empty-heap))
[x&v* (ind-List
(list
(values 'string->list (po-string->list))
(values 'first (po-first))
(values 'rest (po-rest))
(values 'not (po-not))
(values 'left (po-left))
(values 'string-length (po-string-length))
(values 'string-append (po-string-append))
(values 'string->list (po-string->list))
(values 'list->string (po-list->string))
(values 'right (po-right))
(values 'vlen (po-vlen))
(values 'check-expect (po-check-expect))
(values 'equal? (po-equalp))
(values 'eq? (po-eqp))
(values 'zero? (po-zerop))
(values 'sub1 (po-sub1))
(values 'add1 (po-add1))
(values '+ (po-+))
(values '- (po--))
(values '* (po-*))
(values '/ (po-/))
(values '< (po-<))
(values '> (po->))
(values '<= (po-<=))
(values '>= (po->=))
(values '= (po-=))
(values 'empty? (po-emptyp))
(values 'pair? (po-pairp))
(values 'mpair (po-mpair))
(values 'set-left! (po-set-left!))
(values 'set-right! (po-set-right!))
(values 'vref (po-vref))
(values 'cons (po-cons))
(values 'cons? (po-consp))
(values 'vset! (po-vset!))
(values 'mvec (po-mvec))
(values 'list (po-list)))
(list)
(λ (IH e)
(cons (values (fst e)
(v-prim (snd e)))
IH)))]
[base-env-map (hash (append
(map2 pair
(map fst x&v*)
(map some (map snd x&v*)))
(list (pair 'empty (some (v-empty)))
(pair 'map (some (v-addr (ha-prim (pa-map)))))
(pair 'filter (some (v-addr (ha-prim (pa-filter)))))
(pair 'member? (some (v-addr (ha-prim (pa-memberp)))))
(pair 'foldl (some (v-addr (ha-prim (pa-foldl)))))
(pair 'foldr (some (v-addr (ha-prim (pa-foldr)))))
(pair 'andmap (some (v-addr (ha-prim (pa-andmap)))))
(pair 'ormap (some (v-addr (ha-prim (pa-ormap)))))
(pair 'false (some (v-bool #f)))
(pair 'true (some (v-bool #t))))))]
[builtins (hash-keys base-env-map)]
[addr (ha-prim (pa-base-env))]
[hv (h-env (none) base-env-map)]
[the-heap (heap-set the-heap addr hv)]
[base-env (some addr)]
(the-heap (heap-set the-heap (ha-prim (pa-map))
(h-fun base-env
(some 'map)
(list 'f 'xs)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'xs) (t-var 'empty)))
(t-var 'empty)
(t-app (t-var 'cons)
(list (t-app (t-var 'f)
(list (t-app (t-quote (v-prim (po-first)))
(list (t-var 'xs)))))
(t-app (t-var 'map)
(list (t-var 'f)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'xs)))))))))))
(the-heap (heap-set the-heap (ha-prim (pa-memberp))
(h-fun base-env
(some 'member?)
(list 'x 'l)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'l) (t-var 'empty)))
(t-quote (v-bool #f))
(t-or (t-app (t-var 'equal?)
(list (t-app (t-quote (v-prim (po-first)))
(list (t-var 'l)))
(t-var 'x)))
(t-app (t-var 'member?)
(list (t-var 'x)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'l))))))))))
(the-heap (heap-set the-heap (ha-prim (pa-filter))
(h-fun base-env
(some 'filter)
(list 'f 'xs)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'xs) (t-var 'empty)))
(t-var 'empty)
(t-if (t-app (t-var 'f)
(list (t-app (t-quote (v-prim (po-first)))
(list (t-var 'xs)))))
(t-app (t-var 'cons)
(list (t-app (t-quote (v-prim (po-first)))
(list (t-var 'xs)))
(t-app (t-var 'filter)
(list (t-var 'f)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'xs)))))))
(t-app (t-var 'filter)
(list (t-var 'f)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'xs))))))))))
(the-heap (heap-set the-heap (ha-prim (pa-foldl))
(h-fun base-env
(some 'foldl)
(list 'f 'base 'l)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'l) (t-var 'empty)))
(t-var 'base)
(t-app (t-var 'foldl)
(list
(t-var 'f)
(t-app (t-var 'f)
(list
(t-app (t-quote (v-prim (po-first)))
(list (t-var 'l)))
(t-var 'base)))
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'l)))))))))
(the-heap (heap-set the-heap (ha-prim (pa-foldr))
(h-fun base-env
(some 'foldr)
(list 'f 'base 'l)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'l) (t-var 'empty)))
(t-var 'base)
(t-app (t-var 'f)
(list
(t-app (t-quote (v-prim (po-first)))
(list (t-var 'l)))
(t-app (t-var 'foldr)
(list
(t-var 'f)
(t-var 'base)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'l)))))))))))
(the-heap (heap-set the-heap (ha-prim (pa-andmap))
(h-fun base-env
(some 'andmap)
(list 'p? 'l)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'l) (t-var 'empty)))
(t-quote (v-bool #t))
(t-and (t-app (t-var 'p?)
(list
(t-app (t-quote (v-prim (po-first)))
(list (t-var 'l)))))
(t-app (t-var 'andmap)
(list
(t-var 'p?)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'l))))))))))
(the-heap (heap-set the-heap (ha-prim (pa-ormap))
(h-fun base-env
(some 'andmap)
(list 'p? 'l)
(list)
(t-if (t-app (t-var 'equal?) (list (t-var 'l) (t-var 'empty)))
(t-quote (v-bool #t))
(t-or (t-app (t-var 'p?)
(list
(t-app (t-quote (v-prim (po-first)))
(list (t-var 'l)))))
(t-app (t-var 'andmap)
(list
(t-var 'p?)
(t-app (t-quote (v-prim (po-rest)))
(list (t-var 'l))))))))))
)
(values the-heap base-env builtins)))
(define-type Operator
(op-prim [name : PrimitiveOp])
(op-fun [env : Env] [arg* : (Listof Id)] [def* : (Listof (Id * Term))] [body : Term]))
(define-type OtherState
(calling [fun : Val] [args* : (Listof Val)] [env : Env] [ectx : ECtx] [stack : Stack]
[clos-env : Env] [arg* : (Listof Id)] [def* : (Listof (Id * Term))] [body : Term])
(called [e : Term] [env : Env] [stack : Stack])
(returning [v : Val] [stack : Stack])
(returned [v : Val] [env : Env] [ectx : ECtx] [stack : Stack])
(terminated [v* : (Listof Val)])
(errored))
(define-type-alias State (Heap * OtherState))