1. Formal Methods Forum
Type Class in Coq
@tmiya
April 20,2011
@tmiya : Type Class in Coq, 1
2. Polymorphism
Polymorphism
“On Understanding Types, Data Abstraction, and Polymorphism”
(Cardelli and Wegner, 1985)
http://lucacardelli.name/Papers/Onunderstanding.a4.Pdf
polymorphism =
• universal:
parametric : ML
inclusion :
• ad-hoc:
overloading : Haskell Type
class overloading
coercion :
@tmiya : Type Class in Coq, 2
3. Monad
— from
Hask : Haskell
Hask : = = id=
Hask m: return fmap
( )
Hask Hask =
= return, fmap
fmap f
ma KS / mb
O O
return fmap return
a /b
f
@tmiya : Type Class in Coq, 3
4. Monad
(1)(2)
bind (return’ a) f = f a µ ◦ ηT = 1T
bind(·)return
ma AI / ma
O > T@
@@
}}} @@
@@
} @ @@
@@
}}
bind
}} @@
@@
} @@
@@
}} ηT
@@
@@
}}} return @@
@@
}} @@
@@
}}
a a T2 /T
µ
bind ma return’ = ma µ ◦ T η = 1T
bind(·)f Tη
ma / mb
BJ / T2
O |
T@
@@
| @@
@@
|| @ @@
@@
||
bind
| @@
@@
|| @@
@@ µ
|| f @@
return
| @@
@@
@@
|| @@
@@
|||
a b T
@tmiya : Type Class in Coq, 4
5. Monad
(3)
bind (bind ma f) g µ ◦ µT
= bind ma (fun a = bind (f a) g) = µ ◦ Tµ
bind(·)f bind(·)g µT
ma / / mc T3 / T2
O p7 mb 7
pp ppp
ppppp ppppp µ
ppp f ppp g
return Tµ
ppp ppp
a b c T2 /T
µ
bind(·)(λa.bind(f (a))g )
/-
ma o7 mb g gg3 mc
bind(·)gg
f ooooo gggg ggggg
ooo ggggg
ooo ggggg λa.bind(f (a))g
a go ggg
og
c
@tmiya : Type Class in Coq, 5
6. Monad M:Type-Type ( : M = option)
A M A
Haskell
Coq
Class Monad (M:Type - Type):Type := {
return’ : forall {A}, A - M A;
bind : forall {A B}, M A - (A - M B) - M B;
left_unit : forall A B (a:A)(f:A - M B),
bind (return’ a) f = f a;
right_unit : forall A (ma:M A),
bind ma return’ = ma;
assoc : forall A B C (ma:M A)(f:A-M B)(g:B-M C),
bind (bind ma f) g = bind ma (fun a = bind (f a) g)
}.
@tmiya : Type Class in Coq, 6
7. Haskell
Haskell = do
Notation m = f := (bind m f)
(left associativity, at level 49).
Notation ’do’ a - e ; c := (e = (fun a = c))
(at level 59, right associativity).
@tmiya : Type Class in Coq, 7
8. Identity
Identity
M = id’ : A - A
( )
Qed. Defined.
Definition id’(A:Type):Type := A.
Instance Identity : Monad id’ := {
return’ A a := a;
bind A B ma f := f ma
}.
Proof.
(* *)
Defined.
@tmiya : Type Class in Coq, 8
9. Maybe
M = option : A - option A Maybe
return’, bind
Instance Maybe : Monad option := {
return’ A a := Some a;
bind A B ma f :=
match ma with
| None = None
| Some a = f a
end
}.
Proof.
(* *)
Defined.
@tmiya : Type Class in Coq, 9
10. Maybe
Eval compute in (None = (fun x = Some (x + 1))).
Eval compute in (Some 1 = (fun x = Some (x + 1))).
@tmiya : Type Class in Coq, 10
11. List
List flat_map
(Hint: xs ++
app_ass )
Require Import List.
Lemma flat_map_app : forall (A B:Type)
(xs ys:list A)(f:A - list B),
flat_map f (xs++ys) = flat_map f xs ++ flat_map f ys.
Instance List : Monad list := {
return’ A x := x :: nil;
bind A B m f := flat_map f m
}.
@tmiya : Type Class in Coq, 11
12. MonadPlus
Monad MonadPlus
Class MonadPlus (M:Type - Type) : Type := {
monad : Monad M ;
(* MonadPlus *)
}.
OO Coq substructure
(= ?)
MonadPlus Monad monoid (monoid mzero
mplus) bind mzero
(mzero = 0, mplus = +, bind=× )
@tmiya : Type Class in Coq, 12
13. MonadPlus
Class MonadPlus (M:Type - Type) : Type := {
monad : Monad M ;
mzero : forall A, M A;
mplus : forall A, M A - M A - M A;
mzero_left : forall A f, bind (@mzero A) f = (@mzero A);
mzero_right : forall A B (ma:M A),
bind ma (fun x = (@mzero B)) = (@mzero B);
monoid_left_unit : forall A (ma:M A),
mplus _ (@mzero A) ma = ma;
monoid_right_unit : forall A (ma:M A),
mplus _ ma (@mzero A) = ma;
monoid_assoc : forall A (ma mb mc:M A),
mplus _ (mplus _ ma mb) mc = mplus _ ma (mplus _ mb mc)
}.
@tmiya : Type Class in Coq, 13
14. MaybePlus
Maybe MaybePlus
Instance MaybePlus : MonadPlus option := {
monad := Maybe;
mzero A := @None A;
mplus A m1 m2 :=
match m1 with
| None = m2
| Some x = m1
end
}.
Proof.
(* *)
Defined.
@tmiya : Type Class in Coq, 14
15. ListPlus
List ListPlus
app_nil_end, app_ass
@tmiya : Type Class in Coq, 15
16. A R A
(CPS)
Definition cont (R A:Type):Type := (A-R)-R.
R False
Require Import Logic.FunctionalExtensionality.
@tmiya : Type Class in Coq, 16
17. Instance Cont R : Monad (cont R) := {
return’ A a := fun k:A-R = k a;
bind A B m f := fun k:B-R = m (fun a = f a k)
}.
Proof.
(* left_unit *)
intros A B a f. unfold cont in *.
erewrite - eta_expansion. reflexivity.
(* right_unit *)
intros A ma. unfold cont in *.
erewrite eta_expansion. extensionality x.
erewrite - eta_expansion. auto.
(* assoc *)
intros A B C ma f g. reflexivity.
Defined.
@tmiya : Type Class in Coq, 17