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 : Type
not : Bool -> Bool
(“foo”, 23) : (String, Integer)
Left 9 : Either Integer b
Values and types
identity : a -> a
identity x = x
a→a
Haskell Curry
William Howard
(a→b)→a→b
apply : (a -> b) -> a -> b
apply f x = f x
Idris> apply chr 65
'A' : Char
Application / implication
Idris> apply chr 66
'B' : Char
chr : 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 -> Bool
length : List a -> Integer
(==3) : Integer -> Bool
Composition / modus ponens
a ∧ b
conjunction : a -> b -> (a, b)
conjunction x y = (x, y)
a ∨ b
disjunctionl : a -> Either a b
disjunctionl x = Left x
disjunctionr : b -> Either a b
disjunctionr y = Right y
a→a
(a→b)→a→b
a→b→(a, b)
(a, b) → b
a→b
(a→b)→(b→a)
Not : Type -> Type
Not p = p -> Falsity
Falsity : 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 y
secondInverse : (Not a, Not b) -> Not (Either a b)
secondInverse (f, _) (Left x) = f x
secondInverse (_, g) (Right y) = g y
De Morgan's Laws
EverythingIsTrue : Type
EverythingIsTrue = (a : Type) -> a
CantProveEverything : Not EverythingIsTrue
CantProveEverything f = f Falsity
Not 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 list
Haskell> last [1..]
*** Interrupted.
Partial function
head : Vect (n + 1) a -> a
head (x::_) = x
Idris> head []
Can't unify Vect 0 a
with Vect (n + 1) iType
Total 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 6
Even 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.
- 521