Copyright | (c) Nicolás Rodríguez 2021 |
---|---|
License | GPL-3 |
Maintainer | Nicolás Rodríguez |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe |
Language | Haskell2010 |
Type level restrictions for the key ordering in type safe AVL trees.
Documentation
Data type that represents the state of balance of the sub trees in a balanced tree:
LeftHeavy
height(left sub tree) = height(right sub tree) + 1
RightHeavy
height(right sub tree) = height(left sub tree) + 1
Balanced
height(left sub tree) = height(right sub tree)
type family BalancedState (h1 :: Nat) (h2 :: Nat) :: BS where ... Source #
Check from two type level natural numbers, that represent the heights of some left and right sub trees, if some of those sub trees have height larger than the other.
BalancedState 0 0 = 'Balanced | |
BalancedState 1 0 = 'LeftHeavy | |
BalancedState 0 1 = 'RightHeavy | |
BalancedState h1 h2 = BalancedState (h1 - 1) (h2 - 1) |
type family BalancedHeights (h1 :: Nat) (h2 :: Nat) (k :: Nat) :: Bool where ... Source #
Check if two type level natural numbers, that represent the heights of some left and right sub trees, differ at most in one (i.e., the tree is balanced).
BalancedHeights 0 0 _k = 'True | |
BalancedHeights 1 0 _k = 'True | |
BalancedHeights _h1 0 k = TypeError (('Text "The left sub tree at node with key " :<>: 'ShowType k) :<>: 'Text " has +2 greater height!") | |
BalancedHeights 0 1 _k = 'True | |
BalancedHeights 0 _h2 k = TypeError (('Text "The right sub tree at node with key " :<>: 'ShowType k) :<>: 'Text " has +2 greater height!") | |
BalancedHeights h1 h2 k = BalancedHeights (h1 - 1) (h2 - 1) k |
Data type that represents the state of unbalance of the sub trees:
LeftUnbalanced
height(left sub tree) = height(right sub tree) + 2
RightUnbalanced
height(right sub tree) = height(left sub tree) + 2
NotUnbalanced
tree is not unbalanced
type family UnbalancedState (h1 :: Nat) (h2 :: Nat) :: US where ... Source #
Check from two type level natural numbers, that represent the heights of some left and right sub trees, if the tree is balanced or if some of those sub trees is unbalanced.
UnbalancedState 0 0 = 'NotUnbalanced | |
UnbalancedState 1 0 = 'NotUnbalanced | |
UnbalancedState 0 1 = 'NotUnbalanced | |
UnbalancedState 2 0 = 'LeftUnbalanced | |
UnbalancedState 0 2 = 'RightUnbalanced | |
UnbalancedState h1 h2 = UnbalancedState (h1 - 1) (h2 - 1) |