klequis / zz-haskell-notebook

Notes from learning Haskell
1 stars 0 forks source link

Parametricity #42

Closed klequis closed 2 years ago

klequis commented 2 years ago

Parametricity is the property that holds in the presence of parametric polymorphism. Parametricity states that the behavior of a function will be uniform across all concrete applications of that function. Parametricity tells us that the function:

id :: a -> a

const must return the first value—parametricity and the definition of the type require it!

f :: a -> a -> a

Here, f can only return the first or second value, nothing else, and it will always return one or the other consistently without changing. If the function f made use of + or *, its type would necessarily be constrained by the type class Num and thus be an example of ad-hoc, rather than parametric, polymorphism.

blahFunc :: b -> String

blahFunc totally ignores its argument and is effectively a constant value of type String that requires a throw-away argument for no reason.

convList :: a -> [a]

Unless the result is [], the resulting list has values that are all the same—and the list will always be the same length.

klequis commented 2 years ago

removed