The Hitchiker's Guide to the Curry-Howard Correspondence
Chris Ford (@ctford)

Don't panic!
Types are propositions.
Construction is proof.
“Arthur Dent” : String
7 : Integer
String : Typenot : Bool -> Bool
(“foo”, 23) : (String, Integer)
Left 9 : Either Integer bValues and types
identity : a -> a
identity x = xa→a

Haskell Curry
William Howard


(a→b)→a→b
apply : (a -> b) -> a -> b
apply f x = f xIdris> apply chr 65
'A' : CharApplication / implication
Idris> apply chr 66
'B' : Charchr : Integer -> Char(a→b)→(b→c)→(a→c)
comp : (a->b) -> (b->c) -> (a->c)
comp f g x = g (f x)Idris> comp length (7==)
_ : List a -> Boollength : List a -> Integer
(==3) : Integer -> BoolComposition / modus ponens
a ∧ b
conjunction : a -> b -> (a, b)
conjunction x y = (x, y)a ∨ b
disjunctionl : a -> Either a b
disjunctionl x = Left xdisjunctionr : b -> Either a b
disjunctionr y = Right ya→a
(a→b)→a→b
a→b→(a, b)
(a, b) → b
a→b
(a→b)→(b→a)
Not : Type -> Type
Not p = p -> FalsityFalsity : Type
Falsity = Void⊥
second : Not (Either a b) -> (Not a, Not b)
second f = (f . Left, f . Right)first : Either (Not a) (Not b) -> Not (a, b)
first (Left f) (x, _) = f x
first (Right g) (_, y) = g ysecondInverse : (Not a, Not b) -> Not (Either a b)
secondInverse (f, _) (Left x) = f x
secondInverse (_, g) (Right y) = g yDe Morgan's Laws
EverythingIsTrue : Type
EverythingIsTrue = (a : Type) -> aCantProveEverything : Not EverythingIsTrue
CantProveEverything f = f FalsityNot all propositions
Harmless
NullPointerException
council.office.Lock.acquire (Lock.java:42)
council.office.FilingCabinet.find (FilingCabinet.java:42)
council.office.Leopard.beware (Leopard.java:42)
council.office.Lavatory.find (Lavatory.java:42)
council.office.Cellar.find (Cellar.java:42)
council.office.Consultation.post (Consultation.java:42)
council.policy.Bypass.plan (Bypass.java:42)
council.policy.ExpansionManager.execute (ExpansionManager.java:42)
council.policy.Budget.spend (Budget.java:42)Mostly harmless
Haskell> head []
*** Exception: Prelude.head:
*** empty listHaskell> last [1..]
*** Interrupted.Partial function
head : Vect (n + 1) a -> a
head (x::_) = xIdris> head []
Can't unify Vect 0 a
with Vect (n + 1) iTypeTotal function
oops : a -> b
oops x = oops x"Proving" absurdity
data Even : Nat -> Type where
Zero : Even 0
Next : Even n -> Even (n + 2)Zero : Even 0
Next (Next (Next Zero)) : Even 6Even naturals
add : Even m -> Even n -> Even (m + n)
add Zero y = y
add (Next x) y = Next (add x y)fortyTwoIsEven : Even 42
fortyTwoIsEven = mul 21 (Next Zero)
where
mul : (n:Nat) -> Even m -> Even (n*m)
mul Z _ = Zero
mul (S n) e = add e (mul n e)A provably even number
Even 3
¬Even 3
threeAintEven : Not (Even 3)
threeAintEven (Next e) with (e)
| (Next _) impossible
| Zero impossible()
LifeTheUniverseAndEverything
→
Even 42
References
Edwin Brady
Programming in Idris: A Tutorial
idris-lang.org
Brian McKenna
EvenOdd in Agda, Idris, Haskell, Scala
brianmckenna.org
Philip Wadler
Propositions as Types
wadler.blogspot.co.uk
The Hitchiker's Guide to the Curry-Howard Correspondence
By shows
The Hitchiker's Guide to the Curry-Howard Correspondence
Don't Panic! The Curry-Howard Correspondence is an elegant bridge between the planet of logic and the planet of programming, and it's not actually that hard to understand.
- 622