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