Skip to content

Commit

Permalink
Move all recursion-related code to a separate file/package
Browse files Browse the repository at this point in the history
  • Loading branch information
vil1 committed Feb 10, 2019
1 parent 2d53a97 commit 2194b7d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 54 deletions.
58 changes: 7 additions & 51 deletions modules/core/src/main/scala/SchemaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,9 @@ package scalaz

package schema

import monocle.Iso

trait HFunctor[H[_[_], _]] {
def hmap[F[_], G[_]](nt: F ~> G): H[F, ?] ~> H[G, ?]
}

final case class Fix[F[_[_], _], A](unFix: F[Fix[F, ?], A])

final case class HEnvT[E, F[_[_], _], G[_], I](ask: E, fa: F[G, I])
import recursion._

object HEnvT {

implicit def hfunctor[E, F[_[_], _]](implicit F: HFunctor[F]): HFunctor[HEnvT[E, F, ?[_], ?]] =
new HFunctor[HEnvT[E, F, ?[_], ?]] {

def hmap[M[_], N[_]](nt: M ~> N) = new (HEnvT[E, F, M, ?] ~> HEnvT[E, F, N, ?]) {
def apply[I](fm: HEnvT[E, F, M, I]) = HEnvT(fm.ask, F.hmap(nt)(fm.fa))
}
}
}
import monocle.Iso

trait Realisation {
type Prim[A]
Expand Down Expand Up @@ -175,18 +158,18 @@ final case class ComposedInterpreter[F[_], G[_], H[_]](underlying: Interpreter[F
}

class CataInterpreter[S[_[_], _], F[_]](
algebra: SchemaF.HAlgebra[S, F]
algebra: HAlgebra[S, F]
)(implicit ev: HFunctor[S])
extends Interpreter[Fix[S, ?], F] {
final override val interpret = SchemaF.cataNT(algebra)
final override val interpret = cataNT(algebra)
}

class HyloInterpreter[S[_[_], _], F[_], G[_]](
coalgebra: SchemaF.HCoalgebra[S, G],
algebra: SchemaF.HAlgebra[S, F]
coalgebra: HCoalgebra[S, G],
algebra: HAlgebra[S, F]
)(implicit ev: HFunctor[S])
extends Interpreter[G, F] {
final override val interpret = SchemaF.hyloNT(coalgebra, algebra)
final override val interpret = hyloNT(coalgebra, algebra)
}

object SchemaF {
Expand Down Expand Up @@ -256,33 +239,6 @@ object SchemaF {
def toSchema = Fix(new :*:(l.toSchema, r.toSchema))

}

// Schema syntax

///////////////////////
// Schema operations
///////////////////////

type HAlgebra[F[_[_], _], G[_]] = F[G, ?] ~> G
type HCoalgebra[F[_[_], _], G[_]] = G ~> F[G, ?]

def cataNT[S[_[_], _], F[_]](
alg: HAlgebra[S, F]
)(implicit S: HFunctor[S]): (Fix[S, ?] ~> F) =
new (Fix[S, ?] ~> F) { self =>

def apply[A](f: Fix[S, A]): F[A] =
alg.apply[A](S.hmap(self)(f.unFix))
}

def hyloNT[S[_[_], _], F[_], G[_]](coalgebra: HCoalgebra[S, F], algebra: HAlgebra[S, G])(
implicit S: HFunctor[S]
): F ~> G = new (F ~> G) { self =>

def apply[A](fa: F[A]): G[A] =
algebra(S.hmap(self)(coalgebra(fa)))
}

}

trait SchemaModule[R <: Realisation] {
Expand Down
4 changes: 2 additions & 2 deletions modules/generic/src/main/scala/GenericAlgebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package schema

package generic

trait GenericSchemaModule[R <: Realisation] extends SchemaModule[R] {
import recursion._

import SchemaF._
trait GenericSchemaModule[R <: Realisation] extends SchemaModule[R] {

def covariantTargetFunctor[H[_]](
primNT: R.Prim ~> H,
Expand Down
3 changes: 2 additions & 1 deletion modules/generic/src/main/scala/ShowModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ package schema

package generic

import recursion._

trait ShowModule[R <: Realisation] extends GenericSchemaModule[R] {
import SchemaF._

implicit val showDecidableInstance: Decidable[Show] = new Decidable[Show] {
override def choose2[Z, A1, A2](a1: => Show[A1], a2: => Show[A2])(f: Z => A1 \/ A2): Show[Z] =
Expand Down
2 changes: 2 additions & 0 deletions modules/play-json/src/main/scala/PlayJsonModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package schema

package play.json

import recursion._

import Scalaz._
import Liskov._
import _root_.play.api.libs.json._
Expand Down

0 comments on commit 2194b7d

Please sign in to comment.