@@ -3,7 +3,7 @@ package org.combinators.ep.domain.math.eips /*DD:LI:AI*/
3
3
import org .combinators .ep .domain .abstractions .{DataTypeCase , Operation , TypeRep }
4
4
import org .combinators .ep .domain .instances .InstanceRep
5
5
import org .combinators .ep .domain .{abstractions , math }
6
- import org .combinators .ep .generator .Command .Generator
6
+ import org .combinators .ep .generator .Command .{ Generator , lift }
7
7
import org .combinators .ep .generator .{ApproachImplementationProvider , EvolutionImplementationProvider }
8
8
import org .combinators .ep .generator .EvolutionImplementationProvider .monoidInstance
9
9
import org .combinators .ep .generator .communication .{PotentialRequest , ReceivedRequest , Request , SendRequest }
@@ -15,12 +15,8 @@ object M9funct {
15
15
def apply [P <: AnyParadigm , AIP [P <: AnyParadigm ] <: ApproachImplementationProvider .WithParadigm [P ]]
16
16
(paradigm : P )
17
17
(m8Provider : EvolutionImplementationProvider [AIP [paradigm.type ]])
18
- (functional: Functional .WithBase [paradigm.type ],
19
- functionalControl : control.Functional .WithBase [paradigm.MethodBodyContext , paradigm.type ],
20
- ffiArithmetic : Arithmetic .WithBase [paradigm.MethodBodyContext , paradigm.type , Double ],
21
- ffiRealArithmetic : RealArithmetic .WithBase [paradigm.MethodBodyContext , paradigm.type , Double ],
22
- ffiEquals : Equality .WithBase [paradigm.MethodBodyContext , paradigm.type ]
23
- ):
18
+ (functionalControl : control.Functional .WithBase [paradigm.MethodBodyContext , paradigm.type ],
19
+ ffiArithmetic : Arithmetic .WithBase [paradigm.MethodBodyContext , paradigm.type , Double ]):
24
20
EvolutionImplementationProvider [AIP [paradigm.type ]] = {
25
21
val m9Provider = new EvolutionImplementationProvider [AIP [paradigm.type ]] {
26
22
override val model = math.M9 .getModel
@@ -29,7 +25,6 @@ object M9funct {
29
25
for {
30
26
_ <- m8Provider.initialize(forApproach)
31
27
_ <- ffiArithmetic.enable()
32
- _ <- ffiRealArithmetic.enable()
33
28
} yield ()
34
29
}
35
30
@@ -50,89 +45,33 @@ object M9funct {
50
45
(forApproach : AIP [paradigm.type ])
51
46
(onRequest : ReceivedRequest [forApproach.paradigm.syntax.Expression ]):
52
47
Generator [paradigm.MethodBodyContext , Option [paradigm.syntax.Expression ]] = {
53
- import paradigm ._
54
- import AnyParadigm .syntax ._
55
- import methodBodyCapabilities ._
56
48
import functionalControl .functionalCapabilities ._
57
-
58
- // def makeCtorPat(tpe: paradigm.syntax.Type, caseName: paradigm.syntax.Name, attributeNames: Seq[paradigm.syntax.Name]): Generator[functionalControl.PatternContext, paradigm.syntax.Expression] = {
59
- // import functionalControl.patternCapabilities._
60
- // applyConstructorPattern(ConstructorPattern(tpe, caseName), attributeNames.map(patternVariable))
61
- // }
62
- //
63
-
64
- if (onRequest.request.op == math.M9 .Height && onRequest.tpeCase != math.M0 .Lit ) {
65
- val allCase = math.M9 .getModel.flatten.typeCases
66
- for {
67
- zero <- forApproach.reify(InstanceRep (TypeRep .Int )(0 ))
68
- one <- forApproach.reify(InstanceRep (TypeRep .Int )(1 ))
69
- intType <- toTargetLanguageType(TypeRep .Int )
70
-
71
-
72
- // _ <- forEach(onRequest.attributes.toSeq) { case (att, expr) => {
73
- // for {
74
- // attName <- freshName(forApproach.names.mangle(att.name))
75
- // exprVal <- forApproach.dispatch(
76
- // SendRequest(
77
- // expr,
78
- // math.M4.getModel.baseDataType,
79
- // Request(math.M9.Height, Map.empty)
80
- // )
81
- // )
82
- // declVar <- ffiImper.imperativeCapabilities.declareVar(attName, intType, Some(exprVal))
83
- // ifExpr <- ffiArithmetic.arithmeticCapabilities.lt(maxDecl, declVar)
84
- //
85
- // ifStmt <- ffiImper.imperativeCapabilities.ifThenElse(ifExpr, for {
86
- // assignStmt <- ffiImper.imperativeCapabilities.assignVar(maxDecl, declVar)
87
- // _ <- addBlockDefinitions(Seq(assignStmt))
88
- // } yield (),
89
- // Seq.empty
90
- // )
91
-
92
- // insert logic for all types:
93
- // can get all types and find those with just one recursive inner
94
- // attribute; and the height method is defined
95
-
96
- //
97
- // def height_rec(exp:Exp)
98
- // exp match {
99
- // case Sub(left, right) => { leftHeight = height(left); rightHeight = height(right) 1 + if (leftHeight > rightHeight) leftHeight rightHeight }
100
- // case Mult(left, right) => { 1 + max(height_rec(left), height_rec(right)) }
101
- // case Lit(value) => { 0 }
102
- // case Add(left, right) => { 1 + max(height_rec(left), height_rec(right)) }
103
- // case Neg(inner) => { 1 + eval(inner) }
104
- // }
105
- //
106
- // def height(exp : mathdomain.Exp): Integer = {
107
- // val leftHeight = height_rec(exp)
108
- // }
109
- //
110
- //
111
- // maxName <- freshName(forApproach.names.mangle("max"))
112
- // maxDecl <- ffiImper.imperativeCapabilities.declareVar(maxName, intType, Some(zero))
113
- //
114
- // _ <- forEach(onRequest.attributes.toSeq) { case (att, expr) => {
115
- // for {
116
- // attName <- freshName(forApproach.names.mangle(att.name))
117
- // exprVal <- forApproach.dispatch(
118
- // SendRequest(
119
- // expr,
120
- // math.M4.getModel.baseDataType,
121
- // Request(math.M9.Height, Map.empty)
122
- // )
123
- // )
124
- // declVar <- ffiImper.imperativeCapabilities.declareVar(attName, intType, Some(exprVal))
125
- // ifExpr <- ffiArithmetic.arithmeticCapabilities.lt(maxDecl, declVar)
126
- //
127
- // ifStmt <- ffiImper.imperativeCapabilities.ifThenElse(ifExpr, for {
128
- // assignStmt <- ffiImper.imperativeCapabilities.assignVar(maxDecl, declVar)
129
- // _ <- addBlockDefinitions(Seq(assignStmt))
130
- // } yield (),
131
- // Seq.empty
132
- // )
133
- //
134
- // _ <- addBlockDefinitions(Seq(ifStmt))
135
- } yield Some (one)
49
+ import ffiArithmetic .arithmeticCapabilities ._
50
+
51
+ if (onRequest.request.op == math.M9 .Height ) {
52
+ onRequest.tpeCase match {
53
+
54
+ // these are all binary cases
55
+ case binary@ (math.M3 .Neg | math.M3 .Mult | math.M3 .Divd | math.M0 .Add | math.M1 .Sub | math.systemI.I2 .Power | math.M8 .Inv ) =>
56
+ for {
57
+ left <- forApproach.dispatch(SendRequest (
58
+ onRequest.attributes(binary.attributes.head),
59
+ math.M0 .getModel.baseDataType,
60
+ onRequest.request
61
+ ))
62
+
63
+ right <- forApproach.dispatch(SendRequest (
64
+ onRequest.attributes(binary.attributes.tail.head),
65
+ math.M0 .getModel.baseDataType,
66
+ onRequest.request
67
+ ))
68
+
69
+ ifExpr <- ffiArithmetic.arithmeticCapabilities.lt(left, right)
70
+ one <- forApproach.reify(InstanceRep (TypeRep .Int )(1 ))
71
+
72
+ result <- ifThenElse(cond = ifExpr, ifBlock = add(one, right), elseIfs = Seq .empty, elseBlock = add(one, left))
73
+ } yield Some (result)
74
+ }
136
75
} else {
137
76
m8Provider.genericLogic(forApproach)(onRequest)
138
77
}
@@ -142,20 +81,31 @@ object M9funct {
142
81
(forApproach : AIP [paradigm.type ])
143
82
(onRequest : ReceivedRequest [forApproach.paradigm.syntax.Expression ]):
144
83
Generator [paradigm.MethodBodyContext , Option [paradigm.syntax.Expression ]] = {
145
- import paradigm ._
146
- import AnyParadigm .syntax ._
147
- import methodBodyCapabilities ._
84
+ import ffiArithmetic .arithmeticCapabilities ._
148
85
149
86
assert(dependencies(PotentialRequest (onRequest.onType, onRequest.tpeCase, onRequest.request.op)).nonEmpty)
150
87
151
88
onRequest.tpeCase match {
89
+ // Height of a Lit is ZERO.
152
90
case math.M0 .Lit =>
153
91
for {
154
92
zero <- forApproach.reify(InstanceRep (TypeRep .Int )(0 ))
155
93
} yield Some (zero)
156
94
157
- case _ => genericLogic(forApproach)(onRequest)
95
+ case neg@ math.M3 .Neg =>
96
+ for {
97
+ one <- forApproach.reify(InstanceRep (TypeRep .Int )(1 ))
158
98
99
+ inner <- forApproach.dispatch(SendRequest (
100
+ onRequest.attributes(neg.attributes.head),
101
+ math.M0 .getModel.baseDataType,
102
+ onRequest.request
103
+ ))
104
+
105
+ res <- add(one, inner)
106
+ } yield Some (res)
107
+
108
+ case _ => genericLogic(forApproach)(onRequest)
159
109
}
160
110
}
161
111
}
0 commit comments