Commit e74bd43d authored by Philipp Meyer's avatar Philipp Meyer
Browse files

Grouped maps and vector into maplike class

parent bb81817d
......@@ -7,7 +7,7 @@ import Data.SBV
import Util
import Property
evaluateTerm :: (Ord a) => Term a -> SIMap a -> SInteger
evaluateTerm :: (Ord a, Show a) => Term a -> SIMap a -> SInteger
evaluateTerm (Var x) m = val m x
evaluateTerm (Const c) _ = literal c
evaluateTerm (Minus t) m = - evaluateTerm t m
......@@ -23,7 +23,7 @@ opToFunction Ne = (./=)
opToFunction Le = (.<=)
opToFunction Lt = (.<)
evaluateFormula :: (Ord a) => Formula a -> SIMap a -> SBool
evaluateFormula :: (Ord a, Show a) => Formula a -> SIMap a -> SBool
evaluateFormula FTrue _ = true
evaluateFormula FFalse _ = false
evaluateFormula (LinearInequation lhs op rhs) m =
......
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies #-}
module Util
(verbosePut,elems,items,emap,
nubOrd,nubOrdBy,prime,val,vals,mval,zeroVal,positiveVal,sumVal,
......@@ -16,7 +18,7 @@ import Data.Function
- Various maps and functions on them
-}
type Vector a = M.Map a Integer
newtype Vector a = Vector { getVector :: M.Map a Integer }
type Model a = M.Map String a
type VarMap a = M.Map a String
type SIMap a = M.Map a SInteger
......@@ -24,30 +26,46 @@ type SBMap a = M.Map a SBool
type IMap a = M.Map a Integer
type BMap a = M.Map a Bool
val :: (Ord a) => M.Map a b -> a -> b
val = (M.!)
mval :: (Ord a) => M.Map a b -> [a] -> [b]
mval = map . val
zeroVal :: (Ord a) => M.Map a SInteger -> a -> SBool
class MapLike c a b | c -> a, c -> b where
val :: c -> a -> b
vals :: c -> [b]
elems :: c -> [a]
items :: c -> [(a,b)]
mval :: c -> [a] -> [b]
mval = map . val
sumVal :: (Num b) => c -> b
sumVal = sum . vals
instance (Ord a, Show a, Show b) => MapLike (M.Map a b) a b where
val m x = M.findWithDefault
(error ("key " ++ show x ++ " not found in " ++ show m))
x m
vals = M.elems
items = M.toList
elems = M.keys
instance (Ord a, Show a) => MapLike (Vector a) a Integer where
val (Vector v) x = M.findWithDefault 0 x v
vals = vals . getVector
items = M.toList . getVector
elems = M.keys . getVector
instance (Show a) => Show (Vector a) where
show (Vector v) =
"[" ++ intercalate "," (map showEntry (M.toList v)) ++ "]"
where showEntry (i,x) =
show i ++ (if x /= 1 then "(" ++ show x ++ ")" else "")
emap :: (Ord a, Ord b) => (a -> b) -> Vector a -> Vector b
emap f = Vector . M.mapKeys f . getVector
zeroVal :: (Ord a, Show a) => M.Map a SInteger -> a -> SBool
zeroVal m x = val m x .== 0
positiveVal :: (Ord a) => M.Map a SInteger -> a -> SBool
positiveVal :: (Ord a, Show a) => M.Map a SInteger -> a -> SBool
positiveVal m x = val m x .> 0
sumVal :: (Ord a, Num b) => M.Map a b -> b
sumVal = sum . vals
vals :: (Ord a) => M.Map a b -> [b]
vals = M.elems
elems :: (Ord a) => M.Map a b -> [a]
elems = M.keys
items :: M.Map a b -> [(a,b)]
items = M.toList
makeVarMap :: (Show a, Ord a) => [a] -> VarMap a
makeVarMap = makeVarMapWith id
......@@ -57,20 +75,11 @@ makeVarMapWith f xs = M.fromList $ xs `zip` map (f . show) xs
getNames :: VarMap a -> [String]
getNames = M.elems
--instance (Show a) => Show (Vector a) where
-- show (Vector v) =
-- "[" ++ intercalate "," (map showEntry (M.toList v)) ++ "]"
-- where showEntry (i,x) =
-- show i ++ (if x /= 1 then "(" ++ show x ++ ")" else "")
emap :: (Ord a, Ord b) => (a -> b) -> M.Map a c -> M.Map b c
emap = M.mapKeys
buildVector :: (Ord a) => [(a, Integer)] -> Vector a
buildVector = M.fromList
buildVector = makeVector . M.fromList
makeVector :: (Ord a) => M.Map a Integer -> Vector a
makeVector = M.filter (/=0)
makeVector = Vector . M.filter (/=0)
{-
- List functions
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment