src/HOL/Wfrec.thy
author wenzelm
Mon, 21 Aug 2017 17:15:26 +0200
changeset 66475 d8e0fd64216f
parent 63572 c0cbfd2b5a45
child 69593 3dda49e08b9d
permissions -rw-r--r--
misc updates for release;
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
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 58889
diff changeset
     7
section \<open>Well-Founded Recursion Combinator\<close>
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
     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
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
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
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
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
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    14
  where 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
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    16
definition cut :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    17
  where "cut f R x = (\<lambda>y. if (y, x) \<in> R then f y else undefined)"
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    18
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    19
definition adm_wf :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)) \<Rightarrow> bool"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    20
  where "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
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    22
definition wfrec :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)) \<Rightarrow> ('a \<Rightarrow> 'b)"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    23
  where "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
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    31
text \<open>
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    32
  Inductive characterization of \<open>wfrec\<close> combinator; for details see:
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    33
  John Harrison, "Inductive definitions: automation and application".
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    34
\<close>
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
    35
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    36
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
    37
  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
    38
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    39
lemma wfrec_unique:
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    40
  assumes "adm_wf R F" "wf R"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    41
  shows "\<exists>!y. wfrec_rel R F x y"
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 58889
diff changeset
    42
  using \<open>wf R\<close>
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    43
proof induct
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 61799
diff changeset
    44
  define f where "f y = (THE z. wfrec_rel R F y z)" for y
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    45
  case (less x)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    46
  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
    47
    unfolding f_def by (rule theI_unique)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 58889
diff changeset
    48
  with \<open>adm_wf R F\<close> show ?case
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    49
    by (subst wfrec_rel.simps) (auto simp: adm_wf_def)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    50
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
    51
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    52
lemma adm_lemma: "adm_wf R (\<lambda>f x. F (cut f R x) x)"
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    53
  by (auto simp: adm_wf_def intro!: arg_cong[where f="\<lambda>x. F x y" for y] cuts_eq[THEN iffD2])
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    54
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    55
lemma wfrec: "wf R \<Longrightarrow> wfrec R F a = F (cut (wfrec R F) R a) a"
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    56
  apply (simp add: wfrec_def)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    57
  apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality])
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    58
   apply assumption
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    59
  apply (rule wfrec_rel.wfrecI)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    60
  apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    61
  done
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
    62
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
    63
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    64
text \<open>This form avoids giant explosions in proofs.  NOTE USE OF \<open>\<equiv>\<close>.\<close>
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    65
lemma def_wfrec: "f \<equiv> wfrec R F \<Longrightarrow> wf R \<Longrightarrow> f a = F (cut f R a) a"
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    66
  by (auto intro: wfrec)
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    67
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    68
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 58889
diff changeset
    69
subsubsection \<open>Well-founded recursion via genuine fixpoints\<close>
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
    70
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    71
lemma wfrec_fixpoint:
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    72
  assumes wf: "wf R"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    73
    and adm: "adm_wf R F"
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    74
  shows "wfrec R F = F (wfrec R F)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    75
proof (rule ext)
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    76
  fix x
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    77
  have "wfrec R F x = F (cut (wfrec R F) R x) x"
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    78
    using wfrec[of R F] wf by simp
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    79
  also
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    80
  have "\<And>y. (y, x) \<in> R \<Longrightarrow> cut (wfrec R F) R x y = wfrec R F y"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    81
    by (auto simp add: cut_apply)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    82
  then have "F (cut (wfrec R F) R x) x = F (wfrec R F) x"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    83
    using adm adm_wf_def[of R F] by auto
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    84
  finally show "wfrec R F x = F (wfrec R F) x" .
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    85
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
    86
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    87
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 60758
diff changeset
    88
subsection \<open>Wellfoundedness of \<open>same_fst\<close>\<close>
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
    89
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    90
definition same_fst :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> ('b \<times> 'b) set) \<Rightarrow> (('a \<times> 'b) \<times> ('a \<times> 'b)) set"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    91
  where "same_fst P R = {((x', y'), (x, y)) . x' = x \<and> P x \<and> (y',y) \<in> R x}"
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
    92
   \<comment> \<open>For @{const wfrec} declarations where the first n parameters
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 58889
diff changeset
    93
       stay unchanged in the recursive call.\<close>
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
    94
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    95
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
    96
  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
    97
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
lemma wf_same_fst:
58184
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
    99
  assumes prem: "\<And>x. P x \<Longrightarrow> wf (R x)"
db1381d811ab cleanup Wfrec; introduce dependent_wf/wellorder_choice
hoelzl
parents: 55210
diff changeset
   100
  shows "wf (same_fst P R)"
63572
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   101
  apply (simp cong del: imp_cong add: wf_def same_fst_def)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   102
  apply (intro strip)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   103
  apply (rename_tac a b)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   104
  apply (case_tac "wf (R a)")
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   105
   apply (erule_tac a = b in wf_induct)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   106
   apply blast
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   107
  apply (blast intro: prem)
c0cbfd2b5a45 misc tuning and modernization;
wenzelm
parents: 63040
diff changeset
   108
  done
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
   109
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
   110
end