rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% c <= x-y -> y-x <= -c
Constraint = b(less_equal(Constant,Rhs),pred,Info),
get_int_value(Constant, CVal),
rewrite_to_difference(Rhs, TRhs),
switch_arguments_in_difference(TRhs, NRhs),
!,
NCVal is CVal * -1,
NConstraint = b(less_equal(NRhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x+-y <= c -> x-y <= c | x-y <= c -> x-y <= c
% x <= c -> x-_zero <= c | -x <= c -> _zero-x <= c
Constraint = b(less_equal(Lhs,Constant),pred,Info),
get_int_value(Constant, _),
rewrite_to_difference(Lhs, NLhs),
!,
NConstraint = b(less_equal(NLhs,Constant),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x <= y -> x-y <= 0
Constraint = b(less_equal(Id1,Id2),pred,Info),
Id1 = b(identifier(_),_,_),
Id2 = b(identifier(_),_,_),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NConstraint = b(less_equal(NLhs,b(integer(0),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% -x <= y -> y-x <= 0
Constraint = b(less_equal(Id1,Id2),pred,Info),
Id1 = b(unary_minus(b(identifier(_),_,_)),integer,[]),
Id2 = b(identifier(_),_,_),
!,
NLhs = b(minus(Id2,Id1),integer,[]),
NConstraint = b(less_equal(NLhs,b(integer(0),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% c <= y -> _zero-y <= -c | c <= -y -> y-_zero <= -c
Constraint = b(less_equal(Constant,Id2),pred,Info),
get_int_value(Constant, CVal),
( Id2 = b(identifier(Id2Name),integer,Id2Info)
-> NegId2 = b(unary_minus(b(identifier(Id2Name),integer,Id2Info)),integer,[])
; Id2 = b(unary_minus(NegId2),integer,[])
),
rewrite_to_difference(NegId2, Diff),
!,
NCVal is CVal * -1,
NConstraint = b(less_equal(Diff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% c1 <= y-c2 -> _zero-y <= -c2-c1 | c1 <= -y-c2 -> y-_zero <= -c2-c1
Constraint = b(less_equal(Constant1,AddOrSub),pred,Info),
get_int_value(Constant1, CVal1),
get_args_of_add_or_minus_to_addition(AddOrSub, Id2, Constant2),
get_int_value(Constant2, CVal2),
( Id2 = b(identifier(Id2Name),integer,Id2Info)
-> NegId2 = b(unary_minus(b(identifier(Id2Name),integer,Id2Info)),integer,[])
; Id2 = b(unary_minus(NegId2),integer,[])
),
rewrite_to_difference(NegId2, Diff),
!,
NCVal is CVal2 + CVal1 * -1,
NConstraint = b(less_equal(Diff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x <= y-c -> x-y <= -c | x <= y+c -> x-y <= c
Constraint = b(less_equal(Id1,AddOrSub),pred,Info),
Id1 = b(identifier(_),_,_),
get_args_of_add_or_minus_to_addition(AddOrSub, Id2, Constant),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NConstraint = b(less_equal(NLhs,Constant),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x - c <= y -> x-y <= c | x + c <= y -> x-y <= -c
Constraint = b(less_equal(AddOrSub,Id2),pred,Info),
Id2 = b(identifier(_),_,_),
get_args_of_add_or_minus_to_addition(AddOrSub, Id1, Constant),
get_int_value(Constant, CVal),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NCVal is CVal * -1,
NConstraint = b(less_equal(NLhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x - c1 <= c2 -> x-_zero <= c2+c1 | x + c1 <= c2 -> x-_zero <= c2-c1
Constraint = b(less_equal(AddOrSub,Constant2),pred,Info),
get_int_value(Constant2, CVal2),
get_args_of_add_or_minus_to_addition(AddOrSub, Id1, Constant1),
get_int_value(Constant1, CVal1),
!,
rewrite_to_difference(Id1, Diff),
NCVal is CVal2 + CVal1 * -1,
NConstraint = b(less_equal(Diff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x+c1 <= y-c2 -> x-y <= -c2-c1 | x + c1 <= y+c2 -> x-y <= c2-c1
% x-c1 <= y-c2 -> x-y <= -c2+c1 | x-c1 <= y+c2 -> x-y <= c2+c1
Constraint = b(less_equal(AddOrSub1,AddOrSub2),pred,Info),
get_args_of_add_or_minus_to_addition(AddOrSub1, Id1, Constant1),
get_args_of_add_or_minus_to_addition(AddOrSub2, Id2, Constant2),
get_int_value(Constant1, CVal1),
get_int_value(Constant2, CVal2),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NCVal is CVal2 + CVal1 * -1,
NConstraint = b(less_equal(NLhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% c < x-y -> y-x <= -c-1
Constraint = b(less(Constant,Rhs),pred,Info),
get_int_value(Constant, CVal),
rewrite_to_difference(Rhs, TRhs),
switch_arguments_in_difference(TRhs, NRhs),
!,
NCVal is CVal * -1 - 1,
NConstraint = b(less_equal(NRhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x+-y < c -> x-y <= c-1 | x-y < c -> x-y <= c-1
% x < c -> x-_zero <= -c-1 | -x < c -> _zero-x <= -c-1
Constraint = b(less(Lhs,Constant),pred,Info),
get_int_value(Constant, CVal),
rewrite_to_difference(Lhs, NLhs),
!,
NCVal is CVal - 1,
NConstraint = b(less_equal(NLhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x < y -> x-y <= -1
Constraint = b(less(Id1,Id2),pred,Info),
Id1 = b(identifier(_),_,_),
Id2 = b(identifier(_),_,_),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NConstraint = b(less_equal(NLhs,b(integer(-1),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% -x < y -> y-x <= -1
Constraint = b(less(Id1,Id2),pred,Info),
Id1 = b(unary_minus(b(identifier(_),_,_)),integer,[]),
Id2 = b(identifier(_),_,_),
!,
NLhs = b(minus(Id2,Id1),integer,[]),
NConstraint = b(less_equal(NLhs,b(integer(-1),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% c < y -> _zero-y <= -c-1 | c < -y -> y-_zero <= -c-1
Constraint = b(less(Constant,Id2),pred,Info),
get_int_value(Constant, CVal),
( Id2 = b(identifier(Id2Name),integer,Id2Info)
-> NegId2 = b(unary_minus(b(identifier(Id2Name),integer,Id2Info)),integer,[])
; Id2 = b(unary_minus(NegId2),integer,[])
),
rewrite_to_difference(NegId2, Diff),
!,
NCVal is CVal * -1 - 1,
NConstraint = b(less_equal(Diff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% c1 < y-c2 -> _zero-y <= -c2-c1-1 | c1 <= -y-c2 -> y-_zero <= -c2-c1-1
Constraint = b(less(Constant1,AddOrSub),pred,Info),
get_int_value(Constant1, CVal1),
get_args_of_add_or_minus_to_addition(AddOrSub, Id2, Constant2),
get_int_value(Constant2, CVal2),
( Id2 = b(identifier(Id2Name),integer,Id2Info)
-> NegId2 = b(unary_minus(b(identifier(Id2Name),integer,Id2Info)),integer,[])
; Id2 = b(unary_minus(NegId2),integer,[])
),
rewrite_to_difference(NegId2, Diff),
!,
NCVal is CVal2 + CVal1 * -1 - 1,
NConstraint = b(less_equal(Diff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x < y-c -> x-y <= -c-1 | x < y+c -> x-y <= c-1
Constraint = b(less(Id1,AddOrSub),pred,Info),
Id1 = b(identifier(_),_,_),
get_args_of_add_or_minus_to_addition(AddOrSub, Id2, Constant),
get_int_value(Constant, CVal),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NCVal is CVal - 1,
NConstraint = b(less_equal(NLhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x-c < y -> x-y <= c-1 | x+c < y -> x-y <= -c-1
Constraint = b(less(AddOrSub,Id2),pred,Info),
Id2 = b(identifier(_),_,_),
get_args_of_add_or_minus_to_addition(AddOrSub, Id1, Constant),
get_int_value(Constant, CVal),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NCVal is CVal * -1 - 1,
NConstraint = b(less_equal(NLhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x - c1 < c2 -> x-_zero <= c2+c1-1 | x + c1 < c2 -> x-_zero <= c2-c1-1
Constraint = b(less(AddOrSub,Constant2),pred,Info),
get_int_value(Constant2, CVal2),
get_args_of_add_or_minus_to_addition(AddOrSub, Id1, Constant1),
rewrite_to_difference(Id1, Diff),
get_int_value(Constant1, CVal1),
!,
rewrite_to_difference(Id1, Diff),
NCVal is CVal2 + CVal1 * -1 - 1,
NConstraint = b(less_equal(Diff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, [NConstraint]) :-
% x+c1 < y-c2 -> x-y <= -c2-c1-1 | x+c1 < y+c2 -> x-y <= c2-c1-1
% x-c1 < y-c2 -> x-y <= -c2+c1-1 | x-c1 < y+c2 -> x-y <= c2+c1-1
Constraint = b(less(AddOrSub1,AddOrSub2),pred,Info),
get_args_of_add_or_minus_to_addition(AddOrSub1, Id1, Constant1),
get_args_of_add_or_minus_to_addition(AddOrSub2, Id2, Constant2),
get_int_value(Constant1, CVal1),
get_int_value(Constant2, CVal2),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
NCVal is CVal2 + CVal1 * -1 - 1,
NConstraint = b(less_equal(NLhs,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, ConjList) :-
% c = _ -> rewrite(_ = c)
Constraint = b(equal(Constant,Rhs),pred,Info),
get_int_value(Constant, _),
\+ get_int_value(Rhs, _),
!,
rewrite_to_idl_pos(b(equal(Rhs,Constant),pred,Info), ConjList).
rewrite_to_idl_pos(Constraint, [NC1,NC2]) :-
% x-y = c -> x-y <= c & y-x <= -c | x+-y = c -> x-y <= c & y-x <= -c
Constraint = b(equal(Lhs,Constant),pred,Info),
get_int_value(Constant, CVal),
!,
rewrite_to_difference(Lhs, Diff),
switch_arguments_in_difference(Diff, SDiff),
NCVal is CVal * -1,
NC1 = b(less_equal(Diff,Constant),pred,Info),
NC2 = b(less_equal(SDiff,b(integer(NCVal),integer,[])),pred,Info).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% x = y-c -> rewrite(x-y = -c) | x = y+c -> rewrite(x-y = c)
Constraint = b(equal(Id1,AddOrSub),pred,Info),
Id1 = b(identifier(_),_,_),
get_args_of_add_or_minus_to_addition(AddOrSub, Id2, Constant),
!,
NLhs = b(minus(Id1,Id2),integer,[]),
rewrite_to_idl_pos(b(equal(NLhs,Constant),pred,Info), NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% x-c1 = y-c2 -> rewrite(x-y = -c2+c1) | x-c1 = y+c2 -> rewrite(x-y = c1+c2)
% x+c1 = y-c2 -> rewrite(x-y = -c2-c1) | x+c1 = y+c2 -> rewrite(x-y = c1-c2)
Constraint = b(equal(AddOrSub1,AddOrSub2),pred,Info),
get_args_of_add_or_minus_to_addition(AddOrSub1, Id1, Constant1),
get_args_of_add_or_minus_to_addition(AddOrSub2, Id2, Constant2),
get_int_value(Constant1, CVal1),
get_int_value(Constant2, CVal2),
!,
NCVal is CVal2 + CVal1 * -1,
NLhs = b(minus(Id1,Id2),integer,[]),
rewrite_to_idl_pos(b(equal(NLhs,b(integer(NCVal),integer,[])),pred,Info), NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% x = y -> rewrite(x-y = 0)
Constraint = b(equal(Id1,Id2),pred,Info),
Id1 = b(identifier(_),_,_),
Id2 = b(identifier(_),_,_),
!,
Constraint2 = b(equal(b(minus(Id1,Id2),integer,[]),b(integer(0),integer,[])),pred,Info),
rewrite_to_idl_pos(Constraint2, NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% -x = -y -> rewrite(y-x = 0)
Constraint = b(equal(Id1,Id2),pred,Info),
Id1 = b(unary_minus(b(identifier(_),_,_)),integer,[]),
Id2 = b(unary_minus(PosId2),integer,[]),
!,
Constraint2 = b(equal(b(minus(PosId2,Id1),integer,[]),b(integer(0),integer,[])),pred,Info),
rewrite_to_idl_pos(Constraint2, NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% x-c = y -> rewrite(x-y = c) | x+c = y -> rewrite(x-y = -c)
Constraint = b(equal(AddOrSub,Id2),pred,Info),
get_args_of_add_or_minus_to_addition(AddOrSub, Id1, Constant),
Id2 = b(identifier(_),_,_),
get_int_value(Constant, CVal),
!,
NCVal is CVal * -1,
Constraint2 = b(equal(b(minus(Id1,Id2),integer,[]),b(integer(NCVal),integer,[])),pred,Info),
rewrite_to_idl_pos(Constraint2, NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% c1 = y-c2 -> rewrite(y = c1+c2) | c1 = y+c2 -> rewrite(y = c1-c2)
Constraint = b(equal(Constant1,AddOrSub),pred,Info),
get_args_of_add_or_minus_to_addition(AddOrSub, Id2, Constant2),
get_int_value(Constant1, CVal1),
get_int_value(Constant2, CVal2),
!,
NCVal is CVal1 + CVal2 * -1,
Constraint2 = b(equal(Id2,b(integer(NCVal),integer,[])),pred,Info),
rewrite_to_idl_pos(Constraint2, NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% x-c1 = c2 -> rewrite(x = c2+c1) | x+c1 = c2 -> rewrite(x = c2-c1)
Constraint = b(equal(AddOrSub,Constant2),pred,Info),
get_args_of_add_or_minus_to_addition(AddOrSub, Id1, Constant1),
get_int_value(Constant1, CVal1),
get_int_value(Constant2, CVal2),
!,
NCVal is CVal2 + CVal1 * -1,
Constraint2 = b(equal(Id1,b(integer(NCVal),integer,[])),pred,Info),
rewrite_to_idl_pos(Constraint2, NConstraint).
rewrite_to_idl_pos(Constraint, NConstraint) :-
% x = c -> rewrite(x-_zero = c) | -x = c -> rewrite(_zero-x = c)
Constraint = b(equal(Id1,Constant),pred,Info),
Id1 = b(identifier(_),_,_),
get_int_value(Constant, _),
!,
rewrite_to_difference(Id1, Diff),
Constraint2 = b(equal(Diff,Constant),pred,Info),
rewrite_to_idl_pos(Constraint2, NConstraint).