Skip to content

Commit db8df3d

Browse files
committed
First intake for revised FFIs
1 parent c41c6b5 commit db8df3d

155 files changed

Lines changed: 15612 additions & 21 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cogen/src/main/scala/org/combinators/cogen/Command.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ trait Command {
1919
def interpret[Context, Self >: this.type <: Command.WithResult[Result]](implicit interp: Understands[Context, Self]): Command.Generator[Context, Result] = {
2020
val self: Self = this
2121

22-
liftF[Command.Performable[Context, _], Result](Command.Performable[Context, Result, Self](self, interp))
22+
liftF[Command.Performable[Context, *], Result](new Command.Performable[Context, Result] {
23+
type Cmd = Self
24+
val cmd = self
25+
val interpreter = interp
26+
})
2327
}
2428
}
2529

2630
object Command {
2731
type WithResult[R] = Command { type Result = R }
28-
type Generator[C, A] = Free[Performable[C, _], A]
32+
type Generator[C, A] = Free[Performable[C, *], A]
2933

3034
sealed trait Performable[Context, R] {
3135
type Cmd <: Command.WithResult[R]
@@ -41,15 +45,15 @@ object Command {
4145
}
4246

4347
def runGenerator[Context, Result](gen: Generator[Context, Result], inContext: Context): (Context, Result) = {
44-
val compiler: Performable[Context, _] ~> State[Context, _] = new (Performable[Context, _] ~> State[Context, _]) {
48+
val compiler: Performable[Context, *] ~> State[Context, *] = new (Performable[Context, *] ~> State[Context, *]) {
4549
def apply[A](fa: Performable[Context, A]): State[Context, A] =
4650
State[Context, A](ctxt => fa.interpreter.perform(ctxt, fa.cmd))
4751
}
4852
gen.foldMap(compiler).run(inContext).value
4953
}
5054

51-
implicit def monadInstance[C]: Monad[Generator[C, _]] =
52-
cats.free.Free.catsFreeMonadForFree[Performable[C, _]]
55+
implicit def monadInstance[C]: Monad[Generator[C, *]] =
56+
cats.free.Free.catsFreeMonadForFree[Performable[C, *]]
5357

5458
def lift[Context, T](value: T): Command.Generator[Context, T] = monadInstance.pure(value)
5559
def skip[Context]: Generator[Context, Unit] = lift(())

cogen/src/main/scala/org/combinators/cogen/paradigm/AnyParadigm.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ case class ResolveImport[Import, T](forElem: T) extends Command {
7171
type Result = Option[Import]
7272
}
7373

74+
case class Ternary[Expression, R](
75+
condition: Expression,
76+
trueExpression: Expression,
77+
falseExpression: Expression) extends Command {
78+
type Result = R
79+
}
80+
7481
case class IfThenElse[Expression, MandatoryBlock, Block, R](
7582
condition: Expression,
7683
ifBranch: MandatoryBlock,

cogen/src/main/scala/org/combinators/cogen/paradigm/ObjectOriented.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ trait ObjectOriented {
332332
def addClassToProject(classGen: Generator[ClassContext, Unit], qualifiedName: Name* ): Generator[ProjectContext, Unit] = {
333333
import compilationUnitCapabilities._
334334
import base.projectCapabilities._
335-
addCompilationUnit(AddClass(qualifiedName.last, classGen).interpret, qualifiedName*)
335+
addCompilationUnit(AddClass(qualifiedName.last, classGen).interpret, qualifiedName: _*)
336336
}
337337

338338
implicit val canAddTypeLookupForClassesInProject: Understands[ProjectContext, AddTypeLookup[ClassContext, Type]]

cogen/src/main/scala/org/combinators/cogen/paradigm/control/Functional.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.combinators.cogen.paradigm.control
22

3-
import org.combinators.cogen.paradigm.{AnyParadigm, Apply, IfThenElse, Reify}
4-
import org.combinators.cogen.{TypeRep, Command, Understands}
3+
import org.combinators.cogen.paradigm.{AnyParadigm, Apply, IfThenElse, Reify, Ternary}
4+
import org.combinators.cogen.{Command, TypeRep, Understands}
55
import Command.Generator
66

77
case class PatternMatch[MethodBodyContext, PatternContext, Expression](
@@ -60,6 +60,18 @@ trait Functional[Context] extends Lambdas[Context] {
6060
elseBlock
6161
))
6262

63+
implicit val canTernary: Understands[Context, Ternary[Expression, Expression]]
64+
def ternary(
65+
cond: Expression,
66+
trueExpression: Expression,
67+
falseExpression: Expression): Generator[Context, Expression] =
68+
AnyParadigm.capability(
69+
Ternary[Expression, Expression](
70+
cond,
71+
trueExpression,
72+
falseExpression)
73+
)
74+
6375
implicit val canPatternMatch: Understands[Context, PatternMatch[Context, PatternContext, Expression]]
6476
def patternMatch(
6577
onValue: Expression,

cogen/src/main/scala/org/combinators/cogen/paradigm/control/Imperative.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.combinators.cogen.paradigm.control
33
import org.combinators.cogen.{Command, Understands}
44
import Command.Generator
55

6-
import org.combinators.cogen.paradigm.{AnyParadigm, IfThenElse}
6+
import org.combinators.cogen.paradigm.{AnyParadigm, IfThenElse, Ternary}
77

88
case class DeclareVariable[Name, Type, Init, Res](name: Name, tpe: Type, initialization: Init) extends Command {
99
type Result = Res
@@ -55,6 +55,15 @@ trait Imperative[Context] {
5555
condition, ifBranch, elseIfs, elseBranch
5656
))
5757

58+
implicit val canTernary: Understands[Context, Ternary[Expression, Expression]]
59+
def ternary(
60+
condition: Expression,
61+
trueExpression: Expression,
62+
falseExpression: Expression): Generator[Context, Expression] =
63+
AnyParadigm.capability(Ternary[Expression, Expression](
64+
condition, trueExpression, falseExpression
65+
))
66+
5867
implicit val canWhile: Understands[Context, While[Context, Expression, Statement]]
5968
def whileLoop(condition: Expression, block: Generator[Context, Unit]): Generator[Context, Statement] =
6069
AnyParadigm.capability(While[Context, Expression, Statement](condition, block))

cogen/src/main/scala/org/combinators/cogen/paradigm/ffi/Arithmetic.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.combinators.cogen.paradigm.ffi
1+
package org.combinators.cogen.paradigm.ffi /*DI:LI:AI*/
22

33
import org.combinators.cogen.paradigm.{AnyParadigm, Apply}
44
import org.combinators.cogen.{Command, Understands}

cogen/src/main/scala/org/combinators/cogen/paradigm/ffi/Arrays.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package org.combinators.cogen.paradigm.ffi
1+
package org.combinators.cogen.paradigm.ffi /*DI:LI:AI*/
22

33
import org.combinators.cogen.paradigm.{AnyParadigm, Apply}
44
import org.combinators.cogen.{Command, Understands}
55
import Command.Generator
66

7-
case class CreateArray[Type](elementType: Type)
7+
case class CreateArray[Type](elementType: Type, dimensions: Seq[Int])
88
case class Get()
99
case class Set()
1010
case class Length()
@@ -15,20 +15,20 @@ trait Arrays[Context] extends FFI {
1515

1616
trait ArrayCapabilities {
1717
implicit val canCreate: Understands[Context, Apply[CreateArray[Type], Expression, Expression]]
18-
def create(elemTpe: Type, contents: Seq[Expression]): Generator[Context, Expression] =
19-
AnyParadigm.capability(Apply[CreateArray[Type], Expression, Expression](CreateArray(elemTpe), contents))
18+
def create(elemTpe: Type, dimensions: Seq[Int], contents: Seq[Expression]): Generator[Context, Expression] =
19+
AnyParadigm.capability(Apply[CreateArray[Type], Expression, Expression](CreateArray(elemTpe, dimensions), contents))
2020

2121
implicit val canGet: Understands[Context, Apply[Get, Expression, Expression]]
22-
def get(array: Expression, pos:Expression): Generator[Context, Expression] =
23-
AnyParadigm.capability(Apply[Get, Expression, Expression](Get(), Seq(array, pos)))
22+
def get(array: Expression, pos:Seq[Expression]): Generator[Context, Expression] =
23+
AnyParadigm.capability(Apply[Get, Expression, Expression](Get(), array +: pos))
2424

2525
implicit val canSet: Understands[Context, Apply[Set, Expression, Expression]]
26-
def set(array: Expression, pos:Expression, value:Expression): Generator[Context, Expression] =
27-
AnyParadigm.capability(Apply[Set, Expression, Expression](Set(), Seq(array, pos, value)))
26+
def set(array: Expression, pos:Seq[Expression], value:Expression): Generator[Context, Expression] =
27+
AnyParadigm.capability(Apply[Set, Expression, Expression](Set(), array +: pos :+ value))
2828

2929
implicit val canLength: Understands[Context, Apply[Length, Expression, Expression]]
30-
def length(array:Expression): Generator[Context, Expression] =
31-
AnyParadigm.capability(Apply[Length, Expression, Expression](Length(), Seq(array)))
30+
def length(array:Expression, dimension:Seq[Expression]): Generator[Context, Expression] =
31+
AnyParadigm.capability(Apply[Length, Expression, Expression](Length(), array +: dimension))
3232
}
3333
val arrayCapabilities: ArrayCapabilities
3434
}

cogen/src/main/scala/org/combinators/cogen/paradigm/ffi/RealArithmetic.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ case class Log[T]()
1010
case class Sin[T]()
1111
case class Cos[T]()
1212

13+
case class Max[T]()
14+
case class Min[T]()
15+
1316
case class Abs[T]()
1417
case class Floor[T]()
1518

@@ -36,6 +39,14 @@ trait RealArithmetic[Context, T] extends FFI {
3639
def log(base: Expression, x: Expression): Generator[Context, Expression] =
3740
AnyParadigm.capability(Apply[Log[T], Expression, Expression](Log[T](), Seq(base, x)))
3841

42+
implicit val canMax: Understands[Context, Apply[Max[T], Expression, Expression]]
43+
def max(left: Expression, right: Expression): Generator[Context, Expression] =
44+
AnyParadigm.capability(Apply[Max[T], Expression, Expression](Max[T](), Seq(left, right)))
45+
46+
implicit val canMin: Understands[Context, Apply[Min[T], Expression, Expression]]
47+
def min(left: Expression, right: Expression): Generator[Context, Expression] =
48+
AnyParadigm.capability(Apply[Min[T], Expression, Expression](Min[T](), Seq(left, right)))
49+
3950
implicit val canSin: Understands[Context, Apply[Sin[T], Expression, Expression]]
4051
def sin(x: Expression): Generator[Context, Expression] =
4152
AnyParadigm.capability(Apply[Sin[T], Expression, Expression](Sin[T](), Seq(x)))

cogen/src/main/scala/org/combinators/cogen/paradigm/ffi/Strings.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import scala.annotation.tailrec
88

99
case class StringAppend()
1010
case class GetStringLength()
11+
case class GetCharAt()
12+
case class SubString()
1113
case class ToString[Type](sourceType: Type)
1214

1315
trait Strings[Context] extends FFI {
@@ -18,6 +20,14 @@ trait Strings[Context] extends FFI {
1820
def getStringLength(expression: Expression): Generator[Context, Expression] =
1921
AnyParadigm.capability(Apply[GetStringLength, Expression, Expression](GetStringLength(), Seq(expression)))
2022

23+
implicit val canSubString: Understands[Context, Apply[SubString, Expression, Expression]]
24+
def subString(expression: Expression, start:Expression, exclusiveEnd:Expression): Generator[Context, Expression] =
25+
AnyParadigm.capability(Apply[SubString, Expression, Expression](SubString(), Seq(expression, start, exclusiveEnd)))
26+
27+
implicit val canGetCharAt: Understands[Context, Apply[GetCharAt, Expression, Expression]]
28+
def getCharAt(expression: Expression, pos:Expression): Generator[Context, Expression] =
29+
AnyParadigm.capability(Apply[GetCharAt, Expression, Expression](GetCharAt(), Seq(expression, pos)))
30+
2131
implicit val canAppend: Understands[Context, Apply[StringAppend, Expression, Expression]]
2232
def stringAppend(xs: Expression*): Generator[Context, Expression] =
2333
AnyParadigm.capability(Apply[StringAppend, Expression, Expression](StringAppend(), xs))
@@ -40,7 +50,7 @@ trait Strings[Context] extends FFI {
4050
sepExp <- Reify[String, Expression](TypeRep.String, sep).interpret
4151
endExp <- Reify[String, Expression](TypeRep.String, end).interpret
4252
inters = startExp +: make(sepExp, endExp, tl => tl, exprs)
43-
res <- stringAppend(inters*)
53+
res <- stringAppend(inters:_*)
4454
} yield res
4555
}
4656

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# DP Generators that Compile:
2+
3+
- bottomUp/twoSequences/longestCommonSubsequence (missing cases)
4+
- bottomUp/twoSequences/uncrossedLines (missing cases)
5+
- bottomUp/oneSequence/tribonacci
6+
- topDown/oneSequence/tribonacci
7+
-

0 commit comments

Comments
 (0)