src/HOL/Library/While_Combinator.thy
author haftmann
Thu, 25 Jul 2013 08:57:16 +0200
changeset 52729 412c9e0381a1
parent 50577 cfbad2d08412
child 53217 1a8673a6d669
permissions -rw-r--r--
factored syntactic type classes for bot and top (by Alessandro Coglio)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22803
5129e02f4df2 slightly tuned
haftmann
parents: 21404
diff changeset
     1
(*  Title:      HOL/Library/While_Combinator.thy
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     2
    Author:     Tobias Nipkow
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
     3
    Author:     Alexander Krauss
10251
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
14706
71590b7733b7 tuned document;
wenzelm
parents: 14589
diff changeset
     7
header {* A general ``while'' combinator *}
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     8
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14706
diff changeset
     9
theory While_Combinator
30738
0842e906300c normalized imports
haftmann
parents: 27487
diff changeset
    10
imports Main
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14706
diff changeset
    11
begin
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    12
37760
8380686be5cd moved example to its own file in HOL/ex
krauss
parents: 37757
diff changeset
    13
subsection {* Partial version *}
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    14
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    15
definition while_option :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a option" where
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    16
"while_option b c s = (if (\<exists>k. ~ b ((c ^^ k) s))
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    17
   then Some ((c ^^ (LEAST k. ~ b ((c ^^ k) s))) s)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    18
   else None)"
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    19
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    20
theorem while_option_unfold[code]:
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    21
"while_option b c s = (if b s then while_option b c (c s) else Some s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    22
proof cases
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    23
  assume "b s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    24
  show ?thesis
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    25
  proof (cases "\<exists>k. ~ b ((c ^^ k) s)")
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    26
    case True
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    27
    then obtain k where 1: "~ b ((c ^^ k) s)" ..
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    28
    with `b s` obtain l where "k = Suc l" by (cases k) auto
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    29
    with 1 have "~ b ((c ^^ l) (c s))" by (auto simp: funpow_swap1)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    30
    then have 2: "\<exists>l. ~ b ((c ^^ l) (c s))" ..
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    31
    from 1
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    32
    have "(LEAST k. ~ b ((c ^^ k) s)) = Suc (LEAST l. ~ b ((c ^^ Suc l) s))"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    33
      by (rule Least_Suc) (simp add: `b s`)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    34
    also have "... = Suc (LEAST l. ~ b ((c ^^ l) (c s)))"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    35
      by (simp add: funpow_swap1)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    36
    finally
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    37
    show ?thesis 
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    38
      using True 2 `b s` by (simp add: funpow_swap1 while_option_def)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    39
  next
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    40
    case False
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    41
    then have "~ (\<exists>l. ~ b ((c ^^ Suc l) s))" by blast
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    42
    then have "~ (\<exists>l. ~ b ((c ^^ l) (c s)))"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    43
      by (simp add: funpow_swap1)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    44
    with False  `b s` show ?thesis by (simp add: while_option_def)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    45
  qed
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    46
next
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    47
  assume [simp]: "~ b s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    48
  have least: "(LEAST k. ~ b ((c ^^ k) s)) = 0"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    49
    by (rule Least_equality) auto
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    50
  moreover 
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    51
  have "\<exists>k. ~ b ((c ^^ k) s)" by (rule exI[of _ "0::nat"]) auto
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    52
  ultimately show ?thesis unfolding while_option_def by auto 
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    53
qed
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    54
45834
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
    55
lemma while_option_stop2:
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
    56
 "while_option b c s = Some t \<Longrightarrow> EX k. t = (c^^k) s \<and> \<not> b t"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
    57
apply(simp add: while_option_def split: if_splits)
46365
547d1a1dcaf6 rename lambda translation schemes
blanchet
parents: 45834
diff changeset
    58
by (metis (lifting) LeastI_ex)
45834
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
    59
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
    60
lemma while_option_stop: "while_option b c s = Some t \<Longrightarrow> ~ b t"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
    61
by(metis while_option_stop2)
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    62
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    63
theorem while_option_rule:
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    64
assumes step: "!!s. P s ==> b s ==> P (c s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    65
and result: "while_option b c s = Some t"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    66
and init: "P s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    67
shows "P t"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    68
proof -
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    69
  def k == "LEAST k. ~ b ((c ^^ k) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    70
  from assms have t: "t = (c ^^ k) s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    71
    by (simp add: while_option_def k_def split: if_splits)    
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    72
  have 1: "ALL i<k. b ((c ^^ i) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    73
    by (auto simp: k_def dest: not_less_Least)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    74
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    75
  { fix i assume "i <= k" then have "P ((c ^^ i) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    76
      by (induct i) (auto simp: init step 1) }
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    77
  thus "P t" by (auto simp: t)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    78
qed
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    79
50577
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    80
lemma funpow_commute: 
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    81
  "\<lbrakk>\<forall>k' < k. f (c ((c^^k') s)) = c' (f ((c^^k') s))\<rbrakk> \<Longrightarrow> f ((c^^k) s) = (c'^^k) (f s)"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    82
by (induct k arbitrary: s) auto
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    83
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    84
lemma while_option_commute:
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    85
  assumes "\<And>s. b s = b' (f s)" "\<And>s. \<lbrakk>b s\<rbrakk> \<Longrightarrow> f (c s) = c' (f s)" 
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    86
  shows "Option.map f (while_option b c s) = while_option b' c' (f s)"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    87
unfolding while_option_def
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    88
proof (rule trans[OF if_distrib if_cong], safe, unfold option.inject)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    89
  fix k assume "\<not> b ((c ^^ k) s)"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    90
  thus "\<exists>k. \<not> b' ((c' ^^ k) (f s))"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    91
  proof (induction k arbitrary: s)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    92
    case 0 thus ?case by (auto simp: assms(1) intro: exI[of _ 0])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    93
  next
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    94
    case (Suc k)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    95
    hence "\<not> b ((c^^k) (c s))" by (auto simp: funpow_swap1)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    96
    then guess k by (rule exE[OF Suc.IH[of "c s"]])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    97
    with assms show ?case by (cases "b s") (auto simp: funpow_swap1 intro: exI[of _ "Suc k"] exI[of _ "0"])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    98
  qed
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
    99
next
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   100
  fix k assume "\<not> b' ((c' ^^ k) (f s))"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   101
  thus "\<exists>k. \<not> b ((c ^^ k) s)"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   102
  proof (induction k arbitrary: s)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   103
    case 0 thus ?case by (auto simp: assms(1) intro: exI[of _ 0])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   104
  next
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   105
    case (Suc k)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   106
    hence *: "\<not> b' ((c'^^k) (c' (f s)))" by (auto simp: funpow_swap1)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   107
    show ?case
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   108
    proof (cases "b s")
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   109
      case True
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   110
      with assms(2) * have "\<not> b' ((c'^^k) (f (c s)))" by simp 
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   111
      then guess k by (rule exE[OF Suc.IH[of "c s"]])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   112
      thus ?thesis by (auto simp: funpow_swap1 intro: exI[of _ "Suc k"])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   113
    qed (auto intro: exI[of _ "0"])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   114
  qed
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   115
next
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   116
  fix k assume k: "\<not> b' ((c' ^^ k) (f s))"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   117
  have *: "(LEAST k. \<not> b' ((c' ^^ k) (f s))) = (LEAST k. \<not> b ((c ^^ k) s))" (is "?k' = ?k")
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   118
  proof (cases ?k')
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   119
    case 0
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   120
    have "\<not> b' ((c'^^0) (f s))" unfolding 0[symmetric] by (rule LeastI[of _ k]) (rule k)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   121
    hence "\<not> b s" unfolding assms(1) by simp
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   122
    hence "?k = 0" by (intro Least_equality) auto
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   123
    with 0 show ?thesis by auto
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   124
  next
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   125
    case (Suc k')
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   126
    have "\<not> b' ((c'^^Suc k') (f s))" unfolding Suc[symmetric] by (rule LeastI) (rule k)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   127
    moreover
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   128
    { fix k assume "k \<le> k'"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   129
      hence "k < ?k'" unfolding Suc by simp
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   130
      hence "b' ((c' ^^ k) (f s))" by (rule iffD1[OF not_not, OF not_less_Least])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   131
    } note b' = this
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   132
    { fix k assume "k \<le> k'"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   133
      hence "f ((c ^^ k) s) = (c'^^k) (f s)" by (induct k) (auto simp: b' assms)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   134
      with `k \<le> k'` have "b ((c^^k) s)"
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   135
      proof (induct k)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   136
        case (Suc k) thus ?case unfolding assms(1) by (simp only: b')
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   137
      qed (simp add: b'[of 0, simplified] assms(1))
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   138
    } note b = this
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   139
    hence k': "f ((c^^k') s) = (c'^^k') (f s)" by (induct k') (auto simp: assms(2))
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   140
    ultimately show ?thesis unfolding Suc using b
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   141
    by (intro sym[OF Least_equality])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   142
       (auto simp add: assms(1) assms(2)[OF b] k' not_less_eq_eq[symmetric])
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   143
  qed
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   144
  have "f ((c ^^ ?k) s) = (c' ^^ ?k') (f s)" unfolding *
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   145
    by (auto intro: funpow_commute assms(2) dest: not_less_Least)
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   146
  thus "\<exists>z. (c ^^ ?k) s = z \<and> f z = (c' ^^ ?k') (f s)" by blast
cfbad2d08412 useful commutative diagram for while_option
traytel
parents: 50180
diff changeset
   147
qed
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   148
37760
8380686be5cd moved example to its own file in HOL/ex
krauss
parents: 37757
diff changeset
   149
subsection {* Total version *}
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   150
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   151
definition while :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   152
where "while b c s = the (while_option b c s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   153
50008
eb7f574d0303 code for while directly, not via while_option
nipkow
parents: 46365
diff changeset
   154
lemma while_unfold [code]:
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   155
  "while b c s = (if b s then while b c (c s) else s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   156
unfolding while_def by (subst while_option_unfold) simp
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   157
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   158
lemma def_while_unfold:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   159
  assumes fdef: "f == while test do"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   160
  shows "f x = (if test x then f(do x) else x)"
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   161
unfolding fdef by (fact while_unfold)
14300
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
   162
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
   163
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   164
text {*
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   165
 The proof rule for @{term while}, where @{term P} is the invariant.
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   166
*}
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   167
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   168
theorem while_rule_lemma:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   169
  assumes invariant: "!!s. P s ==> b s ==> P (c s)"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   170
    and terminate: "!!s. P s ==> \<not> b s ==> Q s"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   171
    and wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   172
  shows "P s \<Longrightarrow> Q (while b c s)"
19736
wenzelm
parents: 18372
diff changeset
   173
  using wf
wenzelm
parents: 18372
diff changeset
   174
  apply (induct s)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   175
  apply simp
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   176
  apply (subst while_unfold)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   177
  apply (simp add: invariant terminate)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   178
  done
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   179
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   180
theorem while_rule:
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   181
  "[| P s;
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   182
      !!s. [| P s; b s  |] ==> P (c s);
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   183
      !!s. [| P s; \<not> b s  |] ==> Q s;
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   184
      wf r;
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   185
      !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   186
   Q (while b c s)"
19736
wenzelm
parents: 18372
diff changeset
   187
  apply (rule while_rule_lemma)
wenzelm
parents: 18372
diff changeset
   188
     prefer 4 apply assumption
wenzelm
parents: 18372
diff changeset
   189
    apply blast
wenzelm
parents: 18372
diff changeset
   190
   apply blast
wenzelm
parents: 18372
diff changeset
   191
  apply (erule wf_subset)
wenzelm
parents: 18372
diff changeset
   192
  apply blast
wenzelm
parents: 18372
diff changeset
   193
  done
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   194
41720
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   195
text{* Proving termination: *}
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   196
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   197
theorem wf_while_option_Some:
41764
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   198
  assumes "wf {(t, s). (P s \<and> b s) \<and> t = c s}"
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   199
  and "!!s. P s \<Longrightarrow> b s \<Longrightarrow> P(c s)" and "P s"
41720
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   200
  shows "EX t. while_option b c s = Some t"
41764
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   201
using assms(1,3)
41720
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   202
apply (induct s)
41764
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   203
using assms(2)
41720
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   204
apply (subst while_option_unfold)
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   205
apply simp
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   206
done
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   207
f749155883d7 added termination lemmas
nipkow
parents: 37760
diff changeset
   208
theorem measure_while_option_Some: fixes f :: "'s \<Rightarrow> nat"
41764
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   209
shows "(!!s. P s \<Longrightarrow> b s \<Longrightarrow> P(c s) \<and> f(c s) < f s)
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   210
  \<Longrightarrow> P s \<Longrightarrow> EX t. while_option b c s = Some t"
5268aef2fe83 generalized termination lemmas
nipkow
parents: 41720
diff changeset
   211
by(blast intro: wf_while_option_Some[OF wf_if_measure, of P b f])
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   212
45834
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   213
text{* Kleene iteration starting from the empty set and assuming some finite
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   214
bounding set: *}
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   215
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   216
lemma while_option_finite_subset_Some: fixes C :: "'a set"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   217
  assumes "mono f" and "!!X. X \<subseteq> C \<Longrightarrow> f X \<subseteq> C" and "finite C"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   218
  shows "\<exists>P. while_option (\<lambda>A. f A \<noteq> A) f {} = Some P"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   219
proof(rule measure_while_option_Some[where
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   220
    f= "%A::'a set. card C - card A" and P= "%A. A \<subseteq> C \<and> A \<subseteq> f A" and s= "{}"])
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   221
  fix A assume A: "A \<subseteq> C \<and> A \<subseteq> f A" "f A \<noteq> A"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   222
  show "(f A \<subseteq> C \<and> f A \<subseteq> f (f A)) \<and> card C - card (f A) < card C - card A"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   223
    (is "?L \<and> ?R")
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   224
  proof
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   225
    show ?L by(metis A(1) assms(2) monoD[OF `mono f`])
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   226
    show ?R by (metis A assms(2,3) card_seteq diff_less_mono2 equalityI linorder_le_less_linear rev_finite_subset)
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   227
  qed
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   228
qed simp
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   229
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   230
lemma lfp_the_while_option:
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   231
  assumes "mono f" and "!!X. X \<subseteq> C \<Longrightarrow> f X \<subseteq> C" and "finite C"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   232
  shows "lfp f = the(while_option (\<lambda>A. f A \<noteq> A) f {})"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   233
proof-
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   234
  obtain P where "while_option (\<lambda>A. f A \<noteq> A) f {} = Some P"
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   235
    using while_option_finite_subset_Some[OF assms] by blast
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   236
  with while_option_stop2[OF this] lfp_Kleene_iter[OF assms(1)]
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   237
  show ?thesis by auto
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   238
qed
9c232d370244 connect while_option with lfp
nipkow
parents: 41764
diff changeset
   239
50180
c6626861c31a moved lemma
nipkow
parents: 50008
diff changeset
   240
lemma lfp_while:
c6626861c31a moved lemma
nipkow
parents: 50008
diff changeset
   241
  assumes "mono f" and "!!X. X \<subseteq> C \<Longrightarrow> f X \<subseteq> C" and "finite C"
c6626861c31a moved lemma
nipkow
parents: 50008
diff changeset
   242
  shows "lfp f = while (\<lambda>A. f A \<noteq> A) f {}"
c6626861c31a moved lemma
nipkow
parents: 50008
diff changeset
   243
unfolding while_def using assms by (rule lfp_the_while_option) blast
c6626861c31a moved lemma
nipkow
parents: 50008
diff changeset
   244
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   245
end