@@ -6,153 +6,150 @@ import org.junit.Assert._
6
6
import scala .language .implicitConversions
7
7
8
8
trait Cde2 {
9
- type Cde [A ]
10
-
11
- def int (c1 : Int )(using Quotes ): Cde [Int ]
12
- def infix_int_+ (c1 : Cde [Int ], c2 : Cde [Int ])(using Quotes ): Cde [Int ]
13
-
14
- def long (c1 : Long )(using Quotes ): Cde [Long ]
15
- def infix_long_+ (c1 : Cde [Long ], c2 : Cde [Long ])(using Quotes ): Cde [Long ]
16
-
17
- def bool (c1 : Boolean )(using Quotes ): Cde [Boolean ]
18
- def not (c1 : Cde [Boolean ])(using Quotes ): Cde [Boolean ]
19
- def infix_&& (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ]
20
- def infix_|| (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ]
21
-
22
-
23
- // Methods
24
- trait IntMethods :
25
- extension (self : Cde [Int ])
26
- def + (c2 : Cde [Int ])(using Quotes ): Cde [Int ] = infix_int_+(self, c2)
27
- given IntMethods = new IntMethods { }
28
-
29
- trait LongMethods :
30
- extension (self : Cde [Long ])
31
- def + (c2 : Cde [Long ])(using Quotes ): Cde [Long ] = infix_long_+(self, c2)
32
- given LongMethods = new LongMethods { }
33
-
34
- extension (self : Cde [Boolean ])
35
- def && (c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = infix_&& (self, c2)
36
- def || (c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = infix_|| (self, c2)
37
- def unary_! (using Quotes ): Cde [Boolean ] = not(self)
9
+ type Cde [A ]
10
+
11
+ def int (c1 : Int )(using Quotes ): Cde [Int ]
12
+ def infix_int_+ (c1 : Cde [Int ], c2 : Cde [Int ])(using Quotes ): Cde [Int ]
13
+
14
+ def long (c1 : Long )(using Quotes ): Cde [Long ]
15
+ def infix_long_+ (c1 : Cde [Long ], c2 : Cde [Long ])(using Quotes ): Cde [Long ]
16
+
17
+ def bool (c1 : Boolean )(using Quotes ): Cde [Boolean ]
18
+ def not (c1 : Cde [Boolean ])(using Quotes ): Cde [Boolean ]
19
+ def infix_&& (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ]
20
+ def infix_|| (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ]
21
+
22
+ // Methods
23
+ trait IntMethods :
24
+ extension (self : Cde [Int ]) def + (c2 : Cde [Int ])(using Quotes ): Cde [Int ] = infix_int_+(self, c2)
25
+ given IntMethods = new IntMethods {}
26
+
27
+ trait LongMethods :
28
+ extension (self : Cde [Long ]) def + (c2 : Cde [Long ])(using Quotes ): Cde [Long ] = infix_long_+(self, c2)
29
+ given LongMethods = new LongMethods {}
30
+
31
+ extension (self : Cde [Boolean ])
32
+ def && (c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = infix_&& (self, c2)
33
+ def || (c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = infix_|| (self, c2)
34
+ def unary_! (using Quotes ): Cde [Boolean ] = not(self)
38
35
}
39
36
40
37
object exprCode extends Cde2 {
41
- type Cde [A ] = Expr [A ]
42
- given toExpr [A ]: Conversion [Cde [A ], Expr [A ]] = x => x
43
- // given ofExpr[A]: Conversion[Expr[A], Cde[A]] = x => x
44
-
45
- def int (c1 : Int )(using Quotes ): Cde [Int ] = Expr (c1)
46
- def infix_int_+ (c1 : Cde [Int ], c2 : Cde [Int ])(using Quotes ): Cde [Int ] = ' { $ {c1 } + $ {c2 } }
47
-
48
- def long (c1 : Long )(using Quotes ): Cde [Long ] = Expr (c1)
49
- def infix_long_+ (c1 : Cde [Long ], c2 : Cde [Long ])(using Quotes ): Cde [Long ] = ' { $ {c1 } + $ {c2 } }
50
-
51
- def bool (c1 : Boolean )(using Quotes ): Cde [Boolean ] = Expr (c1)
52
- def not (c1 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = ' {! $ {c1} }
53
- def infix_&& (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] =
54
- ' { $ {c1 } && $ {c2} }
55
- def infix_|| (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] =
56
- ' { $ {c1 } || $ {c2} }
38
+ type Cde [A ] = Expr [A ]
39
+ given toExpr [A ]: Conversion [Cde [A ], Expr [A ]] = x => x
40
+ // given ofExpr[A]: Conversion[Expr[A], Cde[A]] = x => x
41
+
42
+ def int (c1 : Int )(using Quotes ): Cde [Int ] = Expr (c1)
43
+ def infix_int_+ (c1 : Cde [Int ], c2 : Cde [Int ])(using Quotes ): Cde [Int ] = ' { $ { c1 } + $ { c2 } }
44
+
45
+ def long (c1 : Long )(using Quotes ): Cde [Long ] = Expr (c1)
46
+ def infix_long_+ (c1 : Cde [Long ], c2 : Cde [Long ])(using Quotes ): Cde [Long ] = ' { $ { c1 } + $ { c2 } }
47
+
48
+ def bool (c1 : Boolean )(using Quotes ): Cde [Boolean ] = Expr (c1)
49
+ def not (c1 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = ' { ! $ { c1 } }
50
+ def infix_&& (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] =
51
+ ' { $ { c1 } && $ { c2 } }
52
+ def infix_|| (c1 : Cde [Boolean ], c2 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] =
53
+ ' { $ { c1 } || $ { c2 } }
57
54
}
58
55
59
56
object psCode extends Cde2 {
60
- type Code [A ] = exprCode.Cde [A ]
61
- given toExpr [A ]: Conversion [Cde [A ], Expr [A ]] = x => x.dyn
62
- // given ofExpr[A]: Conversion[Expr[A], Cde[A]] = x => Cde(Annot.Unk[A](), exprCode.ofExpr(x))
63
-
64
- enum Annot [A ] {
65
- case Sta [A ](x : A ) extends Annot [A ]
66
- case Global [A ]() extends Annot [A ]
67
- case Unk [A ]() extends Annot [A ]
57
+ type Code [A ] = exprCode.Cde [A ]
58
+ given toExpr [A ]: Conversion [Cde [A ], Expr [A ]] = x => x.dyn
59
+ // given ofExpr[A]: Conversion[Expr[A], Cde[A]] = x => Cde(Annot.Unk[A](), exprCode.ofExpr(x))
60
+
61
+ enum Annot [A ] {
62
+ case Sta [A ](x : A ) extends Annot [A ]
63
+ case Global [A ]() extends Annot [A ]
64
+ case Unk [A ]() extends Annot [A ]
65
+ }
66
+
67
+ case class Cde [A ](sta : Annot [A ], dyn : Code [A ])
68
+
69
+ def injCde [A ](x : Code [A ]): Cde [A ] = Cde [A ](Annot .Unk (), x)
70
+ def dyn [A ](x : Cde [A ]): Code [A ] = x.dyn
71
+
72
+ def inj2 [A , B , C ](f : (Code [A ], Code [B ]) => Code [C ]): ((Cde [A ], Cde [B ]) => Cde [C ]) = { (x : Cde [A ], y : Cde [B ]) =>
73
+ val v = f(dyn(x), dyn(y))
74
+ (x, y) match {
75
+ case (Cde (Annot .Unk (), _), _) | (_, Cde (Annot .Unk (), _)) => injCde[C ](v)
76
+ case _ => Cde [C ](Annot .Global (), v)
68
77
}
69
-
70
- case class Cde [A ](sta : Annot [A ], dyn : Code [A ])
71
-
72
- def injCde [A ](x : Code [A ]): Cde [A ] = Cde [A ](Annot .Unk (), x)
73
- def dyn [A ](x : Cde [A ]): Code [A ] = x.dyn
74
-
75
- def inj2 [A , B , C ](f : (Code [A ], Code [B ]) => Code [C ]): ((Cde [A ], Cde [B ]) => Cde [C ]) = {
76
- (x : Cde [A ], y : Cde [B ]) =>
77
- val v = f (dyn(x), dyn(y))
78
- (x, y) match {
79
- case (Cde (Annot .Unk (), _), _) | (_, Cde (Annot .Unk (), _)) => injCde[C ](v)
80
- case _ => Cde [C ](Annot .Global (), v)
81
- }
78
+ }
79
+
80
+ def lift2 [A , B , C ](
81
+ fs : (A , B ) => C
82
+ )(lift : C => Cde [C ])(fd : (Code [A ], Code [B ]) => Code [C ]): ((Cde [A ], Cde [B ]) => Cde [C ]) = { (x : Cde [A ], y : Cde [B ]) =>
83
+ (x, y) match {
84
+ case (Cde (Annot .Sta (a), _), Cde (Annot .Sta (b), _)) => lift(fs(a, b))
85
+ case _ => inj2(fd)(x, y)
82
86
}
87
+ }
83
88
84
- def lift2 [A , B , C ](fs : (A , B ) => C )(lift : C => Cde [C ])(fd : (Code [A ], Code [B ]) => Code [C ]): ((Cde [A ], Cde [B ]) => Cde [C ]) = {
85
- (x : Cde [A ], y : Cde [B ]) =>
86
- (x, y) match {
87
- case (Cde (Annot .Sta (a), _), Cde (Annot .Sta (b), _)) => lift(fs(a, b))
88
- case _ => inj2(fd)(x, y)
89
- }
90
- }
91
-
92
- def int (c1 : Int )(using Quotes ): Cde [Int ] = Cde (Annot .Sta (c1), exprCode.int(c1))
93
- def infix_int_+ (c1 : Cde [Int ], c2 : Cde [Int ])(using Quotes ): Cde [Int ] =
94
- lift2[Int , Int , Int ](_+_)(int)(exprCode.infix_int_+)(c1, c2)
89
+ def int (c1 : Int )(using Quotes ): Cde [Int ] = Cde (Annot .Sta (c1), exprCode.int(c1))
90
+ def infix_int_+ (c1 : Cde [Int ], c2 : Cde [Int ])(using Quotes ): Cde [Int ] =
91
+ lift2[Int , Int , Int ](_ + _)(int)(exprCode.infix_int_+)(c1, c2)
95
92
96
- def long (c1 : Long )(using Quotes ): Cde [Long ] = Cde (Annot .Sta (c1), exprCode.long(c1))
97
- def infix_long_+ (c1 : Cde [Long ], c2 : Cde [Long ])(using Quotes ): Cde [Long ] =
98
- lift2[Long , Long , Long ](_+ _)(long)(exprCode.infix_long_+)(c1, c2)
93
+ def long (c1 : Long )(using Quotes ): Cde [Long ] = Cde (Annot .Sta (c1), exprCode.long(c1))
94
+ def infix_long_+ (c1 : Cde [Long ], c2 : Cde [Long ])(using Quotes ): Cde [Long ] =
95
+ lift2[Long , Long , Long ](_ + _)(long)(exprCode.infix_long_+)(c1, c2)
99
96
100
- def bool (c1 : Boolean )(using Quotes ): Cde [Boolean ] = Cde (Annot .Sta (c1), exprCode.bool(c1))
101
- def not (c1 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = {
102
- c1 match {
103
- case Cde (Annot .Sta (b), _) => bool(! b)
104
- case _ => Cde (c1.sta, exprCode.not(c1.dyn))
105
- }
97
+ def bool (c1 : Boolean )(using Quotes ): Cde [Boolean ] = Cde (Annot .Sta (c1), exprCode.bool(c1))
98
+ def not (c1 : Cde [Boolean ])(using Quotes ): Cde [Boolean ] = {
99
+ c1 match {
100
+ case Cde (Annot .Sta (b), _) => bool(! b)
101
+ case _ => Cde (c1.sta, exprCode.not(c1.dyn))
106
102
}
107
- def infix_&& ( c1 : Cde [ Boolean ], c2 : Cde [ Boolean ])( using Quotes ) : Cde [ Boolean ] = {
108
- (c1, c2) match {
109
- case ( Cde ( Annot . Sta ( true ), _), _) => c2
110
- case (Cde (Annot .Sta (false ), _), _) => bool( false )
111
- case (_, Cde (Annot .Sta (true ), _)) => c1
112
- case (_, Cde (Annot .Sta (false ), _)) => bool( false )
113
- case _ => inj2(exprCode.infix_ && )(c1, c2 )
114
- }
103
+ }
104
+ def infix_&& (c1 : Cde [ Boolean ] , c2 : Cde [ Boolean ])( using Quotes ) : Cde [ Boolean ] = {
105
+ (c1, c2) match {
106
+ case (Cde (Annot .Sta (true ), _), _) => c2
107
+ case (Cde (Annot .Sta (false ), _), _) => bool( false )
108
+ case (_, Cde (Annot .Sta (true ), _)) => c1
109
+ case (_, Cde ( Annot . Sta ( false ), _)) => bool( false )
110
+ case _ => inj2(exprCode.infix_ && )(c1, c2)
115
111
}
116
- def infix_|| ( c1 : Cde [ Boolean ], c2 : Cde [ Boolean ])( using Quotes ) : Cde [ Boolean ] = {
117
- (c1, c2) match {
118
- case ( Cde ( Annot . Sta ( true ), _), _) => bool( true )
119
- case (Cde (Annot .Sta (false ), _), _) => c2
120
- case (_, Cde (Annot .Sta (true ), _)) => bool( true )
121
- case (_, Cde (Annot .Sta (false ), _)) => c1
122
- case _ => inj2(exprCode.infix_ || )(c1, c2)
123
- }
112
+ }
113
+ def infix_|| (c1 : Cde [ Boolean ] , c2 : Cde [ Boolean ])( using Quotes ) : Cde [ Boolean ] = {
114
+ (c1, c2) match {
115
+ case (Cde (Annot .Sta (true ), _), _) => bool( true )
116
+ case (Cde (Annot .Sta (false ), _), _) => c2
117
+ case (_, Cde (Annot .Sta (true ), _)) => bool( true )
118
+ case (_, Cde ( Annot . Sta ( false ), _)) => c1
119
+ case _ => inj2(exprCode.infix_ || )(c1, c2)
124
120
}
125
- }
121
+ }
122
+ }
126
123
127
124
class CdeTest {
128
- import psCode ._
129
- given Compiler = Compiler .make(getClass.getClassLoader)
125
+ import psCode ._
126
+ given Compiler = Compiler .make(getClass.getClassLoader)
130
127
131
- // TODO: make a full example with two different interpreters, modularized as Tomoaki did in Cde.scala with HKTs
132
- @ Test def int_add (): Unit = {
133
- def test (using Quotes ): Cde [Int ] = {
134
- int(1 ) + int(2 )
135
- }
136
-
137
- val t = run { test }
138
- assert(t == 3 )
128
+ // TODO: make a full example with two different interpreters, modularized as Tomoaki did in Cde.scala with HKTs
129
+ @ Test def int_add (): Unit = {
130
+ def test (using Quotes ): Cde [Int ] = {
131
+ int(1 ) + int(2 )
139
132
}
140
133
141
- @ Test def long_add (): Unit = {
142
- def test (using Quotes ): Cde [Long ] = {
143
- long(1L ) + long(2L )
144
- }
134
+ val t = run { test }
135
+ assert(t == 3 )
136
+ }
145
137
146
- val t = run { test }
147
- assert(t == 3L )
138
+ @ Test def long_add (): Unit = {
139
+ def test (using Quotes ): Cde [Long ] = {
140
+ long(1L ) + long(2L )
148
141
}
149
142
150
- @ Test def bool_all (): Unit = {
151
- def test (using Quotes ): Cde [Boolean ] = {
152
- bool(true ) && (bool(false ) || ! bool(true ))
153
- }
143
+ val t = run { test }
144
+ assert(t == 3L )
145
+ }
154
146
155
- val t = run { test }
156
- assert(t == false )
147
+ @ Test def bool_all (): Unit = {
148
+ def test (using Quotes ): Cde [Boolean ] = {
149
+ bool(true ) && (bool(false ) || ! bool(true ))
157
150
}
158
- }
151
+
152
+ val t = run { test }
153
+ assert(t == false )
154
+ }
155
+ }
0 commit comments