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