module Data.Relational (
Tuple(..)
, Relation(..)
) where
import Control.Applicative (liftA2)
import Control.Arrow ((&&&))
import Data.List.Util.Listable (Listable(..), WithHeader(..))
import Data.Maybe (fromMaybe)
import Data.String.Util (Stringy(..))
class Tuple t where
type Attribute t :: *
makeTuple :: [Attribute t] -> t
attributes :: t -> [Attribute t]
instance (Attribute t ~ a, Tuple t) => Listable t a where
fromList = makeTuple
toList = attributes
class Tuple t => Relation n t r | r -> t, r -> n where
names :: r -> [n]
empty :: [n] -> r
makeRelation :: [n] -> [t] -> r
tuples :: r -> [t]
attributeMaybe :: r -> n -> t -> Maybe (Attribute t)
attribute :: r -> n -> t -> Attribute t
attribute = ((fromMaybe (error "Attribute missing from relation.") .) .) . attributeMaybe
instance (Relation n t r, Stringy n) => WithHeader r t where
fromHeaderLists = liftA2 makeRelation (fmap fromString . fst) snd
toHeaderLists = (fmap toString . names) &&& tuples