| 1 |
Set Implicit Arguments. |
| 2 |
|
| 3 |
|
| 4 |
Require Export Relation_Definitions. |
| 5 |
Require Export Setoid. |
| 6 |
Require Import Morphisms. |
| 7 |
|
| 8 |
|
| 9 |
Section Essais. |
| 10 |
|
| 11 |
(* Parametrized Setoid *) |
| 12 |
Parameter K : Type -> Type. |
| 13 |
Parameter equiv : forall A : Type, K A -> K A -> Prop. |
| 14 |
Parameter equiv_refl : forall (A : Type) (x : K A), equiv x x. |
| 15 |
Parameter equiv_sym : forall (A : Type) (x y : K A), equiv x y -> equiv y x. |
| 16 |
Parameter equiv_trans : forall (A : Type) (x y z : K A), equiv x y -> equiv y z |
| 17 |
-> equiv x z. |
| 18 |
|
| 19 |
(* basic operations *) |
| 20 |
Parameter val : forall A : Type, A -> K A. |
| 21 |
Parameter bind : forall A B : Type, K A -> (A -> K B) -> K B. |
| 22 |
|
| 23 |
Parameter |
| 24 |
bind_compat : |
| 25 |
forall (A B : Type) (m1 m2 : K A) (f1 f2 : A -> K B), |
| 26 |
equiv m1 m2 -> |
| 27 |
(forall x : A, equiv (f1 x) (f2 x)) -> equiv (bind m1 f1) (bind m2 f2). |
| 28 |
|
| 29 |
(* monad axioms *) |
| 30 |
Parameter |
| 31 |
bind_val_l : |
| 32 |
forall (A B : Type) (a : A) (f : A -> K B), equiv (bind (val a) f) (f a). |
| 33 |
Parameter |
| 34 |
bind_val_r : |
| 35 |
forall (A : Type) (m : K A), equiv (bind m (fun a => val a)) m. |
| 36 |
Parameter |
| 37 |
bind_assoc : |
| 38 |
forall (A B C : Type) (m : K A) (f : A -> K B) (g : B -> K C), |
| 39 |
equiv (bind (bind m f) g) (bind m (fun a => bind (f a) g)). |
| 40 |
|
| 41 |
|
| 42 |
Hint Resolve equiv_refl equiv_sym equiv_trans: monad. |
| 43 |
|
| 44 |
Add Parametric Relation A : (K A) (@equiv A) |
| 45 |
reflexivity proved by (@equiv_refl A) |
| 46 |
symmetry proved by (@equiv_sym A) |
| 47 |
transitivity proved by (@equiv_trans A) |
| 48 |
as equiv_rel. |
| 49 |
|
| 50 |
Add Parametric Morphism A B : (@bind A B) |
| 51 |
with signature (@equiv A) ==> (pointwise_relation A (@equiv B)) ==> (@equiv B) |
| 52 |
as bind_mor. |
| 53 |
Proof. |
| 54 |
unfold pointwise_relation; intros; apply bind_compat; auto. |
| 55 |
Qed. |
| 56 |
|
| 57 |
Lemma test: |
| 58 |
forall (A B: Type) (m1 m2 m3: K A) (f: A -> A -> K B), |
| 59 |
(equiv m1 m2) -> (equiv m2 m3) -> |
| 60 |
equiv (bind m1 (fun a => bind m2 (fun a' => f a a'))) |
| 61 |
(bind m2 (fun a => bind m3 (fun a' => f a a'))). |
| 62 |
Proof. |
| 63 |
intros A B m1 m2 m3 f H1 H2. |
| 64 |
setoid_rewrite H1. (* this works *) |
| 65 |
setoid_rewrite H2. |
| 66 |
reflexivity. |
| 67 |
Qed. |
| 68 |
|
| 69 |
End Essais. |