Skip to content

Commit f6da121

Browse files
author
Gregory Malecha
committed
modifying the definition of equivalence of options.
1 parent c8826ea commit f6da121

File tree

2 files changed

+76
-50
lines changed

2 files changed

+76
-50
lines changed

theories/Data/Monads/OptionMonadLaws.v

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Require Import ExtLib.Structures.Proper.
77
Require Import ExtLib.Structures.MonadLaws.
88
Require Import ExtLib.Data.Option.
99
Require Import ExtLib.Data.Monads.OptionMonad.
10-
Require Import ExtLib.Tactics.TypeTac.
10+
Require Import ExtLib.Tactics.TypeTac.
1111

1212
Set Implicit Arguments.
1313
Set Strict Implicit.
@@ -51,7 +51,7 @@ Section Laws.
5151
Proof. do 5 red; eauto. Qed.
5252
End parametric.
5353

54-
Theorem equal_match : forall (A B : Type) (eA : type A) (eB : type B),
54+
Theorem equal_match : forall (A B : Type) (eA : type A) (eB : type B),
5555
typeOk eA -> typeOk eB ->
5656
forall (x y : option A) (a b : B) (f g : A -> B),
5757
equal x y ->
@@ -67,11 +67,12 @@ Section Laws.
6767
end.
6868
Proof.
6969
destruct x; destruct y; intros; eauto with typeclass_instances; type_tac.
70+
{ inversion H1. assumption. }
7071
{ inversion H1. }
7172
{ inversion H1. }
7273
Qed.
7374

74-
Instance proper_match : forall (A B : Type) (eA : type A) (eB : type B),
75+
Instance proper_match : forall (A B : Type) (eA : type A) (eB : type B),
7576
typeOk eA -> typeOk eB ->
7677
forall (x : option A),
7778
proper x ->
@@ -100,22 +101,23 @@ Section Laws.
100101
simpl; unfold optionT_eq; simpl; intros.
101102
rewrite bind_associativity; eauto with typeclass_instances; intros; type_tac.
102103
destruct x; destruct y; try solve [ inversion H5 ]; type_tac.
104+
inversion H5; assumption.
103105
eapply equal_match; eauto with typeclass_instances; type_tac.
104106
rewrite bind_of_return; eauto with typeclass_instances; type_tac.
105107
eapply equal_match; eauto with typeclass_instances; type_tac.
106108
eapply equal_match; eauto with typeclass_instances; type_tac.
107109
eapply equal_match; eauto with typeclass_instances; type_tac. }
108110
{ simpl; unfold optionT_eq; simpl; intros. red; simpl; intros. type_tac. }
109-
{ simpl; unfold optionT_eq; simpl; intros. red; simpl; intros.
110-
red; simpl; intros. type_tac.
111+
{ simpl; unfold optionT_eq; simpl; intros. red; simpl; intros.
112+
red; simpl; intros. type_tac.
111113
eapply equal_match; eauto with typeclass_instances; type_tac. }
112114
Qed.
113115

114116
(* Theorem equal_match_option : forall T U (tT : type T) (tU : type U),
115117
typeOk tT -> typeOk tU ->
116118
forall (a b : option T) (f g : T -> U) (x y : U),
117119
equal a b -> equal f g -> equal x y ->
118-
equal match a with
120+
equal match a with
119121
| Some a => f a
120122
| None => x
121123
end
@@ -136,11 +138,11 @@ Section Laws.
136138
{ simpl; unfold lift, optionT_eq; simpl; intros.
137139
unfold liftM.
138140
rewrite bind_associativity; eauto with typeclass_instances; type_tac.
139-
rewrite bind_associativity; eauto with typeclass_instances; type_tac.
141+
rewrite bind_associativity; eauto with typeclass_instances; type_tac.
140142
rewrite bind_of_return; eauto with typeclass_instances; type_tac.
141143
eapply equal_match; eauto with typeclass_instances; type_tac.
142144
eapply equal_match; eauto with typeclass_instances; type_tac. }
143-
{ unfold lift, liftM; simpl; intros. unfold liftM. red; simpl; intros.
145+
{ unfold lift, liftM; simpl; intros. unfold liftM. red; simpl; intros.
144146
unfold optionT_eq; simpl. type_tac. }
145147
Qed.
146148

@@ -162,22 +164,23 @@ Section Laws.
162164
type_tac.
163165
apply proper_fun; intros. repeat rewrite local_ret; eauto with typeclass_instances.
164166
type_tac. eauto with typeclass_instances.
165-
type_tac. type_tac. }
167+
type_tac. type_tac. }
166168
{ simpl. unfold optionT_eq; simpl; intros; unfold liftM.
167169
rewrite local_bind; eauto with typeclass_instances.
168170
type_tac.
169171
destruct x; destruct y; try solve [ inversion H4 ]; type_tac.
172+
inversion H4; assumption.
170173
rewrite local_ret; eauto with typeclass_instances; type_tac.
171174
type_tac. eapply equal_match; eauto with typeclass_instances; type_tac. }
172175
{ simpl. unfold optionT_eq; simpl; intros; unfold liftM.
173176
rewrite local_ret; eauto with typeclass_instances; type_tac. }
174177
{ simpl. unfold optionT_eq; simpl; intros; unfold liftM.
175178
rewrite local_local; eauto with typeclass_instances; type_tac. }
176-
{ unfold local; simpl; intros. red. red. intros. red in H0.
179+
{ unfold local; simpl; intros. red. red. intros. red in H0.
177180
red; simpl. type_tac. }
178-
{ Opaque lift. unfold ask; simpl; intros. red. type_tac.
181+
{ Opaque lift. unfold ask; simpl; intros. red. type_tac.
179182
eapply lift_proper; eauto with typeclass_instances. Transparent lift. }
180-
Qed.
183+
Qed.
181184

182185
(*
183186
Global Instance MonadStateLaws_optionT (s : Type) (t : type s) (tT : typeOk t) (Ms : MonadState s m) (MLs : MonadStateLaws Monad_m _ _ Ms) : MonadStateLaws _ _ _ (@State_optionT _ _ _ Ms).
@@ -195,29 +198,29 @@ Section Laws.
195198
instantiate (1 := fun x => ret (Some x)). simpl. type_tac.
196199
type_tac. type_tac. }
197200
{ type_tac. rewrite bind_of_return; eauto with typeclass_instances.
198-
type_tac. type_tac.
201+
type_tac. type_tac.
199202
eapply equal_match_option; eauto with typeclass_instances; type_tac. }
200203
{ eapply equal_match_option; eauto with typeclass_instances; type_tac. } }
201204
{ simpl; unfold optionT_eq; simpl; intros; unfold liftM; simpl.
202-
repeat rewrite bind_associativity; eauto with typeclass_instances;
205+
repeat rewrite bind_associativity; eauto with typeclass_instances;
203206
try solve [ type_tac; eapply equal_match_option; eauto with typeclass_instances; type_tac ].
204207
rewrite bind_proper; eauto with typeclass_instances.
205208
2: eapply preflexive; eauto with typeclass_instances; type_tac.
206209
instantiate (1 := fun a : unit => bind get (fun x0 : s => ret (Some x0))).
207210
{ rewrite <- bind_associativity; eauto with typeclass_instances.
208211
Require Import MonadTac.
209-
{
212+
{
210213
Ltac cl := eauto with typeclass_instances.
211214
Ltac tcl := solve [ cl ].
212215
Ltac monad_rewrite t :=
213216
first [ t
214-
| rewrite bind_rw_0; [ | tcl | tcl | tcl | t | type_tac ]
217+
| rewrite bind_rw_0; [ | tcl | tcl | tcl | t | type_tac ]
215218
| rewrite bind_rw_1 ].
216219
monad_rewrite ltac:(eapply put_get; eauto with typeclass_instances).
217220
rewrite bind_associativity; cl; try solve_proper.
218221
rewrite bind_rw_1; [ | tcl | tcl | tcl | intros | type_tac ].
219222
Focus 2.
220-
etransitivity. eapply bind_of_return; cl; type_tac.
223+
etransitivity. eapply bind_of_return; cl; type_tac.
221224
instantiate (1 := fun _ => ret (Some x)). simpl. type_tac.
222225
Add Parametric Morphism (T : Type) (tT : type T) (tokT : typeOk tT) : (@equal _ tT)
223226
with signature (equal ==> equal ==> iff)
@@ -248,7 +251,7 @@ assert (Morphisms.Proper (equal ==> Basics.flip Basics.impl)
248251
assert (Morphisms.Proper
249252
(Morphisms.pointwise_relation unit equal ==> equal)
250253
(bind (@put _ _ Ms x))).
251-
{ red. red. intros. eapply bind_proper; cl. solve_proper.
254+
{ red. red. intros. eapply bind_proper; cl. solve_proper.
252255
red; simpl. red in H1. red.
253256
254257
assert bind_proper.
@@ -260,7 +263,7 @@ setoid_rewrite bind_of_return.
260263
261264
rewrite bind_rw_0
262265
3: instantiate (1 := (bind (put x) (fun _ : unit => get))).
263-
266+
264267
265268
266269
@@ -271,15 +274,15 @@ setoid_rewrite bind_of_return.
271274
proper y ->
272275
equal (bind x y) (bind z y).
273276
Proof.
274-
277+
275278
276279
}
277280
{ type_tac. rewrite bind_of_return; eauto with typeclass_instances; type_tac.
278281
eapply equal_match_option; eauto with typeclass_instances; type_tac. } }
279282
280-
283+
281284
Print MonadStateLaws.
282-
*)
285+
*)
283286

284287
Global Instance MonadZeroLaws_optionT : MonadZeroLaws (@Monad_optionT _ Monad_m) type_optionT _.
285288
Proof.

theories/Data/Option.v

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Require Import Coq.Relations.Relation_Definitions.
2+
Require Import Coq.Classes.RelationClasses.
13
Require Import ExtLib.Core.Type.
24
Require Import ExtLib.Core.RelDec.
35
Require Import ExtLib.Structures.Reducible.
@@ -43,19 +45,36 @@ Global Instance Functor_option : Functor option :=
4345
| Some x => Some (f x)
4446
end |}.
4547

48+
Section relation.
49+
Context {T : Type}.
50+
Variable (R : relation T).
51+
52+
Inductive Roption : Relation_Definitions.relation (option T) :=
53+
| Roption_None : Roption None None
54+
| Roption_Some : forall x y, R x y -> Roption (Some x) (Some y).
55+
56+
Lemma Reflexive_Roption : Reflexive R -> Reflexive Roption.
57+
Proof. clear. compute. destruct x; try constructor; auto. Qed.
58+
59+
Lemma Symmetric_Roption : Symmetric R -> Symmetric Roption.
60+
Proof. clear. compute. intros.
61+
destruct H0; constructor. auto.
62+
Qed.
63+
64+
Lemma Transitive_Roption : Transitive R -> Transitive Roption.
65+
Proof.
66+
clear. compute. intros.
67+
destruct H0; auto.
68+
inversion H1. constructor; auto. subst. eapply H; eassumption.
69+
Qed.
70+
End relation.
71+
4672
Section type.
4773
Variable T : Type.
4874
Variable tT : type T.
4975

50-
Definition eqv_option rT (a b : option T) :=
51-
match a , b with
52-
| None , None => True
53-
| Some a , Some b => rT a b
54-
| _ , _ => False
55-
end.
56-
5776
Global Instance type_option : type (option T) :=
58-
{ equal := eqv_option equal
77+
{ equal := Roption equal
5978
; proper := fun x => match x with
6079
| None => True
6180
| Some y => proper y
@@ -66,38 +85,42 @@ Section type.
6685
Global Instance typeOk_option : typeOk type_option.
6786
Proof.
6887
constructor.
69-
{ destruct x; destruct y; simpl; auto; try contradiction; intros.
70-
unfold proper in *. simpl in *.
71-
destruct tTOk.
72-
eapply only_proper in H. intuition. }
73-
{ red. destruct x; simpl; auto. intro. eapply preflexive; [ | eapply H ]. eapply equiv_prefl; auto. }
74-
{ red. destruct x; destruct y; simpl; auto. intros.
75-
destruct tTOk. apply equiv_sym. auto. }
76-
{ red. destruct x; destruct y; destruct z; intros; try contradiction; auto.
77-
simpl in *. destruct tTOk.
78-
etransitivity; eauto. }
88+
{ inversion 1.
89+
split; constructor.
90+
split; simpl; eapply only_proper; eauto. symmetry; eauto. }
91+
{ red. destruct x; simpl. constructor.
92+
eapply preflexive; [ | eapply H ].
93+
eapply equiv_prefl; auto. constructor. }
94+
{ red. destruct 1. constructor. constructor. symmetry. assumption. }
95+
{ red. destruct 1; inversion 1; subst. assumption.
96+
constructor. etransitivity; eauto. }
7997
Qed.
8098

8199
Global Instance proper_Some : proper (@Some T).
82-
Proof. compute; auto. Qed.
100+
Proof. constructor; assumption. Qed.
83101

84102
Global Instance proper_None : proper (@None T).
85-
Proof. compute; auto. Qed.
103+
Proof. constructor. Qed.
86104

87105
End type.
88106

89107
Global Instance FunctorLaws_option : FunctorLaws Functor_option type_option.
90108
Proof.
91109
constructor.
92110
{ simpl. red. destruct x; destruct y; simpl; auto.
93-
red in H0. intros. etransitivity. eapply H0. 2: eauto.
94-
eapply proper_left; eauto. }
95-
{ red; simpl. red; simpl. red; simpl; intros.
96-
destruct x; destruct y; simpl in *; auto.
97-
unfold compose. eauto using equal_app. }
98-
{ red; simpl. red; simpl. red; simpl; intros.
99-
destruct x0; destruct y0; eauto.
100-
red in H2. simpl. eauto using equal_app. }
111+
inversion 1; simpl. red in H0.
112+
unfold id. constructor. etransitivity. eapply H0. 2: eauto.
113+
eapply proper_left; eauto.
114+
inversion 1. }
115+
{ intros. simpl in *.
116+
red. intros.
117+
destruct H4; simpl.
118+
- unfold compose. constructor.
119+
- unfold compose. constructor.
120+
eapply H3. eapply H2. assumption. }
121+
{ intros; simpl in *.
122+
red. red. inversion 2. constructor.
123+
constructor. apply H1. assumption. }
101124
Qed.
102125

103126
Global Instance Injective_Some (T : Type) (a b : T) : Injective (Some a = Some b) :=

0 commit comments

Comments
 (0)