src/HOL/Fun_Def.thy
 author blanchet Wed Sep 24 15:45:55 2014 +0200 (2014-09-24) changeset 58425 246985c6b20b parent 58377 c6f93b8d2d8e child 58819 aa43c6f05bca permissions -rw-r--r--
simpler proof
```     1 (*  Title:      HOL/Fun_Def.thy
```
```     2     Author:     Alexander Krauss, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 header {* Function Definitions and Termination Proofs *}
```
```     6
```
```     7 theory Fun_Def
```
```     8 imports Basic_BNF_Least_Fixpoints Partial_Function SAT
```
```     9 keywords "function" "termination" :: thy_goal and "fun" "fun_cases" :: thy_decl
```
```    10 begin
```
```    11
```
```    12 subsection {* Definitions with default value *}
```
```    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 `\<not> D x` 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 = {*
```
```    92   Args.term >> (fn t => fn ctxt => SIMPLE_METHOD' (Function_Relation.relation_infer_tac ctxt t))
```
```    93 *} "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 = {*
```
```    99   Scan.succeed (SIMPLE_METHOD' o Pat_Completeness.pat_completeness_tac)
```
```   100 *} "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 = {*
```
```   106   Scan.succeed (NO_CASES oo Induction_Schema.induction_schema_tac)
```
```   107 *} "prove an induction principle"
```
```   108
```
```   109 setup {*
```
```   110   Function.setup
```
```   111   #> Function_Fun.setup
```
```   112 *}
```
```   113
```
```   114
```
```   115 subsection {* Measure functions *}
```
```   116
```
```   117 inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
```
```   118 where is_measure_trivial: "is_measure f"
```
```   119
```
```   120 named_theorems measure_function "rules that guide the heuristic generation of measure functions"
```
```   121 ML_file "Tools/Function/measure_functions.ML"
```
```   122
```
```   123 lemma measure_size[measure_function]: "is_measure size"
```
```   124 by (rule is_measure_trivial)
```
```   125
```
```   126 lemma measure_fst[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
```
```   127 by (rule is_measure_trivial)
```
```   128 lemma measure_snd[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
```
```   129 by (rule is_measure_trivial)
```
```   130
```
```   131 ML_file "Tools/Function/lexicographic_order.ML"
```
```   132
```
```   133 method_setup lexicographic_order = {*
```
```   134   Method.sections clasimp_modifiers >>
```
```   135   (K (SIMPLE_METHOD o Lexicographic_Order.lexicographic_order_tac false))
```
```   136 *} "termination prover for lexicographic orderings"
```
```   137
```
```   138 setup Lexicographic_Order.setup
```
```   139
```
```   140
```
```   141 subsection {* Congruence rules *}
```
```   142
```
```   143 lemma let_cong [fundef_cong]:
```
```   144   "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
```
```   145   unfolding Let_def by blast
```
```   146
```
```   147 lemmas [fundef_cong] =
```
```   148   if_cong image_cong INF_cong SUP_cong
```
```   149   bex_cong ball_cong imp_cong map_option_cong Option.bind_cong
```
```   150
```
```   151 lemma split_cong [fundef_cong]:
```
```   152   "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
```
```   153     \<Longrightarrow> split f p = split g q"
```
```   154   by (auto simp: split_def)
```
```   155
```
```   156 lemma comp_cong [fundef_cong]:
```
```   157   "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
```
```   158   unfolding o_apply .
```
```   159
```
```   160
```
```   161 subsection {* Simp rules for termination proofs *}
```
```   162
```
```   163 declare
```
```   164   trans_less_add1[termination_simp]
```
```   165   trans_less_add2[termination_simp]
```
```   166   trans_le_add1[termination_simp]
```
```   167   trans_le_add2[termination_simp]
```
```   168   less_imp_le_nat[termination_simp]
```
```   169   le_imp_less_Suc[termination_simp]
```
```   170
```
```   171 lemma size_prod_simp[termination_simp]:
```
```   172   "size_prod f g p = f (fst p) + g (snd p) + Suc 0"
```
```   173 by (induct p) auto
```
```   174
```
```   175
```
```   176 subsection {* Decomposition *}
```
```   177
```
```   178 lemma less_by_empty:
```
```   179   "A = {} \<Longrightarrow> A \<subseteq> B"
```
```   180 and  union_comp_emptyL:
```
```   181   "\<lbrakk> A O C = {}; B O C = {} \<rbrakk> \<Longrightarrow> (A \<union> B) O C = {}"
```
```   182 and union_comp_emptyR:
```
```   183   "\<lbrakk> A O B = {}; A O C = {} \<rbrakk> \<Longrightarrow> A O (B \<union> C) = {}"
```
```   184 and wf_no_loop:
```
```   185   "R O R = {} \<Longrightarrow> wf R"
```
```   186 by (auto simp add: wf_comp_self[of R])
```
```   187
```
```   188
```
```   189 subsection {* Reduction pairs *}
```
```   190
```
```   191 definition
```
```   192   "reduction_pair P = (wf (fst P) \<and> fst P O snd P \<subseteq> fst P)"
```
```   193
```
```   194 lemma reduction_pairI[intro]: "wf R \<Longrightarrow> R O S \<subseteq> R \<Longrightarrow> reduction_pair (R, S)"
```
```   195 unfolding reduction_pair_def by auto
```
```   196
```
```   197 lemma reduction_pair_lemma:
```
```   198   assumes rp: "reduction_pair P"
```
```   199   assumes "R \<subseteq> fst P"
```
```   200   assumes "S \<subseteq> snd P"
```
```   201   assumes "wf S"
```
```   202   shows "wf (R \<union> S)"
```
```   203 proof -
```
```   204   from rp `S \<subseteq> snd P` have "wf (fst P)" "fst P O S \<subseteq> fst P"
```
```   205     unfolding reduction_pair_def by auto
```
```   206   with `wf S` have "wf (fst P \<union> S)"
```
```   207     by (auto intro: wf_union_compatible)
```
```   208   moreover from `R \<subseteq> fst P` have "R \<union> S \<subseteq> fst P \<union> S" by auto
```
```   209   ultimately show ?thesis by (rule wf_subset)
```
```   210 qed
```
```   211
```
```   212 definition
```
```   213   "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
```
```   214
```
```   215 lemma rp_inv_image_rp:
```
```   216   "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
```
```   217   unfolding reduction_pair_def rp_inv_image_def split_def
```
```   218   by force
```
```   219
```
```   220
```
```   221 subsection {* Concrete orders for SCNP termination proofs *}
```
```   222
```
```   223 definition "pair_less = less_than <*lex*> less_than"
```
```   224 definition "pair_leq = pair_less^="
```
```   225 definition "max_strict = max_ext pair_less"
```
```   226 definition "max_weak = max_ext pair_leq \<union> {({}, {})}"
```
```   227 definition "min_strict = min_ext pair_less"
```
```   228 definition "min_weak = min_ext pair_leq \<union> {({}, {})}"
```
```   229
```
```   230 lemma wf_pair_less[simp]: "wf pair_less"
```
```   231   by (auto simp: pair_less_def)
```
```   232
```
```   233 text {* Introduction rules for @{text pair_less}/@{text pair_leq} *}
```
```   234 lemma pair_leqI1: "a < b \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
```
```   235   and pair_leqI2: "a \<le> b \<Longrightarrow> s \<le> t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
```
```   236   and pair_lessI1: "a < b  \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
```
```   237   and pair_lessI2: "a \<le> b \<Longrightarrow> s < t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
```
```   238   unfolding pair_leq_def pair_less_def by auto
```
```   239
```
```   240 text {* Introduction rules for max *}
```
```   241 lemma smax_emptyI:
```
```   242   "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict"
```
```   243   and smax_insertI:
```
```   244   "\<lbrakk>y \<in> Y; (x, y) \<in> pair_less; (X, Y) \<in> max_strict\<rbrakk> \<Longrightarrow> (insert x X, Y) \<in> max_strict"
```
```   245   and wmax_emptyI:
```
```   246   "finite X \<Longrightarrow> ({}, X) \<in> max_weak"
```
```   247   and wmax_insertI:
```
```   248   "\<lbrakk>y \<in> YS; (x, y) \<in> pair_leq; (XS, YS) \<in> max_weak\<rbrakk> \<Longrightarrow> (insert x XS, YS) \<in> max_weak"
```
```   249 unfolding max_strict_def max_weak_def by (auto elim!: max_ext.cases)
```
```   250
```
```   251 text {* Introduction rules for min *}
```
```   252 lemma smin_emptyI:
```
```   253   "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict"
```
```   254   and smin_insertI:
```
```   255   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_less; (XS, YS) \<in> min_strict\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
```
```   256   and wmin_emptyI:
```
```   257   "(X, {}) \<in> min_weak"
```
```   258   and wmin_insertI:
```
```   259   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_leq; (XS, YS) \<in> min_weak\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_weak"
```
```   260 by (auto simp: min_strict_def min_weak_def min_ext_def)
```
```   261
```
```   262 text {* Reduction Pairs *}
```
```   263
```
```   264 lemma max_ext_compat:
```
```   265   assumes "R O S \<subseteq> R"
```
```   266   shows "max_ext R O (max_ext S \<union> {({},{})}) \<subseteq> max_ext R"
```
```   267 using assms
```
```   268 apply auto
```
```   269 apply (elim max_ext.cases)
```
```   270 apply rule
```
```   271 apply auto[3]
```
```   272 apply (drule_tac x=xa in meta_spec)
```
```   273 apply simp
```
```   274 apply (erule bexE)
```
```   275 apply (drule_tac x=xb in meta_spec)
```
```   276 by auto
```
```   277
```
```   278 lemma max_rpair_set: "reduction_pair (max_strict, max_weak)"
```
```   279   unfolding max_strict_def max_weak_def
```
```   280 apply (intro reduction_pairI max_ext_wf)
```
```   281 apply simp
```
```   282 apply (rule max_ext_compat)
```
```   283 by (auto simp: pair_less_def pair_leq_def)
```
```   284
```
```   285 lemma min_ext_compat:
```
```   286   assumes "R O S \<subseteq> R"
```
```   287   shows "min_ext R O  (min_ext S \<union> {({},{})}) \<subseteq> min_ext R"
```
```   288 using assms
```
```   289 apply (auto simp: min_ext_def)
```
```   290 apply (drule_tac x=ya in bspec, assumption)
```
```   291 apply (erule bexE)
```
```   292 apply (drule_tac x=xc in bspec)
```
```   293 apply assumption
```
```   294 by auto
```
```   295
```
```   296 lemma min_rpair_set: "reduction_pair (min_strict, min_weak)"
```
```   297   unfolding min_strict_def min_weak_def
```
```   298 apply (intro reduction_pairI min_ext_wf)
```
```   299 apply simp
```
```   300 apply (rule min_ext_compat)
```
```   301 by (auto simp: pair_less_def pair_leq_def)
```
```   302
```
```   303
```
```   304 subsection {* Tool setup *}
```
```   305
```
```   306 ML_file "Tools/Function/termination.ML"
```
```   307 ML_file "Tools/Function/scnp_solve.ML"
```
```   308 ML_file "Tools/Function/scnp_reconstruct.ML"
```
```   309 ML_file "Tools/Function/fun_cases.ML"
```
```   310
```
```   311 setup ScnpReconstruct.setup
```
```   312
```
```   313 ML_val -- "setup inactive"
```
```   314 {*
```
```   315   Context.theory_map (Function_Common.set_termination_prover
```
```   316     (ScnpReconstruct.decomp_scnp_tac [ScnpSolve.MAX, ScnpSolve.MIN, ScnpSolve.MS]))
```
```   317 *}
```
```   318
```
```   319 end
```