-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtype.d
More file actions
1813 lines (1730 loc) · 55.7 KB
/
type.d
File metadata and controls
1813 lines (1730 loc) · 55.7 KB
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
// Written in the D programming language
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
module ast.type;
import astopt;
import std.format: format;
enum CaptureAnnotation{
none,
const_,
moved,
once,
spent,
}
static if(language==silq){
enum Annotation{
none,
mfree,
qfree,
}
enum deterministic=Annotation.mfree;
enum pure_=Annotation.qfree;
}else static if(language==psi){
enum Annotation{
none,
pure_,
}
enum deterministic=Annotation.pure_;
enum pure_=Annotation.pure_;
}
import std.array, std.algorithm, std.conv;
import std.functional, std.range;
import ast.expression, ast.declaration, util;
import ast.modules: isInPrelude;
enum NumericType{
none,
Bool,
ℕt,
ℤt,
ℚt,
ℝ,
ℂ,
}
NumericType isNumericTy(Expression t){
if(!t) return NumericType.none;
auto ty = cast(NumericTy)t;
if(!ty) return NumericType.none;
return ty.nty;
}
struct FixedIntTy {
Expression bits;
bool isSigned, isClassical;
bool opCast(T: bool)() const pure @safe nothrow {
return !!bits;
}
}
FixedIntTy isFixedIntTy(Expression e){
assert(e);
assert(e.isSemEvaluated());
auto ce=cast(CallExp)e;
if(!ce || !ce.isSquare) return FixedIntTy();
auto bits=ce.arg;
bool isClassical=ce.isClassical_;
auto id=cast(Identifier)ce.e;
if(!id||!id.meaning||!isInPrelude(id.meaning)) return FixedIntTy();
bool isSigned;
switch(id.name){
case "int":
isSigned=true;
break;
case "uint":
isSigned=false;
break;
default:
return FixedIntTy();
}
return FixedIntTy(bits, isSigned, isClassical);
}
bool isInt(Expression e){
if(auto ty=isFixedIntTy(e)) return ty.isSigned;
else return false;
}
bool isUint(Expression e){
if(auto ty=isFixedIntTy(e)) return !ty.isSigned;
else return false;
}
string preludeNumericTypeName(Expression e){
auto ce=cast(CallExp)e;
if(!ce || !ce.isSquare) return null;
auto id=cast(Identifier)ce.e;
if(!id||!id.meaning||!isInPrelude(id.meaning)) return null;
return id.name;
}
bool isFloat(Expression e){ return preludeNumericTypeName(e)=="float"; }
bool isRat(Expression e){ return preludeNumericTypeName(e)=="rat"; }
bool isSubtype(Expression lhs,Expression rhs){
if(!lhs||!rhs) return false;
assert(lhs.isSemEvaluated());
assert(rhs.isSemEvaluated());
if(lhs is rhs) return true;
if(lhs.isClassical()&&!rhs.isClassical()) {
rhs = rhs.getClassical();
if(!rhs) return false;
if(lhs is rhs) return true;
} else if(!lhs.isClassical()&&rhs.isClassical()) {
return false;
}
return lhs.isSubtypeImpl(rhs);
}
Expression combineTypes(Expression lhs,Expression rhs,bool meet,bool allowQNumeric=false){ // TODO: more general solution // TODO: ⊤/⊥?
if(!lhs||!rhs) return null;
if(isEmpty(lhs)) return meet?lhs:rhs;
if(isEmpty(rhs)) return meet?rhs:lhs;
if(lhs == rhs) return lhs;
auto l=lhs.eval(), r=rhs.eval();
if(isEmpty(lhs)) return meet?lhs:rhs;
if(isEmpty(rhs)) return meet?rhs:lhs;
auto result = l.combineTypesImpl(r,meet);
if(!allowQNumeric && isQNumeric(result)) return null;
return result;
}
Expression joinTypes(Expression lhs,Expression rhs){
return combineTypes(lhs,rhs,false);
}
Expression meetTypes(Expression lhs,Expression rhs){
return combineTypes(lhs,rhs,true);
}
abstract class Type: Expression{
override @property string kind(){ return "type"; }
override string toString(){ return "T"; }
abstract override bool opEquals(Object r);
override Annotation getAnnotation(){ return pure_; }
}
class NumericTy: Type{
private NumericType nty;
static if(language==silq) {
private bool classical;
private this(NumericType nty, bool classical){
assert(nty);
assert(!theNumeric[nty][classical]);
this.nty = nty;
this.classical = classical;
this.type = classical ? ctypeTy() : nty == NumericType.Bool ? qtypeTy() : qnumericTy();
setSemEvaluated();
}
} else {
private enum classical=true;
private this(NumericType nty) {
assert(!theNumeric[nty]);
this.nty = nty;
this.type = typeTy();
setSemEvaluated();
}
}
override NumericTy getClassical(){
if(this.classical) return this;
return numericTy(this.nty, true);
}
override NumericTy getQuantum(){
if(!this.classical) return this;
return numericTy(this.nty, false);
}
override Expression combineTypesImpl(Expression r, bool meet){
auto ty = cast(NumericTy)r;
if(!ty) return null;
if(meet) return numericTy(min(nty, ty.nty), classical || ty.classical);
return numericTy(max(nty, ty.nty), classical && ty.classical);
}
override bool isSubtypeImpl(Expression r){
auto ty = cast(NumericTy)r;
if(!ty) return false;
assert(ty !is this);
return nty <= ty.nty && classical >= ty.classical;
}
override NumericTy copyImpl(CopyArgs args){
return this;
}
override string toString(){
size_t i = 1;
static if(language==silq) {
i = !classical;
}
final switch(nty) {
case NumericType.none: assert(0);
case NumericType.Bool: return "!𝔹"[i..$];
case NumericType.ℕt: return "!ℕ"[i..$];
case NumericType.ℤt: return "!ℤ"[i..$];
case NumericType.ℚt: return "!ℚ"[i..$];
case NumericType.ℝ: return "!ℝ"[i..$];
case NumericType.ℂ: return "!ℂ"[i..$];
}
}
override bool isConstant(){ return true; }
override bool isTotal(){ return true; }
override bool opEquals(Object o){
return o is this;
}
override Expression evalImpl(){ return this; }
mixin VariableFree;
override int componentsImpl(scope int delegate(Expression) dg){
return 0;
}
}
static if(language==silq) private NumericTy[2][7] theNumeric;
else private NumericTy[7] theNumeric;
NumericTy Bool(bool classical=true){
return numericTy(NumericType.Bool, classical);
}
NumericTy ℕt(bool classical=true){
return numericTy(NumericType.ℕt, classical);
}
NumericTy ℤt(bool classical=true){
return numericTy(NumericType.ℤt, classical);
}
NumericTy ℚt(bool classical=true){
return numericTy(NumericType.ℚt, classical);
}
NumericTy ℝ(bool classical=true){
return numericTy(NumericType.ℝ, classical);
}
NumericTy ℂ(bool classical=true){
return numericTy(NumericType.ℂ, classical);
}
NumericTy numericTy(NumericType which, bool classical){
if(!which) return null;
static if(language==silq){
return theNumeric[which][classical] ? theNumeric[which][classical] : (theNumeric[which][classical] = new NumericTy(which, classical));
} else {
return theNumeric[which] ? theNumeric[which] : (theNumeric[which] = new NumericTy(which));
}
}
class AggregateTy: Type{
DatDecl decl;
static if(language==silq){
bool classical;
private AggregateTy classicalTy;
private AggregateTy quantumTy;
}else enum classical=true;
this(DatDecl decl,bool classical,AggregateTy classicalTy=null,AggregateTy quantumTy=null){
if(!classical) assert(decl.isQuantum);
this.decl=decl;
static if(language==silq){
this.classical=classical;
if(classical) this.classicalTy=this;
else this.classicalTy=classicalTy?classicalTy:New!AggregateTy(decl,true,null,decl.isQuantum?this:null);
if(decl.isQuantum){
if(!classical) this.quantumTy=this;
else this.quantumTy=quantumTy?quantumTy:New!AggregateTy(decl,false,this,null);
}
}
this.type=typeTy(); // TODO
setSemEvaluated();
}
override AggregateTy copyImpl(CopyArgs args){
return this;
}
override bool opEquals(Object o){
if(o is this) return true;
if(auto r=cast(AggregateTy)o)
return decl is r.decl && classical==r.classical;
return false;
}
override string toString(){
return decl&&decl.name?decl.name.name:"<anonymous aggregate>";
}
override bool isConstant(){ return true; }
override bool isTotal(){ return true; }
override AggregateTy getClassical(){
static if(language==silq) return classicalTy;
else return this;
}
override AggregateTy getQuantum(){
static if(language==silq) return quantumTy;
else return this;
}
override Expression evalImpl(){ return this; }
mixin VariableFree;
override int componentsImpl(scope int delegate(Expression) e){
return 0;
}
}
class ContextTy: Type{
static if(language==silq) private bool classical;
else private enum classical=true;
private this(bool classical){
static if(language==silq) this.classical=classical;
this.type = classical ? ctypeTy() : typeTy();
setSemEvaluated();
}
override ContextTy copyImpl(CopyArgs args){
return this;
}
override bool isConstant(){ return true; }
override bool isTotal(){ return true; }
override bool opEquals(Object o){
if(o is this) return true;
auto ctx=cast(ContextTy)o;
return ctx&&ctx.classical==classical;
}
override string toString(){
static if(language==silq) return (classical?"!":"")~"`Ctx";
else return "`Ctx";
}
override ContextTy getClassical(){
return contextTy(true);
}
override Expression evalImpl(){ return this; }
mixin VariableFree;
override int componentsImpl(scope int delegate(Expression) e){
return 0;
}
}
static if(language==silq) private ContextTy[2] theContextTy;
else private ContextTy theContextTy;
ContextTy contextTy(bool classical=true){
static if(language==silq) return theContextTy[classical]?theContextTy[classical]:(theContextTy[classical]=new ContextTy(classical));
else return theContextTy?theContextTy:(theContextTy=new ContextTy(true));
}
class BottomTy: Type{
this(){
type = etypeTy;
setSemEvaluated();
}
override BottomTy copyImpl(CopyArgs args){
return this;
}
override string toString(){
return "⊥";
}
override bool isConstant(){ return true; }
override bool isTotal(){ return true; }
override bool opEquals(Object o){
auto bot=cast(BottomTy)o;
return !!bot;
}
override bool isSubtypeImpl(Expression r){
return true;
}
override Expression combineTypesImpl(Expression r,bool meet){
return meet?this:r;
}
override BottomTy getClassical(){
return this;
}
override Expression evalImpl(){ return this; }
mixin VariableFree;
override int componentsImpl(scope int delegate(Expression) e){
return 0;
}
}
private BottomTy theBottomTy;
BottomTy bottom(){
return theBottomTy?theBottomTy:(theBottomTy=new BottomTy());
}
class ClassicalTy: Expression{
Expression inner;
this(Expression inner){
this.inner = inner;
}
override string toString(){
return "!" ~ inner.toString();
}
override ClassicalTy copyImpl(CopyArgs args){
return new ClassicalTy(inner.copy(args));
}
override int componentsImpl(scope int delegate(Expression) dg){
return dg(inner);
}
override int freeVarsImpl(scope int delegate(Identifier) dg){
return inner.freeVarsImpl(dg);
}
override Expression substituteImpl(Expression[Id] subst){
return new ClassicalTy(inner.substitute(subst));
}
override bool unifyImpl(Expression rhs,ref UnificationResult[Id] subst,bool meet){
assert(false);
}
override Annotation getAnnotation(){
return pure_;
}
override Expression evalImpl(){
return inner.eval().getClassical();
}
}
interface ITupleTy{
@property size_t length();
Expression opIndex(size_t i);
Expression opSlice(size_t l,size_t r);
}
class TupleTy: Type,ITupleTy{
Expression[] types;
override ITupleTy isTupleTy(){ return this; }
@property size_t length(){ return types.length; }
Expression opIndex(size_t i){ return types[i]; }
Expression opSlice(size_t l,size_t r){ return tupleTy(types[l..r]); }
this(Expression[] types)in{
assert(types.all!());
}do{
this.types=types;
}
override TupleTy copyImpl(CopyArgs args){
return new TupleTy(types.map!(ty => ty.copy(args)).array);
}
override string toString(){
if(!types.length) return "𝟙";
if(types.length==1) return "("~types[0].toString()~")¹";
string addp(Expression a){
if(cast(FunTy)a) return "("~a.toString()~")";
return a.toString();
}
return types.map!(a=>a.isTupleTy()&&a!=unit?"("~a.toString()~")":addp(a)).join(" × ");
}
override bool isConstant(){ return types.all!(ty=>ty.isConstant()); }
override bool isTotal(){ return types.all!(ty=>ty.isTotal()); }
override int freeVarsImpl(scope int delegate(Identifier) dg){
foreach(t;types)
if(auto r=t.freeVarsImpl(dg))
return r;
return 0;
}
override Type substituteImpl(Expression[Id] subst){
auto ntypes=types.dup;
foreach(ref t;ntypes) t=t.substitute(subst);
return tupleTy(ntypes);
}
override bool unifyImpl(Expression rhs,ref UnificationResult[Id] subst,bool meet){
auto tt=rhs.isTupleTy();
if(!tt||types.length!=tt.length) return false;
return all!(i=>types[i].unify(tt[i],subst,meet))(iota(types.length));
}
override bool opEquals(Object o){
if(o is this) return true;
if(auto r=cast(TupleTy)o)
return types==r.types;
return false;
}
override bool isSubtypeImpl(Expression r){
auto ltup=this,rtup=r.isTupleTy();
if(rtup&<up.types.length==rtup.length)
return all!(i=>isSubtype(ltup.types[i],rtup[i]))(iota(ltup.types.length));
auto rarr=cast(ArrayTy)r;
if(rarr) return all!(i=>isSubtype(ltup.types[i],rarr.next))(iota(ltup.types.length));
return false;
}
override Expression combineTypesImpl(Expression r,bool meet){
auto ltup=this,rtup=r.isTupleTy();
if(rtup&<up.types.length==rtup.length){
auto rtypes=zip(ltup.types,iota(rtup.length).map!(i=>rtup[i])).map!((t)=>combineTypes(t.expand,meet)).array;
if(all!(x=>x !is null)(rtypes)) return tupleTy(rtypes);
}
auto rarr=cast(ArrayTy)r;
if(!rarr&&!meet){
if(auto rvec=cast(VectorTy)r)
rarr=arrayTy(rvec.next);
if(rtup&&!meet){
if(auto next=chain(ltup.types,ltup.types).fold!((a,b)=>combineTypes(a,b,meet))(cast(Expression)bottom))
rarr=arrayTy(next);
}
}
if(rarr){
if(meet){
auto rtypes=zip(ltup.types,iota(length).map!(i=>rarr.next)).map!((t)=>combineTypes(t.expand,meet)).array;
if(all!(x=>x !is null)(rtypes)) return tupleTy(rtypes);
}else{
auto rtype=ltup.types.fold!((a,b)=>combineTypes(a,b,meet))(rarr.next);
if(rtype) return arrayTy(rtype);
}
}
return null;
}
override Expression getClassical(){
auto ntypes=types.map!(x=>x.getClassical()).array;
if(all!(x=>x !is null)(ntypes)) return tupleTy(ntypes);
return null;
}
override Expression getQuantum(){
auto ntypes=types.map!(x=>x.getQuantum()).array;
if(all!(x=>x !is null)(ntypes)) return tupleTy(ntypes);
return null;
}
override bool mayBeClassical(){ return types.all!(x=>x.mayBeClassical()); }
override bool mayBeQuantum(){ return types.all!(x=>x.mayBeQuantum()); }
override int componentsImpl(scope int delegate(Expression) dg){
foreach(x;types) if(auto r=dg(x)) return r;
return 0;
}
override Expression evalImpl(){
assert(isTypeTy(type) || isQNumericTy(type));
auto ntypes=types.map!(t=>t.eval()).array;
if(iota(types.length).all!(i => ntypes[i] is types[i])) return this;
return tupleTy(ntypes);
}
}
Type unit(){ return tupleTy([]); }
Type tupleTy(Expression[] types)in{
assert(types.all!(e=>isType(e)||isQNumeric(e)));
assert(types.all!(e=>e.isSemEvaluated()));
}do{
import ast.lexer: Token,Tok;
if(types.length&&types.all!(x=>x==types[0])){
return vectorTy(types[0], types.length);
}
return memoize!((Expression[] types){
auto r = new TupleTy(types);
r.type = typeOfTupleTy(r.types);
r.setSemEvaluated();
return r;
})(types);
}
size_t numComponents(Expression t){
if(auto tpl=t.isTupleTy())
return tpl.length;
return 1;
}
class ArrayTy: Type{
Expression next;
this(Expression next)in{
assert(next);
}do{
this.next=next;
}
override ArrayTy copyImpl(CopyArgs args){
return new ArrayTy(next.copy(args));
}
override string toString(){
bool p=cast(FunTy)next||next.isTupleTy()&&next!=unit;
return p?"("~next.toString()~")[]":next.toString()~"[]";
}
override bool isConstant(){ return next.isConstant(); }
override bool isTotal(){ return next.isTotal(); }
override int freeVarsImpl(scope int delegate(Identifier) dg){
return next.freeVarsImpl(dg);
}
override ArrayTy substituteImpl(Expression[Id] subst){
return arrayTy(next.substitute(subst));
}
override bool unifyImpl(Expression rhs,ref UnificationResult[Id] subst,bool meet){
if(auto vt=cast(VectorTy)rhs)
return next.unify(vt.next,subst,meet);
if(auto tt=cast(TupleTy)rhs)
return tt.types.all!(ty=>next.unify(ty,subst,meet));
if(auto at=cast(ArrayTy)rhs)
return next.unify(at.next,subst,meet);
return false;
}
override ArrayTy evalImpl(){
assert(isTypeTy(type) || isQNumericTy(type));
return arrayTy(next.eval());
}
override bool opEquals(Object o){
if(auto r=cast(ArrayTy)o)
return next==r.next;
return false;
}
override bool isSubtypeImpl(Expression r){
auto larr=this,rarr=cast(ArrayTy)r;
if(!rarr) return false;
return isSubtype(larr.next,rarr.next);
}
override Expression combineTypesImpl(Expression r,bool meet){
auto larr=this,rarr=cast(ArrayTy)r;
if(rarr){
auto combinedNext=combineTypes(larr.next,rarr.next,meet);
if(combinedNext) return arrayTy(combinedNext);
}
if(auto rvec=cast(VectorTy)r){
auto nnext=combineTypes(next,rvec.next,meet);
if(nnext){
if(meet) return vectorTy(nnext,rvec.num);
else return arrayTy(nnext);
}
}
if(auto rtup=r.isTupleTy()){
if(meet){
auto ntypes=iota(rtup.length).map!(i=>combineTypes(next,rtup[i],meet)).array;
if(all!(x=>x is null)(ntypes)) return tupleTy(ntypes);
}else{
auto rtype=iota(rtup.length).map!(i=>rtup[i]).fold!((a,b)=>a?combineTypes(a,b,meet):null)(next);
if(rtype) return arrayTy(rtype);
}
}
return null;
}
override Expression getClassical(){
auto nnext=next.getClassical();
if(!nnext) return null;
return arrayTy(nnext);
}
override Expression getQuantum(){
return null; // length is classical
}
override bool mayBeClassical(){ return next.mayBeClassical(); }
override bool mayBeQuantum(){ return false; } // TODO: ok?
override int componentsImpl(scope int delegate(Expression) dg){
return dg(next);
}
}
ArrayTy arrayTy(Expression next)in{
assert(isType(next)||isQNumeric(next));
assert(next.isSemEvaluated());
}do{
return memoize!((Expression next){
auto r = new ArrayTy(next);
r.type = typeOfArrayTy(next);
r.setSemEvaluated();
return r;
})(next);
}
class VectorTy: Type, ITupleTy{
Expression next,num;
this(Expression next,Expression num)in{
assert(next);
assert(num);
}do{
this.next=next;
this.num=num;
}
override ITupleTy isTupleTy(){
if(auto len=num.asIntegerConstant())
if(len.get()<=size_t.max) return this;
return null;
}
override VectorTy copyImpl(CopyArgs args){
return new VectorTy(next.copy(args), num.copy(args));
}
@property size_t length(){
auto lit=cast(LiteralExp)num;
assert(!!lit);
return lit.asIntegerConstant().get().to!size_t(); // TODO: avoid crash if length is too big
}
Expression opIndex(size_t i){ return next; }
Expression opSlice(size_t l,size_t r){
assert(0<=l&&l<=r&&r<=length);
auto len=LiteralExp.makeInteger(r-l);
return vectorTy(next,len);
}
override string toString(){
bool p=cast(FunTy)next||next.isTupleTy&&next!=unit;
bool q=!cast(Identifier)num&&!cast(LiteralExp)num; // TODO: improve
return (p?"("~next.toString()~")^":next.toString()~"^")~(q?"("~num.toString()~")":num.toString());
}
override bool isConstant(){ return next.isConstant() && num.isConstant(); }
override bool isTotal(){ return next.isTotal() && num.isTotal(); }
override int freeVarsImpl(scope int delegate(Identifier) dg){
if(auto r=next.freeVarsImpl(dg)) return r;
return num.freeVarsImpl(dg);
}
override VectorTy substituteImpl(Expression[Id] subst){
return vectorTy(next.substitute(subst),num.substitute(subst));
}
override bool unifyImpl(Expression rhs,ref UnificationResult[Id] subst,bool meet){
if(auto tt=cast(TupleTy)rhs)
return tt.types.all!(ty=>next.unify(ty,subst,meet)) && num.unify(LiteralExp.makeInteger(tt.length),subst,meet);
if(auto vt=cast(VectorTy)rhs){
auto r=next.unify(vt.next,subst,meet);
if(auto nlit=num.asIntegerConstant())
if(nlit.get()==0&&num==vt.num) return true;
return r && num.unify(vt.num,subst,meet);
}
return false;
}
override VectorTy evalImpl(){
assert(isTypeTy(type) || isQNumericTy(type));
return vectorTy(next.eval(),num.eval());
}
override bool opEquals(Object o){
if(auto r=cast(VectorTy)o)
return next==r.next&&num==r.num;
return false;
}
override bool isSubtypeImpl(Expression r){
if(auto rarr=cast(ArrayTy)r) return isSubtype(next,rarr.next);
auto lvec=this,rvec=cast(VectorTy)r;
if(rvec){
if(num!=rvec.num) return false;
if(auto nlit=num.asIntegerConstant())
if(nlit.get()==0) return true;
return isSubtype(lvec.next,rvec.next);
}
auto ltup=this.isTupleTy(),rtup=r.isTupleTy();
if(ltup&&rtup&<up.length==rtup.length)
return all!(i=>isSubtype(ltup[i],rtup[i]))(iota(ltup.length));
return false;
}
override Expression combineTypesImpl(Expression r,bool meet){
if(auto rarr=cast(ArrayTy)r){
auto nnext=combineTypes(next,rarr.next,meet);
if(nnext){
if(meet) return vectorTy(nnext,num);
else return arrayTy(nnext);
}
}
auto lvec=this,rvec=cast(VectorTy)r;
if(rvec){
auto nnext=combineTypes(lvec.next,rvec.next,meet);
if(!nnext) return null;
if(num==rvec.num){
return vectorTy(nnext,num);
}else if(!meet){
return arrayTy(nnext);
}else return null;
}
auto ltup=this.isTupleTy(),rtup=r.isTupleTy();
if(rtup){
bool equal=false;
if(ltup) equal=ltup.length==rtup.length;
else equal=num==LiteralExp.makeInteger(rtup.length);
if(equal){
auto rtypes=iota(rtup.length).map!(i=>combineTypes(next,rtup[i],meet)).array;
if(all!(x=>x !is null)(rtypes)) return tupleTy(rtypes);
}else if(!meet){
auto nnext=iota(rtup.length).map!(i=>rtup[i]).fold!((a,b)=>combineTypes(a,b,meet))(next);
if(nnext) return arrayTy(nnext);
}else return null;
}
return null;
}
override Expression getClassical(){
auto nnext=next.getClassical();
if(!nnext) return null;
return vectorTy(nnext,num);
}
override Expression getQuantum(){
auto nnext=next.getQuantum();
if(!nnext){
if(isZero(num)) return unit;
return null;
}
return vectorTy(nnext,num);
}
override bool mayBeClassical(){ return next.mayBeClassical(); }
override bool mayBeQuantum(){ return next.mayBeQuantum(); }
override int componentsImpl(scope int delegate(Expression) dg){
if(auto r=dg(next)) return r;
return dg(num);
}
}
VectorTy vectorTy(Expression next,Expression num)in{
assert(isType(next)||isQNumeric(next));
assert(next.isSemEvaluated(), format("unevaluated vector item type %s", next));
assert(num&&isSubtype(num.type,ℕt(true)));
assert(num.isSemEvaluated(), format("unevaluated vector length %s", num));
}do{
return memoize!((Expression next,Expression num){
auto r = new VectorTy(next,num);
r.type = typeOfVectorTy(next, num);
r.setSemEvaluated();
return r;
})(next,num);
}
VectorTy vectorTy(Expression next, size_t num){
return vectorTy(next, LiteralExp.makeInteger(num));
}
static Expression elementType(Expression ty){
if(auto at=cast(ArrayTy)ty) return at.next;
if(auto vt=cast(VectorTy)ty) return vt.next;
return null;
}
class StringTy: Type{
static if(language==silq) bool classical;
else enum classical=true;
private this(bool classical){
this.type=typeOfStringTy(classical);
setSemEvaluated();
}
override StringTy copyImpl(CopyArgs args){
return this;
}
override string toString(){
static if(language==silq) return classical?"!string":"string";
else return "string";
}
override bool isConstant(){ return true; }
override bool isTotal(){ return true; }
override bool opEquals(Object o){ return o is this; }
override Expression evalImpl(){ return this; }
mixin VariableFree;
override int componentsImpl(scope int delegate(Expression) dg){
return 0;
}
}
StringTy stringTy(bool classical=true){
static if(language==silq) return memoize!((bool classical)=>new StringTy(classical))(classical);
else return memoize!(()=>new StringTy(true));
}
string captureAnnotationToString(CaptureAnnotation captureAnnotation){
return captureAnnotation?captureAnnotation==CaptureAnnotation.const_?"const":text(captureAnnotation):"";
}
bool captureAnnotationSubtype(CaptureAnnotation captureAnnotation1,CaptureAnnotation captureAnnotation2){
if(captureAnnotation1==captureAnnotation2) return true;
if(!captureAnnotation1) return true;
if(!captureAnnotation2) return false;
if(captureAnnotation1==CaptureAnnotation.const_||captureAnnotation1==CaptureAnnotation.moved)
if(captureAnnotation2==CaptureAnnotation.once)
return true;
if(captureAnnotation1==CaptureAnnotation.const_&&captureAnnotation2==CaptureAnnotation.spent)
return true;
return false;
}
bool captureAnnotationCombinable(CaptureAnnotation captureAnnotation1,CaptureAnnotation captureAnnotation2,bool meet){
if(captureAnnotation1==captureAnnotation2) return true;
if(!meet){
if(!captureAnnotation1||!captureAnnotation2) return true;
if(captureAnnotation1==CaptureAnnotation.spent||captureAnnotation2==CaptureAnnotation.spent) return false;
return true; // const, moved, once are intercombinable
}else{
// if(!captureAnnotation1||!captureAnnotation2) return true; // TODO?
if(!captureAnnotation1||!captureAnnotation2) return false;
if(captureAnnotation1==CaptureAnnotation.spent||captureAnnotation2==CaptureAnnotation.spent) return false;
if(captureAnnotation1==CaptureAnnotation.once||captureAnnotation2==CaptureAnnotation.once) return true;
return false; // TODO: combine `const` and `moved` to `none`?
}
}
CaptureAnnotation combineCaptureAnnotation(CaptureAnnotation captureAnnotation1,CaptureAnnotation captureAnnotation2,bool meet)in{
assert(captureAnnotationCombinable(captureAnnotation1,captureAnnotation2,meet));
}do{
if(captureAnnotation1==captureAnnotation2) return captureAnnotation1;
if(!meet){
if(!captureAnnotation1) return captureAnnotation2;
if(!captureAnnotation2) return captureAnnotation1;
assert(captureAnnotation1!=CaptureAnnotation.spent&&captureAnnotation2!=CaptureAnnotation.spent);
return CaptureAnnotation.once;
}else{
if(!captureAnnotation1||!captureAnnotation2) return CaptureAnnotation.none;
assert(captureAnnotation1!=CaptureAnnotation.spent&&captureAnnotation2!=CaptureAnnotation.spent);
assert(captureAnnotation1==CaptureAnnotation.once||captureAnnotation2==CaptureAnnotation.once);
if(captureAnnotation1==CaptureAnnotation.once) return captureAnnotation2;
if(captureAnnotation2==CaptureAnnotation.once) return captureAnnotation1;
assert(0);
}
}
string annotationToString(Annotation annotation){
static if(language==silq) return annotation?text(annotation):"";
static if(language==psi){
final switch(annotation){
case Annotation.none: return "";
case Annotation.pure_: return "pure";
}
}
}
class ProductTy: Type{
Parameter[] params;
bool[] isConst;
Id[] names;
Expression dom, cod; // `dom` set by semantic analysis
bool isSquare,isTuple;
CaptureAnnotation captureAnnotation;
Annotation annotation;
static if(language==silq){
private ProductTy classicalTy;
bool isClassical_;
}else enum isClassical_=true;
CaptureAnnotation captureKind(){ return isClassical_ ? CaptureAnnotation.none : captureAnnotation; }
this(Parameter[] params, Expression cod, bool isSquare, bool isTuple, CaptureAnnotation captureAnnotation, Annotation annotation, bool isClassical_)in{
assert(cod);
// TODO: assert that all names are distinct
}do{
this.params = params;
this.isConst = params.map!(p => p.isConst).array;
this.names = params.map!(p => p.name ? p.getId : Id()).array;
this.isSquare = isSquare;
this.isTuple = isTuple;
this.cod = cod;
this.captureAnnotation = captureAnnotation;
this.annotation = annotation;
static if(language==silq){
this.isClassical_=isClassical_;
}
}
override void setSemCompleted() {
assert(dom && dom.isSemEvaluated(), format("completed semantic analysis of product type without domain: %s", this));
assert(cod && cod.isSemCompleted(), format("completed semantic analysis of product type without codomain: %s", this));
super.setSemCompleted();
}
override ProductTy copyImpl(CopyArgs args){
auto r = new ProductTy(params.map!(p=>p.copy(args)).array, cod.copy(args), isSquare, isTuple, captureAnnotation, annotation, isClassical_);
if(args.preserveSemantic) {
r.dom = dom;
}
return r;
}
/+private+/ @property ITupleTy tdom()in{ // TODO: make private
assert(isTuple);
assert(dom);
}do{
auto r=dom.isTupleTy;
assert(!!r);
return r;
}
override string toString(){
auto c=cod ? cod.toString() : "<missing codomain>";
auto del=isSquare?"[]":"()";
string getParamKind(bool const_){
string paramKind=null;
static if(language==silq){
if(const_&&!isSquare) paramKind="const ";
if(!const_&&isSquare) paramKind="moved ";
}
return paramKind;
}
string r;
if(cod && !cod.hasAnyFreeVar(names)){
string d;
string addp(bool const_,Expression a,string del="()"){
auto paramKind=getParamKind(const_);
if(cast(ProductTy)a) return del[0]~(paramKind?paramKind~"(":"")~a.toString()~(paramKind?")":"")~del[1];
if(a.isTupleTy()) return (paramKind?paramKind~"(":"")~a.toString()~(paramKind?")":"");
return paramKind~a.toString();
}
d=params.empty?dom.toString():params.map!((p){
auto paramKind=getParamKind(p.isConst);
auto pty = p.vtype ? p.vtype : p.dtype;
auto ptup = pty.isTupleTy();
return (ptup && ptup.length > 1) ? "("~paramKind~pty.toString()~")" : addp(p.isConst, pty);
}).join(" × ");
if(isTuple && params.length == 1) {
d="("~d~")¹";
}
static if(language==silq) auto arrow=(isClassical_?"!":"")~"→";
else enum arrow="→";
if(isSquare) d=del[0]~d~del[1];
r=d~" "~(captureAnnotation!=CaptureAnnotation.const_?captureAnnotationToString(captureAnnotation):"")~arrow~(annotation?annotationToString(annotation):"")~" "~c;
}else{
string args;
args=params.map!((p){
auto paramKind=getParamKind(p.isConst);
auto pty = p.vtype ? p.vtype : p.dtype;
return paramKind~(p.name ? p.name.toString() : "_")~":"~pty.toString();
}).join(",");
static if(language==silq) auto pi=(isClassical_?"!":"")~"∏";
else enum pi="Π";
r=pi~del[0]~args~del[1]~(captureAnnotation!=CaptureAnnotation.const_?captureAnnotationToString(captureAnnotation):"")
~(captureAnnotation!=CaptureAnnotation.const_&&annotation?" ":"")~annotationToString(annotation)~". "~c;
}
return r;
}
override bool isConstant(){ return dom.isConstant() && cod.isConstant(); }
override bool isTotal(){ return dom.isTotal() && cod.isTotal(); }
@property size_t nargs(){
return params.length;
}
Expression argTy(size_t i){
auto ty = params[i].vtype;
assert(ty.isSemEvaluated());
return ty;
}
bool argConst(size_t i){
return params[i].isConst;
}
bool argConstForReverse(size_t i){
if(argConst(i)) return true;