Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class-based algebras. #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG/minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- move `attributeCoelgotM` to `attributeElgotAlgebraM`, updating it to the generalized form.
28 changes: 21 additions & 7 deletions core/src/main/scala/matryoshka/AlgebraOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@

package matryoshka

import scalaz._
import scalaz.syntax.comonad._
import scalaz._, Scalaz._

sealed class AlgebraOps[F[_], A](self: Algebra[F, A]) {
def generalize[W[_]: Comonad](implicit F: Functor[F]): GAlgebra[W, F, A] =
node => self(node ∘ (_.copoint))
final class GElgotAlgebraMOps[E[_], G[_], M[_], F[_], A](
self: GElgotAlgebraM[E, G, M, F, A]) {

def generalizeElgot[W[_]: Comonad]: ElgotAlgebra[W, F, A] =
w => self(w.copoint)
def attribute(implicit E: Comonad[E], G: Comonad[G], M: Functor[M], F: Functor[F]) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As GElgotAlgebraM is now a class, this could be added as a method therein to avoid having to define this Ops class.

matryoshka.attribute[E, G, M, F, A](self)
}

final class GAlgebraMOps[G[_], M[_], F[_], A](self: GAlgebraM[G, M, F, A]) {
def generalizeElgot[E[_]: Comonad]: GElgotAlgebraM[E, G, M, F, A] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For operations like this that apply to a GElgotAlgebraM with particular type arguments, you could still define them as methods and then require implicit evidence that the required type parameters have the necessary value, i.e.

final class GElgotAlgebraM[E[_], G[_], M[_], F[_], A](f: E[F[G[A]]] => M[A]) {
  def generalizeElgot[EE[_]: Comonad](implicit ev: GElgotAlgebraM[E, G, M, F, A] === GAlgebraM[G, M, F, A]): GElgotAlgebraM[EE, G, M, F, A] =
    new GElgotAlgebraM(matryoshka.generalizeW[E, F[G[A]], M[A]](f))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this (fixing the final E to be EE), but ended up with a complaint that f is of type E[F[G[A]]] => M[A], but expected F[G[A]] => M[A].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, you'll want to use the evidence parameter to convert the value, try

new GElgotAlgebraM(matryoshka.generalizeW[E, F[G[A]], M[A]](ev(self).f))

matryoshka.generalizeW[E, F[G[A]], M[A]](self)
}

final class ElgotAlgebraMOps[E[_], M[_], F[_], A](self: ElgotAlgebraM[E, M, F, A]) {
def generalize[G[_]: Comonad](implicit EF: Functor[λ[α => E[F[α]]]]):
GElgotAlgebraM[E, G, M, F, A] =
matryoshka.generalizeAlgebra[λ[α => E[F[α]]], G, Id, M, F, A](self)
}

final class GElgotAlgebraOps[E[_], G[_], F[_], A](self: GElgotAlgebra[E, G, F, A]) {
def generalizeM[M[_]: Applicative]: GElgotAlgebraM[E, G, M, F, A] =
matryoshka.generalizeM[M, E[F[G[A]]], A](self)
}
20 changes: 13 additions & 7 deletions core/src/main/scala/matryoshka/CoalgebraOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

package matryoshka

import scalaz._
import scalaz.syntax.monad._
import scalaz._, Scalaz._

sealed class CoalgebraOps[F[_], A](self: Coalgebra[F, A]) {
def generalize[M[_]: Monad](implicit F: Functor[F]): GCoalgebra[M, F, A] =
self(_).map(_.map(_.point[M]))
final class GElgotCoalgebraOps[E[_], G[_], F[_], A](self: GElgotCoalgebra[E, G, F, A]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments here regarding making these methods on GElgotCoalgebra.

def generalizeM[M[_]: Applicative]:
GElgotCoalgebraM[E, G, M, F, A] =
matryoshka.generalizeM[M, A, E[F[G[A]]]](self)
}

def generalizeM[M[_]: Monad]: CoalgebraM[M, F, A] = self(_).point[M]
final class ElgotCoalgebraMOps[E[_], M[_], F[_], A](self: ElgotCoalgebraM[E, M, F, A]) {
def generalize[G[_]: Applicative](implicit MEF: Functor[λ[α => M[E[F[α]]]]]): GElgotCoalgebraM[E, G, M, F, A] =
matryoshka.generalizeCoalgebra[λ[α => M[E[F[α]]]], G, Id, A](self)
}

def generalizeElgot[M[_]: Monad]: CoalgebraM[M, F, A] = self.generalizeM
final class GCoalgebraMOps[G[_], M[_], F[_], A](self: GCoalgebraM[G, M, F, A]) {
def generalizeElgot[E[_]: Applicative](implicit M: Functor[M]): GElgotCoalgebraM[E, G, M, F, A] =
matryoshka.generalizeCoalgebra[M, E, λ[α => F[G[α]]], A](self)
}
12 changes: 6 additions & 6 deletions core/src/main/scala/matryoshka/IdOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ package matryoshka
import scalaz._

sealed class IdOps[A](self: A) {
def hylo[F[_]: Functor, B](f: F[B] => B, g: A => F[A]): B =
def hylo[F[_]: Functor, B](f: Algebra[F, B], g: Coalgebra[F, A]): B =
matryoshka.hylo(self)(f, g)
def hyloM[M[_]: Monad, F[_]: Traverse, B](f: F[B] => M[B], g: A => M[F[A]]):
def hyloM[M[_]: Monad, F[_]: Traverse, B](f: AlgebraM[M, F, B], g: CoalgebraM[M, F, A]):
M[B] =
matryoshka.hyloM(self)(f, g)
def ghylo[F[_]: Functor, W[_]: Comonad, M[_]: Monad, B](
w: DistributiveLaw[F, W],
m: DistributiveLaw[M, F],
f: F[W[B]] => B,
g: A => F[M[A]]):
f: GAlgebra[W, F, B],
g: GCoalgebra[M, F, A]):
B =
matryoshka.ghylo(self)(w, m, f, g)

def chrono[F[_]: Functor, B](
g: F[Cofree[F, B]] => B, f: A => F[Free[F, A]]):
g: GAlgebra[Cofree[F, ?], F, B], f: GCoalgebra[Free[F, ?], F, A]):
B =
matryoshka.chrono(self)(g, f)

Expand All @@ -47,7 +47,7 @@ sealed class IdOps[A](self: A) {
def elgotAna[F[_]: Functor, B](ψ: A => B \/ F[A]): Free[F, B] =
matryoshka.elgotAna(self)(ψ)

def elgot[F[_]: Functor, B](φ: F[B] => B, ψ: A => B \/ F[A]): B =
def elgot[F[_]: Functor, B](φ: Algebra[F, B], ψ: ElgotCoalgebra[B \/ ?, F, A]): B =
matryoshka.elgot(self)(φ, ψ)

def coelgot[F[_]: Functor, B](φ: ((A, F[B])) => B, ψ: A => F[A]): B =
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/matryoshka/Proj.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import scalaz._
/** An extractor to make it easier to pattern-match on arbitrary Recursive
* structures.
*
* NB: This extractor is irrufutable and doesn’t break exhaustiveness checking.
* NB: This extractor is irrefutable and doesn’t break exhaustiveness checking.
*/
object Proj {
def unapply[T[_[_]]: Recursive, F[_]: Functor](obj: T[F]): Some[F[T[F]]] =
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/matryoshka/TraverseT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import simulacrum.typeclass
def transAnaM[M[_]: Monad, F[_]: Functor, G[_]: Traverse](t: T[F])(f: F[T[F]] => M[G[T[F]]]): M[T[G]] =
traverse(t)(f(_).flatMap(_.traverse(transAnaM(_)(f))))

def transApoM[M[_]: Monad, F[_]: Functor, G[_]: Traverse](t: T[F])(f: F[T[F]] => M[G[T[G] \/ T[F]]]):
M[T[G]] =
traverse(t)(f(_).flatMap(_.traverse(_.fold(_.point[M], transApoM(_)(f)))))

def topDownCataM[F[_]: Traverse, M[_]: Monad, A](
t: T[F], a: A)(
f: (A, T[F]) => M[(A, T[F])]):
Expand Down
151 changes: 151 additions & 0 deletions core/src/main/scala/matryoshka/algebra.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2014 - 2015 SlamData Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package matryoshka

import scala.Function1

import scalaz._, Scalaz._

/** The most general algebra, using both generalized and Elgot comonads as
* well as a monad.
*/
final class GElgotAlgebraM[E[_], G[_], M[_], F[_], A](f: E[F[G[A]]] => M[A])
extends Function1[E[F[G[A]]], M[A]] {
def apply(v1: E[F[G[A]]]) = f(v1)
}
object GElgotAlgebraM {
implicit def zip[E[_]: Functor, G[_]: Functor, M[_]: Applicative, F[_]: Functor]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the potential here for zip to get shadowed by a local definition since it's such a common identifier, so it might make sense to call this method gElgotAlgebraMZip or similar.

On further inspection maybe this was intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional as such … but I have never managed to have this implicit found, so I always have to explicitly refer to GElgotAlgebraM.zip.zip, with some type parameters thrown in to get it to work. So the short name seemed useful.

I would be happy to give it a long name if it means that implicit search will find it, but no luck so far.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also add zip as a method on GElgotAlgebraM (and then just delegate to it here) which would eliminate the need to explicitly refer to this instance when you aren't working abstractly with some F[_]: Zip.

Zip[GElgotAlgebraM[E, G, M, F, ?]] =
new Zip[GElgotAlgebraM[E, G, M, F, ?]] {
def zip[A, B](
a: ⇒ GElgotAlgebraM[E, G, M, F, A],
b: ⇒ GElgotAlgebraM[E, G, M, F, B]) =
node => (a(node ∘ (_ ∘ (_ ∘ (_._1)))) ⊛ b(node ∘ (_ ∘ (_ ∘ (_._2)))))((_, _))
}
}

sealed class GElgotCoalgebraM[E[_], G[_], M[_], F[_], A](f: A => M[E[F[G[A]]]])
extends Function1[A, M[E[F[G[A]]]]] {
def apply(v1: A) = f(v1)
}

// NB: This class is needed to avoid the `Id[Id[_]]` “cyclic alias” issue.
final class GCoalgebra[G[_], F[_], A](f: A => F[G[A]])
extends GElgotCoalgebraM[Id, G, Id, F, A](f)

sealed trait ZeroIdInstances {
implicit def toGElgotAlgebraM[E[_], G[_], M[_], F[_], A](f: E[F[G[A]]] => M[A]): GElgotAlgebraM[E, G, M, F, A] =
new GElgotAlgebraM[E, G, M, F, A](f)

implicit def toGElgotAlgebraMOps[E[_], G[_], M[_], F[_], A](
a: GElgotAlgebraM[E, G, M, F, A]):
GElgotAlgebraMOps[E, G, M, F, A] =
new GElgotAlgebraMOps[E, G, M, F, A](a)

implicit def toGElgotCoalgebraM[E[_], G[_], M[_], F[_], A](f: A => M[E[F[G[A]]]]): GElgotCoalgebraM[E, G, M, F, A] =
new GElgotCoalgebraM[E, G, M, F, A](f)

}

sealed trait OneIdInstances extends ZeroIdInstances {
implicit def toGElgotAlgebraOps[E[_], G[_], F[_], A](
a: GElgotAlgebra[E, G, F, A]):
GElgotAlgebraOps[E, G, F, A] =
new GElgotAlgebraOps[E, G, F, A](a)

implicit def toElgotAlgebraMOps[E[_], M[_], F[_], A](
a: ElgotAlgebraM[E, M, F, A]):
ElgotAlgebraMOps[E, M, F, A] =
new ElgotAlgebraMOps[E, M, F, A](a)

implicit def toGAlgebraMOps[G[_], M[_], F[_], A](a: GAlgebraM[G, M, F, A]):
GAlgebraMOps[G, M, F, A] =
new GAlgebraMOps[G, M, F, A](a)


implicit def toGElgotCoalgebraOps[E[_], G[_], F[_], A](
a: GElgotCoalgebra[E, G, F, A]):
GElgotCoalgebraOps[E, G, F, A] =
new GElgotCoalgebraOps[E, G, F, A](a)

implicit def toElgotCoalgebraMOps[E[_], M[_], F[_], A](
a: ElgotCoalgebraM[E, M, F, A]):
ElgotCoalgebraMOps[E, M, F, A] =
new ElgotCoalgebraMOps[E, M, F, A](a)

implicit def toGCoalgebraMOps[G[_], M[_], F[_], A](a: GCoalgebraM[G, M, F, A]):
GCoalgebraMOps[G, M, F, A] =
new GCoalgebraMOps[G, M, F, A](a)
}

sealed trait TwoIdInstances extends OneIdInstances {
// FIXME: somehow this causes an ambiguous implicit with a lower priority
// implicit.
// implicit def toElgotAlgebra[E[_], F[_], A](f: E[F[A]] => A):
// ElgotAlgebra[E, F, A] =
// new GElgotAlgebraM[E, Id, Id, F, A](f)
// Poor man’s unapply trick
implicit def toElgotAlgebraU[E[_[_], _], F[_], A, X[_]](f: E[X, F[A]] => A):
ElgotAlgebra[E[X, ?], F, A] =
new GElgotAlgebraM[E[X, ?], Id, Id, F, A](f)

implicit def toElgotCoalgebra[E[_], F[_], A](f: A => E[F[A]]):
ElgotCoalgebra[E, F, A] =
new GElgotCoalgebraM[E, Id, Id, F, A](f)
// Poor man’s unapply trick
implicit def toElgotCoalgebraU[E[_[_], _], F[_], A, X[_]](f: A => E[X, F[A]]):
ElgotCoalgebra[E[X, ?], F, A] =
new GElgotCoalgebraM[E[X, ?], Id, Id, F, A](f)

implicit def toGAlgebra[G[_], F[_], A](f: F[G[A]] => A): GAlgebra[G, F, A] =
new GElgotAlgebraM[Id, G, Id, F, A](f)
// Poor man’s unapply trick
implicit def toGAlgebraU[G[_[_], _], F[_], A, X[_]](f: F[G[X, A]] => A):
GAlgebra[G[X, ?], F, A] =
new GElgotAlgebraM[Id, G[X, ?], Id, F, A](f)

implicit def toGCoalgebra[G[_], F[_], A](f: A => F[G[A]]):
GCoalgebra[G, F, A] =
new GCoalgebra[G, F, A](f)
// Poor man’s unapply trick
implicit def toGCoalgebraU[G[_[_], _], F[_], A, X[_]](f: A => F[G[X, A]]):
GCoalgebra[G[X, ?], F, A] =
new GCoalgebra[G[X, ?], F, A](f)

implicit def toAlgebraM[M[_], F[_], A](f: F[A] => M[A]): AlgebraM[M, F, A] =
new GElgotAlgebraM[Id, Id, M, F, A](f)
// Poor man’s unapply trick
implicit def toAlgebraMU[M[_[_], _], F[_], A, X[_]](f: F[A] => M[X, A]):
AlgebraM[M[X, ?], F, A] =
new GElgotAlgebraM[Id, Id, M[X, ?], F, A](f)

implicit def toCoalgebraM[M[_], F[_], A](f: A => M[F[A]]):
CoalgebraM[M, F, A] =
new GElgotCoalgebraM[Id, Id, M, F, A](f)
// Poor man’s unapply trick
implicit def toCoalgebraMU[M[_[_], _], F[_], A, X[_]](f: A => M[X, F[A]]):
CoalgebraM[M[X, ?], F, A] =
new GElgotCoalgebraM[Id, Id, M[X, ?], F, A](f)
}

trait ThreeIdInstances extends TwoIdInstances {
implicit def toAlgebra[F[_], A](f: F[A] => A): Algebra[F, A] =
new GElgotAlgebraM[Id, Id, Id, F, A](f)

implicit def toCoalgebra[F[_], A](f: A => F[A]): Coalgebra[F, A] =
new GCoalgebra[Id, F, A](f)
}
3 changes: 3 additions & 0 deletions core/src/main/scala/matryoshka/cofree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ trait CofreeInstances {
implicit def cofreeShow[F[_], A: Show](implicit F: (Show ~> λ[α => Show[F[α]]])):
Show[Cofree[F, A]] =
Show.shows(cof => "(" + cof.head.show + ", " + F(cofreeShow).shows(cof.tail) + ")")

implicit def toCofreeOps[F[_], A](a: Cofree[F, A]): CofreeOps[F, A] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could make CofreeOps an implicit class and avoid this extra def. On that note, you can probably make it a value class as well.

new CofreeOps[F, A](a)
}

object cofree extends CofreeInstances
3 changes: 3 additions & 0 deletions core/src/main/scala/matryoshka/free.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ trait FreeInstances {
_.point[Free[G, ?]].point[M],
f(_).map(Free.liftF(_).join))
}

implicit def toFreeOps[F[_], A](a: Free[F, A]): FreeOps[F, A] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as for toCofreeOps.

new FreeOps[F, A](a)
}

object free extends FreeInstances
Loading