Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat does the `forall` keyword in Haskell/GHC do?
    text
    copied!<p>I'm beginning to understand how the <code>forall</code> keyword is used in so-called "existential types" like this:</p> <pre><code>data ShowBox = forall s. Show s =&gt; SB s </code></pre> <p>This is only a subset, however, of how <code>forall</code> is used and I simply cannot wrap my mind around its use in things like this:</p> <pre><code>runST :: forall a. (forall s. ST s a) -&gt; a </code></pre> <p>Or explaining why these are different:</p> <pre><code>foo :: (forall a. a -&gt; a) -&gt; (Char,Bool) bar :: forall a. ((a -&gt; a) -&gt; (Char, Bool)) </code></pre> <p>Or the whole <code>RankNTypes</code> stuff...</p> <p>I tend to prefer clear, jargon-free English rather than the kinds of language which are normal in academic environments. Most of the explanations I attempt to read on this (the ones I can find through search engines) have these problems:</p> <ol> <li>They're incomplete. They explain one part of the use of this keyword (like "existential types") which makes me feel happy until I read code that uses it in a completely different way (like <code>runST</code>, <code>foo</code> and <code>bar</code> above).</li> <li>They're densely packed with assumptions that I've read the latest in whatever branch of discrete math, category theory or abstract algebra is popular this week. (If I never read the words "consult the paper <em>whatever</em> for details of implementation" again, it will be too soon.)</li> <li>They're written in ways that frequently turn even simple concepts into tortuously twisted and fractured grammar and semantics.</li> </ol> <p>So...</p> <p>On to the actual question. Can anybody completely explain the <code>forall</code> keyword in clear, plain English (or, if it exists somewhere, point to such a clear explanation which I've missed) that doesn't assume I'm a mathematician steeped in the jargon?</p> <hr> <p><em>Edited to add:</em></p> <p>There were two stand-out answers from the higher-quality ones below, but unfortunately I can only choose one as best. <a href="https://stackoverflow.com/questions/3071136/what-does-the-forall-keyword-in-haskell-ghc-do/3071365#3071365">Norman's answer</a> was detailed and useful, explaining things in a way that showed some of the theoretical underpinnings of <code>forall</code> and at the same time showing me some of the practical implications of it. <a href="https://stackoverflow.com/questions/3071136/what-does-the-forall-keyword-in-haskell-ghc-do/3071932#3071932">yairchu's answer</a> covered an area nobody else mentioned (scoped type variables) and illustrated all of the concepts with code and a GHCi session. Were it possible to select both as best, I would. Unfortunately I can't and, after looking over both answers closely, I've decided that yairchu's slightly edges out Norman's because of the illustrative code and attached explanation. This is a bit unfair, however, because really I needed both answers to understand this to the point that <code>forall</code> doesn't leave me with a faint sense of dread when I see it in a type signature.</p>
 

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