-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcogen-eq-flow.scm
1407 lines (1334 loc) · 45 KB
/
cogen-eq-flow.scm
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
;;; cogen-eq-flow
;;; copyright © 1996, 1997, 1998, 1999, 2000 by Peter Thiemann
;;; non-commercial use is free as long as the original copright notice
;;; remains intact
;;; binding-time analysis based on annotated type systems
;;; step 0
;;; the driver for the bta
(define *bta-max-bt* #f)
;;; debugging and trace aids
;;; (define *bta-display-level* 1)
(define-syntax bta-debug-level
(syntax-rules ()
((_ level arg ...)
(if (>= *bta-display-level* level)
(begin arg ...)))))
(define *bta-bt-points* '())
(define (bta-note-dynamic! ann)
(set! *bta-bt-points* (cons (cons *bta-max-bt* ann) *bta-bt-points*)))
(define (bta-note-level! lv ann)
(set! *bta-bt-points* (cons (cons lv ann) *bta-bt-points*)))
(define *bta-mutable-defines* '())
;;; binding-time analysis
;;; `d*' list of function definitions
;;; `symtab' is an initial symbol table where only constructors,
;;; selectors, and constructor tests are defined
;;; `skeleton' function call with arguments replaced by binding times
(define (bta-run d* symtab skeleton)
(bta-debug-level 1 (display "bta-run") (newline))
(let ((goal-proc (car skeleton))
(bts (cdr skeleton)))
(set! *bta-max-bt*
(apply max (cons 1 bts)))
(type->btann! the-top-type (make-ann))
(set! *bta-bt-points* '())
(bta-note-dynamic! (type->btann the-top-type))
(set! *bta-mutable-defines*
(collect-mutable-def-names d*))
(let* ((d (annDefLookup goal-proc d*))
(formals (or (annDefFetchProcFormals d)
(let ((body (annDefFetchProcBody d)))
(cond
((annIsLambda? body)
(annFetchLambdaVars body))
(else
(error "specified goal is not a procedure"))))))
(d0 (annMakeDef '$goal formals
(bta-insert-def-mutable
d*
(ann-maybe-coerce
(annMakeCall goal-proc (map annMakeVar formals))))))
(mutable-defs (collect-mutable-defs d*))
(d* (cons d0 d*))
(do-type-inference (full-collect-d* d*))
(proc-node (full-ecr (annDefFetchProcBTVar d0)))
(proc-type (node-fetch-type proc-node))
(proc-type-tcargs (type-fetch-args proc-type))
(proc-type-args
(let loop ((node (car proc-type-tcargs)))
(let ((type (node-fetch-type node)))
(let ((args (type-fetch-args type))
(ctor (type-fetch-ctor type)))
(if (equal? ctor ctor-product)
(cons (node-fetch-type (car args))
(loop (cadr args)))
'())))))
(proc-type-result (full-ecr (cadr proc-type-tcargs)))
(dynamize-result (bta-note-dynamic!
(type-fetch-btann (node-fetch-type proc-type-result))))
;; (do-type-inference (full-make-base proc-type-result))
(do-effect-analysis
(if *scheme->abssyn-static-references*
(effect-analysis d* (+ *scheme->abssyn-label-counter* 1))))
(construct-bt-constraints (wft-d* d*))
; (SHOW-BT_CONSTRAINTS
; (with-output-to-file "/tmp/display-bt-constraints.scm"
; (lambda ()
; (p (display-bts-d* d*)))))
(bt-ann* (bt-ann-sort
(append (map (lambda (bt type)
(cons bt (type-fetch-btann type)))
bts proc-type-args)
*bta-bt-points*))))
(btc-propagate *bta-max-bt* (type-fetch-btann
(node-fetch-type proc-type-result)))
(for-each (lambda (bt-ann)
(btc-propagate (car bt-ann) (cdr bt-ann)))
bt-ann*)
;;; (for-each (bta-typesig d* symtab) def-typesig*)
;;; (p (display-bts-d* d*))
(bta-solve-d* d*)
(append mutable-defs d*))))
(define bt-ann-sort
(lambda (bt-ann*)
(generic-sort (lambda (bt-ann1 bt-ann2)
(>= (car bt-ann1) (car bt-ann2))) bt-ann*)))
(define (bta-insert-def-mutable d* body)
(let ((make-op (annMakeOp1 #f
wft-define-mutable-property
#f
(parse-type '(all t t)))))
(let loop ((d* d*))
(if (null? d*)
body
(let ((def (car d*))
(d* (cdr d*)))
(if (annIsDefMutable? def)
(annMakeBegin
(make-op '_DEFINE
(list (annMakeVar (annDefFetchProcName def))
(ann-maybe-coerce (annDefFetchProcBody def))))
(loop d*))
(loop d*)))))))
(define (collect-mutable-defs d*)
(let loop ((d* d*) (defs '()))
(if (null? d*)
defs
(let ((def (car d*))
(d* (cdr d*)))
(if (annIsDefMutable? def)
(let ((name (annDefFetchProcName def)))
(loop d*
(cons
(annMakeDef name
#f
(annMakeConst name))
defs)))
(loop d* defs))))))
(define (collect-mutable-def-names d*)
(let loop ((d* d*) (names '()))
(if (null? d*)
names
(let ((def (car d*))
(d* (cdr d*)))
(if (annIsDefMutable? def)
(let ((name (annDefFetchProcName def)))
(loop d* (cons name names)))
(loop d* names))))))
(define (make-variable-filter names)
(lambda (var)
(not (memq (annFetchVar var) names))))
;;; step 1
;;; type inference
;;; types:
;;; T ::= \bot | \top | \chi (T, ..., T)
;;; \chi \in TypeConstructors
;;; { basic(0), ->(2), *(2) } \subseteq TypeConstructors
;;; representation of nodes in the contraint graph
;;; a graph node contains two fields
;;; father - #f if node is a root otherwise a pointer to another node
;;; info - #f or pointer to info node
;;;
;;; an info node contains the following fields
;;; id - unique number
;;; weight - # elements in the equivalence class
;;; leq - #f or left side of a \preceq constraint
;;; representation:
(define-record node (father info))
(define-record info (id)
(weight 1)
(type (make-type ctor-bot '()))
(dlist '())
(fvs '()))
(define-record type (ctor args)
(btann (make-ann))
(stann (make-ann))
(memo (make-ann))
(effect #f)) ;only for function types
(define-record ann ()
(bt 0)
(dlist '())
(visited #f))
;;; interface:
(define node-id 0)
(define new-node
(lambda ()
(set! node-id (+ 1 node-id))
(make-node #f (make-info node-id))))
(define node-fetch-father node->father)
(define node-fetch-info node->info)
(define node-set-father! node->father!)
(define node-set-info! node->info!)
(define info-fetch-id info->id)
(define info-fetch-weight info->weight)
(define info-fetch-type info->type)
(define info-fetch-dlist info->dlist)
(define info-fetch-fvs info->fvs)
(define info-set-weight! info->weight!)
(define info-set-type! info->type!)
(define info-set-dlist! info->dlist!)
(define info-set-fvs! info->fvs!)
;;;
;;; represent \bot by ***bot***, \top by type constructor ***top***
(define new-type make-type)
(define type-fetch-ctor type->ctor)
(define type-fetch-args type->args)
(define type-fetch-btann type->btann)
(define type-fetch-stann type->stann)
(define type-fetch-effect type->effect)
(define (ann+>dlist! x ann)
(ann->dlist! ann (cons x (ann->dlist ann))))
(define ctor-bot '***bot***)
(define ctor-top '***top***)
(define ctor-basic 'b)
(define ctor-eval '***eval-dont-lift-me***)
(define ctor-function '->)
(define ctor-product '*)
(define ctor-reference 'ref)
(define ctor-vector 'vector)
(define ctor-uninteresting (list ctor-bot ctor-basic))
(define type-bottom?
(lambda (type)
(equal? (type-fetch-ctor type) ctor-bot)))
(define type-function?
(lambda (type)
(equal? (type-fetch-ctor type) ctor-function)))
(define type-product?
(lambda (type)
(equal? (type-fetch-ctor type) ctor-product)))
(define node-fetch-type
(lambda (node)
(info-fetch-type (node-fetch-info (full-ecr node)))))
(define info-dynamic?
(lambda (info)
(let ((type (info-fetch-type info)))
(and type (eq? type the-top-type)))))
;;; find representative of equivalence class (ecr)
(define (full-ecr node)
(let loop ((node node) (ancestors '()))
(let ((father (node-fetch-father node)))
(if father
(loop father (cons node ancestors))
(begin
(for-each (lambda (ancestor)
(node-set-father! ancestor node))
ancestors)
node)))))
;;; enforce a basic value
(define (full-make-base node)
(full-add-leq node ctor-basic '()))
(define (full-make-top node)
(full-add-leq node ctor-top '()))
(define the-top-type
(let ((type (make-type ctor-top '())))
(ann->visited! (type-fetch-btann type) -1)
type))
;;; dynamize a node
(define (full-dynamize node)
(full-dynamize-ecr (full-ecr node)))
(define (full-dynamize-ecr ecr)
(full-dynamize-info (node-fetch-info ecr)))
(define (full-dynamize-info info)
(if (not (info-dynamic? info))
(begin
;; (display "dynamize") (newline)
(let ((dlist (info-fetch-dlist info))
(type (info-fetch-type info)))
(info-set-type! info the-top-type)
(for-each full-dynamize dlist)
(for-each full-dynamize (type-fetch-args type))))))
;;; add free variables
(define (full-add-fvs node fvs)
(let ((info (node-fetch-info (full-ecr node))))
(info-set-fvs! info (append fvs (info-fetch-fvs info)))))
;;; add a type constraint
(define (full-add-leq node ctor args)
(full-add-leq-ecr (full-ecr node) ctor args))
(define (full-add-leq-ecr ecr ctor args)
(let* ((info (node-fetch-info ecr))
(old-type (info-fetch-type info))
(old-ctor (type-fetch-ctor old-type)))
(if (or (type-bottom? old-type)
(eq? ctor-eval old-ctor))
(info-set-type! info (new-type ctor args))
(cond
((eq? the-top-type old-type)
(for-each full-dynamize args))
((eq? ctor old-ctor)
(for-each full-equate args (type-fetch-args old-type)))
((eq? ctor ctor-eval)
'ready)
(else
(full-dynamize-info info)
(for-each full-dynamize args))))))
;;; add constraint < arg-nodes -> res-node > \preceq node
(define (full-add-function node arg-nodes res-node)
(let ((tv-arglist (new-node)))
(full-add-leq node ctor-function (list tv-arglist res-node))
(let loop ((tv-arglist tv-arglist)
(arg-nodes arg-nodes))
(if (null? arg-nodes)
#f
(let ((tv-rest (new-node))
(arg-node (car arg-nodes)))
(full-add-leq tv-arglist ctor-product (list arg-node tv-rest))
(loop tv-rest (cdr arg-nodes)))))))
(define (function-argument-types node)
(let ((type (node-fetch-type node)))
(if (type-function? type)
(let* ((args (type-fetch-args type)))
(let loop ((argument-type (node-fetch-type (car args)))
(rev-result '()))
(if (type-product? argument-type)
(let ((args (type-fetch-args argument-type)))
(loop (node-fetch-type (cadr args))
(cons (car args) rev-result)))
(reverse rev-result))))
'())))
(define (function-result-type node)
(let ((type (node-fetch-type node)))
(if (type-function? type)
(let* ((args (type-fetch-args type)))
(cadr args))
#f)))
;;; add constraint < arg-nodes . final-arg-node -> res-node > \preceq node
(define (full-add-vfunction node arg-nodes final-arg-node res-node)
(let ((tv-arglist (new-node)))
(full-add-leq node ctor-function (list tv-arglist res-node))
(let loop ((tv-arglist tv-arglist)
(arg-nodes arg-nodes))
(if (null? arg-nodes)
(full-equate tv-arglist final-arg-node)
(let ((tv-rest (new-node))
(arg-node (car arg-nodes)))
(full-add-leq tv-arglist ctor-product (list arg-node tv-rest))
(loop tv-rest (cdr arg-nodes)))))))
;;; install a dependency node1 |> node2
(define (full-make-depend node1 node2)
(let* ((ecr1 (full-ecr node1))
(ecr2 (full-ecr node2))
(info1 (node-fetch-info ecr1))
(info2 (node-fetch-info ecr2)))
(if (info-dynamic? info1)
(full-dynamize-info info2)
(info-set-dlist! info1 (cons ecr2 (info-fetch-dlist info1))))))
(define (full-make-depend-ecr ecr nodes)
(info-set-dlist! (node-fetch-info ecr) nodes))
(define (full-add-depend-ecr ecr node)
(let ((info (node-fetch-info ecr)))
(info-set-dlist! info (cons node (info-fetch-dlist info)))))
;;; equate two nodes --- the union part
(define (full-equate node1 node2)
(let ((ecr1 (full-ecr node1))
(ecr2 (full-ecr node2)))
(if (eq? ecr1 ecr2)
'nothing-to-do
(let ((info1 (node-fetch-info ecr1))
(info2 (node-fetch-info ecr2)))
(if (info-dynamic? info1)
(if (info-dynamic? info2)
'nothing-to-do
(full-dynamize-info info2))
(if (info-dynamic? info2)
(full-dynamize-info info1)
(let* ((weight1 (info-fetch-weight info1))
(weight2 (info-fetch-weight info2))
(xc (< weight1 weight2))
(father (if xc ecr2 ecr1))
(son (if xc ecr1 ecr2))
(info (if xc info2 info1))
(old-info (if xc info1 info2)))
(node-set-father! son father)
(node-set-info! son #f)
(info-set-weight! info (+ weight1 weight2))
(info-set-dlist! info (append (info-fetch-dlist info1)
(info-fetch-dlist
info2)))
(info-set-fvs! info (append (info-fetch-fvs info1)
(info-fetch-fvs info2)))
(let* ((old-type (info-fetch-type old-info)))
(if (not (type-bottom? old-type))
(full-add-leq-ecr father
(type-fetch-ctor old-type)
(type-fetch-args old-type)))))))))))
;;; generate type variables and constraints
(define full-collect-lift-list #f)
(define (full-collect-d* d*)
(let ((symtab
(extend-env*
(map annDefFetchProcName d*)
(map (lambda (d)
(let ((node (new-node)))
(annDefSetProcBTVar! d node)
node))
d*)
the-empty-env)))
(set! full-collect-lift-list '())
(for-each (lambda (d) (full-collect-d symtab d)) d*)
(full-collect-process-lifts)
;; (display "lift-list processed") (newline)
(set! full-collect-lift-list '())))
(define (full-collect-process-lifts)
;; (display-line full-collect-process-lifts)
(let ((changes #f))
(let loop ((cur-lifts full-collect-lift-list)
(remaining-lifts '()))
(if (null? cur-lifts)
(begin
;;(display-line "done?")
(if (not changes)
'DONE
(begin
(set! changes #f)
(loop remaining-lifts '()))))
(let* ((lift-pair (car cur-lifts))
(node1 (full-ecr (car lift-pair)))
(node2 (full-ecr (cdr lift-pair)))
(ctor1 (type-fetch-ctor (node-fetch-type node1)))
(ctor2 (type-fetch-ctor (node-fetch-type node2))))
;;(display (list "lift" ctor2 (info-fetch-id (node-fetch-info node2)) "to" ctor1 (info-fetch-id (node-fetch-info node1)) "?"))
(if (eq? ctor2 ctor-bot)
(begin
;;(display-line " no")
(loop (cdr cur-lifts) (cons lift-pair remaining-lifts)))
(begin
(set! changes #t)
(if (eq? ctor2 ctor-basic)
(full-make-base node1)
(full-equate node1 node2))
;;(display-line (list "yes" (type-fetch-ctor (node-fetch-type (full-ecr node1)))))
(loop (cdr cur-lifts) remaining-lifts))))))))
(define (full-collect-d symtab d)
(let ((formals (annDefFetchProcFormals d)))
;; (display (list 'full-collect-d (annDefFetchProcName d)
;; (annDefFetchProcFormals d))) (newline)
(if formals
(let* ((arg-nodes (map (lambda (v) (new-node)) formals))
(bodytv (full-collect (annDefFetchProcBody d)
(extend-env* formals arg-nodes symtab)))
(proctv (annDefFetchProcBTVar d)))
(full-add-function proctv arg-nodes bodytv))
(let ((bodytv (full-collect (annDefFetchProcBody d) symtab))
(proctv (annDefFetchProcBTVar d)))
(full-equate bodytv proctv)
(if (annIsDefMutable? d)
(full-make-top proctv))))))
;;; interpret a type into the internal representation
(define (interpret-type type)
(bta-debug-level 1 (display-line "interpret-type: " type))
(let loop ((type type) (tenv the-empty-env))
(cond
((type-var? type)
(apply-env tenv (type-var->tvar type)
(lambda ()
(new-node))))
((type-all? type)
(loop (type-all->type type)
(extend-env (type-all->tvar type) (new-node) tenv)))
((type-rec? type)
(let* ((rec-node (new-node))
(rec-type
(loop (type-rec->type type)
(extend-env (type-rec->tvar type) rec-node tenv))))
(full-equate rec-node rec-type)
rec-node))
((type-app? type)
(let ((node (new-node))
(ctor (type-app->tcon type))
(args (map (lambda (type) (loop type tenv)) (type-app->types type))))
(if (eq? ctor ctor-function)
(if (null? args)
(error "function type constructor used as constant")
(let loop ((rev-args '()) (results args))
(let ((next-results (cdr results)))
(if (null? next-results)
(full-add-function node (reverse rev-args) (car results))
(loop (cons (car results) rev-args) next-results)))))
(full-add-leq node ctor args))
node))
(else
(error "interpret-type: unknown type form")))))
;;; full-collect returns the type variable of e
;;; this is ___ONLY___ concerned with type inference
(define (full-collect e symtab)
(let loop ((e e))
;;(display (list "full-collect" (vector-ref e 0))) (newline)
(let ((phi (new-node)))
(annExprSetType! e phi)
(cond
((annIsVar? e)
(call-with-current-continuation
(lambda (k)
(full-equate phi (apply-env symtab (annFetchVar e)
(lambda ()
(bta-debug-level 1 (display-line "unknown var: " (annFetchVar e) " considered TOP"))
(annSetVarGlobal! e #t)
(full-make-top phi)
(k 'ignored)))))))
((annIsConst? e)
(full-make-base phi))
((annIsCond? e)
(let ((phi-1 (loop (annFetchCondTest e)))
(phi-2 (loop (annFetchCondThen e)))
(phi-3 (loop (annFetchCondElse e))))
(full-equate phi-2 phi)
(full-equate phi-3 phi)))
((annIsOp? e)
(let* ((phi* (map loop (annFetchOpArgs e)))
(type (annFetchOpType e)))
(if type
(let ((prescribed-type (interpret-type type))
(inferred-type (new-node)))
(full-add-function inferred-type phi* phi)
(full-equate inferred-type prescribed-type)))
;(full-make-base phi)
;(for-each (lambda (phi-i) (full-make-base phi-i)) phi*)
(if (eq? INTERNAL-IDENTITY (annFetchOpName e))
(bta-internal-identity-property phi phi*))))
((annIsCall? e)
(let ((phi* (map loop (annFetchCallArgs e))))
(full-add-function
(apply-env symtab (annFetchCallName e) (lambda () (error "unknown proc")))
phi* phi)))
((annIsLet? e)
(let* ((phi-1 (loop (annFetchLetHeader e)))
(phi-2 (full-collect (annFetchLetBody e)
(extend-env (annFetchLetVar e) phi-1 symtab))))
(full-equate phi phi-2)))
((annIsBegin? e)
(let* ((phi-1 (loop (annFetchBeginHeader e)))
(phi-2 (loop (annFetchBeginBody e))))
(full-equate phi phi-2)))
((annIsVLambda? e)
(let* ((fixed-formals (annFetchVLambdaFixedVars e))
(var-formal (annFetchVLambdaVar e))
(vars (cons var-formal fixed-formals))
(pvs (map (lambda (v) (new-node)) vars))
;(fvs (map (lambda (v) (cdr (assoc (annFetchVar v) symtab))) (annFreeVars e)))
(phi-0 (full-collect (annFetchVLambdaBody e)
(extend-env* vars pvs symtab))))
(annSetVLambdaBTVars! e pvs)
(full-add-vfunction phi (cdr pvs) (car pvs) phi-0)))
((annIsLambda? e)
(let* ((vars (annFetchLambdaVars e))
(pvs (map (lambda (v) (new-node)) vars))
(fvs (annFreeVars e))
(phi-0 (full-collect (annFetchLambdaBody e)
(extend-env* vars pvs symtab))))
(annSetLambdaBTVars! e pvs)
(full-add-function phi pvs phi-0)
(full-add-fvs phi fvs)))
((annIsApp? e)
(let* ((phi-0 (loop (annFetchAppRator e)))
(phi* (map loop (annFetchAppRands e))))
(full-add-function phi-0 phi* phi)))
((annIsCtor? e)
(let* ((phi* (map loop (annFetchCtorArgs e)))
(ctor (annFetchCtorName e))
(ctorDesc (annFetchCtorDesc e))
(nr-pre (desc-np ctorDesc))
(nr-arg (desc-nc ctorDesc))
(nr-post (- (desc-nt ctorDesc) (+ nr-pre nr-arg)))
(pp (append (nlist nr-pre new-node)
phi*
(nlist nr-post new-node))))
(full-add-leq phi (desc-type ctorDesc) pp)))
((annIsSel? e)
(let* ((phi-0 (loop (annFetchSelArg e)))
(comp (annFetchSelComp e))
(desc (annFetchSelDesc e))
(nr-pre (desc-np desc))
(nr-arg (desc-nc desc))
(nr-post (- (desc-nt desc) (+ nr-pre nr-arg))))
(full-add-leq phi-0 (desc-type desc)
(append (nlist nr-pre new-node)
(nlist (- comp 1) new-node)
(list phi)
(nlist (- nr-arg comp) new-node)
(nlist nr-post new-node)))))
((annIsTest? e)
(let* ((phi-0 (loop (annFetchTestArg e)))
(desc (annFetchTestDesc e)))
(full-add-leq phi-0 (desc-type desc) (nlist (desc-nt desc) new-node))
(full-make-base phi)))
((annIsEval? e)
(let ((phi-0 (loop (annFetchEvalBody e))))
(full-make-base phi-0)
(full-add-leq phi ctor-eval '())))
((annIsRef? e)
(let ((phi-0 (loop (annFetchRefArg e))))
(full-add-leq phi ctor-reference (list phi-0))))
((annIsDeref? e)
(let ((phi-0 (loop (annFetchDerefArg e))))
(full-add-leq phi-0 ctor-reference (list phi))))
((annIsAssign? e)
(let ((phi-0 (loop (annFetchAssignRef e)))
(phi-1 (loop (annFetchAssignArg e))))
(full-add-leq phi-0 ctor-reference (list phi-1))
(full-make-base phi)))
((annIsCellEq? e)
(let* ((phi* (map loop (annFetchCellEqArgs e)))
(phi-1 (car phi*))
(phi-2 (cadr phi*)))
(full-add-leq phi-1 ctor-reference (list (new-node)))
(full-equate phi-1 phi-2)))
((annIsVector? e)
(let ((phi-0 (loop (annFetchVectorSize e)))
(phi-1 (loop (annFetchVectorArg e))))
(full-make-base phi-0)
(full-add-leq phi ctor-vector (list phi-1))))
((annIsVref? e)
(let ((phi-0 (loop (annFetchVrefArg e)))
(phi-1 (loop (annFetchVrefIndex e))))
(full-make-base phi-1)
(full-add-leq phi-0 ctor-vector (list phi))))
((annIsVlen? e)
(let ((phi-0 (loop (annFetchVlenVec e))))
(full-add-leq phi-0 ctor-vector (list (new-node)))
(full-make-base phi)))
((annIsVset? e)
(let ((phi-0 (loop (annFetchVsetVec e)))
(phi-1 (loop (annFetchVsetIndex e)))
(phi-2 (loop (annFetchVsetArg e))))
(full-make-base phi-1)
(full-add-leq phi-0 ctor-vector (list phi-2))
(full-make-base phi)))
((annIsVfill? e)
(let ((phi-0 (loop (annFetchVfillVec e)))
(phi-1 (loop (annFetchVfillArg e))))
(full-add-leq phi-0 ctor-vector (list phi-1))
(full-make-base phi)))
(else
(error "full-collect: unrecognized syntax")))
phi)))
;;; property functions for built-in operators
(define bta-internal-identity-property
(lambda (phi phi*)
(full-make-depend (car phi*) phi)
(set! full-collect-lift-list
(cons (cons phi (car phi*))
full-collect-lift-list))))
;;; step 2
;;; well-formedness constraints
(define wft-visited 0)
(define (wft-t type)
(let ((targs (type-fetch-args type))
(btann (type-fetch-btann type))
(stann (type-fetch-stann type)))
(if (eq? type the-top-type)
'nothing-to-do
(if (ann->visited btann)
'nothing-to-do ;break recursion for recursive types
(begin
(set! wft-visited (+ 1 wft-visited))
(ann->visited! btann wft-visited)
(ann->visited! stann (+ 1000000 wft-visited))
(let loop ((args targs)
(dlist (cons stann (ann->dlist btann)))) ;beta <= sigma
(if (null? args)
(begin
(ann->dlist! btann dlist)
(for-each (lambda (node) (wft-t (node-fetch-type node)))
targs)
(let ((effect (type-fetch-effect type)))
(if effect
(begin
(let ((ctor (type-fetch-ctor type)))
(cond
((eq? ctor ctor-function)
;; (display (list "effect on function" (display-bts-eff effect))) (newline)
(effect-for-each
(lambda (lab)
(let* ((reftype
(effect-label->type lab))
(rbtann (type-fetch-btann
reftype)))
(ann+>dlist! rbtann btann)))
effect))
((eq? ctor ctor-top)
;; (display (list "effect on top" (display-bts-eff effect))) (newline)
(effect-for-each
(lambda (lab)
(let* ((reftype
(effect-label->type lab))
(rbtann (type-fetch-btann
reftype)))
(bta-note-dynamic! rbtann)))
effect))
((eq? ctor ctor-reference)
'nothing-to-do)
((eq? ctor ctor-vector)
'nothing-to-do)
(else
(error "effect on " ctor))))))))
(let ((arg (node-fetch-type (car args))))
(let ((arg-stann (type-fetch-stann arg)))
(ann+>dlist! stann arg-stann)) ; sigma_i <= sigma
(loop (cdr args) (cons (type-fetch-btann arg) dlist))))))))))
;beta <= beta_i
(define (wft-e e auto-memo)
(let loop ((e e))
;; (display "wft") (display e) (newline)
(let* ((ecr (full-ecr (annExprFetchType e)))
(type (node-fetch-type ecr)))
(annExprSetType! e ecr)
(wft-t type)
(cond
((annIsVar? e)
;; this is really unfortunate:
;; how do we lift a variable????
;; >>> we apply an invisible primitive to each variable!!!
)
((annIsCond? e)
(let ((test-exp (annFetchCondTest e)))
(loop test-exp)
(loop (annFetchCondThen e))
(loop (annFetchCondElse e))
(let* ((btann (type-fetch-btann type))
(test-type (node-fetch-type (annExprFetchType test-exp)))
(test-btann (type-fetch-btann test-type))
(test-stann (type-fetch-stann test-type)))
(if auto-memo
(ann+>dlist! btann test-btann)) ; beta => beta_1
(ann+>dlist! test-btann test-stann)))) ; sigma_1 <= beta_1
((annIsOp? e)
(let ((op-arg-types (map loop (annFetchOpArgs e)))
(op-property (annFetchOpProperty e))
(op-name (annFetchOpName e)))
(cond
((procedure? op-property)
(op-property type op-arg-types))
((number? op-property)
(wft-number-property op-property type op-arg-types))
((eq? op-name INTERNAL-IDENTITY)
(let* ((arg-type (car op-arg-types))
(arg-btann (type-fetch-btann arg-type))
(arg-stann (type-fetch-stann arg-type)))
(ann+>dlist! (type-fetch-btann type) arg-btann)
(ann+>dlist! (type-fetch-stann type) arg-stann)))
((and (symbol? op-property)
(assoc op-property wft-property-table))
=> (lambda (sym/prop) ((cdr sym/prop) type op-arg-types)))
(else
(wft-depend-property type op-arg-types)))))
((annIsCall? e)
(for-each loop (annFetchCallArgs e)))
((annIsLet? e)
(loop (annFetchLetHeader e))
(loop (annFetchLetBody e)))
((annIsBegin? e)
(loop (annFetchBeginHeader e))
(loop (annFetchBeginBody e)))
((annIsVLambda? e)
(loop (annFetchVLambdaBody e)))
((annIsLambda? e)
(loop (annFetchLambdaBody e)))
((annIsApp? e)
(loop (annFetchAppRator e))
(for-each loop (annFetchAppRands e)))
((annIsCtor? e)
(for-each loop (annFetchCtorArgs e)))
((annIsSel? e)
(loop (annFetchSelArg e)))
((annIsTest? e)
(let* ((btann (type-fetch-btann type))
(arg (annFetchTestArg e))
(arg-type (node-fetch-type (annExprFetchType arg)))
(arg-btann (type-fetch-btann arg-type)))
(loop arg)
(ann+>dlist! btann arg-btann))) ; beta_arg <= beta
((annIsEval? e)
(let ((stann (type-fetch-stann type))
(btann (type-fetch-btann type)))
(ann+>dlist! btann stann)) ; sigma <= beta
(loop (annFetchEvalBody e)))
((annIsRef? e)
(loop (annFetchRefArg e)))
((annIsDeref? e)
(loop (annFetchDerefArg e)))
((annIsAssign? e)
(let* ((btann (type-fetch-btann type))
(ref (annFetchAssignRef e)))
(loop ref)
(loop (annFetchAssignArg e))
(let* ((ref-type (node-fetch-type (annExprFetchType ref)))
(ref-btann (type-fetch-btann ref-type)))
(ann+>dlist! btann ref-btann)))) ;beta_ref <= beta
((annIsCellEq? e)
(for-each loop (annFetchCellEqArgs e)))
((annIsVector? e)
(loop (annFetchVectorArg e))
(let ((size (annFetchVectorSize e)))
(loop size)
(let* ((size-type (node-fetch-type (annExprFetchType size)))
(size-btann (type-fetch-btann size-type))
(btann (type-fetch-btann type)))
(ann+>dlist! size-btann btann)
(ann+>dlist! btann size-btann)))) ; beta_size == beta
((annIsVref? e)
(let ((ref (annFetchVrefArg e))
(index (annFetchVrefIndex e)))
(loop ref)
(loop index)
(let* ((ref-type (node-fetch-type (annExprFetchType ref)))
(ref-btann (type-fetch-btann ref-type))
(index-type (node-fetch-type (annExprFetchType index)))
(index-btann (type-fetch-btann index-type)))
(ann+>dlist! index-btann ref-btann)
(ann+>dlist! ref-btann index-btann)))) ;beta_index == beta_ref
((annIsVlen? e)
(let ((btann (type-fetch-btann type))
(ref (annFetchVlenVec e)))
(loop ref)
(let* ((ref-type (node-fetch-type (annExprFetchType ref)))
(ref-btann (type-fetch-btann ref-type)))
(ann+>dlist! btann ref-btann)))) ;beta_ref <= beta
((annIsVset? e)
(let* ((btann (type-fetch-btann type))
(ref (annFetchVsetVec e))
(index (annFetchVsetIndex e)))
(loop ref)
(loop index)
(loop (annFetchVsetArg e))
(let* ((ref-type (node-fetch-type (annExprFetchType ref)))
(ref-btann (type-fetch-btann ref-type))
(index-type (node-fetch-type (annExprFetchType index)))
(index-btann (type-fetch-btann index-type)))
(ann+>dlist! index-btann ref-btann)
(ann+>dlist! ref-btann index-btann) ;beta_index == beta_ref
(ann+>dlist! btann ref-btann)))) ;beta_ref <= beta
((annIsVfill? e)
(let* ((btann (type-fetch-btann type))
(ref (annFetchVfillVec e)))
(loop ref)
(loop (annFetchVfillArg e))
(let* ((ref-type (node-fetch-type (annExprFetchType ref)))
(ref-btann (type-fetch-btann ref-type)))
(ann+>dlist! btann ref-btann)))) ;beta_ref <= beta
(else
'nothing-to-do))
type)))
(define (wft-d* d*)
(set! wft-visited 0)
(for-each
(lambda (d)
(wft-t (node-fetch-type (annDefFetchProcBTVar d)))
(wft-e (annDefFetchProcBody d) (annDefFetchProcAutoMemo d)))
d*))
;;; wft property functions for built-in operators
;;; each function accepts the result type and the list of argument types
(define wft-depend-property
(lambda (type type*)
(let ((btann (type-fetch-btann type))
(stann (type-fetch-stann type)))
(ann+>dlist! btann stann) ; sigma <= beta
(for-each
(lambda (arg-type)
(let ((arg-btann (type-fetch-btann arg-type))
(arg-stann (type-fetch-stann arg-type)))
(ann+>dlist! arg-btann arg-stann) ; sigma_i <= beta_i
(ann+>dlist! btann arg-btann) ; beta_i <= beta
(ann+>dlist! arg-btann btann) ; beta <= beta_i
))
type*))))
(define wft-apply-property
(lambda (type type*)
(bta-debug-level 3 (display "wft-apply-property") (newline))
(wft-depend-property type type*)
(let ((type-fun (car type*))
(type-args (cdr type*)))
(let ((btann (type-fetch-btann type))
(fun-btann (type-fetch-btann type-fun)))
(ann+>dlist! fun-btann btann))))) ; btann <= fun-btann
(define wft-dynamic-property
(lambda (type type*)
(bta-debug-level 3 (display "wft-dynamic-property") (newline))
(for-each (lambda (type)
(bta-note-dynamic! (type-fetch-btann type)))
(cons type type*))))
(define wft-error-property
(lambda (type type*)
(bta-debug-level 3 (display "wft-error-property") (newline))
(let ((btann (type-fetch-btann type))
(stann (type-fetch-stann type)))
;; (ann+>dlist! btann stann) ; sigma <= beta
(for-each
(lambda (arg-type)
(let ((arg-btann (type-fetch-btann arg-type))
(arg-stann (type-fetch-stann arg-type)))
(ann+>dlist! arg-btann arg-stann) ; sigma_i <= beta_i
(ann+>dlist! btann arg-btann) ; beta_i <= beta
(ann+>dlist! arg-btann btann) ; beta <= beta_i
))
type*))))
(define wft-number-property
(lambda (level type type*)
(bta-debug-level 3 (display "wft-number-property") (newline))
(bta-note-level! level (type-fetch-btann type))))
(define wft-define-data-property
(lambda (type type*)
(bta-note-level! (- *bta-max-bt* 1) (type-fetch-btann type))))
(define wft-define-mutable-property
(lambda (type type*)
(bta-note-level! (- *bta-max-bt* 1) (type-fetch-btann type))
(for-each (lambda (arg-type)
(bta-note-level! *bta-max-bt* (type-fetch-btann arg-type)))
type*)))
(define (wft-make-memo-property level active)
(let ((not-initialized #t))
(lambda (type type*)
(if not-initialized
(begin
(set! level ((eval `(lambda (max-level) ,level) (interaction-environment))
*bta-max-bt*))
(set! active ((eval `(lambda (max-level) ,active) (interaction-environment))
*bta-max-bt*))
(if (< level 0) (set! level 0))
(if (>= level *bta-max-bt*) (set! level *bta-max-bt*))
(set! not-initialized #f)))
(cond
((and (number? active)
(<= active *bta-max-bt*))
(for-each (lambda (type)
(bta-note-level!
level
(type-fetch-btann type)))
(cons type type*)))
((not (number? active))
;; everything else is enforced by the typing
(bta-note-level! level (type-fetch-btann type)))
(else
(wft-depend-property type type*))))))
(define wft-property-table
`((apply . ,wft-apply-property)
(dynamic . ,wft-dynamic-property)
(error . ,wft-error-property)
(opaque . ,wft-dynamic-property)))
;;; propagate binding-time constraints
(define btc-propagate
(lambda (bt ann)
(bta-debug-level 3 (display "btc-propagate ") (display bt) (display " ("))
(let loop ((ann ann))
(bta-debug-level 3 (display (ann->visited ann)) (display " "))
(if (< (ann->bt ann) bt)
(let ((dlist (ann->dlist ann)))
(bta-debug-level 3 (display (map ann->visited dlist)) (display " "))
(ann->bt! ann bt)
(if dlist
(for-each loop dlist)))))
(bta-debug-level 3 (display ")") (newline))))
;;; step 3