src/HOL/Library/While_Combinator.thy
author nipkow
Wed Dec 13 09:32:55 2000 +0100 (2000-12-13)
changeset 10653 55f33da63366
parent 10269 cc20c9d7e682
child 10673 337c00fd385b
permissions -rw-r--r--
small mods.
wenzelm@10251
     1
(*  Title:      HOL/Library/While.thy
wenzelm@10251
     2
    ID:         $Id$
wenzelm@10251
     3
    Author:     Tobias Nipkow
wenzelm@10251
     4
    Copyright   2000 TU Muenchen
wenzelm@10251
     5
*)
wenzelm@10251
     6
wenzelm@10251
     7
header {*
wenzelm@10251
     8
 \title{A general ``while'' combinator}
wenzelm@10251
     9
 \author{Tobias Nipkow}
wenzelm@10251
    10
*}
wenzelm@10251
    11
wenzelm@10251
    12
theory While_Combinator = Main:
wenzelm@10251
    13
wenzelm@10251
    14
text {*
wenzelm@10251
    15
 We define a while-combinator @{term while} and prove: (a) an
wenzelm@10251
    16
 unrestricted unfolding law (even if while diverges!)  (I got this
wenzelm@10251
    17
 idea from Wolfgang Goerigk), and (b) the invariant rule for reasoning
wenzelm@10251
    18
 about @{term while}.
wenzelm@10251
    19
*}
wenzelm@10251
    20
wenzelm@10251
    21
consts while_aux :: "('a => bool) \<times> ('a => 'a) \<times> 'a => 'a"
wenzelm@10251
    22
recdef while_aux
wenzelm@10251
    23
  "same_fst (\<lambda>b. True) (\<lambda>b. same_fst (\<lambda>c. True) (\<lambda>c.
wenzelm@10251
    24
      {(t, s).  b s \<and> c s = t \<and>
wenzelm@10251
    25
        \<not> (\<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))}))"
wenzelm@10251
    26
  "while_aux (b, c, s) =
wenzelm@10251
    27
    (if (\<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))
wenzelm@10251
    28
      then arbitrary
wenzelm@10251
    29
      else if b s then while_aux (b, c, c s)
wenzelm@10251
    30
      else s)"
wenzelm@10251
    31
wenzelm@10251
    32
constdefs
wenzelm@10251
    33
  while :: "('a => bool) => ('a => 'a) => 'a => 'a"
wenzelm@10251
    34
  "while b c s == while_aux (b, c, s)"
wenzelm@10251
    35
wenzelm@10251
    36
ML_setup {*
wenzelm@10251
    37
  goalw_cterm [] (cterm_of (sign_of (the_context ()))
wenzelm@10269
    38
    (HOLogic.mk_Trueprop (hd (RecdefPackage.tcs_of (the_context ()) "while_aux"))));
nipkow@10653
    39
  br wf_same_fst 1;
nipkow@10653
    40
  br wf_same_fst 1;
wenzelm@10251
    41
  by (asm_full_simp_tac (simpset() addsimps [wf_iff_no_infinite_down_chain]) 1);
wenzelm@10251
    42
  by (Blast_tac 1);
wenzelm@10251
    43
  qed "while_aux_tc";
wenzelm@10269
    44
*} (* FIXME cannot access recdef tcs in Isar yet! *)
wenzelm@10251
    45
wenzelm@10251
    46
lemma while_aux_unfold:
wenzelm@10251
    47
  "while_aux (b, c, s) =
wenzelm@10251
    48
    (if \<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1))
wenzelm@10251
    49
      then arbitrary
wenzelm@10251
    50
      else if b s then while_aux (b, c, c s)
wenzelm@10251
    51
      else s)"
wenzelm@10251
    52
  apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]])
wenzelm@10251
    53
   apply (simp add: same_fst_def)
wenzelm@10251
    54
  apply (rule refl)
wenzelm@10251
    55
  done
wenzelm@10251
    56
wenzelm@10251
    57
text {*
wenzelm@10251
    58
 The recursion equation for @{term while}: directly executable!
wenzelm@10251
    59
*}
wenzelm@10251
    60
wenzelm@10251
    61
theorem while_unfold:
wenzelm@10251
    62
    "while b c s = (if b s then while b c (c s) else s)"
wenzelm@10251
    63
  apply (unfold while_def)
wenzelm@10251
    64
  apply (rule while_aux_unfold [THEN trans])
wenzelm@10251
    65
  apply auto
wenzelm@10251
    66
  apply (subst while_aux_unfold)
wenzelm@10251
    67
  apply simp
wenzelm@10251
    68
  apply clarify
wenzelm@10251
    69
  apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE)
wenzelm@10251
    70
  apply blast
wenzelm@10251
    71
  done
wenzelm@10251
    72
wenzelm@10251
    73
text {*
wenzelm@10251
    74
 The proof rule for @{term while}, where @{term P} is the invariant.
wenzelm@10251
    75
*}
wenzelm@10251
    76
nipkow@10653
    77
theorem while_rule_lemma[rule_format]:
wenzelm@10251
    78
  "(!!s. P s ==> b s ==> P (c s)) ==>
wenzelm@10251
    79
    (!!s. P s ==> \<not> b s ==> Q s) ==>
wenzelm@10251
    80
    wf {(t, s). P s \<and> b s \<and> t = c s} ==>
wenzelm@10251
    81
    P s --> Q (while b c s)"
wenzelm@10251
    82
proof -
wenzelm@10251
    83
  case antecedent
wenzelm@10251
    84
  assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
wenzelm@10251
    85
  show ?thesis
wenzelm@10251
    86
    apply (induct s rule: wf [THEN wf_induct])
wenzelm@10251
    87
    apply simp
wenzelm@10251
    88
    apply clarify
wenzelm@10251
    89
    apply (subst while_unfold)
wenzelm@10251
    90
    apply (simp add: antecedent)
wenzelm@10251
    91
    done
wenzelm@10251
    92
qed
wenzelm@10251
    93
nipkow@10653
    94
theorem while_rule:
nipkow@10653
    95
  "\<lbrakk> P s; \<And>s. \<lbrakk> P s; b s \<rbrakk> \<Longrightarrow> P(c s);
nipkow@10653
    96
    \<And>s.\<lbrakk> P s; \<not> b s \<rbrakk> \<Longrightarrow> Q s;
nipkow@10653
    97
    wf r;  \<And>s. \<lbrakk> P s; b s \<rbrakk> \<Longrightarrow> (c s,s) \<in> r \<rbrakk> \<Longrightarrow>
nipkow@10653
    98
    Q (while b c s)"
nipkow@10653
    99
apply (rule while_rule_lemma)
nipkow@10653
   100
prefer 4 apply assumption
nipkow@10653
   101
apply blast
nipkow@10653
   102
apply blast
nipkow@10653
   103
apply(erule wf_subset)
nipkow@10653
   104
apply blast
nipkow@10653
   105
done
nipkow@10653
   106
wenzelm@10251
   107
hide const while_aux
wenzelm@10251
   108
wenzelm@10251
   109
end