-
Notifications
You must be signed in to change notification settings - Fork 9
/
rust.rkt
4537 lines (3639 loc) · 137 KB
/
rust.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- coding: utf-8; -*-
;; Cheat sheet for unicode, using M-x set-input-method as TeX:
;; \alpha -> α
;; \beta -> β
;; \gamma -> γ
;; \cdot -> ·
;; \ell -> ℓ
;; \in -> ∈
;; \notin -> ∉
;; and so on
;; lifetime-≤
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; REDEX MODEL FOR RUST
;;
;; Current state:
;;
;; FIXME - Type system rules do not handle vectors well
;;
;; FIXME - Type system rules for match issue a loan for entire Option but
;; in Real Rust we have this downcast notion so we can issue a more
;; targeted loan; I don't think this makes any real difference in practice,
;; so long as we are limited to Option loans.
;;
;; FIXME - Consider just removing by-ref match and instead having an operator
;; to convert Option<T> to Option<&T>
;;
;; FIXME - check that local variables types are bounded by their lifetime
;;
;; FIXME - add in relationships between lifetime parameters that we can infer from types
#lang racket
(require redex
rackunit)
(define-language Patina
(prog (srs fns))
;; structures:
(srs (sr ...))
(sr (struct s ℓs (ty ...)))
;; lifetimes:
(ℓs (ℓ ...))
;; function def'ns
(fns (fn ...))
(fn (fun g ℓs vdecls bk))
;; blocks:
(bk (block ℓ vdecls sts))
;; variable decls
(vdecls [vdecl ...])
(vdecl (x ty))
;; statements:
[sts (st ...)]
(st (lv = rv)
(lv := rv)
(free lv) ;; shallow free
(drop lv) ;; deep free
(call g ℓs lvs)
(match lv (Some mode x => bk) (None => bk))
bk)
;; lvalues :
;; changing "field names" to be numbers
(lvs (lv ...))
(lv x ;; variable
(lv · f) ;; field projection
(lv @ lv) ;; indexing
(* lv)) ;; deref
;; rvalues :
(rv lv ;; use lvalue
(& ℓ mq lv) ;; take address of lvalue
(struct s ℓs (lv ...)) ;; struct constant
number ;; constant number
(lv + lv) ;; sum
(new lv) ;; allocate memory
(Some lv) ;; create an Option with Some
(None ty) ;; create an Option with None
(vec ty lv ...) ;; create a fixed-length vector
(vec-len lv) ;; extract length of a vector
(pack lv ty) ;; convert fixed-length to DST
)
(mode (ref ℓ mq) by-value)
;; types :
(tys (ty ...))
(ty (struct s ℓs) ;; s<'ℓ...>
(~ ty) ;; ~t
(& ℓ mq ty) ;; &'ℓ mq t
int
(Option ty)
(vec ty olen))
;; mq : mutability qualifier
(mq mut imm)
(mqs [mq ...])
;; variables
(x variable-not-otherwise-mentioned)
;; function names
(g variable-not-otherwise-mentioned)
;; structure names
(s variable-not-otherwise-mentioned)
;; labels
(ℓ variable-not-otherwise-mentioned)
;; field "names"
(f number)
(fs [f ...])
;; z -- sizes, offsets
[zs (z ...)]
[z number]
;; l -- vector lengths
[l number]
;; olen -- optional vector lengths
[olen number erased]
;; hack for debugging
(debug debug-me)
)
(check-not-false (redex-match Patina ty (term (vec int 3))))
;;;;
;;
;; EVALUATION
;;
;;;;
(define-extended-language Patina-machine Patina
;; a configuration of the machine
[C (prog H V T S)]
;; H (heap) : maps addresses to heap values
[H ((α hv) ...)]
;; hv (heap values)
[hv (ptr α) (int number) void]
;; V: maps variable names to addresses
[V (vmap ...)]
[vmap ((x α) ...)]
;; T : a map from names to types
[T (vdecls ...)]
;; θ : a substitution (from to)
[θ [(ℓ ℓ) ...]]
;; S (stack) : stack-frames contain pending statements
[S (sf ...)]
[sf (ℓ sts)]
[(αs βs γs) (number ...)]
[(α β γ) number]
)
;; unit test setup for helper fns
(define test-srs
(term [(struct A () (int))
(struct B (l0) (int (& l0 mut int)))
(struct C (l0) ((struct A ())
(struct B (l0))))
(struct D (l0) ((struct C (l0))
(struct A ())
(struct C (l0))
(struct B (l0))))
(struct E () [(~ int)])
]))
(check-not-false (redex-match Patina-machine srs test-srs))
(define test-T (term [[(i int)
(p (~ int))]
[(a (struct A ()))
(b (struct B (static)))
(c (struct C (static)))
(q (& b1 imm int))
(r (~ int))
(s (Option (~ int)))
(ints3 (vec int 3))
(i0 int)
(i1 int)
(i2 int)
(i3 int)
(ints3p (& b1 imm (vec int 3)))
(intsp (& b1 imm (vec int erased)))
]]))
(check-not-false (redex-match Patina-machine T test-T))
(define test-V (term (((i 10)
(p 11))
((a 12)
(b 13)
(c 15)
(q 18)
(r 19)
(s 20)
(ints3 22)
(i0 25)
(i1 26)
(i2 27)
(i3 28)
(ints3p 29)
(intsp 30)))))
(check-not-false (redex-match Patina-machine V test-V))
(define test-H (term [(10 (int 22)) ;; i == 22
(11 (ptr 99)) ;; p == 99
(12 (int 23)) ;; a:0
(13 (int 24)) ;; b:0
(14 (ptr 98)) ;; b:1
(15 (int 25)) ;; c:1:0
(16 (int 26)) ;; c:1:0
(17 (ptr 97)) ;; c:1:1
(18 (ptr 98)) ;; q
(19 (ptr 96)) ;; r
(20 (int 1)) ;; s – discriminant
(21 (ptr 95)) ;; s – payload
(22 (int 10)) ;; ints3 @ 0
(23 (int 11)) ;; ints3 @ 1
(24 (int 12)) ;; ints3 @ 2
(25 (int 0)) ;; i0
(26 (int 1)) ;; i1
(27 (int 2)) ;; i2
(28 (int 3)) ;; i3
(29 (ptr 22)) ;; ints3p
(30 (ptr 22)) ;; intsp ptr
(31 (int 3)) ;; intsp dst
(95 (int 31)) ;; *payload(s)
(96 (int 30)) ;; *c:1:1
(97 (int 29)) ;; *c:1:1
(98 (int 28)) ;; *b:1
(99 (int 27))])) ;; *p
(check-not-false (redex-match Patina-machine H test-H))
(define test-dst-srs
(term [(struct RCDataInt3 () [int (vec int 3)])
(struct RCInt3 (l0) [(& l0 imm (struct RCDataInt3 []))])
(struct RCDataIntN () (int (vec int erased)))
(struct RCIntN (l0) [(& l0 imm (struct RCDataIntN []))])
(struct Cycle1 () [(Option (~ (struct Cycle []))) (vec int erased)])
(struct Cycle2 () [(Option (~ (struct Cycle [])))])
]))
(check-not-false (redex-match Patina-machine srs test-dst-srs))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; simple test prog that assigns to the result pointer
(define twentytwo-main
(term (fun main [a] [(outp (& a mut int))]
(block l0 [] [((* outp) = 22)]))))
(check-not-false (redex-match Patina-machine fn twentytwo-main))
(define twentytwo-fns (term [,twentytwo-main]))
(check-not-false (redex-match Patina-machine fns twentytwo-fns))
(define twentytwo-prog
(term (,test-srs ,twentytwo-fns)))
(check-not-false (redex-match Patina-machine prog twentytwo-prog))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; test prog that creates a linked list and counts down
(define sum-srs
(term [(struct List () (int
(Option (~ (struct List [])))))]))
(check-not-false (redex-match Patina-machine srs sum-srs))
(define sum-main
(term (fun main [a] [(outp (& a mut int))]
(block l0
[(i int)
(n (Option (~ (struct List []))))
(s (struct List []))
(l (~ (struct List [])))]
[(i = 22)
(n = (None (~ (struct List []))))
(s = (struct List [] (i n)))
(l = (new s))
(i := 44)
(n = (Some l))
(s = (struct List [] (i n)))
(l = (new s))
(block l1
[(p (& l1 imm (struct List [])))]
[(p = (& l1 imm (* l)))
(call sum-list [l1 a] [p outp])
])
(drop l)
]))))
(check-not-false (redex-match Patina-machine fn sum-main))
;; fn sum-list<'a,'b>(inp: &'a List, outp: &'b mut int) {
;; let r: int = inp.0;
;; match inp.1 {
;; Some(ref next1) => { // next1: &~List
;; let next2 = &**next1;
;; let b = 0;
;; {
;; let c = &mut b;
;; sum-list(next2, c);
;; }
;; *outp = r + b;
;; }
;; None => {
;; *outp = r + b;
;; }
;; }
;; }
(define sum-sum-list
(term (fun sum-list [a b] [(inp (& a imm (struct List [])))
(outp (& b mut int))]
(block l0
[(r int)]
[(r = ((* inp) · 0))
(match ((* inp) · 1)
(Some (ref l0 imm) next1 =>
(block l1
[(next2 (& l1 imm (struct List [])))
(b int)]
[(next2 = (& l1 imm (* (* next1))))
(b = 0)
(block l3
[(c (& l3 mut int))]
[(c = (& l3 mut b))
(call sum-list [l1 l3] [next2 c])])
((* outp) := (r + b))]))
(None =>
(block l1
[]
[((* outp) := r)])))]))))
(check-not-false (redex-match Patina-machine fn sum-sum-list))
(define sum-fns
(term (,sum-main ,sum-sum-list)))
(define sum-prog
(term (,sum-srs ,sum-fns)))
(check-not-false (redex-match Patina-machine prog sum-prog))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; test prog that creates an RC vector, casts it to DST, and then
;; accesses it
(define dst-srs
(term [(struct RCDataInt3 () [int (vec int 3)])
(struct RCInt3 (l0) [(& l0 imm (struct RCDataInt3 []))])
(struct RCDataIntN () (int (vec int erased)))
(struct RCIntN (l0) [(& l0 imm (struct RCDataIntN []))])
]))
;; gonna be super tedious...
(define dst-main
(term (fun main [a] [(outp (& a mut int))]
(block l0 [(i1 int)
(i2 int)
(i3 int)
(v (vec int 3))
(rd3 (struct RCDataInt3 []))
(rd3p (& l0 imm (struct RCDataInt3 [])))
(rdNp (& l0 imm (struct RCDataIntN [])))
]
[(i1 = 22)
(i2 = 23)
(i3 = 24)
(v = (vec int i1 i2 i3))
(i1 = 1)
(rd3 = (struct RCDataInt3 [] (i1 v)))
(rd3p = (& l0 imm rd3))
(rdNp = (pack rd3p (& l0 imm (struct RCDataIntN []))))
(i1 = 0)
(i2 = 0)
(i2 = ((((* rdNp) · 1) @ i1) + i2))
(i1 = 1)
(i2 = ((((* rdNp) · 1) @ i1) + i2))
(i1 = 2)
(i2 = ((((* rdNp) · 1) @ i1) + i2))
((* outp) = i2)
]))))
(check-not-false (redex-match Patina-machine fn dst-main))
(define dst-prog
(term (,dst-srs [,dst-main])))
(check-not-false (redex-match Patina-machine prog dst-prog))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initial -- the initial state of the machine
(define initial-H (term [(0 (int 0)) ;; static value for result code
(1 (ptr 0))])) ;; pointer to result code
(check-not-false (redex-match Patina-machine H initial-H))
(define initial-V (term [[(resultp 1)]]))
(check-not-false (redex-match Patina-machine V initial-V))
(define initial-T (term [[(resultp (& l0 mut int))]]))
(check-not-false (redex-match Patina-machine T initial-T))
(define initial-S (term [(l0 [(call main (l0) (resultp))])]))
(check-not-false (redex-match Patina-machine S initial-S))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ∈, ∉ -- a metafunction like member
(define-metafunction Patina-machine
∈ : any [any ...] -> any
[(∈ any_0 [any_1 ...])
,(not (not (member (term any_0) (term [any_1 ...]))))])
(define-metafunction Patina-machine
∉ : any [any ...] -> any
[(∉ any_0 [any_1 ...])
,(not (member (term any_0) (term [any_1 ...])))])
(test-equal (term (∈ 1 [1 2 3])) (term #t))
(test-equal (term (∈ 4 [1 2 3])) (term #f))
(test-equal (term (∉ 1 [1 2 3])) (term #f))
(test-equal (term (∉ 4 [1 2 3])) (term #t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; common logic functions in term form
(define-metafunction Patina-machine
¬ : boolean -> boolean
[(¬ #t) #f]
[(¬ #f) #t])
(define-metafunction Patina-machine
∨ : boolean boolean -> boolean
[(∨ #f #f) #f]
[(∨ _ _) #t])
(define-metafunction Patina-machine
∧ : boolean boolean -> boolean
[(∧ #t #t) #t]
[(∧ _ _) #f])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; if-true list bools -- like filter but takes a list of booleans
(define-metafunction Patina-machine
if-true : [any ...] [boolean ...] -> [any ...]
[(if-true any_0 any_1)
,(for/list ([x (term any_0)] [t (term any_1)] #:when t) x)])
(test-equal (term (if-true [1 2 3 4 5] [#f #t #f #t #f]))
(term [2 4]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ∀, ∃ -- useful functions for operating over vectors of booleans.
;; Particularly useful in combination with macros and maps.
(define-metafunction Patina-machine
∀ : [any ...] -> boolean
[(∀ [_ ... #f _ ...]) #f]
[(∀ _) #t])
(define-metafunction Patina-machine
∃ : [any ...] -> boolean
[(∃ [#f ...]) #f]
[(∃ _) #t])
(define-metafunction Patina-machine
∄ : [any ...] -> boolean
[(∄ any) (¬ (∃ any))])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ∪ -- a metafunction for set union
(define-metafunction Patina-machine
∪ : [any ...] [any ...] -> [any ...]
[(∪ [any_0 any_1 ...] [any_2 ...])
(∪ [any_1 ...] [any_2 ...])
(side-condition (term (∈ any_0 [any_2 ...])))]
[(∪ [any_0 any_1 ...] [any_2 ...])
(∪ [any_1 ...] [any_0 any_2 ...])]
[(∪ [] [any_1 ...])
[any_1 ...]]
)
(test-equal (term (∪ [1 4] [1 2 3])) (term [4 1 2 3]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ⊆ -- a metafunction for subseteq comparison
(define-metafunction Patina-machine
⊆ : [any ...] [any ...] -> boolean
[(⊆ [] [any ...]) #t]
[(⊆ [any_0 any_1 ...] [any_2 ...])
(and (∀ [∃ any_0 [any_2 ...]])
(⊆ [any_1 ...] [any_2 ...]))]
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ⊎ -- a metafunction for disjoint set union
(define-metafunction Patina-machine
⊎ : [any ...] [any ...] -> [any ...]
[(⊎ [any_0 ...] [any_1 ...])
([any_0 ...] [any_1 ...])]
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; \\ -- a metafunction for set difference
(define-metafunction Patina-machine
\\ : [any ...] [any ...] -> [any ...]
[(\\ any_0 any_1)
,(remove* (term any_1) (term any_0))])
(test-equal (term (\\ [1 2 3] [2])) (term [1 3]))
(test-equal (term (\\ [1 2 3] [4])) (term [1 2 3]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; has -- a metafunction like assoc that works on lists like '((k v) (k1 v1))
(define-metafunction Patina-machine
has : any [(any any) ...] -> any
[(has any_k {_ ... [any_k _] _ ...}) #t]
[(has _ _) #f])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; get -- a metafunction like assoc that works on lists like '((k v) (k1 v1))
(define-metafunction Patina-machine
get : any [(any any) ...] -> any
[(get any {_ ... [any any_v] _ ...}) any_v])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; get* -- search through multiple assoc lists
(define (get* key lists)
(for*/first ([l lists]
[p (in-value (assoc key l))]
#:when p)
(second p)))
(test-equal (get* (term a) (term (((a 1) (b 2)) ((c 3)))))
1)
(test-equal (get* (term c) (term (((a 1) (b 2)) ((c 3)))))
3)
(test-equal (get* (term e) (term (((a 1) (b 2)) ((c 3)) ((d 4) (e 5)))))
5)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; sort-heap -- sort heap address in ascending order
(define (sort-heap heap)
(sort heap (λ (pair1 pair2) (< (car pair1)
(car pair2)))))
;; useful heap predicates
(define (in-range addr base size)
(and (>= addr base)
(< addr (+ base size))))
(define (select H α z)
(let* [(matching (filter (λ (pair) (in-range (car pair) α z)) H))
(sorted (sort-heap matching))
(values (map cadr sorted))]
values))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; reject – useful for debugging since it causes contract errors
(define-metafunction Patina-machine
reject : debug -> number
[(reject debug) 0])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; sum
(define-metafunction Patina-machine
sum : zs -> z
[(sum any) ,(apply + (term any))])
(test-equal (term (sum [1 2 3]))
(term 6))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; len
(define-metafunction Patina-machine
len : [any ...] -> number
[(len any) ,(length (term any))])
(test-equal (term (len [1 2 3]))
(term 3))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; prefix sum
(define-metafunction Patina-machine
prefix-sum : z zs -> zs
[(prefix-sum z_a ()) ()]
[(prefix-sum z_a (z_b z_c ...))
[z_d z_e ...]
(where z_d (sum [z_a z_b]))
(where [z_e ...] (prefix-sum z_d [z_c ...]))]
)
(test-equal (term (prefix-sum 0 ()))
(term ()))
(test-equal (term (prefix-sum 0 (1 2 3)))
(term (1 3 6)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; offset, inc, and dec
(define-metafunction Patina-machine
offset : α z -> α
[(offset α z) ,(+ (term α) (term z))])
(define-metafunction Patina-machine
inc : α -> α
[(inc z) ,(add1 (term z))])
(define-metafunction Patina-machine
dec : α -> α
[(dec z) ,(sub1 (term z))])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; deref function -- search a heap for a given address.
(define-metafunction Patina-machine
deref : H α -> hv
[(deref H α)
(get α H)])
(test-equal (term (deref [(1 (ptr 22))] 1)) (term (ptr 22)))
(test-equal (term (deref [(2 (ptr 23)) (1 (int 22))] 1)) (term (int 22)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; fun-defn --
(define-metafunction Patina-machine
fun-defn : fns g -> fn
[(fun-defn (fn_0 fn_1 ...) g)
fn_0
(where (fun g ℓs ((x ty) ...) bk) fn_0)]
[(fun-defn (fn_0 fn_1 ...) g)
(fun-defn (fn_1 ...) g)])
(test-equal (term (fun-defn ,twentytwo-fns main))
(term ,twentytwo-main))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; update -- replaces value for α
(define-metafunction Patina-machine
update : H α hv -> H
[(update ((α_0 hv_0) (α_1 hv_1) ...) α_0 hv_2)
((α_0 hv_2) (α_1 hv_1) ...)]
[(update ((α_0 hv_0) (α_1 hv_1) ...) α_2 hv_2)
,(append (term ((α_0 hv_0))) (term (update ((α_1 hv_1) ...) α_2 hv_2)))])
(test-equal (term (update [(2 (ptr 23)) (1 (int 22))] 1 (int 23)))
(term ((2 (ptr 23)) (1 (int 23)))))
(test-equal (term (update [(2 (ptr 23)) (1 (int 22))] 2 (int 23)))
(term ((2 (int 23)) (1 (int 22)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; extend -- grows a heap with z contiguous new addresses
(define-metafunction Patina-machine
extend : H α z -> H
[(extend H α 0) H]
[(extend ((β hv) ...) α z)
(extend ((α void) (β hv) ...)
,(add1 (term α))
,(sub1 (term z)))])
(test-equal (term (extend [(10 (ptr 1))
(11 (int 2))
(12 (ptr 3))]
13
3))
(term [(15 void)
(14 void)
(13 void)
(10 (ptr 1))
(11 (int 2))
(12 (ptr 3))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; shrink -- removes z contiguous addresses from domain of heap
(define-metafunction Patina-machine
shrink : H α z -> H
[(shrink H α z)
,(filter (λ (pair) (not (in-range (car pair) (term α) (term z))))
(term H))])
(test-equal (term (shrink [(10 (ptr 1))
(11 (int 2))
(12 (ptr 3))
(13 (ptr 4))
(14 (ptr 5))]
11
3))
(term [(10 (ptr 1))
(14 (ptr 5))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; subst-ℓ
(define-metafunction Patina-machine
subst-ℓ : θ ℓ -> ℓ
[(subst-ℓ θ static) static]
[(subst-ℓ θ ℓ) (get ℓ θ) (side-condition (term (has ℓ θ)))]
[(subst-ℓ θ ℓ) ℓ (side-condition (term (¬ (has ℓ θ))))]
)
(test-equal (term (subst-ℓ [] static)) (term static))
(test-equal (term (subst-ℓ [(a b)] a)) (term b))
(test-equal (term (subst-ℓ [(a b)] c)) (term c))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; subst-ty
(define-metafunction Patina-machine
subst-ty : θ ty -> ty
[(subst-ty θ (struct s [ℓ ...]))
(struct s [(subst-ℓ θ ℓ) ...])]
[(subst-ty θ (~ ty))
(~ (subst-ty θ ty))]
[(subst-ty θ (& ℓ mq ty))
(& (subst-ℓ θ ℓ) mq (subst-ty θ ty))]
[(subst-ty θ int)
int]
[(subst-ty θ (Option ty))
(Option (subst-ty θ ty))]
[(subst-ty θ (vec ty olen))
(vec (subst-ty θ ty) olen)]
)
(test-equal (term (subst-ty [(a b) (b c)] (struct s [a b])))
(term (struct s [b c])))
(test-equal (term (subst-ty [(a b) (b c)] (~ (struct s [a b]))))
(term (~ (struct s [b c]))))
(test-equal (term (subst-ty [(a b) (b c)] (& a mut (struct s [a b]))))
(term (& b mut (struct s [b c]))))
(test-equal (term (subst-ty [(a b) (b c)] int))
(term int))
(test-equal (term (subst-ty [(a b) (b c)] (Option (& a mut int))))
(term (Option (& b mut int))))
(test-equal (term (subst-ty [(a b) (b c)] (vec (& a mut int) erased)))
(term (vec (& b mut int) erased)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; subst-vdecl(s)
(define-metafunction Patina-machine
subst-vdecl : θ vdecl -> vdecl
[(subst-vdecl θ (x ty)) (x (subst-ty θ ty))])
(define-metafunction Patina-machine
subst-vdecls : θ vdecls -> vdecls
[(subst-vdecls θ (vdecl ...)) ((subst-vdecl θ vdecl) ...)])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; subst-rv
(define-metafunction Patina-machine
subst-rv : θ rv -> rv
[(subst-rv θ (& ℓ mq lv))
(& (subst-ℓ θ ℓ) mq lv)]
[(subst-rv θ (struct s (ℓ ...) (lv ...)))
(struct s ((subst-ℓ θ ℓ) ...) (lv ...))]
[(subst-rv θ (None ty))
(None (subst-ty θ ty))]
[(subst-rv θ (vec ty lv ...))
(vec (subst-ty θ ty) lv ...)]
[(subst-rv θ (pack lv ty))
(pack lv (subst-ty θ ty))]
[(subst-rv _ any) any])
(test-equal (term (subst-rv ((a b) (b c)) (& a imm x)))
(term (& b imm x)))
(test-equal (term (subst-rv ((a b) (b c)) (struct s (a b) (x y))))
(term (struct s (b c) (x y))))
(test-equal (term (subst-rv ((a b) (b c)) (None (& a imm int))))
(term (None (& b imm int))))
(test-equal (term (subst-rv ((a b) (b c)) (vec (& a imm int) x y z)))
(term (vec (& b imm int) x y z)))
(test-equal (term (subst-rv ((a b) (b c)) (pack x (& a imm int))))
(term (pack x (& b imm int))))
(test-equal (term (subst-rv ((a b) (b c)) (x + y)))
(term (x + y)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; subst-mode / subst-st / subst-bk
(define-metafunction Patina-machine
subst-mode : θ mode -> mode
[(subst-mode θ by-value) by-value]
[(subst-mode θ (ref ℓ mq)) (ref (subst-ℓ θ ℓ) mq)])
(define-metafunction Patina-machine
subst-st : θ st -> st
[(subst-st θ (lv = rv))
(lv = (subst-rv θ rv))]
[(subst-st θ (lv := rv))
(lv := (subst-rv θ rv))]
[(subst-st θ (call g (ℓ ...) lvs))
(call g ((subst-ℓ θ ℓ) ...) lvs)]
[(subst-st θ (match lv (Some mode x => bk_1) (None => bk_2)))
(match lv (Some (subst-mode θ mode) x => (subst-bk θ bk_1)) (None => (subst-bk θ bk_2)))]
[(subst-st θ bk)
(subst-bk θ bk)]
[(subst-st _ any) any])
(define-metafunction Patina-machine
subst-bk : θ bk -> bk
[(subst-bk θ_0 (block ℓ_0 ((x ty) ...) (st ...)))
(block ℓ_1 ((x (subst-ty θ_2 ty)) ...) ((subst-st θ_2 st) ...))
; ℓ_0 is being rebound, so remove the now irrelevant old substutitions for ℓ_0
(where θ_1 (if-true θ_0 ,(map (λ (kv) (not (eq? (term ℓ_0) (car kv)))) (term θ_0))))
; ℓ_1 is a capture-avoiding (deterministic) replacement for ℓ_0 (will be ℓ_0 if it's already fine)
(where ℓ_1 ,(variable-not-in (term θ_1) (term ℓ_0)))
; add the substitution [ℓ_0 ↦ ℓ_1] to θ_1
(where θ_2 (∪ θ_1 ((ℓ_0 ℓ_1))))
]
)
(test-equal (term (subst-st ((a b) (b c)) (x = (& a imm y))))
(term (x = (& b imm y))))
(test-equal (term (subst-st ((a b) (b c)) (call g (b a) (x y z))))
(term (call g (c b) (x y z))))
(test-equal
(term (subst-st ((a b) (b c)) (block d ((x (& d imm int))) ((y = (vec (& a imm int) x))))))
(term (block d ((x (& d imm int))) ((y = (vec (& b imm int) x)))))
)
(test-equal
(term (subst-st ((a a) (b c)) (block a ((x (& a imm int))) ((y = (vec (& b imm int) x))))))
(term (block a ((x (& a imm int))) ((y = (vec (& c imm int) x))))))
(test-equal
(term (subst-st ((a b)) (block c ((x (& d imm int))) ((y = (vec (& a imm int) x))))))
(term (block c ((x (& d imm int))) ((y = (vec (& b imm int) x))))))
; avoid capture
(test-equal
(term (subst-st ((a b) (b c))
(block a ((x (& a imm int))) ((y = (vec (& b imm int) x))))))
(term (block a ((x (& a imm int))) ((y = (vec (& c imm int) x))))))
; a case where we actually need to use a different name
(test-equal
(term (subst-st ((a b) (b c))
(block c ((x (& a imm int)) (y (& b imm int))) ((y = (vec (& c imm int) x))))))
(term (block c1 ((x (& b imm int)) (y (& c imm int))) ((y = (vec (& c1 imm int) x))))))
(test-equal
(term (subst-st ((l1 ℓfresh))
(block lll ((p (& l1 imm (struct List ()))))
((p = (& l1 imm (* l)))
(call sum-list (l1 a) (p outp))))))
(term (block lll ((p (& ℓfresh imm (struct List ()))))
((p = (& ℓfresh imm (* l)))
(call sum-list (ℓfresh a) (p outp))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; field-tys
(define-metafunction Patina-machine
field-tys : srs s ℓs -> (ty ...)
[(field-tys ((struct s_0 (ℓ_0 ...) (ty_0 ...)) sr ...) s_0 [ℓ_1 ...])
((subst-ty θ ty_0) ...)
(where θ [(ℓ_0 ℓ_1) ...])]
[(field-tys ((struct s_0 (ℓ_0 ...) (ty_0 ...)) sr ...) s_1 ℓs_1)
(field-tys (sr ...) s_1 ℓs_1)])
(test-equal (term (field-tys ,test-srs A ()))
(term (int)))
(test-equal (term (field-tys ,test-srs D (static)))
(term ((struct C (static)) (struct A ()) (struct C (static)) (struct B (static)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; is-DST
(define-metafunction Patina-machine
is-DST : srs ty -> boolean
[(is-DST srs ty) (is-DST-1 [] srs ty)])
(define-metafunction Patina-machine
is-DST-1 : [s ...] srs ty -> boolean
[(is-DST-1 [s_a ...] srs (struct s ℓs))
#false
(side-condition (member (term s) (term [s_a ...])))]
[(is-DST-1 [s_a ...] srs (struct s ℓs))
(is-DST-1 [s s_a ...] srs ty_z)
(where (ty_a ... ty_z) (field-tys srs s ℓs))]
[(is-DST-1 [s ...] srs (~ ty)) #false]
[(is-DST-1 [s ...] srs (& ℓ mq ty)) #false]
[(is-DST-1 [s ...] srs int) #false]
[(is-DST-1 [s ...] srs (Option ty)) (is-DST-1 [s ...] srs ty)]
[(is-DST-1 [s ...] srs (vec ty erased)) #true]
[(is-DST-1 [s ...] srs (vec ty l)) #false])
(test-equal (term (is-DST ,test-dst-srs (~ (vec int erased))))
#false)
(test-equal (term (is-DST ,test-dst-srs (vec int erased)))
#true)
(test-equal (term (is-DST ,test-dst-srs (struct RCDataInt3 [])))
#false)
(test-equal (term (is-DST ,test-dst-srs (struct RCInt3 [a])))
#false)
(test-equal (term (is-DST ,test-dst-srs (struct RCDataIntN [])))
#true)
(test-equal (term (is-DST ,test-dst-srs (struct RCIntN [a])))
#false)
(test-equal (term (is-DST ,test-dst-srs (struct Cycle1 [])))
#true)
(test-equal (term (is-DST ,test-dst-srs (struct Cycle2 [])))
#false)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; sizeof
(define-metafunction Patina-machine
sizeof : srs ty -> z
[(sizeof srs int)
1]
[(sizeof srs (~ ty))
1
(side-condition (not (term (is-DST srs ty))))]
[(sizeof srs (~ ty))
2
(side-condition (term (is-DST srs ty)))]