diff --git a/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/Constants.scala b/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/Constants.scala index 1e57f89..cab81bc 100644 --- a/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/Constants.scala +++ b/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/Constants.scala @@ -18,10 +18,9 @@ object Constants { def appK(a: CombinatorBT): Node[Combinator] = Node(kCom, a) - def app4(c1: CombinatorBT, c2: CombinatorBT, c3: CombinatorBT, c4: CombinatorBT): CombinatorBT = Node(c1, app3(c2, c3, c4)) + def app4(c1: CombinatorBT, c2: CombinatorBT, c3: CombinatorBT, c4: CombinatorBT): CombinatorBT = Node(app3(c1, c2, c3), c4) - def app3(c1: CombinatorBT, c2: CombinatorBT, c3: CombinatorBT): CombinatorBT = Node(c1, Node(c2, c3)) + def app3(c1: CombinatorBT, c2: CombinatorBT, c3: CombinatorBT): CombinatorBT = Node(Node(c1, c2), c3) def com(c: Combinator): CombinatorBT = Leaf(c) - } diff --git a/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoder.scala b/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoder.scala index 8de318e..e4ee0a9 100644 --- a/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoder.scala +++ b/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoder.scala @@ -1,6 +1,5 @@ package pl.writeonly.catculator.core.adt.calculus -import cats.implicits.catsSyntaxEq import pl.writeonly.catculator.core.adt.calculus.Combinator.CombinatorBT import pl.writeonly.catculator.core.adt.calculus.Constants._ import spire.math.Natural @@ -21,9 +20,9 @@ object InputEncoder { def cons(a: CombinatorBT, b: CombinatorBT): CombinatorBT = app3(sCom, Constants.app3SI(appK(a)), appK(b)) - def church(n: Natural): CombinatorBT = n.toBigInt match { - case n if n === BigInt(0) => falseCom - case n => succChurch(Natural(n - BigInt(1))) + def church(n: Natural): CombinatorBT = n match { + case n if n === Natural.zero => falseCom + case n => succChurch(n - Natural.one) } private def succChurch(n: Natural): CombinatorBT = successor(church(n)) diff --git a/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/tree/BinaryTree.scala b/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/tree/BinaryTree.scala index fc527ba..9ca2f18 100644 --- a/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/tree/BinaryTree.scala +++ b/catculator-core/src/main/scala/pl/writeonly/catculator/core/adt/tree/BinaryTree.scala @@ -3,5 +3,3 @@ package pl.writeonly.catculator.core.adt.tree enum BinaryTree[+A]: case Leaf(leaf: A) case Node(first: BinaryTree[A], follow: BinaryTree[A]) - -object BinaryTree {} diff --git a/catculator-core/src/main/scala/pl/writeonly/catculator/core/reducers/LambdaReducer.scala b/catculator-core/src/main/scala/pl/writeonly/catculator/core/reducers/LambdaReducer.scala index fc98729..03f73e8 100644 --- a/catculator-core/src/main/scala/pl/writeonly/catculator/core/reducers/LambdaReducer.scala +++ b/catculator-core/src/main/scala/pl/writeonly/catculator/core/reducers/LambdaReducer.scala @@ -1,5 +1,7 @@ package pl.writeonly.catculator.core.reducers +import cats.Applicative +import cats.implicits._ import pl.writeonly.catculator.core.adt.calculus.Combinator._ import pl.writeonly.catculator.core.adt.calculus.Lambda import pl.writeonly.catculator.core.adt.calculus.Lambda._ @@ -7,11 +9,8 @@ import pl.writeonly.catculator.core.adt.tree.BinaryTree._ object LambdaReducer { def toCombinatorBT(l: Lambda): Either[Lambda, CombinatorBT] = l match { - case Com(c) => Right(Leaf(c)) - case App(f, x) => for { - fc <- toCombinatorBT(f) - xc <- toCombinatorBT(x) - } yield Node(fc, xc) - case _ => Left(l) + case Com(c) => Right(Leaf(c)) + case App(f, x) => Applicative[Either[Lambda, *]].map2(toCombinatorBT(f), toCombinatorBT(x))(Node.apply) + case _ => Left(l) } } diff --git a/catculator-core/src/test/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoderSpec.scala b/catculator-core/src/test/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoderSpec.scala index 6466b37..2bc8f72 100644 --- a/catculator-core/src/test/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoderSpec.scala +++ b/catculator-core/src/test/scala/pl/writeonly/catculator/core/adt/calculus/InputEncoderSpec.scala @@ -8,8 +8,8 @@ import spire.math.Natural class InputEncoderSpec extends TableDrivenPropertySpec { - private val oneCom = Node(Leaf(S), Node(Node(Leaf(S), Node(Node(Leaf(K), Leaf(S)), Leaf(K))), Node(Leaf(K), Leaf(I)))) - private val zeroListCom = Node(Leaf(S), Node(Node(Leaf(S), Node(Leaf(I), Node(Leaf(K), Node(Leaf(K), Leaf(I))))), Node(Leaf(K), Node(Leaf(K), Leaf(I))))) + private val oneCom = Node(Node(Leaf(S), Node(Node(Leaf(S), Node(Leaf(K), Leaf(S))), Leaf(K))), Node(Leaf(K), Leaf(I))) + private val zeroListCom = Node(Node(Leaf(S), Node(Node(Leaf(S), Leaf(I)), Node(Leaf(K), Node(Leaf(K), Leaf(I))))), Node(Leaf(K), Node(Leaf(K), Leaf(I)))) it should "church number" in {