All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalaz.Id.scala Maven / Gradle / Ivy

The newest version!
package org.specs2.internal.scalaz

/** Mixed into object `Id` in the package object [[scalaz]]. */
trait IdInstances {

  // Declaring here instead of the scalaz package object in order to avoid compiler crash in 2.9.2.

  /** The strict identity type constructor. Can be thought of as `Tuple1`, but with no
   *  runtime representation.
   */
  type Id[+X] = X

  // TODO Review!
  type Identity[+X] = Need[X]

  val id: Traverse[Id] with Each[Id] with Monad[Id] with Comonad[Id] with Cojoin[Id] with Distributive[Id] with Zip[Id] with Unzip[Id] with Cozip[Id] =
    new Traverse[Id] with Each[Id] with Monad[Id] with Comonad[Id] with Cobind.FromCojoin[Id] with Distributive[Id] with Zip[Id] with Unzip[Id] with Cozip[Id] {
      def point[A](a: => A): A = a

      def bind[A, B](a: A)(f: A => B): B = f(a)

      def cojoin[A](a: Id[A]): A = a

      def copoint[A](p: Id[A]): A = p

      def zip[A, B](a: => Id[A], b: => Id[B]): (A, B) = (a, b)

      def unzip[A, B](a: Id[(A, B)]): (A, B) = (a._1, a._2)

      def cozip[A, B](a: Id[A \/ B]): (A \/ B) = a

      def traverseImpl[G[_] : Applicative, A, B](fa: Id[A])(f: (A) => G[B]): G[Id[B]] = f(fa)

      def distributeImpl[G[_] : Functor, A, B](fa: G[A])(f: (A) => Id[B]): Id[G[B]] = Functor[G].map(fa)(f)

      override def foldRight[A, B](fa: Id[A], z: => B)(f: (A, => B) => B): B = f(fa, z)

      // Overrides for efficiency.

      override def lift[A, B](f: (A) => B): Id[A] => Id[B] = f

      // `ffa: Id[Id[A]]`, gives, "cyclic aliasing or subtyping involving type Id", but `ffa: A` is identical.
      override def join[A](ffa: A) = ffa

      override def traverse[A, G[_], B](value: G[A])(f: A => Id[B])(implicit G: Traverse[G]): Id[G[B]] = G.map(value)(f)

      override def sequence[A, G[_] : Traverse](as: G[Id[A]]): Id[G[A]] = as

      override def ap[A, B](fa: => Id[A])(f: => Id[A => B]): Id[B] = f(fa)

      def each[A](fa: Id[A])(f: (A) => Unit) {
        f(fa)
      }

      override def compose[G[_]](implicit G0: Applicative[G]): Applicative[G] = G0

      // TODO Fun compiler bug? "can't existentially abstract over parameterized type G"
      // override def product1[G[_]](implicit G0: Applicative[G]): Applicative[G] = G0
    }
}

object Id extends IdInstances




© 2015 - 2025 Weber Informatics LLC | Privacy Policy