我想在Coq中使用类型类定义Maybe monad。
Monad
继承Functor
。
我想证明Some (f x') = fmap f (Some x')
,这是monad法律之一。
我使用了compute
,reflexivity
和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.
答案 0 :(得分:5)
您的问题源于您使用option_function
而不是Qed
结束Defined
的定义。
使用Qed
,您以某种方式'隐藏'fmap
的内部。然后,您无法再展开其定义(例如,使用unfold
和simpl
策略)。使用Defined
而不是Qed
让您告诉Coq您打算使用fmap
后者的定义,因此它应该是可展开的。