Skip to content

Commit

Permalink
improve type inference of NonDet handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
moleike committed Jan 7, 2024
1 parent 0dd5c04 commit bd1be19
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
13 changes: 12 additions & 1 deletion core/src/main/scala/evidence/effect/NonDet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import cats.Alternative
import cats.Monad
import cats.implicits._
import evidence.Ctx.In
import cats.Foldable
import cats.MonoidK

type NonDet = [E, Ans] =>> NonDet.Syn[E, Ans]

Expand Down Expand Up @@ -33,7 +35,11 @@ object NonDet:
NonDet.choose.ifM(x, y)
}

def allResults[E, A, F[_]](using
def choice[E, A, G[_]](
ps: G[Eff[E, A]]
)(using G: Foldable[G], F: MonoidK[Eff[E, *]]): Eff[E, A] = G.foldK(ps)

def amb[E, A, F[_]](using
F: Alternative[F]
): Eff[NonDet :* E, A] => Eff[E, F[A]] =
Eff.handlerRet(
Expand All @@ -49,3 +55,8 @@ object NonDet:
,
_
)

def allResults[E, A]: Eff[NonDet :* E, A] => Eff[E, List[A]] = amb[E, A, List]

def firstResult[E, A]: Eff[NonDet :* E, A] => Eff[E, Option[A]] =
amb[E, A, Option]
14 changes: 7 additions & 7 deletions examples/src/main/scala/Example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object Example:
y <- NonDet.choose
yield (x && !y) || (!x && y)

println(NonDet.allResults[Nothing, Boolean, Vector](xor).run)
println(NonDet.allResults(xor).run)

def branches[F[_]: Monad](using
F: MonoidK[F],
Expand All @@ -135,9 +135,9 @@ object Example:
y <- G.foldK(List(4, 5, 6).map(_.pure[F]))
yield (x, y)

println(branches[Vector])
println(branches[List])

println(NonDet.allResults[Nothing, (Int, Int), Vector](branches).run)
println(NonDet.allResults(branches).run)

// def logging[F[_]: Monad](implicit F: Tell[F, Log]): F[Unit] =
// // Example of some logging activity in your application
Expand Down Expand Up @@ -180,7 +180,7 @@ object Example:

println(
NonDet
.allResults[Nothing, (Int, String), Option](
.firstResult(
Parse.parse("999")(Parse.digit)
)
.run
Expand All @@ -191,7 +191,7 @@ object Example:

println(
NonDet
.allResults[Nothing, (Int, String), Option](
.firstResult(
Parse.parse("[12345678]")(
brackets(Parse.number)
)
Expand All @@ -201,9 +201,9 @@ object Example:

println(
NonDet
.allResults[Nothing, (String, String), Option](
.firstResult(
Parse.parse("bazz")(
Parse.choice(
NonDet.choice(
Parse.string("foo") :: Parse
.string("bar") :: Parse.string("baz") :: Nil
)
Expand Down
8 changes: 2 additions & 6 deletions examples/src/main/scala/Parse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import evidence.*
import evidence.effect.*
import cats.implicits._
import evidence.effect.NonDet.given
import cats.Alternative
import cats.Monad
import cats.MonoidK
import cats.Foldable
import cats.Alternative

type Parse = [E, Ans] =>> Parse.Syn[E, Ans]

Expand All @@ -24,18 +24,14 @@ object Parse:
)(p)

def many[E, A](p: Eff[E, A]): NonDet :? E ?=> Eff[E, List[A]] =
Alternative[Eff[E, *]].combineK(many1(p), List.empty[A].pure)
Alternative[Eff[E, *]].combineK(many1(p), Nil.pure)

def many1[E, A](p: Eff[E, A]): NonDet :? E ?=> Eff[E, List[A]] =
for
x <- p
xs <- many(p)
yield x :: xs

def choice[F[_]: Monad, G[_], A](
ps: G[F[A]]
)(using F: MonoidK[F], G: Foldable[G]): F[A] = G.foldK(ps)

def parse[E, A](
input: String
): NonDet :? E ?=> Eff[Parse :* E, A] => Eff[E, (A, String)] =
Expand Down

0 comments on commit bd1be19

Please sign in to comment.