Skip to content

Commit

Permalink
PolyConf should accept step
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Dec 25, 2023
1 parent f000c6e commit 81a5ae1
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions shared/src/test/scala/cpstest/PolyContT.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
package cpstest

import cps.CpsTryMonad
import scala.util.*
import cps.*
import cpstest.PolyContT.PolyStepTry


sealed trait PolyContT[F[_], A] {

basePolyContT =>

def run[B](f: A => F[B]): F[B]
def run[B](f: A => F[B])(using m: CpsTryMonad[F]): F[B] =
runTry{
case Success(a) => f(a)
case Failure(ex) => m.fromTry(Failure(ex))
}

def runTry[B](f: Try[A]=>F[B]): F[B]

def map[B](f: A => B)(using m:CpsTryMonad[F]): PolyContT[F,B] =
new PolyContT[F,B]{
override def run[C](g: B => F[C]): F[C] =
m.flatMap(basePolyContT.run(x => summon[CpsTryMonad[F]].pure(f(x))))(g)
override def runTry[C](g: Try[B] => F[C]): F[C] =
m.flatMapTry(basePolyContT.run(x => summon[CpsTryMonad[F]].pure(f(x))))(g)
}

def flatMap[B](f: A => PolyContT[F,B])(using m:CpsTryMonad[F]): PolyContT[F,B] =
new PolyContT[F,B]{
override def runTry[C](g: Try[B]=> F[C]): F[C] =
basePolyContT.run(a => f(a).runTry(g))
}

def flatMapTry[B](f: Try[A] => PolyContT[F,B])(using m:CpsTryMonad[F]): PolyContT[F,B] =
new PolyContT[F,B]{
override def runTry[C](g: Try[B]=> F[C]): F[C] =
basePolyContT.runTry(a => f(a).runTry(g))
}


def shiftTry(poly: PolyStepTry[F,A])(using m:CpsTryMonad[F]): PolyContT[F,A] =
new PolyContT[F,A]{
override def runTry[B](f: Try[A] => F[B]): F[B] =
poly.run(f)
}

}



object PolyContT {

sealed trait ContTStep[F[_],A,B] extends Function1[A, F[B]] {

}

trait ContTStep[F[_],A,B] extends Function1[A, F[B]]

case class FunStep[F[_],A,B](f: A => F[B]) extends ContTStep[F,A,B] {
def apply(a: A): F[B] = f(a)
Expand All @@ -36,5 +60,9 @@ object PolyContT {
summon[CpsTryMonad[F]].flatMap(f(a))(g)
}

trait PolyStepTry[F[_],A] {
def run[B](k: (Try[A]=>F[B])): F[B]
}


}

0 comments on commit 81a5ae1

Please sign in to comment.