src/HOL/HOLCF/Cont.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (20 months ago)
changeset 67022 49309fe530fd
parent 62175 8ffc4d0e652d
child 67312 0d25e02759b7
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
     1 (*  Title:      HOL/HOLCF/Cont.thy
     2     Author:     Franz Regensburger
     3     Author:     Brian Huffman
     4 *)
     5 
     6 section \<open>Continuity and monotonicity\<close>
     7 
     8 theory Cont
     9 imports Pcpo
    10 begin
    11 
    12 text \<open>
    13    Now we change the default class! Form now on all untyped type variables are
    14    of default class po
    15 \<close>
    16 
    17 default_sort po
    18 
    19 subsection \<open>Definitions\<close>
    20 
    21 definition
    22   monofun :: "('a \<Rightarrow> 'b) \<Rightarrow> bool"  \<comment> "monotonicity"  where
    23   "monofun f = (\<forall>x y. x \<sqsubseteq> y \<longrightarrow> f x \<sqsubseteq> f y)"
    24 
    25 definition
    26   cont :: "('a::cpo \<Rightarrow> 'b::cpo) \<Rightarrow> bool"
    27 where
    28   "cont f = (\<forall>Y. chain Y \<longrightarrow> range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i))"
    29 
    30 lemma contI:
    31   "\<lbrakk>\<And>Y. chain Y \<Longrightarrow> range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i)\<rbrakk> \<Longrightarrow> cont f"
    32 by (simp add: cont_def)
    33 
    34 lemma contE:
    35   "\<lbrakk>cont f; chain Y\<rbrakk> \<Longrightarrow> range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i)"
    36 by (simp add: cont_def)
    37 
    38 lemma monofunI: 
    39   "\<lbrakk>\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y\<rbrakk> \<Longrightarrow> monofun f"
    40 by (simp add: monofun_def)
    41 
    42 lemma monofunE: 
    43   "\<lbrakk>monofun f; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> f x \<sqsubseteq> f y"
    44 by (simp add: monofun_def)
    45 
    46 
    47 subsection \<open>Equivalence of alternate definition\<close>
    48 
    49 text \<open>monotone functions map chains to chains\<close>
    50 
    51 lemma ch2ch_monofun: "\<lbrakk>monofun f; chain Y\<rbrakk> \<Longrightarrow> chain (\<lambda>i. f (Y i))"
    52 apply (rule chainI)
    53 apply (erule monofunE)
    54 apply (erule chainE)
    55 done
    56 
    57 text \<open>monotone functions map upper bound to upper bounds\<close>
    58 
    59 lemma ub2ub_monofun: 
    60   "\<lbrakk>monofun f; range Y <| u\<rbrakk> \<Longrightarrow> range (\<lambda>i. f (Y i)) <| f u"
    61 apply (rule ub_rangeI)
    62 apply (erule monofunE)
    63 apply (erule ub_rangeD)
    64 done
    65 
    66 text \<open>a lemma about binary chains\<close>
    67 
    68 lemma binchain_cont:
    69   "\<lbrakk>cont f; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> range (\<lambda>i::nat. f (if i = 0 then x else y)) <<| f y"
    70 apply (subgoal_tac "f (\<Squnion>i::nat. if i = 0 then x else y) = f y")
    71 apply (erule subst)
    72 apply (erule contE)
    73 apply (erule bin_chain)
    74 apply (rule_tac f=f in arg_cong)
    75 apply (erule is_lub_bin_chain [THEN lub_eqI])
    76 done
    77 
    78 text \<open>continuity implies monotonicity\<close>
    79 
    80 lemma cont2mono: "cont f \<Longrightarrow> monofun f"
    81 apply (rule monofunI)
    82 apply (drule (1) binchain_cont)
    83 apply (drule_tac i=0 in is_lub_rangeD1)
    84 apply simp
    85 done
    86 
    87 lemmas cont2monofunE = cont2mono [THEN monofunE]
    88 
    89 lemmas ch2ch_cont = cont2mono [THEN ch2ch_monofun]
    90 
    91 text \<open>continuity implies preservation of lubs\<close>
    92 
    93 lemma cont2contlubE:
    94   "\<lbrakk>cont f; chain Y\<rbrakk> \<Longrightarrow> f (\<Squnion>i. Y i) = (\<Squnion>i. f (Y i))"
    95 apply (rule lub_eqI [symmetric])
    96 apply (erule (1) contE)
    97 done
    98 
    99 lemma contI2:
   100   fixes f :: "'a::cpo \<Rightarrow> 'b::cpo"
   101   assumes mono: "monofun f"
   102   assumes below: "\<And>Y. \<lbrakk>chain Y; chain (\<lambda>i. f (Y i))\<rbrakk>
   103      \<Longrightarrow> f (\<Squnion>i. Y i) \<sqsubseteq> (\<Squnion>i. f (Y i))"
   104   shows "cont f"
   105 proof (rule contI)
   106   fix Y :: "nat \<Rightarrow> 'a"
   107   assume Y: "chain Y"
   108   with mono have fY: "chain (\<lambda>i. f (Y i))"
   109     by (rule ch2ch_monofun)
   110   have "(\<Squnion>i. f (Y i)) = f (\<Squnion>i. Y i)"
   111     apply (rule below_antisym)
   112     apply (rule lub_below [OF fY])
   113     apply (rule monofunE [OF mono])
   114     apply (rule is_ub_thelub [OF Y])
   115     apply (rule below [OF Y fY])
   116     done
   117   with fY show "range (\<lambda>i. f (Y i)) <<| f (\<Squnion>i. Y i)"
   118     by (rule thelubE)
   119 qed
   120 
   121 subsection \<open>Collection of continuity rules\<close>
   122 
   123 named_theorems cont2cont "continuity intro rule"
   124 
   125 
   126 subsection \<open>Continuity of basic functions\<close>
   127 
   128 text \<open>The identity function is continuous\<close>
   129 
   130 lemma cont_id [simp, cont2cont]: "cont (\<lambda>x. x)"
   131 apply (rule contI)
   132 apply (erule cpo_lubI)
   133 done
   134 
   135 text \<open>constant functions are continuous\<close>
   136 
   137 lemma cont_const [simp, cont2cont]: "cont (\<lambda>x. c)"
   138   using is_lub_const by (rule contI)
   139 
   140 text \<open>application of functions is continuous\<close>
   141 
   142 lemma cont_apply:
   143   fixes f :: "'a::cpo \<Rightarrow> 'b::cpo \<Rightarrow> 'c::cpo" and t :: "'a \<Rightarrow> 'b"
   144   assumes 1: "cont (\<lambda>x. t x)"
   145   assumes 2: "\<And>x. cont (\<lambda>y. f x y)"
   146   assumes 3: "\<And>y. cont (\<lambda>x. f x y)"
   147   shows "cont (\<lambda>x. (f x) (t x))"
   148 proof (rule contI2 [OF monofunI])
   149   fix x y :: "'a" assume "x \<sqsubseteq> y"
   150   then show "f x (t x) \<sqsubseteq> f y (t y)"
   151     by (auto intro: cont2monofunE [OF 1]
   152                     cont2monofunE [OF 2]
   153                     cont2monofunE [OF 3]
   154                     below_trans)
   155 next
   156   fix Y :: "nat \<Rightarrow> 'a" assume "chain Y"
   157   then show "f (\<Squnion>i. Y i) (t (\<Squnion>i. Y i)) \<sqsubseteq> (\<Squnion>i. f (Y i) (t (Y i)))"
   158     by (simp only: cont2contlubE [OF 1] ch2ch_cont [OF 1]
   159                    cont2contlubE [OF 2] ch2ch_cont [OF 2]
   160                    cont2contlubE [OF 3] ch2ch_cont [OF 3]
   161                    diag_lub below_refl)
   162 qed
   163 
   164 lemma cont_compose:
   165   "\<lbrakk>cont c; cont (\<lambda>x. f x)\<rbrakk> \<Longrightarrow> cont (\<lambda>x. c (f x))"
   166 by (rule cont_apply [OF _ _ cont_const])
   167 
   168 text \<open>Least upper bounds preserve continuity\<close>
   169 
   170 lemma cont2cont_lub [simp]:
   171   assumes chain: "\<And>x. chain (\<lambda>i. F i x)" and cont: "\<And>i. cont (\<lambda>x. F i x)"
   172   shows "cont (\<lambda>x. \<Squnion>i. F i x)"
   173 apply (rule contI2)
   174 apply (simp add: monofunI cont2monofunE [OF cont] lub_mono chain)
   175 apply (simp add: cont2contlubE [OF cont])
   176 apply (simp add: diag_lub ch2ch_cont [OF cont] chain)
   177 done
   178 
   179 text \<open>if-then-else is continuous\<close>
   180 
   181 lemma cont_if [simp, cont2cont]:
   182   "\<lbrakk>cont f; cont g\<rbrakk> \<Longrightarrow> cont (\<lambda>x. if b then f x else g x)"
   183 by (induct b) simp_all
   184 
   185 subsection \<open>Finite chains and flat pcpos\<close>
   186 
   187 text \<open>Monotone functions map finite chains to finite chains.\<close>
   188 
   189 lemma monofun_finch2finch:
   190   "\<lbrakk>monofun f; finite_chain Y\<rbrakk> \<Longrightarrow> finite_chain (\<lambda>n. f (Y n))"
   191 apply (unfold finite_chain_def)
   192 apply (simp add: ch2ch_monofun)
   193 apply (force simp add: max_in_chain_def)
   194 done
   195 
   196 text \<open>The same holds for continuous functions.\<close>
   197 
   198 lemma cont_finch2finch:
   199   "\<lbrakk>cont f; finite_chain Y\<rbrakk> \<Longrightarrow> finite_chain (\<lambda>n. f (Y n))"
   200 by (rule cont2mono [THEN monofun_finch2finch])
   201 
   202 text \<open>All monotone functions with chain-finite domain are continuous.\<close>
   203 
   204 lemma chfindom_monofun2cont: "monofun f \<Longrightarrow> cont (f::'a::chfin \<Rightarrow> 'b::cpo)"
   205 apply (erule contI2)
   206 apply (frule chfin2finch)
   207 apply (clarsimp simp add: finite_chain_def)
   208 apply (subgoal_tac "max_in_chain i (\<lambda>i. f (Y i))")
   209 apply (simp add: maxinch_is_thelub ch2ch_monofun)
   210 apply (force simp add: max_in_chain_def)
   211 done
   212 
   213 text \<open>All strict functions with flat domain are continuous.\<close>
   214 
   215 lemma flatdom_strict2mono: "f \<bottom> = \<bottom> \<Longrightarrow> monofun (f::'a::flat \<Rightarrow> 'b::pcpo)"
   216 apply (rule monofunI)
   217 apply (drule ax_flat)
   218 apply auto
   219 done
   220 
   221 lemma flatdom_strict2cont: "f \<bottom> = \<bottom> \<Longrightarrow> cont (f::'a::flat \<Rightarrow> 'b::pcpo)"
   222 by (rule flatdom_strict2mono [THEN chfindom_monofun2cont])
   223 
   224 text \<open>All functions with discrete domain are continuous.\<close>
   225 
   226 lemma cont_discrete_cpo [simp, cont2cont]: "cont (f::'a::discrete_cpo \<Rightarrow> 'b::cpo)"
   227 apply (rule contI)
   228 apply (drule discrete_chain_const, clarify)
   229 apply (simp add: is_lub_const)
   230 done
   231 
   232 end