{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Tree.AVL.Extern.Constructors
( AVL (AVL),
mkAVL,
IsBalancedT (..),
IsBalancedC (..),
IsAlmostBalancedT (..),
)
where
import Data.Kind (Type)
import Data.Proxy (Proxy (Proxy))
import Data.Tree.AVL.Invariants (BalancedHeights, Height)
import Data.Tree.BST.Extern.Constructors (IsBSTC (isBSTT), IsBSTT)
import Data.Tree.ITree (ITree, Tree (EmptyTree, ForkTree))
import Data.Tree.Node (Node)
import Prelude (Bool (True), Show (show), (++))
data AVL :: Tree -> Type where
AVL :: ITree t -> IsBSTT t -> IsBalancedT t -> AVL t
instance Show (AVL t) where
show :: AVL t -> String
show (AVL ITree t
t IsBSTT t
_ IsBalancedT t
_) = String
"AVL $ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ITree t -> String
forall a. Show a => a -> String
show ITree t
t
data IsBalancedT :: Tree -> Type where
EmptyIsBalancedT :: IsBalancedT 'EmptyTree
ForkIsBalancedT ::
(BalancedHeights (Height l) (Height r) n ~ 'True) =>
IsBalancedT l ->
Proxy (Node n a) ->
IsBalancedT r ->
IsBalancedT ('ForkTree l (Node n a) r)
class IsBalancedC (t :: Tree) where
isBalancedT :: IsBalancedT t
instance IsBalancedC 'EmptyTree where
isBalancedT :: IsBalancedT 'EmptyTree
isBalancedT = IsBalancedT 'EmptyTree
EmptyIsBalancedT
instance
(IsBalancedC l, IsBalancedC r, BalancedHeights (Height l) (Height r) n ~ 'True) =>
IsBalancedC ('ForkTree l (Node n a) r)
where
isBalancedT :: IsBalancedT ('ForkTree l (Node n a) r)
isBalancedT = IsBalancedT l
-> Proxy (Node n a)
-> IsBalancedT r
-> IsBalancedT ('ForkTree l (Node n a) r)
forall (l :: Tree) (r :: Tree) (n :: Nat) a.
(BalancedHeights (Height l) (Height r) n ~ 'True) =>
IsBalancedT l
-> Proxy (Node n a)
-> IsBalancedT r
-> IsBalancedT ('ForkTree l (Node n a) r)
ForkIsBalancedT IsBalancedT l
forall (t :: Tree). IsBalancedC t => IsBalancedT t
isBalancedT (forall {k} (t :: k). Proxy t
forall {n :: Nat} {a}. Proxy (Node n a)
Proxy :: Proxy (Node n a)) IsBalancedT r
forall (t :: Tree). IsBalancedC t => IsBalancedT t
isBalancedT
mkAVL :: (IsBSTC t, IsBalancedC t) => ITree t -> AVL t
mkAVL :: forall (t :: Tree). (IsBSTC t, IsBalancedC t) => ITree t -> AVL t
mkAVL ITree t
t = ITree t -> IsBSTT t -> IsBalancedT t -> AVL t
forall (t :: Tree). ITree t -> IsBSTT t -> IsBalancedT t -> AVL t
AVL ITree t
t IsBSTT t
forall (t :: Tree). IsBSTC t => IsBSTT t
isBSTT IsBalancedT t
forall (t :: Tree). IsBalancedC t => IsBalancedT t
isBalancedT
data IsAlmostBalancedT :: Tree -> Type where
ForkIsAlmostBalancedT :: IsBalancedT l -> Proxy (Node n a) -> IsBalancedT r -> IsAlmostBalancedT ('ForkTree l (Node n a) r)