Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's start with a code example:</p> <pre><code>foob :: forall a b. (b -&gt; b) -&gt; b -&gt; (a -&gt; b) -&gt; Maybe a -&gt; b foob postProcess onNothin onJust mval = postProcess val where val :: b val = maybe onNothin onJust mval </code></pre> <p>This code doesn't compile (syntax error) in plain Haskell 98. It requires an extension to support the <code>forall</code> keyword.</p> <p>Basically, there are 3 <strong>different</strong> common uses for the <code>forall</code> keyword (or at least so it <em>seems</em>), and each has its own Haskell extension: <code>ScopedTypeVariables</code>, <code>RankNTypes</code>/<code>Rank2Types</code>, <code>ExistentialQuantification</code>.</p> <p>The code above doesn't get a syntax error with either of those enabled, but only type-checks with <code>ScopedTypeVariables</code> enabled.</p> <p><strong>Scoped Type Variables:</strong></p> <p>Scoped type variables helps one specify types for code inside <code>where</code> clauses. It makes the <code>b</code> in <code>val :: b</code> the same one as the <code>b</code> in <code>foob :: forall a b. (b -&gt; b) -&gt; b -&gt; (a -&gt; b) -&gt; Maybe a -&gt; b</code>.</p> <p><em>A confusing point</em>: you may hear that when you omit the <code>forall</code> from a type it is actually still implicitly there. (<a href="https://stackoverflow.com/questions/3071136/what-does-the-forall-keyword-in-haskell-ghc-do/3071365#3071365">from Norman's answer: "normally these languages omit the forall from polymorphic types"</a>). This claim is correct, <strong>but</strong> it refers to the other uses of <code>forall</code>, and not to the <code>ScopedTypeVariables</code> use.</p> <p><strong>Rank-N-Types:</strong></p> <p>Let's start with that <code>mayb :: b -&gt; (a -&gt; b) -&gt; Maybe a -&gt; b</code> is equivalent to <code>mayb :: forall a b. b -&gt; (a -&gt; b) -&gt; Maybe a -&gt; b</code>, <strong>except</strong> for when <code>ScopedTypeVariables</code> is enabled.</p> <p>This means that it works for every <code>a</code> and <code>b</code>.</p> <p>Let's say you want to do something like this.</p> <pre><code>ghci&gt; let putInList x = [x] ghci&gt; liftTup putInList (5, "Blah") ([5], ["Blah"]) </code></pre> <p>What's must be the type of this <code>liftTup</code>? It's <code>liftTup :: (forall x. x -&gt; f x) -&gt; (a, b) -&gt; (f a, f b)</code>. To see why, let's try to code it:</p> <pre><code>ghci&gt; let liftTup liftFunc (a, b) = (liftFunc a, liftFunc b) ghci&gt; liftTup (\x -&gt; [x]) (5, "Hello") No instance for (Num [Char]) ... ghci&gt; -- huh? ghci&gt; :t liftTup liftTup :: (t -&gt; t1) -&gt; (t, t) -&gt; (t1, t1) </code></pre> <p>"Hmm.. why does GHC infer that the tuple must contain two of the same type? Let's tell it they don't have to be"</p> <pre><code>-- test.hs liftTup :: (x -&gt; f x) -&gt; (a, b) -&gt; (f a, f b) liftTup liftFunc (t, v) = (liftFunc t, liftFunc v) ghci&gt; :l test.hs Couldnt match expected type 'x' against inferred type 'b' ... </code></pre> <p>Hmm. so here ghc doesn't let us apply <code>liftFunc</code> on <code>v</code> because <code>v :: b</code> and <code>liftFunc</code> wants an <code>x</code>. We really want our function to get a function that accepts any possible <code>x</code>!</p> <pre><code>{-# LANGUAGE RankNTypes #-} liftTup :: (forall x. x -&gt; f x) -&gt; (a, b) -&gt; (f a, f b) liftTup liftFunc (t, v) = (liftFunc t, liftFunc v) </code></pre> <p>So it's not <code>liftTup</code> that works for all <code>x</code>, it's the function that it gets that does.</p> <p><strong>Existential Quantification:</strong></p> <p>Let's use an example:</p> <pre><code>-- test.hs {-# LANGUAGE ExistentialQuantification #-} data EQList = forall a. EQList [a] eqListLen :: EQList -&gt; Int eqListLen (EQList x) = length x ghci&gt; :l test.hs ghci&gt; eqListLen $ EQList ["Hello", "World"] 2 </code></pre> <p>How is that different from Rank-N-Types?</p> <pre><code>ghci&gt; :set -XRankNTypes ghci&gt; length (["Hello", "World"] :: forall a. [a]) Couldnt match expected type 'a' against inferred type '[Char]' ... </code></pre> <p>With Rank-N-Types, <code>forall a</code> meant that your expression must fit all possible <code>a</code>s. For example:</p> <pre><code>ghci&gt; length ([] :: forall a. [a]) 0 </code></pre> <p>An empty list does work as a list of any type.</p> <p>So with Existential-Quantification, <code>forall</code>s in <code>data</code> definitions mean that, the value contained <strong>can</strong> be of <strong>any</strong> suitable type, not that it <strong>must</strong> be of <strong>all</strong> suitable types.</p>
    singulars
    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. 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