-
Notifications
You must be signed in to change notification settings - Fork 17
/
Trees.scala
1054 lines (853 loc) · 35 KB
/
Trees.scala
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
package com.thoughtworks.compute
import java.util.IdentityHashMap
import com.github.ghik.silencer.silent
import com.thoughtworks.compute.Expressions._
import com.thoughtworks.compute.NDimensionalAffineTransform.MatrixData
import com.thoughtworks.feature.Factory.{Factory1, Factory2, Factory3, inject}
import scala.annotation.meta.companionObject
import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.language.higherKinds
import scala.util.hashing.MurmurHash3
/**
* @author 杨博 (Yang Bo)
*/
trait Trees extends Expressions {
final class StructuralComparisonContext extends IdentityHashMap[Tree, Tree]
private def childHashCode(child: Any, context: HashCodeContext): Int = {
child match {
case childTree: Tree @unchecked =>
childTree.structuralHashCode(context)
case childArray: Array[_] =>
arrayHashCode(childArray, context)
case childSeq: Seq[_] =>
seqHashCode(childSeq, context)
case _ =>
child.##
}
}
private def seqHashCode(childSeq: Seq[_], context: HashCodeContext): Int = {
val iterator = childSeq.iterator
@tailrec
def seqLoop(h: Int, i: Int): Int = {
if (iterator.hasNext) {
seqLoop(h = MurmurHash3.mix(h, childHashCode(iterator.next(), context)), i = i + 1)
} else {
MurmurHash3.finalizeHash(h, i)
}
}
seqLoop(MurmurHash3.seqSeed, 0)
}
private def arrayHashCode[@specialized A](childArray: Array[A], context: HashCodeContext): Int = {
val length = childArray.length
if (length == 0) {
MurmurHash3.arraySeed
} else {
val last = length - 1
@tailrec
def arrayLoop(h: Int, i: Int): Int = {
if (i < last) {
arrayLoop(h = MurmurHash3.mix(h, childHashCode(childArray(i), context)), i = i + 1)
} else {
MurmurHash3.finalizeHash(MurmurHash3.mixLast(h, childHashCode(childArray(i), context)), length)
}
}
arrayLoop(MurmurHash3.arraySeed, 0)
}
}
/** @group AST */
trait Operator extends Tree { thisOperator =>
def structuralHashCode(context: HashCodeContext): Int = {
val productArity: Int = this.productArity
if (productArity == 0) {
productPrefix.##
} else {
context.asScala.getOrElseUpdate(
this, {
val last = productArity - 1
@tailrec
def loop(h: Int = productPrefix.hashCode, i: Int = 0): Int = {
if (i < last) {
loop(h = MurmurHash3.mix(h, childHashCode(productElement(i), context)), i = i + 1)
} else {
MurmurHash3.finalizeHash(MurmurHash3.mixLast(h, childHashCode(productElement(i), context)),
productArity)
}
}
loop()
}
)
}
}
private def isSameChild(left: Any, right: Any, map: StructuralComparisonContext): Boolean = {
left match {
case left: Tree @unchecked =>
right match {
case right: Tree @unchecked =>
left.isSameStructure(right, map)
case _ =>
false
}
case left: Array[_] =>
right match {
case right: Array[_] =>
val leftLength = left.length
val rightLength = right.length
@tailrec def arrayLoop(start: Int): Boolean = {
if (start < leftLength) {
if (isSameChild(left(start), right(start), map)) {
arrayLoop(start + 1)
} else {
false
}
} else {
true
}
}
leftLength == rightLength && arrayLoop(0)
case _ =>
false
}
case left: Seq[_] =>
right match {
case right: Seq[_] =>
val leftIterator = left.iterator
val rightIterator = right.iterator
@tailrec def seqLoop(): Boolean = {
if (leftIterator.hasNext) {
if (rightIterator.hasNext) {
if (isSameChild(leftIterator.next(), rightIterator.next(), map)) {
seqLoop()
} else {
false
}
} else {
false
}
} else {
if (rightIterator.hasNext) {
false
} else {
true
}
}
}
seqLoop()
}
case _ =>
left == right
}
}
def isSameStructure(that: Tree, map: StructuralComparisonContext): Boolean = {
map.get(this) match {
case null =>
this.getClass == that.getClass && {
assert(this.productArity == that.productArity)
map.put(this, that)
val productArity: Int = this.productArity
@tailrec
def fieldLoop(from: Int): Boolean = {
if (from < productArity) {
if (isSameChild(this.productElement(from), that.productElement(from), map)) {
fieldLoop(from + 1)
} else {
false
}
} else {
true
}
}
fieldLoop(0)
}
case existing =>
existing eq that
}
}
}
/** @group AST */
trait Parameter extends Tree { thisParameter =>
val id: Any
}
final class HashCodeContext extends IdentityHashMap[Tree, Int] {
var numberOfParameters = 0
}
final class AlphaConversionContext extends IdentityHashMap[Tree, Tree] {
var seed: Int = 0
}
/** @group AST */
trait Tree extends Product { thisTree =>
type TermIn[C <: Category]
protected def erasedExport(foreignCategory: Category, context: ExportContext): Category#Term
final def export(foreignCategory: Category, context: ExportContext): TermIn[foreignCategory.type] = {
erasedExport(foreignCategory, context).asInstanceOf[TermIn[foreignCategory.type]]
}
def isSameStructure(that: Tree, map: StructuralComparisonContext): Boolean
def structuralHashCode(context: HashCodeContext): Int
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree
final def alphaConversion(context: AlphaConversionContext): Tree {
type TermIn[C <: Category] = thisTree.TermIn[C]
} = {
erasedAlphaConversion(context).asInstanceOf[Tree {
type TermIn[C <: Category] = thisTree.TermIn[C]
}]
}
}
final class ExportContext extends IdentityHashMap[Tree, Category#Term]
protected trait TreeTerm extends ExpressionApi with TermApi { thisTerm: Term =>
def alphaConversion: ThisTerm
@inline
final def in(foreignCategory: Category): TermIn[foreignCategory.type] = {
tree.export(foreignCategory, new ExportContext)
}
@inline
final def tree: Tree { type TermIn[C <: Category] = thisTerm.TermIn[C] } =
tree0.asInstanceOf[Tree {
type TermIn[C <: Category] = thisTerm.TermIn[C]
}]
protected val tree0: Tree
}
type Term <: TreeTerm
}
object Trees {
/**
* @author 杨博 (Yang Bo)
*/
trait ValueTrees extends Values with Trees {
protected trait ValueTreeType extends ValueTypeApi { thisValueType =>
def in(foreignCategory: Category): TypeIn[foreignCategory.type]
def term(tree: Tree): ThisTerm
}
type ValueType <: (Type with Any) with ValueTreeType
protected trait ValueTreeTerm extends TermApi with ValueTermApi { thisValue: ValueTerm =>
def valueType: ThisType
def alphaConversion: ThisTerm = {
valueType.term(tree.alphaConversion(new AlphaConversionContext)).asInstanceOf[ThisTerm]
}
}
type ValueTerm <: (Term with Any) with ValueTreeTerm
}
/**
* @author 杨博 (Yang Bo)
*/
trait StructuralTrees extends Trees {
protected trait StructuralTreeTerm extends TreeTerm { this: Term =>
@transient
override lazy val hashCode: Int = {
tree.structuralHashCode(new HashCodeContext)
}
override def equals(that: Any): Boolean = {
that match {
case that: StructuralTreeTerm =>
tree.isSameStructure(that.tree, new StructuralComparisonContext)
case _ =>
false
}
}
}
type Term <: StructuralTreeTerm
}
/**
* @author 杨博 (Yang Bo)
*/
trait FloatTrees extends Floats with ValueTrees {
protected trait FloatTreeTerm extends FloatTermApi with ValueTreeTerm with FloatExpressionApi {
thisFloat: FloatTerm =>
def valueType: ThisType = float
def unary_- : FloatTerm = float.term(UnaryMinus(tree))
def unary_+ : FloatTerm = float.term(UnaryPlus(tree))
def +(rightHandSide: FloatTerm): FloatTerm = float.term(Plus(tree, rightHandSide.tree))
def -(rightHandSide: FloatTerm): FloatTerm = float.term(Minus(tree, rightHandSide.tree))
def *(rightHandSide: FloatTerm): FloatTerm = float.term(Times(tree, rightHandSide.tree))
def /(rightHandSide: FloatTerm): FloatTerm = float.term(Div(tree, rightHandSide.tree))
def %(rightHandSide: FloatTerm): FloatTerm = float.term(Percent(tree, rightHandSide.tree))
}
type FloatTerm <: (ValueTerm with Any) with FloatTreeTerm
/** @group AST */
protected type FloatTree = Tree {
type TermIn[C <: Category] = C#FloatTerm
}
/** @group AST */
protected trait FloatOperator extends Operator {
type TermIn[C <: Category] = C#FloatTerm
}
/** @group AST */
@(silent @companionObject)
final case class FloatParameter(id: Any) extends Parameter { thisParameter =>
type TermIn[C <: Category] = C#FloatTerm
def isSameStructure(that: Tree, map: StructuralComparisonContext): Boolean = {
map.get(this) match {
case null =>
map.put(this, that)
true
case existing =>
existing eq that
}
}
def structuralHashCode(context: HashCodeContext): Int = {
context.asScala.getOrElseUpdate(this, {
val newId = context.numberOfParameters
context.numberOfParameters = newId + 1
newId
})
}
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.parameter(id))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(
id = new AnyRef {
override val toString: String = raw"""α-converted(${id.toString})"""
}
)
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class FloatLiteral(value: Float) extends FloatOperator {
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = this
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.literal(value))
}
}
/** @group AST */
@(silent @companionObject)
final case class Exp(operand0: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.exp(operand0.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Log(operand0: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.log(operand0.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Abs(operand0: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.abs(operand0.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Tanh(operand0: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.tanh(operand0.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Sqrt(operand0: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, foreignCategory.float.sqrt(operand0.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Min(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this,
foreignCategory.float.min(operand0.export(foreignCategory, context),
operand1.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Max(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this,
foreignCategory.float.max(operand0.export(foreignCategory, context),
operand1.export(foreignCategory, context)))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Plus(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, operand0.export(foreignCategory, context) + operand1.export(foreignCategory, context))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Minus(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, operand0.export(foreignCategory, context) - operand1.export(foreignCategory, context))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Times(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, operand0.export(foreignCategory, context) * operand1.export(foreignCategory, context))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Div(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, operand0.export(foreignCategory, context) / operand1.export(foreignCategory, context))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Percent(operand0: FloatTree, operand1: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, operand0.export(foreignCategory, context) % operand1.export(foreignCategory, context))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand0 = operand0.alphaConversion(context), operand1 = operand1.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class UnaryMinus(operand: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, operand.export(foreignCategory, context).unary_-)
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand = operand.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class UnaryPlus(operand: FloatTree) extends FloatOperator {
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala.getOrElseUpdate(this, operand.export(foreignCategory, context).unary_+)
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(operand = operand.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
protected trait FloatTreeType extends ValueTreeType with FloatTypeApi {
@inline
def in(foreignCategory: Category): TypeIn[foreignCategory.type] = {
foreignCategory.float
}
@inject
protected def termFactory: Factory1[Tree, ThisTerm]
@inline
def literal(value: JvmValue): ThisTerm = {
termFactory.newInstance(FloatLiteral(value))
}
@inline
def parameter(id: Any): ThisTerm = {
termFactory.newInstance(FloatParameter(id))
}
@inline
def term(tree: Tree): ThisTerm = termFactory.newInstance(tree)
@inline
def min(leftHandSide: FloatTerm, rightHandSide: FloatTerm): FloatTerm = {
term(Min(leftHandSide.tree, rightHandSide.tree))
}
@inline
def max(leftHandSide: FloatTerm, rightHandSide: FloatTerm): FloatTerm = {
term(Max(leftHandSide.tree, rightHandSide.tree))
}
@inline
def log(operand: FloatTerm): FloatTerm = {
term(Log(operand.tree))
}
@inline
def exp(operand: FloatTerm): FloatTerm = {
term(Exp(operand.tree))
}
@inline
def abs(operand: FloatTerm): FloatTerm = {
term(Abs(operand.tree))
}
@inline
def sqrt(operand: FloatTerm): FloatTerm = {
term(Sqrt(operand.tree))
}
@inline
def tanh(operand: FloatTerm): FloatTerm = {
term(Tanh(operand.tree))
}
}
type FloatType <: (ValueType with Any) with FloatTreeType
}
/**
* @author 杨博 (Yang Bo)
*/
trait ArrayTrees extends Arrays with ValueTrees {
/** @group AST */
type ArrayTree[LocalElement <: ValueTerm] = Tree {
type TermIn[C <: Category] = C#ArrayTerm { type Element = LocalElement#TermIn[C] }
}
/** @group AST */
@(silent @companionObject)
final case class Extract[LocalElement <: ValueTerm](array: ArrayTree[LocalElement]) extends Operator {
protected def erasedExport(foreignCategory: Category, map: ExportContext) = {
map.asScala.getOrElseUpdate(this, array.export(foreignCategory, map).extract)
}
type TermIn[C <: Category] = LocalElement#TermIn[C]
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(array = array.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Transform[LocalElement <: ValueTerm](array: ArrayTree[LocalElement], matrix: MatrixData)
extends Operator {
type TermIn[C <: Category] = C#ArrayTerm {
type Element = LocalElement#TermIn[C]
}
protected def erasedExport(foreignCategory: Category, map: ExportContext) = {
map.asScala.getOrElseUpdate(this, array.export(foreignCategory, map).transform(matrix))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(array = array.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
protected trait ArrayTreeTerm extends ArrayTermApi with TreeTerm { thisArray: ArrayTerm =>
def alphaConversion: ThisTerm = {
arrayTermFactory[Element]
.newInstance(elementType, tree.alphaConversion(new AlphaConversionContext))
.asInstanceOf[ThisTerm]
}
val elementType: ValueType
def extract: Element = {
elementType.term(Extract[Element](tree)).asInstanceOf[Element]
}
def transform(matrix: MatrixData): ThisTerm = {
val translatedTree = Transform[Element](tree, matrix)
arrayTermFactory[Element]
.newInstance(elementType, translatedTree)
.asInstanceOf[ThisTerm]
}
}
type ArrayTerm <: (Term with Any) with ArrayTreeTerm
/** @group AST */
@(silent @companionObject)
final case class Fill[LocalElement <: ValueTerm](element: Tree {
type TermIn[C <: Category] = LocalElement#TermIn[C]
}) extends Operator {
type TermIn[C <: Category] = C#ArrayTerm {
type Element = LocalElement#TermIn[C]
}
protected def erasedExport(foreignCategory: Category, map: ExportContext) = {
map.asScala.getOrElseUpdate(this, element.export(foreignCategory, map).fill)
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy[LocalElement](element = element.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
protected trait ElementTreeTerm extends ElementTermApi with ValueTreeTerm {
thisValue: ValueTerm =>
def fill: ArrayTerm {
type Element = thisValue.ThisTerm
} = {
val fillTree = Fill[thisValue.ThisTerm](
tree.asInstanceOf[Tree { type TermIn[C <: Category] = thisValue.ThisTerm#TermIn[C] }])
arrayTermFactory[ThisTerm]
.newInstance(thisValue.valueType, fillTree)
}
}
type ValueTerm <: (Term with Any) with ElementTreeTerm
/** @group AST */
@(silent @companionObject)
final case class ArrayParameter[LocalElement <: ValueTerm](id: Any, padding: Tree {
type TermIn[C <: Category] = LocalElement#TermIn[C]
}, shape: Array[Int])
extends Parameter {
type TermIn[C <: Category] = C#ArrayTerm {
type Element = LocalElement#TermIn[C]
}
def isSameStructure(that: Tree, map: StructuralComparisonContext): Boolean = {
map.get(this) match {
case null =>
that match {
case ArrayParameter(thatId, thatPadding, thatShape)
if padding == thatPadding && java.util.Arrays.equals(shape, thatShape) =>
map.put(this, that)
true
case _ =>
false
}
case existing =>
existing eq that
}
}
def structuralHashCode(context: HashCodeContext): Int = {
context.asScala.getOrElseUpdate(
this, {
val h = context.numberOfParameters
context.numberOfParameters = h + 1
MurmurHash3.finalizeHash(
MurmurHash3.mixLast(
MurmurHash3.mix(
h,
padding.##
),
MurmurHash3.arrayHash(shape)
),
3
)
}
)
}
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
def foreignTerm = {
foreignCategory.array.parameter(id, padding.export(foreignCategory, context), shape)
}
context.asScala.getOrElseUpdate(this, foreignTerm)
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy[LocalElement](
padding = padding
.alphaConversion(context)
.asInstanceOf[Tree {
type TermIn[C <: Category] = LocalElement#TermIn[C]
}],
id = {
val id = context.seed
context.seed = id + 1
id
}
)
context.asScala.getOrElseUpdate(this, converted)
}
}
@inject
protected def arrayTermFactory[LocalElement <: ValueTerm]: Factory2[ValueType,
Tree,
ArrayTerm {
type Element = LocalElement
}]
protected trait TreeArraySingleton extends ArraySingletonApi {
def parameter[Element0 <: ValueTerm](id: Any, padding: Element0, shape: Array[Int]): ArrayTerm {
type Element = Element0
} = {
val parameterTree = ArrayParameter[Element0](
id,
padding.tree.asInstanceOf[Tree { type TermIn[C <: Category] = Element0#TermIn[C] }],
shape
)
arrayTermFactory[Element0].newInstance(padding.valueType, parameterTree)
}
}
type ArraySingleton <: TreeArraySingleton
}
private val tupleHashSeed = "TupleType".##
trait TupleTrees extends ValueTrees with Tuples {
protected trait TupleTreeType extends TupleTypeApi with ValueTreeType {
@transient
override lazy val hashCode: Int = {
MurmurHash3.finalizeHash(MurmurHash3.mixLast(MurmurHash3.mix(tupleHashSeed, elementType.##), length), 2)
}
override def equals(that: scala.Any): Boolean = {
that match {
case that: TupleTreeType =>
this.elementType.equals(that.elementType) && this.length.equals(that.length)
case _ =>
false
}
}
val elementType: ValueType
val length: Int
def term(tree: Tree): ThisTerm = {
tupleTermFactory[Element].newInstance(elementType, length, tree).asInstanceOf[ThisTerm]
}
def in(foreignCategory: Category): TypeIn[foreignCategory.type] = {
foreignCategory.tuple.apply(elementType.in(foreignCategory), length).asInstanceOf[TypeIn[foreignCategory.type]]
}
}
type TupleType <: (ValueType with Any) with TupleTreeType
@inject
protected def tupleTreeTypeFactory[LocalElement <: ValueTerm]: Factory2[ValueType,
Int,
TupleType {
type Element = LocalElement
}]
/** @group AST */
@(silent @companionObject)
final case class TupleParameter(id: Any, elementType: ValueType, length: Int) extends Parameter {
type TermIn[C <: Category] = C#TupleTerm {
type Element = elementType.TermIn[C]
}
def isSameStructure(that: Tree, map: StructuralComparisonContext): Boolean = {
map.get(this) match {
case null =>
that match {
case TupleParameter(thatId, thatElementType, thatLength)
if elementType == thatElementType && length == thatLength =>
map.put(this, that)
true
case _ =>
false
}
case existing =>
existing eq that
}
}
def structuralHashCode(context: HashCodeContext): Int = {
context.asScala.getOrElseUpdate(
this, {
val h = context.numberOfParameters
context.numberOfParameters = h + 1
MurmurHash3.finalizeHash(
MurmurHash3.mixLast(
MurmurHash3.mix(
h,
elementType.##
),
length
),
3
)
}
)
}
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, foreignCategory.tuple.parameter(id, elementType.in(foreignCategory), length))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(
id = new AnyRef {
override val toString: String = raw"""α-converted(${id.toString})"""
}
)
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
@(silent @companionObject)
final case class Concatenate[Element0 <: ValueTerm](elementTrees: Seq[Tree {
type TermIn[C <: Category] = Element0#TermIn[C]
}]) extends Operator { thisTree =>
type TermIn[C <: Category] = C#TupleTerm { type Element = Element0#TermIn[C] }
protected def erasedExport(foreignCategory: Category, context: ExportContext): Category#Term = {
def foreignTerm = {
val foreignElements = elementTrees.map(_.export(foreignCategory, context))
foreignCategory.tuple.join(foreignElements: _*)
}
context.asScala.getOrElseUpdate(this, foreignTerm)
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
val converted = copy[Element0](
elementTrees = elementTrees.map(_.alphaConversion(context))
)
context.asScala.getOrElseUpdate(this, converted)
}
}
/** @group AST */
type TupleTree[LocalElement <: ValueTerm] = Tree {
type TermIn[C <: Category] = C#TupleTerm { type Element = LocalElement#TermIn[C] }
}
/** @group AST */
@(silent @companionObject)
final case class Apply[LocalElement <: ValueTerm](tuple: TupleTree[LocalElement], index: Int) extends Operator {
type TermIn[C <: Category] = LocalElement#TermIn[C]
protected def erasedExport(foreignCategory: Category, context: ExportContext) = {
context.asScala
.getOrElseUpdate(this, tuple.export(foreignCategory, context).split.apply(index))
}
protected def erasedAlphaConversion(context: AlphaConversionContext): Tree = {
def converted = copy(tuple = tuple.alphaConversion(context))
context.asScala.getOrElseUpdate(this, converted)
}
}
protected trait TupleTreeTerm extends ValueTreeTerm with TupleTermApi {
thisTuple: TupleTerm =>