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