src/ZF/Induct/Binary_Trees.thy
author wenzelm
Wed, 14 Nov 2001 23:22:43 +0100
changeset 12194 13909cb72129
child 14157 8bf06363bbb5
permissions -rw-r--r--
converted datatype examples moved from ZF/ex to ZF/Induct;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12194
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     1
(*  Title:      ZF/Induct/Binary_Trees.thy
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     4
    Copyright   1992  University of Cambridge
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     5
*)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     6
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     7
header {* Binary trees *}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     8
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
     9
theory Binary_Trees = Main:
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    10
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    11
subsection {* Datatype definition *}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    12
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    13
consts
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    14
  bt :: "i => i"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    15
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    16
datatype "bt(A)" =
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    17
  Lf | Br ("a \<in> A", "t1 \<in> bt(A)", "t2 \<in> bt(A)")
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    18
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    19
declare bt.intros [simp]
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    20
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    21
lemma Br_neq_left: "l \<in> bt(A) ==> (!!x r. Br(x, l, r) \<noteq> l)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    22
  by (induct set: bt) auto
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    23
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    24
lemma Br_iff: "Br(a, l, r) = Br(a', l', r') <-> a = a' & l = l' & r = r'"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    25
  -- "Proving a freeness theorem."
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    26
  by (fast elim!: bt.free_elims)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    27
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    28
inductive_cases BrE: "Br(a, l, r) \<in> bt(A)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    29
  -- "An elimination rule, for type-checking."
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    30
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    31
text {*
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    32
  \medskip Lemmas to justify using @{term bt} in other recursive type
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    33
  definitions.
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    34
*}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    35
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    36
lemma bt_mono: "A \<subseteq> B ==> bt(A) \<subseteq> bt(B)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    37
  apply (unfold bt.defs)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    38
  apply (rule lfp_mono)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    39
    apply (rule bt.bnd_mono)+
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    40
  apply (rule univ_mono basic_monos | assumption)+
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    41
  done
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    42
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    43
lemma bt_univ: "bt(univ(A)) \<subseteq> univ(A)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    44
  apply (unfold bt.defs bt.con_defs)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    45
  apply (rule lfp_lowerbound)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    46
   apply (rule_tac [2] A_subset_univ [THEN univ_mono])
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    47
  apply (fast intro!: zero_in_univ Inl_in_univ Inr_in_univ Pair_in_univ)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    48
  done
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    49
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    50
lemma bt_subset_univ: "A \<subseteq> univ(B) ==> bt(A) \<subseteq> univ(B)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    51
  apply (rule subset_trans)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    52
   apply (erule bt_mono)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    53
  apply (rule bt_univ)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    54
  done
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    55
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    56
lemma bt_rec_type:
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    57
  "[| t \<in> bt(A);
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    58
    c \<in> C(Lf);
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    59
    !!x y z r s. [| x \<in> A;  y \<in> bt(A);  z \<in> bt(A);  r \<in> C(y);  s \<in> C(z) |] ==>
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    60
    h(x, y, z, r, s) \<in> C(Br(x, y, z))
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    61
  |] ==> bt_rec(c, h, t) \<in> C(t)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    62
  -- {* Type checking for recursor -- example only; not really needed. *}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    63
  apply (induct_tac t)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    64
   apply simp_all
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    65
  done
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    66
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    67
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    68
subsection {* Number of nodes *}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    69
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    70
consts
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    71
  n_nodes :: "i => i"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    72
primrec
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    73
  "n_nodes(Lf) = 0"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    74
  "n_nodes(Br(a, l, r)) = succ(n_nodes(l) #+ n_nodes(r))"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    75
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    76
lemma n_nodes_type [simp]: "t \<in> bt(A) ==> n_nodes(t) \<in> nat"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    77
  by (induct_tac t) auto
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    78
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    79
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    80
subsection {* Number of leaves *}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    81
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    82
consts
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    83
  n_leaves :: "i => i"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    84
primrec
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    85
  "n_leaves(Lf) = 1"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    86
  "n_leaves(Br(a, l, r)) = n_leaves(l) #+ n_leaves(r)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    87
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    88
lemma n_leaves_type [simp]: "t \<in> bt(A) ==> n_leaves(t) \<in> nat"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    89
  by (induct_tac t) auto
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    90
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    91
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    92
subsection {* Reflecting trees *}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    93
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    94
consts
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    95
  bt_reflect :: "i => i"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    96
primrec
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    97
  "bt_reflect(Lf) = Lf"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    98
  "bt_reflect(Br(a, l, r)) = Br(a, bt_reflect(r), bt_reflect(l))"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
    99
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   100
lemma bt_reflect_type [simp]: "t \<in> bt(A) ==> bt_reflect(t) \<in> bt(A)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   101
  by (induct_tac t) auto
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   102
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   103
text {*
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   104
  \medskip Theorems about @{term n_leaves}.
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   105
*}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   106
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   107
lemma n_leaves_reflect: "t \<in> bt(A) ==> n_leaves(bt_reflect(t)) = n_leaves(t)"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   108
  by (induct_tac t) (simp_all add: add_commute n_leaves_type)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   109
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   110
lemma n_leaves_nodes: "t \<in> bt(A) ==> n_leaves(t) = succ(n_nodes(t))"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   111
  by (induct_tac t) (simp_all add: add_succ_right)
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   112
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   113
text {*
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   114
  Theorems about @{term bt_reflect}.
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   115
*}
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   116
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   117
lemma bt_reflect_bt_reflect_ident: "t \<in> bt(A) ==> bt_reflect(bt_reflect(t)) = t"
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   118
  by (induct_tac t) simp_all
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   119
13909cb72129 converted datatype examples moved from ZF/ex to ZF/Induct;
wenzelm
parents:
diff changeset
   120
end