Commit 2f387156 authored by Philipp Meyer's avatar Philipp Meyer

Rename LinearInequation to Equation

parent ebce00a0
......@@ -107,12 +107,12 @@ parseOp = (reservedOp "<" *> return Lt) <|>
(reservedOp "=%" *> (ModEq <$> integer)) <|>
(reservedOp "!=%" *> (ModNe <$> integer))
linIneq :: Parser (Formula String)
linIneq = do
equation :: Parser (Formula String)
equation = do
lhs <- term
op <- parseOp
rhs <- term
return (LinearInequation lhs op rhs)
return (Equation lhs op rhs)
formOperatorTable :: [[Operator String () Identity (Formula String)]]
formOperatorTable =
......@@ -122,7 +122,7 @@ formOperatorTable =
]
formAtom :: Parser (Formula String)
formAtom = try linIneq
formAtom = try equation
<|> (reserved "true" *> return FTrue)
<|> (reserved "false" *> return FFalse)
<|> parens formula
......
......@@ -64,7 +64,7 @@ negateOp (ModNe m) = (ModEq m)
data Formula a =
FTrue | FFalse
| LinearInequation (Term a) Op (Term a)
| Equation (Term a) Op (Term a)
| Neg (Formula a)
| Formula a :&: Formula a
| Formula a :|: Formula a
......@@ -78,11 +78,11 @@ negationNormalForm (Neg (FTrue)) = FFalse
negationNormalForm (Neg (FFalse)) = FTrue
negationNormalForm (Neg (g :&: h)) = (negationNormalForm (Neg g)) :|: (negationNormalForm (Neg h))
negationNormalForm (Neg (g :|: h)) = (negationNormalForm (Neg g)) :&: (negationNormalForm (Neg h))
negationNormalForm (Neg (LinearInequation u op t)) = LinearInequation u (negateOp op) t
negationNormalForm (Neg (Equation u op t)) = Equation u (negateOp op) t
negationNormalForm (Neg (Neg g)) = negationNormalForm g
negationNormalForm (g :&: h) = (negationNormalForm g) :&: (negationNormalForm h)
negationNormalForm (g :|: h) = (negationNormalForm g) :|: (negationNormalForm h)
negationNormalForm f@(LinearInequation _ _ _) = f
negationNormalForm f@(Equation _ _ _) = f
negationNormalForm FTrue = FTrue
negationNormalForm FFalse = FFalse
......@@ -91,14 +91,14 @@ eliminateModulo = eliminateModulo' 0
eliminateModulo' :: Int -> (Int -> a) -> Formula a -> (Formula a, [a])
eliminateModulo' _ _ (Neg g) = error "Formula not in negation normal form: cannot eliminate modulo"
eliminateModulo' n makeVar (LinearInequation lhs (ModEq m) rhs) =
eliminateModulo' n makeVar (Equation lhs (ModEq m) rhs) =
let k = makeVar n
in (LinearInequation lhs Eq (rhs :+: ((Const m) :*: (Var k))), [k])
eliminateModulo' n makeVar (LinearInequation lhs (ModNe m) rhs) =
in (Equation lhs Eq (rhs :+: ((Const m) :*: (Var k))), [k])
eliminateModulo' n makeVar (Equation lhs (ModNe m) rhs) =
let j = makeVar (n + 1)
k = makeVar n
in ((LinearInequation lhs Eq (rhs :+: ((Var j) :+: ((Const m) :*: (Var k))))) :&:
(LinearInequation (Const 0) Lt (Var j)) :&: (LinearInequation (Var j) Lt (Const m)), [k, j])
in ((Equation lhs Eq (rhs :+: ((Var j) :+: ((Const m) :*: (Var k))))) :&:
(Equation (Const 0) Lt (Var j)) :&: (Equation (Var j) Lt (Const m)), [k, j])
eliminateModulo' n makeVar (g :|: h) =
let (g', ag) = eliminateModulo' n makeVar g
(h', ah) = eliminateModulo' (n + length ag) makeVar h
......@@ -112,7 +112,7 @@ eliminateModulo' _ _ f = (f, [])
instance (Show a) => Show (Formula a) where
show FTrue = "true"
show FFalse = "false"
show (LinearInequation lhs op rhs) =
show (Equation lhs op rhs) =
show lhs ++ " " ++ show op ++ " " ++ show rhs
show (Neg p) = "¬" ++ "(" ++ show p ++ ")"
show (p :&: q) = "(" ++ show p ++ " ∧ " ++ show q ++ ")"
......@@ -121,8 +121,8 @@ instance (Show a) => Show (Formula a) where
instance Functor Formula where
fmap _ FTrue = FTrue
fmap _ FFalse = FFalse
fmap f (LinearInequation lhs op rhs) =
LinearInequation (fmap f lhs) op (fmap f rhs)
fmap f (Equation lhs op rhs) =
Equation (fmap f lhs) op (fmap f rhs)
fmap f (Neg p) = Neg (fmap f p)
fmap f (p :&: q) = fmap f p :&: fmap f q
fmap f (p :|: q) = fmap f p :|: fmap f q
......
......@@ -27,7 +27,7 @@ opToFunction Lt = (.<)
evaluateFormula :: (Ord a, Show a) => Formula a -> SIMap a -> SBool
evaluateFormula FTrue _ = true
evaluateFormula FFalse _ = false
evaluateFormula (LinearInequation lhs op rhs) m =
evaluateFormula (Equation lhs op rhs) m =
opToFunction op (evaluateTerm lhs m) (evaluateTerm rhs m)
evaluateFormula (Neg p) m = bnot $ evaluateFormula p m
evaluateFormula (p :&: q) m = evaluateFormula p m &&& evaluateFormula q m
......
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