PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/core/src/main/scala/scalaz/Category.scala

http://github.com/scalaz/scalaz
Scala | 73 lines | 39 code | 13 blank | 21 comment | 0 complexity | 2428d604150fe74c374d3b2ed3c95945 MD5 | raw file
 1package scalaz
 2
 3////
 4/**
 5 * [[scalaz.Compose]] with identity.
 6 *
 7 * @see [[scalaz.Category.CategoryLaw]]
 8 */
 9////
10trait Category[=>:[_, _]] extends Compose[=>:] { self =>
11  ////
12  // TODO GeneralizedCategory, GeneralizedFunctor, et al, from Scalaz6 ?
13
14  /** The left and right identity over `compose`. */
15  def id[A]: A =>: A
16
17  /** `monoid`, but universally quantified. */
18  def empty: PlusEmpty[λ[α => α =>: α]] =
19    new PlusEmpty[λ[α => α =>: α]] with ComposePlus {
20      def empty[A] = id
21    }
22
23  /** The endomorphism monoid, where `zero`=`id` and
24    * `append`=`compose`.
25    */
26  def monoid[A]: Monoid[A =>: A] =
27    new Monoid[A =>: A] with ComposeSemigroup[A] {
28      def zero = id
29    }
30
31  trait CategoryLaw extends ComposeLaw {
32    /** `_ <<< id` is vacuous. */
33    def leftIdentity[A, B](ab: (A =>: B))(implicit E: Equal[A =>: B]): Boolean = {
34      val ab1 = compose(ab, id[A])
35      E.equal(ab, ab1)
36    }
37    /** `id <<< _` is vacuous. */
38    def rightIdentity[A, B](ab: (A =>: B))(implicit E: Equal[A =>: B]): Boolean = {
39      val ab1 = compose(id[B], ab)
40      E.equal(ab, ab1)
41    }
42  }
43
44  def categoryLaw = new CategoryLaw {}
45
46  ////
47  val categorySyntax: scalaz.syntax.CategorySyntax[=>:] =
48    new scalaz.syntax.CategorySyntax[=>:] { def F = Category.this }
49}
50
51object Category {
52  @inline def apply[F[_, _]](implicit F: Category[F]): Category[F] = F
53
54  import Isomorphism._
55
56  def fromIso[F[_, _], G[_, _]](D: F <~~> G)(implicit E: Category[G]): Category[F] =
57    new IsomorphismCategory[F, G] {
58      override def G: Category[G] = E
59      override def iso: F <~~> G = D
60    }
61
62  ////
63  ////
64}
65
66trait IsomorphismCategory[F[_, _], G[_, _]] extends Category[F] with IsomorphismCompose[F, G]{
67  implicit def G: Category[G]
68  ////
69
70  override def id[A]: F[A, A] =
71    iso.from(G.id)
72  ////
73}