module Data.Forest.StructuredPaired where
import Control.Lens
import Data.Bifoldable
import Data.Bifunctor
import Data.Bitraversable
import Data.Monoid
import GHC.Generics (Generic)
import Data.Forest.Static
data SPForest r t
= SPR r
| SPT t (SPForest r t) t
| SPJ [SPForest r t]
| SPE
deriving (Read,Show,Eq,Ord,Generic)
makePrisms ''SPForest
instance Bifunctor SPForest where
first f = \case
SPR r → SPR (f r)
SPT l t r → SPT l (first f t) r
SPJ xs → SPJ (map (first f) xs)
SPE → SPE
{-# Inlinable first #-}
second g = \case
SPR r → SPR r
SPT l t r → SPT (g l) (second g t) (g r)
SPJ xs → SPJ (map (second g) xs)
SPE → SPE
{-# Inlinable second #-}
bimap f g = \case
SPR r → SPR (f r)
SPT l t r → SPT (g l) (bimap f g t) (g r)
SPJ xs → SPJ (map (bimap f g) xs)
SPE → SPE
{-# Inlinable bimap #-}
instance Bifoldable SPForest where
bifoldMap f g = \case
SPR r → f r
SPT l t r → g l <> bifoldMap f g t <> g r
SPJ xs → error "Bifoldable"
SPE → mempty
{-# Inlinable bifoldMap #-}
instance Bitraversable SPForest where
bitraverse f g = \case
SPR r → SPR <$> f r
SPT l t r → SPT <$> g l <*> bitraverse f g t <*> g r
SPJ xs → error "Bitraversable"
SPE → pure SPE
{-# Inlinable bitraverse #-}
toStaticForest ∷ SPForest r t → Forest p v a
toStaticForest = undefined