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