Skip to content

ExplicitForall

theGhostJW edited this page Jan 11, 2019 · 1 revision

Explicit forall

Source - Stack Overflow question

There is only really one thing to remember about 'forall': it binds types to some scope. Once you understand that, everything is fairly easy. It is the equivalent of 'lambda' (or a form of 'let') on the type level

Universal qualification vs Existential quantification

Universal qualification

Works for Every Type i.e. is unconstrained:

Form of a unversally quantified type:

#!haskell
forall a. f a.

A value of that type can be thought of as a function that takes a type a as its argument and returns a value of type f a. Except that in Haskell these type arguments are passed implicitly by the type system. This "function" has to give you the same value no matter which type it receives, so the value is polymorphic.

Existential quantification

Running Example

AST Using Normal Data Type

#!haskell

data Expr = I Int         -- integer constants
          | Add Expr Expr -- add two expressions
          | Mul Expr Expr -- multiply two expressions
          deriving Show

eval :: Expr -> Int
eval (I n)       = n
eval (Add e1 e2) = eval e1 + eval e2
eval (Mul e1 e2) = eval e1 * eval e2