src/HOL/ex/InductiveInvariant_examples.thy
author paulson
Wed Mar 30 08:33:41 2005 +0200 (2005-03-30)
changeset 15636 57c437b70521
parent 14244 f58598341d30
child 16417 9bc16273c2d4
permissions -rw-r--r--
converted from DOS to UNIX format
paulson@15636
     1
theory InductiveInvariant_examples = InductiveInvariant :
paulson@15636
     2
paulson@15636
     3
(** Authors: Sava Krsti\'{c} and John Matthews **)
paulson@15636
     4
(**    Date: Oct 17, 2003                      **)
paulson@15636
     5
paulson@15636
     6
text "A simple example showing how to use an inductive invariant
paulson@15636
     7
      to solve termination conditions generated by recdef on
paulson@15636
     8
      nested recursive function definitions."
paulson@15636
     9
paulson@15636
    10
consts g :: "nat => nat"
paulson@15636
    11
paulson@15636
    12
recdef (permissive) g "less_than"
paulson@15636
    13
  "g 0 = 0"
paulson@15636
    14
  "g (Suc n) = g (g n)"
paulson@15636
    15
paulson@15636
    16
text "We can prove the unsolved termination condition for
paulson@15636
    17
      g by showing it is an inductive invariant."
paulson@15636
    18
paulson@15636
    19
recdef_tc g_tc[simp]: g
paulson@15636
    20
apply (rule allI)
paulson@15636
    21
apply (rule_tac x=n in tfl_indinv_wfrec [OF g_def])
paulson@15636
    22
apply (auto simp add: indinv_def split: nat.split)
paulson@15636
    23
apply (frule_tac x=nat in spec)
paulson@15636
    24
apply (drule_tac x="f nat" in spec)
paulson@15636
    25
by auto
paulson@15636
    26
paulson@15636
    27
paulson@15636
    28
text "This declaration invokes Isabelle's simplifier to
paulson@15636
    29
      remove any termination conditions before adding
paulson@15636
    30
      g's rules to the simpset."
paulson@15636
    31
declare g.simps [simplified, simp]
paulson@15636
    32
paulson@15636
    33
paulson@15636
    34
text "This is an example where the termination condition generated
paulson@15636
    35
      by recdef is not itself an inductive invariant."
paulson@15636
    36
paulson@15636
    37
consts g' :: "nat => nat"
paulson@15636
    38
recdef (permissive) g' "less_than"
paulson@15636
    39
  "g' 0 = 0"
paulson@15636
    40
  "g' (Suc n) = g' n + g' (g' n)"
paulson@15636
    41
paulson@15636
    42
thm g'.simps
paulson@15636
    43
paulson@15636
    44
paulson@15636
    45
text "The strengthened inductive invariant is as follows
paulson@15636
    46
      (this invariant also works for the first example above):"
paulson@15636
    47
paulson@15636
    48
lemma g'_inv: "g' n = 0"
paulson@15636
    49
thm tfl_indinv_wfrec [OF g'_def]
paulson@15636
    50
apply (rule_tac x=n in tfl_indinv_wfrec [OF g'_def])
paulson@15636
    51
by (auto simp add: indinv_def split: nat.split)
paulson@15636
    52
paulson@15636
    53
recdef_tc g'_tc[simp]: g'
paulson@15636
    54
by (simp add: g'_inv)
paulson@15636
    55
paulson@15636
    56
text "Now we can remove the termination condition from
paulson@15636
    57
      the rules for g' ."
paulson@15636
    58
thm g'.simps [simplified]
paulson@15636
    59
paulson@15636
    60
paulson@15636
    61
text {* Sometimes a recursive definition is partial, that is, it
paulson@15636
    62
        is only meant to be invoked on "good" inputs. As a contrived
paulson@15636
    63
        example, we will define a new version of g that is only
paulson@15636
    64
        well defined for even inputs greater than zero. *}
paulson@15636
    65
paulson@15636
    66
consts g_even :: "nat => nat"
paulson@15636
    67
recdef (permissive) g_even "less_than"
paulson@15636
    68
  "g_even (Suc (Suc 0)) = 3"
paulson@15636
    69
  "g_even n = g_even (g_even (n - 2) - 1)"
paulson@15636
    70
paulson@15636
    71
paulson@15636
    72
text "We can prove a conditional version of the unsolved termination
paulson@15636
    73
      condition for @{term g_even} by proving a stronger inductive invariant."
paulson@15636
    74
paulson@15636
    75
lemma g_even_indinv: "\<exists>k. n = Suc (Suc (2*k)) ==> g_even n = 3"
paulson@15636
    76
apply (rule_tac D="{n. \<exists>k. n = Suc (Suc (2*k))}" and x=n in tfl_indinv_on_wfrec [OF g_even_def])
paulson@15636
    77
apply (auto simp add: indinv_on_def split: nat.split)
paulson@15636
    78
by (case_tac ka, auto)
paulson@15636
    79
paulson@15636
    80
paulson@15636
    81
text "Now we can prove that the second recursion equation for @{term g_even}
paulson@15636
    82
      holds, provided that n is an even number greater than two."
paulson@15636
    83
paulson@15636
    84
theorem g_even_n: "\<exists>k. n = 2*k + 4 ==> g_even n = g_even (g_even (n - 2) - 1)"
paulson@15636
    85
apply (subgoal_tac "(\<exists>k. n - 2 = 2*k + 2) & (\<exists>k. n = 2*k + 2)")
paulson@15636
    86
by (auto simp add: g_even_indinv, arith)
paulson@15636
    87
paulson@15636
    88
paulson@15636
    89
text "McCarthy's ninety-one function. This function requires a
paulson@15636
    90
      non-standard measure to prove termination."
paulson@15636
    91
paulson@15636
    92
consts ninety_one :: "nat => nat"
paulson@15636
    93
recdef (permissive) ninety_one "measure (%n. 101 - n)"
paulson@15636
    94
  "ninety_one x = (if 100 < x
paulson@15636
    95
                     then x - 10
paulson@15636
    96
                     else (ninety_one (ninety_one (x+11))))"
paulson@15636
    97
paulson@15636
    98
text "To discharge the termination condition, we will prove
paulson@15636
    99
      a strengthened inductive invariant:
paulson@15636
   100
         S x y == x < y + 11"
paulson@15636
   101
paulson@15636
   102
lemma ninety_one_inv: "n < ninety_one n + 11"
paulson@15636
   103
apply (rule_tac x=n in tfl_indinv_wfrec [OF ninety_one_def])
paulson@15636
   104
apply force
paulson@15636
   105
apply (auto simp add: indinv_def measure_def inv_image_def)
paulson@15636
   106
apply (frule_tac x="x+11" in spec)
paulson@15636
   107
apply (frule_tac x="f (x + 11)" in spec)
paulson@15636
   108
by arith
paulson@15636
   109
paulson@15636
   110
text "Proving the termination condition using the
paulson@15636
   111
      strengthened inductive invariant."
paulson@15636
   112
paulson@15636
   113
recdef_tc ninety_one_tc[rule_format]: ninety_one
paulson@15636
   114
apply clarify
paulson@15636
   115
by (cut_tac n="x+11" in ninety_one_inv, arith)
paulson@15636
   116
paulson@15636
   117
text "Now we can remove the termination condition from
paulson@15636
   118
      the simplification rule for @{term ninety_one}."
paulson@15636
   119
paulson@15636
   120
theorem def_ninety_one:
paulson@15636
   121
"ninety_one x = (if 100 < x
paulson@15636
   122
                   then x - 10
paulson@15636
   123
                   else ninety_one (ninety_one (x+11)))"
paulson@15636
   124
by (subst ninety_one.simps,
paulson@15636
   125
    simp add: ninety_one_tc measure_def inv_image_def)
paulson@15636
   126
paulson@14244
   127
end