src/HOLCF/Cont.thy
author huffman
Mon May 11 08:28:09 2009 -0700 (2009-05-11)
changeset 31095 b79d140f6d0b
parent 31076 99fe356cbbc2
child 31902 862ae16a799d
permissions -rw-r--r--
simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
     1 (*  Title:      HOLCF/Cont.thy
     2     Author:     Franz Regensburger
     3 *)
     4 
     5 header {* Continuity and monotonicity *}
     6 
     7 theory Cont
     8 imports Pcpo
     9 begin
    10 
    11 text {*
    12    Now we change the default class! Form now on all untyped type variables are
    13    of default class po
    14 *}
    15 
    16 defaultsort po
    17 
    18 subsection {* Definitions *}
    19 
    20 definition
    21   monofun :: "('a \<Rightarrow> 'b) \<Rightarrow> bool"  -- "monotonicity"  where
    22   "monofun f = (\<forall>x y. x \<sqsubseteq> y \<longrightarrow> f x \<sqsubseteq> f y)"
    23 
    24 definition
    25   contlub :: "('a::cpo \<Rightarrow> 'b::cpo) \<Rightarrow> bool"  -- "first cont. def" where
    26   "contlub f = (\<forall>Y. chain Y \<longrightarrow> f (\<Squnion>i. Y i) = (\<Squnion>i. f (Y i)))"
    27 
    28 definition
    29   cont :: "('a::cpo \<Rightarrow> 'b::cpo) \<Rightarrow> bool"  -- "secnd cont. def" where
    30   "cont f = (\<forall>Y. chain Y \<longrightarrow> range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i))"
    31 
    32 lemma contlubI:
    33   "\<lbrakk>\<And>Y. chain Y \<Longrightarrow> f (\<Squnion>i. Y i) = (\<Squnion>i. f (Y i))\<rbrakk> \<Longrightarrow> contlub f"
    34 by (simp add: contlub_def)
    35 
    36 lemma contlubE: 
    37   "\<lbrakk>contlub f; chain Y\<rbrakk> \<Longrightarrow> f (\<Squnion>i. Y i) = (\<Squnion>i. f (Y i))" 
    38 by (simp add: contlub_def)
    39 
    40 lemma contI:
    41   "\<lbrakk>\<And>Y. chain Y \<Longrightarrow> range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i)\<rbrakk> \<Longrightarrow> cont f"
    42 by (simp add: cont_def)
    43 
    44 lemma contE:
    45   "\<lbrakk>cont f; chain Y\<rbrakk> \<Longrightarrow> range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i)"
    46 by (simp add: cont_def)
    47 
    48 lemma monofunI: 
    49   "\<lbrakk>\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y\<rbrakk> \<Longrightarrow> monofun f"
    50 by (simp add: monofun_def)
    51 
    52 lemma monofunE: 
    53   "\<lbrakk>monofun f; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> f x \<sqsubseteq> f y"
    54 by (simp add: monofun_def)
    55 
    56 
    57 subsection {* @{prop "monofun f \<and> contlub f \<equiv> cont f"} *}
    58 
    59 text {* monotone functions map chains to chains *}
    60 
    61 lemma ch2ch_monofun: "\<lbrakk>monofun f; chain Y\<rbrakk> \<Longrightarrow> chain (\<lambda>i. f (Y i))"
    62 apply (rule chainI)
    63 apply (erule monofunE)
    64 apply (erule chainE)
    65 done
    66 
    67 text {* monotone functions map upper bound to upper bounds *}
    68 
    69 lemma ub2ub_monofun: 
    70   "\<lbrakk>monofun f; range Y <| u\<rbrakk> \<Longrightarrow> range (\<lambda>i. f (Y i)) <| f u"
    71 apply (rule ub_rangeI)
    72 apply (erule monofunE)
    73 apply (erule ub_rangeD)
    74 done
    75 
    76 text {* left to right: @{prop "monofun f \<and> contlub f \<Longrightarrow> cont f"} *}
    77 
    78 lemma monocontlub2cont: "\<lbrakk>monofun f; contlub f\<rbrakk> \<Longrightarrow> cont f"
    79 apply (rule contI)
    80 apply (rule thelubE)
    81 apply (erule (1) ch2ch_monofun)
    82 apply (erule (1) contlubE [symmetric])
    83 done
    84 
    85 text {* first a lemma about binary chains *}
    86 
    87 lemma binchain_cont:
    88   "\<lbrakk>cont f; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> range (\<lambda>i::nat. f (if i = 0 then x else y)) <<| f y"
    89 apply (subgoal_tac "f (\<Squnion>i::nat. if i = 0 then x else y) = f y")
    90 apply (erule subst)
    91 apply (erule contE)
    92 apply (erule bin_chain)
    93 apply (rule_tac f=f in arg_cong)
    94 apply (erule lub_bin_chain [THEN thelubI])
    95 done
    96 
    97 text {* right to left: @{prop "cont f \<Longrightarrow> monofun f \<and> contlub f"} *}
    98 text {* part1: @{prop "cont f \<Longrightarrow> monofun f"} *}
    99 
   100 lemma cont2mono: "cont f \<Longrightarrow> monofun f"
   101 apply (rule monofunI)
   102 apply (drule (1) binchain_cont)
   103 apply (drule_tac i=0 in is_ub_lub)
   104 apply simp
   105 done
   106 
   107 lemmas cont2monofunE = cont2mono [THEN monofunE]
   108 
   109 lemmas ch2ch_cont = cont2mono [THEN ch2ch_monofun]
   110 
   111 text {* right to left: @{prop "cont f \<Longrightarrow> monofun f \<and> contlub f"} *}
   112 text {* part2: @{prop "cont f \<Longrightarrow> contlub f"} *}
   113 
   114 lemma cont2contlub: "cont f \<Longrightarrow> contlub f"
   115 apply (rule contlubI)
   116 apply (rule thelubI [symmetric])
   117 apply (erule (1) contE)
   118 done
   119 
   120 lemmas cont2contlubE = cont2contlub [THEN contlubE]
   121 
   122 lemma contI2:
   123   assumes mono: "monofun f"
   124   assumes below: "\<And>Y. \<lbrakk>chain Y; chain (\<lambda>i. f (Y i))\<rbrakk>
   125      \<Longrightarrow> f (\<Squnion>i. Y i) \<sqsubseteq> (\<Squnion>i. f (Y i))"
   126   shows "cont f"
   127 apply (rule monocontlub2cont)
   128 apply (rule mono)
   129 apply (rule contlubI)
   130 apply (rule below_antisym)
   131 apply (rule below, assumption)
   132 apply (erule ch2ch_monofun [OF mono])
   133 apply (rule is_lub_thelub)
   134 apply (erule ch2ch_monofun [OF mono])
   135 apply (rule ub2ub_monofun [OF mono])
   136 apply (rule is_lubD1)
   137 apply (erule cpo_lubI)
   138 done
   139 
   140 subsection {* Continuity simproc *}
   141 
   142 ML {*
   143 structure Cont2ContData = NamedThmsFun
   144   ( val name = "cont2cont" val description = "continuity intro rule" )
   145 *}
   146 
   147 setup Cont2ContData.setup
   148 
   149 text {*
   150   Given the term @{term "cont f"}, the procedure tries to construct the
   151   theorem @{term "cont f == True"}. If this theorem cannot be completely
   152   solved by the introduction rules, then the procedure returns a
   153   conditional rewrite rule with the unsolved subgoals as premises.
   154 *}
   155 
   156 simproc_setup cont_proc ("cont f") = {*
   157   fn phi => fn ss => fn ct =>
   158     let
   159       val tr = instantiate' [] [SOME ct] @{thm Eq_TrueI};
   160       val rules = Cont2ContData.get (Simplifier.the_context ss);
   161       val tac = REPEAT_ALL_NEW (match_tac rules);
   162     in SINGLE (tac 1) tr end
   163 *}
   164 
   165 subsection {* Continuity of basic functions *}
   166 
   167 text {* The identity function is continuous *}
   168 
   169 lemma cont_id [cont2cont]: "cont (\<lambda>x. x)"
   170 apply (rule contI)
   171 apply (erule cpo_lubI)
   172 done
   173 
   174 text {* constant functions are continuous *}
   175 
   176 lemma cont_const [cont2cont]: "cont (\<lambda>x. c)"
   177 apply (rule contI)
   178 apply (rule lub_const)
   179 done
   180 
   181 text {* application of functions is continuous *}
   182 
   183 lemma cont_apply:
   184   fixes f :: "'a::cpo \<Rightarrow> 'b::cpo \<Rightarrow> 'c::cpo" and t :: "'a \<Rightarrow> 'b"
   185   assumes 1: "cont (\<lambda>x. t x)"
   186   assumes 2: "\<And>x. cont (\<lambda>y. f x y)"
   187   assumes 3: "\<And>y. cont (\<lambda>x. f x y)"
   188   shows "cont (\<lambda>x. (f x) (t x))"
   189 proof (rule monocontlub2cont [OF monofunI contlubI])
   190   fix x y :: "'a" assume "x \<sqsubseteq> y"
   191   then show "f x (t x) \<sqsubseteq> f y (t y)"
   192     by (auto intro: cont2monofunE [OF 1]
   193                     cont2monofunE [OF 2]
   194                     cont2monofunE [OF 3]
   195                     below_trans)
   196 next
   197   fix Y :: "nat \<Rightarrow> 'a" assume "chain Y"
   198   then show "f (\<Squnion>i. Y i) (t (\<Squnion>i. Y i)) = (\<Squnion>i. f (Y i) (t (Y i)))"
   199     by (simp only: cont2contlubE [OF 1] ch2ch_cont [OF 1]
   200                    cont2contlubE [OF 2] ch2ch_cont [OF 2]
   201                    cont2contlubE [OF 3] ch2ch_cont [OF 3]
   202                    diag_lub)
   203 qed
   204 
   205 lemma cont_compose:
   206   "\<lbrakk>cont c; cont (\<lambda>x. f x)\<rbrakk> \<Longrightarrow> cont (\<lambda>x. c (f x))"
   207 by (rule cont_apply [OF _ _ cont_const])
   208 
   209 text {* if-then-else is continuous *}
   210 
   211 lemma cont_if [simp]:
   212   "\<lbrakk>cont f; cont g\<rbrakk> \<Longrightarrow> cont (\<lambda>x. if b then f x else g x)"
   213 by (induct b) simp_all
   214 
   215 subsection {* Finite chains and flat pcpos *}
   216 
   217 text {* monotone functions map finite chains to finite chains *}
   218 
   219 lemma monofun_finch2finch:
   220   "\<lbrakk>monofun f; finite_chain Y\<rbrakk> \<Longrightarrow> finite_chain (\<lambda>n. f (Y n))"
   221 apply (unfold finite_chain_def)
   222 apply (simp add: ch2ch_monofun)
   223 apply (force simp add: max_in_chain_def)
   224 done
   225 
   226 text {* The same holds for continuous functions *}
   227 
   228 lemma cont_finch2finch:
   229   "\<lbrakk>cont f; finite_chain Y\<rbrakk> \<Longrightarrow> finite_chain (\<lambda>n. f (Y n))"
   230 by (rule cont2mono [THEN monofun_finch2finch])
   231 
   232 lemma chfindom_monofun2cont: "monofun f \<Longrightarrow> cont (f::'a::chfin \<Rightarrow> 'b::cpo)"
   233 apply (rule monocontlub2cont)
   234 apply assumption
   235 apply (rule contlubI)
   236 apply (frule chfin2finch)
   237 apply (clarsimp simp add: finite_chain_def)
   238 apply (subgoal_tac "max_in_chain i (\<lambda>i. f (Y i))")
   239 apply (simp add: maxinch_is_thelub ch2ch_monofun)
   240 apply (force simp add: max_in_chain_def)
   241 done
   242 
   243 text {* some properties of flat *}
   244 
   245 lemma flatdom_strict2mono: "f \<bottom> = \<bottom> \<Longrightarrow> monofun (f::'a::flat \<Rightarrow> 'b::pcpo)"
   246 apply (rule monofunI)
   247 apply (drule ax_flat)
   248 apply auto
   249 done
   250 
   251 lemma flatdom_strict2cont: "f \<bottom> = \<bottom> \<Longrightarrow> cont (f::'a::flat \<Rightarrow> 'b::pcpo)"
   252 by (rule flatdom_strict2mono [THEN chfindom_monofun2cont])
   253 
   254 text {* functions with discrete domain *}
   255 
   256 lemma cont_discrete_cpo [simp]: "cont (f::'a::discrete_cpo \<Rightarrow> 'b::cpo)"
   257 apply (rule contI)
   258 apply (drule discrete_chain_const, clarify)
   259 apply (simp add: lub_const)
   260 done
   261 
   262 end