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