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