4

Coqで型クラスを使ってMaybeモナドを定義したい。 Monadを継承しFunctorます。

Some (f x') = fmap f (Some x')モナド則の一つであるを証明したい。computereflexivityおよびを使用しdestruct option_functorましたが、証明できませんでした。fmap私は適切に単純化することはできません。

Class Functor (F: Type -> Type) := {
   fmap : forall {A B}, (A -> B) -> (F A -> F B)
 ; homo_id : forall {A} (x : F A), x = fmap (fun x' => x') x
 ; homo_comp : forall {A B C} (f : A -> B) (g : B -> C) (x : F A),
     fmap (fun x' => g (f x')) x = fmap g (fmap f x)
}.

Class Monad (M: Type -> Type) := {
   functor :> Functor M
 ; unit : forall {A}, A -> M A
 ; join : forall {A}, M (M A) -> M A
 ; unit_nat : forall {A B} (f : A -> B) (x : A), unit (f x) = fmap f (unit x)
 ; join_nat : forall {A B} (f : A -> B) (x : M (M A)), join (fmap (fmap f) x) = fmap f (join x)
 ; identity : forall {A} (x : M A), join (unit x) = x /\ x = join (fmap unit x)
 ; associativity : forall {A} (x : M (M (M A))), join (join x) = join (fmap join x)
}.

Instance option_functor : Functor option := {
   fmap A B f x :=
     match x with
     | None => None
     | Some x' => Some (f x')
     end
}.
Proof.
  intros. destruct x; reflexivity.
  intros. destruct x; reflexivity.
Qed.

Instance option_monad : Monad option := {
   unit A x := Some x
 ; join A x :=
     match x with
     | Some (Some x') => Some x'
     | _ => None
     end
}.
Proof.
  Admitted.
4

1 に答える 1

5

問題は、 の定義を の代わりに で終了したという事実から発生option_functionQedますDefined

を使用するQedと、何らかの方法で の内部を「隠す」ことができますfmap。その場合、その定義を展開することはできなくなります (たとえばunfoldandsimplタクティクスを使用)。Defined代わりに使用すると、後者Qedの定義を使用するつもりであることを Coq に伝えることができるため、展開可能にする必要があります。fmap

于 2014-03-10T08:25:42.843 に答える