src/HOL/Partial_Function.thy
author kuncar
Wed, 18 Apr 2012 17:04:03 +0200
changeset 47545 a2850a16e30f
parent 46950 d0181abdbdac
child 48891 c0eafbd55de3
permissions -rw-r--r--
Lifting: generate more thms & note them & tuned
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
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46041
diff changeset
     9
keywords "partial_function" :: thy_decl
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    10
uses 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    11
  "Tools/Function/function_lib.ML" 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    12
  "Tools/Function/partial_function.ML" 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    13
begin
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    14
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    15
setup Partial_Function.setup
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    16
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    17
subsection {* Axiomatic setup *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    18
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    19
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
    20
  definitions with ccpo fixed points. *}
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    21
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    22
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
    23
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
    24
definition "img_ord f ord = (\<lambda>x y. ord (f x) (f y))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    25
definition "img_lub f g Lub = (\<lambda>A. g (Lub (f ` A)))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    26
43081
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    27
lemma chain_fun: 
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    28
  assumes A: "chain (fun_ord ord) A"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    29
  shows "chain ord {y. \<exists>f\<in>A. y = f a}" (is "chain ord ?C")
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    30
proof (rule chainI)
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    31
  fix x y assume "x \<in> ?C" "y \<in> ?C"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    32
  then obtain f g where fg: "f \<in> A" "g \<in> A" 
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    33
    and [simp]: "x = f a" "y = g a" by blast
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    34
  from chainD[OF A fg]
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    35
  show "ord x y \<or> ord y x" unfolding fun_ord_def by auto
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    36
qed
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    37
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    38
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
    39
by (rule monotoneI) (auto simp: fun_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    40
40288
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    41
lemma let_mono[partial_function_mono]:
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    42
  "(\<And>x. monotone orda ordb (\<lambda>f. b f x))
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    43
  \<Longrightarrow> monotone orda ordb (\<lambda>f. Let t (b f))"
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    44
by (simp add: Let_def)
520199d8b66e added rule let_mono
krauss
parents: 40252
diff changeset
    45
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    46
lemma if_mono[partial_function_mono]: "monotone orda ordb F 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    47
\<Longrightarrow> monotone orda ordb G
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    48
\<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
    49
unfolding monotone_def by simp
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    50
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    51
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
    52
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    53
locale partial_function_definitions = 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    54
  fixes leq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    55
  fixes lub :: "'a set \<Rightarrow> 'a"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    56
  assumes leq_refl: "leq x x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    57
  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
    58
  assumes leq_antisym: "leq x y \<Longrightarrow> leq y x \<Longrightarrow> x = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    59
  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
    60
  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
    61
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    62
lemma partial_function_lift:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    63
  assumes "partial_function_definitions ord lb"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    64
  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
    65
proof -
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    66
  interpret partial_function_definitions ord lb by fact
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    67
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    68
  show ?thesis
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    69
  proof
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    70
    fix x show "?ordf x x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    71
      unfolding fun_ord_def by (auto simp: leq_refl)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    72
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    73
    fix x y z assume "?ordf x y" "?ordf y z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    74
    thus "?ordf x z" unfolding fun_ord_def 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    75
      by (force dest: leq_trans)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    76
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    77
    fix x y assume "?ordf x y" "?ordf y x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    78
    thus "x = y" unfolding fun_ord_def
43081
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
    79
      by (force intro!: dest: leq_antisym)
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    80
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    81
    fix A f assume f: "f \<in> A" and A: "chain ?ordf A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    82
    thus "?ordf f (?lubf A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    83
      unfolding fun_lub_def fun_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    84
      by (blast intro: lub_upper chain_fun[OF A] f)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    85
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    86
    fix A :: "('b \<Rightarrow> 'a) set" and g :: "'b \<Rightarrow> 'a"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    87
    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
    88
    show "?ordf (?lubf A) g" unfolding fun_lub_def fun_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    89
      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
    90
   qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    91
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    92
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    93
lemma ccpo: assumes "partial_function_definitions ord lb"
46041
1e3ff542e83e remove constant 'ccpo.lub', re-use constant 'Sup' instead
huffman
parents: 45297
diff changeset
    94
  shows "class.ccpo lb ord (mk_less ord)"
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    95
using assms unfolding partial_function_definitions_def mk_less_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    96
by unfold_locales blast+
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    97
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    98
lemma partial_function_image:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
    99
  assumes "partial_function_definitions ord Lub"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   100
  assumes inj: "\<And>x y. f x = f y \<Longrightarrow> x = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   101
  assumes inv: "\<And>x. f (g x) = x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   102
  shows "partial_function_definitions (img_ord f ord) (img_lub f g Lub)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   103
proof -
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   104
  let ?iord = "img_ord f ord"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   105
  let ?ilub = "img_lub f g Lub"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   106
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   107
  interpret partial_function_definitions ord Lub by fact
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   108
  show ?thesis
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   109
  proof
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   110
    fix A x assume "chain ?iord A" "x \<in> A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   111
    then have "chain ord (f ` A)" "f x \<in> f ` A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   112
      by (auto simp: img_ord_def intro: chainI dest: chainD)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   113
    thus "?iord x (?ilub A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   114
      unfolding inv img_lub_def img_ord_def by (rule lub_upper)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   115
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   116
    fix A x assume "chain ?iord A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   117
      and 1: "\<And>z. z \<in> A \<Longrightarrow> ?iord z x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   118
    then have "chain ord (f ` A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   119
      by (auto simp: img_ord_def intro: chainI dest: chainD)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   120
    thus "?iord (?ilub A) x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   121
      unfolding inv img_lub_def img_ord_def
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   122
      by (rule lub_least) (auto dest: 1[unfolded img_ord_def])
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   123
  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
   124
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   125
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   126
context partial_function_definitions
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   127
begin
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   128
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   129
abbreviation "le_fun \<equiv> fun_ord leq"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   130
abbreviation "lub_fun \<equiv> fun_lub lub"
46041
1e3ff542e83e remove constant 'ccpo.lub', re-use constant 'Sup' instead
huffman
parents: 45297
diff changeset
   131
abbreviation "fixp_fun \<equiv> ccpo.fixp lub_fun le_fun"
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   132
abbreviation "mono_body \<equiv> monotone le_fun leq"
46041
1e3ff542e83e remove constant 'ccpo.lub', re-use constant 'Sup' instead
huffman
parents: 45297
diff changeset
   133
abbreviation "admissible \<equiv> ccpo.admissible lub_fun le_fun"
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   134
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   135
text {* Interpret manually, to avoid flooding everything with facts about
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   136
  orders *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   137
46041
1e3ff542e83e remove constant 'ccpo.lub', re-use constant 'Sup' instead
huffman
parents: 45297
diff changeset
   138
lemma ccpo: "class.ccpo lub_fun le_fun (mk_less le_fun)"
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   139
apply (rule ccpo)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   140
apply (rule partial_function_lift)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   141
apply (rule partial_function_definitions_axioms)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   142
done
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   143
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   144
text {* The crucial fixed-point theorem *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   145
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   146
lemma mono_body_fixp: 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   147
  "(\<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
   148
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
   149
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   150
text {* Version with curry/uncurry combinators, to be used by package *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   151
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   152
lemma fixp_rule_uc:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   153
  fixes F :: "'c \<Rightarrow> 'c" and
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   154
    U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a" and
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   155
    C :: "('b \<Rightarrow> 'a) \<Rightarrow> 'c"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   156
  assumes mono: "\<And>x. mono_body (\<lambda>f. U (F (C f)) x)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   157
  assumes eq: "f \<equiv> C (fixp_fun (\<lambda>f. U (F (C f))))"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   158
  assumes inverse: "\<And>f. C (U f) = f"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   159
  shows "f = F f"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   160
proof -
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   161
  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
   162
  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
   163
    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
   164
  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
   165
  also have "... = F f" by (simp add: eq)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   166
  finally show "f = F f" .
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   167
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   168
43082
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   169
text {* Fixpoint induction rule *}
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   170
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   171
lemma fixp_induct_uc:
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   172
  fixes F :: "'c \<Rightarrow> 'c" and
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   173
    U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a" and
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   174
    C :: "('b \<Rightarrow> 'a) \<Rightarrow> 'c" and
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   175
    P :: "('b \<Rightarrow> 'a) \<Rightarrow> bool"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   176
  assumes mono: "\<And>x. mono_body (\<lambda>f. U (F (C f)) x)"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   177
  assumes eq: "f \<equiv> C (fixp_fun (\<lambda>f. U (F (C f))))"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   178
  assumes inverse: "\<And>f. U (C f) = f"
46041
1e3ff542e83e remove constant 'ccpo.lub', re-use constant 'Sup' instead
huffman
parents: 45297
diff changeset
   179
  assumes adm: "ccpo.admissible lub_fun le_fun P"
43082
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   180
  assumes step: "\<And>f. P (U f) \<Longrightarrow> P (U (F f))"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   181
  shows "P (U f)"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   182
unfolding eq inverse
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   183
apply (rule ccpo.fixp_induct[OF ccpo adm])
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   184
apply (insert mono, auto simp: monotone_def fun_ord_def)[1]
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   185
by (rule_tac f="C x" in step, simp add: inverse)
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   186
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   187
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   188
text {* Rules for @{term mono_body}: *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   189
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   190
lemma const_mono[partial_function_mono]: "monotone ord leq (\<lambda>f. c)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   191
by (rule monotoneI) (rule leq_refl)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   192
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   193
end
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   194
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   195
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   196
subsection {* Flat interpretation: tailrec and option *}
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   197
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   198
definition 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   199
  "flat_ord b x y \<longleftrightarrow> x = b \<or> x = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   200
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   201
definition 
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   202
  "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
   203
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   204
lemma flat_interpretation:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   205
  "partial_function_definitions (flat_ord b) (flat_lub b)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   206
proof
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   207
  fix A x assume 1: "chain (flat_ord b) A" "x \<in> A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   208
  show "flat_ord b x (flat_lub b A)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   209
  proof cases
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   210
    assume "x = b"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   211
    thus ?thesis by (simp add: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   212
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   213
    assume "x \<noteq> b"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   214
    with 1 have "A - {b} = {x}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   215
      by (auto elim: chainE simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   216
    then have "flat_lub b A = x"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   217
      by (auto simp: flat_lub_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   218
    thus ?thesis by (auto simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   219
  qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   220
next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   221
  fix A z assume A: "chain (flat_ord b) A"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   222
    and z: "\<And>x. x \<in> A \<Longrightarrow> flat_ord b x z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   223
  show "flat_ord b (flat_lub b A) z"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   224
  proof cases
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   225
    assume "A \<subseteq> {b}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   226
    thus ?thesis
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   227
      by (auto simp: flat_lub_def flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   228
  next
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   229
    assume nb: "\<not> A \<subseteq> {b}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   230
    then obtain y where y: "y \<in> A" "y \<noteq> b" by auto
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   231
    with A have "A - {b} = {y}"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   232
      by (auto elim: chainE simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   233
    with nb have "flat_lub b A = y"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   234
      by (auto simp: flat_lub_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   235
    with z y show ?thesis by auto    
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   236
  qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   237
qed (auto simp: flat_ord_def)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   238
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   239
interpretation tailrec!:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   240
  partial_function_definitions "flat_ord undefined" "flat_lub undefined"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   241
by (rule flat_interpretation)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   242
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   243
interpretation option!:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   244
  partial_function_definitions "flat_ord None" "flat_lub None"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   245
by (rule flat_interpretation)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   246
42949
618adb3584e5 separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
krauss
parents: 40288
diff changeset
   247
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   248
abbreviation "option_ord \<equiv> flat_ord None"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   249
abbreviation "mono_option \<equiv> monotone (fun_ord option_ord) option_ord"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   250
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   251
lemma bind_mono[partial_function_mono]:
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   252
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
   253
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
   254
proof (rule monotoneI)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   255
  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
   256
  with mf
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   257
  have "option_ord (B f) (B g)" by (rule monotoneD[of _ _ _ f g])
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   258
  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
   259
    unfolding flat_ord_def by auto    
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   260
  also from mg
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   261
  have "\<And>y'. option_ord (C y' f) (C y' g)"
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   262
    by (rule monotoneD) (rule fg)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   263
  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
   264
    unfolding flat_ord_def by (cases "B g") auto
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   265
  finally (option.leq_trans)
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   266
  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
   267
qed
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   268
43081
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   269
lemma flat_lub_in_chain:
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   270
  assumes ch: "chain (flat_ord b) A "
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   271
  assumes lub: "flat_lub b A = a"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   272
  shows "a = b \<or> a \<in> A"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   273
proof (cases "A \<subseteq> {b}")
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   274
  case True
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   275
  then have "flat_lub b A = b" unfolding flat_lub_def by simp
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   276
  with lub show ?thesis by simp
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   277
next
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   278
  case False
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   279
  then obtain c where "c \<in> A" and "c \<noteq> b" by auto
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   280
  { fix z assume "z \<in> A"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   281
    from chainD[OF ch `c \<in> A` this] have "z = c \<or> z = b"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   282
      unfolding flat_ord_def using `c \<noteq> b` by auto }
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   283
  with False have "A - {b} = {c}" by auto
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   284
  with False have "flat_lub b A = c" by (auto simp: flat_lub_def)
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   285
  with `c \<in> A` lub show ?thesis by simp
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   286
qed
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   287
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   288
lemma option_admissible: "option.admissible (%(f::'a \<Rightarrow> 'b option).
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   289
  (\<forall>x y. f x = Some y \<longrightarrow> P x y))"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   290
proof (rule ccpo.admissibleI[OF option.ccpo])
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   291
  fix A :: "('a \<Rightarrow> 'b option) set"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   292
  assume ch: "chain option.le_fun A"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   293
    and IH: "\<forall>f\<in>A. \<forall>x y. f x = Some y \<longrightarrow> P x y"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   294
  from ch have ch': "\<And>x. chain option_ord {y. \<exists>f\<in>A. y = f x}" by (rule chain_fun)
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   295
  show "\<forall>x y. option.lub_fun A x = Some y \<longrightarrow> P x y"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   296
  proof (intro allI impI)
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   297
    fix x y assume "option.lub_fun A x = Some y"
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   298
    from flat_lub_in_chain[OF ch' this[unfolded fun_lub_def]]
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   299
    have "Some y \<in> {y. \<exists>f\<in>A. y = f x}" by simp
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   300
    then have "\<exists>f\<in>A. f x = Some y" by auto
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   301
    with IH show "P x y" by auto
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   302
  qed
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   303
qed
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   304
43082
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   305
lemma fixp_induct_option:
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   306
  fixes F :: "'c \<Rightarrow> 'c" and
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   307
    U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a option" and
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   308
    C :: "('b \<Rightarrow> 'a option) \<Rightarrow> 'c" and
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   309
    P :: "'b \<Rightarrow> 'a \<Rightarrow> bool"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   310
  assumes mono: "\<And>x. mono_option (\<lambda>f. U (F (C f)) x)"
46041
1e3ff542e83e remove constant 'ccpo.lub', re-use constant 'Sup' instead
huffman
parents: 45297
diff changeset
   311
  assumes eq: "f \<equiv> C (ccpo.fixp (fun_lub (flat_lub None)) (fun_ord option_ord) (\<lambda>f. U (F (C f))))"
43082
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   312
  assumes inverse2: "\<And>f. U (C f) = f"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   313
  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"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   314
  assumes defined: "U f x = Some y"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   315
  shows "P x y"
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   316
  using step defined option.fixp_induct_uc[of U F C, OF mono eq inverse2 option_admissible]
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   317
  by blast
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   318
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   319
declaration {* Partial_Function.init "tailrec" @{term tailrec.fixp_fun}
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   320
  @{term tailrec.mono_body} @{thm tailrec.fixp_rule_uc} NONE *}
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   321
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   322
declaration {* Partial_Function.init "option" @{term option.fixp_fun}
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   323
  @{term option.mono_body} @{thm option.fixp_rule_uc} 
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   324
  (SOME @{thm fixp_induct_option}) *}
8d0c44de9773 generic fixpoint induction (with explicit curry/uncurry predicates) and instance for option type
krauss
parents: 43081
diff changeset
   325
43081
1a39c9898ae6 admissibility on option type
krauss
parents: 43080
diff changeset
   326
40252
029400b6c893 hide_const various constants, in particular to avoid ugly qualifiers in HOLCF
krauss
parents: 40107
diff changeset
   327
hide_const (open) chain
40107
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   328
374f3ef9f940 first version of partial_function package
krauss
parents:
diff changeset
   329
end