module Data.Function.Tabulated (
DomainFunction1(..)
, TabulatedFunction1(..)
, TabulatedFunctionImpl1
, DomainFunction2(..)
, TabulatedFunction2(..)
, TabulatedFunctionImpl2
, DomainFunction3(..)
, TabulatedFunction3(..)
, TabulatedFunctionImpl3
) where
import Data.List (nub, sort)
import Data.Tuple.Util (curry3, fst3, snd3, trd3)
import qualified Data.Map as M
class DomainFunction1 t where
domain1 :: Ord a
=> t a
-> [a]
evaluate1 :: Ord a
=> t a
-> a
-> Maybe Double
class DomainFunction1 t => TabulatedFunction1 t where
fromTable1 :: Ord a
=> [(a, Double)]
-> t a
fromUnTable1 :: Ord a
=> (b -> (a, Double))
-> [b]
-> t a
fromUnTable1 = (fromTable1 .) . map
data TabulatedFunctionImpl1 a = TabulatedFunctionImpl1 [a] (M.Map a Double)
deriving Show
instance DomainFunction1 TabulatedFunctionImpl1 where
domain1 (TabulatedFunctionImpl1 d _) = d
evaluate1 (TabulatedFunctionImpl1 _ m) = flip M.lookup m
instance TabulatedFunction1 TabulatedFunctionImpl1 where
fromTable1 x =
TabulatedFunctionImpl1
(sort $ nub $ map fst x)
(M.fromList x)
class DomainFunction2 t where
domain2 :: (Ord a, Ord b)
=> t a b
-> ([a], [b])
evaluate2 :: (Ord a, Ord b)
=> t a b
-> a
-> b
-> Maybe Double
class DomainFunction2 t => TabulatedFunction2 t where
fromTable2 :: (Ord a, Ord b)
=> [((a, b), Double)]
-> t a b
fromUnTable2 :: (Ord a, Ord b)
=> (c -> ((a, b), Double))
-> [c]
-> t a b
fromUnTable2 = (fromTable2 .) . map
data TabulatedFunctionImpl2 a b = TabulatedFunctionImpl2 ([a], [b]) (M.Map (a, b) Double)
deriving Show
instance DomainFunction2 TabulatedFunctionImpl2 where
domain2 (TabulatedFunctionImpl2 d _) = d
evaluate2 (TabulatedFunctionImpl2 _ m) = curry (`M.lookup` m)
instance TabulatedFunction2 TabulatedFunctionImpl2 where
fromTable2 x =
TabulatedFunctionImpl2
(sort $ nub $ map (fst . fst) x, sort $ nub $ map (snd . fst) x)
(M.fromList x)
class DomainFunction3 t where
domain3 :: (Ord a, Ord b, Ord c)
=> t a b c
-> ([a], [b], [c])
evaluate3 :: (Ord a, Ord b, Ord c)
=> t a b c
-> a
-> b
-> c
-> Maybe Double
class DomainFunction3 t => TabulatedFunction3 t where
fromTable3 :: (Ord a, Ord b, Ord c)
=> [((a, b, c), Double)]
-> t a b c
fromUnTable3 :: (Ord a, Ord b, Ord c)
=> (d -> ((a, b, c), Double))
-> [d]
-> t a b c
fromUnTable3 = (fromTable3 .) . map
data TabulatedFunctionImpl3 a b c = TabulatedFunctionImpl3 ([a], [b], [c]) (M.Map (a, b, c) Double)
deriving Show
instance DomainFunction3 TabulatedFunctionImpl3 where
domain3 (TabulatedFunctionImpl3 d _) = d
evaluate3 (TabulatedFunctionImpl3 _ m) = curry3 (`M.lookup` m)
instance TabulatedFunction3 TabulatedFunctionImpl3 where
fromTable3 x =
TabulatedFunctionImpl3
(sort $ nub $ map (fst3 . fst) x, sort $ nub $ map (snd3 . fst) x, sort $ nub $ map (trd3 . fst) x)
(M.fromList x)