
In this tutorial we use this abstract domain as an illustration of how to implement an (in this case, non-relational) abstract domain within CiaoPP. In order to run an analysis, CiaoPP's fixpoints (whether plai, inc, di, dd, etc.) will invoke several operations in order to reason about and operate with the abstractions. These operations range from fairly simple to implement and are required by default – as the less_or_equal/2 predicate – to more complex ones that may only be required for some special optimizations. A complete list of those operations can be found in the CiaoPP reference manual.
CiaoPP provides several interfaces for defining new abstract domains, each offering different levels of abstraction. Here we will use the high-level interface, which automatically generates the more complex operations required by the top-down analysis framework (such as call_to_entry/9, exit_to_prime/7, and others) from some simpler definitions provided by the user, together with common lattice operations such as join, meet, and widening. This approach allows the user to focus primarily on the core abstraction behavior of the domain, restricting the domain to only more basic cases.
The advantages of these interfaces are that they are closer to the "mathematical" definition of an Abstract Domain, i.e., they are just basic lattice operations and abstraction/concretization functions, that do not require knowledge about the inner workings of a top-down analyzer.
Broadly, the operations that we have to define can be classified into the following categories:
Our first task is to create a module and include the interface for domain definition as follows:
:- module(interval, [], [fsyntax,assertions]). :- include(domain(basicdom_nonrel_base)). %! This is an example of a simple interval domain.This will load the interface for non-relational abstract domains.
By default, non-relational abstractions are of the form [V1/Prop1, V2/Prop2, ..., VN/PropN] with V1, ..., VN distinct program-variables and Prop1, ..., PropN their abstraction. It is also assumed that the lattice has $top as its topmost element and $bot as its bottom-most.
The interface also provides operations to get the current abstract value of a variable (current_value(Abs,Var,Value)), set a new value for a variable in the abstraction (replace_value(Abs,Var,NewValue,NewAbs)), project over a set of variables (project(Vars,Abs,Proj)), and extend the domain of the abstraction (augment(Vars,Abs,AugAbs)).
A lattice is a partially ordered set that has both least upper bound and greatest lower bound. Thus, to describe a lattice we have to define its elements, a (partial) order, the least upper bound and the greatest lower bound.
Since we want to define a domain of intervals our elements will be of the form i(X,Y), i.e., the closed interval from X to Y with X and Y integer numbers.
The order relation will be: i(X1,Y1) i(X2,Y2) iff X1 X2 and Y1 Y2. And we provide it to the domain interface via the operation less_or_equal_el/2:
inf('$inf'). neginf('$ninf'). less_or_equal_el(_, '$top') :- !. less_or_equal_el('$bot', _) :- !. less_or_equal_el(i(X1,Y1),i(X2,Y2)) :- leq(X1,X2), leq(Y1,Y2). leq(_, I) :- inf(I), !. leq(I, _) :- inf(I), !, fail. leq(NInf, _) :- neginf(NInf), !. leq(_, NInf) :- neginf(NInf), !, fail. leq(N1, N2) :- N1 =< N2.Similarly, we need to provide the least upper bound and the greatest lower bound:
glb_el(X, Top, X) :- top(Top), !. glb_el(Top, X, X) :- top(Top), !. glb_el(i(X1,Y1), i(X2,Y2), i(X3,Y3)) :- max(X1,X2,X3), min(Y1,Y2,Y3), leq(X3, Y3), !. glb_el(_, _, B) :- bot(B). lub_el(Top, _, Top) :- top(Top), !. lub_el(_, Top, Top) :- top(Top), !. lub_el(Bot, X, X) :- bot(Bot), !. lub_el(X, Bot, X) :- bot(Bot), !. lub_el(i(X1,Y1), i(X2,Y2), I) :- simplify_interval(i(~min(X1,X2),~max(Y1,Y2)), I). %% If i(-∞,∞) -> $topIs common to also equip the lattice with a widen operator that ensures the termination of the fixpoint. In this case we define a rather aggressive widening that widens each side of the interval if any precision is lost while lubbing. Notice that we need to inform the fixpoint that we consider this domain to need a widening operation.
needs(widen). % Tell CiaoPP that a widen is necessary. widen_el(Bot, W, W) :- bot(Bot), !. widen_el(W, Bot, W) :- bot(Bot), !. widen_el(Top, _, Top) :- top(Top), !. widen_el(_, Top, Top) :- top(Top), !. widen_el(I1, I2, W) :- I1 = i(X,X), number(X), I2 = i(Y,Y), number(Y), !, lub_el(I1,I2,W). widen_el(I1, I2, W) :- lub_el(I1,I2,Lub), min_interval(Lub,MinLub), max_interval(Lub,MaxLub), % if the lower bound lub is smaller than any of lower bounds, widen ( ( \+ leq(~min_interval(I1), MinLub) ; \+ leq(~min_interval(I1), MinLub)) -> neginf(W0) ; W0 = MinLub ), % if the upper bound lub is bigger than any of the upper bounds, widen ( ( \+ leq(MaxLub, ~max_interval(I1)) ; \+ leq(MaxLub, ~max_interval(I2))) -> inf(W1) ; W1 = MaxLub ), simplify_interval(i(W0, W1), W).
Now that we have a lattice, the next step is to map values from the concrete world (a.k.a. the program) to our freshly defined interval lattice. In the context of pure Logic Programming, only unification has to be abstracted; however, in real programs, other structures (builtins) appear. The interface only requires an operation to abstract the unification, while another (optional) operation handles the rest of the builtins.
First, we define the abstraction of unification. When encountering an unification, the interface will reduce it to a list of simpler unifications of the form X=T where X is a program variable and T a term. Thus, we only need to describe how to abstract terms, since the interface will take care of updating the abstraction as well:
abstract_term(T,Abs,Value) :- var(T), !, current_value(Abs,T,Value). % If it is a variable, get its value abstract_term(N,_Abs,Value) :- integer(N), !, Value = i(N,N). abstract_term(_T,_Abs,'$top'). %% In any other case it is top.Unification was quite easy; however, this domain deals with numeric values and we want to abstract other kinds of operations. All of these operations are builtins and are treated slightly differently. First, we need to identify which builtins our domain may know how to abstract and then describe their behavior. Notice that in logic programs, arithmetic operations are evaluated with the is/2 builtin. For simplicity, we assume the left side to be a variable (or an integer value).
known_builtin('is/2'). known_builtin('>/2'). known_builtin('</2'). known_builtin('>=/2'). known_builtin('=</2'). %! We assume that the left side is a var o a value. abstract_literal('is/2', is(X,Y), Call, Succ) :- abstract_term(X, Call, ValX), abstract_eval(Y, Call, ValY), glb_el(ValX, ValY, Glb), replace_value(Call, X, Glb, Succ). abstract_literal('=</2',=<(X,Y), Call, Succ) :- abstract_term(X, Call, ValX), abstract_term(Y, Call, ValY), glb_el(ValX, ValY, Glb), abstract_leq_values(Glb,X,ValX,Y,ValY,Call,Succ). abstract_literal('>=/2',>=(X,Y), Call, Succ) :- abstract_literal('=</2',=<(Y,X),Call,Succ). abstract_literal('>/2', >(X,Y), Call, Succ) :- abstract_literal('>=/2',>=(X,Y),Call,Succ). abstract_literal('</2', <(X,Y), Call, Succ) :- abstract_literal('=</2',=<(X,Y),Call,Succ). abstract_eval(X,Abs,Value) :- var(X), !, abstract_term(X,Abs,Value). abstract_eval(X,_Abs,Value) :- number(X), !, Value = i(X,X). abstract_eval(+(X,Y),Abs,Value) :- abstract_eval(X,Abs,IX), abstract_eval(Y,Abs,IY), abstract_op(+,IX,IY,Value). abstract_eval(-(X,Y),Abs,Value) :- abstract_eval(X,Abs,IX), abstract_eval(Y,Abs,IY), abstract_op(-,IX,IY,Value). abstract_eval(*(X,Y),Abs,Value) :- abstract_eval(X,Abs,IX), abstract_eval(Y,Abs,IY), abstract_op(*,IX,IY,Value). abstract_eval(/(X,Y),Abs,Value) :- abstract_eval(X,Abs,IX), abstract_eval(Y,Abs,IY), abstract_op(/,IX,IY,Value). abstract_op(+,i(X1,Y1),i(X2,Y2),I) :- !, abstract_sum(i(X1,Y1),i(X2,Y2),I). abstract_op(-,i(X1,Y1),i(X2,Y2),I) :- !, abstract_diff(i(X1,Y1),i(X2,Y2),I). abstract_op(*,i(X1,Y1),i(X2,Y2),I) :- !, abstract_prod(i(X1,Y1),i(X2,Y2),I). abstract_op(/,i(X1,Y1),i(X2,Y2),I) :- !, abstract_div(i(X1,Y1),i(X2,Y2),I). abstract_op(_,I1,_I2,I) :- bot(I1), !, bot(I). abstract_op(_,_I1,I2,I) :- bot(I2), !, bot(I). abstract_op(_,I1,_I2,I) :- top(I1), !, top(I). abstract_op(_,_I1,I2,I) :- top(I2), !, top(I).
Ciao's Assertion Language provides a rich way to express analysis information, which can later be used or provide extra information to the analysis.
This requires, however, translating the internal representation used by our domain (Var/i(X,Y)) into properties that Ciao understands and vice versa. In this case, we will translate it to the constraint property – a native property of the Ciao Assertion Language. To do so, we need to iterate over the abstraction and generate the constraints.
asub_to_prop(Bottom,'$bottom') :- is_bottom(Bottom), !. asub_to_prop(ASub,constraint(Props)) :- asub_to_ctr(ASub,Props). asub_to_ctr([],[]). asub_to_ctr([V/I|ASub],Ctrs) :- elem_to_ctr(I,V,CtrI), asub_to_ctr(ASub,Ctrs0), append(CtrI,Ctrs0,Ctrs). elem_to_ctr(i(X,Y),_Var,Ctr) :- neginf(X), inf(Y), !, Ctr = []. elem_to_ctr(i(X,Y),Var,Ctr) :- neginf(X), !, Ctr = [Var =< Y]. elem_to_ctr(i(X,Y),Var,Ctr) :- inf(Y), !, Ctr = [Var >= X]. elem_to_ctr(i(X,Y),Var,Ctr) :- !, Ctr = [X =< Var, Var =< Y]. elem_to_ctr(_,_,[]).Similarly, we want to be able to read information from the Ciao Assertion Language. This is a little bit more complex. First, we need to signal which properties our domain can understand and collect all the info. Then all the collected info has to be translated to our internal representation.
% collect all the info, is called by CiaoPP with Struct0 an accumulator input_interface(constraint(Ctrs), _Kind, Struct0, Struct1) :- append(Struct0,Ctrs,Struct1). input_user_interface(Ctrs, Qv, NewASub, _, _) :- topmost(Qv,_,TopASub), ctrs_to_intervals(Ctrs,TopASub,NewASub). % Assume simple correct constraints X >= Num, etc ctrs_to_intervals([],ASub0,NewASub) :- !, NewASub = ASub0. ctrs_to_intervals([X=Y|Ctrs],ASub0,NewASub) :- var(X), number(Y), current_value(ASub0,X,'$top'), replace_value(ASub0,X,i(Y,Y),ASub1), ctrs_to_intervals(Ctrs,ASub1,NewASub). ctrs_to_intervals([X=Y|Ctrs],ASub0,NewASub) :- var(Y), number(X),!, ctrs_to_intervals([Y=X|Ctrs],ASub0,NewASub). ctrs_to_intervals([=<(X,Y)|Ctrs],ASub0,NewASub) :- var(X), number(Y), valid_value_r(X,ASub0,LeftSide),!, % top or inf on the right-side. replace_value(ASub0,X,i(LeftSide,Y),ASub1), ctrs_to_intervals(Ctrs,ASub1,NewASub). ctrs_to_intervals([>=(X,Y)|Ctrs],ASub0,NewASub) :- var(X), number(Y), valid_value_l(X,ASub0,RightSide),!, % top or -inf on the left-side. replace_value(ASub0,X,i(Y,RightSide),ASub1), ctrs_to_intervals(Ctrs,ASub1,NewASub). ctrs_to_intervals([=<(X,Y)|Ctrs],ASub0,NewASub) :- var(Y), number(X), !, ctrs_to_intervals([>=(Y,X)|Ctrs],ASub0,NewASub). ctrs_to_intervals([>=(X,Y)|Ctrs],ASub0,NewASub) :- var(Y), number(X), !, ctrs_to_intervals([=<(Y,X)|Ctrs],ASub0,NewASub). ctrs_to_intervals([>(X,Y)|Ctrs],ASub0,NewASub) :- ctrs_to_intervals([>=(X,Y)|Ctrs],ASub0,NewASub). ctrs_to_intervals([<(X,Y)|Ctrs],ASub0,NewASub) :- ctrs_to_intervals([=<(X,Y)|Ctrs],ASub0,NewASub). ctrs_to_intervals([_|Ctrs],ASub0,NewASub) :- %% Ignore other ctrs !, ctrs_to_intervals(Ctrs,ASub0,NewASub).
Now that we have implemented our domain, it is time to see it in action. The following program presents some behaviors we would like to abstract with our interval domain:
:- module(test_interval, [], []). % Compute the sum and difference of two numbers example1(A, B, Sum, Diff) :- Sum is A + B, Diff is A - B. % Compute the product and division of two numbers example2(X, Y, Prod, Quot) :- Prod is X * Y, Y \= 0, Quot is X / Y. % Test predicates with known ranges :- export(test1/2). test1(Sum, Diff) :- example1(2, 5, Sum, Diff). :- export(test2/2). test2(Prod, Quot) :- example2(3, 2, Prod, Quot). % Assign values and propagate intervals :- export(test3/3). test3(X, Y, Z) :- X >= 1, % X in [1, 4] X =< 4, Y >= 2, % Y in [2, 6] Y =< 6, Z is X + Y. % Sum will be in [3, 10] % Mixed arithmetic with bounds :- export(test4/4). test4(A, B, C, D) :- A >= 2, % A in [2, 3] A =< 3, B >= 1, % B in [1, 2] B =< 2, C is A * B, % Product interval D is C - A. % Difference interval :- export(test5/3). test5(P, Q, R) :- P =< 5, % P in (-∞, 5] Q >= 10, % Q in [10, ∞) R is P / Q. % R in (0.5, ∞)To run the analysis, open the CiaoPP toplevel, load the domain we just implemented with:
?- use_module('path/to/interval.pl').and then run:?- module('path/to/test_interval.pl'), analyze(interval), output.This will load the target module and run the analysis using our interval domain. If you run the analysis with the flag :- set_pp_flag(pp_info,on) your analysis output will look like this::- module(_1,[],[assertions]). :- export(test1/2). :- export(test2/2). :- export(test3/3). :- export(test4/4). :- export(test5/3). :- true pred example1(A,B,Sum,Diff) : (native_props:constraint([2=<A,A=<2,5=<B,B=<5])) => (native_props:constraint([2=<A,A=<2,5=<B,B=<5,7=<Sum,Sum=<7,-3=<Diff,Diff=< -3])). example1(A,B,Sum,Diff) :- true(native_props:constraint([2=<A,A=<2,5=<B,B=<5])), Sum is A+B, true(native_props:constraint([2=<A,A=<2,5=<B,B=<5,7=<Sum,Sum=<7])), Diff is A-B, true(native_props:constraint([2=<A,A=<2,5=<B,B=<5,7=<Sum,Sum=<7,-3=<Diff,Diff=< -3])). :- true pred example2(X,Y,Prod,Quot) : (native_props:constraint([3=<X,X=<3,2=<Y,Y=<2])) => (native_props:constraint([3=<X,X=<3,2=<Y,Y=<2,6=<Prod,Prod=<6,1.5=<Quot,Quot=<1.5])). example2(X,Y,Prod,Quot) :- true(native_props:constraint([3=<X,X=<3,2=<Y,Y=<2])), Prod is X*Y, true(native_props:constraint([3=<X,X=<3,2=<Y,Y=<2,6=<Prod,Prod=<6])), Y\=0, true(native_props:constraint([3=<X,X=<3,2=<Y,Y=<2,6=<Prod,Prod=<6])), Quot is X/Y, true(native_props:constraint([3=<X,X=<3,2=<Y,Y=<2,6=<Prod,Prod=<6,1.5=<Quot,Quot=<1.5])). :- true pred test1(Sum,Diff) : (native_props:constraint([])) => (native_props:constraint([7=<Sum,Sum=<7,-3=<Diff,Diff=< -3])). test1(Sum,Diff) :- true(native_props:constraint([])), example1(2,5,Sum,Diff), true(native_props:constraint([7=<Sum,Sum=<7,-3=<Diff,Diff=< -3])). :- true pred test2(Prod,Quot) : (native_props:constraint([])) => (native_props:constraint([6=<Prod,Prod=<6,1.5=<Quot,Quot=<1.5])). test2(Prod,Quot) :- true(native_props:constraint([])), example2(3,2,Prod,Quot), true(native_props:constraint([6=<Prod,Prod=<6,1.5=<Quot,Quot=<1.5])). :- true pred test3(X,Y,Z) : (native_props:constraint([])) => (native_props:constraint([1=<X,X=<4,2=<Y,Y=<6,3=<Z,Z=<10])). test3(X,Y,Z) :- true(native_props:constraint([])), X>=1, true(native_props:constraint([X>=1])), X=<4, true(native_props:constraint([1=<X,X=<4])), Y>=2, true(native_props:constraint([1=<X,X=<4,Y>=2])), Y=<6, true(native_props:constraint([1=<X,X=<4,2=<Y,Y=<6])), Z is X+Y, true(native_props:constraint([1=<X,X=<4,2=<Y,Y=<6,3=<Z,Z=<10])). :- true pred test4(A,B,C,D) : (native_props:constraint([])) => (native_props:constraint([2=<A,A=<3,1=<B,B=<2,2=<C,C=<6,0=<D,D=<3])). test4(A,B,C,D) :- true(native_props:constraint([])), A>=2, true(native_props:constraint([A>=2])), A=<3, true(native_props:constraint([2=<A,A=<3])), B>=1, true(native_props:constraint([2=<A,A=<3,B>=1])), B=<2, true(native_props:constraint([2=<A,A=<3,1=<B,B=<2])), C is A*B, true(native_props:constraint([2=<A,A=<3,1=<B,B=<2,2=<C,C=<6])), D is C-A, true(native_props:constraint([2=<A,A=<3,1=<B,B=<2,2=<C,C=<6,0=<D,D=<3])). :- true pred test5(P,Q,R) : (native_props:constraint([])) => (native_props:constraint([P=<5,Q>=10,R=<0.5])). test5(P,Q,R) :- true(native_props:constraint([])), P=<5, true(native_props:constraint([P=<5])), Q>=10, true(native_props:constraint([P=<5,Q>=10])), R is P/Q, true(native_props:constraint([P=<5,Q>=10,R=<0.5])).
In this tutorial, we have illustrated how to define a simple non-relational interval abstract domain in CiaoPP, covering the key steps from lattice definition to value abstraction and interaction with the assertion language. While the interval domain is a classic example, the principles demonstrated here apply to a wide range of abstract domains. By using CiaoPP's high-level interfaces, domain developers can focus on the core mathematical properties of their abstractions, leaving the system to handle many of the low-level operational details. This approach makes it easier to experiment with new domains, reason about program properties, and extend static analyses in a modular and systematic way. For more advanced use cases, relational domains can be defined as well, though they require a more involved setup and are beyond the scope of this short tutorial.
You can inspect the full code below. If you want to run it in the Playground, make sure that CiaoPP is loaded there first. You can also run some tests to observe the behavior of some of the operations defined above.
:- module(interval, [], [fsyntax,assertions]).
:- include(domain(basicdom_nonrel_base)).
% --- Arithmetic abstraction tests --------------------------------------
:- test abstract_literal('is/2', is(X, 2+Y), Call, Succ)
: (Call = [X/'$top', Y/i(1,3)])
=> (Succ = [X/i(3,5), Y/i(1,3)]).
:- test abstract_literal('is/2', is(Z, X/Y), Call, Succ)
: (Call = [X/i(4,8), Y/i(1,2), Z/'$top'])
=> (Succ = [X/i(4,8), Y/i(1,2), Z/i(2.0,8.0)]).
:- test abstract_literal('is/2', is(Z, X/Y), Call, Succ)
: (Call = [X/i(-4,4), Y/i(-1,1), Z/'$top'])
=> (Succ = [X/i(-4,4), Y/i(-1,1), Z/'$top']).
% --- Inequalities ------------------------------------------------------
:- test abstract_literal('>=/2', >=(X,2), Call, Succ)
: (Call = [X/i(0,5)])
=> (Succ = [X/i(2,5)]).
:- test abstract_literal('=</2', =<(X,4), Call, Succ)
: (Call = [X/i(2,10)])
=> (Succ = [X/i(2,4)]).
:- test abstract_literal('=</2', =<(X,10), Call, Mid)
: ( Call = [X/i(0,20)] )
=> (abstract_literal('>=/2', >=(X,4), Mid, Succ),
Succ = [X/i(4,10)]
).
% --- Inconsistent constraints -----------------------------------------
:- test abstract_literal('>=/2', >=(X,8), Call, M1)
: (Call = [X/i(0,10)])
=> (abstract_literal('=</2', =<(X,5), M1, Succ),
Succ = [X/'$bot']
).
%--- Widen -----------------------------------------------------------------------------
:- test widen_el(V1, V2, W)
: ( V1 = i(2,3), V2 = i(1,3) )
=>( W = i('$neginf',3) ).
:- test widen_el(V1, V2, W)
: ( V1 = i(2,4), V2 = i(1,3) )
=>( W = '$top' ).
:- test widen_el(V1, V2, W)
: ( V1 = i(2,2), V2 = i(4,4) )
=>( W = i(2,4) ).
%--------------------------------------------------------------------------------
%! This is an example of a simple interval domain.
top('$top').
bot('$bot').
inf('$inf').
neginf('$neginf').
less_or_equal_el(_, '$top') :- !.
less_or_equal_el('$bot', _) :- !.
less_or_equal_el(i(X1,Y1),i(X2,Y2)) :-
leq(X1,X2),
leq(Y1,Y2).
leq(_, I) :- inf(I), !.
leq(I, _) :- inf(I), !, fail.
leq(NInf, _) :- neginf(NInf), !.
leq(_, NInf) :- neginf(NInf), !, fail.
leq(N1, N2) :-
N1 =< N2.
%--------------------------------------------------------------------------------
glb_el(X, Top, X) :- top(Top), !.
glb_el(Top, X, X) :- top(Top), !.
glb_el(i(X1,Y1), i(X2,Y2), i(X3,Y3)) :-
max(X1,X2,X3),
min(Y1,Y2,Y3),
leq(X3, Y3), !.
glb_el(_, _, B) :-
bot(B).
lub_el(Top, _, Top) :- top(Top), !.
lub_el(_, Top, Top) :- top(Top), !.
lub_el(Bot, X, X) :- bot(Bot), !.
lub_el(X, Bot, X) :- bot(Bot), !.
lub_el(i(X1,Y1), i(X2,Y2), I) :-
simplify_interval(i(~min(X1,X2),~max(Y1,Y2)), I). %% If i(-∞,∞) -> $top
%--------------------------------------------------------------------------------
needs(widen).
:- export(widen_el/3).
widen_el(Bot, W, W) :- bot(Bot), !.
widen_el(W, Bot, W) :- bot(Bot), !.
widen_el(Top, _, Top) :- top(Top), !.
widen_el(_, Top, Top) :- top(Top), !.
widen_el(I1, I2, W) :-
I1 = i(X,X), number(X),
I2 = i(Y,Y), number(Y), !,
lub_el(I1,I2,W).
widen_el(I1, I2, W) :-
lub_el(I1,I2,Lub),
min_interval(Lub,MinLub),
max_interval(Lub,MaxLub),
% if the lower bound lub is smaller than any of lower bounds, widen
( ( \+ leq(~min_interval(I1), MinLub) ;
\+ leq(~min_interval(I1), MinLub)) ->
neginf(W0)
;
W0 = MinLub
),
% if the upper bound lub is bigger than any of the upper bounds, widen
( ( \+ leq(MaxLub, ~max_interval(I1)) ;
\+ leq(MaxLub, ~max_interval(I2))) ->
inf(W1)
;
W1 = MaxLub
),
simplify_interval(i(W0, W1), W).
%--------------------------------------------------------------------------------
abstract_term(X,Abs,Value) :-
var(X), !, current_value(Abs,X,Value). % If is a variable, get its value
abstract_term(N,_Abs,Value) :-
number(N), !, Value = i(N,N).
abstract_term(_T,_Abs,'$top'). %% In any other case it is top.
%--------------------------------------------------------------------------------
known_builtin('is/2').
known_builtin('>/2').
known_builtin('</2').
known_builtin('>=/2').
known_builtin('=</2').
:- export(abstract_literal/4).
abstract_literal('is/2', is(X,Y), Call, Succ) :-
abstract_term(X, Call, ValX),
abstract_eval(Y, Call, ValY),
glb_el(ValX, ValY, Glb),
replace_value(Call, X, Glb, Succ).
abstract_literal('=</2',=<(X,Y), Call, Succ) :-
abstract_term(X, Call, ValX),
abstract_term(Y, Call, ValY),
glb_el(ValX, ValY, Glb),
abstract_leq_values(Glb,X,ValX,Y,ValY,Call,Succ).
abstract_literal('>=/2',>=(X,Y), Call, Succ) :-
abstract_literal('=</2',=<(Y,X),Call,Succ).
abstract_literal('>/2', >(X,Y), Call, Succ) :-
abstract_literal('>=/2',>=(X,Y),Call,Succ).
abstract_literal('</2', <(X,Y), Call, Succ) :-
abstract_literal('=</2',=<(X,Y),Call,Succ).
abstract_eval(X,Abs,Value) :-
var(X), !, abstract_term(X,Abs,Value).
abstract_eval(X,_Abs,Value) :-
number(X), !, Value = i(X,X).
abstract_eval(+(X,Y),Abs,Value) :-
abstract_eval(X,Abs,IX),
abstract_eval(Y,Abs,IY),
abstract_op(+,IX,IY,Value).
abstract_eval(-(X,Y),Abs,Value) :-
abstract_eval(X,Abs,IX),
abstract_eval(Y,Abs,IY),
abstract_op(-,IX,IY,Value).
abstract_eval(*(X,Y),Abs,Value) :-
abstract_eval(X,Abs,IX),
abstract_eval(Y,Abs,IY),
abstract_op(*,IX,IY,Value).
abstract_eval(/(X,Y),Abs,Value) :-
abstract_eval(X,Abs,IX),
abstract_eval(Y,Abs,IY),
abstract_op(/,IX,IY,Value).
abstract_op(+,i(X1,Y1),i(X2,Y2),I) :- !, abstract_sum(i(X1,Y1),i(X2,Y2),I).
abstract_op(-,i(X1,Y1),i(X2,Y2),I) :- !, abstract_diff(i(X1,Y1),i(X2,Y2),I).
abstract_op(*,i(X1,Y1),i(X2,Y2),I) :- !, abstract_prod(i(X1,Y1),i(X2,Y2),I).
abstract_op(/,i(X1,Y1),i(X2,Y2),I) :- !, abstract_div(i(X1,Y1),i(X2,Y2),I).
abstract_op(_,I1,_I2,I) :- bot(I1), !, bot(I).
abstract_op(_,_I1,I2,I) :- bot(I2), !, bot(I).
abstract_op(_,I1,_I2,I) :- top(I1), !, top(I).
abstract_op(_,_I1,I2,I) :- top(I2), !, top(I).
abstract_leq_values(Glb,_X,ValX,_Y,ValY,Call,Succ) :-
bot(Glb),
less_or_equal_el(ValX,ValY), !,
Succ = Call.
abstract_leq_values(Glb,_,_,_,_,_,Succ) :-
bot(Glb), !, Succ = '$bottom'.
abstract_leq_values(_Glb,X,ValX,Y,ValY,Call,Succ) :-
max_interval(ValX,MaxX),
min_interval(ValX,MinX),
max_interval(ValY,MaxY),
min_interval(ValY,MinY),
min(MaxX,MaxY,MinMax),
max(MinX,MinY,MaxMin),
NValX = ~simplify_interval(i(MinX,MinMax)),
NValY = ~simplify_interval(i(MaxMin,MaxY)),
replace_value(Call,X,NValX,Succ0),
replace_value(Succ0,Y,NValY,Succ).
abstract_sum(i(X1,Y1),i(X2,Y2), I) :-
add(X1,X2,X3),
add(Y1,Y2,Y3),
simplify_interval(i(X3,Y3),I).
abstract_diff(i(X1,Y1),i(X2,Y2),I) :-
subtract(X1,X2,X3),
subtract(Y1,Y2,Y3),
simplify_interval(i(X3,Y3),I).
abstract_prod(i(X1,Y1),i(X2,Y2),I) :-
multiply(X1,X2,A),
multiply(X1,Y2,B),
multiply(Y1,X2,C),
multiply(Y1,Y2,D),
min_list([A,B,C,D],X3),
max_list([A,B,C,D],Y3),
simplify_interval(i(X3,Y3),I).
abstract_div(_,i(0,0),I) :- !, bot(I). % Can't divide by zero
abstract_div(_,i(X2,Y2),I) :- % Can converge to ∞ or -∞.
leq(X2,0), leq(0,Y2), !, top(I).
abstract_div(i(X1,Y1),i(X2,Y2),I) :-
divide(X1,X2,A),
divide(X1,Y2,B),
divide(Y1,X2,C),
divide(Y1,Y2,D),
min_list([A,B,C,D],X3),
max_list([A,B,C,D],Y3),
simplify_interval(i(X3,Y3),I).
%% we assume won't have something like -∞ + ∞,
add(Inf,_,A) :- inf(Inf), !, A = Inf.
add(_,Inf,A) :- inf(Inf), !, A = Inf.
add(NInf,_,A) :- neginf(NInf), !, A = NInf.
add(_,NInf,A) :- neginf(NInf), !, A = NInf.
add(X,Y,S) :- S is X + Y.
%
subtract(Inf,_,A) :- inf(Inf), !, A = Inf.
subtract(_,Inf,A) :- inf(Inf), !, neginf(A).
subtract(NInf,_,A) :- neginf(NInf), !, A = NInf.
subtract(_,NInf,A) :- neginf(NInf), !, inf(A).
subtract(X,Y,S) :- S is X - Y.
%
multiply(V0, V1, 0) :-
(V0 = 0 ; V1 = 0), !.
multiply(V0,V1,V2) :-
inf(V0), !,
( leq(0,V1) ->
V2 = V0
;
neginf(V2)
).
multiply(V0,V1,V2) :-
neginf(V0), !,
( leq(0,V1) ->
V2 = V0
;
inf(V2)
).
multiply(V0,V1,V2) :-
inf(V1), !,
( leq(0,V0) ->
V2 = V1
;
neginf(V2)
).
multiply(V0,V1,V2) :-
neginf(V1), !,
( leq(0,V0) ->
V2 = V1
;
inf(V2)
).
multiply(V0,V1,V2) :- V2 is V0*V1.
divide(V0,V1,V2) :-
inf(V0), !,
( leq(0,V1) ->
V2 = V0
;
neginf(V2)
).
divide(V0,V1,V2) :-
neginf(V0), !,
( leq(0,V1) ->
V2 = V0
;
inf(V2)
).
divide(_V0,V1,V2) :-
( inf(V1) ; neginf(V1) ), !, V2 = 0.
divide(V0,V1,V2) :- V2 is V0/V1.
%--------------------------------------------------------------------------------
asub_to_native(ASub, _, _, [Props], []) :-
asub_to_prop(ASub,Props).
asub_to_prop(Bottom,'$bottom') :- is_bottom(Bottom), !.
asub_to_prop(ASub,constraint(Props)) :-
asub_to_ctr(ASub,Props).
asub_to_ctr([],[]).
asub_to_ctr([V/I|ASub],Ctrs) :-
elem_to_ctr(I,V,CtrI),
asub_to_ctr(ASub,Ctrs0),
append(CtrI,Ctrs0,Ctrs).
elem_to_ctr(i(X,Y),_Var,Ctr) :-
neginf(X), inf(Y), !, Ctr = [].
elem_to_ctr(i(X,Y),Var,Ctr) :-
neginf(X), !, Ctr = [Var =< Y].
elem_to_ctr(i(X,Y),Var,Ctr) :-
inf(Y), !, Ctr = [Var >= X].
elem_to_ctr(i(X,Y),Var,Ctr) :-
!, Ctr = [X =< Var, Var =< Y].
elem_to_ctr(_,_,[]).
is_bottom('$bottom').
is_bottom([_/'$bot'|_]).
is_bottom([_|ASub]) :- is_bottom(ASub).
%--------------------------------------------------------------------------------
input_interface(constraint(Ctrs), _Kind, Struct0, Struct1) :-
append(Struct0,Ctrs,Struct1).
input_user_interface(Ctrs, Qv, NewASub, _, _) :-
topmost(Qv,_,TopASub),
ctrs_to_intervals(Ctrs,TopASub,NewASub).
% Assume simple correct constraints X >= Num, etc
ctrs_to_intervals([],ASub0,NewASub) :- !, NewASub = ASub0.
ctrs_to_intervals([X=Y|Ctrs],ASub0,NewASub) :-
var(X), number(Y), current_value(ASub0,X,'$top'),
replace_value(ASub0,X,i(Y,Y),ASub1),
ctrs_to_intervals(Ctrs,ASub1,NewASub).
ctrs_to_intervals([X=Y|Ctrs],ASub0,NewASub) :-
var(Y), number(X),!,
ctrs_to_intervals([Y=X|Ctrs],ASub0,NewASub).
ctrs_to_intervals([=<(X,Y)|Ctrs],ASub0,NewASub) :-
var(X), number(Y), valid_value_r(X,ASub0,LeftSide),!, % top or inf in right-side.
replace_value(ASub0,X,i(LeftSide,Y),ASub1),
ctrs_to_intervals(Ctrs,ASub1,NewASub).
ctrs_to_intervals([>=(X,Y)|Ctrs],ASub0,NewASub) :-
var(X), number(Y), valid_value_l(X,ASub0,RightSide),!, % top or -inf in left
replace_value(ASub0,X,i(Y,RightSide),ASub1),
ctrs_to_intervals(Ctrs,ASub1,NewASub).
ctrs_to_intervals([=<(X,Y)|Ctrs],ASub0,NewASub) :-
var(Y), number(X), !, ctrs_to_intervals([>=(Y,X)|Ctrs],ASub0,NewASub).
ctrs_to_intervals([>=(X,Y)|Ctrs],ASub0,NewASub) :-
var(Y), number(X), !, ctrs_to_intervals([=<(Y,X)|Ctrs],ASub0,NewASub).
ctrs_to_intervals([>(X,Y)|Ctrs],ASub0,NewASub) :-
ctrs_to_intervals([>=(X,Y)|Ctrs],ASub0,NewASub).
ctrs_to_intervals([<(X,Y)|Ctrs],ASub0,NewASub) :-
ctrs_to_intervals([=<(X,Y)|Ctrs],ASub0,NewASub).
ctrs_to_intervals([_|Ctrs],ASub0,NewASub) :- %% Ignore other ctrs
!, ctrs_to_intervals(Ctrs,ASub0,NewASub).
valid_value_l(X,ASub,RightSide) :-
current_value(ASub,X,Val), !,
( Val = '$top' ->
inf(RightSide)
;
Val = i(_,RightSide)
).
valid_value_r(X,ASub,LeftSide) :-
current_value(ASub,X,Val), !,
( Val = '$top' ->
neginf(LeftSide)
;
Val = i(LeftSide,_)
).
%--------------------------------------------------------------------------------
% Utilities
simplify_interval(i(NInf, Inf), Top) :-
neginf(NInf),
inf(Inf), !,
top(Top).
simplify_interval(E, E).
max_interval('$top',M) :- !, inf(M).
max_interval(i(_,Y),Y) :- !.
min_interval('$top',M) :- !, neginf(M).
min_interval(i(X,_),X) :- !.
max(N0,N1,N0) :- leq(N1, N0), !.
max(_,N1,N1).
max_list(L, X) :-
neginf(Min),
max_list_(L, Min, X).
max_list_([], X, X).
max_list_([X|Xs], Max0,Max) :-
max(X,Max0,Max1),
max_list_(Xs, Max1, Max).
min(N0,N1,N1) :- leq(N1, N0), !.
min(N0,_,N0).
min_list(L, X) :-
inf(Max),
min_list_(L, Max, X).
min_list_([], X, X).
min_list_([X|Xs], Min0, Min) :-
min(X,Min0,Min1),
min_list_(Xs, Min1, Min).