alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Forall.scala)

This example Scala source code file (Forall.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

control, cps, dne, forall, foralls, not, nothing, throwable

The Forall.scala Scala example source code

package scalaz

/** A universally quantified value */
trait Forall[P[_]] { self =>
  def apply[A]: P[A]

  /** `Forall` is an endofunctor in an endofunctor category */
  def map[Q[_]](f: P ~> Q) = new Forall[Q] {
    def apply[A]: Q[A] = f(self.apply)
  }
}

object Forall extends Foralls

trait Foralls {
  /** Universal quantification by doubly negating an existential. */
  type Not[A] = A => Nothing
  type DNE[P[_]] = Not[P[A]] forSome {type A}
  type CPS[P[_]] = Not[DNE[P]]

  /** Construct a universal quantifier by continuation-passing. */
  def apply[P[_]](p: CPS[P]): Forall[P] = new Forall[P] {
    def apply[A]: P[A] = {
      case class Control(arg: P[A]) extends Throwable
      try {
        p((arg: P[A]) => throw new Control(arg))
      } catch {
        case Control(arg) => arg
      }
    }
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Forall.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.