Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed May 15, 2024
1 parent d844486 commit 96cc29f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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._
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 96cc29f

Please sign in to comment.