-
Notifications
You must be signed in to change notification settings - Fork 17
/
Expressions.scala
229 lines (162 loc) · 5.96 KB
/
Expressions.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
package com.thoughtworks.compute
import com.thoughtworks.feature.Factory.inject
import com.thoughtworks.feature.{Factory, ImplicitApply}
import com.thoughtworks.compute.NDimensionalAffineTransform.MatrixData
import scala.language.higherKinds
/**
* @author 杨博 (Yang Bo)
*/
trait Expressions {
type Category >: this.type <: Expressions
protected trait ExpressionApi {
type TermIn[C <: Category] <: C#Term
type ThisTerm = TermIn[Expressions.this.type]
}
protected trait TermApi extends ExpressionApi { this: Term =>
}
/** @template */
type Term <: TermApi
protected trait TypeApi extends ExpressionApi
/** @template */
type Type <: TypeApi
}
object Expressions {
trait Anonymous extends Any
object Anonymous {
implicit def implicitValue[A, Constructor, ImplicitApplied](
implicit factory: Factory.Aux[(A with Anonymous), Constructor],
implicitApply: ImplicitApply.Aux[Constructor, ImplicitApplied],
asImplicitValue: ImplicitApplied <:< (A with Anonymous)
): Implicitly[A] = {
asImplicitValue(implicitApply(factory.newInstance))
}
}
type Implicitly[A] = A with Anonymous
/**
* @author 杨博 (Yang Bo)
*/
trait Values extends Expressions {
type Category >: this.type <: Values
protected trait ValueExpressionApi extends ExpressionApi { thisValue =>
type JvmValue
type TermIn[C <: Category] <: C#ValueTerm
type TypeIn[C <: Category] <: C#ValueType {
type JvmValue = thisValue.JvmValue
}
type ThisType = TypeIn[Values.this.type]
}
protected trait ValueTermApi extends TermApi with ValueExpressionApi { this: ValueTerm =>
}
/** @template */
type ValueTerm <: (Term with Any) with ValueTermApi
protected trait ValueTypeApi extends ValueExpressionApi
protected trait ValueSingletonApi extends ValueTypeApi {
def literal(value: JvmValue): ThisTerm
def parameter(id: Any): ThisTerm
}
/** @template */
type ValueType <: (Type with Any) with ValueTypeApi
}
// TODO: Boolean types
/**
* @author 杨博 (Yang Bo)
*/
trait Floats extends Values {
type Category >: this.type <: Floats
protected trait FloatExpressionApi extends ValueExpressionApi {
type JvmValue = Float
type TermIn[C <: Category] = C#FloatTerm
type TypeIn[C <: Category] = C#FloatType
}
protected trait FloatTermApi extends ValueTermApi with FloatExpressionApi { this: FloatTerm =>
def +(rightHandSide: FloatTerm): FloatTerm
def -(rightHandSide: FloatTerm): FloatTerm
def *(rightHandSide: FloatTerm): FloatTerm
def /(rightHandSide: FloatTerm): FloatTerm
def %(rightHandSide: FloatTerm): FloatTerm
def unary_- : FloatTerm
def unary_+ : FloatTerm
}
/** @template */
type FloatTerm <: (ValueTerm with Any) with FloatTermApi
protected trait FloatTypeApi extends ValueSingletonApi with FloatExpressionApi {
def min(leftHandSide: FloatTerm, rightHandSide: FloatTerm): FloatTerm
def max(leftHandSide: FloatTerm, rightHandSide: FloatTerm): FloatTerm
def log(operand: FloatTerm): FloatTerm
def exp(operand: FloatTerm): FloatTerm
def abs(operand: FloatTerm): FloatTerm
def sqrt(operand: FloatTerm): FloatTerm
def tanh(operand: FloatTerm): FloatTerm
}
/** @template */
type FloatType <: (ValueType with Any) with FloatTypeApi
@inject
val float: Implicitly[FloatType]
}
/**
* @author 杨博 (Yang Bo)
*/
trait Arrays extends Values {
type Category >: this.type <: Arrays
protected trait ElementTermApi extends ValueTermApi { thisValue: ValueTerm =>
// TODO: Remove this method
def fill: ArrayTerm {
type Element = thisValue.ThisTerm
}
}
override type ValueTerm <: (Term with Any) with ElementTermApi
protected trait ArrayTermApi extends TermApi { thisArray: ArrayTerm =>
type TermIn[C <: Category] = C#ArrayTerm {
type Element = thisArray.Element#TermIn[C]
}
type Element <: ValueTerm
def extract: Element
def transform(matrix: MatrixData): ThisTerm
}
/** @template */
type ArrayTerm <: (Term with Any) with ArrayTermApi
protected trait ArraySingletonApi {
def parameter[Element0 <: ValueTerm](id: Any, padding: Element0, shape: Array[Int]): ArrayTerm {
type Element = Element0
}
}
/** @template */
type ArraySingleton <: ArraySingletonApi
@inject
val array: Implicitly[ArraySingleton]
}
trait Tuples extends Values {
type Category >: this.type <: Tuples
protected trait TupleExpressionApi extends ValueExpressionApi { thisTuple =>
type Element <: ValueTerm
type JvmValue = Array[Element#JvmValue]
type TermIn[C <: Category] = C#TupleTerm {
type Element = thisTuple.Element#TermIn[C]
}
type TypeIn[C <: Category] = C#TupleType {
type Element = thisTuple.Element#TermIn[C]
type JvmValue = thisTuple.JvmValue
}
}
protected trait TupleTermApi extends ValueTermApi with TupleExpressionApi { this: TupleTerm =>
def split: Seq[Element]
}
/** @template */
type TupleTerm <: (ValueTerm with Any) with TupleTermApi
protected trait TupleTypeApi extends ValueTypeApi with TupleExpressionApi {}
/** @template */
type TupleType <: (ValueType with Any) with TupleTypeApi
@inject
val tuple: Implicitly[TupleSingleton]
protected trait TupleSingletonApi {
def apply(element: ValueType, length: Int): TupleType { type Element = element.ThisTerm }
def parameter(id: Any, element: ValueType, length: Int): TupleTerm { type Element = element.ThisTerm }
def join[Element0 <: ValueTerm](elements: Element0*): TupleTerm { type Element = Element0 }
}
/** @template */
type TupleSingleton <: TupleSingletonApi
}
trait AllExpressions extends Tuples with Floats with Arrays {
type Category >: this.type <: Tuples with Floats with Arrays
}
}