{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
module Data.JoinSemilattice.Class.FlatMapping where
import Data.JoinSemilattice.Class.Zipping (Zipping)
import Data.JoinSemilattice.Defined (Defined (..))
import Data.JoinSemilattice.Intersect (Intersect (..), Intersectable)
import qualified Data.JoinSemilattice.Intersect as Intersect
import Data.Kind (Constraint, Type)
import Prelude hiding (unzip)
class Zipping f c => FlatMapping (f :: Type -> Type) (c :: Type -> Constraint) | f -> c where
flatMapR :: (c x, c y) => (Maybe (x -> f y), Maybe (f y -> x)) -> ((f x, f y) -> (f x, f y))
instance FlatMapping Defined Eq where
flatMapR ( fs, gs ) ( xs, ys )
= ( case gs of Just g -> Exactly (g ys)
Nothing -> mempty
, case xs of
Unknown -> Unknown
Conflict -> Conflict
Exactly x -> case fs of Just f -> f x
Nothing -> mempty
)
instance FlatMapping Intersect Intersectable where
flatMapR ( fs, gs ) ( xs, ys )
= ( case gs of Just g -> Intersect.map g (Intersect.powerSet ys)
Nothing -> mempty
, case fs of Just f -> foldr (Intersect.union . f) (Intersect mempty) xs
Nothing -> mempty
)