src/HOL/ex/Abstract_NAT.thy
author haftmann
Tue, 20 Mar 2007 08:27:15 +0100
changeset 22473 753123c89d72
parent 21404 eb85850d3eb7
child 23253 b1f3f53c60b5
permissions -rw-r--r--
explizit "type" superclass
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     1
(*
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     4
*)
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     5
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     6
header {* Abstract Natural Numbers with polymorphic recursion *}
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     7
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     8
theory Abstract_NAT
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
     9
imports Main
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    10
begin
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    11
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    12
text {* Axiomatic Natural Numbers (Peano) -- a monomorphic theory. *}
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    13
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    14
locale NAT =
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    15
  fixes zero :: 'n
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    16
    and succ :: "'n \<Rightarrow> 'n"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    17
  assumes succ_inject [simp]: "(succ m = succ n) = (m = n)"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    18
    and succ_neq_zero [simp]: "succ m \<noteq> zero"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    19
    and induct [case_names zero succ, induct type: 'n]:
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    20
      "P zero \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (succ n)) \<Longrightarrow> P n"
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    21
begin
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    22
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    23
lemma zero_neq_succ [simp]: "zero \<noteq> succ m"
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    24
  by (rule succ_neq_zero [symmetric])
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    25
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    26
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    27
text {* \medskip Primitive recursion as a (functional) relation -- polymorphic! *}
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    28
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    29
inductive2
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    30
  Rec :: "'a \<Rightarrow> ('n \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'n \<Rightarrow> 'a \<Rightarrow> bool"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    31
  for e :: 'a and r :: "'n \<Rightarrow> 'a \<Rightarrow> 'a"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    32
where
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    33
    Rec_zero: "Rec e r zero e"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    34
  | Rec_succ: "Rec e r m n \<Longrightarrow> Rec e r (succ m) (r m n)"
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    35
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    36
lemma Rec_functional:
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    37
  fixes x :: 'n
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    38
  shows "\<exists>!y::'a. Rec e r x y"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    39
proof -
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    40
  let ?R = "Rec e r"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    41
  show ?thesis
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    42
  proof (induct x)
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    43
    case zero
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    44
    show "\<exists>!y. ?R zero y"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    45
    proof
21392
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
    46
      show "?R zero e" ..
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    47
      fix y assume "?R zero y"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    48
      then show "y = e" by cases simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    49
    qed
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    50
  next
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    51
    case (succ m)
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    52
    from `\<exists>!y. ?R m y`
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    53
    obtain y where y: "?R m y"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    54
      and yy': "\<And>y'. ?R m y' \<Longrightarrow> y = y'" by blast
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    55
    show "\<exists>!z. ?R (succ m) z"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    56
    proof
21392
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
    57
      from y show "?R (succ m) (r m y)" ..
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    58
      fix z assume "?R (succ m) z"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    59
      then obtain u where "z = r m u" and "?R m u" by cases simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    60
      with yy' show "z = r m y" by (simp only:)
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    61
    qed
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    62
  qed
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    63
qed
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    64
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    65
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    66
text {* \medskip The recursion operator -- polymorphic! *}
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    67
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    68
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21392
diff changeset
    69
  rec :: "'a \<Rightarrow> ('n \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'n \<Rightarrow> 'a" where
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    70
  "rec e r x = (THE y. Rec e r x y)"
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    71
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    72
lemma rec_eval:
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    73
  assumes Rec: "Rec e r x y"
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    74
  shows "rec e r x = y"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    75
  unfolding rec_def
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    76
  using Rec_functional and Rec by (rule the1_equality)
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    77
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    78
lemma rec_zero [simp]: "rec e r zero = e"
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    79
proof (rule rec_eval)
21392
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
    80
  show "Rec e r zero e" ..
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    81
qed
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    82
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    83
lemma rec_succ [simp]: "rec e r (succ m) = r m (rec e r m)"
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    84
proof (rule rec_eval)
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    85
  let ?R = "Rec e r"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    86
  have "?R m (rec e r m)"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    87
    unfolding rec_def using Rec_functional by (rule theI')
21392
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
    88
  then show "?R (succ m) (r m (rec e r m))" ..
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    89
qed
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    90
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
    91
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    92
text {* \medskip Example: addition (monomorphic) *}
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    93
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    94
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21392
diff changeset
    95
  add :: "'n \<Rightarrow> 'n \<Rightarrow> 'n" where
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    96
  "add m n = rec n (\<lambda>_ k. succ k) m"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    97
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    98
lemma add_zero [simp]: "add zero n = n"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
    99
  and add_succ [simp]: "add (succ m) n = succ (add m n)"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   100
  unfolding add_def by simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   101
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   102
lemma add_assoc: "add (add k m) n = add k (add m n)"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   103
  by (induct k) simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   104
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   105
lemma add_zero_right: "add m zero = m"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   106
  by (induct m) simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   107
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   108
lemma add_succ_right: "add m (succ n) = succ (add m n)"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   109
  by (induct m) simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   110
21392
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
   111
lemma "add (succ (succ (succ zero))) (succ (succ zero)) =
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
   112
    succ (succ (succ (succ (succ zero))))"
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
   113
  by simp
e571e84cbe89 tuned proofs;
wenzelm
parents: 21368
diff changeset
   114
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   115
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   116
text {* \medskip Example: replication (polymorphic) *}
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   117
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   118
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21392
diff changeset
   119
  repl :: "'n \<Rightarrow> 'a \<Rightarrow> 'a list" where
21368
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   120
  "repl n x = rec [] (\<lambda>_ xs. x # xs) n"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   121
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   122
lemma repl_zero [simp]: "repl zero x = []"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   123
  and repl_succ [simp]: "repl (succ n) x = x # repl n x"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   124
  unfolding repl_def by simp_all
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   125
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   126
lemma "repl (succ (succ (succ zero))) True = [True, True, True]"
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   127
  by simp
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   128
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   129
end
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   130
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   131
bffb2240d03f converted to 'inductive2';
wenzelm
parents: 20713
diff changeset
   132
text {* \medskip Just see that our abstract specification makes sense \dots *}
19087
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   133
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   134
interpretation NAT [0 Suc]
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   135
proof (rule NAT.intro)
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   136
  fix m n
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   137
  show "(Suc m = Suc n) = (m = n)" by simp
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   138
  show "Suc m \<noteq> 0" by simp
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   139
  fix P
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   140
  assume zero: "P 0"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   141
    and succ: "\<And>n. P n \<Longrightarrow> P (Suc n)"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   142
  show "P n"
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   143
  proof (induct n)
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   144
    case 0 show ?case by (rule zero)
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   145
  next
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   146
    case Suc then show ?case by (rule succ)
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   147
  qed
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   148
qed
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   149
8d83af663714 Abstract Natural Numbers with polymorphic recursion.
wenzelm
parents:
diff changeset
   150
end