gpt4 book ai didi

monads - 在 Coq 中定义 Maybe monad

转载 作者:行者123 更新时间:2023-12-05 01:04:54 24 4
gpt4 key购买 nike

我想在 Coq 中使用类型类来定义 Maybe monad。Monad继承 Functor .

我要证明Some (f x') = fmap f (Some x') ,这是单子(monad)定律之一。
我用了compute , reflexivitydestruct 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.

最佳答案

您的问题源于您结束了 option_function 的定义。与 Qed而不是 Defined .

使用 Qed ,您以某种方式“隐藏”了 fmap 的内部结构.然后你不能再展开它的定义(例如使用 unfoldsimpl 策略)。使用 Defined而不是 Qed让你告诉 Coq 你打算使用 fmap 的定义后者,所以它应该是可展开的。

关于monads - 在 Coq 中定义 Maybe monad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com