src/HOL/ex/Rewrite_Examples.thy
author noschinl
Wed Apr 15 15:10:01 2015 +0200 (2015-04-15)
changeset 60079 ef4fe30e9ef1
parent 60054 ef4878146485
child 60088 0a064330a885
permissions -rw-r--r--
rewrite: add ML interface
noschinl@59739
     1
theory Rewrite_Examples
noschinl@59739
     2
imports Main "~~/src/HOL/Library/Rewrite"
noschinl@59739
     3
begin
noschinl@59739
     4
noschinl@59739
     5
section \<open>The rewrite Proof Method by Example\<close>
noschinl@59739
     6
noschinl@59739
     7
(* This file is intended to give an overview over
noschinl@59739
     8
   the features of the pattern-based rewrite proof method.
noschinl@59739
     9
noschinl@59739
    10
   See also https://www21.in.tum.de/~noschinl/Pattern-2014/
noschinl@59739
    11
*)
noschinl@59739
    12
noschinl@59739
    13
lemma
noschinl@59739
    14
  fixes a::int and b::int and c::int
noschinl@59739
    15
  assumes "P (b + a)"
noschinl@59739
    16
  shows "P (a + b)"
noschinl@59739
    17
by (rewrite at "a + b" add.commute)
noschinl@59739
    18
   (rule assms)
noschinl@59739
    19
noschinl@59739
    20
(* Selecting a specific subterm in a large, ambiguous term. *)
noschinl@59739
    21
lemma
noschinl@59739
    22
  fixes a b c :: int
noschinl@59739
    23
  assumes "f (a - a + (a - a)) + f (   0    + c) = f 0 + f c"
noschinl@59739
    24
  shows   "f (a - a + (a - a)) + f ((a - a) + c) = f 0 + f c"
noschinl@60047
    25
  by (rewrite in "f _ + f \<hole> = _" diff_self) fact
noschinl@59739
    26
noschinl@59739
    27
lemma
noschinl@59739
    28
  fixes a b c :: int
noschinl@59739
    29
  assumes "f (a - a +    0   ) + f ((a - a) + c) = f 0 + f c"
noschinl@59739
    30
  shows   "f (a - a + (a - a)) + f ((a - a) + c) = f 0 + f c"
noschinl@60047
    31
  by (rewrite at "f (_ + \<hole>) + f _ = _" diff_self) fact
noschinl@59739
    32
noschinl@59739
    33
lemma
noschinl@59739
    34
  fixes a b c :: int
noschinl@59739
    35
  assumes "f (  0   + (a - a)) + f ((a - a) + c) = f 0 + f c"
noschinl@59739
    36
  shows   "f (a - a + (a - a)) + f ((a - a) + c) = f 0 + f c"
noschinl@60047
    37
  by (rewrite in "f (\<hole> + _) + _ = _" diff_self) fact
noschinl@59739
    38
noschinl@59739
    39
lemma
noschinl@59739
    40
  fixes a b c :: int
noschinl@59739
    41
  assumes "f (a - a +    0   ) + f ((a - a) + c) = f 0 + f c"
noschinl@59739
    42
  shows   "f (a - a + (a - a)) + f ((a - a) + c) = f 0 + f c"
noschinl@60047
    43
  by (rewrite in "f (_ + \<hole>) + _ = _" diff_self) fact
noschinl@59739
    44
noschinl@59739
    45
lemma
noschinl@59739
    46
  fixes x y :: nat
noschinl@59739
    47
  shows"x + y > c \<Longrightarrow> y + x > c"
noschinl@60047
    48
  by (rewrite at "\<hole> > c" add.commute) assumption
noschinl@59739
    49
noschinl@59739
    50
(* We can also rewrite in the assumptions.  *)
noschinl@59739
    51
lemma
noschinl@59739
    52
  fixes x y :: nat
noschinl@59739
    53
  assumes "y + x > c \<Longrightarrow> y + x > c"
noschinl@59739
    54
  shows   "x + y > c \<Longrightarrow> y + x > c"
noschinl@59739
    55
  by (rewrite in asm add.commute) fact
noschinl@59739
    56
noschinl@59739
    57
lemma
noschinl@59739
    58
  fixes x y :: nat
noschinl@59739
    59
  assumes "y + x > c \<Longrightarrow> y + x > c"
noschinl@59739
    60
  shows   "x + y > c \<Longrightarrow> y + x > c"
noschinl@59739
    61
  by (rewrite in "x + y > c" at asm add.commute) fact
noschinl@59739
    62
noschinl@59739
    63
lemma
noschinl@59739
    64
  fixes x y :: nat
noschinl@59739
    65
  assumes "y + x > c \<Longrightarrow> y + x > c"
noschinl@59739
    66
  shows   "x + y > c \<Longrightarrow> y + x > c"
noschinl@60047
    67
  by (rewrite at "\<hole> > c" at asm  add.commute) fact
noschinl@59739
    68
noschinl@59739
    69
lemma
noschinl@59739
    70
  assumes "P {x::int. y + 1 = 1 + x}"
noschinl@59739
    71
  shows   "P {x::int. y + 1 = x + 1}"
noschinl@60047
    72
  by (rewrite at "x+1" in "{x::int. \<hole> }" add.commute) fact
noschinl@59739
    73
noschinl@59739
    74
lemma
noschinl@59739
    75
  assumes "P {x::int. y + 1 = 1 + x}"
noschinl@59739
    76
  shows   "P {x::int. y + 1 = x + 1}"
noschinl@60047
    77
  by (rewrite at "any_identifier_will_work+1" in "{any_identifier_will_work::int. \<hole> }" add.commute)
noschinl@59739
    78
   fact
noschinl@59739
    79
noschinl@59739
    80
lemma
noschinl@59739
    81
  assumes "P {(x::nat, y::nat, z). x + z * 3 = Q (\<lambda>s t. s * t + y - 3)}"
noschinl@59739
    82
  shows   "P {(x::nat, y::nat, z). x + z * 3 = Q (\<lambda>s t. y + s * t - 3)}"
noschinl@60047
    83
  by (rewrite at "b + d * e" in "\<lambda>(a, b, c). _ = Q (\<lambda>d e. \<hole>)" add.commute) fact
noschinl@59739
    84
noschinl@60052
    85
(* This is not limited to the first assumption *)
noschinl@60052
    86
lemma
noschinl@60052
    87
  assumes "PROP P \<equiv> PROP Q"
noschinl@60052
    88
  shows "PROP R \<Longrightarrow> PROP P \<Longrightarrow> PROP Q"
noschinl@60052
    89
    by (rewrite at asm assms)
noschinl@60052
    90
noschinl@60053
    91
(* Rewriting "at asm" selects each full assumption, not any parts *)
noschinl@60053
    92
lemma
noschinl@60053
    93
  assumes "(PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP S \<Longrightarrow> PROP R)"
noschinl@60053
    94
  shows "PROP S \<Longrightarrow> (PROP P \<Longrightarrow> PROP Q) \<Longrightarrow> PROP R"
noschinl@60053
    95
  apply (rewrite at asm assms)
noschinl@60053
    96
  apply assumption
noschinl@60053
    97
  done
noschinl@60053
    98
noschinl@60052
    99
noschinl@59739
   100
noschinl@59739
   101
(* Rewriting with conditional rewriting rules works just as well. *)
noschinl@59739
   102
lemma test_theorem:
noschinl@59739
   103
  fixes x :: nat
noschinl@59739
   104
  shows "x \<le> y \<Longrightarrow> x \<ge> y \<Longrightarrow> x = y"
noschinl@59739
   105
  by (rule Orderings.order_antisym)
noschinl@59739
   106
noschinl@60050
   107
(* Premises of the conditional rule yield new subgoals. The
noschinl@60050
   108
   assumptions of the goal are propagated into these subgoals
noschinl@60050
   109
*)
noschinl@59739
   110
lemma
noschinl@60050
   111
  fixes f :: "nat \<Rightarrow> nat"
noschinl@60050
   112
  shows "f x \<le> 0 \<Longrightarrow> f x \<ge> 0 \<Longrightarrow> f x = 0"
noschinl@59739
   113
  apply (rewrite at "f x" to "0" test_theorem)
noschinl@60050
   114
  apply assumption
noschinl@60050
   115
  apply assumption
noschinl@59739
   116
  apply (rule refl)
noschinl@60050
   117
  done
noschinl@59739
   118
noschinl@60054
   119
(* This holds also for rewriting in assumptions. The order of assumptions is preserved *)
noschinl@60054
   120
lemma
noschinl@60054
   121
  assumes rewr: "PROP P \<Longrightarrow> PROP Q \<Longrightarrow> PROP R \<equiv> PROP R'"
noschinl@60054
   122
  assumes A1: "PROP S \<Longrightarrow> PROP T \<Longrightarrow> PROP U \<Longrightarrow> PROP P"
noschinl@60054
   123
  assumes A2: "PROP S \<Longrightarrow> PROP T \<Longrightarrow> PROP U \<Longrightarrow> PROP Q"
noschinl@60054
   124
  assumes C: "PROP S \<Longrightarrow> PROP R' \<Longrightarrow> PROP T \<Longrightarrow> PROP U \<Longrightarrow> PROP V"
noschinl@60054
   125
  shows "PROP S \<Longrightarrow> PROP R \<Longrightarrow> PROP T \<Longrightarrow> PROP U \<Longrightarrow> PROP V"
noschinl@60054
   126
  apply (rewrite at asm rewr)
noschinl@60054
   127
  apply (fact A1)
noschinl@60054
   128
  apply (fact A2)
noschinl@60054
   129
  apply (fact C)
noschinl@60054
   130
  done
noschinl@60054
   131
noschinl@60054
   132
noschinl@59739
   133
(*
noschinl@59739
   134
   Instantiation.
noschinl@59739
   135
noschinl@59739
   136
   Since all rewriting is now done via conversions,
noschinl@59739
   137
   instantiation becomes fairly easy to do.
noschinl@59739
   138
*)
noschinl@59739
   139
noschinl@59739
   140
(* We first introduce a function f and an extended
noschinl@59739
   141
   version of f that is annotated with an invariant. *)
noschinl@59739
   142
fun f :: "nat \<Rightarrow> nat" where "f n = n"
noschinl@59739
   143
definition "f_inv (I :: nat \<Rightarrow> bool) n \<equiv> f n"
noschinl@59739
   144
noschinl@59739
   145
lemma annotate_f: "f = f_inv I"
noschinl@59739
   146
  by (simp add: f_inv_def fun_eq_iff)
noschinl@59739
   147
noschinl@59739
   148
(* We have a lemma with a bound variable n, and
noschinl@59739
   149
   want to add an invariant to f. *)
noschinl@59739
   150
lemma
noschinl@59739
   151
  assumes "P (\<lambda>n. f_inv (\<lambda>_. True) n + 1) = x"
noschinl@59739
   152
  shows "P (\<lambda>n. f n + 1) = x"
noschinl@59739
   153
  by (rewrite to "f_inv (\<lambda>_. True)" annotate_f) fact
noschinl@59739
   154
noschinl@59739
   155
(* We can also add an invariant that contains the variable n bound in the outer context.
noschinl@59739
   156
   For this, we need to bind this variable to an identifier. *)
noschinl@59739
   157
lemma
noschinl@59739
   158
  assumes "P (\<lambda>n. f_inv (\<lambda>x. n < x + 1) n + 1) = x"
noschinl@59739
   159
  shows "P (\<lambda>n. f n + 1) = x"
noschinl@60047
   160
  by (rewrite in "\<lambda>n. \<hole>" to "f_inv (\<lambda>x. n < x + 1)" annotate_f) fact
noschinl@59739
   161
noschinl@59739
   162
(* Any identifier will work *)
noschinl@59739
   163
lemma
noschinl@59739
   164
  assumes "P (\<lambda>n. f_inv (\<lambda>x. n < x + 1) n + 1) = x"
noschinl@59739
   165
  shows "P (\<lambda>n. f n + 1) = x"
noschinl@60047
   166
  by (rewrite in "\<lambda>abc. \<hole>" to "f_inv (\<lambda>x. abc < x + 1)" annotate_f) fact
noschinl@59739
   167
noschinl@59739
   168
(* The "for" keyword. *)
noschinl@59739
   169
lemma
noschinl@59739
   170
  assumes "P (2 + 1)"
noschinl@59739
   171
  shows "\<And>x y. P (1 + 2 :: nat)"
noschinl@59739
   172
by (rewrite in "P (1 + 2)" at for (x) add.commute) fact
noschinl@59739
   173
noschinl@59739
   174
lemma
noschinl@59739
   175
  assumes "\<And>x y. P (y + x)"
noschinl@59739
   176
  shows "\<And>x y. P (x + y :: nat)"
noschinl@59739
   177
by (rewrite in "P (x + _)" at for (x y) add.commute) fact
noschinl@59739
   178
noschinl@59739
   179
lemma
noschinl@59739
   180
  assumes "\<And>x y z. y + x + z = z + y + (x::int)"
noschinl@59739
   181
  shows   "\<And>x y z. x + y + z = z + y + (x::int)"
noschinl@59739
   182
by (rewrite at "x + y" in "x + y + z" in concl at for (x y z) add.commute) fact
noschinl@59739
   183
noschinl@59739
   184
lemma
noschinl@59739
   185
  assumes "\<And>x y z. z + (x + y) = z + y + (x::int)"
noschinl@59739
   186
  shows   "\<And>x y z. x + y + z = z + y + (x::int)"
noschinl@59739
   187
by (rewrite at "(_ + y) + z" in concl at for (y z) add.commute) fact
noschinl@59739
   188
noschinl@59739
   189
lemma
noschinl@59739
   190
  assumes "\<And>x y z. x + y + z = y + z + (x::int)"
noschinl@59739
   191
  shows   "\<And>x y z. x + y + z = z + y + (x::int)"
noschinl@60047
   192
by (rewrite at "\<hole> + _" at "_ = \<hole>" in concl at for () add.commute) fact
noschinl@59739
   193
noschinl@59739
   194
(* The all-keyword can be used anywhere in the pattern where there is an \<And>-Quantifier. *)
noschinl@59739
   195
lemma
noschinl@59739
   196
  assumes "(\<And>(x::int). x < 1 + x)"
noschinl@59739
   197
  and     "(x::int) + 1 > x"
noschinl@59739
   198
  shows   "(\<And>(x::int). x + 1 > x) \<Longrightarrow> (x::int) + 1 > x"
noschinl@59739
   199
by (rewrite at "x + 1" in for (x) at asm add.commute)
noschinl@59739
   200
   (rule assms)
noschinl@59739
   201
noschinl@60079
   202
(* The rewrite method also has an ML interface *)
noschinl@60079
   203
lemma
noschinl@60079
   204
  assumes "\<And>a b. P ((a + 1) * (1 + b)) "
noschinl@60079
   205
  shows "\<And>a b :: nat. P ((a + 1) * (b + 1))"
noschinl@60079
   206
  apply (tactic \<open>
noschinl@60079
   207
    let
noschinl@60079
   208
      val (x, ctxt) = yield_singleton Variable.add_fixes "x" @{context}
noschinl@60079
   209
      (* Note that the pattern order is reversed *)
noschinl@60079
   210
      val pat = [
noschinl@60079
   211
        Rewrite.For [(x, SOME @{typ nat})],
noschinl@60079
   212
        Rewrite.In,
noschinl@60079
   213
        Rewrite.Term (@{const plus(nat)} $ Free (x, @{typ nat}) $ @{term "1 :: nat"}, [])]
noschinl@60079
   214
      val to = NONE
noschinl@60079
   215
    in Rewrite.rewrite_tac ctxt (pat, to) @{thms add.commute} 1 end
noschinl@60079
   216
  \<close>)
noschinl@60079
   217
  apply (fact assms)
noschinl@60079
   218
  done
noschinl@60079
   219
noschinl@60079
   220
lemma
noschinl@60079
   221
  assumes "Q (\<lambda>b :: int. P (\<lambda>a. a + b) (\<lambda>a. a + b))"
noschinl@60079
   222
  shows "Q (\<lambda>b :: int. P (\<lambda>a. a + b) (\<lambda>a. b + a))"
noschinl@60079
   223
  apply (tactic \<open>
noschinl@60079
   224
    let
noschinl@60079
   225
      val (x, ctxt) = yield_singleton Variable.add_fixes "x" @{context}
noschinl@60079
   226
      val pat = [
noschinl@60079
   227
        Rewrite.Concl,
noschinl@60079
   228
        Rewrite.In,
noschinl@60079
   229
        Rewrite.Term (Free ("Q", (@{typ "int"} --> TVar (("'b",0), [])) --> @{typ bool})
noschinl@60079
   230
          $ Abs ("x", @{typ int}, Rewrite.mk_hole 1 (@{typ int} --> TVar (("'b",0), [])) $ Bound 0), [(x, @{typ int})]),
noschinl@60079
   231
        Rewrite.In,
noschinl@60079
   232
        Rewrite.Term (@{const plus(int)} $ Free (x, @{typ int}) $ Var (("c", 0), @{typ int}), [])
noschinl@60079
   233
        ]
noschinl@60079
   234
      val to = NONE
noschinl@60079
   235
    in Rewrite.rewrite_tac ctxt (pat, to) @{thms add.commute} 1 end
noschinl@60079
   236
  \<close>)
noschinl@60079
   237
  apply (fact assms)
noschinl@60079
   238
  done
noschinl@60079
   239
noschinl@60079
   240
(* There is also conversion-like rewrite function: *)
noschinl@60079
   241
ML \<open>
noschinl@60079
   242
  val ct = @{cprop "Q (\<lambda>b :: int. P (\<lambda>a. a + b) (\<lambda>a. b + a))"}
noschinl@60079
   243
  val (x, ctxt) = yield_singleton Variable.add_fixes "x" @{context}
noschinl@60079
   244
  val pat = [
noschinl@60079
   245
    Rewrite.Concl,
noschinl@60079
   246
    Rewrite.In,
noschinl@60079
   247
    Rewrite.Term (Free ("Q", (@{typ "int"} --> TVar (("'b",0), [])) --> @{typ bool})
noschinl@60079
   248
      $ Abs ("x", @{typ int}, Rewrite.mk_hole 1 (@{typ int} --> TVar (("'b",0), [])) $ Bound 0), [(x, @{typ int})]),
noschinl@60079
   249
    Rewrite.In,
noschinl@60079
   250
    Rewrite.Term (@{const plus(int)} $ Free (x, @{typ int}) $ Var (("c", 0), @{typ int}), [])
noschinl@60079
   251
    ]
noschinl@60079
   252
  val to = NONE
noschinl@60079
   253
  val ct_ths = Rewrite.rewrite ctxt (pat, to) @{thms add.commute} ct
noschinl@60079
   254
    |> Seq.list_of
noschinl@60079
   255
\<close>
noschinl@60079
   256
noschinl@59739
   257
end
noschinl@59739
   258