src/HOL/FunDef.thy
author haftmann
Fri Nov 27 08:41:10 2009 +0100 (2009-11-27)
changeset 33963 977b94b64905
parent 33471 5aef13872723
child 34228 bc0cea4cae52
permissions -rw-r--r--
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
     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/decompose.ML")
    28   ("Tools/Function/descent.ML")
    29   ("Tools/Function/scnp_solve.ML")
    30   ("Tools/Function/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/function_lib.ML"
   107 use "Tools/Function/function_common.ML"
   108 use "Tools/Function/context_tree.ML"
   109 use "Tools/Function/function_core.ML"
   110 use "Tools/Function/sum_tree.ML"
   111 use "Tools/Function/mutual.ML"
   112 use "Tools/Function/pattern_split.ML"
   113 use "Tools/Function/relation.ML"
   114 use "Tools/Function/function.ML"
   115 use "Tools/Function/pat_completeness.ML"
   116 use "Tools/Function/fun.ML"
   117 use "Tools/Function/induction_schema.ML"
   118 
   119 setup {* 
   120   Function.setup
   121   #> Pat_Completeness.setup
   122   #> Function_Fun.setup
   123   #> Induction_Schema.setup
   124 *}
   125 
   126 subsection {* Measure Functions *}
   127 
   128 inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
   129 where is_measure_trivial: "is_measure f"
   130 
   131 use "Tools/Function/measure_functions.ML"
   132 setup MeasureFunctions.setup
   133 
   134 lemma measure_size[measure_function]: "is_measure size"
   135 by (rule is_measure_trivial)
   136 
   137 lemma measure_fst[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
   138 by (rule is_measure_trivial)
   139 lemma measure_snd[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
   140 by (rule is_measure_trivial)
   141 
   142 use "Tools/Function/lexicographic_order.ML"
   143 setup Lexicographic_Order.setup 
   144 
   145 
   146 subsection {* Congruence Rules *}
   147 
   148 lemma let_cong [fundef_cong]:
   149   "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
   150   unfolding Let_def by blast
   151 
   152 lemmas [fundef_cong] =
   153   if_cong image_cong INT_cong UN_cong
   154   bex_cong ball_cong imp_cong
   155 
   156 lemma split_cong [fundef_cong]:
   157   "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
   158     \<Longrightarrow> split f p = split g q"
   159   by (auto simp: split_def)
   160 
   161 lemma comp_cong [fundef_cong]:
   162   "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
   163   unfolding o_apply .
   164 
   165 subsection {* Simp rules for termination proofs *}
   166 
   167 lemma termination_basic_simps[termination_simp]:
   168   "x < (y::nat) \<Longrightarrow> x < y + z" 
   169   "x < z \<Longrightarrow> x < y + z"
   170   "x \<le> y \<Longrightarrow> x \<le> y + (z::nat)"
   171   "x \<le> z \<Longrightarrow> x \<le> y + (z::nat)"
   172   "x < y \<Longrightarrow> x \<le> (y::nat)"
   173 by arith+
   174 
   175 declare le_imp_less_Suc[termination_simp]
   176 
   177 lemma prod_size_simp[termination_simp]:
   178   "prod_size f g p = f (fst p) + g (snd p) + Suc 0"
   179 by (induct p) auto
   180 
   181 subsection {* Decomposition *}
   182 
   183 lemma less_by_empty: 
   184   "A = {} \<Longrightarrow> A \<subseteq> B"
   185 and  union_comp_emptyL:
   186   "\<lbrakk> A O C = {}; B O C = {} \<rbrakk> \<Longrightarrow> (A \<union> B) O C = {}"
   187 and union_comp_emptyR:
   188   "\<lbrakk> A O B = {}; A O C = {} \<rbrakk> \<Longrightarrow> A O (B \<union> C) = {}"
   189 and wf_no_loop: 
   190   "R O R = {} \<Longrightarrow> wf R"
   191 by (auto simp add: wf_comp_self[of R])
   192 
   193 
   194 subsection {* Reduction Pairs *}
   195 
   196 definition
   197   "reduction_pair P = (wf (fst P) \<and> fst P O snd P \<subseteq> fst P)"
   198 
   199 lemma reduction_pairI[intro]: "wf R \<Longrightarrow> R O S \<subseteq> R \<Longrightarrow> reduction_pair (R, S)"
   200 unfolding reduction_pair_def by auto
   201 
   202 lemma reduction_pair_lemma:
   203   assumes rp: "reduction_pair P"
   204   assumes "R \<subseteq> fst P"
   205   assumes "S \<subseteq> snd P"
   206   assumes "wf S"
   207   shows "wf (R \<union> S)"
   208 proof -
   209   from rp `S \<subseteq> snd P` have "wf (fst P)" "fst P O S \<subseteq> fst P"
   210     unfolding reduction_pair_def by auto
   211   with `wf S` have "wf (fst P \<union> S)" 
   212     by (auto intro: wf_union_compatible)
   213   moreover from `R \<subseteq> fst P` have "R \<union> S \<subseteq> fst P \<union> S" by auto
   214   ultimately show ?thesis by (rule wf_subset) 
   215 qed
   216 
   217 definition
   218   "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
   219 
   220 lemma rp_inv_image_rp:
   221   "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
   222   unfolding reduction_pair_def rp_inv_image_def split_def
   223   by force
   224 
   225 
   226 subsection {* Concrete orders for SCNP termination proofs *}
   227 
   228 definition "pair_less = less_than <*lex*> less_than"
   229 definition [code del]: "pair_leq = pair_less^="
   230 definition "max_strict = max_ext pair_less"
   231 definition [code del]: "max_weak = max_ext pair_leq \<union> {({}, {})}"
   232 definition [code del]: "min_strict = min_ext pair_less"
   233 definition [code del]: "min_weak = min_ext pair_leq \<union> {({}, {})}"
   234 
   235 lemma wf_pair_less[simp]: "wf pair_less"
   236   by (auto simp: pair_less_def)
   237 
   238 text {* Introduction rules for @{text pair_less}/@{text pair_leq} *}
   239 lemma pair_leqI1: "a < b \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
   240   and pair_leqI2: "a \<le> b \<Longrightarrow> s \<le> t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
   241   and pair_lessI1: "a < b  \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
   242   and pair_lessI2: "a \<le> b \<Longrightarrow> s < t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
   243   unfolding pair_leq_def pair_less_def by auto
   244 
   245 text {* Introduction rules for max *}
   246 lemma smax_emptyI: 
   247   "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict" 
   248   and smax_insertI: 
   249   "\<lbrakk>y \<in> Y; (x, y) \<in> pair_less; (X, Y) \<in> max_strict\<rbrakk> \<Longrightarrow> (insert x X, Y) \<in> max_strict"
   250   and wmax_emptyI: 
   251   "finite X \<Longrightarrow> ({}, X) \<in> max_weak" 
   252   and wmax_insertI:
   253   "\<lbrakk>y \<in> YS; (x, y) \<in> pair_leq; (XS, YS) \<in> max_weak\<rbrakk> \<Longrightarrow> (insert x XS, YS) \<in> max_weak" 
   254 unfolding max_strict_def max_weak_def by (auto elim!: max_ext.cases)
   255 
   256 text {* Introduction rules for min *}
   257 lemma smin_emptyI: 
   258   "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict" 
   259   and smin_insertI: 
   260   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_less; (XS, YS) \<in> min_strict\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
   261   and wmin_emptyI: 
   262   "(X, {}) \<in> min_weak" 
   263   and wmin_insertI: 
   264   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_leq; (XS, YS) \<in> min_weak\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_weak" 
   265 by (auto simp: min_strict_def min_weak_def min_ext_def)
   266 
   267 text {* Reduction Pairs *}
   268 
   269 lemma max_ext_compat: 
   270   assumes "R O S \<subseteq> R"
   271   shows "max_ext R O (max_ext S \<union> {({},{})}) \<subseteq> max_ext R"
   272 using assms 
   273 apply auto
   274 apply (elim max_ext.cases)
   275 apply rule
   276 apply auto[3]
   277 apply (drule_tac x=xa in meta_spec)
   278 apply simp
   279 apply (erule bexE)
   280 apply (drule_tac x=xb in meta_spec)
   281 by auto
   282 
   283 lemma max_rpair_set: "reduction_pair (max_strict, max_weak)"
   284   unfolding max_strict_def max_weak_def 
   285 apply (intro reduction_pairI max_ext_wf)
   286 apply simp
   287 apply (rule max_ext_compat)
   288 by (auto simp: pair_less_def pair_leq_def)
   289 
   290 lemma min_ext_compat: 
   291   assumes "R O S \<subseteq> R"
   292   shows "min_ext R O  (min_ext S \<union> {({},{})}) \<subseteq> min_ext R"
   293 using assms 
   294 apply (auto simp: min_ext_def)
   295 apply (drule_tac x=ya in bspec, assumption)
   296 apply (erule bexE)
   297 apply (drule_tac x=xc in bspec)
   298 apply assumption
   299 by auto
   300 
   301 lemma min_rpair_set: "reduction_pair (min_strict, min_weak)"
   302   unfolding min_strict_def min_weak_def 
   303 apply (intro reduction_pairI min_ext_wf)
   304 apply simp
   305 apply (rule min_ext_compat)
   306 by (auto simp: pair_less_def pair_leq_def)
   307 
   308 
   309 subsection {* Tool setup *}
   310 
   311 use "Tools/Function/termination.ML"
   312 use "Tools/Function/decompose.ML"
   313 use "Tools/Function/descent.ML"
   314 use "Tools/Function/scnp_solve.ML"
   315 use "Tools/Function/scnp_reconstruct.ML"
   316 
   317 setup {* ScnpReconstruct.setup *}
   318 
   319 ML_val -- "setup inactive"
   320 {*
   321   Context.theory_map (Function_Common.set_termination_prover (ScnpReconstruct.decomp_scnp 
   322   [ScnpSolve.MAX, ScnpSolve.MIN, ScnpSolve.MS])) 
   323 *}
   324 
   325 end