Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix A: Syntax Comparison

For readers coming from other functional languages, these tables map familiar syntax to Lean equivalents. The goal is not completeness but orientation: enough to read Lean code without constantly consulting documentation.

Type Declarations

ConceptHaskellOCamlLean 4
Type aliastype Name = Stringtype name = stringabbrev Name := String
Product typedata Point = Point Int Inttype point = { x: int; y: int }structure Point where x : Int; y : Int
Sum typedata Maybe a = Nothing | Just atype 'a option = None | Some of 'ainductive Option (α : Type) where ...
Recursive typedata List a = Nil | Cons a (List a)type 'a list = Nil | Cons of 'a * ...inductive List (α : Type) where ...
Type classclass Eq a where (==) :: a -> a -> BoolN/A (use modules)class Eq (α : Type) where eq : α → α → Bool
Instanceinstance Eq Int where ...N/Ainstance : Eq Int where ...

Function Definitions

ConceptHaskellOCamlLean 4
Named functionf x = x + 1let f x = x + 1def f (x : Nat) := x + 1
Lambda\x -> x + 1fun x -> x + 1fun x => x + 1
Type signaturef :: Int -> Intval f : int -> intdef f : Int → Int
Pattern matchingcase x of { Just a -> ...; Nothing -> ... }match x with Some a -> ... | None -> ...match x with | some a => ... | none => ...
Guardsf x | x > 0 = ... | otherwise = ...N/A (use if)if x > 0 then ... else ...
Where clausef x = y + 1 where y = x * 2let f x = let y = x * 2 in y + 1def f x := let y := x * 2; y + 1
Partial applicationmap (+1)List.map ((+) 1)List.map (· + 1)

Monads and Effects

ConceptHaskellOCamlLean 4
Bindx >>= f or do { a <- x; f a }N/A (use let*)x >>= f or do let a ← x; f a
Returnreturn x or pure xN/Apure x
Monad transformerStateT s m aN/AStateT σ m α
IO actionIO aunit -> 'aIO α
PrintputStrLn "hello"print_endline "hello"IO.println "hello"

Common Operations

ConceptHaskellOCamlLean 4
List literal[1, 2, 3][1; 2; 3][1, 2, 3]
List consx : xsx :: xsx :: xs
List mapmap f xsList.map f xsxs.map f or List.map f xs
List filterfilter p xsList.filter p xsxs.filter p
Function compositionf . gN/A (use fun x -> f (g x))f ∘ g
String concats1 ++ s2s1 ^ s2s1 ++ s2
Tuple(a, b)(a, b)(a, b)
Tuple accessfst p, snd pfst p, snd pp.1, p.2
If expressionif c then t else fif c then t else fif c then t else f

Key Differences

Explicit types: Lean requires explicit type annotations more often than Haskell. Where Haskell infers id x = x has type a -> a, Lean prefers def id (x : α) : α := x.

Unicode: Lean uses unicode operators freely: for function types, for universal quantification, for conjunction. ASCII alternatives exist (->, forall, /\) but idiomatic Lean uses unicode.

Termination: Every Lean function must terminate. Haskell allows infinite loops; Lean rejects them. Use partial for functions you cannot prove terminating.

Dependent types: Lean’s (n : Nat) → Vector α n has no Haskell equivalent. Types depending on values is what makes Lean a theorem prover.

Propositions vs Booleans: Lean distinguishes Prop (logical propositions, erased at runtime) from Bool (computational booleans). Haskell’s Bool is both.