| 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(external_function_call(XFUN,[Arg1]),_Info,LocalState,State,Value,WF) :- | |
| 33 | unary_external_function_to_clpfd(XFUN,SV1,ClpfdExpr),!, | |
| 34 | b_compute_arith_expression(Arg1,LocalState,State,SV1,WF), | |
| 35 | Value = ClpfdExpr. | |
| 36 | b_compute_arith_expression2(Expr,Info,LocalState,State,Value,WF) :- | |
| 37 | ? | b_compute_expression2(Expr,integer,Info,LocalState,State,int(Value),WF). |
| 38 | %b_compute_expression2(Expr,integer,Info,LocalState,State,R,WF), R=int(Value). % this version does not seem faster | |
| 39 | % kernel_objects:basic_type2(integer,int(Value)). | |
| 40 | ||
| 41 | unary_external_function_to_clpfd('ABS',Arg1,abs(Arg1)). | |
| 42 | ||
| 43 | ||
| 44 | ||
| 45 | % Note: calling b_compute_arith_expression will also instantiate a variable to at least the int(_) skeleton; thereby enabling propagation | |
| 46 | % this is potentially better than calling the default versions of the predicates, which may wait until the int(_) skeleton is set up before propagation | |
| 47 | ||
| 48 | ||
| 49 | :- use_module(clpfd_interface,[clpfd_eq_expr_optimized/2]). | |
| 50 | ||
| 51 | b_test_arith_equal_boolean_expression(Arg1,Arg2,LocalState,State,WF) :- | |
| 52 | ? | b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF), |
| 53 | ? | b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF), |
| 54 | ? | clpfd_eq_expr_optimized(CLPFD_Expr1,CLPFD_Expr2). |
| 55 | ||
| 56 | %(preferences:preference(use_smt_mode,true)-> clpfd_eq_expr_optimized(CLPFD_Expr1,CLPFD_Expr2) | |
| 57 | % ; clpfd_eq_expr(CLPFD_Expr1,CLPFD_Expr2)). | |
| 58 | % maybe we should call clpfd_eq_expr_optimized only in SMT mode and call clpfd_eq_expr otherwise ?? | |
| 59 | % initially test 1077 ran considerably slower with clpfd_eq_expr_optimized |