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