src/HOL/Fun_Def.thy
 author paulson Mon May 23 15:33:24 2016 +0100 (2016-05-23) changeset 63114 27afe7af7379 parent 61841 4d3527b94f2a child 63432 ba7901e94e7b permissions -rw-r--r--
Lots of new material for multivariate analysis
```     1 (*  Title:      HOL/Fun_Def.thy
```
```     2     Author:     Alexander Krauss, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 section \<open>Function Definitions and Termination Proofs\<close>
```
```     6
```
```     7 theory Fun_Def
```
```     8 imports Basic_BNF_LFPs Partial_Function SAT
```
```     9 keywords "function" "termination" :: thy_goal and "fun" "fun_cases" :: thy_decl
```
```    10 begin
```
```    11
```
```    12 subsection \<open>Definitions with default value\<close>
```
```    13
```
```    14 definition
```
```    15   THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
```
```    16   "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
```
```    17
```
```    18 lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
```
```    19   by (simp add: theI' THE_default_def)
```
```    20
```
```    21 lemma THE_default1_equality:
```
```    22     "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
```
```    23   by (simp add: the1_equality THE_default_def)
```
```    24
```
```    25 lemma THE_default_none:
```
```    26     "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
```
```    27   by (simp add:THE_default_def)
```
```    28
```
```    29
```
```    30 lemma fundef_ex1_existence:
```
```    31   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
```
```    32   assumes ex1: "\<exists>!y. G x y"
```
```    33   shows "G x (f x)"
```
```    34   apply (simp only: f_def)
```
```    35   apply (rule THE_defaultI')
```
```    36   apply (rule ex1)
```
```    37   done
```
```    38
```
```    39 lemma fundef_ex1_uniqueness:
```
```    40   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
```
```    41   assumes ex1: "\<exists>!y. G x y"
```
```    42   assumes elm: "G x (h x)"
```
```    43   shows "h x = f x"
```
```    44   apply (simp only: f_def)
```
```    45   apply (rule THE_default1_equality [symmetric])
```
```    46    apply (rule ex1)
```
```    47   apply (rule elm)
```
```    48   done
```
```    49
```
```    50 lemma fundef_ex1_iff:
```
```    51   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
```
```    52   assumes ex1: "\<exists>!y. G x y"
```
```    53   shows "(G x y) = (f x = y)"
```
```    54   apply (auto simp:ex1 f_def THE_default1_equality)
```
```    55   apply (rule THE_defaultI')
```
```    56   apply (rule ex1)
```
```    57   done
```
```    58
```
```    59 lemma fundef_default_value:
```
```    60   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
```
```    61   assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
```
```    62   assumes "\<not> D x"
```
```    63   shows "f x = d x"
```
```    64 proof -
```
```    65   have "\<not>(\<exists>y. G x y)"
```
```    66   proof
```
```    67     assume "\<exists>y. G x y"
```
```    68     hence "D x" using graph ..
```
```    69     with \<open>\<not> D x\<close> show False ..
```
```    70   qed
```
```    71   hence "\<not>(\<exists>!y. G x y)" by blast
```
```    72
```
```    73   thus ?thesis
```
```    74     unfolding f_def
```
```    75     by (rule THE_default_none)
```
```    76 qed
```
```    77
```
```    78 definition in_rel_def[simp]:
```
```    79   "in_rel R x y == (x, y) \<in> R"
```
```    80
```
```    81 lemma wf_in_rel:
```
```    82   "wf R \<Longrightarrow> wfP (in_rel R)"
```
```    83   by (simp add: wfP_def)
```
```    84
```
```    85 ML_file "Tools/Function/function_core.ML"
```
```    86 ML_file "Tools/Function/mutual.ML"
```
```    87 ML_file "Tools/Function/pattern_split.ML"
```
```    88 ML_file "Tools/Function/relation.ML"
```
```    89 ML_file "Tools/Function/function_elims.ML"
```
```    90
```
```    91 method_setup relation = \<open>
```
```    92   Args.term >> (fn t => fn ctxt => SIMPLE_METHOD' (Function_Relation.relation_infer_tac ctxt t))
```
```    93 \<close> "prove termination using a user-specified wellfounded relation"
```
```    94
```
```    95 ML_file "Tools/Function/function.ML"
```
```    96 ML_file "Tools/Function/pat_completeness.ML"
```
```    97
```
```    98 method_setup pat_completeness = \<open>
```
```    99   Scan.succeed (SIMPLE_METHOD' o Pat_Completeness.pat_completeness_tac)
```
```   100 \<close> "prove completeness of (co)datatype patterns"
```
```   101
```
```   102 ML_file "Tools/Function/fun.ML"
```
```   103 ML_file "Tools/Function/induction_schema.ML"
```
```   104
```
```   105 method_setup induction_schema = \<open>
```
```   106   Scan.succeed (Method.CONTEXT_TACTIC oo Induction_Schema.induction_schema_tac)
```
```   107 \<close> "prove an induction principle"
```
```   108
```
```   109
```
```   110 subsection \<open>Measure functions\<close>
```
```   111
```
```   112 inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
```
```   113 where is_measure_trivial: "is_measure f"
```
```   114
```
```   115 named_theorems measure_function "rules that guide the heuristic generation of measure functions"
```
```   116 ML_file "Tools/Function/measure_functions.ML"
```
```   117
```
```   118 lemma measure_size[measure_function]: "is_measure size"
```
```   119 by (rule is_measure_trivial)
```
```   120
```
```   121 lemma measure_fst[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
```
```   122 by (rule is_measure_trivial)
```
```   123 lemma measure_snd[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
```
```   124 by (rule is_measure_trivial)
```
```   125
```
```   126 ML_file "Tools/Function/lexicographic_order.ML"
```
```   127
```
```   128 method_setup lexicographic_order = \<open>
```
```   129   Method.sections clasimp_modifiers >>
```
```   130   (K (SIMPLE_METHOD o Lexicographic_Order.lexicographic_order_tac false))
```
```   131 \<close> "termination prover for lexicographic orderings"
```
```   132
```
```   133
```
```   134 subsection \<open>Congruence rules\<close>
```
```   135
```
```   136 lemma let_cong [fundef_cong]:
```
```   137   "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
```
```   138   unfolding Let_def by blast
```
```   139
```
```   140 lemmas [fundef_cong] =
```
```   141   if_cong image_cong INF_cong SUP_cong
```
```   142   bex_cong ball_cong imp_cong map_option_cong Option.bind_cong
```
```   143
```
```   144 lemma split_cong [fundef_cong]:
```
```   145   "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
```
```   146     \<Longrightarrow> case_prod f p = case_prod g q"
```
```   147   by (auto simp: split_def)
```
```   148
```
```   149 lemma comp_cong [fundef_cong]:
```
```   150   "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
```
```   151   unfolding o_apply .
```
```   152
```
```   153
```
```   154 subsection \<open>Simp rules for termination proofs\<close>
```
```   155
```
```   156 declare
```
```   157   trans_less_add1[termination_simp]
```
```   158   trans_less_add2[termination_simp]
```
```   159   trans_le_add1[termination_simp]
```
```   160   trans_le_add2[termination_simp]
```
```   161   less_imp_le_nat[termination_simp]
```
```   162   le_imp_less_Suc[termination_simp]
```
```   163
```
```   164 lemma size_prod_simp[termination_simp]:
```
```   165   "size_prod f g p = f (fst p) + g (snd p) + Suc 0"
```
```   166 by (induct p) auto
```
```   167
```
```   168
```
```   169 subsection \<open>Decomposition\<close>
```
```   170
```
```   171 lemma less_by_empty:
```
```   172   "A = {} \<Longrightarrow> A \<subseteq> B"
```
```   173 and  union_comp_emptyL:
```
```   174   "\<lbrakk> A O C = {}; B O C = {} \<rbrakk> \<Longrightarrow> (A \<union> B) O C = {}"
```
```   175 and union_comp_emptyR:
```
```   176   "\<lbrakk> A O B = {}; A O C = {} \<rbrakk> \<Longrightarrow> A O (B \<union> C) = {}"
```
```   177 and wf_no_loop:
```
```   178   "R O R = {} \<Longrightarrow> wf R"
```
```   179 by (auto simp add: wf_comp_self[of R])
```
```   180
```
```   181
```
```   182 subsection \<open>Reduction pairs\<close>
```
```   183
```
```   184 definition
```
```   185   "reduction_pair P = (wf (fst P) \<and> fst P O snd P \<subseteq> fst P)"
```
```   186
```
```   187 lemma reduction_pairI[intro]: "wf R \<Longrightarrow> R O S \<subseteq> R \<Longrightarrow> reduction_pair (R, S)"
```
```   188 unfolding reduction_pair_def by auto
```
```   189
```
```   190 lemma reduction_pair_lemma:
```
```   191   assumes rp: "reduction_pair P"
```
```   192   assumes "R \<subseteq> fst P"
```
```   193   assumes "S \<subseteq> snd P"
```
```   194   assumes "wf S"
```
```   195   shows "wf (R \<union> S)"
```
```   196 proof -
```
```   197   from rp \<open>S \<subseteq> snd P\<close> have "wf (fst P)" "fst P O S \<subseteq> fst P"
```
```   198     unfolding reduction_pair_def by auto
```
```   199   with \<open>wf S\<close> have "wf (fst P \<union> S)"
```
```   200     by (auto intro: wf_union_compatible)
```
```   201   moreover from \<open>R \<subseteq> fst P\<close> have "R \<union> S \<subseteq> fst P \<union> S" by auto
```
```   202   ultimately show ?thesis by (rule wf_subset)
```
```   203 qed
```
```   204
```
```   205 definition
```
```   206   "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
```
```   207
```
```   208 lemma rp_inv_image_rp:
```
```   209   "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
```
```   210   unfolding reduction_pair_def rp_inv_image_def split_def
```
```   211   by force
```
```   212
```
```   213
```
```   214 subsection \<open>Concrete orders for SCNP termination proofs\<close>
```
```   215
```
```   216 definition "pair_less = less_than <*lex*> less_than"
```
```   217 definition "pair_leq = pair_less^="
```
```   218 definition "max_strict = max_ext pair_less"
```
```   219 definition "max_weak = max_ext pair_leq \<union> {({}, {})}"
```
```   220 definition "min_strict = min_ext pair_less"
```
```   221 definition "min_weak = min_ext pair_leq \<union> {({}, {})}"
```
```   222
```
```   223 lemma wf_pair_less[simp]: "wf pair_less"
```
```   224   by (auto simp: pair_less_def)
```
```   225
```
```   226 text \<open>Introduction rules for \<open>pair_less\<close>/\<open>pair_leq\<close>\<close>
```
```   227 lemma pair_leqI1: "a < b \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
```
```   228   and pair_leqI2: "a \<le> b \<Longrightarrow> s \<le> t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
```
```   229   and pair_lessI1: "a < b  \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
```
```   230   and pair_lessI2: "a \<le> b \<Longrightarrow> s < t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
```
```   231   unfolding pair_leq_def pair_less_def by auto
```
```   232
```
```   233 text \<open>Introduction rules for max\<close>
```
```   234 lemma smax_emptyI:
```
```   235   "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict"
```
```   236   and smax_insertI:
```
```   237   "\<lbrakk>y \<in> Y; (x, y) \<in> pair_less; (X, Y) \<in> max_strict\<rbrakk> \<Longrightarrow> (insert x X, Y) \<in> max_strict"
```
```   238   and wmax_emptyI:
```
```   239   "finite X \<Longrightarrow> ({}, X) \<in> max_weak"
```
```   240   and wmax_insertI:
```
```   241   "\<lbrakk>y \<in> YS; (x, y) \<in> pair_leq; (XS, YS) \<in> max_weak\<rbrakk> \<Longrightarrow> (insert x XS, YS) \<in> max_weak"
```
```   242 unfolding max_strict_def max_weak_def by (auto elim!: max_ext.cases)
```
```   243
```
```   244 text \<open>Introduction rules for min\<close>
```
```   245 lemma smin_emptyI:
```
```   246   "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict"
```
```   247   and smin_insertI:
```
```   248   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_less; (XS, YS) \<in> min_strict\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
```
```   249   and wmin_emptyI:
```
```   250   "(X, {}) \<in> min_weak"
```
```   251   and wmin_insertI:
```
```   252   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_leq; (XS, YS) \<in> min_weak\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_weak"
```
```   253 by (auto simp: min_strict_def min_weak_def min_ext_def)
```
```   254
```
```   255 text \<open>Reduction Pairs\<close>
```
```   256
```
```   257 lemma max_ext_compat:
```
```   258   assumes "R O S \<subseteq> R"
```
```   259   shows "max_ext R O (max_ext S \<union> {({},{})}) \<subseteq> max_ext R"
```
```   260 using assms
```
```   261 apply auto
```
```   262 apply (elim max_ext.cases)
```
```   263 apply rule
```
```   264 apply auto[3]
```
```   265 apply (drule_tac x=xa in meta_spec)
```
```   266 apply simp
```
```   267 apply (erule bexE)
```
```   268 apply (drule_tac x=xb in meta_spec)
```
```   269 by auto
```
```   270
```
```   271 lemma max_rpair_set: "reduction_pair (max_strict, max_weak)"
```
```   272   unfolding max_strict_def max_weak_def
```
```   273 apply (intro reduction_pairI max_ext_wf)
```
```   274 apply simp
```
```   275 apply (rule max_ext_compat)
```
```   276 by (auto simp: pair_less_def pair_leq_def)
```
```   277
```
```   278 lemma min_ext_compat:
```
```   279   assumes "R O S \<subseteq> R"
```
```   280   shows "min_ext R O  (min_ext S \<union> {({},{})}) \<subseteq> min_ext R"
```
```   281 using assms
```
```   282 apply (auto simp: min_ext_def)
```
```   283 apply (drule_tac x=ya in bspec, assumption)
```
```   284 apply (erule bexE)
```
```   285 apply (drule_tac x=xc in bspec)
```
```   286 apply assumption
```
```   287 by auto
```
```   288
```
```   289 lemma min_rpair_set: "reduction_pair (min_strict, min_weak)"
```
```   290   unfolding min_strict_def min_weak_def
```
```   291 apply (intro reduction_pairI min_ext_wf)
```
```   292 apply simp
```
```   293 apply (rule min_ext_compat)
```
```   294 by (auto simp: pair_less_def pair_leq_def)
```
```   295
```
```   296
```
```   297 subsection \<open>Tool setup\<close>
```
```   298
```
```   299 ML_file "Tools/Function/termination.ML"
```
```   300 ML_file "Tools/Function/scnp_solve.ML"
```
```   301 ML_file "Tools/Function/scnp_reconstruct.ML"
```
```   302 ML_file "Tools/Function/fun_cases.ML"
```
```   303
```
```   304 ML_val \<comment> "setup inactive"
```
```   305 \<open>
```
```   306   Context.theory_map (Function_Common.set_termination_prover
```
```   307     (K (ScnpReconstruct.decomp_scnp_tac [ScnpSolve.MAX, ScnpSolve.MIN, ScnpSolve.MS])))
```
```   308 \<close>
```
```   309
```
```   310 end
```