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