src/HOL/Data_Structures/RBT.thy
changeset 64960 8be78855ee7a
parent 64952 f11e974b47e0
child 67910 b42473502373
equal deleted inserted replaced
64959:9ca021bd718d 64960:8be78855ee7a
    11 type_synonym 'a rbt = "('a,color)tree"
    11 type_synonym 'a rbt = "('a,color)tree"
    12 
    12 
    13 abbreviation R where "R l a r \<equiv> Node Red l a r"
    13 abbreviation R where "R l a r \<equiv> Node Red l a r"
    14 abbreviation B where "B l a r \<equiv> Node Black l a r"
    14 abbreviation B where "B l a r \<equiv> Node Black l a r"
    15 
    15 
    16 fun bal :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    16 fun baliL :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    17 "bal (R (R t1 a1 t2) a2 t3) a3 t4 = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    17 "baliL (R (R t1 a1 t2) a2 t3) a3 t4 = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    18 "bal (R t1 a1 (R t2 a2 t3)) a3 t4 = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    18 "baliL (R t1 a1 (R t2 a2 t3)) a3 t4 = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    19 "bal t1 a1 (R (R t2 a2 t3) a3 t4) = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    19 "baliL t1 a t2 = B t1 a t2"
    20 "bal t1 a1 (R t2 a2 (R t3 a3 t4)) = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    20 
    21 "bal t1 a t2 = B t1 a t2"
    21 fun baliR :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    22 (* Warning: takes 30 secs to compile (2017) *)
    22 "baliR t1 a1 (R (R t2 a2 t3) a3 t4) = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    23 text\<open>Markus Reiter and Alexander Krauss added a first line not found in Okasaki:
    23 "baliR t1 a1 (R t2 a2 (R t3 a3 t4)) = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
    24 @{prop "bal (R t1 a1 t2) a2 (R t3 a3 t4) = R (B t1 a1 t2) a2 (B t3 a3 t4)"}
    24 "baliR t1 a t2 = B t1 a t2"
    25 The motivation is unclear. However: it speeds up pattern compilation
       
    26 and lemma \<open>invc_bal\<close> below can be simplified to
       
    27   @{prop "\<lbrakk>invc_sons l; invc_sons r\<rbrakk> \<Longrightarrow> invc (bal l a r)"}
       
    28 All other proofs are unaffected.\<close>
       
    29 
    25 
    30 fun paint :: "color \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    26 fun paint :: "color \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    31 "paint c Leaf = Leaf" |
    27 "paint c Leaf = Leaf" |
    32 "paint c (Node _ l a r) = Node c l a r"
    28 "paint c (Node _ l a r) = Node c l a r"
    33 
    29 
    34 fun balL :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    30 fun baldL :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    35 "balL (R t1 x t2) y t3 = R (B t1 x t2) y t3" |
    31 "baldL (R t1 x t2) y t3 = R (B t1 x t2) y t3" |
    36 "balL bl x (B t1 y t2) = bal bl x (R t1 y t2)" |
    32 "baldL bl x (B t1 y t2) = baliR bl x (R t1 y t2)" |
    37 "balL bl x (R (B t1 y t2) z t3) = R (B bl x t1) y (bal t2 z (paint Red t3))" |
    33 "baldL bl x (R (B t1 y t2) z t3) = R (B bl x t1) y (baliR t2 z (paint Red t3))" |
    38 "balL t1 x t2 = R t1 x t2"
    34 "baldL t1 x t2 = R t1 x t2"
    39 
    35 
    40 fun balR :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    36 fun baldR :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    41 "balR t1 x (R t2 y t3) = R t1 x (B t2 y t3)" |
    37 "baldR t1 x (R t2 y t3) = R t1 x (B t2 y t3)" |
    42 "balR (B t1 x t2) y t3 = bal (R t1 x t2) y t3" |
    38 "baldR (B t1 x t2) y t3 = baliL (R t1 x t2) y t3" |
    43 "balR (R t1 x (B t2 y t3)) z t4 = R (bal (paint Red t1) x t2) y (B t3 z t4)" |
    39 "baldR (R t1 x (B t2 y t3)) z t4 = R (baliL (paint Red t1) x t2) y (B t3 z t4)" |
    44 "balR t1 x t2 = R t1 x t2"
    40 "baldR t1 x t2 = R t1 x t2"
    45 
    41 
    46 fun combine :: "'a rbt \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    42 fun combine :: "'a rbt \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    47 "combine Leaf t = t" |
    43 "combine Leaf t = t" |
    48 "combine t Leaf = t" |
    44 "combine t Leaf = t" |
    49 "combine (R t1 a t2) (R t3 c t4) =
    45 "combine (R t1 a t2) (R t3 c t4) =
    51      R u2 b u3 \<Rightarrow> (R (R t1 a u2) b (R u3 c t4)) |
    47      R u2 b u3 \<Rightarrow> (R (R t1 a u2) b (R u3 c t4)) |
    52      t23 \<Rightarrow> R t1 a (R t23 c t4))" |
    48      t23 \<Rightarrow> R t1 a (R t23 c t4))" |
    53 "combine (B t1 a t2) (B t3 c t4) =
    49 "combine (B t1 a t2) (B t3 c t4) =
    54   (case combine t2 t3 of
    50   (case combine t2 t3 of
    55      R t2' b t3' \<Rightarrow> R (B t1 a t2') b (B t3' c t4) |
    51      R t2' b t3' \<Rightarrow> R (B t1 a t2') b (B t3' c t4) |
    56      t23 \<Rightarrow> balL t1 a (B t23 c t4))" |
    52      t23 \<Rightarrow> baldL t1 a (B t23 c t4))" |
    57 "combine t1 (R t2 a t3) = R (combine t1 t2) a t3" |
    53 "combine t1 (R t2 a t3) = R (combine t1 t2) a t3" |
    58 "combine (R t1 a t2) t3 = R t1 a (combine t2 t3)" 
    54 "combine (R t1 a t2) t3 = R t1 a (combine t2 t3)" 
    59 
    55 
    60 end
    56 end