Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This corresponds to existential quantification. In pseudo-Haskell,</p> <pre><code>type NumberList = exists a . Num a =&gt; [a] </code></pre> <p>I say «pseudo» because GHC doesn't allow introducing existential quantifiers on the fly — you need to create a separate datatype for that.</p> <p>Now, most of the type you'd use NumberList to the left of the arrow, where «exists» effectively changes its meaning to «forall».</p> <p>That is, instead of writing</p> <pre><code>isIncreasing :: NumberList -&gt; Bool </code></pre> <p>which is the same as</p> <pre><code>isIncreasing :: (exists a . Num a =&gt; [a]) -&gt; Bool </code></pre> <p>you can write</p> <pre><code>isIncreasing :: forall a . Num a =&gt; [a] -&gt; Bool </code></pre> <p>or simply</p> <pre><code>isIncreasing :: Num a =&gt; [a] -&gt; Bool </code></pre> <p>Of course, having a type synonym seems like less code, but it has disadvantages as well. These disadvantages, by the way, are typical for object-oriented programming, which is based on the existential approach.</p> <p>For example, you want to concatenate two lists. Ordinarily you would write</p> <pre><code>(++) :: forall a . [a] -&gt; [a] -&gt; [a] </code></pre> <p>(where again <code>forall</code> is unnecessary and added for clarity). Since <code>a</code> is the same across the entire signature, that ensures that you are concatenating lists of the same type.</p> <p>How do we concatenate two numeric lists? A signature</p> <pre><code>(++) :: NumberList -&gt; NumberList -&gt; NumberList </code></pre> <p>wouldn't work, since one list may contain Ints and the other may contain Doubles. And the resulting NumberList has to contain values of a single type.</p> <p>Or, say, you want to find the sum of the list elements.</p> <p>Usually you write</p> <pre><code>sum :: Num a =&gt; [a] -&gt; a </code></pre> <p>Notice that the result type is the same as the type of list elements. Alas, we cannot do the same for NumberList!</p> <pre><code>sum :: NumberList -&gt; ??? </code></pre> <p>What is the result type? We could apply existential quantification there as well.</p> <pre><code>sum :: NumberList -&gt; (exists a . Num a =&gt; a) </code></pre> <p>But now the connection between the original list type and the sum type is lost — at least for Haskell's type system. If you then decide to write a function like</p> <pre><code>multiplySum :: Integer -&gt; [Integer] -&gt; Integer multiplySum x ys = x * sum ys </code></pre> <p>then you'll get a type error, because <code>sum ys</code> may be of any type, not necessarily of type Integer.</p> <p>It would work if you pushed everything to extreme and made every type existentially-quantified — but then you'd end up essentially with another object-oriented-like language with all their problems.</p> <p>(That said, there are some good use cases for existential quantification, of course.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload