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