src/HOL/Partial_Function.thy
author blanchet
Fri, 27 May 2011 10:30:08 +0200
changeset 43016 42330f25142c
parent 42949 618adb3584e5
child 43080 73a1d6a7ef1d
permissions -rw-r--r--
renamed "try" "try_methods"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     1
(* Title:    HOL/Partial_Function.thy
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     2
   Author:   Alexander Krauss, TU Muenchen
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     3
*)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     4
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     5
header {* Partial Function Definitions *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     6
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     7
theory Partial_Function
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     8
imports Complete_Partial_Order Option
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
     9
uses 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    10
  "Tools/Function/function_lib.ML" 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    11
  "Tools/Function/partial_function.ML" 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    12
begin
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    13
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    14
setup Partial_Function.setup
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    15
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    16
subsection {* Axiomatic setup *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    17
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    18
text {* This techical locale constains the requirements for function
42949
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
    19
  definitions with ccpo fixed points. *}
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    20
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    21
definition "fun_ord ord f g \<longleftrightarrow> (\<forall>x. ord (f x) (g x))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    22
definition "fun_lub L A = (\<lambda>x. L {y. \<exists>f\<in>A. y = f x})"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    23
definition "img_ord f ord = (\<lambda>x y. ord (f x) (f y))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    24
definition "img_lub f g Lub = (\<lambda>A. g (Lub (f ` A)))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    25
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    26
lemma call_mono[partial_function_mono]: "monotone (fun_ord ord) ord (\<lambda>f. f t)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    27
by (rule monotoneI) (auto simp: fun_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    28
40288
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    29
lemma let_mono[partial_function_mono]:
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    30
  "(\<And>x. monotone orda ordb (\<lambda>f. b f x))
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    31
  \<Longrightarrow> monotone orda ordb (\<lambda>f. Let t (b f))"
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    32
by (simp add: Let_def)
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    33
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    34
lemma if_mono[partial_function_mono]: "monotone orda ordb F 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    35
\<Longrightarrow> monotone orda ordb G
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    36
\<Longrightarrow> monotone orda ordb (\<lambda>f. if c then F f else G f)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    37
unfolding monotone_def by simp
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    38
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    39
definition "mk_less R = (\<lambda>x y. R x y \<and> \<not> R y x)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    40
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    41
locale partial_function_definitions = 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    42
  fixes leq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    43
  fixes lub :: "'a set \<Rightarrow> 'a"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    44
  assumes leq_refl: "leq x x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    45
  assumes leq_trans: "leq x y \<Longrightarrow> leq y z \<Longrightarrow> leq x z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    46
  assumes leq_antisym: "leq x y \<Longrightarrow> leq y x \<Longrightarrow> x = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    47
  assumes lub_upper: "chain leq A \<Longrightarrow> x \<in> A \<Longrightarrow> leq x (lub A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    48
  assumes lub_least: "chain leq A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> leq x z) \<Longrightarrow> leq (lub A) z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    49
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    50
lemma partial_function_lift:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    51
  assumes "partial_function_definitions ord lb"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    52
  shows "partial_function_definitions (fun_ord ord) (fun_lub lb)" (is "partial_function_definitions ?ordf ?lubf")
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    53
proof -
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    54
  interpret partial_function_definitions ord lb by fact
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    55
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    56
  { fix A a assume A: "chain ?ordf A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    57
    have "chain ord {y. \<exists>f\<in>A. y = f a}" (is "chain ord ?C")
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    58
    proof (rule chainI)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    59
      fix x y assume "x \<in> ?C" "y \<in> ?C"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    60
      then obtain f g where fg: "f \<in> A" "g \<in> A" 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    61
        and [simp]: "x = f a" "y = g a" by blast
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    62
      from chainD[OF A fg]
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    63
      show "ord x y \<or> ord y x" unfolding fun_ord_def by auto
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    64
    qed }
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    65
  note chain_fun = this
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    66
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    67
  show ?thesis
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    68
  proof
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    69
    fix x show "?ordf x x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    70
      unfolding fun_ord_def by (auto simp: leq_refl)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    71
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    72
    fix x y z assume "?ordf x y" "?ordf y z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    73
    thus "?ordf x z" unfolding fun_ord_def 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    74
      by (force dest: leq_trans)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    75
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    76
    fix x y assume "?ordf x y" "?ordf y x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    77
    thus "x = y" unfolding fun_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    78
      by (force intro!: ext dest: leq_antisym)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    79
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    80
    fix A f assume f: "f \<in> A" and A: "chain ?ordf A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    81
    thus "?ordf f (?lubf A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    82
      unfolding fun_lub_def fun_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    83
      by (blast intro: lub_upper chain_fun[OF A] f)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    84
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    85
    fix A :: "('b \<Rightarrow> 'a) set" and g :: "'b \<Rightarrow> 'a"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    86
    assume A: "chain ?ordf A" and g: "\<And>f. f \<in> A \<Longrightarrow> ?ordf f g"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    87
    show "?ordf (?lubf A) g" unfolding fun_lub_def fun_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    88
      by (blast intro: lub_least chain_fun[OF A] dest: g[unfolded fun_ord_def])
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    89
   qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    90
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    91
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    92
lemma ccpo: assumes "partial_function_definitions ord lb"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    93
  shows "class.ccpo ord (mk_less ord) lb"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    94
using assms unfolding partial_function_definitions_def mk_less_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    95
by unfold_locales blast+
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    96
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    97
lemma partial_function_image:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    98
  assumes "partial_function_definitions ord Lub"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    99
  assumes inj: "\<And>x y. f x = f y \<Longrightarrow> x = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   100
  assumes inv: "\<And>x. f (g x) = x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   101
  shows "partial_function_definitions (img_ord f ord) (img_lub f g Lub)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   102
proof -
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   103
  let ?iord = "img_ord f ord"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   104
  let ?ilub = "img_lub f g Lub"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   105
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   106
  interpret partial_function_definitions ord Lub by fact
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   107
  show ?thesis
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   108
  proof
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   109
    fix A x assume "chain ?iord A" "x \<in> A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   110
    then have "chain ord (f ` A)" "f x \<in> f ` A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   111
      by (auto simp: img_ord_def intro: chainI dest: chainD)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   112
    thus "?iord x (?ilub A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   113
      unfolding inv img_lub_def img_ord_def by (rule lub_upper)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   114
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   115
    fix A x assume "chain ?iord A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   116
      and 1: "\<And>z. z \<in> A \<Longrightarrow> ?iord z x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   117
    then have "chain ord (f ` A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   118
      by (auto simp: img_ord_def intro: chainI dest: chainD)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   119
    thus "?iord (?ilub A) x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   120
      unfolding inv img_lub_def img_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   121
      by (rule lub_least) (auto dest: 1[unfolded img_ord_def])
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   122
  qed (auto simp: img_ord_def intro: leq_refl dest: leq_trans leq_antisym inj)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   123
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   124
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   125
context partial_function_definitions
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   126
begin
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   127
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   128
abbreviation "le_fun \<equiv> fun_ord leq"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   129
abbreviation "lub_fun \<equiv> fun_lub lub"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   130
abbreviation "fixp_fun == ccpo.fixp le_fun lub_fun"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   131
abbreviation "mono_body \<equiv> monotone le_fun leq"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   132
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   133
text {* Interpret manually, to avoid flooding everything with facts about
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   134
  orders *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   135
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   136
lemma ccpo: "class.ccpo le_fun (mk_less le_fun) lub_fun"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   137
apply (rule ccpo)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   138
apply (rule partial_function_lift)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   139
apply (rule partial_function_definitions_axioms)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   140
done
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   141
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   142
text {* The crucial fixed-point theorem *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   143
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   144
lemma mono_body_fixp: 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   145
  "(\<And>x. mono_body (\<lambda>f. F f x)) \<Longrightarrow> fixp_fun F = F (fixp_fun F)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   146
by (rule ccpo.fixp_unfold[OF ccpo]) (auto simp: monotone_def fun_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   147
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   148
text {* Version with curry/uncurry combinators, to be used by package *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   149
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   150
lemma fixp_rule_uc:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   151
  fixes F :: "'c \<Rightarrow> 'c" and
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   152
    U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a" and
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   153
    C :: "('b \<Rightarrow> 'a) \<Rightarrow> 'c"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   154
  assumes mono: "\<And>x. mono_body (\<lambda>f. U (F (C f)) x)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   155
  assumes eq: "f \<equiv> C (fixp_fun (\<lambda>f. U (F (C f))))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   156
  assumes inverse: "\<And>f. C (U f) = f"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   157
  shows "f = F f"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   158
proof -
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   159
  have "f = C (fixp_fun (\<lambda>f. U (F (C f))))" by (simp add: eq)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   160
  also have "... = C (U (F (C (fixp_fun (\<lambda>f. U (F (C f)))))))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   161
    by (subst mono_body_fixp[of "%f. U (F (C f))", OF mono]) (rule refl)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   162
  also have "... = F (C (fixp_fun (\<lambda>f. U (F (C f)))))" by (rule inverse)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   163
  also have "... = F f" by (simp add: eq)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   164
  finally show "f = F f" .
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   165
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   166
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   167
text {* Rules for @{term mono_body}: *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   168
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   169
lemma const_mono[partial_function_mono]: "monotone ord leq (\<lambda>f. c)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   170
by (rule monotoneI) (rule leq_refl)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   171
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   172
end
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   173
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   174
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   175
subsection {* Flat interpretation: tailrec and option *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   176
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   177
definition 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   178
  "flat_ord b x y \<longleftrightarrow> x = b \<or> x = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   179
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   180
definition 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   181
  "flat_lub b A = (if A \<subseteq> {b} then b else (THE x. x \<in> A - {b}))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   182
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   183
lemma flat_interpretation:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   184
  "partial_function_definitions (flat_ord b) (flat_lub b)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   185
proof
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   186
  fix A x assume 1: "chain (flat_ord b) A" "x \<in> A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   187
  show "flat_ord b x (flat_lub b A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   188
  proof cases
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   189
    assume "x = b"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   190
    thus ?thesis by (simp add: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   191
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   192
    assume "x \<noteq> b"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   193
    with 1 have "A - {b} = {x}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   194
      by (auto elim: chainE simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   195
    then have "flat_lub b A = x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   196
      by (auto simp: flat_lub_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   197
    thus ?thesis by (auto simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   198
  qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   199
next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   200
  fix A z assume A: "chain (flat_ord b) A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   201
    and z: "\<And>x. x \<in> A \<Longrightarrow> flat_ord b x z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   202
  show "flat_ord b (flat_lub b A) z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   203
  proof cases
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   204
    assume "A \<subseteq> {b}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   205
    thus ?thesis
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   206
      by (auto simp: flat_lub_def flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   207
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   208
    assume nb: "\<not> A \<subseteq> {b}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   209
    then obtain y where y: "y \<in> A" "y \<noteq> b" by auto
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   210
    with A have "A - {b} = {y}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   211
      by (auto elim: chainE simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   212
    with nb have "flat_lub b A = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   213
      by (auto simp: flat_lub_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   214
    with z y show ?thesis by auto    
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   215
  qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   216
qed (auto simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   217
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   218
interpretation tailrec!:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   219
  partial_function_definitions "flat_ord undefined" "flat_lub undefined"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   220
by (rule flat_interpretation)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   221
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   222
interpretation option!:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   223
  partial_function_definitions "flat_ord None" "flat_lub None"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   224
by (rule flat_interpretation)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   225
42949
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   226
declaration {* Partial_Function.init "tailrec" @{term tailrec.fixp_fun}
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   227
  @{term tailrec.mono_body} @{thm tailrec.fixp_rule_uc} *}
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   228
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   229
declaration {* Partial_Function.init "option" @{term option.fixp_fun}
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   230
  @{term option.mono_body} @{thm option.fixp_rule_uc} *}
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   231
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   232
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   233
abbreviation "option_ord \<equiv> flat_ord None"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   234
abbreviation "mono_option \<equiv> monotone (fun_ord option_ord) option_ord"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   235
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   236
lemma bind_mono[partial_function_mono]:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   237
assumes mf: "mono_option B" and mg: "\<And>y. mono_option (\<lambda>f. C y f)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   238
shows "mono_option (\<lambda>f. Option.bind (B f) (\<lambda>y. C y f))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   239
proof (rule monotoneI)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   240
  fix f g :: "'a \<Rightarrow> 'b option" assume fg: "fun_ord option_ord f g"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   241
  with mf
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   242
  have "option_ord (B f) (B g)" by (rule monotoneD[of _ _ _ f g])
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   243
  then have "option_ord (Option.bind (B f) (\<lambda>y. C y f)) (Option.bind (B g) (\<lambda>y. C y f))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   244
    unfolding flat_ord_def by auto    
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   245
  also from mg
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   246
  have "\<And>y'. option_ord (C y' f) (C y' g)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   247
    by (rule monotoneD) (rule fg)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   248
  then have "option_ord (Option.bind (B g) (\<lambda>y'. C y' f)) (Option.bind (B g) (\<lambda>y'. C y' g))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   249
    unfolding flat_ord_def by (cases "B g") auto
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   250
  finally (option.leq_trans)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   251
  show "option_ord (Option.bind (B f) (\<lambda>y. C y f)) (Option.bind (B g) (\<lambda>y'. C y' g))" .
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   252
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   253
40252
029400b6c893 hide_const various constants, in particular to avoid ugly qualifiers in HOLCF
krauss
parents: 40107
diff changeset
   254
hide_const (open) chain
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   255
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   256
end
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   257