Skip to content

Commit

Permalink
use pattern+guard notation
Browse files Browse the repository at this point in the history
  • Loading branch information
gruhn committed Apr 22, 2024
1 parent 5e05eb2 commit 0fbf6b6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 36 deletions.
18 changes: 7 additions & 11 deletions src/Theory/LinearArithmatic/Simplex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,12 @@ boundViolation (Tableau basis bounds assignment) var = do
let current_value = assignment M.! var
bound <- M.lookup var bounds
case bound of
(UpperBound, bound_value) ->
if current_value <= bound_value then
Nothing
else
Just MustDecrease
(LowerBound, bound_value) -> do
if bound_value <= current_value then
Nothing
else
Just MustIncrease
(UpperBound, bound_value)
| current_value <= bound_value -> Nothing
| otherwise -> Just MustDecrease
(LowerBound, bound_value)
| bound_value <= current_value -> Nothing
| otherwise -> Just MustIncrease

isBoundViolated :: Tableau -> Var -> Bool
isBoundViolated tableau var = isJust $ boundViolation tableau var
Expand Down Expand Up @@ -324,4 +320,4 @@ simplex constraints = do
let original_vars = varsInAll constraints
assignment = M.restrictKeys (getAssignment tableau_n) original_vars

return assignment
return assignment
7 changes: 3 additions & 4 deletions src/Theory/NonLinearRealArithmatic/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ varsIn expr = case expr of
substitute :: Var -> Expr a -> Expr a -> Expr a
substitute var expr_subst_with expr_subst_in =
case expr_subst_in of
Const _ ->
expr_subst_in
Var var' ->
if var == var' then expr_subst_with else expr_subst_in
Const _ -> expr_subst_in
Var var' | var == var' -> expr_subst_with
Var _ | otherwise -> expr_subst_in
UnaryOp op sub_expr ->
UnaryOp op (substitute var expr_subst_with sub_expr)
BinaryOp op sub_expr1 sub_expr2 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,12 @@ contractWith (constraint, var) var_domains = new_domain
Equals -> max lower_bound lower_bound' :..: min upper_bound upper_bound'
LessEquals -> lower_bound :..: min upper_bound upper_bound'
GreaterEquals -> max lower_bound lower_bound' :..: upper_bound
LessThan ->
if lower_bound >= upper_bound' then
Interval.empty
else
lower_bound :..: min upper_bound upper_bound'
GreaterThan ->
if upper_bound <= lower_bound' then
Interval.empty
else
max lower_bound lower_bound' :..: upper_bound
LessThan
| lower_bound >= upper_bound' -> Interval.empty
| otherwise -> lower_bound :..: min upper_bound upper_bound'
GreaterThan
| upper_bound <= lower_bound' -> Interval.empty
| otherwise -> max lower_bound lower_bound' :..: upper_bound

old_domain = var_domains M.! var
new_domain = IntervalUnion.reduce $ IntervalUnion $ do
Expand Down
11 changes: 4 additions & 7 deletions src/Theory/NonLinearRealArithmatic/Polynomial.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,10 @@ fromExpr expr =

UnaryOp (Exp n) (Const a) -> Polynomial [ Term (a^n) M.empty ]
UnaryOp (Exp n) (Var var) -> Polynomial [ Term 1 (M.singleton var n) ]
UnaryOp (Exp n) expr ->
if n < 1 then
error "Non-positive exponents not supported"
else if n == 1 then
fromExpr expr
else
fromExpr $ BinaryOp Mul expr (UnaryOp (Exp (n-1)) expr)
UnaryOp (Exp n) expr
| n < 1 -> error "Non-positive exponents not supported"
| n == 1 -> fromExpr expr
| otherwise -> fromExpr $ BinaryOp Mul expr (UnaryOp (Exp (n-1)) expr)

BinaryOp Add expr1 expr2 -> fromExpr expr1 + fromExpr expr2
BinaryOp Sub expr1 expr2 -> fromExpr expr1 - fromExpr expr2
Expand Down
7 changes: 3 additions & 4 deletions src/Theory/NonLinearRealArithmatic/UnivariatePolynomial.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ divide dividend divisor = go 0 dividend

(Nothing, _) -> (quotient, 0)

(Just (rem_exp, rem_coeff), Just (div_exp, div_coeff)) ->
if rem_exp < div_exp then
(quotient, remainder)
else
(Just (rem_exp, rem_coeff), Just (div_exp, div_coeff))
| rem_exp < div_exp -> (quotient, remainder)
| otherwise ->
let quot_term = term (rem_exp - div_exp) (rem_coeff / div_coeff)
in go (quotient + quot_term) (remainder - quot_term * divisor)

0 comments on commit 0fbf6b6

Please sign in to comment.