Lesson 25 of 55
9 mins readPython Type Inference & Type Variables
In Plain English
Haskell uses the Hindley-Milner type system to infer the most general type of every expression automatically. Type variables (lowercase like a, b) enable parametric polymorphism.
Deep Dive: How It Works
Type Variables: In id :: a -> a, a is a type variable that can represent any concrete type (Int, String, [Bool]).
Parametric Polymorphism: Functions like length :: [a] -> Int work uniformly across lists of any element type.
Type vs Value Names: Types and type constructors always start with capital letters (Int, Bool, Maybe); variables and type parameters start with lowercase (x, a, elem).
Core Rules to Remember

Complete Type Inference: Type signatures are optional because the compiler infers them, but writing them is best practice.

Capitalization Distinction: Capitalized names are concrete Types/Constructors; lowercase names are variables.
Live Interactive Example
Hit Run Code to see it liveParametric Polymorphic Functions
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: myId and myConst work across any input types seamlessly.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Implement Polymorphic First Element Function
Define `firstElem :: (a, b) -> a` where `firstElem (x, _) = x`.
Print `"First: "` followed by `show (firstElem (123, "test"))`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.