src/HOL/HOLCF/Fix.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (20 months ago)
changeset 67003 49850a679c2c
parent 62175 8ffc4d0e652d
child 67312 0d25e02759b7
permissions -rw-r--r--
more robust sorted_entries;
     1 (*  Title:      HOL/HOLCF/Fix.thy
     2     Author:     Franz Regensburger
     3     Author:     Brian Huffman
     4 *)
     5 
     6 section \<open>Fixed point operator and admissibility\<close>
     7 
     8 theory Fix
     9 imports Cfun
    10 begin
    11 
    12 default_sort pcpo
    13 
    14 subsection \<open>Iteration\<close>
    15 
    16 primrec iterate :: "nat \<Rightarrow> ('a::cpo \<rightarrow> 'a) \<rightarrow> ('a \<rightarrow> 'a)" where
    17     "iterate 0 = (\<Lambda> F x. x)"
    18   | "iterate (Suc n) = (\<Lambda> F x. F\<cdot>(iterate n\<cdot>F\<cdot>x))"
    19 
    20 text \<open>Derive inductive properties of iterate from primitive recursion\<close>
    21 
    22 lemma iterate_0 [simp]: "iterate 0\<cdot>F\<cdot>x = x"
    23 by simp
    24 
    25 lemma iterate_Suc [simp]: "iterate (Suc n)\<cdot>F\<cdot>x = F\<cdot>(iterate n\<cdot>F\<cdot>x)"
    26 by simp
    27 
    28 declare iterate.simps [simp del]
    29 
    30 lemma iterate_Suc2: "iterate (Suc n)\<cdot>F\<cdot>x = iterate n\<cdot>F\<cdot>(F\<cdot>x)"
    31 by (induct n) simp_all
    32 
    33 lemma iterate_iterate:
    34   "iterate m\<cdot>F\<cdot>(iterate n\<cdot>F\<cdot>x) = iterate (m + n)\<cdot>F\<cdot>x"
    35 by (induct m) simp_all
    36 
    37 text \<open>The sequence of function iterations is a chain.\<close>
    38 
    39 lemma chain_iterate [simp]: "chain (\<lambda>i. iterate i\<cdot>F\<cdot>\<bottom>)"
    40 by (rule chainI, unfold iterate_Suc2, rule monofun_cfun_arg, rule minimal)
    41 
    42 
    43 subsection \<open>Least fixed point operator\<close>
    44 
    45 definition
    46   "fix" :: "('a \<rightarrow> 'a) \<rightarrow> 'a" where
    47   "fix = (\<Lambda> F. \<Squnion>i. iterate i\<cdot>F\<cdot>\<bottom>)"
    48 
    49 text \<open>Binder syntax for @{term fix}\<close>
    50 
    51 abbreviation
    52   fix_syn :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a"  (binder "\<mu> " 10) where
    53   "fix_syn (\<lambda>x. f x) \<equiv> fix\<cdot>(\<Lambda> x. f x)"
    54 
    55 notation (ASCII)
    56   fix_syn  (binder "FIX " 10)
    57 
    58 text \<open>Properties of @{term fix}\<close>
    59 
    60 text \<open>direct connection between @{term fix} and iteration\<close>
    61 
    62 lemma fix_def2: "fix\<cdot>F = (\<Squnion>i. iterate i\<cdot>F\<cdot>\<bottom>)"
    63 unfolding fix_def by simp
    64 
    65 lemma iterate_below_fix: "iterate n\<cdot>f\<cdot>\<bottom> \<sqsubseteq> fix\<cdot>f"
    66   unfolding fix_def2
    67   using chain_iterate by (rule is_ub_thelub)
    68 
    69 text \<open>
    70   Kleene's fixed point theorems for continuous functions in pointed
    71   omega cpo's
    72 \<close>
    73 
    74 lemma fix_eq: "fix\<cdot>F = F\<cdot>(fix\<cdot>F)"
    75 apply (simp add: fix_def2)
    76 apply (subst lub_range_shift [of _ 1, symmetric])
    77 apply (rule chain_iterate)
    78 apply (subst contlub_cfun_arg)
    79 apply (rule chain_iterate)
    80 apply simp
    81 done
    82 
    83 lemma fix_least_below: "F\<cdot>x \<sqsubseteq> x \<Longrightarrow> fix\<cdot>F \<sqsubseteq> x"
    84 apply (simp add: fix_def2)
    85 apply (rule lub_below)
    86 apply (rule chain_iterate)
    87 apply (induct_tac i)
    88 apply simp
    89 apply simp
    90 apply (erule rev_below_trans)
    91 apply (erule monofun_cfun_arg)
    92 done
    93 
    94 lemma fix_least: "F\<cdot>x = x \<Longrightarrow> fix\<cdot>F \<sqsubseteq> x"
    95 by (rule fix_least_below, simp)
    96 
    97 lemma fix_eqI:
    98   assumes fixed: "F\<cdot>x = x" and least: "\<And>z. F\<cdot>z = z \<Longrightarrow> x \<sqsubseteq> z"
    99   shows "fix\<cdot>F = x"
   100 apply (rule below_antisym)
   101 apply (rule fix_least [OF fixed])
   102 apply (rule least [OF fix_eq [symmetric]])
   103 done
   104 
   105 lemma fix_eq2: "f \<equiv> fix\<cdot>F \<Longrightarrow> f = F\<cdot>f"
   106 by (simp add: fix_eq [symmetric])
   107 
   108 lemma fix_eq3: "f \<equiv> fix\<cdot>F \<Longrightarrow> f\<cdot>x = F\<cdot>f\<cdot>x"
   109 by (erule fix_eq2 [THEN cfun_fun_cong])
   110 
   111 lemma fix_eq4: "f = fix\<cdot>F \<Longrightarrow> f = F\<cdot>f"
   112 apply (erule ssubst)
   113 apply (rule fix_eq)
   114 done
   115 
   116 lemma fix_eq5: "f = fix\<cdot>F \<Longrightarrow> f\<cdot>x = F\<cdot>f\<cdot>x"
   117 by (erule fix_eq4 [THEN cfun_fun_cong])
   118 
   119 text \<open>strictness of @{term fix}\<close>
   120 
   121 lemma fix_bottom_iff: "(fix\<cdot>F = \<bottom>) = (F\<cdot>\<bottom> = \<bottom>)"
   122 apply (rule iffI)
   123 apply (erule subst)
   124 apply (rule fix_eq [symmetric])
   125 apply (erule fix_least [THEN bottomI])
   126 done
   127 
   128 lemma fix_strict: "F\<cdot>\<bottom> = \<bottom> \<Longrightarrow> fix\<cdot>F = \<bottom>"
   129 by (simp add: fix_bottom_iff)
   130 
   131 lemma fix_defined: "F\<cdot>\<bottom> \<noteq> \<bottom> \<Longrightarrow> fix\<cdot>F \<noteq> \<bottom>"
   132 by (simp add: fix_bottom_iff)
   133 
   134 text \<open>@{term fix} applied to identity and constant functions\<close>
   135 
   136 lemma fix_id: "(\<mu> x. x) = \<bottom>"
   137 by (simp add: fix_strict)
   138 
   139 lemma fix_const: "(\<mu> x. c) = c"
   140 by (subst fix_eq, simp)
   141 
   142 subsection \<open>Fixed point induction\<close>
   143 
   144 lemma fix_ind: "\<lbrakk>adm P; P \<bottom>; \<And>x. P x \<Longrightarrow> P (F\<cdot>x)\<rbrakk> \<Longrightarrow> P (fix\<cdot>F)"
   145 unfolding fix_def2
   146 apply (erule admD)
   147 apply (rule chain_iterate)
   148 apply (rule nat_induct, simp_all)
   149 done
   150 
   151 lemma cont_fix_ind:
   152   "\<lbrakk>cont F; adm P; P \<bottom>; \<And>x. P x \<Longrightarrow> P (F x)\<rbrakk> \<Longrightarrow> P (fix\<cdot>(Abs_cfun F))"
   153 by (simp add: fix_ind)
   154 
   155 lemma def_fix_ind:
   156   "\<lbrakk>f \<equiv> fix\<cdot>F; adm P; P \<bottom>; \<And>x. P x \<Longrightarrow> P (F\<cdot>x)\<rbrakk> \<Longrightarrow> P f"
   157 by (simp add: fix_ind)
   158 
   159 lemma fix_ind2:
   160   assumes adm: "adm P"
   161   assumes 0: "P \<bottom>" and 1: "P (F\<cdot>\<bottom>)"
   162   assumes step: "\<And>x. \<lbrakk>P x; P (F\<cdot>x)\<rbrakk> \<Longrightarrow> P (F\<cdot>(F\<cdot>x))"
   163   shows "P (fix\<cdot>F)"
   164 unfolding fix_def2
   165 apply (rule admD [OF adm chain_iterate])
   166 apply (rule nat_less_induct)
   167 apply (case_tac n)
   168 apply (simp add: 0)
   169 apply (case_tac nat)
   170 apply (simp add: 1)
   171 apply (frule_tac x=nat in spec)
   172 apply (simp add: step)
   173 done
   174 
   175 lemma parallel_fix_ind:
   176   assumes adm: "adm (\<lambda>x. P (fst x) (snd x))"
   177   assumes base: "P \<bottom> \<bottom>"
   178   assumes step: "\<And>x y. P x y \<Longrightarrow> P (F\<cdot>x) (G\<cdot>y)"
   179   shows "P (fix\<cdot>F) (fix\<cdot>G)"
   180 proof -
   181   from adm have adm': "adm (case_prod P)"
   182     unfolding split_def .
   183   have "\<And>i. P (iterate i\<cdot>F\<cdot>\<bottom>) (iterate i\<cdot>G\<cdot>\<bottom>)"
   184     by (induct_tac i, simp add: base, simp add: step)
   185   hence "\<And>i. case_prod P (iterate i\<cdot>F\<cdot>\<bottom>, iterate i\<cdot>G\<cdot>\<bottom>)"
   186     by simp
   187   hence "case_prod P (\<Squnion>i. (iterate i\<cdot>F\<cdot>\<bottom>, iterate i\<cdot>G\<cdot>\<bottom>))"
   188     by - (rule admD [OF adm'], simp, assumption)
   189   hence "case_prod P (\<Squnion>i. iterate i\<cdot>F\<cdot>\<bottom>, \<Squnion>i. iterate i\<cdot>G\<cdot>\<bottom>)"
   190     by (simp add: lub_Pair)
   191   hence "P (\<Squnion>i. iterate i\<cdot>F\<cdot>\<bottom>) (\<Squnion>i. iterate i\<cdot>G\<cdot>\<bottom>)"
   192     by simp
   193   thus "P (fix\<cdot>F) (fix\<cdot>G)"
   194     by (simp add: fix_def2)
   195 qed
   196 
   197 lemma cont_parallel_fix_ind:
   198   assumes "cont F" and "cont G"
   199   assumes "adm (\<lambda>x. P (fst x) (snd x))"
   200   assumes "P \<bottom> \<bottom>"
   201   assumes "\<And>x y. P x y \<Longrightarrow> P (F x) (G y)"
   202   shows "P (fix\<cdot>(Abs_cfun F)) (fix\<cdot>(Abs_cfun G))"
   203 by (rule parallel_fix_ind, simp_all add: assms)
   204 
   205 subsection \<open>Fixed-points on product types\<close>
   206 
   207 text \<open>
   208   Bekic's Theorem: Simultaneous fixed points over pairs
   209   can be written in terms of separate fixed points.
   210 \<close>
   211 
   212 lemma fix_cprod:
   213   "fix\<cdot>(F::'a \<times> 'b \<rightarrow> 'a \<times> 'b) =
   214    (\<mu> x. fst (F\<cdot>(x, \<mu> y. snd (F\<cdot>(x, y)))),
   215     \<mu> y. snd (F\<cdot>(\<mu> x. fst (F\<cdot>(x, \<mu> y. snd (F\<cdot>(x, y)))), y)))"
   216   (is "fix\<cdot>F = (?x, ?y)")
   217 proof (rule fix_eqI)
   218   have 1: "fst (F\<cdot>(?x, ?y)) = ?x"
   219     by (rule trans [symmetric, OF fix_eq], simp)
   220   have 2: "snd (F\<cdot>(?x, ?y)) = ?y"
   221     by (rule trans [symmetric, OF fix_eq], simp)
   222   from 1 2 show "F\<cdot>(?x, ?y) = (?x, ?y)" by (simp add: prod_eq_iff)
   223 next
   224   fix z assume F_z: "F\<cdot>z = z"
   225   obtain x y where z: "z = (x,y)" by (rule prod.exhaust)
   226   from F_z z have F_x: "fst (F\<cdot>(x, y)) = x" by simp
   227   from F_z z have F_y: "snd (F\<cdot>(x, y)) = y" by simp
   228   let ?y1 = "\<mu> y. snd (F\<cdot>(x, y))"
   229   have "?y1 \<sqsubseteq> y" by (rule fix_least, simp add: F_y)
   230   hence "fst (F\<cdot>(x, ?y1)) \<sqsubseteq> fst (F\<cdot>(x, y))"
   231     by (simp add: fst_monofun monofun_cfun)
   232   hence "fst (F\<cdot>(x, ?y1)) \<sqsubseteq> x" using F_x by simp
   233   hence 1: "?x \<sqsubseteq> x" by (simp add: fix_least_below)
   234   hence "snd (F\<cdot>(?x, y)) \<sqsubseteq> snd (F\<cdot>(x, y))"
   235     by (simp add: snd_monofun monofun_cfun)
   236   hence "snd (F\<cdot>(?x, y)) \<sqsubseteq> y" using F_y by simp
   237   hence 2: "?y \<sqsubseteq> y" by (simp add: fix_least_below)
   238   show "(?x, ?y) \<sqsubseteq> z" using z 1 2 by simp
   239 qed
   240 
   241 end