rewrite_facilities.v 3.18 KB
Newer Older
Sergey Bozhko's avatar
Sergey Bozhko committed
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
From mathcomp Require Import ssreflect ssrbool eqtype ssrnat seq path fintype bigop.

(** * Rewriting *)
(** In this file we prove a few lemmas 
   that simplify work with rewriting. *)
Section RewriteFacilities.
  
  Lemma diseq:
    forall {X : Type} (p : X -> Prop) (x y : X),
      ~ p x -> p y -> x <> y.
  Proof. intros ? ? ? ? NP P EQ; subst; auto. Qed.

  
  Lemma eqprop_to_eqbool {X : eqType} {a b : X}: a = b -> a == b.
  Proof. by intros; apply/eqP. Qed.

  Lemma eqbool_true {X : eqType} {a b : X}: a == b -> a == b = true.
  Proof. by move =>/eqP EQ; subst b; rewrite eq_refl. Qed.

  Lemma eqbool_false {X : eqType} {a b : X}: a != b -> a == b = false.
  Proof. by apply negbTE. Qed.

  
  Lemma eqbool_to_eqprop {X : eqType} {a b : X}: a == b -> a = b.
  Proof. by intros; apply/eqP. Qed.

  Lemma neqprop_to_neqbool {X : eqType} {a b : X}: a <> b -> a != b.
  Proof. by intros; apply/eqP. Qed.

  Lemma neqbool_to_neqprop {X : eqType} {a b : X}: a != b -> a <> b.
  Proof. by intros; apply/eqP. Qed.

  Lemma neq_sym {X : eqType} {a b : X}:
    a != b -> b != a.
  Proof.
    intros NEQ; apply/eqP; intros EQ;
      subst b; move: NEQ => /eqP NEQ; auto. Qed.

  Lemma neq_antirefl {X : eqType} {a : X}:
    (a != a) = false.
  Proof. by apply/eqP. Qed.
  
  
  Lemma option_inj_eq {X : eqType} {a b : X}:
    a == b -> Some a == Some b.
  Proof. by move => /eqP EQ; apply/eqP; rewrite EQ. Qed.

  Lemma option_inj_neq {X : eqType} {a b : X}:
    a != b -> Some a != Some b.
  Proof.
    by move => /eqP NEQ;
     apply/eqP; intros CONTR;
       apply: NEQ; inversion_clear CONTR. Qed.

  (** Example *)
  (* As a motivation for this file, we consider the following example. *)
  Section Example.

    (* Let X  be an arbitrary type ... *)
    Context {X : eqType}.

    (* ... f be an arbitrary function [bool -> bool] ... *)
    Variable f : bool -> bool.

    (* ... p be an arbitrary predicate on X ... *)
    Variable p : X -> Prop.

    (* ... and let a and b be two elements of X such that ... *)
    Variables a b : X.
    
    (* ... p holds for a and doesn't hold for b. *)
    Hypothesis H_pa : p a.
    Hypothesis H_npb : ~ p b.

    (* The following examples are commented out
       to expose the insides of the proofs. *)
    
    (*
    (* Simplifying some relatively sophisticated 
       expressions can be quite tedious. *)
81 82 83
    [Goal f ((a == b) && f false) = f false.]
    [Proof.]
      (* Things like [simpl/compute] make no sense here. *)
Sergey Bozhko's avatar
Sergey Bozhko committed
84
      (* One can use [replace] to generate a new goal. *)
85
      [replace (a == b) with false; last first.]
Sergey Bozhko's avatar
Sergey Bozhko committed
86 87
      (* However, this leads to a "loss of focus". Moreover, 
         the resulting goal is not so trivial to prove. *)
88 89 90 91
      [{ apply/eqP; rewrite eq_sym eqbF_neg.]
      [    by apply/eqP; intros EQ; subst b; apply H_npb. }]
      [  by rewrite Bool.andb_false_l.]
    [Abort.]
Sergey Bozhko's avatar
Sergey Bozhko committed
92 93 94 95
     *)
    
    (*
    (* The second attempt. *)
96
    [Goal f ((a == b) && f false) = f false.]
Sergey Bozhko's avatar
Sergey Bozhko committed
97 98
      (* With the lemmas above one can compose multiple 
         transformations in a single rewrite. *)
99 100 101
      [  by rewrite (eqbool_false (neq_sym (neqprop_to_neqbool (diseq _ _ _ H_npb H_pa))))]
      [        Bool.andb_false_l.]
    [Qed.]
Sergey Bozhko's avatar
Sergey Bozhko committed
102 103 104 105 106
    *)
    
  End Example.
  
End RewriteFacilities.