-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #845 from OlivierBlanvillain/free-invariant
Add InvariantMonoidal and FreeInvariantMonoidal
- Loading branch information
Showing
28 changed files
with
669 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cats | ||
|
||
import cats.functor.Invariant | ||
import simulacrum.typeclass | ||
|
||
/** | ||
* Invariant version of a Monoidal. | ||
* | ||
* Must obey the laws defined in cats.laws.InvariantMonoidalLaws. | ||
*/ | ||
@typeclass trait InvariantMonoidal[F[_]] extends Invariant[F] with Cartesian[F] { | ||
def pure[A](a: A): F[A] | ||
} | ||
|
||
object InvariantMonoidal extends KernelInvariantMonoidalInstances | ||
|
||
/** | ||
* InvariantMonoidal instances for types that are housed in cats.kernel and therefore | ||
* can't have instances for this type class in their companion objects. | ||
*/ | ||
private[cats] trait KernelInvariantMonoidalInstances { | ||
implicit val catsInvariantMonoidalSemigroup: InvariantMonoidal[Semigroup] = new InvariantMonoidal[Semigroup] { | ||
def product[A, B](fa: Semigroup[A], fb: Semigroup[B]): Semigroup[(A, B)] = new Semigroup[(A, B)] { | ||
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2) | ||
} | ||
|
||
def imap[A, B](fa: Semigroup[A])(f: A => B)(g: B => A): Semigroup[B] = new Semigroup[B] { | ||
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y))) | ||
} | ||
|
||
def pure[A](a: A): Semigroup[A] = new Semigroup[A] { | ||
def combine(x: A, y: A): A = a | ||
} | ||
} | ||
|
||
implicit val catsInvariantMonoidalMonoid: InvariantMonoidal[Monoid] = new InvariantMonoidal[Monoid] { | ||
def product[A, B](fa: Monoid[A], fb: Monoid[B]): Monoid[(A, B)] = new Monoid[(A, B)] { | ||
val empty = fa.empty -> fb.empty | ||
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2) | ||
} | ||
|
||
def imap[A, B](fa: Monoid[A])(f: A => B)(g: B => A): Monoid[B] = new Monoid[B] { | ||
val empty = f(fa.empty) | ||
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y))) | ||
} | ||
|
||
def pure[A](a: A): Monoid[A] = new Monoid[A] { | ||
val empty = a | ||
def combine(x: A, y: A): A = a | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package cats | ||
|
||
|
||
/** | ||
* A type class which abstracts over the ability to lift an M[A] into a | ||
* MonadTransformer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.