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