src/HOL/Fun_Def.thy
author wenzelm
Thu Mar 14 16:55:06 2019 +0100 (5 weeks ago)
changeset 69913 ca515cf61651
parent 69605 a96320074298
permissions -rw-r--r--
more specific keyword kinds;
blanchet@55085
     1
(*  Title:      HOL/Fun_Def.thy
wenzelm@20324
     2
    Author:     Alexander Krauss, TU Muenchen
wenzelm@22816
     3
*)
wenzelm@20324
     4
wenzelm@60758
     5
section \<open>Function Definitions and Termination Proofs\<close>
wenzelm@20324
     6
blanchet@55085
     7
theory Fun_Def
wenzelm@63654
     8
  imports Basic_BNF_LFPs Partial_Function SAT
wenzelm@63654
     9
  keywords
wenzelm@69913
    10
    "function" "termination" :: thy_goal_defn and
wenzelm@69913
    11
    "fun" "fun_cases" :: thy_defn
krauss@19564
    12
begin
krauss@19564
    13
wenzelm@60758
    14
subsection \<open>Definitions with default value\<close>
krauss@20536
    15
wenzelm@63654
    16
definition THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a"
wenzelm@63654
    17
  where "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
krauss@20536
    18
krauss@20536
    19
lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
wenzelm@22816
    20
  by (simp add: theI' THE_default_def)
krauss@20536
    21
wenzelm@63654
    22
lemma THE_default1_equality: "\<exists>!x. P x \<Longrightarrow> P a \<Longrightarrow> THE_default d P = a"
wenzelm@22816
    23
  by (simp add: the1_equality THE_default_def)
krauss@20536
    24
wenzelm@63654
    25
lemma THE_default_none: "\<not> (\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
wenzelm@63654
    26
  by (simp add: THE_default_def)
krauss@20536
    27
krauss@20536
    28
krauss@19564
    29
lemma fundef_ex1_existence:
wenzelm@63654
    30
  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    31
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    32
  shows "G x (f x)"
wenzelm@22816
    33
  apply (simp only: f_def)
wenzelm@22816
    34
  apply (rule THE_defaultI')
wenzelm@22816
    35
  apply (rule ex1)
wenzelm@22816
    36
  done
krauss@21051
    37
krauss@19564
    38
lemma fundef_ex1_uniqueness:
wenzelm@63654
    39
  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    40
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    41
  assumes elm: "G x (h x)"
wenzelm@22816
    42
  shows "h x = f x"
wenzelm@22816
    43
  apply (simp only: f_def)
wenzelm@22816
    44
  apply (rule THE_default1_equality [symmetric])
wenzelm@22816
    45
   apply (rule ex1)
wenzelm@22816
    46
  apply (rule elm)
wenzelm@22816
    47
  done
krauss@19564
    48
krauss@19564
    49
lemma fundef_ex1_iff:
wenzelm@63654
    50
  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    51
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    52
  shows "(G x y) = (f x = y)"
krauss@20536
    53
  apply (auto simp:ex1 f_def THE_default1_equality)
wenzelm@22816
    54
  apply (rule THE_defaultI')
wenzelm@22816
    55
  apply (rule ex1)
wenzelm@22816
    56
  done
krauss@19564
    57
krauss@20654
    58
lemma fundef_default_value:
wenzelm@63654
    59
  assumes f_def: "f \<equiv> (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    60
  assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
wenzelm@22816
    61
  assumes "\<not> D x"
wenzelm@22816
    62
  shows "f x = d x"
krauss@20654
    63
proof -
krauss@21051
    64
  have "\<not>(\<exists>y. G x y)"
krauss@20654
    65
  proof
krauss@21512
    66
    assume "\<exists>y. G x y"
wenzelm@63654
    67
    then have "D x" using graph ..
wenzelm@60758
    68
    with \<open>\<not> D x\<close> show False ..
krauss@20654
    69
  qed
wenzelm@63654
    70
  then have "\<not>(\<exists>!y. G x y)" by blast
wenzelm@63654
    71
  then show ?thesis
wenzelm@63654
    72
    unfolding f_def by (rule THE_default_none)
krauss@20654
    73
qed
krauss@20654
    74
wenzelm@63654
    75
definition in_rel_def[simp]: "in_rel R x y \<equiv> (x, y) \<in> R"
berghofe@23739
    76
wenzelm@63654
    77
lemma wf_in_rel: "wf R \<Longrightarrow> wfP (in_rel R)"
berghofe@23739
    78
  by (simp add: wfP_def)
berghofe@23739
    79
wenzelm@69605
    80
ML_file \<open>Tools/Function/function_core.ML\<close>
wenzelm@69605
    81
ML_file \<open>Tools/Function/mutual.ML\<close>
wenzelm@69605
    82
ML_file \<open>Tools/Function/pattern_split.ML\<close>
wenzelm@69605
    83
ML_file \<open>Tools/Function/relation.ML\<close>
wenzelm@69605
    84
ML_file \<open>Tools/Function/function_elims.ML\<close>
wenzelm@47701
    85
wenzelm@60758
    86
method_setup relation = \<open>
wenzelm@47701
    87
  Args.term >> (fn t => fn ctxt => SIMPLE_METHOD' (Function_Relation.relation_infer_tac ctxt t))
wenzelm@60758
    88
\<close> "prove termination using a user-specified wellfounded relation"
wenzelm@47701
    89
wenzelm@69605
    90
ML_file \<open>Tools/Function/function.ML\<close>
wenzelm@69605
    91
ML_file \<open>Tools/Function/pat_completeness.ML\<close>
wenzelm@47432
    92
wenzelm@60758
    93
method_setup pat_completeness = \<open>
wenzelm@47432
    94
  Scan.succeed (SIMPLE_METHOD' o Pat_Completeness.pat_completeness_tac)
wenzelm@60758
    95
\<close> "prove completeness of (co)datatype patterns"
wenzelm@47432
    96
wenzelm@69605
    97
ML_file \<open>Tools/Function/fun.ML\<close>
wenzelm@69605
    98
ML_file \<open>Tools/Function/induction_schema.ML\<close>
krauss@19564
    99
wenzelm@60758
   100
method_setup induction_schema = \<open>
wenzelm@61841
   101
  Scan.succeed (Method.CONTEXT_TACTIC oo Induction_Schema.induction_schema_tac)
wenzelm@60758
   102
\<close> "prove an induction principle"
wenzelm@47432
   103
blanchet@56643
   104
wenzelm@60758
   105
subsection \<open>Measure functions\<close>
krauss@29125
   106
krauss@29125
   107
inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
wenzelm@63654
   108
  where is_measure_trivial: "is_measure f"
krauss@29125
   109
wenzelm@57959
   110
named_theorems measure_function "rules that guide the heuristic generation of measure functions"
wenzelm@69605
   111
ML_file \<open>Tools/Function/measure_functions.ML\<close>
krauss@29125
   112
krauss@29125
   113
lemma measure_size[measure_function]: "is_measure size"
wenzelm@63654
   114
  by (rule is_measure_trivial)
krauss@29125
   115
krauss@29125
   116
lemma measure_fst[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
wenzelm@63654
   117
  by (rule is_measure_trivial)
wenzelm@63654
   118
krauss@29125
   119
lemma measure_snd[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
wenzelm@63654
   120
  by (rule is_measure_trivial)
krauss@29125
   121
wenzelm@69605
   122
ML_file \<open>Tools/Function/lexicographic_order.ML\<close>
wenzelm@47432
   123
wenzelm@60758
   124
method_setup lexicographic_order = \<open>
wenzelm@47432
   125
  Method.sections clasimp_modifiers >>
wenzelm@47432
   126
  (K (SIMPLE_METHOD o Lexicographic_Order.lexicographic_order_tac false))
wenzelm@60758
   127
\<close> "termination prover for lexicographic orderings"
wenzelm@47432
   128
krauss@29125
   129
wenzelm@60758
   130
subsection \<open>Congruence rules\<close>
krauss@29125
   131
wenzelm@63654
   132
lemma let_cong [fundef_cong]: "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
wenzelm@22816
   133
  unfolding Let_def by blast
krauss@22622
   134
wenzelm@22816
   135
lemmas [fundef_cong] =
lars@67487
   136
  if_cong image_cong
blanchet@55466
   137
  bex_cong ball_cong imp_cong map_option_cong Option.bind_cong
krauss@19564
   138
wenzelm@22816
   139
lemma split_cong [fundef_cong]:
wenzelm@63654
   140
  "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q \<Longrightarrow> case_prod f p = case_prod g q"
wenzelm@22816
   141
  by (auto simp: split_def)
krauss@19934
   142
wenzelm@63654
   143
lemma comp_cong [fundef_cong]: "f (g x) = f' (g' x') \<Longrightarrow> (f \<circ> g) x = (f' \<circ> g') x'"
wenzelm@63654
   144
  by (simp only: o_apply)
krauss@19934
   145
blanchet@56643
   146
wenzelm@60758
   147
subsection \<open>Simp rules for termination proofs\<close>
krauss@26875
   148
blanchet@56643
   149
declare
blanchet@56643
   150
  trans_less_add1[termination_simp]
blanchet@56643
   151
  trans_less_add2[termination_simp]
blanchet@56643
   152
  trans_le_add1[termination_simp]
blanchet@56643
   153
  trans_le_add2[termination_simp]
blanchet@56643
   154
  less_imp_le_nat[termination_simp]
blanchet@56643
   155
  le_imp_less_Suc[termination_simp]
krauss@26875
   156
wenzelm@63654
   157
lemma size_prod_simp[termination_simp]: "size_prod f g p = f (fst p) + g (snd p) + Suc 0"
wenzelm@63654
   158
  by (induct p) auto
krauss@26875
   159
blanchet@56643
   160
wenzelm@60758
   161
subsection \<open>Decomposition\<close>
krauss@29125
   162
wenzelm@63654
   163
lemma less_by_empty: "A = {} \<Longrightarrow> A \<subseteq> B"
wenzelm@63654
   164
  and union_comp_emptyL: "A O C = {} \<Longrightarrow> B O C = {} \<Longrightarrow> (A \<union> B) O C = {}"
wenzelm@63654
   165
  and union_comp_emptyR: "A O B = {} \<Longrightarrow> A O C = {} \<Longrightarrow> A O (B \<union> C) = {}"
wenzelm@63654
   166
  and wf_no_loop: "R O R = {} \<Longrightarrow> wf R"
wenzelm@63654
   167
  by (auto simp add: wf_comp_self [of R])
krauss@29125
   168
krauss@29125
   169
wenzelm@60758
   170
subsection \<open>Reduction pairs\<close>
krauss@29125
   171
wenzelm@63654
   172
definition "reduction_pair P \<longleftrightarrow> wf (fst P) \<and> fst P O snd P \<subseteq> fst P"
krauss@29125
   173
krauss@32235
   174
lemma reduction_pairI[intro]: "wf R \<Longrightarrow> R O S \<subseteq> R \<Longrightarrow> reduction_pair (R, S)"
wenzelm@63654
   175
  by (auto simp: reduction_pair_def)
krauss@29125
   176
krauss@29125
   177
lemma reduction_pair_lemma:
krauss@29125
   178
  assumes rp: "reduction_pair P"
krauss@29125
   179
  assumes "R \<subseteq> fst P"
krauss@29125
   180
  assumes "S \<subseteq> snd P"
krauss@29125
   181
  assumes "wf S"
krauss@29125
   182
  shows "wf (R \<union> S)"
krauss@29125
   183
proof -
wenzelm@60758
   184
  from rp \<open>S \<subseteq> snd P\<close> have "wf (fst P)" "fst P O S \<subseteq> fst P"
krauss@29125
   185
    unfolding reduction_pair_def by auto
wenzelm@60758
   186
  with \<open>wf S\<close> have "wf (fst P \<union> S)"
krauss@29125
   187
    by (auto intro: wf_union_compatible)
wenzelm@60758
   188
  moreover from \<open>R \<subseteq> fst P\<close> have "R \<union> S \<subseteq> fst P \<union> S" by auto
wenzelm@47701
   189
  ultimately show ?thesis by (rule wf_subset)
krauss@29125
   190
qed
krauss@29125
   191
wenzelm@63654
   192
definition "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
krauss@29125
   193
wenzelm@63654
   194
lemma rp_inv_image_rp: "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
wenzelm@63654
   195
  unfolding reduction_pair_def rp_inv_image_def split_def by force
krauss@29125
   196
krauss@29125
   197
wenzelm@60758
   198
subsection \<open>Concrete orders for SCNP termination proofs\<close>
krauss@29125
   199
krauss@29125
   200
definition "pair_less = less_than <*lex*> less_than"
wenzelm@67613
   201
definition "pair_leq = pair_less\<^sup>="
krauss@29125
   202
definition "max_strict = max_ext pair_less"
haftmann@37767
   203
definition "max_weak = max_ext pair_leq \<union> {({}, {})}"
haftmann@37767
   204
definition "min_strict = min_ext pair_less"
haftmann@37767
   205
definition "min_weak = min_ext pair_leq \<union> {({}, {})}"
krauss@29125
   206
krauss@29125
   207
lemma wf_pair_less[simp]: "wf pair_less"
krauss@29125
   208
  by (auto simp: pair_less_def)
krauss@29125
   209
wenzelm@61799
   210
text \<open>Introduction rules for \<open>pair_less\<close>/\<open>pair_leq\<close>\<close>
krauss@29125
   211
lemma pair_leqI1: "a < b \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
krauss@29125
   212
  and pair_leqI2: "a \<le> b \<Longrightarrow> s \<le> t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
krauss@29125
   213
  and pair_lessI1: "a < b  \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
krauss@29125
   214
  and pair_lessI2: "a \<le> b \<Longrightarrow> s < t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
wenzelm@63654
   215
  by (auto simp: pair_leq_def pair_less_def)
krauss@29125
   216
wenzelm@60758
   217
text \<open>Introduction rules for max\<close>
wenzelm@63654
   218
lemma smax_emptyI: "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict"
wenzelm@47701
   219
  and smax_insertI:
wenzelm@63654
   220
    "y \<in> Y \<Longrightarrow> (x, y) \<in> pair_less \<Longrightarrow> (X, Y) \<in> max_strict \<Longrightarrow> (insert x X, Y) \<in> max_strict"
wenzelm@63654
   221
  and wmax_emptyI: "finite X \<Longrightarrow> ({}, X) \<in> max_weak"
krauss@29125
   222
  and wmax_insertI:
wenzelm@63654
   223
    "y \<in> YS \<Longrightarrow> (x, y) \<in> pair_leq \<Longrightarrow> (XS, YS) \<in> max_weak \<Longrightarrow> (insert x XS, YS) \<in> max_weak"
wenzelm@63654
   224
  by (auto simp: max_strict_def max_weak_def elim!: max_ext.cases)
krauss@29125
   225
wenzelm@60758
   226
text \<open>Introduction rules for min\<close>
wenzelm@63654
   227
lemma smin_emptyI: "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict"
wenzelm@47701
   228
  and smin_insertI:
wenzelm@63654
   229
    "x \<in> XS \<Longrightarrow> (x, y) \<in> pair_less \<Longrightarrow> (XS, YS) \<in> min_strict \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
wenzelm@63654
   230
  and wmin_emptyI: "(X, {}) \<in> min_weak"
wenzelm@47701
   231
  and wmin_insertI:
wenzelm@63654
   232
    "x \<in> XS \<Longrightarrow> (x, y) \<in> pair_leq \<Longrightarrow> (XS, YS) \<in> min_weak \<Longrightarrow> (XS, insert y YS) \<in> min_weak"
wenzelm@63654
   233
  by (auto simp: min_strict_def min_weak_def min_ext_def)
krauss@29125
   234
wenzelm@63654
   235
text \<open>Reduction Pairs.\<close>
krauss@29125
   236
wenzelm@47701
   237
lemma max_ext_compat:
krauss@32235
   238
  assumes "R O S \<subseteq> R"
wenzelm@63654
   239
  shows "max_ext R O (max_ext S \<union> {({}, {})}) \<subseteq> max_ext R"
wenzelm@63654
   240
  using assms
wenzelm@63654
   241
  apply auto
wenzelm@63654
   242
  apply (elim max_ext.cases)
wenzelm@63654
   243
  apply rule
wenzelm@63654
   244
     apply auto[3]
wenzelm@63654
   245
  apply (drule_tac x=xa in meta_spec)
wenzelm@63654
   246
  apply simp
wenzelm@63654
   247
  apply (erule bexE)
wenzelm@63654
   248
  apply (drule_tac x=xb in meta_spec)
wenzelm@63654
   249
  apply auto
wenzelm@63654
   250
  done
krauss@29125
   251
krauss@29125
   252
lemma max_rpair_set: "reduction_pair (max_strict, max_weak)"
wenzelm@47701
   253
  unfolding max_strict_def max_weak_def
wenzelm@63654
   254
  apply (intro reduction_pairI max_ext_wf)
wenzelm@63654
   255
   apply simp
wenzelm@63654
   256
  apply (rule max_ext_compat)
wenzelm@63654
   257
  apply (auto simp: pair_less_def pair_leq_def)
wenzelm@63654
   258
  done
krauss@29125
   259
wenzelm@47701
   260
lemma min_ext_compat:
krauss@32235
   261
  assumes "R O S \<subseteq> R"
krauss@32235
   262
  shows "min_ext R O  (min_ext S \<union> {({},{})}) \<subseteq> min_ext R"
wenzelm@63654
   263
  using assms
wenzelm@63654
   264
  apply (auto simp: min_ext_def)
wenzelm@63654
   265
  apply (drule_tac x=ya in bspec, assumption)
wenzelm@63654
   266
  apply (erule bexE)
wenzelm@63654
   267
  apply (drule_tac x=xc in bspec)
wenzelm@63654
   268
   apply assumption
wenzelm@63654
   269
  apply auto
wenzelm@63654
   270
  done
krauss@29125
   271
krauss@29125
   272
lemma min_rpair_set: "reduction_pair (min_strict, min_weak)"
wenzelm@47701
   273
  unfolding min_strict_def min_weak_def
wenzelm@63654
   274
  apply (intro reduction_pairI min_ext_wf)
wenzelm@63654
   275
   apply simp
wenzelm@63654
   276
  apply (rule min_ext_compat)
wenzelm@63654
   277
  apply (auto simp: pair_less_def pair_leq_def)
wenzelm@63654
   278
  done
krauss@29125
   279
krauss@29125
   280
haftmann@64591
   281
subsection \<open>Yet another induction principle on the natural numbers\<close>
haftmann@64591
   282
haftmann@64591
   283
lemma nat_descend_induct [case_names base descend]:
haftmann@64591
   284
  fixes P :: "nat \<Rightarrow> bool"
haftmann@64591
   285
  assumes H1: "\<And>k. k > n \<Longrightarrow> P k"
haftmann@64591
   286
  assumes H2: "\<And>k. k \<le> n \<Longrightarrow> (\<And>i. i > k \<Longrightarrow> P i) \<Longrightarrow> P k"
haftmann@64591
   287
  shows "P m"
haftmann@64591
   288
  using assms by induction_schema (force intro!: wf_measure [of "\<lambda>k. Suc n - k"])+
haftmann@64591
   289
haftmann@64591
   290
wenzelm@60758
   291
subsection \<open>Tool setup\<close>
krauss@29125
   292
wenzelm@69605
   293
ML_file \<open>Tools/Function/termination.ML\<close>
wenzelm@69605
   294
ML_file \<open>Tools/Function/scnp_solve.ML\<close>
wenzelm@69605
   295
ML_file \<open>Tools/Function/scnp_reconstruct.ML\<close>
wenzelm@69605
   296
ML_file \<open>Tools/Function/fun_cases.ML\<close>
krauss@29125
   297
wenzelm@67443
   298
ML_val \<comment> \<open>setup inactive\<close>
wenzelm@60758
   299
\<open>
krauss@36521
   300
  Context.theory_map (Function_Common.set_termination_prover
blanchet@60682
   301
    (K (ScnpReconstruct.decomp_scnp_tac [ScnpSolve.MAX, ScnpSolve.MIN, ScnpSolve.MS])))
wenzelm@60758
   302
\<close>
krauss@26875
   303
krauss@19564
   304
end