PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/main/scala/scalaz/Forall.scala

http://github.com/scalaz/scalaz
Scala | 32 lines | 23 code | 5 blank | 4 comment | 0 complexity | 83050a4fcb500507165c8cfa2ce26e0d MD5 | raw file
 1package scalaz
 2
 3/** A universally quantified value */
 4trait Forall[P[_]] { self =>
 5  def apply[A]: P[A]
 6
 7  /** `Forall` is an endofunctor in an endofunctor category */
 8  def map[Q[_]](f: P ~> Q) = new Forall[Q] {
 9    def apply[A]: Q[A] = f(self.apply)
10  }
11}
12
13object Forall extends Foralls
14
15trait Foralls {
16  /** Universal quantification by doubly negating an existential. */
17  type Not[A] = A => Nothing
18  type DNE[P[_]] = Not[P[A]] forSome {type A}
19  type CPS[P[_]] = Not[DNE[P]]
20
21  /** Construct a universal quantifier by continuation-passing. */
22  def apply[P[_]](p: CPS[P]): Forall[P] = new Forall[P] {
23    def apply[A]: P[A] = {
24      case class Control(arg: P[A]) extends Throwable(null, null, true, false)
25      try {
26        p((arg: P[A]) => throw new Control(arg))
27      } catch {
28        case Control(arg) => arg
29      }
30    }
31  }
32}