forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjulia-interpreter.scm
1546 lines (1352 loc) · 47.3 KB
/
julia-interpreter.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
#|
TODO:
* local variable identification pass
* varargs
* apply, splat
* builtin scalar conversions, implicit conversion mechanism
- modules
- separate type for literals
- global var declaration
- optional arguments
- quote, expr, and symbol types, user-level macros
- where clauses
- more builtin functions (shifts, bitwise ops, primitive i/o)
- keyword arguments
- try/catch
|#
(define *julia-interpreter* #t)
(define julia-globals (make-table))
; --- tuples ---
; note: every julia value is represented as a vector whose first
; element is the type of the value
(define (julia-tuple . args) args)
(define (tuple->list t) t)
(define (list->tuple l) l)
(define (tuples->alist t) t)
(define (alist->tuples l) l)
(define (tuple-append t1 t2) (append t1 t2))
(define (tuple-ref t i) (list-ref t i))
(define (tuple-set! t i x) (set-car! (list-tail t i) x))
(define (tuple-length t) (length t))
(define (tuple? x) (or (pair? x) (null? x)))
(define (make-tuple-type typelist) (list->tuple typelist))
; --- singleton null value ---
(define julia-null (julia-tuple))
(define (j-null? x) (null? x))
; --- type objects, reflection ---
(define (put-type name t) (table-set! julia-globals name t))
(define AbstractKind (vector 'StructKind
'TagType 'Type-type julia-null
(julia-tuple 'name 'super 'parameters)
#f;(julia-tuple Symbol-type Type-type Tuple-type)
#f #f))
(define (make-tag-type name super parameters)
(vector AbstractKind name super parameters))
(define (tag-type-name t) (vector-ref t 1))
(define (tag-type-super t) (vector-ref t 2))
(define (tag-type-params t) (vector-ref t 3))
(define Any-type (make-tag-type 'Any 'Any julia-null))
(define Type-type (make-tag-type 'Type Any-type julia-null))
(define Tuple-type (make-tag-type 'Tuple Any-type julia-null))
(define Symbol-type (make-tag-type 'Symbol Any-type julia-null))
(vector-set! AbstractKind 2 Type-type)
(vector-set! AbstractKind 5 (julia-tuple Symbol-type Type-type Tuple-type))
(define FunctionKind (vector 'StructKind
'Function Type-type julia-null
(julia-tuple 'from 'to)
(julia-tuple Type-type Type-type)
#f #f))
(define (type-var? a) #f) ; forward decl
(define (make-function-type a b)
; convert X-->Y to (X,)-->Y if X is not already a tuple or typevar
(let ((a (if (and (not (tuple? a))
(not (type-var? a)))
(julia-tuple a)
a)))
(vector FunctionKind a b)))
(define (func-type? t) (and (vector? t) (eq? (vector-ref t 0) FunctionKind)))
(define function-type? func-type?)
(define (func-fromtype t) (vector-ref t 1))
(define (func-totype t) (vector-ref t 2))
(define any-func (make-function-type Any-type Any-type))
(define (make-closure proc env)
(vector any-func proc env))
(define StructKind (vector 'StructKind
'StructType AbstractKind julia-null
(julia-tuple 'name 'super 'parameters
'names 'types 'new 'convert)
(julia-tuple Symbol-type Type-type Tuple-type
Tuple-type Tuple-type
any-func any-func)
#f #f))
(define (make-generic-function name) julia-null) ; forward decl
(define (add-method-for gf types meth) #t) ; forward decl
(define (make-struct-type name super params fnames ftypes . ctor)
(let* ((cnvt (make-generic-function 'convert))
(T (vector StructKind name super params fnames ftypes
#f cnvt)))
(vector-set! T 6
(make-closure
(if (pair? ctor)
(car ctor)
(lambda (ce args)
(apply vector ce args)))
T))
; add the identity conversion
(add-method-for cnvt (julia-tuple T) (make-closure (lambda (ce args)
(car args)) #f))
T))
(vector-set! AbstractKind 0 StructKind)
(vector-set! FunctionKind 0 StructKind)
(vector-set! StructKind 0 StructKind)
(define (struct-type? t) (and (vector? t) (eq? (vector-ref t 0) StructKind)))
(define (tag-type? t)
(and (vector? t)
(or (eq? (vector-ref t 0) AbstractKind)
(eq? (vector-ref t 0) StructKind))))
(define UnionKind (vector StructKind
'Union Type-type julia-null
(julia-tuple 'types)
(julia-tuple Tuple-type)
#f #f))
(define (all-pairs? pred lst)
(every (lambda (first)
(every (lambda (second) (pred first second))
(filter (lambda (x) (not (eq? x first))) lst)))
lst))
(define (make-union-type params)
; avoid constructing union types that would force the conform procedure
; to perform an exponential search. this would happen if a union contained
; two types A and B such that there exists a type X that could match either,
; but potentially with different type parameter assignments. For example:
; (Union(Complex[T],Complex[S]), T)
; This should match (Complex[Int32], Float64), but we can only determine that
; by trying the rest of the pattern with both T=Int32 and S=Int32.
; Of course this *could* be implemented, but it's way more complex than
; what is needed, which is combining disjoint types like () and Int, or
; EmptyTree and TreeNode.
(define (non-overlapping? a b)
(not (and (has-typevars? a)
(has-typevars? b)
(conform a b))))
(if (all-pairs? non-overlapping? params)
(vector UnionKind (list->tuple params))
(error "Type pattern too complex")))
(define (union-type? t) (and (vector? t) (eq? (vector-ref t 0) UnionKind)))
(define (union-type-types t) (vector-ref t 1))
(define Bottom-type (make-union-type '()))
(put-type 'Bottom Bottom-type)
; --- type constructors ---
(define TypeConstructor
(make-struct-type 'TypeConstructor Any-type julia-null
(julia-tuple 'parameters 'body)
(julia-tuple Tuple-type Type-type)))
(put-type 'TypeConstructor TypeConstructor)
(define (make-type-constructor params body)
(vector TypeConstructor params body))
(define (type-ctor? t)
(and (vector? t) (eq? (vector-ref t 0) TypeConstructor)))
(define (type-ctor-params t) (vector-ref t 1))
(define (type-ctor-type t) (vector-ref t 2))
(define TypeVar
(make-struct-type 'TypeVar Any-type julia-null
(julia-tuple 'name 'lb 'ub)
(julia-tuple Symbol-type Type-type Type-type)))
(put-type 'TypeVar TypeVar)
(define (make-type-var name . b)
(vector TypeVar name
(if (pair? b) (car b) Bottom-type)
(if (pair? b) (cadr b) Any-type)))
(set! type-var? (lambda (t)
(and (vector? t) (eq? (vector-ref t 0) TypeVar))))
(define (type-var-name t) (vector-ref t 1))
(define (type-var-lb t) (vector-ref t 2))
(define (type-var-ub t) (vector-ref t 3))
(define (type-vars l) (map (lambda (n) (make-type-var n)) l))
; --- general type accessors and predicates ---
(define (type-name t)
(cond ((tag-type? t) (vector-ref t 1))
((tuple? t) 'Tuple)
((type-ctor? t) (type-name (type-ctor-type t)))
(else (vector-ref (vector-ref t 0) 1))))
(define (type-super t)
(cond ((tuple? t) Tuple-type)
((eq? t Any-type) Any-type)
((tag-type? t) (vector-ref t 2))
(else Any-type)))
(define (type-params t)
(cond ((tuple? t) t)
((tag-type? t) (vector-ref t 3))
((union-type? t) (vector-ref t 1))
((func-type? t) (list (func-fromtype t) (func-totype t)))
(else julia-null)))
(define (type-field-names t)
(if (struct-type? t) (vector-ref t 4) julia-null))
(define (type-field-types t)
(if (struct-type? t) (vector-ref t 5) julia-null))
(define (closure-proc c) (vector-ref c 1))
(define (closure-env c) (vector-ref c 2))
(define (j-closure? x) (and (vector? x)
(func-type? (vector-ref x 0))))
(define (generic-function? x) (and (j-closure? x)
(eq? (closure-proc x) j-apply-generic)))
; get the type of a value
(define (type-of v)
(cond ((eq? v julia-null) julia-null)
; for now, allow scheme symbols to act as julia symbols
((symbol? v) Symbol-type)
((string? v) Any-type) ; temporary
((procedure? v) Any-type)
((number? v) Any-type)
((tuple? v) (make-tuple-type
(map type-of (tuple->list v))))
(else (vector-ref v 0))))
(define (type? v) (or (and (tuple? v)
(every (lambda (x)
(or (type-var? x)
(type? x))) v))
(and (vector? v)
(let ((tag (vector-ref v 0)))
(or (eq? tag UnionKind)
(eq? tag StructKind)
(eq? tag FunctionKind)
(eq? tag AbstractKind))))))
; --- define primitive types ---
(put-type 'Any Any-type)
(put-type 'Tuple Tuple-type)
(put-type 'Type Type-type)
(put-type 'Symbol Symbol-type)
(define sequence-type
(let ((v (type-vars '(T))))
(make-type-constructor v (make-tag-type '... Any-type v))))
(put-type '... sequence-type)
(define (sequence-type? t)
(and (type? t)
(eq? (type-name t) '...)))
(put-type 'Function
(let ((v (type-vars '(A B))))
(make-type-constructor v (apply make-function-type v))))
; --- type functions ---
(define (has-params? t)
(not (j-null? (type-params t))))
(define (type-param0 t)
(tuple-ref (type-params t) 0))
(define (type-param t n)
(tuple-ref (type-params t) n))
(define (j-int32? x)
(and (vector? x) (eq? (type-name (type-of x)) 'Int32)))
(define (instantiate-type tc params)
(if (not (every (lambda (p)
(or (type? p) (type-var? p) (j-int32? p)))
params))
(error "Invalid parameter for type" (type-name tc)))
(let ((tp (type-ctor-params tc)))
(check-same-length
params tp
(lambda () (error "Too few parameters for type" (type-name tc)))
(lambda () (error "Too many parameters for type" (type-name tc))))
(instantiate-type- (type-ctor-type tc) (map cons tp params))))
; instantiate a type using the bindings in the given type environment
; (an assoc list from names to types)
(define (instantiate-type- type env)
(instantiate-type-- type env '()))
(define *type-cache* '())
(define (lookup-type table key)
(assoc-p key table
(lambda (x y)
(and (eq? (car x) (car y))
(andmap (lambda (u v) (type-eqv? u v))
(cdr x) (cdr y))))))
(define (cache-type! key type)
(set! *type-cache* (cons (cons key type) *type-cache*)))
(define (type-eqv? a b) (type-eqv?- a b '()))
(define (type-eqv?- a b stack)
(cond ((eq? a b) #t)
((j-int32? a) (and (j-int32? b) (equal? (j-unbox a) (j-unbox b))))
((member-p (cons a b) stack (lambda (x y)
(and (eq? (car x) (car y))
(eq? (cdr x) (cdr y)))))
#t)
((and (type? a) (type? b))
(and (eq? (type-name a) (type-name b))
(let ((newstack
(cons (cons a b) stack)))
(andmap (lambda (x y)
(type-eqv?- x y newstack))
(type-params a)
(type-params b)))))
(else #f)))
(define (instantiate-type-- type env stack)
(define (recur t) (instantiate-type-- t env stack))
(cond
((type-var? type) (lookup type env type))
((not (type? type)) type)
((null? env) type)
((tuple? type)
(map recur type))
((union-type? type)
(make-union-type (map recur (union-type-types type))))
((func-type? type)
(make-function-type (recur (func-fromtype type))
(recur (func-totype type))))
((null? (tag-type-params type)) type)
((tag-type? type)
(let* ((i-params (map (lambda (t)
(if (eq? t type) type
(recur t)))
(tag-type-params type)))
(key (cons (type-name type) i-params)))
(cond ((lookup-type stack key) => cdr)
((lookup-type *type-cache* key) => cdr)
; always instantiate a type using the original type
; constructor and prototype; don't copy a type that's
; already been instantiated (with TypeVars)
((let ((tc (table-ref julia-globals (type-name type) #f)))
(and tc
(not (eq? (type-ctor-type tc) type))
(instantiate-type tc i-params))))
((struct-type? type)
(let* ((newstruct
(make-struct-type (tag-type-name type)
(recur (type-super type)) i-params
(type-field-names type)
#f
(closure-proc (vector-ref type 6))))
(newstack (cons (cons key newstruct) stack)))
(if (type-field-types type)
(vector-set!
newstruct 5
(map (lambda (ft)
(instantiate-type-- ft env newstack))
(type-field-types type))))
(if (generic-function? (vector-ref type 7))
(vector-set!
newstruct 7
(instantiate-generic-function (vector-ref type 7) env
newstack)))
(cache-type! key newstruct)
newstruct))
(else (make-tag-type (tag-type-name type)
(recur (type-super type)) i-params)))))))
(define (type-equal? a b)
(and (subtype? a b)
(subtype? b a)))
(define (tuple-elementwise? pred child parent)
(let loop ((cp (type-params child)) ; child parameters
(pp (type-params parent))) ; parent parameters
(let ((cseq (and (pair? cp) (sequence-type? (car cp))))
(pseq (and (pair? pp) (sequence-type? (car pp)))))
(cond
((null? cp) (or (null? pp) pseq))
((and cseq (not pseq)) #f)
((null? pp) #f)
(else
(and (pred (if cseq
(type-param0 (car cp))
(car cp))
(if pseq
(type-param0 (car pp))
(car pp)))
; if both end up on sequence types, and
; parameter matched. stop with "yes" now,
; otherwise we'd start looping forever
(or (and pseq cseq)
(loop (if cseq cp (cdr cp))
(if pseq pp (cdr pp))))))))))
(define (tuple-subtype? child parent)
(tuple-elementwise? subtype? child parent))
(define (subtype? child parent)
(cond ((eq? child parent) #t)
((type-var? parent) #t
#;(and (subtype? child (type-var-ub parent))
(subtype? (type-var-lb parent) child)))
((type-var? child) #f
#;(and (subtype? (type-var-ub child) parent)
(subtype? (type-var-lb child) parent)))
((eq? parent Tuple-type) (tuple? child))
((eq? parent Any-type) #t)
((eq? child Any-type) #f)
((and (j-int32? child) (j-int32? parent))
(= (j-unbox child) (j-unbox parent)))
; recursively handle union types
((union-type? child)
(every (lambda (t) (subtype? t parent))
(type-params child)))
((union-type? parent)
(any (lambda (t) (subtype? child t))
(type-params parent)))
((and (tuple? child) (tuple? parent))
(tuple-subtype? child parent))
; functions are contravariant in first parameter
((and (func-type? child)
(func-type? parent))
(and (or (type-var? (func-fromtype parent))
(subtype? (func-fromtype parent)
(func-fromtype child)))
(subtype? (func-totype child)
(func-totype parent))))
; handle sibling instantiations of the same generic type.
((eq? (type-name child)
(type-name parent))
(let loop ((cp (type-params child)) ; child parameters
(pp (type-params parent))) ; parent parameters
(cond
((null? cp) (null? pp))
((null? pp) #f)
(else
; default to invariance
(and (type-equal? (car cp) (car pp))
(loop (cdr cp) (cdr pp)))))))
; otherwise walk up the type hierarchy
(else (subtype? (type-super child) parent))))
; tells whether a type conforms to a given type pattern (a type
; containing TypeVars)
; returns #f or an assoc list showing the assignment of
; type parameters that makes the relation hold
(define (conform t pat) (conform- t pat '()))
; generate list of corresponding type components, (type . T) if parameter
; T might correspond to type
(define (conform- child parent env)
;(display "conform- ")
;(julia-print child) (display " ")
;(julia-print parent) (newline)
(cond ((type-var? parent)
(let ((val (lookup parent env #f)))
(if val
(and (type-eqv? child val)
env)
(cons (cons parent child) env))))
((type-var? child) #f)
((and (j-int32? child) (j-int32? parent))
(and (= (j-unbox child) (j-unbox parent))
env))
((eq? child parent) env)
((eq? parent Any-type) env)
((eq? child Any-type) #f)
((union-type? child)
(foldl (lambda (t env)
(and env
(conform- t parent env)))
env
(type-params child)))
((union-type? parent)
(any (lambda (t) (conform- child t env))
(type-params parent)))
; handle tuple types, or any sibling instantiations of the same
; generic type. parameters must be consistent.
; examples:
; (a, a) conforms (b, c) YES
; (a, b) conforms (c, c) NO
; (Int8, Int8) conforms (Int, Int) YES
; (a, a) conforms (Int8, Int8) NO
((eq? (type-name child)
(type-name parent))
(let loop ((cp (type-params child)) ; child parameters
(pp (type-params parent)) ; parent parameters
(env env))
(let ((cseq (and (pair? cp) (sequence-type? (car cp))))
(pseq (and (pair? pp) (sequence-type? (car pp)))))
(cond
((null? cp) (cond ((null? pp) env)
(pseq env)
(else #f)))
((and cseq (not pseq)) #f)
((null? pp) #f)
(else
; default to covariance
(let ((newenv (conform- (if cseq
(type-param0 (car cp))
(car cp))
(if pseq
(type-param0 (car pp))
(car pp))
env)))
(and newenv
; if both end up on sequence types, and
; parameter matched. stop with "yes" now,
; otherwise we'd start looping forever
(if (and pseq cseq)
newenv
(loop (if cseq cp (cdr cp))
(if pseq pp (cdr pp))
newenv)))))))))
; otherwise walk up the type hierarchy
(else
(conform- (type-super child) parent env))))
(define (has-typevars? t)
(or (type-var? t)
(and (type? t)
(not (eq? t scalar-type)) ; circular reference problem
(any has-typevars? (type-params t)))))
; --- function objects ---
(define (make-lambda-closure lambda-expr cloenv)
(make-closure j-eval-body (cons lambda-expr cloenv)))
(define (lambda-closure-expr c)
(let ((e (car (closure-env c))))
(if (and (pair? e)
(eq? (car e) 'lambda))
e
(error "Not a lambda closure"))))
(define (lambda-closure-env c)
(lambda-closure-expr c) ; for the error check
(cdr (closure-env c)))
(define (lambda-closure? x)
(let ((e (closure-env x)))
(and (pair? e)
(pair? (car e))
(eq? (caar e) 'lambda))))
; --- type-keyed hash table ---
(define (make-method-table) (vector '()))
(define (mt:mlist mt) (vector-ref mt 0))
(define (method-table-assoc-p methlist type pred)
(let loop ((m methlist))
(if (null? m)
#f
(if (pred type (caar m))
(car m)
(loop (cdr m))))))
(define (instantiate-method f env)
; env contains static parameter assignments
; compilation goes here
(if (lambda-closure? f)
(make-lambda-closure
(let ((e (lambda-closure-expr f)))
`(lambda ,(cadr e)
; insert the static parameter values!
,(append (list-head (caddr e) 4)
(list (append (map (lambda (p)
(cons (type-var-name (car p))
(cdr p)))
env)
(list-ref (caddr e) 4))))
,(cadddr e)))
(lambda-closure-env f))
f))
(define (method-table-assoc methtable type)
(let ((m (method-table-assoc-p (mt:mlist methtable) type
(lambda (t mt)
(if (has-typevars? mt)
(conform t mt)
(subtype? t mt))))))
(and m
(if (has-typevars? (car m))
(let* ((env (conform type (car m)))
(newmeth (instantiate-method (cdr m) env)))
; cache result in concrete method table
(method-table-insert! methtable type newmeth)
(cons type newmeth))
m))))
(define (method-table-insert-p mlist type method pred)
(let ((m (method-table-assoc-p mlist type pred)))
(if (and m (type-equal? type (car m)))
(begin (set-car! m type) ; replace existing key
(set-cdr! m method)
mlist)
(cons-in-order (cons type method) mlist car pred))))
; test whether a type is not more general than another
; this defines the method search order. note it has nothing to do with
; whether a and b are compatible in any way (e.g. assignable, convertible)
(define (type<=? a b)
(if (has-typevars? a)
(if (has-typevars? b)
(not (not (conform a b)))
(subtype? a b))
(or (subtype? a b)
(and (not (subtype? b a))
(has-typevars? b)))))
(define (method-table-insert! mt type method)
(vector-set! mt 0
(method-table-insert-p (mt:mlist mt)
type method type<=?)))
; --- generic functions ---
(define gf-type any-func)
(define (j-apply-generic ce args)
(let* ((argtype (make-tuple-type (map type-of args)))
(meth (best-method (vector-ref ce 0) argtype)))
(if meth
; applicable without conversion
((closure-proc (cdr meth)) (closure-env (cdr meth)) args)
(error "No method for function" (vector-ref ce 1) "matching types"
(map julia->string (type-params argtype))))))
(set! make-generic-function
(lambda (name)
;(display name) (newline)
(make-closure j-apply-generic (vector (make-method-table) name))))
(define (instantiate-generic-function gf env stack)
(let ((meths (map (lambda (p)
(cons (instantiate-type-- (car p) env stack)
(instantiate-method (cdr p) env)))
(mt:mlist (gf-mtable gf))))
(newgf (make-generic-function (gf-name gf))))
(for-each (lambda (p) (add-method-for newgf (car p) (cdr p)))
meths)
newgf))
(define (best-method methtable argtype)
(method-table-assoc methtable argtype))
(define (gf-mtable gf) (vector-ref (closure-env gf) 0))
(define (gf-name gf) (vector-ref (closure-env gf) 1))
; "convert" a type constructor to a type pattern by passing new
; typevars for all its parameters.
(define (tc->type tc)
(instantiate-type tc (map (lambda (tv)
(make-type-var (gensym)
(type-var-lb tv)
(type-var-ub tv)))
(type-ctor-params tc))))
; add a method for certain types
(set! add-method-for
(lambda (gf types meth)
(define (add-dummy-type-params t)
(cond ((type-ctor? t) (tc->type t))
((tuple? t) (map add-dummy-type-params t))
((union-type? t) (make-union-type (map add-dummy-type-params
(union-type-types t))))
((func-type? t) (make-function-type
(add-dummy-type-params (func-fromtype t))
(add-dummy-type-params (func-totype t))))
(else t)))
(method-table-insert! (gf-mtable gf)
(add-dummy-type-params types)
meth)
#t))
; --- define some key builtin types ---
(define Tensor-type
(let ((v (type-vars '(T n))))
(make-type-constructor v (make-tag-type 'Tensor Any-type v))))
(put-type 'Tensor Tensor-type)
(define scalar-type (instantiate-type Tensor-type (list Bottom-type
Bottom-type)))
(put-type 'Scalar scalar-type)
(define number-type (make-tag-type 'Number scalar-type julia-null))
(put-type 'Number number-type)
(define real-type (make-tag-type 'Real number-type julia-null))
(put-type 'Real real-type)
(define int-type (make-tag-type 'Int real-type julia-null))
(put-type 'Int int-type)
(define float-type (make-tag-type 'Float real-type julia-null))
(put-type 'Float float-type)
(define (make-scalar-type name super)
(make-struct-type name super julia-null julia-null julia-null
(lambda (type args)
(j-box type (if (number? (car args))
(car args)
(j-unbox (car args)))))))
(define bool-type (make-scalar-type 'Bool scalar-type))
(define int8-type (make-scalar-type 'Int8 int-type))
(define uint8-type (make-scalar-type 'Uint8 int-type))
(define int16-type (make-scalar-type 'Int16 int-type))
(define uint16-type (make-scalar-type 'Uint16 int-type))
(define int32-type (make-scalar-type 'Int32 int-type))
(define uint32-type (make-scalar-type 'Uint32 int-type))
(define int64-type (make-scalar-type 'Int64 int-type))
(define uint64-type (make-scalar-type 'Uint64 int-type))
(define float32-type (make-scalar-type 'Float32 float-type))
(define float64-type (make-scalar-type 'Float64 float-type))
(define buffer-type
(let ((v (type-vars '(T))))
(make-type-constructor
v
(make-struct-type 'Buffer Any-type v (julia-tuple 'length)
(julia-tuple int32-type)
(lambda (type args)
(make-buffer type (car args)))))))
(define (make-buffer type n)
(vector type n (make-vector (j-unbox n) 0)))
(put-type 'Bool bool-type)
(put-type 'Int8 int8-type)
(put-type 'Uint8 uint8-type)
(put-type 'Int16 int16-type)
(put-type 'Uint16 uint16-type)
(put-type 'Int32 int32-type)
(put-type 'Uint32 uint32-type)
(put-type 'Int64 int64-type)
(put-type 'Uint64 uint64-type)
(put-type 'Float32 float32-type)
(put-type 'Float64 float64-type)
(put-type 'Buffer buffer-type)
; --- true and false values ---
(define julia-true (vector bool-type 1))
(define julia-false (vector bool-type 0))
; --- type conversions ---
(define (convert-tuple x to)
(let loop ((cp (tuple->list x))
(pp (type-params to))
(result '()))
(let ((pseq (and (pair? pp) (sequence-type? (car pp)))))
(cond
((null? cp) (and (or (null? pp) pseq)
(list->tuple (reverse result))))
((null? pp) #f)
(else
(loop (cdr cp) (if pseq pp (cdr pp))
(cons (j-convert (car cp)
(if pseq
(type-param0 (car pp))
(car pp)))
result)))))))
(define (j-convert x to-type)
(if (and (tuple? x) (tuple? to-type))
(or (convert-tuple x to-type)
(error "Invalid tuple conversion"))
(let ((t (type-of x)))
(if (subtype? t to-type)
x
(let* ((m (j-get-field to-type 'convert))
(result (j-apply m (list x))))
(if (subtype? (type-of result) to-type)
result
(error "Conversion to" (julia->string to-type)
"failed")))))))
; --- builtin functions ---
(define (field-offset obj fld)
(let ((fl (type-field-names (type-of obj))))
(let loop ((i 0)
(L (tuple-length fl)))
(if (< i L)
(if (eq? fld (tuple-ref fl i))
(+ i 1)
(loop (+ i 1) L))
(error "Type" (type-name (type-of obj)) "has no field" fld)))))
(define (to-symbol x)
(if (symbol? x) x
(if (not (eq? (type-of x) Symbol-type))
(error "Expected symbol")
(vector-ref x 1))))
(define (j-get-field obj fld)
(vector-ref obj (field-offset obj (to-symbol fld))))
(define (j-set-field obj fld v)
(let ((i (field-offset obj (to-symbol fld))))
(vector-set! obj i
(j-convert v (tuple-ref (type-field-types (type-of obj))
(- i 1)))))
obj)
(define (j-tuple . args) (if (null? args) julia-null
(apply julia-tuple args)))
(define (j-tuple-ref v i)
(let ((i (j-unbox i)))
(if (= i 0) (error "Tuple index out of range")
(tuple-ref v (- i 1)))))
(define (j-buffer-length v) (j-get-field v 'length))
(define (buffer-data v) (vector-ref v 2))
(define *unboxable-types*
(list bool-type int8-type uint8-type int16-type uint16-type
int32-type uint32-type float32-type float64-type))
(define (unboxable-type? t)
(memq t *unboxable-types*))
(define (j-buffer-ref v i)
(let ((el (vector-ref (buffer-data v) (- (j-unbox i) 1))))
(if (not (vector? el))
(j-box (type-param0 (type-of v)) el)
el)))
(define (j-buffer-set v i rhs)
(let ((rhs (if (unboxable-type? (type-param0 (type-of v)))
(j-unbox rhs)
rhs)))
(vector-set! (buffer-data v) (- (j-unbox i) 1) rhs)))
(define (j-false? x)
(and (vector? x)
(eq? (vector-ref x 0) bool-type)
(= (vector-ref x 1) 0)))
(define (j-is x y) (eq? x y))
(define (j-box type . v)
(if (null? v)
(vector type *julia-unbound*)
(vector type (car v))))
(define (j-unbox v)
(let ((x (vector-ref v 1)))
(if (eq? x *julia-unbound*)
(error "Undefined closed variable")
x)))
(define (j-box-set b v) (begin (vector-set! b 1 v) julia-null))
; fix scalar type to include a proper int32(0)
; this creates a circular reference
(tuple-set! (vector-ref scalar-type 3) 1 (j-box int32-type 0))
; set element type of scalar to scalar
(tuple-set! (vector-ref scalar-type 3) 0 scalar-type)
#|
function ref(t::TypeConstructor, params...)
return instantiate_type(t, params)
end
|#
(let ((ref-gf (make-generic-function 'ref)))
(table-set! julia-globals 'ref ref-gf)
(add-method-for ref-gf
(julia-tuple TypeConstructor
(instantiate-type
sequence-type (list Any-type)))
(make-closure
(lambda (ce args)
(instantiate-type (car args) (cdr args)))
#f)))
; --- evaluator ---
(define *empty-env* '(()))
(define (make-numeric-literal n)
(if (not (flonum? n))
(j-box int32-type n)
(j-box float64-type n)))
(define (scm->julia x)
(cond ((symbol? x) x)
((null? x) x)
((number? x) (make-numeric-literal x))
((or (vector? x) (string? x)) x)
((atom? x) (error "cannot quote" x))
(else
(j-apply (eval-sym 'expr *empty-env*)
(map scm->julia x)))))
(define (julia->scm x)
(cond ((symbol? x) x)
((string? x) x)
((pair? x) (map julia->scm x))
((eq? (type-of x) bool-type) (if (j-false? x) 'false 'true))
((subtype? (type-of x) number-type) (j-unbox x))
((eq? (type-name (type-of x)) 'Expr)
(cons (julia->scm (j-get-field x 'head))
(map julia->scm (julialist->scmlist (j-get-field x 'args)))))
(else
(error "invalid syntax: " x (julia->string x)))))
(define (julialist->scmlist l)
(if (eq? (type-name (type-of l)) 'EmptyList)
'()
(cons (j-get-field l 'head)
(julialist->scmlist (j-get-field l 'tail)))))
(define *julia-unbound* (vector '*unbound*))
(define (eval-sym s env)
(let ((a (assq s (car env))))
(if a
(if (eq? (cdr a) *julia-unbound*)
(error "Undefined variable" s)
(cdr a))
(or (table-ref julia-globals s #f)
(error "Undefined variable" s)))))
(define (j-bound? s env)
(let ((b (assq s (car env))))
(if b
(not (eq? (cdr b) *julia-unbound*))
(table-ref julia-globals s #f))))
(define (j-eval e env)
(cond ((eq? e 'false) julia-false)
((eq? e 'true) julia-true)
((symbol? e) (eval-sym e env))
((number? e) (make-numeric-literal e))
((or (vector? e) (string? e)) e)
(else
(case (car e)
((quote) (scm->julia (cadr e)))
((null) julia-null)
((line) julia-null)
((top) (eval-sym (cadr e) *empty-env*))
((lambda) e) ; remaining lambdas are data
((unbound) ; check if identifier is bound
(if (not (j-bound? (cadr e) env))
julia-true julia-false))
((box-unbound)
(let ((x (vector-ref (j-eval (cadr e) env) 1)))
(if (eq? x *julia-unbound*)
julia-true julia-false)))
((closure-ref) (tuple-ref (cdr env) (cadr e)))