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