summaryrefslogtreecommitdiff
path: root/Tutorial.v
blob: 84d40fcc4414001d448ed16152449e9b7a03d15c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
(***************************************************************************)
(*  This is part of aac_tactics, it is distributed under the terms of the  *)
(*         GNU Lesser General Public License version 3                     *)
(*              (see file LICENSE for more details)                        *)
(*                                                                         *)
(*       Copyright 2009-2010: Thomas Braibant, Damien Pous.                *)
(***************************************************************************)

(** * Examples about uses of the aac_rewrite library *)

(*
   Depending on your installation, either uncomment the following two
   lines, or add them to your .coqrc files, replacing path_to_aac_tactics
   with the correct path for your installation:

   Add Rec LoadPath "path_to_aac_tactics".
   Add ML Path "path_to_aac_tactics".
*)
Require Export  AAC.
Require Instances.

(** ** Introductory examples  *)

(** First, we settle in the context of Z, and show an usage of our
tactics: we rewrite an universally quantified hypothesis modulo
associativity and commutativity. *)

Section introduction.
  Import ZArith.
  Import Instances.Z.
  
  Variables a b c : Z.
  Hypothesis H: forall x, x + Zopp x = 0.
  Goal a + b + c + Zopp (c + a) = b.
    aac_rewrite H.
    aac_reflexivity.
  Qed.
  Goal a + c+ Zopp (b + a + Zopp b) = c.
    do 2 aac_rewrite H.
    reflexivity.
  Qed.

  (** Notes: 
     - the tactic handles arbitrary function symbols like [Zopp] (as
       long as they are proper morphisms);
     - here, ring would have done the job.
     *)

End introduction.  

(** Second, we show how to exploit binomial identities to prove a goal
about pythagorean triples, without breaking a sweat. By comparison,
even if the goal and the hypothesis are both in normal form, making
the rewrites using standard tools is difficult.
*)

Section binomials.

  Import ZArith.
  Import Instances.Z.
 
  Notation "x ^2" := (x*x) (at level 40).
  Notation "2 ⋅ x" := (x+x) (at level 41).
  Lemma Hbin1: forall x y, (x+y)^2   = x^2 + y^2 +  2⋅x*y. Proof. intros; ring. Qed.
  Lemma Hbin2: forall x y, x^2 + y^2 = (x+y)^2   + -(2⋅x*y). Proof. intros; ring.  Qed.
  Lemma Hopp : forall x, x + -x = 0. Proof Zplus_opp_r.  
  
  Variables a b c : Z.
  Hypothesis H : c^2 + 2⋅(a+1)*b  = (a+1+b)^2.
  Goal a^2 + b^2 + 2⋅a + 1 = c^2.
    aacu_rewrite <- Hbin1.
    aac_rewrite Hbin2.
    aac_rewrite <- H.
    aac_rewrite Hopp.
    aac_reflexivity.
  Qed.

  (** Note: after the [aac_rewrite <- H], one could use [ring] to close the proof.*)

End binomials.

(** ** Usage *)

(** One can also work in an abstract context, with arbitrary
   associative and commutative operators.

   (Note that one can declare several operations of each kind;
   however, to be able to use this plugin, one currently needs at least
   one associative operator, and one associative-commutative
   operator.) *)

Section base.
  
  Context {X} {R} {E: Equivalence R} 
  {plus} {zero}
  {dot} {one}
  {A:  @Op_A X R dot one}        
  {AC: Op_AC R plus zero}.      
 
  Notation "x == y"  := (R x y) (at level 70).
  Notation "x * y"   := (dot x y) (at level 40, left associativity).
  Notation "1"       := (one).
  Notation "x + y"   := (plus x y) (at level 50, left associativity).
  Notation "0"       := (zero).

  (** In the very first example, [ring] would have solved the
  goal. Here, since [dot] does not necessarily distribute over [plus],
  it is not possible to rely on it. *)

  Section reminder.
    Hypothesis H : forall x, x * x == x.
    Variables a b c : X.
    
    Goal (a+b+c)*(c+a+b) == a+b+c.
      aac_rewrite H.
      aac_reflexivity.
    Qed.

    (** Note: the tactic starts by normalizing terms, so that trailing
    units are always eliminated. *)

    Goal ((a+b)+0+c)*((c+a)+b*1) == a+b+c.
      aac_rewrite H.
      aac_reflexivity.
    Qed.
  End reminder.
  
  (** We can deal with "proper" morphisms of arbitrary arity (here [f],
     or [Zopp] earlier), and rewrite under morphisms (here [g]). *)

  Section morphisms.
    Variable f : X -> X -> X.
    Hypothesis Hf : Proper (R ==> R ==> R) f.
    Variable g : X -> X.
    Hypothesis Hg : Proper (R ==> R) g.
    
    Variable a b: X.
    Hypothesis H : forall x y, x+f (b+y) x == y+x.
    Goal g ((f (a+b) a) + a) == g (a+a).
      aac_rewrite H.
      reflexivity.
    Qed.
  End morphisms.

  (** ** Selecting what and where to rewrite  *)

  (** There are sometimes several possible rewriting. We now show how
  to interact with the tactic to select the desired one. *)

  Section occurrence.
    Variable f : X -> X  .
    Variable a : X.
    Hypothesis Hf : Proper (R ==> R) f.
    Hypothesis H : forall x, x + x == x. 

    Goal f(a+a)+f(a+a) == f a.
      (** In case there are several possible solutions, one can print
         the different solutions using the [aac_instances] tactic (in
         proofgeneral, look at buffer *coq* ): *)
      aac_instances H.
      (** the default choice is the smallest possible context (number
      0), but one can choose the desired context; *)
      aac_rewrite H subterm 1.            
      (** now the goal is [f a + f a  == f a], there is only one solution. *)      
      aac_rewrite H.
      reflexivity.
    Qed.
  
  End occurrence.

  Section subst.
    Variables a b c d : X.
    Hypothesis H: forall x y, a*x*y*b == a*(x+y)*b.
    Hypothesis H': forall x, x + x == x.
    
    Goal a*c*d*c*d*b  == a*c*d*b.
    (** Here, there is only one possible context, but several substitutions; *)
      aac_instances H.
      (** we can select them with the proper keyword.  *)
      aac_rewrite H subst 1. 
      aac_rewrite H'.
      aac_reflexivity.
    Qed.
  End subst.
  
  (** As expected, one can use both keyword together to select the
     correct subterm and the correct substitution. *)

  Section both.
    Variables a b c d : X.
    Hypothesis H: forall x y, a*x*y*b == a*(x+y)*b.
    Hypothesis H': forall x, x + x == x.
    
    Goal a*c*d*c*d*b*b == a*(c*d+b)*b.
      aac_instances H.
      aac_rewrite H subterm 1 subst 1.
      aac_rewrite H.
      aac_rewrite H'.
      aac_reflexivity.
    Qed.
      
  End both.

  (** We now turn on explaining the distinction between [aac_rewrite]
  and [aacu_rewrite]: [aac_rewrite] rejects solutions in which
  variables are instantiated by units, the companion tactic,
  [aacu_rewrite] allows such solutions.  *)

  Section dealing_with_units.
    Variables a b c: X.
    Hypothesis H: forall x, a*x*a == x.
    Goal a*a == 1.
      (** Here, x must be instantiated with 1, hence no solutions; *)
      aac_instances H.            
      (** while we get solutions with the "aacu" tactic. *)      
      aacu_instances H.
      aacu_rewrite H.
      reflexivity.
    Qed.
      
    (** We introduced this distinction because it allows us to rule
    out dummy cases in common situations: *)

    Hypothesis H': forall x y z,  x*y + x*z == x*(y+z).
    Goal a*b*c + a*c+ a*b == a*(c+b*(1+c)).
    (** 6 solutions without units,  *)
      aac_instances H'.
    (** more than 52 with units. *)
      aacu_instances H'.
    Abort.    
    
  End dealing_with_units.  
End base.

(** ** Declaring instances *)

(** One can use one's own operations: it suffices to declare them as
   instances of our classes. (Note that these instances are already
   declared in file [Instances.v].) *)

Section Peano.
  Require Import Arith.
     
  Program  Instance nat_plus : Op_AC eq plus O.
  Solve All Obligations using firstorder.

  Program  Instance nat_dot : Op_AC eq mult 1.
  Solve All Obligations using firstorder.
  

  (** Caveat: we need at least an instance of an operator that is AC
   and one that is A for a given relation. However, one can reuse an
   AC operator as an A operator. *)

  Definition default_a := AC_A nat_dot. Existing Instance default_a.
End Peano.

(** ** Caveats  *)

Section Peano'.
  Import Instances.Peano.

  (** 1. We have a special treatment for units, thus, [S x + x] does not
    match [S 0], which is considered as a unit (one) of the mult
    operation. *)

  Section caveat_one.
    Definition double_plus_one x := 2*x + 1. 
    Hypothesis H : forall x, S x + x = double_plus_one x.
    Goal S O = double_plus_one O.
      try aac_rewrite H.         (** 0 solutions (normal since it would use 0 to instantiate x) *)
      try aacu_rewrite H.        (** 0 solutions (abnormal) *)
    Abort.
  End caveat_one.

  (** 2. We cannot at the moment have two classes with the same
  units: in the following example, [0] is understood as the unit of
  [max] rather than as the unit of [plus]. *)

  Section max.
    Program Instance aac_max  : Op_AC eq Max.max O  := Build_Op_AC _ _ _ Max.max_assoc Max.max_comm.
    Variable a : nat.
    Goal 0 +  a = a.
      try aac_reflexivity.
    Abort.
  End max.
End Peano'.

(** 3. If some computations are possible in the goal or in the
hypothesis, the inference mechanism we use will make the
conversion. However, it seems that in most cases, these conversions
can be done by hand using simpl rather than rewrite. *)

Section Z.

  (* The order of the following lines is important in order to get the right scope afterwards. *)
  Import ZArith.
  Open Scope Z_scope.
  Opaque Zmult.
  
  Hypothesis dot_ann_left :forall x, x * 0 = 0. 
  Hypothesis dot_ann_right :forall x, 0 * x = 0. 

  Goal forall a, a*0 = 0.
    intros. aacu_rewrite dot_ann_left. reflexivity.
  Qed.

  (** Here the tactic fails, since the 0*a is converted to 0, and no
  rewrite can occur (even though Zmult is opaque). *)
  Goal forall a,  0*a = 0.
    intros. try aacu_rewrite dot_ann_left.
  Abort. 

  (**  Here the tactic fails, since the 0*x is converted to 0 in the hypothesis. *)
  Goal forall a,  a*0 = 0.
    intros. try aacu_rewrite dot_ann_right.
  Abort. 
  
 
End Z.