File: theme-d-type-translation.scm

package info (click to toggle)
theme-d 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,784 kB
  • sloc: lisp: 47,684; sh: 4,200; makefile: 455; ansic: 319
file content (837 lines) | stat: -rw-r--r-- 23,971 bytes parent folder | download
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
;; Copyright (C) 2008-2013 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.



;; *** Translation of type expressions ***


(import (rnrs exceptions)
	(srfi srfi-1)
	(th-scheme-utilities stdutils)
	(th-scheme-utilities hrecord))


(define gl-ctr 0)

(define make-constructor-fwd! '())

(define	make-parametrized-class-instance-fwd '())

(define contains-duplicate-field-names-fwd? '())

(define binder-get-instance-fwd '())

(define do-bind-type-vars1-fwd '())

(define get-union-of-types0-fwd '())


(define (is-tuple-type? binder to)
  (and
   (is-target-object? to)
   ;; The following test should not be strictly necessary.
   (not (eqv? to '()))
   (or (target-type=? to tc-nil)
       (and (is-tc-pair? to)
	    (is-type? binder (tt-car to))
	    (is-tuple-type? binder (tt-cdr to))))))


(set! is-tuple-type-fwd? is-tuple-type?)


(define (tuple-type-length binder tup)
  (assert (is-target-object? tup))
  (cond
   ((target-type=? tup tc-nil) 0)
   ((is-tc-pair? tup)
    (+ (tuple-type-length binder (tt-cdr tup)) 1))
   (else (raise 'tuple-type-length:error))))


;; The following procedure accepts markers in the tuple type.
(define (is-tuple-type1? binder to)
  (and
   (is-target-object? to)
   ;; The following test should not be strictly necessary.
   (not (eqv? to '()))
   (or (target-type=? to tc-nil)
       (and (is-tc-pair? to)
	    (let ((head (tt-car to)))
	      (is-type? binder head))
	    (is-tuple-type1? binder (tt-cdr to))))))


(define (is-tuple-type0? to)
  (and
   (is-target-object? to)
   ;; The following test should not be strictly necessary.
   (not (eqv? to '()))
   (or (target-type=? to tc-nil)
       (and (is-tc-pair? to)
	    (is-type0? (tt-car to))
	    (is-tuple-type0? (tt-cdr to))))))


(set! is-tuple-type0-fwd? is-tuple-type0?)


;; A general tuple is either a tuple with a tail
;; or an ordinary tuple.

(define (is-general-tuple-type? binder repr)
  (cond
   ;; Tuple types use <null> instead of null.
   ((null? repr) #f)
   ((not (hrecord? repr)) #f)
   ((eqv? repr tc-nil) #t)
   ((is-t-uniform-list-type2? binder repr) #t)
   ((is-tc-pair? repr)
    (and
     (is-type? binder (tt-car repr))
     (is-general-tuple-type? binder (tt-cdr repr))))
   (else #f)))


(set! is-general-tuple-type-fwd? is-general-tuple-type?)


;; This procedure returns the length of the fixed part when the argument
;; has a tail.
(define (general-tuple-type-length binder tup)
  (assert (is-target-object? tup))
  (cond
   ((target-type=? tup tc-nil) 0)
   ;; We accept type variables as the tail component type.
   ((is-t-uniform-list-type2? binder tup) 0)
   ((is-tc-pair? tup)
    (+ (general-tuple-type-length binder (tt-cdr tup)) 1))
   (else (raise 'general-tuple-type-length:error))))


(define (tuple-type->list-reject-cycles0 tuple-type visited)
  (cond
   ((or (null? tuple-type) (eqv? tuple-type tc-nil))
    '())
   ((memv tuple-type visited)
    (raise 'tuple-cycles-not-allowed))
   (else
    (cons (tt-car tuple-type)
	  (tuple-type->list-reject-cycles0
	   (tt-cdr tuple-type)
	   (cons tuple-type visited))))))


(define (tuple-type->list-reject-cycles tuple-type)
  (tuple-type->list-reject-cycles0 tuple-type '()))


(set! tuple-type->list-reject-cycles-fwd tuple-type->list-reject-cycles)


(define (gen-tuple-type->list0 binder tuple-type visited)
  (cond
   ((or (null? tuple-type) (eqv? tuple-type tc-nil))
    '())
   ((memv tuple-type visited)
    (raise 'tuple-cycles-not-allowed))
   ((is-t-uniform-list-type? binder tuple-type)
    (list (make-rest-object (get-uniform-list-param binder tuple-type))))
   (else
    (cons (tt-car tuple-type)
	  (gen-tuple-type->list0
	   binder
	   (tt-cdr tuple-type)
	   (cons tuple-type visited))))))


(define (gen-tuple-type->type-list binder tuple-type)
  (make-type-list-object (gen-tuple-type->list0 binder tuple-type '())))


(set! gen-tuple-type->type-list-fwd gen-tuple-type->type-list)


(define (tuple-type-ref tuple-type index)
  (assert (>= index 0))
  (cond
   ((or (null? tuple-type) (eqv? tuple-type tc-nil))
    (raise 'tuple-type-ref:index-out-of-range))
   ((= index 0)
    (tt-car tuple-type))
   (else
    (tuple-type-ref (tt-cdr tuple-type) (- index 1)))))


(define (make-irregular-union lst-members)
  (make-apti tmt-union lst-members))


(define (make-irregular-uniform-list type-args)
  (make-apti tplt-uniform-list type-args))


(define (translate-uniform-list-type-expression binder
						type-arg)
  (assert (is-binder? binder))
  (assert (is-entity? type-arg))
  (if (entity-is-none1? binder type-arg)
      (raise 'none-as-uniform-list-member-type)
      (let* ((type-arg-list (construct-toplevel-type-repr
			     binder
			     (list type-arg)))
	     (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	     (type-params
	      (if regular?
		  (tuple-type->list-reject-cycles type-arg-list)
		  '())))
	(if regular?
	    (make-tt-uniform-list (car type-params))
	    (make-irregular-uniform-list (list type-arg))))))


(set! translate-uniform-list-type-expression-fwd
      translate-uniform-list-type-expression)


(define (translate-irregular-pair-class args)
  (make-apti tpc-pair args))


(define (translate-pair-class-expression0 type-args)
  (assert (and (list? type-args)
	       (and-map? is-entity? type-args)))
  (assert (= (length type-args) 2))
  (make-tpci-pair (car type-args) (cadr type-args)))


(set! translate-pair-class-expression0-fwd translate-pair-class-expression0)


(define (translate-pair-class-expression binder
					 type-args)
  (assert (is-binder? binder))
  (assert (and (list? type-args)
	       (and-map? is-entity? type-args)))
  (let* ((type-arg-list (construct-toplevel-type-repr
			 binder
			 type-args))
	 (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	 (params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '())))
    (cond
     ((not regular?)
      (translate-irregular-pair-class type-args))
     ((not (= (length params) 2))
      (raise 'invalid-number-of-pair-component-types))
     ((or (null? (car params)) (null? (cadr params)))
      (raise 'corrupted-pair-class-structures))
     ((or (eqv? (car params) tt-none)
	  (eqv? (cadr params) tt-none))
      (raise 'type-parameter-none-in-pair-class))
     (else
      (make-tpci-pair (car params) (cadr params))))))
 

;; We do not allow type operations with tuple types.
;; Type lists (which are now called tuple-types)
;; may be used for that purpose.
(define (make-tuple-type . subexprs)
  (dvar1-set! subexprs)
  (assert (list? subexprs))
  (assert (and-map? is-target-object? subexprs))
  (if (null? subexprs)
      tc-nil
      (make-tpci-pair
       (car subexprs)
       (apply make-tuple-type (cdr subexprs)))))


(set! make-tuple-type-fwd
      make-tuple-type)


(define (translate-tuple-type-with-tail tuple-type tail-part)
  (assert (is-tuple-type0? tuple-type))
  (assert (is-target-object? tail-part))
  (if (or (null? tuple-type) (eqv? tuple-type tc-nil))
      tail-part
      (let* ((old-tail (tt-cdr tuple-type))
	     (new-tail (translate-tuple-type-with-tail old-tail
						       tail-part))
	     (tuple-head (tt-car tuple-type)))
	(if (eqv? new-tail old-tail)
	    tuple-type
	    (make-tpci-pair tuple-head new-tail)))))


(set! translate-tuple-type-with-tail-fwd
      translate-tuple-type-with-tail)


(define (join-two-tuple-types t1 t2)
  (assert (is-tuple-type0? t1))
  (assert (is-tuple-type0? t2))
  (if (or (null? t1) (eqv? t1 tc-nil))
      t2
      (let* ((old-tail (tt-cdr t1))
	     (new-tail (join-two-tuple-types old-tail t2))
	     (tuple-head (tt-car t1)))
	(if (eqv? new-tail old-tail)
	    t1
	    (make-tpci-pair tuple-head new-tail)))))


(define (join-tuple-types . tuple-types)
  (if (null? tuple-types)
      tc-nil
      (join-two-tuple-types (car tuple-types)
			    (apply join-tuple-types (cdr tuple-types)))))


(set! join-tuple-types-fwd join-tuple-types)


(define (translate-general-proc-type-expression0
	 simple?
	 arg-list-desc result-type pure?
	 always-returns? never-returns?
	 static-method?)
  (assert (boolean? simple?))
  (assert (is-target-object? arg-list-desc))
  (assert (is-target-object? result-type))
  (assert (boolean? pure?))
  (assert (boolean? always-returns?))
  (assert (boolean? never-returns?))
  (assert (boolean? static-method?))
  (make-tpti-general-proc simple? arg-list-desc result-type
			  pure? always-returns? never-returns?
			  static-method?))


(define (translate-general-proc-type-expression
	 binder simple?
	 arg-desc-exprs result-type-desc pure?
	 always-returns? never-returns?
	 static-method?)
  (assert (is-binder? binder))
  (assert (boolean? simple?))
  (assert (list? arg-desc-exprs))
  (assert (and-map? is-entity? arg-desc-exprs))
  (assert (is-entity? result-type-desc))
  (assert (boolean? pure?))
  (assert (boolean? always-returns?))
  (assert (boolean? never-returns?))
  (assert (boolean? static-method?))
  (let ((arg-list-desc
	 (construct-toplevel-type-repr binder arg-desc-exprs)))
    (translate-general-proc-type-expression0
     simple?
     arg-list-desc result-type-desc pure?
     always-returns? never-returns?
     static-method?)))


(define (translate-proc-type-expression
	 binder arg-desc-exprs result-type-desc pure?
	 always-returns? never-returns? static-method?)
  (translate-general-proc-type-expression
   binder #f arg-desc-exprs result-type-desc pure?
   always-returns? never-returns? static-method?))


(define (translate-simple-proc-class-expression
	 binder arg-desc-exprs result-type-desc pure?
	 always-returns? never-returns? static-method?)
  (translate-general-proc-type-expression
   binder #t arg-desc-exprs result-type-desc pure?
   always-returns? never-returns? static-method?))


(define tt-general-procedure
  (make-tpti-general-proc
   #f
   tt-list-of-objects
   tt-none
   #f #f #f #f))


(define tt-general-proc-with-value
  (make-tpti-general-proc
   #f
   tt-list-of-objects
   tc-object
   #f #f #f #f))


(define tt-general-simple-proc  
  (make-tpti-general-proc
   #t
   tt-list-of-objects
   tt-none
   #f #f #f #f))


(define tt-general-simple-proc-with-value
  (make-tpti-general-proc
   #t
   tt-list-of-objects
   tc-object
   #f #f #f #f))


(define tt-general-function
  (make-tpti-general-proc
   #f
   tt-list-of-objects
   tt-none
   #t #f #f #f))


(define tt-general-func-with-value
  (make-tpti-general-proc
   #f
   tt-list-of-objects
   tc-object
   #t #f #f #f))


;; A pure procedure with no result value has no effect
;; except possibly raising an exception.
(define tt-general-simple-func
  (make-tpti-general-proc
   #t
   tt-list-of-objects
   tt-none
   #t #f #f #f))


(define tt-general-simple-func-with-value
  (make-tpti-general-proc
   #t
   tt-list-of-objects
   tc-object
   #t #f #f #f))


(define (translate-vector-expression0 member-type)
  (make-tpci-vector member-type member-type))


(define (translate-vector-expression binder member-type-list)
  (let* ((type-arg-list (construct-toplevel-type-repr
			 binder
			 member-type-list))
	 (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '()))
	 (tc
	  (if regular?
	      (begin
		(strong-assert (= (length type-params) 1))
		(strong-assert (not (eqv? (car type-params) tt-none)))
		(translate-vector-expression0 (car type-params)))
	      (make-apti tpc-vector member-type-list))))
    tc))


(define (translate-mutable-vector-expression0 member-type)
  (make-tpci-mutable-vector member-type member-type))


(define (translate-mutable-vector-expression binder member-type-list)
  (let* ((type-arg-list (construct-toplevel-type-repr
			 binder
			 member-type-list))
	 (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '()))
	 (tc
	  (if regular?
	      (begin
		(strong-assert (= (length type-params) 1))
		(strong-assert (not (eqv? (car type-params) tt-none)))
		(translate-mutable-vector-expression0 (car type-params)))
	      (make-apti tpc-mutable-vector member-type-list))))
    tc))


(define (translate-value-vector-expression0 member-type)
  (make-tpci-value-vector member-type member-type))


(define (translate-value-vector-expression binder member-type-list)
  (let* ((type-arg-list (construct-toplevel-type-repr
			 binder
			 member-type-list))
	 (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '()))
	 (tc
	  (if regular?
	      (begin
		(strong-assert (= (length type-params) 1))
		(strong-assert (not (eqv? (car type-params) tt-none)))
		(translate-value-vector-expression0 (car type-params)))
	      (make-apti tpc-value-vector member-type-list))))
    tc))


(define (translate-mutable-value-vector-expression0 member-type)
  (make-tpci-mutable-value-vector member-type member-type))


(define (translate-mutable-value-vector-expression binder member-type-list)
  (let* ((type-arg-list (construct-toplevel-type-repr
			 binder
			 member-type-list))
	 (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '()))
	 (tc
	  (if regular?
	      (begin
		(strong-assert (= (length type-params) 1))
		(strong-assert (not (eqv? (car type-params) tt-none)))
		(translate-mutable-value-vector-expression0 (car type-params)))
	      (make-apti tpc-vector member-type-list))))
    tc))


;; Argument inside-param-def? is not used.
(define (translate-param-class-instance-expr binder param-class type-args
					     inside-param-def?
					     make-ctr?)
  (assert (is-binder? binder))
  (assert (is-target-object? param-class))
  (dvar1-set! type-args)
  (assert (and (list? type-args)
	       (and-map? is-entity? type-args)))
  (assert (boolean? inside-param-def?))
  (assert (boolean? make-ctr?))
  (let* ((type-arg-list (construct-toplevel-type-repr binder
						      type-args))
	 (tuple? (is-tuple-type? binder type-arg-list))
	 (regular?
	  (and
	   tuple?
	   (not (contains-type-modifiers-fwd? type-arg-list))))
	 ;; The following definition is unnecessary.
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '())))
    (cond
     ((not regular?)
      (make-apti param-class type-args))
     (tuple?
      (let ((type-params (tuple-type->list-reject-cycles type-arg-list)))
	(cond
	 ((hfield-ref param-class 'incomplete?)
	  (make-apti param-class type-params))
	 ;; We allow <none> as a value of a type parameters.
	 ;; ((and (not-null? type-params)
	 ;;       (or-map? entity-is-none? type-params))
	 ;;  (raise 'invalid-use-of-none-1))
	 (else
	  (let ((to
		 (binder-get-instance-fwd
		  binder param-class
		  type-params
		  make-ctr?)))
	    ;; Not sure if it is necessary to make the constructor
	    ;; here.
	    ;; Procedure binder-get-instance contains code
	    ;; to make the constructor.
	    (if (and make-ctr?
		     (tno-field-ref param-class 'instance-has-constructor?)
		     (hrecord? to)
		     (is-target-object? to)
		     (not (hfield-ref to 'incomplete?))
		     ;; There should be no need to test field type-constructor.
		     (null? (tno-field-ref to 'proc-constructor)))
		(make-constructor-fwd!
		 binder
		 to))
	    (if (not (hfield-ref to 'incomplete?))
		(tno-field-set! to 'l-param-exprs type-params))
	    to)))))
     (else
      ;; This is logically impossible.
      (raise 'internal-error)))))


(define (translate-param-sgn-instance-expr binder param-sgn type-args)
  (assert (is-binder? binder))
  (assert (is-target-object? param-sgn))
  (assert (and (list? type-args)
	       (and-map? is-entity? type-args)))
  (let* ((type-arg-list (construct-toplevel-type-repr binder
						      type-args))
	 (regular? (not (contains-type-modifiers-fwd? type-arg-list)))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '())))
    (cond
     ((not regular?) (make-apti param-sgn type-args))
     ((is-tuple-type? binder type-arg-list)
      (let ((type-params (tuple-type->list-reject-cycles type-arg-list)))
	(cond
	 ((hfield-ref param-sgn 'incomplete?)
	  (make-param-sgn-inst-object param-sgn type-params))
	 ;; We allow <none> as a value of a type parameter.
	 ;; ((and (not-null? type-params)
	 ;;       (or-map? entity-is-none? type-params))
	 ;;  (raise 'invalid-use-of-none-2))
	 (else
	  (let ((to
		 (binder-get-instance-fwd
		  binder param-sgn
		  type-params
		  #f)))
	    to)))))
     (else (raise 'invalid-param-signature-instance)))))


(define (translate-param-ltype-instance-expr binder param-ltype type-args)
  (assert (is-binder? binder))
  (assert (is-target-object? param-ltype))
  (assert (and (list? type-args)
	       (and-map? is-target-object? type-args)))
  (let* ((type-arg-list (construct-toplevel-type-repr binder
						      type-args))
	 (tuple? (is-tuple-type? binder type-arg-list))
	 (regular? 
	  (and
	   tuple?
	   (not (contains-type-modifiers-fwd? type-arg-list))))
	 (type-params
	  (if tuple?
	      (tuple-type->list-reject-cycles type-arg-list)
	      type-args)))
    (cond
     ((not regular?) (make-apti param-ltype type-args))
     (tuple?
      (cond
       ((hfield-ref param-ltype 'incomplete?)
	(make-apti param-ltype type-params))
       ;; <none> is allowed as a type parameter.
       ;; ((and (not-null? type-params)
       ;; 	     (or-map? entity-is-none? type-params))
       ;; 	(raise 'invalid-use-of-none-3))
       (else
	(let ((to
	       (binder-get-instance-fwd
		binder param-ltype
		type-params
		#f)))
	  to))))
     (else
      ;; This is logically impossible.
      (raise 'internal-error)))))


(define (translate-param-ltype-instance-expr2 binder param-ltype type-args
					      visited)
  (assert (is-binder? binder))
  (assert (is-target-object? param-ltype))
  (assert (and (list? type-args)
	       (and-map? is-target-object? type-args)))
  (let* ((type-arg-list (construct-toplevel-type-repr binder
						      type-args))
	 (tuple? (is-tuple-type? binder type-arg-list))
	 (regular? 
	  (and
	   tuple?
	   (not (contains-type-modifiers-fwd? type-arg-list))))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '())))
    (cond
     ((not regular?) (cons (make-apti param-ltype type-args) '()))
     (tuple?
      (cond
       ((hfield-ref param-ltype 'incomplete?)
	(cons (make-apti param-ltype type-params) '()))
       ;; ((and (not-null? type-params)
       ;; 	     (or-map? entity-is-none? type-params))
       ;; 	(raise 'invalid-use-of-none-4))
       ((eq? param-ltype tmt-union)
	(cons (get-union-of-types0-fwd binder type-params) '()))
       (else
	(let ((tvars (tno-field-ref param-ltype 'l-tvars)))
	  (if (not (= (length tvars) (length type-params)))
	      (raise 'invalid-number-of-type-parameters))
	  (let* ((plt-expr (tno-field-ref param-ltype 'x-value-expr))
		 (bindings (map cons tvars type-params))
		 (pr (do-bind-type-vars1-fwd
		      binder bindings plt-expr visited)))
	    pr)))))
     (else
      ;; This is logically impossible.
      (raise 'internal-error)))))


(define (translate-param-ltype-instance-expr3 binder param-ltype type-args
					      visited)
  (assert (is-binder? binder))
  (assert (is-target-object? param-ltype))
  (assert (and (list? type-args)
	       (and-map? is-target-object? type-args)))
  (let* ((type-arg-list (construct-toplevel-type-repr binder
						      type-args))
	 (tuple? (is-tuple-type? binder type-arg-list))
	 (regular? 
	  (and
	   tuple?
	   (not (contains-type-modifiers-fwd? type-arg-list))))
	 (type-params
	  (if regular?
	      (tuple-type->list-reject-cycles type-arg-list)
	      '())))
    (cond
     ((and
       (eq? param-ltype tmt-union)
       (= (length type-args) 1)
       (is-t-splice? (car type-args))
       (is-t-uniform-list-type?
	binder (tno-field-ref (car type-args) 'type-component)))
      (let ((tt-param
	     (get-uniform-list-param
	      binder (tno-field-ref (car type-args) 'type-component))))
	(cons tt-param '())))
     ((not regular?) (cons (make-apti param-ltype type-args) '()))
     (tuple?
      (cond
       ((let* ((param-cache (hfield-ref binder 'param-cache))
	       (binding (param-cache-fetch param-cache
					   param-ltype type-params)))
	  binding)
	=>
	(lambda (pr1)
	  (cons (cdr pr1) '())))
       ((hfield-ref param-ltype 'incomplete?)
	(cons (make-apti param-ltype type-params) '()))
       ;; ((and (not-null? type-params)
       ;; 	     (or-map? entity-is-none? type-params))
       ;; 	(raise 'invalid-use-of-none-5))
       ((eq? param-ltype tmt-union)
	(cons (get-union-of-types0-fwd binder type-params) '()))
       (else
	(let ((tvars (tno-field-ref param-ltype 'l-tvars)))
	  (if (not (= (length tvars) (length type-params)))
	      (raise 'invalid-number-of-type-parameters))
	  (let* ((plt-expr (tno-field-ref param-ltype 'x-value-expr))
		 (bindings (map cons tvars type-params))
		 (pr (do-bind-type-vars1-fwd
		      binder bindings plt-expr visited)))
	    pr)))))
     (else
      ;; This is logically impossible.
      (raise 'internal-error)))))


(define (get-param-def-type r-param-type)
  (let* ((type-type (get-entity-type r-param-type)))
    (cond
     ((eq? type-type t-param-class) 'class)
     ((eq? type-type t-param-logical-type) 'logical-type)
     ((eq? type-type tpc-param-proc) 'procedure) 
     ((eq? type-type t-param-signature) 'signature)
     (else 
      (dvar1-set! r-param-type)
      (raise 'internal-invalid-param-def-3)))))


(define (contains-incomplete-types? item)
  (and (is-target-object? item) (hfield-ref item 'incomplete?)))


(define (make-union-expression0 types)
  (assert (and-map? is-target-object? types))
  (apply make-tt-union types))


;; If any of the types is none return none.

(define (get-union-of-types0 binder types)
  (assert (is-binder? binder))
  ;; and-map? returns #t on empty argument list.
  (assert (and (list? types)
	       (and-map? is-target-object? types)))
  (cond
   ((or-map? (lambda (type)
	       (target-type=? type tt-none))
	     types)
    tt-none)
   ((or-map? contains-type-variables-fwd? types)
    (make-union-expression0 types))
   ((or-map? contains-incomplete-types? types)
    (make-union-expression0 types))
   (else
    (let* ((count (length types))
	   (vec-dupl (make-vector count #t))
	   (res '()))
      (do ((lst1 types (cdr lst1)) (i1 0 (+ i1 1)))
	  ((>= i1 count))
	(assert (not-null? lst1))
	(let ((cur-elem1 (car lst1)))
	  (do ((i2 (+ i1 1) (+ i2 1)))
	      ((>= i2 count))
	    (let ((cur-elem2 (list-ref types i2)))
	      (assert (not-null? cur-elem2))
	      (cond
	       ((and (vector-ref vec-dupl i2)
		     (is-t-subtype? binder cur-elem1 cur-elem2))
		(vector-set! vec-dupl i1 #f))
	       ((and (vector-ref vec-dupl i1)
		     (is-t-subtype? binder cur-elem2 cur-elem1))
		(vector-set! vec-dupl i2 #f)))))))
      (do ((i3 0 (+ i3 1)) (lst3 types (cdr lst3))) ((>= i3 count))
	(if (vector-ref vec-dupl i3)
	    (set! res (append res (list (car lst3))))))
      (let ((len (length res)))
	(cond
	 ((> len 1) (apply make-tt-union res))
	 ((= len 1) (car res))
	 ;; Formerly we had tt-none in the following.
	 ((= len 0) tc-nil)
	 (else (raise 'internal-error))))))))


(set! get-union-of-types0-fwd get-union-of-types0)


(define (get-union-of-types binder args)
  (assert (is-binder? binder))
  (let ((arg-list (construct-toplevel-type-repr
		    binder
		    args)))
    (if (is-t-type-variable? arg-list)
	(make-irregular-union (list (make-splice-object arg-list)))
	(let* ((regular?
		(not (contains-type-modifiers-fwd? arg-list)))
	       (params
		(if regular?
		    (tuple-type->list-reject-cycles arg-list)
		    '()))
	       (result
		(if regular?
		    (get-union-of-types0 binder
					 params)
		    (make-irregular-union args))))
	  result))))