src/HOL/Library/While_Combinator.thy
author wenzelm
Wed, 03 Jan 2001 21:24:29 +0100
changeset 10774 4de3a0d3ae28
parent 10673 337c00fd385b
child 10984 8f49dcbec859
permissions -rw-r--r--
recdef_tc;
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
10774
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    32
recdef_tc while_aux_tc: while_aux
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    33
  apply (rule wf_same_fst)
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    34
  apply (rule wf_same_fst)
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    35
  apply (simp add: wf_iff_no_infinite_down_chain)
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    36
  apply blast
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    37
  done
4de3a0d3ae28 recdef_tc;
wenzelm
parents: 10673
diff changeset
    38
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    39
constdefs
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    40
  while :: "('a => bool) => ('a => 'a) => 'a => 'a"
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    41
  "while b c s == while_aux (b, c, s)"
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    42
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    43
lemma while_aux_unfold:
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    44
  "while_aux (b, c, s) =
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    45
    (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
    46
      then arbitrary
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    47
      else if b s then while_aux (b, c, c s)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    48
      else s)"
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    49
  apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]])
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    50
   apply (simp add: same_fst_def)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    51
  apply (rule refl)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    52
  done
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    53
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    54
text {*
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    55
 The recursion equation for @{term while}: directly executable!
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
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    58
theorem while_unfold:
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    59
    "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
    60
  apply (unfold while_def)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    61
  apply (rule while_aux_unfold [THEN trans])
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    62
  apply auto
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    63
  apply (subst while_aux_unfold)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    64
  apply simp
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    65
  apply clarify
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    66
  apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    67
  apply blast
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    68
  done
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    69
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    70
text {*
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    71
 The proof rule for @{term while}, where @{term P} is the invariant.
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
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    74
theorem while_rule_lemma[rule_format]:
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    75
  "(!!s. P s ==> b s ==> P (c s)) ==>
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    76
    (!!s. P s ==> \<not> b s ==> Q s) ==>
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    77
    wf {(t, s). P s \<and> b s \<and> t = c s} ==>
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    78
    P s --> Q (while b c s)"
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    79
proof -
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    80
  case antecedent
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    81
  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
    82
  show ?thesis
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    83
    apply (induct s rule: wf [THEN wf_induct])
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    84
    apply simp
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    85
    apply clarify
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    86
    apply (subst while_unfold)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    87
    apply (simp add: antecedent)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    88
    done
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    89
qed
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    90
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    91
theorem while_rule:
10673
337c00fd385b unsymbolize;
wenzelm
parents: 10653
diff changeset
    92
  "[| P s; !!s. [| P s; b s  |] ==> P (c s);
337c00fd385b unsymbolize;
wenzelm
parents: 10653
diff changeset
    93
    !!s. [| P s; \<not> b s  |] ==> Q s;
337c00fd385b unsymbolize;
wenzelm
parents: 10653
diff changeset
    94
    wf r;  !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    95
    Q (while b c s)"
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    96
apply (rule while_rule_lemma)
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    97
prefer 4 apply assumption
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    98
apply blast
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    99
apply blast
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   100
apply(erule wf_subset)
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   101
apply blast
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   102
done
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   103
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   104
hide const while_aux
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   105
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   106
end