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