SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Formal Methods Forum

                        Type Class in Coq
                                    @tmiya

                                  April 20,2011




@tmiya : Type Class in Coq,                          1
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
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

Contenu connexe

Plus de tmiya

Coq Tutorial
Coq TutorialCoq Tutorial
Coq Tutorialtmiya
 
RegExp20110305
RegExp20110305RegExp20110305
RegExp20110305tmiya
 
Coq Party 20101127
Coq Party 20101127Coq Party 20101127
Coq Party 20101127tmiya
 
Maude20100719
Maude20100719Maude20100719
Maude20100719tmiya
 
Formal methods20100529
Formal methods20100529Formal methods20100529
Formal methods20100529tmiya
 
Coq 20100208a
Coq 20100208aCoq 20100208a
Coq 20100208atmiya
 

Plus de tmiya (6)

Coq Tutorial
Coq TutorialCoq Tutorial
Coq Tutorial
 
RegExp20110305
RegExp20110305RegExp20110305
RegExp20110305
 
Coq Party 20101127
Coq Party 20101127Coq Party 20101127
Coq Party 20101127
 
Maude20100719
Maude20100719Maude20100719
Maude20100719
 
Formal methods20100529
Formal methods20100529Formal methods20100529
Formal methods20100529
 
Coq 20100208a
Coq 20100208aCoq 20100208a
Coq 20100208a
 

Dernier

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 

Dernier (20)

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 

Typeclass

  • 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