-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
553741f
commit 9e0bbac
Showing
8 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
20 changes: 15 additions & 5 deletions
20
...r-core/src/main/scala/pl/writeonly/catculator/core/calculators/CombinatorCalculator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
package pl.writeonly.catculator.core.calculators | ||
|
||
import pl.writeonly.catculator.core.adt.calculus.Combinator.CombinatorBT | ||
import pl.writeonly.catculator.core.adt.tree.BinaryTree.Leaf | ||
import pl.writeonly.catculator.core.adt.tree.BinaryTree.Node | ||
import pl.writeonly.catculator.core.adt.calculus.Combinator._ | ||
import pl.writeonly.catculator.core.adt.tree.BinaryTree._ | ||
|
||
object CombinatorCalculator { | ||
|
||
def calculate(c: CombinatorBT): Unit = c match { | ||
def interpret(c: CombinatorBT): CombinatorBT = c match { | ||
case l @ Leaf(_) => l | ||
case Node(Leaf(I), x) => x | ||
case Node(Leaf(K), x) => Leaf(K) | ||
case Node(Node(Leaf(K), x), y) => x | ||
case Node(Node(Node(Leaf(S), x), y), z) => | ||
val first = Node(x, z) | ||
val second = Node(y, z) | ||
Node(first, second) | ||
|
||
case Node(f, x) => | ||
case Leaf(a) => | ||
val reducedF = interpret(f) | ||
val reducedX = interpret(x) | ||
interpret(Node(reducedF, reducedX)) | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...lator-core/src/main/scala/pl/writeonly/catculator/core/calculators/lazyk/Calculator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package pl.writeonly.catculator.core.calculators.lazyk | ||
|
||
import pl.writeonly.catculator.core.adt.calculus.Combinator | ||
import pl.writeonly.catculator.core.adt.calculus.Combinator._ | ||
import pl.writeonly.catculator.core.adt.tree.BinaryTree | ||
import pl.writeonly.catculator.core.adt.tree.BinaryTree._ | ||
import pl.writeonly.catculator.core.calculators.lazyk.ADT._ | ||
import pl.writeonly.catculator.core.calculators.lazyk.Reducer.flippedApply | ||
import spire.math.Natural | ||
|
||
object Calculator { | ||
|
||
val number0: ADTBT = Leaf(ADT.Num(Natural(0))) | ||
|
||
val trueVar: ADTBT = Leaf(ADT.Com(K)) | ||
|
||
val falseVar: ADTBT = Node(trueVar, Leaf(ADT.Com(I))) | ||
|
||
def run(program: ADTBT): Iterator[Safe[Natural]] = runWithTerminator(falseVar, program) | ||
|
||
private def runWithTerminator(terminator: ADTBT, combinator: ADTBT): Iterator[Safe[Natural]] = | ||
println("runWithTerminator2") | ||
Iterator.unfold[Safe[Natural], (ADTBT, Safe[ADTBT])](terminator, Right(combinator)) { case (t: ADTBT, cM: Safe[ADTBT]) => | ||
val a = cM.flatMap(realizeWithTrue) | ||
val s = cM.flatMap(Reducer.flippedApply(t, _)) | ||
Option(a, (t, s)) | ||
} | ||
|
||
private def runWithTerminatorNumber(terminator: ADTBT, combinator: ADTBT): Safe[Unit] = realizeWithTrue(combinator).flatMap(output(terminator, combinator, _)) | ||
|
||
def realizeWithTrue(combinator: ADTBT): Safe[Natural] = flippedApply(trueVar, combinator).flatMap(realize) | ||
|
||
def realize(combinator: ADTBT): Safe[Natural] = flippedApply(Leaf(ADT.Succ()), combinator).flatMap(flippedApply(number0, _)).flatMap(naturalSafe) | ||
|
||
private def naturalSafe(combinator: ADTBT): Safe[Natural] = combinator match { | ||
case Leaf(ADT.Num(x)) => Right(x) | ||
case x => Left(s"Invalid output format. Output should be the list of Church numerals. $x") | ||
} | ||
|
||
private def output(terminator: ADTBT, combinator: ADTBT, number: Natural): Safe[Unit] = Reducer | ||
.apply(combinator, terminator) | ||
.flatMap(runWithTerminatorNumber(terminator, _)) | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ulator-core/src/main/scala/pl/writeonly/catculator/core/calculators/lazyk/Evaluator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package pl.writeonly.catculator.core.calculators.lazyk | ||
|
||
import pl.writeonly.catculator.core.adt.calculus.InputEncoder._ | ||
import pl.writeonly.catculator.core.adt.tree.BinaryTree.Node | ||
import pl.writeonly.catculator.core.calculators.lazyk.ADT.ADTBT | ||
import pl.writeonly.catculator.core.calculators.lazyk.ADT.Safe | ||
import pl.writeonly.catculator.core.calculators.lazyk.ADT.fromCombinatorBT | ||
import pl.writeonly.catculator.core.calculators.lazyk.Calculator.run | ||
import pl.writeonly.catculator.core.calculators.lazyk.Reducer.reduce | ||
|
||
object Evaluator { | ||
|
||
def evalCombinator(combinator: ADTBT, input: String): Safe[String] = | ||
for c <- reduce(Node(combinator, fromCombinatorBT(readInput(input)))) | ||
yield run(c).map(_.toOption.get.toBigInt.toInt.toChar).mkString | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...r-core/src/test/scala/pl/writeonly/catculator/core/calculators/lazyk/CalculatorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pl.writeonly.catculator.core.calculators.lazyk | ||
|
||
import mouse.all.anySyntaxMouse | ||
import org.scalatest.prop.TableFor1 | ||
import pl.writeonly.catculator.core.TableDrivenPropertySpec | ||
import pl.writeonly.catculator.core.adt.calculus.Combinator.CombinatorBT | ||
import pl.writeonly.catculator.core.adt.calculus.Constants._ | ||
import pl.writeonly.catculator.core.adt.calculus.InputEncoder._ | ||
import pl.writeonly.catculator.core.calculators.lazyk.ADT._ | ||
import spire.math.Natural | ||
|
||
class CalculatorSpec extends TableDrivenPropertySpec { | ||
|
||
val numbers: TableFor1[Long] = Table("number", 0, 1, 4, 8, 9, 16, 27, 36, 64, 81, 100, 121, 125, 256) | ||
|
||
def realizeFromCombinatorBT(c: CombinatorBT): Safe[Natural] = c |> fromCombinatorBT |> Calculator.realize | ||
|
||
it should "realize false" in { | ||
val result = realizeFromCombinatorBT(falseCom) | ||
result.value shouldBe Natural.zero | ||
} | ||
|
||
it should "realize I" in { | ||
val result = realizeFromCombinatorBT(iCom) | ||
result.value shouldBe Natural.one | ||
} | ||
|
||
it should "realize successor false" in { | ||
val c = successor(falseCom) | ||
val result = realizeFromCombinatorBT(c) | ||
result.value shouldBe Natural.one | ||
} | ||
|
||
it should "realize numbers" in { | ||
forAll(numbers) { number => | ||
val natural = Natural(number) | ||
val c = church(natural) | ||
val result = realizeFromCombinatorBT(c) | ||
result.value shouldBe natural | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters