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 8200cab
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 31 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,8 +1,7 @@
package pl.writeonly.catculator.core.calculators.lazyk

import pl.writeonly.catculator.core.adt.calculus.Combinator
import pl.writeonly.catculator.core.adt.calculus.Combinator.CombinatorBT
import pl.writeonly.catculator.core.adt.calculus.Constants
import pl.writeonly.catculator.core.adt.calculus._
import pl.writeonly.catculator.core.adt.tree.BinaryTree
import pl.writeonly.catculator.core.adt.tree.BinaryTree._
import spire.math.Natural
Expand All @@ -14,8 +13,10 @@ enum ADT:

object ADT {

type Safe[A] = Either[String, A]

type ADTBT = BinaryTree[ADT]
type ADTBTSafe = Either[String, ADTBT]
type ADTBTSafe = Safe[ADTBT]

val number0: ADTBT = Leaf(ADT.Num(Natural.zero))
val number1: ADTBT = Leaf(ADT.Num(Natural.one))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.writeonly.catculator.core.calculators.lazyk

import cats.Applicative
import pl.writeonly.catculator.core.adt.calculus.Combinator
import pl.writeonly.catculator.core.adt.calculus.Combinator._
import pl.writeonly.catculator.core.adt.tree.BinaryTree
Expand All @@ -15,12 +16,7 @@ object Reducer {
case x => Right(x)
}

def applyM(fM: ADTBTSafe, xM: ADTBTSafe): ADTBTSafe =
for
f <- fM
x <- xM
r <- apply(f, x)
yield r
def applyM(f: ADTBTSafe, x: ADTBTSafe): ADTBTSafe = Applicative[Safe[*]].map2(f, x)(apply).flatten

def flippedApply(x: ADTBT, y: ADTBT): ADTBTSafe = apply(y, x)

Expand All @@ -36,5 +32,4 @@ object Reducer {
case Leaf(ADT.Num(x)) => Right(Leaf(ADT.Num(x + Natural.one)))
case _ => Left(s"attempted to apply inc to a non-number $x")
}

}
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package pl.writeonly.catculator.core.calculators.lazyk

import pl.writeonly.catculator.core.UnitSpec
import pl.writeonly.catculator.core.adt.calculus.Combinator.*
import pl.writeonly.catculator.core.adt.tree.BinaryTree.*
import pl.writeonly.catculator.core.calculators.lazyk.ADT.*
import pl.writeonly.catculator.core.calculators.lazyk.Reducer.*
import pl.writeonly.catculator.core.adt.calculus.Combinator._
import pl.writeonly.catculator.core.adt.tree.BinaryTree._
import pl.writeonly.catculator.core.calculators.lazyk.ADT._
import pl.writeonly.catculator.core.calculators.lazyk.Reducer._

class ReducerSpec extends UnitSpec {
"A Reducer" should {
Expand Down

0 comments on commit 8200cab

Please sign in to comment.