I'm reading A Little Lens Starter Tutorial and they use a seemingly magical function forall
like this:
>>> forall $ \tuple -> view _1 tuple == fst tuple
True
What exactly does this function do and more importantly what package is it from. I can't find it using Hoogle, etc.
In Haskell, all polymorphic type signatures are considered to be implicitly prefixed with forall . What forall here does is play the role of universal quantifier. For function f , it means it is saying "for all types, this function takes that type and returns the same type.".
() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .
We can define our own types in Haskell using a data declaration, which we introduce via a series of examples (§4.2. 1). The type being defined here is Bool, and it has exactly two values: True and False.
The operator = is used to assign a value to a variable, e.g. phi = 1.618 . The scope of such variables can be controlled by using let or where clauses. x and y are formal parameters for the add function. This corresponds to the notion of "variable" in the lambda calculus.
Haha, embarrassed author here.
I invented forall
to mime quickCheck
and make intuitive sense, but not to be executable. In my defense, I took inspiration from the Little Schemer which happily introduces syntax and semantics far before they're executable in order to build intuition by practice.
Except, of course, my forall
is not actually executable at all and it has lead to a lot of confusion.
So, my suggestion to anyone curious is to investigate quickcheck
as a stand-in for forall
. My suggestion to myself, now carried out, is to edit the tutorial to note my artistic license there.
Sorry about any trouble.
That's not actually executable code. See this answer: https://twitter.com/vh4x0r/status/429256064245067777
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With