/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

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