{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Tree.BST.Extern.Constructors
( BST (BST),
mkBST,
IsBSTT (..),
IsBSTC (..),
)
where
import Data.Kind (Type)
import Data.Proxy (Proxy (Proxy))
import Data.Tree.BST.Invariants (GtN, LtN)
import Data.Tree.ITree (ITree, Tree (EmptyTree, ForkTree))
import Data.Tree.Node (Node)
import Prelude (Bool (True), Show (show), (++))
data BST :: Tree -> Type where
BST :: ITree t -> IsBSTT t -> BST t
instance Show (BST t) where
show :: BST t -> String
show (BST ITree t
t IsBSTT t
_) = String
"BST $ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ITree t -> String
forall a. Show a => a -> String
show ITree t
t
data IsBSTT :: Tree -> Type where
EmptyIsBSTT :: IsBSTT 'EmptyTree
ForkIsBSTT ::
(LtN l n ~ 'True, GtN r n ~ 'True) =>
IsBSTT l ->
Proxy (Node n a) ->
IsBSTT r ->
IsBSTT ('ForkTree l (Node n a) r)
class IsBSTC (t :: Tree) where
isBSTT :: IsBSTT t
instance IsBSTC 'EmptyTree where
isBSTT :: IsBSTT 'EmptyTree
isBSTT = IsBSTT 'EmptyTree
EmptyIsBSTT
instance
(IsBSTC l, IsBSTC r, LtN l n ~ 'True, GtN r n ~ 'True) =>
IsBSTC ('ForkTree l (Node n a) r)
where
isBSTT :: IsBSTT ('ForkTree l (Node n a) r)
isBSTT = IsBSTT l
-> Proxy (Node n a)
-> IsBSTT r
-> IsBSTT ('ForkTree l (Node n a) r)
forall (l :: Tree) (n :: Nat) (r :: Tree) a.
(LtN l n ~ 'True, GtN r n ~ 'True) =>
IsBSTT l
-> Proxy (Node n a)
-> IsBSTT r
-> IsBSTT ('ForkTree l (Node n a) r)
ForkIsBSTT IsBSTT l
forall (t :: Tree). IsBSTC t => IsBSTT t
isBSTT (forall {k} (t :: k). Proxy t
forall {n :: Nat} {a}. Proxy (Node n a)
Proxy :: Proxy (Node n a)) IsBSTT r
forall (t :: Tree). IsBSTC t => IsBSTT t
isBSTT
mkBST :: (IsBSTC t) => ITree t -> BST t
mkBST :: forall (t :: Tree). IsBSTC t => ITree t -> BST t
mkBST ITree t
t = ITree t -> IsBSTT t -> BST t
forall (t :: Tree). ITree t -> IsBSTT t -> BST t
BST ITree t
t IsBSTT t
forall (t :: Tree). IsBSTC t => IsBSTT t
isBSTT