Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Haskell function forall do and where is it defined?

Tags:

haskell

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.

like image 440
mdm Avatar asked Jan 31 '14 17:01

mdm


People also ask

What does forall do in Haskell?

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.".

What does () mean in Haskell?

() 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 () .

How can you explicitly specify the types of functions in your program in Haskell?

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.

How do I assign a variable in Haskell?

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.


2 Answers

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.

like image 86
J. Abrahamson Avatar answered Sep 22 '22 02:09

J. Abrahamson


That's not actually executable code. See this answer: https://twitter.com/vh4x0r/status/429256064245067777

like image 21
bergey Avatar answered Sep 21 '22 02:09

bergey