Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Existential Quantification over Values

I came across existential quantification over values in the Scala Language Specification (3.2.10 Existential Types).

x: y.Inner forSome{val y : Outer}

Does someone have illustrative use cases for it?

T forSome {val x: S} is defined as T forSome { type t <: S with Singleton }. The Singletron trait is mentioned in the Specification (3.2.1 Singleton Types) but I could not find it in the Scaladoc. Where is it defined?

like image 732
Thomas Jung Avatar asked Feb 03 '10 10:02

Thomas Jung


People also ask

What are existential quantifiers example?

The Existential Quantifier For example, "Someone loves you" could be transformed into the propositional form, x P(x), where: P(x) is the predicate meaning: x loves you, The universe of discourse contains (but is not limited to) all living creatures.

What are the 2 types of quantification?

The two fundamental kinds of quantification in predicate logic are universal quantification and existential quantification.

What do you mean by existential quantifiers?

Definition of existential quantifier : a quantifier (such as for some in "for some x, 2x + 5 = 8") that asserts that there exists at least one value of a variable. — called also existential operator.

What is the difference between universal and existential quantifiers?

The universal quantifier, meaning "for all", "for every", "for each", etc. The existential quantifier, meaning "for some", "there exists", "there is one", etc. A statement of the form: x, if P(x) then Q(x). A statement of the form: x such that, if P(x) then Q(x).


1 Answers

It is useful along with inner classes as alluded in the type names. See for example the Graph and Node classes defined in A Tour of Scala: Inner Classes. Existential quantification over a value is used to write the type of the nodes of some unspecified graph.

type SomeNode = g.Node forSome { val g: Graph }

This might be useful if you wanted to have a method that took two nodes as arguments that had to come from the same graph.

def somethingWithTwoNodes[N <: g.Node forSome { val g: Graph }](n1: N, n2: N) = (n1,n2)

Note that 2.7 will not accept that method definition because it thinks there's some sort of recursion in N.

Then if you have

val g1 = new Graph
val g2 = new Graph

then these compile

somethingWithTwoNodes(g1.newNode, g1.newNode)
somethingWithTwoNodes(g2.newNode, g2.newNode)

but these doesn't

somethingWithTwoNodes(g1.newNode, g2.newNode)
somethingWithTwoNodes(g2.newNode, g1.newNode)

As for the Singleton trait, it's not really defined in the typical way, i.e. there isn't a class file for it. It's like the types Any, AnyVal, AnyRef and Null. It is defined in src/compiler/scala/tools/nsc/symtab/Definitions.scala along with these other types, but I doubt that is very useful information to have. It's also an odd beast being a final trait which means you can't mix it in when defining a trait or class, it's really more of a marker that the compiler ascribes to a type to say that it is unique from any other type.

like image 96
Geoff Reedy Avatar answered Oct 04 '22 01:10

Geoff Reedy