Skip to content

Instantly share code, notes, and snippets.

@inanna-malick
Last active December 28, 2025 21:28
Show Gist options
  • Select an option

  • Save inanna-malick/1a212e5a5ba1a6728a2de0af6031bc71 to your computer and use it in GitHub Desktop.

Select an option

Save inanna-malick/1a212e5a5ba1a6728a2de0af6031bc71 to your computer and use it in GitHub Desktop.
Liberties Constrain; Constraints Liberate

Liberties Constrain; Constraints Liberate

The more polymorphic your function, the less it can do—and the more you know about it from the type alone.

-- Very constrained type → almost no implementation freedom
mystery :: forall a. [a] -> [a]
-- Can only rearrange, duplicate, or drop elements.
-- Cannot create new 'a's, or compare them for equality/ordering.
-- Parametricity gives you theorems for free. Lack of constraints provides documentation
-- Likely something simple like 'return all but the first element of the list `[a]`


-- Specifically constrained type
mystery :: forall a. Ord => [a] -> [a]
-- Can only compare elements, nothing else. Almost certainly a sorting function
-- Constraints provide guidance and documentation.

-- Very specific type → implementation could do anything
mystery' :: [Int] -> [Int]
-- Could return [42] always, could sum them, could do anything.
@inanna-malick
Copy link
Author

Legend: for mystery :: forall a. Ord a => [a] -> [a] means a function named mystery having the type (formal shape) of 'for all values (generic) of name a (forall a) that implement Ordering (Ord) we have a function from a list of a ([a]) to another list of a ([a]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment