src/HOL/FunDef.thy
author krauss
Thu Jun 19 11:46:14 2008 +0200 (2008-06-19)
changeset 27271 ba2a00d35df1
parent 26875 e18574413bc4
child 29125 d41182a8135c
permissions -rw-r--r--
generalized induct_scheme method to prove conditional induction schemes.
wenzelm@20324
     1
(*  Title:      HOL/FunDef.thy
wenzelm@20324
     2
    ID:         $Id$
wenzelm@20324
     3
    Author:     Alexander Krauss, TU Muenchen
wenzelm@22816
     4
*)
wenzelm@20324
     5
wenzelm@22816
     6
header {* General recursive function definitions *}
wenzelm@20324
     7
krauss@19564
     8
theory FunDef
krauss@26748
     9
imports Wellfounded
wenzelm@22816
    10
uses
krauss@23203
    11
  ("Tools/function_package/fundef_lib.ML")
wenzelm@22816
    12
  ("Tools/function_package/fundef_common.ML")
wenzelm@22816
    13
  ("Tools/function_package/inductive_wrap.ML")
wenzelm@22816
    14
  ("Tools/function_package/context_tree.ML")
wenzelm@22816
    15
  ("Tools/function_package/fundef_core.ML")
krauss@25556
    16
  ("Tools/function_package/sum_tree.ML")
wenzelm@22816
    17
  ("Tools/function_package/mutual.ML")
wenzelm@22816
    18
  ("Tools/function_package/pattern_split.ML")
wenzelm@22816
    19
  ("Tools/function_package/fundef_package.ML")
wenzelm@22816
    20
  ("Tools/function_package/auto_term.ML")
krauss@26875
    21
  ("Tools/function_package/measure_functions.ML")
krauss@26748
    22
  ("Tools/function_package/lexicographic_order.ML")
krauss@26748
    23
  ("Tools/function_package/fundef_datatype.ML")
krauss@27271
    24
  ("Tools/function_package/induction_scheme.ML")
krauss@19564
    25
begin
krauss@19564
    26
wenzelm@22816
    27
text {* Definitions with default value. *}
krauss@20536
    28
krauss@20536
    29
definition
wenzelm@21404
    30
  THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
krauss@20536
    31
  "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
krauss@20536
    32
krauss@20536
    33
lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
wenzelm@22816
    34
  by (simp add: theI' THE_default_def)
krauss@20536
    35
wenzelm@22816
    36
lemma THE_default1_equality:
wenzelm@22816
    37
    "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
wenzelm@22816
    38
  by (simp add: the1_equality THE_default_def)
krauss@20536
    39
krauss@20536
    40
lemma THE_default_none:
wenzelm@22816
    41
    "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
wenzelm@22816
    42
  by (simp add:THE_default_def)
krauss@20536
    43
krauss@20536
    44
krauss@19564
    45
lemma fundef_ex1_existence:
wenzelm@22816
    46
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    47
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    48
  shows "G x (f x)"
wenzelm@22816
    49
  apply (simp only: f_def)
wenzelm@22816
    50
  apply (rule THE_defaultI')
wenzelm@22816
    51
  apply (rule ex1)
wenzelm@22816
    52
  done
krauss@21051
    53
krauss@19564
    54
lemma fundef_ex1_uniqueness:
wenzelm@22816
    55
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    56
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    57
  assumes elm: "G x (h x)"
wenzelm@22816
    58
  shows "h x = f x"
wenzelm@22816
    59
  apply (simp only: f_def)
wenzelm@22816
    60
  apply (rule THE_default1_equality [symmetric])
wenzelm@22816
    61
   apply (rule ex1)
wenzelm@22816
    62
  apply (rule elm)
wenzelm@22816
    63
  done
krauss@19564
    64
krauss@19564
    65
lemma fundef_ex1_iff:
wenzelm@22816
    66
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    67
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    68
  shows "(G x y) = (f x = y)"
krauss@20536
    69
  apply (auto simp:ex1 f_def THE_default1_equality)
wenzelm@22816
    70
  apply (rule THE_defaultI')
wenzelm@22816
    71
  apply (rule ex1)
wenzelm@22816
    72
  done
krauss@19564
    73
krauss@20654
    74
lemma fundef_default_value:
wenzelm@22816
    75
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    76
  assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
wenzelm@22816
    77
  assumes "\<not> D x"
wenzelm@22816
    78
  shows "f x = d x"
krauss@20654
    79
proof -
krauss@21051
    80
  have "\<not>(\<exists>y. G x y)"
krauss@20654
    81
  proof
krauss@21512
    82
    assume "\<exists>y. G x y"
krauss@21512
    83
    hence "D x" using graph ..
krauss@21512
    84
    with `\<not> D x` show False ..
krauss@20654
    85
  qed
krauss@21051
    86
  hence "\<not>(\<exists>!y. G x y)" by blast
wenzelm@22816
    87
krauss@20654
    88
  thus ?thesis
krauss@20654
    89
    unfolding f_def
krauss@20654
    90
    by (rule THE_default_none)
krauss@20654
    91
qed
krauss@20654
    92
berghofe@23739
    93
definition in_rel_def[simp]:
berghofe@23739
    94
  "in_rel R x y == (x, y) \<in> R"
berghofe@23739
    95
berghofe@23739
    96
lemma wf_in_rel:
berghofe@23739
    97
  "wf R \<Longrightarrow> wfP (in_rel R)"
berghofe@23739
    98
  by (simp add: wfP_def)
berghofe@23739
    99
krauss@26875
   100
inductive is_measure :: "('a \<Rightarrow> nat) \<Rightarrow> bool"
krauss@26875
   101
where is_measure_trivial: "is_measure f"
berghofe@23739
   102
krauss@23203
   103
use "Tools/function_package/fundef_lib.ML"
krauss@19564
   104
use "Tools/function_package/fundef_common.ML"
krauss@20523
   105
use "Tools/function_package/inductive_wrap.ML"
krauss@19564
   106
use "Tools/function_package/context_tree.ML"
krauss@22166
   107
use "Tools/function_package/fundef_core.ML"
krauss@25556
   108
use "Tools/function_package/sum_tree.ML"
krauss@19770
   109
use "Tools/function_package/mutual.ML"
krauss@20270
   110
use "Tools/function_package/pattern_split.ML"
krauss@21319
   111
use "Tools/function_package/auto_term.ML"
krauss@19564
   112
use "Tools/function_package/fundef_package.ML"
krauss@26875
   113
use "Tools/function_package/measure_functions.ML"
krauss@26748
   114
use "Tools/function_package/lexicographic_order.ML"
krauss@26748
   115
use "Tools/function_package/fundef_datatype.ML"
krauss@27271
   116
use "Tools/function_package/induction_scheme.ML"
krauss@19564
   117
krauss@25567
   118
setup {* 
krauss@25567
   119
  FundefPackage.setup 
krauss@25567
   120
  #> InductionScheme.setup
krauss@26875
   121
  #> MeasureFunctions.setup
krauss@26748
   122
  #> LexicographicOrder.setup 
krauss@26748
   123
  #> FundefDatatype.setup
krauss@25567
   124
*}
krauss@19770
   125
haftmann@22838
   126
lemma let_cong [fundef_cong]:
haftmann@22838
   127
  "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
wenzelm@22816
   128
  unfolding Let_def by blast
krauss@22622
   129
wenzelm@22816
   130
lemmas [fundef_cong] =
haftmann@22838
   131
  if_cong image_cong INT_cong UN_cong
haftmann@22838
   132
  bex_cong ball_cong imp_cong
krauss@19564
   133
wenzelm@22816
   134
lemma split_cong [fundef_cong]:
haftmann@22838
   135
  "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
wenzelm@22816
   136
    \<Longrightarrow> split f p = split g q"
wenzelm@22816
   137
  by (auto simp: split_def)
krauss@19934
   138
wenzelm@22816
   139
lemma comp_cong [fundef_cong]:
haftmann@22838
   140
  "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
wenzelm@22816
   141
  unfolding o_apply .
krauss@19934
   142
krauss@26875
   143
subsection {* Setup for termination proofs *}
krauss@26875
   144
krauss@26875
   145
text {* Rules for generating measure functions *}
krauss@26875
   146
krauss@26875
   147
lemma [measure_function]: "is_measure size"
krauss@26875
   148
by (rule is_measure_trivial)
krauss@26875
   149
krauss@26875
   150
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (fst p))"
krauss@26875
   151
by (rule is_measure_trivial)
krauss@26875
   152
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (\<lambda>p. f (snd p))"
krauss@26875
   153
by (rule is_measure_trivial)
krauss@26875
   154
krauss@26749
   155
lemma termination_basic_simps[termination_simp]:
krauss@26749
   156
  "x < (y::nat) \<Longrightarrow> x < y + z" 
krauss@26749
   157
  "x < z \<Longrightarrow> x < y + z"
krauss@26875
   158
  "x \<le> y \<Longrightarrow> x \<le> y + (z::nat)"
krauss@26875
   159
  "x \<le> z \<Longrightarrow> x \<le> y + (z::nat)"
krauss@26875
   160
  "x < y \<Longrightarrow> x \<le> (y::nat)"
krauss@26749
   161
by arith+
krauss@26749
   162
krauss@26875
   163
declare le_imp_less_Suc[termination_simp]
krauss@26875
   164
krauss@26875
   165
lemma prod_size_simp[termination_simp]:
krauss@26875
   166
  "prod_size f g p = f (fst p) + g (snd p) + Suc 0"
krauss@26875
   167
by (induct p) auto
krauss@26875
   168
krauss@26875
   169
krauss@19564
   170
end