src/HOL/ex/Tree23.thy
author haftmann
Fri, 11 Jun 2010 17:14:02 +0200
changeset 37407 61dd8c145da7
parent 33436 0b5f07dd68f5
child 42463 f270e3e18be5
permissions -rw-r--r--
declare lex_prod_def [code del]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33436
nipkow
parents:
diff changeset
     1
(*  Title:      HOL/ex/Tree23.thy
nipkow
parents:
diff changeset
     2
    Author:     Tobias Nipkow, TU Muenchen
nipkow
parents:
diff changeset
     3
*)
nipkow
parents:
diff changeset
     4
nipkow
parents:
diff changeset
     5
header {* 2-3 Trees *}
nipkow
parents:
diff changeset
     6
nipkow
parents:
diff changeset
     7
theory Tree23
nipkow
parents:
diff changeset
     8
imports Main
nipkow
parents:
diff changeset
     9
begin
nipkow
parents:
diff changeset
    10
nipkow
parents:
diff changeset
    11
text{* This is a very direct translation of some of the functions in table.ML
nipkow
parents:
diff changeset
    12
in the Isabelle source code. That source is due to Makarius Wenzel and Stefan
nipkow
parents:
diff changeset
    13
Berghofer.
nipkow
parents:
diff changeset
    14
nipkow
parents:
diff changeset
    15
So far this file contains only data types and functions, but no proofs. Feel
nipkow
parents:
diff changeset
    16
free to have a go at the latter!
nipkow
parents:
diff changeset
    17
nipkow
parents:
diff changeset
    18
Note that because of complicated patterns and mutual recursion, these
nipkow
parents:
diff changeset
    19
function definitions take a few minutes and can also be seen as stress tests
nipkow
parents:
diff changeset
    20
for the function definition facility.  *}
nipkow
parents:
diff changeset
    21
nipkow
parents:
diff changeset
    22
types key = int -- {*for simplicity, should be a type class*}
nipkow
parents:
diff changeset
    23
nipkow
parents:
diff changeset
    24
datatype ord = LESS | EQUAL | GREATER
nipkow
parents:
diff changeset
    25
nipkow
parents:
diff changeset
    26
definition "ord i j = (if i<j then LESS else if i=j then EQUAL else GREATER)"
nipkow
parents:
diff changeset
    27
nipkow
parents:
diff changeset
    28
datatype 'a tree23 =
nipkow
parents:
diff changeset
    29
  Empty |
nipkow
parents:
diff changeset
    30
  Branch2 "'a tree23" "key * 'a" "'a tree23" |
nipkow
parents:
diff changeset
    31
  Branch3 "'a tree23" "key * 'a" "'a tree23" "key * 'a" "'a tree23"
nipkow
parents:
diff changeset
    32
nipkow
parents:
diff changeset
    33
datatype 'a growth =
nipkow
parents:
diff changeset
    34
  Stay "'a tree23" |
nipkow
parents:
diff changeset
    35
  Sprout "'a tree23" "key * 'a" "'a tree23"
nipkow
parents:
diff changeset
    36
nipkow
parents:
diff changeset
    37
fun add :: "key \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a growth" where
nipkow
parents:
diff changeset
    38
"add key y Empty = Sprout Empty (key,y) Empty" |
nipkow
parents:
diff changeset
    39
"add key y (Branch2 left (k,x) right) =
nipkow
parents:
diff changeset
    40
   (case ord key k of
nipkow
parents:
diff changeset
    41
      LESS =>
nipkow
parents:
diff changeset
    42
        (case add key y left of
nipkow
parents:
diff changeset
    43
           Stay left' => Stay (Branch2 left' (k,x) right)
nipkow
parents:
diff changeset
    44
         | Sprout left1 q left2
nipkow
parents:
diff changeset
    45
           => Stay (Branch3 left1 q left2 (k,x) right))
nipkow
parents:
diff changeset
    46
    | EQUAL => Stay (Branch2 left (k,y) right)
nipkow
parents:
diff changeset
    47
    | GREATER =>
nipkow
parents:
diff changeset
    48
        (case add key y right of
nipkow
parents:
diff changeset
    49
           Stay right' => Stay (Branch2 left (k,x) right')
nipkow
parents:
diff changeset
    50
         | Sprout right1 q right2
nipkow
parents:
diff changeset
    51
           => Stay (Branch3 left (k,x) right1 q right2)))" |
nipkow
parents:
diff changeset
    52
"add key y (Branch3 left (k1,x1) mid (k2,x2) right) =
nipkow
parents:
diff changeset
    53
   (case ord key k1 of
nipkow
parents:
diff changeset
    54
      LESS =>
nipkow
parents:
diff changeset
    55
        (case add key y left of
nipkow
parents:
diff changeset
    56
           Stay left' => Stay (Branch3 left' (k1,x1) mid (k2,x2) right)
nipkow
parents:
diff changeset
    57
         | Sprout left1 q left2
nipkow
parents:
diff changeset
    58
           => Sprout (Branch2 left1 q left2) (k1,x1) (Branch2 mid (k2,x2) right))
nipkow
parents:
diff changeset
    59
    | EQUAL => Stay (Branch3 left (k1,y) mid (k2,x2) right)
nipkow
parents:
diff changeset
    60
    | GREATER =>
nipkow
parents:
diff changeset
    61
        (case ord key k2 of
nipkow
parents:
diff changeset
    62
           LESS =>
nipkow
parents:
diff changeset
    63
             (case add key y mid of
nipkow
parents:
diff changeset
    64
                Stay mid' => Stay (Branch3 left (k1,x1) mid' (k2,x2) right)
nipkow
parents:
diff changeset
    65
              | Sprout mid1 q mid2
nipkow
parents:
diff changeset
    66
                => Sprout (Branch2 left (k1,x1) mid1) q (Branch2 mid2 (k2,x2) right))
nipkow
parents:
diff changeset
    67
         | EQUAL => Stay (Branch3 left (k1,x1) mid (k2,y) right)
nipkow
parents:
diff changeset
    68
         | GREATER =>
nipkow
parents:
diff changeset
    69
             (case add key y right of
nipkow
parents:
diff changeset
    70
                Stay right' => Stay (Branch3 left (k1,x1) mid (k2,x2) right')
nipkow
parents:
diff changeset
    71
              | Sprout right1 q right2
nipkow
parents:
diff changeset
    72
                => Sprout (Branch2 left (k1,x1) mid) (k2,x2) (Branch2 right1 q right2))))"
nipkow
parents:
diff changeset
    73
nipkow
parents:
diff changeset
    74
definition add0 :: "key \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
nipkow
parents:
diff changeset
    75
"add0 k y t =
nipkow
parents:
diff changeset
    76
  (case add k y t of Stay t' => t' | Sprout l p r => Branch2 l p r)"
nipkow
parents:
diff changeset
    77
nipkow
parents:
diff changeset
    78
value "add0 5 e (add0 4 d (add0 3 c (add0 2 b (add0 1 a Empty))))"
nipkow
parents:
diff changeset
    79
nipkow
parents:
diff changeset
    80
fun compare where
nipkow
parents:
diff changeset
    81
"compare None (k2, _) = LESS" |
nipkow
parents:
diff changeset
    82
"compare (Some k1) (k2, _) = ord k1 k2"
nipkow
parents:
diff changeset
    83
nipkow
parents:
diff changeset
    84
fun if_eq where
nipkow
parents:
diff changeset
    85
"if_eq EQUAL x y = x" |
nipkow
parents:
diff changeset
    86
"if_eq _ x y = y"
nipkow
parents:
diff changeset
    87
nipkow
parents:
diff changeset
    88
fun del :: "key option \<Rightarrow> 'a tree23 \<Rightarrow> ((key * 'a) * bool * 'a tree23)option"
nipkow
parents:
diff changeset
    89
where
nipkow
parents:
diff changeset
    90
"del (Some k) Empty = None" |
nipkow
parents:
diff changeset
    91
"del None (Branch2 Empty p Empty) = Some(p, (True, Empty))" |
nipkow
parents:
diff changeset
    92
"del None (Branch3 Empty p Empty q Empty) = Some(p, (False, Branch2 Empty q Empty))" |
nipkow
parents:
diff changeset
    93
"del k (Branch2 Empty p Empty) = (case compare k p of
nipkow
parents:
diff changeset
    94
      EQUAL => Some(p, (True, Empty)) | _ => None)" |
nipkow
parents:
diff changeset
    95
"del k (Branch3 Empty p Empty q Empty) = (case compare k p of
nipkow
parents:
diff changeset
    96
      EQUAL => Some(p, (False, Branch2 Empty q Empty))
nipkow
parents:
diff changeset
    97
    | _ => (case compare k q of
nipkow
parents:
diff changeset
    98
        EQUAL => Some(q, (False, Branch2 Empty p Empty))
nipkow
parents:
diff changeset
    99
      | _ => None))" |
nipkow
parents:
diff changeset
   100
"del k (Branch2 l p r) = (case compare k p of
nipkow
parents:
diff changeset
   101
      LESS => (case del k l of None \<Rightarrow> None |
nipkow
parents:
diff changeset
   102
        Some(p', (False, l')) => Some(p', (False, Branch2 l' p r))
nipkow
parents:
diff changeset
   103
      | Some(p', (True, l')) => Some(p', case r of
nipkow
parents:
diff changeset
   104
          Branch2 rl rp rr => (True, Branch3 l' p rl rp rr)
nipkow
parents:
diff changeset
   105
        | Branch3 rl rp rm rq rr => (False, Branch2
nipkow
parents:
diff changeset
   106
            (Branch2 l' p rl) rp (Branch2 rm rq rr))))
nipkow
parents:
diff changeset
   107
    | or => (case del (if_eq or None k) r of None \<Rightarrow> None |
nipkow
parents:
diff changeset
   108
        Some(p', (False, r')) => Some(p', (False, Branch2 l (if_eq or p' p) r'))
nipkow
parents:
diff changeset
   109
      | Some(p', (True, r')) => Some(p', case l of
nipkow
parents:
diff changeset
   110
          Branch2 ll lp lr => (True, Branch3 ll lp lr (if_eq or p' p) r')
nipkow
parents:
diff changeset
   111
        | Branch3 ll lp lm lq lr => (False, Branch2
nipkow
parents:
diff changeset
   112
            (Branch2 ll lp lm) lq (Branch2 lr (if_eq or p' p) r')))))" |
nipkow
parents:
diff changeset
   113
"del k (Branch3 l p m q r) = (case compare k q of
nipkow
parents:
diff changeset
   114
      LESS => (case compare k p of
nipkow
parents:
diff changeset
   115
        LESS => (case del k l of None \<Rightarrow> None |
nipkow
parents:
diff changeset
   116
          Some(p', (False, l')) => Some(p', (False, Branch3 l' p m q r))
nipkow
parents:
diff changeset
   117
        | Some(p', (True, l')) => Some(p', (False, case (m, r) of
nipkow
parents:
diff changeset
   118
            (Branch2 ml mp mr, Branch2 _ _ _) => Branch2 (Branch3 l' p ml mp mr) q r
nipkow
parents:
diff changeset
   119
          | (Branch3 ml mp mm mq mr, _) => Branch3 (Branch2 l' p ml) mp (Branch2 mm mq mr) q r
nipkow
parents:
diff changeset
   120
          | (Branch2 ml mp mr, Branch3 rl rp rm rq rr) =>
nipkow
parents:
diff changeset
   121
              Branch3 (Branch2 l' p ml) mp (Branch2 mr q rl) rp (Branch2 rm rq rr))))
nipkow
parents:
diff changeset
   122
      | or => (case del (if_eq or None k) m of None \<Rightarrow> None |
nipkow
parents:
diff changeset
   123
          Some(p', (False, m')) => Some(p', (False, Branch3 l (if_eq or p' p) m' q r))
nipkow
parents:
diff changeset
   124
        | Some(p', (True, m')) => Some(p', (False, case (l, r) of
nipkow
parents:
diff changeset
   125
            (Branch2 ll lp lr, Branch2 _ _ _) => Branch2 (Branch3 ll lp lr (if_eq or p' p) m') q r
nipkow
parents:
diff changeset
   126
          | (Branch3 ll lp lm lq lr, _) => Branch3 (Branch2 ll lp lm) lq (Branch2 lr (if_eq or p' p) m') q r
nipkow
parents:
diff changeset
   127
          | (_, Branch3 rl rp rm rq rr) => Branch3 l (if_eq or p' p) (Branch2 m' q rl) rp (Branch2 rm rq rr)))))
nipkow
parents:
diff changeset
   128
    | or => (case del (if_eq or None k) r of None \<Rightarrow> None |
nipkow
parents:
diff changeset
   129
        Some(q', (False, r')) => Some(q', (False, Branch3 l p m (if_eq or q' q) r'))
nipkow
parents:
diff changeset
   130
      | Some(q', (True, r')) => Some(q', (False, case (l, m) of
nipkow
parents:
diff changeset
   131
          (Branch2 _ _ _, Branch2 ml mp mr) => Branch2 l p (Branch3 ml mp mr (if_eq or q' q) r')
nipkow
parents:
diff changeset
   132
        | (_, Branch3 ml mp mm mq mr) => Branch3 l p (Branch2 ml mp mm) mq (Branch2 mr (if_eq or q' q) r')
nipkow
parents:
diff changeset
   133
        | (Branch3 ll lp lm lq lr, Branch2 ml mp mr) =>
nipkow
parents:
diff changeset
   134
            Branch3 (Branch2 ll lp lm) lq (Branch2 lr p ml) mp (Branch2 mr (if_eq or q' q) r')))))"
nipkow
parents:
diff changeset
   135
nipkow
parents:
diff changeset
   136
definition del0 :: "key \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
nipkow
parents:
diff changeset
   137
"del0 k t = (case del (Some k) t of None \<Rightarrow> t | Some(_,(_,t')) \<Rightarrow> t')"
nipkow
parents:
diff changeset
   138
nipkow
parents:
diff changeset
   139
nipkow
parents:
diff changeset
   140
(* yes, this is rather slow *)
nipkow
parents:
diff changeset
   141
fun ord0 :: "'a tree23 \<Rightarrow> bool"
nipkow
parents:
diff changeset
   142
and ordl :: "key \<Rightarrow> 'a tree23 \<Rightarrow> bool"
nipkow
parents:
diff changeset
   143
and ordr :: "'a tree23 \<Rightarrow> key \<Rightarrow> bool"
nipkow
parents:
diff changeset
   144
and ordlr :: "key \<Rightarrow> 'a tree23 \<Rightarrow> key \<Rightarrow> bool"
nipkow
parents:
diff changeset
   145
where
nipkow
parents:
diff changeset
   146
"ord0 Empty = True" |
nipkow
parents:
diff changeset
   147
"ord0 (Branch2 l p r)  = (ordr l (fst p) & ordl (fst p) r)" |
nipkow
parents:
diff changeset
   148
"ord0 (Branch3 l p m q r)  = (ordr l (fst p) & ordlr (fst p) m (fst q) & ordl (fst q) r)" |
nipkow
parents:
diff changeset
   149
nipkow
parents:
diff changeset
   150
"ordl _ Empty = True" |
nipkow
parents:
diff changeset
   151
"ordl x (Branch2 l p r)  = (ordlr x l (fst p) & ordl (fst p) r)" |
nipkow
parents:
diff changeset
   152
"ordl x (Branch3 l p m q r)  = (ordlr x l (fst p) & ordlr (fst p) m (fst q) & ordl (fst q) r)" |
nipkow
parents:
diff changeset
   153
nipkow
parents:
diff changeset
   154
"ordr Empty _ = True" |
nipkow
parents:
diff changeset
   155
"ordr (Branch2 l p r) x = (ordr l (fst p) & ordlr (fst p) r x)" |
nipkow
parents:
diff changeset
   156
"ordr (Branch3 l p m q r) x = (ordr l (fst p) & ordlr (fst p) m (fst q) & ordlr (fst q) r x)" |
nipkow
parents:
diff changeset
   157
nipkow
parents:
diff changeset
   158
"ordlr x Empty y = (x < y)" |
nipkow
parents:
diff changeset
   159
"ordlr x (Branch2 l p r) y = (ordlr x l (fst p) & ordlr (fst p) r y)" |
nipkow
parents:
diff changeset
   160
"ordlr x (Branch3 l p m q r) y = (ordlr x l (fst p) & ordlr (fst p) m (fst q) & ordlr (fst q) r y)"
nipkow
parents:
diff changeset
   161
nipkow
parents:
diff changeset
   162
fun height :: "'a tree23 \<Rightarrow> nat" where
nipkow
parents:
diff changeset
   163
"height Empty = 0" |
nipkow
parents:
diff changeset
   164
"height (Branch2 l _ r) = Suc(max (height l) (height r))" |
nipkow
parents:
diff changeset
   165
"height (Branch3 l _ m _ r) = Suc(max (height l) (max (height m) (height r)))"
nipkow
parents:
diff changeset
   166
nipkow
parents:
diff changeset
   167
text{* Is a tree balanced? *}
nipkow
parents:
diff changeset
   168
fun bal :: "'a tree23 \<Rightarrow> bool" where
nipkow
parents:
diff changeset
   169
"bal Empty = True" |
nipkow
parents:
diff changeset
   170
"bal (Branch2 l _ r) = (bal l & bal r & height l = height r)" |
nipkow
parents:
diff changeset
   171
"bal (Branch3 l _ m _ r) = (bal l & bal m & bal r & height l = height m & height m = height r)"
nipkow
parents:
diff changeset
   172
nipkow
parents:
diff changeset
   173
text{* This is a little test harness and should be commented out once the
nipkow
parents:
diff changeset
   174
above functions have been proved correct. *}
nipkow
parents:
diff changeset
   175
nipkow
parents:
diff changeset
   176
datatype 'a act = Add int 'a | Del int
nipkow
parents:
diff changeset
   177
nipkow
parents:
diff changeset
   178
fun exec where
nipkow
parents:
diff changeset
   179
"exec [] t = t" |
nipkow
parents:
diff changeset
   180
"exec (Add k x # as) t = exec as (add0 k x t)" |
nipkow
parents:
diff changeset
   181
"exec (Del k # as) t = exec as (del0 k t)"
nipkow
parents:
diff changeset
   182
nipkow
parents:
diff changeset
   183
text{* Some quick checks: *}
nipkow
parents:
diff changeset
   184
nipkow
parents:
diff changeset
   185
lemma "ord0(exec as Empty)"
nipkow
parents:
diff changeset
   186
quickcheck
nipkow
parents:
diff changeset
   187
oops
nipkow
parents:
diff changeset
   188
nipkow
parents:
diff changeset
   189
lemma "bal(exec as Empty)"
nipkow
parents:
diff changeset
   190
quickcheck
nipkow
parents:
diff changeset
   191
oops
nipkow
parents:
diff changeset
   192
nipkow
parents:
diff changeset
   193
end