Typeclass
Upcoming SlideShare
Loading in...5
×
 

Typeclass

on

  • 1,810 views

Introduction of type-classes in Coq

Introduction of type-classes in Coq

Statistics

Views

Total Views
1,810
Views on SlideShare
1,807
Embed Views
3

Actions

Likes
2
Downloads
15
Comments
0

1 Embed 3

https://twimg0-a.akamaihd.net 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Typeclass Typeclass Presentation Transcript

    • Formal Methods Forum Type Class in Coq @tmiya April 20,2011@tmiya : Type Class in Coq, 1
    • PolymorphismPolymorphism “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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • ListPlus List ListPlus app_nil_end, app_ass@tmiya : Type Class in Coq, 15
    • 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
    • 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