src/HOL/FunDef.thy
author krauss
Fri Apr 25 16:28:06 2008 +0200 (2008-04-25)
changeset 26749 397a1aeede7d
parent 26748 4d51ddd6aa5c
child 26875 e18574413bc4
permissions -rw-r--r--
* New attribute "termination_simp": Simp rules for termination proofs
* General lemmas about list_size
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@25567
    21
  ("Tools/function_package/induction_scheme.ML")
krauss@26748
    22
  ("Tools/function_package/lexicographic_order.ML")
krauss@26748
    23
  ("Tools/function_package/fundef_datatype.ML")
krauss@19564
    24
begin
krauss@19564
    25
wenzelm@22816
    26
text {* Definitions with default value. *}
krauss@20536
    27
krauss@20536
    28
definition
wenzelm@21404
    29
  THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
krauss@20536
    30
  "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
krauss@20536
    31
krauss@20536
    32
lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
wenzelm@22816
    33
  by (simp add: theI' THE_default_def)
krauss@20536
    34
wenzelm@22816
    35
lemma THE_default1_equality:
wenzelm@22816
    36
    "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
wenzelm@22816
    37
  by (simp add: the1_equality THE_default_def)
krauss@20536
    38
krauss@20536
    39
lemma THE_default_none:
wenzelm@22816
    40
    "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
wenzelm@22816
    41
  by (simp add:THE_default_def)
krauss@20536
    42
krauss@20536
    43
krauss@19564
    44
lemma fundef_ex1_existence:
wenzelm@22816
    45
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    46
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    47
  shows "G x (f x)"
wenzelm@22816
    48
  apply (simp only: f_def)
wenzelm@22816
    49
  apply (rule THE_defaultI')
wenzelm@22816
    50
  apply (rule ex1)
wenzelm@22816
    51
  done
krauss@21051
    52
krauss@19564
    53
lemma fundef_ex1_uniqueness:
wenzelm@22816
    54
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    55
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    56
  assumes elm: "G x (h x)"
wenzelm@22816
    57
  shows "h x = f x"
wenzelm@22816
    58
  apply (simp only: f_def)
wenzelm@22816
    59
  apply (rule THE_default1_equality [symmetric])
wenzelm@22816
    60
   apply (rule ex1)
wenzelm@22816
    61
  apply (rule elm)
wenzelm@22816
    62
  done
krauss@19564
    63
krauss@19564
    64
lemma fundef_ex1_iff:
wenzelm@22816
    65
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    66
  assumes ex1: "\<exists>!y. G x y"
wenzelm@22816
    67
  shows "(G x y) = (f x = y)"
krauss@20536
    68
  apply (auto simp:ex1 f_def THE_default1_equality)
wenzelm@22816
    69
  apply (rule THE_defaultI')
wenzelm@22816
    70
  apply (rule ex1)
wenzelm@22816
    71
  done
krauss@19564
    72
krauss@20654
    73
lemma fundef_default_value:
wenzelm@22816
    74
  assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
wenzelm@22816
    75
  assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
wenzelm@22816
    76
  assumes "\<not> D x"
wenzelm@22816
    77
  shows "f x = d x"
krauss@20654
    78
proof -
krauss@21051
    79
  have "\<not>(\<exists>y. G x y)"
krauss@20654
    80
  proof
krauss@21512
    81
    assume "\<exists>y. G x y"
krauss@21512
    82
    hence "D x" using graph ..
krauss@21512
    83
    with `\<not> D x` show False ..
krauss@20654
    84
  qed
krauss@21051
    85
  hence "\<not>(\<exists>!y. G x y)" by blast
wenzelm@22816
    86
krauss@20654
    87
  thus ?thesis
krauss@20654
    88
    unfolding f_def
krauss@20654
    89
    by (rule THE_default_none)
krauss@20654
    90
qed
krauss@20654
    91
berghofe@23739
    92
definition in_rel_def[simp]:
berghofe@23739
    93
  "in_rel R x y == (x, y) \<in> R"
berghofe@23739
    94
berghofe@23739
    95
lemma wf_in_rel:
berghofe@23739
    96
  "wf R \<Longrightarrow> wfP (in_rel R)"
berghofe@23739
    97
  by (simp add: wfP_def)
berghofe@23739
    98
berghofe@23739
    99
krauss@23203
   100
use "Tools/function_package/fundef_lib.ML"
krauss@19564
   101
use "Tools/function_package/fundef_common.ML"
krauss@20523
   102
use "Tools/function_package/inductive_wrap.ML"
krauss@19564
   103
use "Tools/function_package/context_tree.ML"
krauss@22166
   104
use "Tools/function_package/fundef_core.ML"
krauss@25556
   105
use "Tools/function_package/sum_tree.ML"
krauss@19770
   106
use "Tools/function_package/mutual.ML"
krauss@20270
   107
use "Tools/function_package/pattern_split.ML"
krauss@21319
   108
use "Tools/function_package/auto_term.ML"
krauss@19564
   109
use "Tools/function_package/fundef_package.ML"
krauss@25567
   110
use "Tools/function_package/induction_scheme.ML"
krauss@26748
   111
use "Tools/function_package/lexicographic_order.ML"
krauss@26748
   112
use "Tools/function_package/fundef_datatype.ML"
krauss@19564
   113
krauss@25567
   114
setup {* 
krauss@25567
   115
  FundefPackage.setup 
krauss@25567
   116
  #> InductionScheme.setup
krauss@26748
   117
  #> LexicographicOrder.setup 
krauss@26748
   118
  #> FundefDatatype.setup
krauss@25567
   119
*}
krauss@19770
   120
haftmann@22838
   121
lemma let_cong [fundef_cong]:
haftmann@22838
   122
  "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
wenzelm@22816
   123
  unfolding Let_def by blast
krauss@22622
   124
wenzelm@22816
   125
lemmas [fundef_cong] =
haftmann@22838
   126
  if_cong image_cong INT_cong UN_cong
haftmann@22838
   127
  bex_cong ball_cong imp_cong
krauss@19564
   128
wenzelm@22816
   129
lemma split_cong [fundef_cong]:
haftmann@22838
   130
  "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
wenzelm@22816
   131
    \<Longrightarrow> split f p = split g q"
wenzelm@22816
   132
  by (auto simp: split_def)
krauss@19934
   133
wenzelm@22816
   134
lemma comp_cong [fundef_cong]:
haftmann@22838
   135
  "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
wenzelm@22816
   136
  unfolding o_apply .
krauss@19934
   137
krauss@26749
   138
lemma termination_basic_simps[termination_simp]:
krauss@26749
   139
  "x < y \<Longrightarrow> x < Suc y"
krauss@26749
   140
  "x < (y::nat) \<Longrightarrow> x < y + z" 
krauss@26749
   141
  "x < z \<Longrightarrow> x < y + z"
krauss@26749
   142
by arith+
krauss@26749
   143
krauss@19564
   144
end