module ToySolver.Data.ArithRel
(
RelOp (..)
, flipOp
, negOp
, showOp
, evalOp
, ArithRel (..)
, fromArithRel
, IsArithRel (..)
, (.<.), (.<=.), (.>=.), (.>.), (.==.), (./=.)
) where
import qualified Data.IntSet as IS
import ToySolver.Data.Boolean
import ToySolver.Data.Var
infix 4 .<., .<=., .>=., .>., .==., ./=.
data RelOp = Lt | Le | Ge | Gt | Eql | NEq
deriving (Show, Eq, Ord)
flipOp :: RelOp -> RelOp
flipOp Le = Ge
flipOp Ge = Le
flipOp Lt = Gt
flipOp Gt = Lt
flipOp Eql = Eql
flipOp NEq = NEq
negOp :: RelOp -> RelOp
negOp Lt = Ge
negOp Le = Gt
negOp Ge = Lt
negOp Gt = Le
negOp Eql = NEq
negOp NEq = Eql
showOp :: RelOp -> String
showOp Lt = "<"
showOp Le = "<="
showOp Ge = ">="
showOp Gt = ">"
showOp Eql = "="
showOp NEq = "/="
evalOp :: Ord a => RelOp -> a -> a -> Bool
evalOp Lt = (<)
evalOp Le = (<=)
evalOp Ge = (>=)
evalOp Gt = (>)
evalOp Eql = (==)
evalOp NEq = (/=)
class IsArithRel e r | r -> e where
arithRel :: RelOp -> e -> e -> r
(.<.) :: IsArithRel e r => e -> e -> r
a .<. b = arithRel Lt a b
(.<=.) :: IsArithRel e r => e -> e -> r
a .<=. b = arithRel Le a b
(.>.) :: IsArithRel e r => e -> e -> r
a .>. b = arithRel Gt a b
(.>=.) :: IsArithRel e r => e -> e -> r
a .>=. b = arithRel Ge a b
(.==.) :: IsArithRel e r => e -> e -> r
a .==. b = arithRel Eql a b
(./=.) :: IsArithRel e r => e -> e -> r
a ./=. b = arithRel NEq a b
data ArithRel e = ArithRel e RelOp e
deriving (Show, Eq, Ord)
instance Complement (ArithRel c) where
notB (ArithRel lhs op rhs) = ArithRel lhs (negOp op) rhs
instance IsArithRel e (ArithRel e) where
arithRel op a b = ArithRel a op b
instance Variables e => Variables (ArithRel e) where
vars (ArithRel a _ b) = vars a `IS.union` vars b
instance Functor ArithRel where
fmap f (ArithRel a op b) = ArithRel (f a) op (f b)
fromArithRel :: IsArithRel e r => ArithRel e -> r
fromArithRel (ArithRel a op b) = arithRel op a b