src/HOL/Partial_Function.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 57959 1bfed12a7646
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
     1 (* Title:    HOL/Partial_Function.thy
     2    Author:   Alexander Krauss, TU Muenchen
     3 *)
     4 
     5 header {* Partial Function Definitions *}
     6 
     7 theory Partial_Function
     8 imports Complete_Partial_Order Fun_Def_Base Option
     9 keywords "partial_function" :: thy_decl
    10 begin
    11 
    12 named_theorems partial_function_mono "monotonicity rules for partial function definitions"
    13 ML_file "Tools/Function/partial_function.ML"
    14 
    15 
    16 subsection {* Axiomatic setup *}
    17 
    18 text {* This techical locale constains the requirements for function
    19   definitions with ccpo fixed points. *}
    20 
    21 definition "fun_ord ord f g \<longleftrightarrow> (\<forall>x. ord (f x) (g x))"
    22 definition "fun_lub L A = (\<lambda>x. L {y. \<exists>f\<in>A. y = f x})"
    23 definition "img_ord f ord = (\<lambda>x y. ord (f x) (f y))"
    24 definition "img_lub f g Lub = (\<lambda>A. g (Lub (f ` A)))"
    25 
    26 lemma chain_fun: 
    27   assumes A: "chain (fun_ord ord) A"
    28   shows "chain ord {y. \<exists>f\<in>A. y = f a}" (is "chain ord ?C")
    29 proof (rule chainI)
    30   fix x y assume "x \<in> ?C" "y \<in> ?C"
    31   then obtain f g where fg: "f \<in> A" "g \<in> A" 
    32     and [simp]: "x = f a" "y = g a" by blast
    33   from chainD[OF A fg]
    34   show "ord x y \<or> ord y x" unfolding fun_ord_def by auto
    35 qed
    36 
    37 lemma call_mono[partial_function_mono]: "monotone (fun_ord ord) ord (\<lambda>f. f t)"
    38 by (rule monotoneI) (auto simp: fun_ord_def)
    39 
    40 lemma let_mono[partial_function_mono]:
    41   "(\<And>x. monotone orda ordb (\<lambda>f. b f x))
    42   \<Longrightarrow> monotone orda ordb (\<lambda>f. Let t (b f))"
    43 by (simp add: Let_def)
    44 
    45 lemma if_mono[partial_function_mono]: "monotone orda ordb F 
    46 \<Longrightarrow> monotone orda ordb G
    47 \<Longrightarrow> monotone orda ordb (\<lambda>f. if c then F f else G f)"
    48 unfolding monotone_def by simp
    49 
    50 definition "mk_less R = (\<lambda>x y. R x y \<and> \<not> R y x)"
    51 
    52 locale partial_function_definitions = 
    53   fixes leq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
    54   fixes lub :: "'a set \<Rightarrow> 'a"
    55   assumes leq_refl: "leq x x"
    56   assumes leq_trans: "leq x y \<Longrightarrow> leq y z \<Longrightarrow> leq x z"
    57   assumes leq_antisym: "leq x y \<Longrightarrow> leq y x \<Longrightarrow> x = y"
    58   assumes lub_upper: "chain leq A \<Longrightarrow> x \<in> A \<Longrightarrow> leq x (lub A)"
    59   assumes lub_least: "chain leq A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> leq x z) \<Longrightarrow> leq (lub A) z"
    60 
    61 lemma partial_function_lift:
    62   assumes "partial_function_definitions ord lb"
    63   shows "partial_function_definitions (fun_ord ord) (fun_lub lb)" (is "partial_function_definitions ?ordf ?lubf")
    64 proof -
    65   interpret partial_function_definitions ord lb by fact
    66 
    67   show ?thesis
    68   proof
    69     fix x show "?ordf x x"
    70       unfolding fun_ord_def by (auto simp: leq_refl)
    71   next
    72     fix x y z assume "?ordf x y" "?ordf y z"
    73     thus "?ordf x z" unfolding fun_ord_def 
    74       by (force dest: leq_trans)
    75   next
    76     fix x y assume "?ordf x y" "?ordf y x"
    77     thus "x = y" unfolding fun_ord_def
    78       by (force intro!: dest: leq_antisym)
    79   next
    80     fix A f assume f: "f \<in> A" and A: "chain ?ordf A"
    81     thus "?ordf f (?lubf A)"
    82       unfolding fun_lub_def fun_ord_def
    83       by (blast intro: lub_upper chain_fun[OF A] f)
    84   next
    85     fix A :: "('b \<Rightarrow> 'a) set" and g :: "'b \<Rightarrow> 'a"
    86     assume A: "chain ?ordf A" and g: "\<And>f. f \<in> A \<Longrightarrow> ?ordf f g"
    87     show "?ordf (?lubf A) g" unfolding fun_lub_def fun_ord_def
    88       by (blast intro: lub_least chain_fun[OF A] dest: g[unfolded fun_ord_def])
    89    qed
    90 qed
    91 
    92 lemma ccpo: assumes "partial_function_definitions ord lb"
    93   shows "class.ccpo lb ord (mk_less ord)"
    94 using assms unfolding partial_function_definitions_def mk_less_def
    95 by unfold_locales blast+
    96 
    97 lemma partial_function_image:
    98   assumes "partial_function_definitions ord Lub"
    99   assumes inj: "\<And>x y. f x = f y \<Longrightarrow> x = y"
   100   assumes inv: "\<And>x. f (g x) = x"
   101   shows "partial_function_definitions (img_ord f ord) (img_lub f g Lub)"
   102 proof -
   103   let ?iord = "img_ord f ord"
   104   let ?ilub = "img_lub f g Lub"
   105 
   106   interpret partial_function_definitions ord Lub by fact
   107   show ?thesis
   108   proof
   109     fix A x assume "chain ?iord A" "x \<in> A"
   110     then have "chain ord (f ` A)" "f x \<in> f ` A"
   111       by (auto simp: img_ord_def intro: chainI dest: chainD)
   112     thus "?iord x (?ilub A)"
   113       unfolding inv img_lub_def img_ord_def by (rule lub_upper)
   114   next
   115     fix A x assume "chain ?iord A"
   116       and 1: "\<And>z. z \<in> A \<Longrightarrow> ?iord z x"
   117     then have "chain ord (f ` A)"
   118       by (auto simp: img_ord_def intro: chainI dest: chainD)
   119     thus "?iord (?ilub A) x"
   120       unfolding inv img_lub_def img_ord_def
   121       by (rule lub_least) (auto dest: 1[unfolded img_ord_def])
   122   qed (auto simp: img_ord_def intro: leq_refl dest: leq_trans leq_antisym inj)
   123 qed
   124 
   125 context partial_function_definitions
   126 begin
   127 
   128 abbreviation "le_fun \<equiv> fun_ord leq"
   129 abbreviation "lub_fun \<equiv> fun_lub lub"
   130 abbreviation "fixp_fun \<equiv> ccpo.fixp lub_fun le_fun"
   131 abbreviation "mono_body \<equiv> monotone le_fun leq"
   132 abbreviation "admissible \<equiv> ccpo.admissible lub_fun le_fun"
   133 
   134 text {* Interpret manually, to avoid flooding everything with facts about
   135   orders *}
   136 
   137 lemma ccpo: "class.ccpo lub_fun le_fun (mk_less le_fun)"
   138 apply (rule ccpo)
   139 apply (rule partial_function_lift)
   140 apply (rule partial_function_definitions_axioms)
   141 done
   142 
   143 text {* The crucial fixed-point theorem *}
   144 
   145 lemma mono_body_fixp: 
   146   "(\<And>x. mono_body (\<lambda>f. F f x)) \<Longrightarrow> fixp_fun F = F (fixp_fun F)"
   147 by (rule ccpo.fixp_unfold[OF ccpo]) (auto simp: monotone_def fun_ord_def)
   148 
   149 text {* Version with curry/uncurry combinators, to be used by package *}
   150 
   151 lemma fixp_rule_uc:
   152   fixes F :: "'c \<Rightarrow> 'c" and
   153     U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a" and
   154     C :: "('b \<Rightarrow> 'a) \<Rightarrow> 'c"
   155   assumes mono: "\<And>x. mono_body (\<lambda>f. U (F (C f)) x)"
   156   assumes eq: "f \<equiv> C (fixp_fun (\<lambda>f. U (F (C f))))"
   157   assumes inverse: "\<And>f. C (U f) = f"
   158   shows "f = F f"
   159 proof -
   160   have "f = C (fixp_fun (\<lambda>f. U (F (C f))))" by (simp add: eq)
   161   also have "... = C (U (F (C (fixp_fun (\<lambda>f. U (F (C f)))))))"
   162     by (subst mono_body_fixp[of "%f. U (F (C f))", OF mono]) (rule refl)
   163   also have "... = F (C (fixp_fun (\<lambda>f. U (F (C f)))))" by (rule inverse)
   164   also have "... = F f" by (simp add: eq)
   165   finally show "f = F f" .
   166 qed
   167 
   168 text {* Fixpoint induction rule *}
   169 
   170 lemma fixp_induct_uc:
   171   fixes F :: "'c \<Rightarrow> 'c" and
   172     U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a" and
   173     C :: "('b \<Rightarrow> 'a) \<Rightarrow> 'c" and
   174     P :: "('b \<Rightarrow> 'a) \<Rightarrow> bool"
   175   assumes mono: "\<And>x. mono_body (\<lambda>f. U (F (C f)) x)"
   176   assumes eq: "f \<equiv> C (fixp_fun (\<lambda>f. U (F (C f))))"
   177   assumes inverse: "\<And>f. U (C f) = f"
   178   assumes adm: "ccpo.admissible lub_fun le_fun P"
   179   and bot: "P (\<lambda>_. lub {})"
   180   assumes step: "\<And>f. P (U f) \<Longrightarrow> P (U (F f))"
   181   shows "P (U f)"
   182 unfolding eq inverse
   183 apply (rule ccpo.fixp_induct[OF ccpo adm])
   184 apply (insert mono, auto simp: monotone_def fun_ord_def bot fun_lub_def)[2]
   185 by (rule_tac f="C x" in step, simp add: inverse)
   186 
   187 
   188 text {* Rules for @{term mono_body}: *}
   189 
   190 lemma const_mono[partial_function_mono]: "monotone ord leq (\<lambda>f. c)"
   191 by (rule monotoneI) (rule leq_refl)
   192 
   193 end
   194 
   195 
   196 subsection {* Flat interpretation: tailrec and option *}
   197 
   198 definition 
   199   "flat_ord b x y \<longleftrightarrow> x = b \<or> x = y"
   200 
   201 definition 
   202   "flat_lub b A = (if A \<subseteq> {b} then b else (THE x. x \<in> A - {b}))"
   203 
   204 lemma flat_interpretation:
   205   "partial_function_definitions (flat_ord b) (flat_lub b)"
   206 proof
   207   fix A x assume 1: "chain (flat_ord b) A" "x \<in> A"
   208   show "flat_ord b x (flat_lub b A)"
   209   proof cases
   210     assume "x = b"
   211     thus ?thesis by (simp add: flat_ord_def)
   212   next
   213     assume "x \<noteq> b"
   214     with 1 have "A - {b} = {x}"
   215       by (auto elim: chainE simp: flat_ord_def)
   216     then have "flat_lub b A = x"
   217       by (auto simp: flat_lub_def)
   218     thus ?thesis by (auto simp: flat_ord_def)
   219   qed
   220 next
   221   fix A z assume A: "chain (flat_ord b) A"
   222     and z: "\<And>x. x \<in> A \<Longrightarrow> flat_ord b x z"
   223   show "flat_ord b (flat_lub b A) z"
   224   proof cases
   225     assume "A \<subseteq> {b}"
   226     thus ?thesis
   227       by (auto simp: flat_lub_def flat_ord_def)
   228   next
   229     assume nb: "\<not> A \<subseteq> {b}"
   230     then obtain y where y: "y \<in> A" "y \<noteq> b" by auto
   231     with A have "A - {b} = {y}"
   232       by (auto elim: chainE simp: flat_ord_def)
   233     with nb have "flat_lub b A = y"
   234       by (auto simp: flat_lub_def)
   235     with z y show ?thesis by auto    
   236   qed
   237 qed (auto simp: flat_ord_def)
   238 
   239 interpretation tailrec!:
   240   partial_function_definitions "flat_ord undefined" "flat_lub undefined"
   241   where "flat_lub undefined {} \<equiv> undefined"
   242 by (rule flat_interpretation)(simp add: flat_lub_def)
   243 
   244 interpretation option!:
   245   partial_function_definitions "flat_ord None" "flat_lub None"
   246   where "flat_lub None {} \<equiv> None"
   247 by (rule flat_interpretation)(simp add: flat_lub_def)
   248 
   249 
   250 abbreviation "tailrec_ord \<equiv> flat_ord undefined"
   251 abbreviation "mono_tailrec \<equiv> monotone (fun_ord tailrec_ord) tailrec_ord"
   252 
   253 lemma tailrec_admissible:
   254   "ccpo.admissible (fun_lub (flat_lub c)) (fun_ord (flat_ord c))
   255      (\<lambda>a. \<forall>x. a x \<noteq> c \<longrightarrow> P x (a x))"
   256 proof(intro ccpo.admissibleI strip)
   257   fix A x
   258   assume chain: "Complete_Partial_Order.chain (fun_ord (flat_ord c)) A"
   259     and P [rule_format]: "\<forall>f\<in>A. \<forall>x. f x \<noteq> c \<longrightarrow> P x (f x)"
   260     and defined: "fun_lub (flat_lub c) A x \<noteq> c"
   261   from defined obtain f where f: "f \<in> A" "f x \<noteq> c"
   262     by(auto simp add: fun_lub_def flat_lub_def split: split_if_asm)
   263   hence "P x (f x)" by(rule P)
   264   moreover from chain f have "\<forall>f' \<in> A. f' x = c \<or> f' x = f x"
   265     by(auto 4 4 simp add: Complete_Partial_Order.chain_def flat_ord_def fun_ord_def)
   266   hence "fun_lub (flat_lub c) A x = f x"
   267     using f by(auto simp add: fun_lub_def flat_lub_def)
   268   ultimately show "P x (fun_lub (flat_lub c) A x)" by simp
   269 qed
   270 
   271 lemma fixp_induct_tailrec:
   272   fixes F :: "'c \<Rightarrow> 'c" and
   273     U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a" and
   274     C :: "('b \<Rightarrow> 'a) \<Rightarrow> 'c" and
   275     P :: "'b \<Rightarrow> 'a \<Rightarrow> bool" and
   276     x :: "'b"
   277   assumes mono: "\<And>x. monotone (fun_ord (flat_ord c)) (flat_ord c) (\<lambda>f. U (F (C f)) x)"
   278   assumes eq: "f \<equiv> C (ccpo.fixp (fun_lub (flat_lub c)) (fun_ord (flat_ord c)) (\<lambda>f. U (F (C f))))"
   279   assumes inverse2: "\<And>f. U (C f) = f"
   280   assumes step: "\<And>f x y. (\<And>x y. U f x = y \<Longrightarrow> y \<noteq> c \<Longrightarrow> P x y) \<Longrightarrow> U (F f) x = y \<Longrightarrow> y \<noteq> c \<Longrightarrow> P x y"
   281   assumes result: "U f x = y"
   282   assumes defined: "y \<noteq> c"
   283   shows "P x y"
   284 proof -
   285   have "\<forall>x y. U f x = y \<longrightarrow> y \<noteq> c \<longrightarrow> P x y"
   286     by(rule partial_function_definitions.fixp_induct_uc[OF flat_interpretation, of _ U F C, OF mono eq inverse2])
   287       (auto intro: step tailrec_admissible simp add: fun_lub_def flat_lub_def)
   288   thus ?thesis using result defined by blast
   289 qed
   290 
   291 lemma admissible_image:
   292   assumes pfun: "partial_function_definitions le lub"
   293   assumes adm: "ccpo.admissible lub le (P o g)"
   294   assumes inj: "\<And>x y. f x = f y \<Longrightarrow> x = y"
   295   assumes inv: "\<And>x. f (g x) = x"
   296   shows "ccpo.admissible (img_lub f g lub) (img_ord f le) P"
   297 proof (rule ccpo.admissibleI)
   298   fix A assume "chain (img_ord f le) A"
   299   then have ch': "chain le (f ` A)"
   300     by (auto simp: img_ord_def intro: chainI dest: chainD)
   301   assume "A \<noteq> {}"
   302   assume P_A: "\<forall>x\<in>A. P x"
   303   have "(P o g) (lub (f ` A))" using adm ch'
   304   proof (rule ccpo.admissibleD)
   305     fix x assume "x \<in> f ` A"
   306     with P_A show "(P o g) x" by (auto simp: inj[OF inv])
   307   qed(simp add: `A \<noteq> {}`)
   308   thus "P (img_lub f g lub A)" unfolding img_lub_def by simp
   309 qed
   310 
   311 lemma admissible_fun:
   312   assumes pfun: "partial_function_definitions le lub"
   313   assumes adm: "\<And>x. ccpo.admissible lub le (Q x)"
   314   shows "ccpo.admissible  (fun_lub lub) (fun_ord le) (\<lambda>f. \<forall>x. Q x (f x))"
   315 proof (rule ccpo.admissibleI)
   316   fix A :: "('b \<Rightarrow> 'a) set"
   317   assume Q: "\<forall>f\<in>A. \<forall>x. Q x (f x)"
   318   assume ch: "chain (fun_ord le) A"
   319   assume "A \<noteq> {}"
   320   hence non_empty: "\<And>a. {y. \<exists>f\<in>A. y = f a} \<noteq> {}" by auto
   321   show "\<forall>x. Q x (fun_lub lub A x)"
   322     unfolding fun_lub_def
   323     by (rule allI, rule ccpo.admissibleD[OF adm chain_fun[OF ch] non_empty])
   324       (auto simp: Q)
   325 qed
   326 
   327 
   328 abbreviation "option_ord \<equiv> flat_ord None"
   329 abbreviation "mono_option \<equiv> monotone (fun_ord option_ord) option_ord"
   330 
   331 lemma bind_mono[partial_function_mono]:
   332 assumes mf: "mono_option B" and mg: "\<And>y. mono_option (\<lambda>f. C y f)"
   333 shows "mono_option (\<lambda>f. Option.bind (B f) (\<lambda>y. C y f))"
   334 proof (rule monotoneI)
   335   fix f g :: "'a \<Rightarrow> 'b option" assume fg: "fun_ord option_ord f g"
   336   with mf
   337   have "option_ord (B f) (B g)" by (rule monotoneD[of _ _ _ f g])
   338   then have "option_ord (Option.bind (B f) (\<lambda>y. C y f)) (Option.bind (B g) (\<lambda>y. C y f))"
   339     unfolding flat_ord_def by auto    
   340   also from mg
   341   have "\<And>y'. option_ord (C y' f) (C y' g)"
   342     by (rule monotoneD) (rule fg)
   343   then have "option_ord (Option.bind (B g) (\<lambda>y'. C y' f)) (Option.bind (B g) (\<lambda>y'. C y' g))"
   344     unfolding flat_ord_def by (cases "B g") auto
   345   finally (option.leq_trans)
   346   show "option_ord (Option.bind (B f) (\<lambda>y. C y f)) (Option.bind (B g) (\<lambda>y'. C y' g))" .
   347 qed
   348 
   349 lemma flat_lub_in_chain:
   350   assumes ch: "chain (flat_ord b) A "
   351   assumes lub: "flat_lub b A = a"
   352   shows "a = b \<or> a \<in> A"
   353 proof (cases "A \<subseteq> {b}")
   354   case True
   355   then have "flat_lub b A = b" unfolding flat_lub_def by simp
   356   with lub show ?thesis by simp
   357 next
   358   case False
   359   then obtain c where "c \<in> A" and "c \<noteq> b" by auto
   360   { fix z assume "z \<in> A"
   361     from chainD[OF ch `c \<in> A` this] have "z = c \<or> z = b"
   362       unfolding flat_ord_def using `c \<noteq> b` by auto }
   363   with False have "A - {b} = {c}" by auto
   364   with False have "flat_lub b A = c" by (auto simp: flat_lub_def)
   365   with `c \<in> A` lub show ?thesis by simp
   366 qed
   367 
   368 lemma option_admissible: "option.admissible (%(f::'a \<Rightarrow> 'b option).
   369   (\<forall>x y. f x = Some y \<longrightarrow> P x y))"
   370 proof (rule ccpo.admissibleI)
   371   fix A :: "('a \<Rightarrow> 'b option) set"
   372   assume ch: "chain option.le_fun A"
   373     and IH: "\<forall>f\<in>A. \<forall>x y. f x = Some y \<longrightarrow> P x y"
   374   from ch have ch': "\<And>x. chain option_ord {y. \<exists>f\<in>A. y = f x}" by (rule chain_fun)
   375   show "\<forall>x y. option.lub_fun A x = Some y \<longrightarrow> P x y"
   376   proof (intro allI impI)
   377     fix x y assume "option.lub_fun A x = Some y"
   378     from flat_lub_in_chain[OF ch' this[unfolded fun_lub_def]]
   379     have "Some y \<in> {y. \<exists>f\<in>A. y = f x}" by simp
   380     then have "\<exists>f\<in>A. f x = Some y" by auto
   381     with IH show "P x y" by auto
   382   qed
   383 qed
   384 
   385 lemma fixp_induct_option:
   386   fixes F :: "'c \<Rightarrow> 'c" and
   387     U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a option" and
   388     C :: "('b \<Rightarrow> 'a option) \<Rightarrow> 'c" and
   389     P :: "'b \<Rightarrow> 'a \<Rightarrow> bool"
   390   assumes mono: "\<And>x. mono_option (\<lambda>f. U (F (C f)) x)"
   391   assumes eq: "f \<equiv> C (ccpo.fixp (fun_lub (flat_lub None)) (fun_ord option_ord) (\<lambda>f. U (F (C f))))"
   392   assumes inverse2: "\<And>f. U (C f) = f"
   393   assumes step: "\<And>f x y. (\<And>x y. U f x = Some y \<Longrightarrow> P x y) \<Longrightarrow> U (F f) x = Some y \<Longrightarrow> P x y"
   394   assumes defined: "U f x = Some y"
   395   shows "P x y"
   396   using step defined option.fixp_induct_uc[of U F C, OF mono eq inverse2 option_admissible]
   397   unfolding fun_lub_def flat_lub_def by(auto 9 2)
   398 
   399 declaration {* Partial_Function.init "tailrec" @{term tailrec.fixp_fun}
   400   @{term tailrec.mono_body} @{thm tailrec.fixp_rule_uc} @{thm tailrec.fixp_induct_uc}
   401   (SOME @{thm fixp_induct_tailrec[where c=undefined]}) *}
   402 
   403 declaration {* Partial_Function.init "option" @{term option.fixp_fun}
   404   @{term option.mono_body} @{thm option.fixp_rule_uc} @{thm option.fixp_induct_uc}
   405   (SOME @{thm fixp_induct_option}) *}
   406 
   407 hide_const (open) chain
   408 
   409 end