1 % b_arithmetic_expressions.pl
2 % returns for arithmetic expressions a symbolic value that can be called in CLP(FD)
3
4 % :- use_module(kernel_waitflags,[add_error_wf/5]). included in b_interpreter
5
6 b_compute_arith_expression(b(Expr,Type,Info),LS,S,R,WF) :- !,
7 (ground(Type), Type == integer -> true
8 ; add_error_wf(b_compute_arith_expression,'Arithmetic expression has illegal type: ',b(Expr,Type,Info),Info,WF)
9 ),
10 ? b_compute_arith_expression2(Expr,Info,LS,S,R,WF).
11
12 b_compute_arith_expression(Expr,LS,S,R,WF) :-
13 add_error_wf(b_compute_arith_expression,'Expression not properly wrapped: ',Expr,Expr,WF),
14 b_compute_arith_expression2(Expr,unknown,LS,S,R,WF).
15
16
17 b_compute_arith_expression2(unary_minus(Arg1),_I,LocalState,State,Value,WF) :- !,
18 b_compute_arith_expression(Arg1,LocalState,State,SV1,WF),
19 Value = '-'(SV1).
20 b_compute_arith_expression2(add(Arg1,Arg2),_I,LocalState,State,Value,WF) :- !,
21 ? b_compute_arith_expression(Arg1,LocalState,State,SV1,WF),
22 ? b_compute_arith_expression(Arg2,LocalState,State,SV2,WF),
23 Value = '+'(SV1,SV2).
24 b_compute_arith_expression2(minus(Arg1,Arg2),_I,LocalState,State,Value,WF) :- !,
25 b_compute_arith_expression(Arg1,LocalState,State,SV1,WF),
26 ? b_compute_arith_expression(Arg2,LocalState,State,SV2,WF),
27 Value = '-'(SV1,SV2).
28 b_compute_arith_expression2(multiplication(Arg1,Arg2),_I,LocalState,State,Value,WF) :- !,
29 ? b_compute_arith_expression(Arg1,LocalState,State,SV1,WF),
30 b_compute_arith_expression(Arg2,LocalState,State,SV2,WF),
31 Value = '*'(SV1,SV2).
32 b_compute_arith_expression2(Expr,Info,LocalState,State,Value,WF) :-
33 ? b_compute_expression2(Expr,integer,Info,LocalState,State,int(Value),WF).
34 %b_compute_expression2(Expr,integer,Info,LocalState,State,R,WF), R=int(Value). % this version does not seem faster
35 % kernel_objects:basic_type2(integer,int(Value)).
36
37 % be sure to register all cases here in clpfd_arith_integer_expression in b_ast_cleanup
38
39
40 % Note: calling b_compute_arith_expression will also instantiate a variable to at least the int(_) skeleton; thereby enabling propagation
41 % this is potentially better than calling the default versions of the predicates, which may wait until the int(_) skeleton is set up before propagation
42
43
44 :- use_module(clpfd_interface,[clpfd_eq_expr_optimized/2]).
45
46 b_test_arith_equal_boolean_expression(Arg1,Arg2,LocalState,State,WF) :-
47 ? b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF),
48 ? b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF),
49 ? clpfd_eq_expr_optimized(CLPFD_Expr1,CLPFD_Expr2).
50
51 %(preferences:preference(use_smt_mode,true)-> clpfd_eq_expr_optimized(CLPFD_Expr1,CLPFD_Expr2)
52 % ; clpfd_eq_expr(CLPFD_Expr1,CLPFD_Expr2)).
53 % maybe we should call clpfd_eq_expr_optimized only in SMT mode and call clpfd_eq_expr otherwise ??
54 % initially test 1077 ran considerably slower with clpfd_eq_expr_optimized