src/HOL/FunDef.thy
author kuncar
Fri Dec 09 18:07:04 2011 +0100 (2011-12-09)
changeset 45802 b16f976db515
parent 40108 dbab949c2717
child 46526 c4cf9d03c352
permissions -rw-r--r--
Quotient_Info stores only relation maps
     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 uses
    10   "Tools/prop_logic.ML"
    11   "Tools/sat_solver.ML"
    12   ("Tools/Function/function_common.ML")
    13   ("Tools/Function/context_tree.ML")
    14   ("Tools/Function/function_core.ML")
    15   ("Tools/Function/sum_tree.ML")
    16   ("Tools/Function/mutual.ML")
    17   ("Tools/Function/pattern_split.ML")
    18   ("Tools/Function/function.ML")
    19   ("Tools/Function/relation.ML")
    20   ("Tools/Function/measure_functions.ML")
    21   ("Tools/Function/lexicographic_order.ML")
    22   ("Tools/Function/pat_completeness.ML")
    23   ("Tools/Function/fun.ML")
    24   ("Tools/Function/induction_schema.ML")
    25   ("Tools/Function/termination.ML")
    26   ("Tools/Function/scnp_solve.ML")
    27   ("Tools/Function/scnp_reconstruct.ML")
    28 begin
    29 
    30 subsection {* Definitions with default value. *}
    31 
    32 definition
    33   THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
    34   "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
    35 
    36 lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
    37   by (simp add: theI' THE_default_def)
    38 
    39 lemma THE_default1_equality:
    40     "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
    41   by (simp add: the1_equality THE_default_def)
    42 
    43 lemma THE_default_none:
    44     "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
    45   by (simp add:THE_default_def)
    46 
    47 
    48 lemma fundef_ex1_existence:
    49   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    50   assumes ex1: "\<exists>!y. G x y"
    51   shows "G x (f x)"
    52   apply (simp only: f_def)
    53   apply (rule THE_defaultI')
    54   apply (rule ex1)
    55   done
    56 
    57 lemma fundef_ex1_uniqueness:
    58   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    59   assumes ex1: "\<exists>!y. G x y"
    60   assumes elm: "G x (h x)"
    61   shows "h x = f x"
    62   apply (simp only: f_def)
    63   apply (rule THE_default1_equality [symmetric])
    64    apply (rule ex1)
    65   apply (rule elm)
    66   done
    67 
    68 lemma fundef_ex1_iff:
    69   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    70   assumes ex1: "\<exists>!y. G x y"
    71   shows "(G x y) = (f x = y)"
    72   apply (auto simp:ex1 f_def THE_default1_equality)
    73   apply (rule THE_defaultI')
    74   apply (rule ex1)
    75   done
    76 
    77 lemma fundef_default_value:
    78   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    79   assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
    80   assumes "\<not> D x"
    81   shows "f x = d x"
    82 proof -
    83   have "\<not>(\<exists>y. G x y)"
    84   proof
    85     assume "\<exists>y. G x y"
    86     hence "D x" using graph ..
    87     with `\<not> D x` show False ..
    88   qed
    89   hence "\<not>(\<exists>!y. G x y)" by blast
    90 
    91   thus ?thesis
    92     unfolding f_def
    93     by (rule THE_default_none)
    94 qed
    95 
    96 definition in_rel_def[simp]:
    97   "in_rel R x y == (x, y) \<in> R"
    98 
    99 lemma wf_in_rel:
   100   "wf R \<Longrightarrow> wfP (in_rel R)"
   101   by (simp add: wfP_def)
   102 
   103 use "Tools/Function/function_common.ML"
   104 use "Tools/Function/context_tree.ML"
   105 use "Tools/Function/function_core.ML"
   106 use "Tools/Function/sum_tree.ML"
   107 use "Tools/Function/mutual.ML"
   108 use "Tools/Function/pattern_split.ML"
   109 use "Tools/Function/relation.ML"
   110 use "Tools/Function/function.ML"
   111 use "Tools/Function/pat_completeness.ML"
   112 use "Tools/Function/fun.ML"
   113 use "Tools/Function/induction_schema.ML"
   114 
   115 setup {* 
   116   Function.setup
   117   #> Pat_Completeness.setup
   118   #> Function_Fun.setup
   119   #> Induction_Schema.setup
   120 *}
   121 
   122 subsection {* Measure Functions *}
   123 
   124 inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
   125 where is_measure_trivial: "is_measure f"
   126 
   127 use "Tools/Function/measure_functions.ML"
   128 setup MeasureFunctions.setup
   129 
   130 lemma measure_size[measure_function]: "is_measure size"
   131 by (rule is_measure_trivial)
   132 
   133 lemma measure_fst[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
   134 by (rule is_measure_trivial)
   135 lemma measure_snd[measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
   136 by (rule is_measure_trivial)
   137 
   138 use "Tools/Function/lexicographic_order.ML"
   139 setup Lexicographic_Order.setup 
   140 
   141 
   142 subsection {* Congruence Rules *}
   143 
   144 lemma let_cong [fundef_cong]:
   145   "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
   146   unfolding Let_def by blast
   147 
   148 lemmas [fundef_cong] =
   149   if_cong image_cong INT_cong UN_cong
   150   bex_cong ball_cong imp_cong
   151 
   152 lemma split_cong [fundef_cong]:
   153   "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
   154     \<Longrightarrow> split f p = split g q"
   155   by (auto simp: split_def)
   156 
   157 lemma comp_cong [fundef_cong]:
   158   "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
   159   unfolding o_apply .
   160 
   161 subsection {* Simp rules for termination proofs *}
   162 
   163 lemma termination_basic_simps[termination_simp]:
   164   "x < (y::nat) \<Longrightarrow> x < y + z" 
   165   "x < z \<Longrightarrow> x < y + z"
   166   "x \<le> y \<Longrightarrow> x \<le> y + (z::nat)"
   167   "x \<le> z \<Longrightarrow> x \<le> y + (z::nat)"
   168   "x < y \<Longrightarrow> x \<le> (y::nat)"
   169 by arith+
   170 
   171 declare le_imp_less_Suc[termination_simp]
   172 
   173 lemma prod_size_simp[termination_simp]:
   174   "prod_size f g p = f (fst p) + g (snd p) + Suc 0"
   175 by (induct p) auto
   176 
   177 subsection {* Decomposition *}
   178 
   179 lemma less_by_empty: 
   180   "A = {} \<Longrightarrow> A \<subseteq> B"
   181 and  union_comp_emptyL:
   182   "\<lbrakk> A O C = {}; B O C = {} \<rbrakk> \<Longrightarrow> (A \<union> B) O C = {}"
   183 and union_comp_emptyR:
   184   "\<lbrakk> A O B = {}; A O C = {} \<rbrakk> \<Longrightarrow> A O (B \<union> C) = {}"
   185 and wf_no_loop: 
   186   "R O R = {} \<Longrightarrow> wf R"
   187 by (auto simp add: wf_comp_self[of R])
   188 
   189 
   190 subsection {* Reduction Pairs *}
   191 
   192 definition
   193   "reduction_pair P = (wf (fst P) \<and> fst P O snd P \<subseteq> fst P)"
   194 
   195 lemma reduction_pairI[intro]: "wf R \<Longrightarrow> R O S \<subseteq> R \<Longrightarrow> reduction_pair (R, S)"
   196 unfolding reduction_pair_def by auto
   197 
   198 lemma reduction_pair_lemma:
   199   assumes rp: "reduction_pair P"
   200   assumes "R \<subseteq> fst P"
   201   assumes "S \<subseteq> snd P"
   202   assumes "wf S"
   203   shows "wf (R \<union> S)"
   204 proof -
   205   from rp `S \<subseteq> snd P` have "wf (fst P)" "fst P O S \<subseteq> fst P"
   206     unfolding reduction_pair_def by auto
   207   with `wf S` have "wf (fst P \<union> S)" 
   208     by (auto intro: wf_union_compatible)
   209   moreover from `R \<subseteq> fst P` have "R \<union> S \<subseteq> fst P \<union> S" by auto
   210   ultimately show ?thesis by (rule wf_subset) 
   211 qed
   212 
   213 definition
   214   "rp_inv_image = (\<lambda>(R,S) f. (inv_image R f, inv_image S f))"
   215 
   216 lemma rp_inv_image_rp:
   217   "reduction_pair P \<Longrightarrow> reduction_pair (rp_inv_image P f)"
   218   unfolding reduction_pair_def rp_inv_image_def split_def
   219   by force
   220 
   221 
   222 subsection {* Concrete orders for SCNP termination proofs *}
   223 
   224 definition "pair_less = less_than <*lex*> less_than"
   225 definition "pair_leq = pair_less^="
   226 definition "max_strict = max_ext pair_less"
   227 definition "max_weak = max_ext pair_leq \<union> {({}, {})}"
   228 definition "min_strict = min_ext pair_less"
   229 definition "min_weak = min_ext pair_leq \<union> {({}, {})}"
   230 
   231 lemma wf_pair_less[simp]: "wf pair_less"
   232   by (auto simp: pair_less_def)
   233 
   234 text {* Introduction rules for @{text pair_less}/@{text pair_leq} *}
   235 lemma pair_leqI1: "a < b \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
   236   and pair_leqI2: "a \<le> b \<Longrightarrow> s \<le> t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_leq"
   237   and pair_lessI1: "a < b  \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
   238   and pair_lessI2: "a \<le> b \<Longrightarrow> s < t \<Longrightarrow> ((a, s), (b, t)) \<in> pair_less"
   239   unfolding pair_leq_def pair_less_def by auto
   240 
   241 text {* Introduction rules for max *}
   242 lemma smax_emptyI: 
   243   "finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> ({}, Y) \<in> max_strict" 
   244   and smax_insertI: 
   245   "\<lbrakk>y \<in> Y; (x, y) \<in> pair_less; (X, Y) \<in> max_strict\<rbrakk> \<Longrightarrow> (insert x X, Y) \<in> max_strict"
   246   and wmax_emptyI: 
   247   "finite X \<Longrightarrow> ({}, X) \<in> max_weak" 
   248   and wmax_insertI:
   249   "\<lbrakk>y \<in> YS; (x, y) \<in> pair_leq; (XS, YS) \<in> max_weak\<rbrakk> \<Longrightarrow> (insert x XS, YS) \<in> max_weak" 
   250 unfolding max_strict_def max_weak_def by (auto elim!: max_ext.cases)
   251 
   252 text {* Introduction rules for min *}
   253 lemma smin_emptyI: 
   254   "X \<noteq> {} \<Longrightarrow> (X, {}) \<in> min_strict" 
   255   and smin_insertI: 
   256   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_less; (XS, YS) \<in> min_strict\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_strict"
   257   and wmin_emptyI: 
   258   "(X, {}) \<in> min_weak" 
   259   and wmin_insertI: 
   260   "\<lbrakk>x \<in> XS; (x, y) \<in> pair_leq; (XS, YS) \<in> min_weak\<rbrakk> \<Longrightarrow> (XS, insert y YS) \<in> min_weak" 
   261 by (auto simp: min_strict_def min_weak_def min_ext_def)
   262 
   263 text {* Reduction Pairs *}
   264 
   265 lemma max_ext_compat: 
   266   assumes "R O S \<subseteq> R"
   267   shows "max_ext R O (max_ext S \<union> {({},{})}) \<subseteq> max_ext R"
   268 using assms 
   269 apply auto
   270 apply (elim max_ext.cases)
   271 apply rule
   272 apply auto[3]
   273 apply (drule_tac x=xa in meta_spec)
   274 apply simp
   275 apply (erule bexE)
   276 apply (drule_tac x=xb in meta_spec)
   277 by auto
   278 
   279 lemma max_rpair_set: "reduction_pair (max_strict, max_weak)"
   280   unfolding max_strict_def max_weak_def 
   281 apply (intro reduction_pairI max_ext_wf)
   282 apply simp
   283 apply (rule max_ext_compat)
   284 by (auto simp: pair_less_def pair_leq_def)
   285 
   286 lemma min_ext_compat: 
   287   assumes "R O S \<subseteq> R"
   288   shows "min_ext R O  (min_ext S \<union> {({},{})}) \<subseteq> min_ext R"
   289 using assms 
   290 apply (auto simp: min_ext_def)
   291 apply (drule_tac x=ya in bspec, assumption)
   292 apply (erule bexE)
   293 apply (drule_tac x=xc in bspec)
   294 apply assumption
   295 by auto
   296 
   297 lemma min_rpair_set: "reduction_pair (min_strict, min_weak)"
   298   unfolding min_strict_def min_weak_def 
   299 apply (intro reduction_pairI min_ext_wf)
   300 apply simp
   301 apply (rule min_ext_compat)
   302 by (auto simp: pair_less_def pair_leq_def)
   303 
   304 
   305 subsection {* Tool setup *}
   306 
   307 use "Tools/Function/termination.ML"
   308 use "Tools/Function/scnp_solve.ML"
   309 use "Tools/Function/scnp_reconstruct.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