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