module Bayes.PrivateTypes(
BayesianDiscreteVariable(..)
, Set(..)
, DV(..)
, DVSet(..)
, DVI(..)
, setDVValue
, (=:)
, instantiationValue
, instantiationVariable
, Vertex(..)
, getMinBound
, Index(..)
, forAllInstantiations
, indicesForDomain
, fromIndex
, instantiationDetails
, allInstantiationsForOneVariable
) where
import qualified Data.List as L
class Set s where
emptySet :: s a
union :: Eq a => s a -> s a -> s a
intersection :: Eq a => s a -> s a -> s a
difference :: Eq a => s a -> s a -> s a
isEmpty :: s a -> Bool
isElem :: Eq a => a -> s a -> Bool
addElem :: Eq a => a -> s a -> s a
nbElements :: s a -> Int
subset :: Eq a => s a -> s a -> Bool
equal :: Eq a => s a -> s a -> Bool
equal sa sb = (sa `subset` sb) && (sb `subset` sa)
instance Set [] where
emptySet = []
union = L.union
intersection = L.intersect
difference a b = a L.\\ b
isEmpty [] = True
isEmpty _ = False
isElem = L.elem
addElem a l = if a `elem` l then l else a:l
nbElements = length
subset sa sb = all (`elem` sb) sa
newtype Vertex = Vertex {vertexId :: Int} deriving(Eq,Ord)
instance Show Vertex where
show (Vertex v) = "v" ++ show v
class BayesianDiscreteVariable v where
dimension :: v -> Int
getMinBound :: Bounded a => a -> a
getMinBound _ = minBound
data DV = DV !Vertex !Int deriving(Eq,Ord)
newtype DVSet s = DVSet [DV] deriving(Eq)
fromDVSet :: DVSet s -> [DV]
fromDVSet (DVSet l) = l
instance Show DV where
show (DV v d) = show v ++ "(" ++ show d ++ ")"
instance BayesianDiscreteVariable DV where
dimension (DV _ d) = d
newtype Index s = Index Int deriving(Eq)
fromIndex :: Index s -> Int
fromIndex (Index i) = i
indicesForDomain :: DVSet s -> [[Index s]]
indicesForDomain (DVSet l) = mapM indicesForOneDomain l
where
indicesForOneDomain (DV _ d) = map Index [0..d1]
allInstantiationsForOneVariable :: DV -> [DVI Int]
allInstantiationsForOneVariable v@(DV _ d) = map (setDVValue v) [0..d1]
forAllInstantiations :: DVSet s -> [[DVI Int]]
forAllInstantiations (DVSet l) = mapM allInstantiationsForOneVariable l
data DVI a = DVI DV !a deriving(Eq)
instance Show a => Show (DVI a) where
show (DVI (DV v _) i) = show v ++ "=" ++ show i
type DVISet a = [DVI a]
setDVValue :: DV -> a -> DVI a
setDVValue v a = DVI v a
(=:) :: (Bounded b, Enum b) => DV -> b -> DVI Int
(=:) a b = setDVValue a (fromEnum b fromEnum (getMinBound b))
instance BayesianDiscreteVariable (DVI a) where
dimension (DVI v _) = dimension v
instantiationDetails :: [DVI Int] -> (DVSet s, [Index s])
instantiationDetails l = (DVSet $ map instantiationVariable l, map (Index . instantiationValue) l)
instantiationValue (DVI _ v) = v
instantiationVariable (DVI dv _) = dv