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