src/HOL/Wfrec.thy
author blanchet
Tue, 16 Sep 2014 19:23:37 +0200
changeset 58352 37745650a3f4
parent 58184 db1381d811ab
child 58889 5b7a9633cfa8
permissions -rw-r--r--
register 'prod' and 'sum' as datatypes, to allow N2M through them
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55210
d1e3b708d74b tuned headers;
wenzelm
parents: 55017
diff changeset
     1
(*  Title:      HOL/Wfrec.thy
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     2
    Author:     Tobias Nipkow
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     3
    Author:     Lawrence C Paulson
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     4
    Author:     Konrad Slind
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     5
*)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     6
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     7
header {* Well-Founded Recursion Combinator *}
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     8
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
     9
theory Wfrec
55017
2df6ad1dbd66 adapted to move of Wfrec
blanchet
parents: 55016
diff changeset
    10
imports Wellfounded
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    11
begin
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    12
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    13
inductive wfrec_rel :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool" for R F where
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    14
  wfrecI: "(\<And>z. (z, x) \<in> R \<Longrightarrow> wfrec_rel R F z (g z)) \<Longrightarrow> wfrec_rel R F x (F g x)"
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    15
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    16
definition cut :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b" where
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    17
  "cut f R x = (\<lambda>y. if (y, x) \<in> R then f y else undefined)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    18
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    19
definition adm_wf :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)) \<Rightarrow> bool" where
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    20
  "adm_wf R F \<longleftrightarrow> (\<forall>f g x. (\<forall>z. (z, x) \<in> R \<longrightarrow> f z = g z) \<longrightarrow> F f x = F g x)"
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    21
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    22
definition wfrec :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)) \<Rightarrow> ('a \<Rightarrow> 'b)" where
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    23
  "wfrec R F = (\<lambda>x. THE y. wfrec_rel R (\<lambda>f x. F (cut f R x) x) x y)"
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    24
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    25
lemma cuts_eq: "(cut f R x = cut g R x) \<longleftrightarrow> (\<forall>y. (y, x) \<in> R \<longrightarrow> f y = g y)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    26
  by (simp add: fun_eq_iff cut_def)
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    27
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    28
lemma cut_apply: "(x, a) \<in> R \<Longrightarrow> cut f R a x = f x"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    29
  by (simp add: cut_def)
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    30
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    31
text{*Inductive characterization of wfrec combinator; for details see:
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    32
John Harrison, "Inductive definitions: automation and application"*}
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    33
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    34
lemma theI_unique: "\<exists>!x. P x \<Longrightarrow> P x \<longleftrightarrow> x = The P"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    35
  by (auto intro: the_equality[symmetric] theI)
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    36
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    37
lemma wfrec_unique: assumes "adm_wf R F" "wf R" shows "\<exists>!y. wfrec_rel R F x y"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    38
  using `wf R`
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    39
proof induct
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    40
  def f \<equiv> "\<lambda>y. THE z. wfrec_rel R F y z"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    41
  case (less x)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    42
  then have "\<And>y z. (y, x) \<in> R \<Longrightarrow> wfrec_rel R F y z \<longleftrightarrow> z = f y"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    43
    unfolding f_def by (rule theI_unique)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    44
  with `adm_wf R F` show ?case
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    45
    by (subst wfrec_rel.simps) (auto simp: adm_wf_def)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    46
qed
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    47
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    48
lemma adm_lemma: "adm_wf R (\<lambda>f x. F (cut f R x) x)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    49
  by (auto simp add: adm_wf_def
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    50
           intro!: arg_cong[where f="\<lambda>x. F x y" for y] cuts_eq[THEN iffD2])
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    51
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    52
lemma wfrec: "wf R \<Longrightarrow> wfrec R F a = F (cut (wfrec R F) R a) a"
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    53
apply (simp add: wfrec_def)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    54
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    55
apply (rule wfrec_rel.wfrecI)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    56
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    57
done
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    58
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    59
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    60
text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    61
lemma def_wfrec: "f \<equiv> wfrec R F \<Longrightarrow> wf R \<Longrightarrow> f a = F (cut f R a) a"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    62
 by (auto intro: wfrec)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    63
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    64
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    65
subsubsection {* Well-founded recursion via genuine fixpoints *}
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    66
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    67
lemma wfrec_fixpoint:
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    68
  assumes WF: "wf R" and ADM: "adm_wf R F"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    69
  shows "wfrec R F = F (wfrec R F)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    70
proof (rule ext)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    71
  fix x
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    72
  have "wfrec R F x = F (cut (wfrec R F) R x) x"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    73
    using wfrec[of R F] WF by simp
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    74
  also
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    75
  { have "\<And> y. (y,x) \<in> R \<Longrightarrow> (cut (wfrec R F) R x) y = (wfrec R F) y"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    76
      by (auto simp add: cut_apply)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    77
    hence "F (cut (wfrec R F) R x) x = F (wfrec R F) x"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    78
      using ADM adm_wf_def[of R F] by auto }
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    79
  finally show "wfrec R F x = F (wfrec R F) x" .
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    80
qed
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    81
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    82
subsection {* Wellfoundedness of @{text same_fst} *}
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    83
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    84
definition same_fst :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> ('b \<times> 'b) set) \<Rightarrow> (('a \<times> 'b) \<times> ('a \<times> 'b)) set" where
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    85
  "same_fst P R = {((x', y'), (x, y)) . x' = x \<and> P x \<and> (y',y) \<in> R x}"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    86
   --{*For @{const wfrec} declarations where the first n parameters
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    87
       stay unchanged in the recursive call. *}
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    88
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    89
lemma same_fstI [intro!]: "P x \<Longrightarrow> (y', y) \<in> R x \<Longrightarrow> ((x, y'), (x, y)) \<in> same_fst P R"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    90
  by (simp add: same_fst_def)
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    91
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    92
lemma wf_same_fst:
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    93
  assumes prem: "\<And>x. P x \<Longrightarrow> wf (R x)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    94
  shows "wf (same_fst P R)"
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    95
apply (simp cong del: imp_cong add: wf_def same_fst_def)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    96
apply (intro strip)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    97
apply (rename_tac a b)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    98
apply (case_tac "wf (R a)")
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
    99
 apply (erule_tac a = b in wf_induct, blast)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
   100
apply (blast intro: prem)
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
   101
done
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
   102
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents:
diff changeset
   103
end