-
Notifications
You must be signed in to change notification settings - Fork 1
/
StandardSystem.scala
482 lines (388 loc) · 13.8 KB
/
StandardSystem.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
package singularity
import scala.language.implicitConversions
//todo: add more docs for each component
/**
* The standard component set used in the Singularity paper. Defines commonly used data structures like
* Integers, Vectors, Tuples, and Graphs.
*/
//noinspection TypeAnnotation
object StandardSystem {
private var _funcMap = Map[String, EFunction]()
def funcMap: Map[String, EFunction] = _funcMap
// Type declarations
case object EInt extends EType()
case class EByteArray(byteNum: Int) extends EType()
case object EByte extends EType()
case class EVect(elemT: EType) extends EType(elemT)
case object EBool extends EType()
case class EPair(t1: EType, t2: EType) extends EType(t1,t2)
case class EGraph(edgeT: EType) extends EType(edgeT)
case object EUnit extends EType()
// Value declarations
@SerialVersionUID(-2901336594181525995L)
case class IntValue(value: Int) extends EValue {
def hasType(ty: EType): Boolean = ty == EInt
override def toString: String = value.toString
def memoryUsage: Long = 1
def cost: Double = math.log10(math.abs(value.toDouble)+1)
}
case class ByteArrayValue(value: IS[Byte]) extends EValue {
override def hasType(ty: EType): Boolean = ty match {
case EByteArray(sz) => sz == value.length
case _ => false
}
override def toString: String = s"[${value.mkString(":")}]"
override def memoryUsage: Long = value.length + 1
def cost: Double = value.map(v => v.toDouble / Byte.MaxValue).sum
}
case class ByteValue(value: Byte) extends EValue {
override def hasType(ty: EType): Boolean = ty == EByte
override def toString: String = s"$value"
override def memoryUsage: Long = 1
def cost: Double = value.toDouble / Byte.MaxValue
}
@SerialVersionUID(4070837804269965038L)
case class VectValue(value: Vector[EValue]) extends EValue{
def hasType(ty: EType): Boolean = ty match {
case EVect(et) => value.isEmpty || value.head.hasType(et)
case _ => false
}
override def toString: String = value.mkString("[",",","]")
def memoryUsage: Long = value.map(_.memoryUsage).sum + 1
def cost: Double = value.map(_.cost).sum
}
case class BoolValue(value: Boolean) extends EValue{
def hasType(ty: EType): Boolean = ty == EBool
def memoryUsage: Long = 1
override def toString: String = if(value)"T" else "F"
def cost: Double = 0
}
case class PairValue(value: (EValue, EValue)) extends EValue{
def hasType(ty: EType): Boolean = ty match {
case EPair(t1, t2) => value._1.hasType(t1) && value._2.hasType(t2)
case _ => false
}
def memoryUsage: Long = value._1.memoryUsage + value._2.memoryUsage
override def toString: String = value.toString()
def cost: Double = value._1.cost + value._2.cost
}
case class GraphValue(nodeNum: Int, edges: IS[(Int, Int, EValue)]) extends EValue{
def hasType(ty: EType): Boolean = ty match {
case EGraph(edgeT) => edges.isEmpty || edges.head._3.hasType(edgeT)
case _ => false
}
def memoryUsage: Long = nodeNum + edges.map(_._3.memoryUsage).sum + 1
def shiftIndex(offset: Int): GraphValue = {
this.copy(edges = edges.map{ case (n1, n2, v) => (n1+offset, n2+offset, v)})
}
def cost: Double = edges.map(_._3.cost).sum
}
case object UnitValue extends EValue{
def hasType(ty: EType): Boolean = ty == EUnit
def memoryUsage: Long = 1
def cost: Double = 0
}
implicit def intValue(v: Int): IntValue = IntValue(v)
implicit def vectValue[E](v: Vector[EValue]): VectValue = {
VectValue(v)
}
implicit def boolValue(b: Boolean): BoolValue = BoolValue(b)
implicit def pairValue[A,B](p: (A, B))(implicit convA: A => EValue, convB: B => EValue): PairValue = {
PairValue(convA(p._1) -> convB(p._2))
}
object GraphValue{
def empty = GraphValue(0, IS())
}
trait ComponentSet{
private var _collection = IS[EFunction]()
def collection: IS[EFunction] = _collection
def register(f: EFunction): Unit ={
_collection :+= f
if(_funcMap.contains(f.name)){
throw new Exception(s"Component ${f.name} collision!")
}else{
_funcMap = _funcMap.updated(f.name, f)
}
}
def mkConcrete(name: String, argTypes: IS[EType], returnType: EType,
eval: PartialFunction[IS[EValue], EValue]): EConcreteFunc = {
val f = EConcreteFunc(name, argTypes, returnType, eval)
register(f)
f
}
def mkAbstract(name: String, tyVarNum: Int,
typeInstantiation: (IS[EType]) => (IS[EType], EType),
eval: PartialFunction[IS[EValue], EValue]): EAbstractFunc = {
val f = EAbstractFunc(name, tyVarNum, typeInstantiation, eval)
register(f)
f
}
}
val IntComponents = new ComponentSet {
val inc = mkConcrete("inc", IS(EInt), EInt, {
case IS(IntValue(i)) => i + 1
})
val dec = mkConcrete("dec", IS(EInt), EInt, {
case IS(IntValue(i)) => i - 1
})
val neg = mkConcrete("neg", IS(EInt), EInt, {
case IS(IntValue(i)) => -i
})
val plus = mkConcrete("plus", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => a + b
})
val minus = mkConcrete("minus", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => a - b
})
val times = mkConcrete("times", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => a * b
})
/** protected divide */
val divide = mkConcrete("divide", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => if(b==0) 1 else a / b
})
val modular = mkConcrete("modular", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => if(b==0) 1 else a % b
})
}
val BitComponents = new ComponentSet {
val shiftByteLeft = mkConcrete("shiftBL", IS(EInt), EInt, {
case IS(IntValue(a)) => a << 8
})
val bitAnd = mkConcrete("bitAnd", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => a & b
})
val bitOr = mkConcrete("bitOr", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => a | b
})
val bitXor = mkConcrete("bitXor", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) => a ^ b
})
val bitShift = mkConcrete("bitShift", IS(EInt, EInt), EInt, {
case IS(IntValue(a), IntValue(b)) =>
if (b >= 0)
a << b
else
a >>> (-b)
})
}
val VectComponents = new ComponentSet {
val append = mkAbstract("append", 1, {
case IS(e) => IS(EVect(e), e) -> EVect(e)
}, {
case IS(VectValue(vec), v) => vec :+ v
})
val prepend = mkAbstract("prepend", 1, {
case IS(e) => IS(e, EVect(e)) -> EVect(e)
}, {
case IS(v, VectValue(vec)) => v +: vec
})
val access = mkAbstract("access", 1, {
case IS(e) => IS(EVect(e), EInt, e) -> e
}, {
case IS(VectValue(vec), IntValue(v), default) =>
if (vec.isEmpty) default
else {
val i = SimpleMath.wrapInRange(v, vec.length)
vec(i)
}
})
val last = mkAbstract("last", 1, {
case IS(e) => IS(EVect(e), e) -> e
}, {
case IS(VectValue(vec), default) =>
if (vec.isEmpty) default
else {
vec.last
}
})
val concat = mkAbstract("concat", 1, {
case IS(e) => IS(EVect(e), EVect(e)) -> EVect(e)
}, {
case IS(VectValue(v1), VectValue(v2)) => v1 ++ v2
})
val length = mkAbstract("length", 1, {
case IS(e) => IS(EVect(e)) -> EInt
}, {
case IS(VectValue(v1)) => v1.length
})
}
val AdvancedVectComponents = new ComponentSet{
val shift = mkConcrete("shift", IS(EVect(EInt), EInt), EVect(EInt), {
case IS(VectValue(vec), IntValue(v)) => vec.map{
case IntValue(i) => IntValue(i+v)
}
})
val intToString = mkConcrete("int2String", IS(EInt, EInt), EVect(EInt), {
case IS(IntValue(x), IntValue(base)) =>
val p = SimpleMath.safeAbs(x)
val b = if(base < 10) 10 else base
SimpleMath.natToList(p, b).toVector.map(IntValue.apply)
})
val head = mkAbstract("head", 1, {
case IS(e) => IS(EVect(e), e) -> e
}, {
case IS(VectValue(vec), default) =>
if (vec.isEmpty) default
else {
vec.head
}
})
}
val BoolComponents = new ComponentSet {
val not = mkConcrete("not", IS(EBool), EBool, {
case IS(BoolValue(b)) => !b
})
val and = mkConcrete("and", IS(EBool, EBool), EBool, {
case IS(BoolValue(b1), BoolValue(b2)) => b1 && b2
})
val or = mkConcrete("or", IS(EBool, EBool), EBool, {
case IS(BoolValue(b1), BoolValue(b2)) => b1 || b2
})
val lessThan = mkConcrete("lessThan", IS(EInt, EInt), EBool, {
case IS(IntValue(i1), IntValue(i2)) => i1 < i2
})
val equal = mkAbstract("equal", 1, {
case IS(e) => IS(e, e) -> EBool
}, {
case IS(v1, v2) => v1 == v2
})
val ifElse = mkAbstract("ifElse", 1, {
case IS(e) => IS(EBool, e, e) -> e
}, {
case IS(BoolValue(b), v1, v2) => if (b) v1 else v2
})
}
val PairComponents = new ComponentSet {
val pair1 = mkAbstract("pair1", tyVarNum = 2,
typeInstantiation = {
case IS(t1,t2) => IS(EPair(t1,t2)) -> t1
}, eval = {
case IS(PairValue(value)) => value._1
}
)
val pair2 = mkAbstract("pair2", tyVarNum = 2,
typeInstantiation = {
case IS(t1,t2) => IS(EPair(t1,t2)) -> t2
}, eval = {
case IS(PairValue(value)) => value._2
}
)
val mkPair = mkAbstract("mkPair", tyVarNum = 2,
typeInstantiation = {
case IS(t1,t2) => IS(t1,t2) -> EPair(t1,t2)
}, eval = {
case IS(v1,v2) => (v1,v2)
}
)
}
/** add an isolated vertex */
val GraphComponents = new ComponentSet {
val emptyGraph = mkAbstract("emptyGraph", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS() -> EGraph(eT)
}, eval = {
case IS() => GraphValue(0, IS())
})
val addNode = mkAbstract("addNode", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT)) -> EGraph(eT)
}, eval = {
case IS(gv: GraphValue) => gv.copy(nodeNum = gv.nodeNum+1)
})
/** add an isolated edge */
val addEdge = mkAbstract("addEdge", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), eT) -> EGraph(eT)
}, eval = {
case IS(GraphValue(nodeNum, edges), edgeValue) =>
val newEdges = edges :+ (nodeNum, nodeNum+1, edgeValue)
GraphValue(nodeNum + 2, newEdges)
})
/** add an edge from/to an existing vertex */
val growEdge = mkAbstract("growEdge", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), EInt, eT) -> EGraph(eT)
}, eval = {
case IS(g @ GraphValue(nodeNum, edges), IntValue(nodeToGrow), edgeValue) =>
if(nodeNum == 0) g
else{
val n = SimpleMath.wrapInRange(nodeToGrow, nodeNum)
val newEdges = edges :+ (n, nodeNum, edgeValue)
GraphValue(nodeNum + 1, newEdges)
}
})
/** add a self loop to a vertex */
val growSelfLoop = mkAbstract("growSelfLoop", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), EInt, eT) -> EGraph(eT)
}, eval = {
case IS(g @ GraphValue(nodeNum, edges), IntValue(nodeToGrow), edgeValue) =>
if(nodeNum == 0) g
else{
val n = SimpleMath.wrapInRange(nodeToGrow, nodeNum)
val newEdges = edges :+ (n, n, edgeValue)
GraphValue(nodeNum, newEdges)
}
})
/** add an edge between two existing vertices */
val bridgeEdge = mkAbstract("bridgeEdge", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), EInt, EInt, eT) -> EGraph(eT)
}, eval = {
case IS(g @ GraphValue(nodeNum, edges), IntValue(from), IntValue(to), edgeValue) =>
if(nodeNum == 0) g
else{
val n1 = SimpleMath.wrapInRange(from, nodeNum)
val n2 = SimpleMath.wrapInRange(to, nodeNum)
val newEdges = edges :+ (n1, n2, edgeValue)
GraphValue(nodeNum, newEdges)
}
})
/** delete an edge */
val deleteEdge = mkAbstract("deleteEdge", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), EInt) -> EGraph(eT)
}, eval = {
case IS(g @ GraphValue(nodeNum, edges), IntValue(edgeIndex)) =>
if(edges.isEmpty) g
else{
val e = SimpleMath.wrapInRange(edgeIndex, nodeNum)
val newEdges = edges.patch(e, IS(), e+1)
GraphValue(nodeNum, newEdges)
}
})
val mergeGraph = mkAbstract("mergeGraph", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), EGraph(eT)) -> EGraph(eT)
}, eval = {
case IS(g1: GraphValue, g2: GraphValue) =>
GraphValue(g1.nodeNum + g2.nodeNum, g1.edges + g2.shiftIndex(g1.nodeNum).edges)
})
val updateEdgeValue = mkAbstract("updateEdge", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), EInt, eT) -> EGraph(eT)
}, eval = {
case IS(g1: GraphValue, eId: IntValue, v: EValue) =>
val eNum = g1.edges.length
if(eNum==0) g1
else{
val index = SimpleMath.wrapInRange(eId.value, eNum)
val (from, to, _) = g1.edges(index)
val newEdges = g1.edges.updated(index, (from, to, v))
GraphValue(g1.nodeNum, newEdges)
}
}
)
val addCompleteNode = mkAbstract("addCompleteNode", tyVarNum = 1,
typeInstantiation = {
case IS(eT) => IS(EGraph(eT), eT) -> EGraph(eT),
}, eval = {
case IS(g1: GraphValue, v: EValue) =>
val nV = g1.nodeNum
val newEdges = (0 until nV).map(i => (i, nV, v))
GraphValue(nV+1, g1.edges ++ newEdges)
}
)
}
}