| 1 | | % (c) 2016-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, |
| 2 | | % Heinrich Heine Universitaet Duesseldorf |
| 3 | | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) |
| 4 | | |
| 5 | | :- module(clpfd_tables,[can_translate_avl_to_table/3, translate_avl_to_table/2, check_element_of_table/4, |
| 6 | | check_element_of_avl_with_table/4, |
| 7 | | clpfd_in_table/2, |
| 8 | | |
| 9 | | can_translate_function_to_element_constraint/2, |
| 10 | | check_apply_with_element_constraint/5, check_apply_with_element_constraint/6]). |
| 11 | | |
| 12 | | :- use_module(module_information,[module_info/2]). |
| 13 | | :- module_info(group,kernel). |
| 14 | | :- module_info(description,'Provide interface to CLP(FD) table constraint.'). |
| 15 | | |
| 16 | | % Example usage of CLPFD table/2 : |
| 17 | | %| ?- table([[X,Y]],[[1,2],[2,3],[3,4],[4,5]]), X in 1..2. |
| 18 | | %X in 1..2, |
| 19 | | %Y in 2..3 ? |
| 20 | | |
| 21 | | :- use_module(library(clpfd)). |
| 22 | | :- use_module(library(avl),[avl_domain/2]). |
| 23 | | :- use_module(library(lists),[maplist/3, maplist/4, maplist/2]). |
| 24 | | :- use_module(kernel_waitflags,[add_fd_variables_for_labeling/2]). |
| 25 | | :- use_module(debug,[debug_println/2]). |
| 26 | | :- use_module(error_manager,[add_internal_error/2]). |
| 27 | | |
| 28 | | :- use_module(self_check). |
| 29 | | :- assert_must_succeed((custom_explicit_sets:construct_avl_from_lists([(int(1),int(2)),(int(2),int(3))],avl_set(A)), |
| 30 | | clpfd_tables:can_translate_avl_to_table(A,Skel,wf), Skel==(integer,integer))). |
| 31 | | :- assert_must_succeed((custom_explicit_sets:construct_avl_from_lists([(int(1),pred_true),(int(2),pred_false)],avl_set(A)), |
| 32 | | clpfd_tables:can_translate_avl_to_table(A,Skel,wf), Skel==(integer,boolean))). |
| 33 | | :- assert_must_fail((custom_explicit_sets:construct_avl_from_lists([(int(1),int(2)),(int(2),int(3))],avl_set(A)), |
| 34 | | clpfd_tables:can_translate_avl_to_table(A,_,no_wf_available))). |
| 35 | | :- assert_must_fail((custom_explicit_sets:construct_avl_from_lists([pred_true,pred_false],avl_set(A)), |
| 36 | | clpfd_tables:can_translate_avl_to_table(A,_,wf))). |
| 37 | | % large integers which are not at the root of the AVL tree: |
| 38 | | :- assert_must_fail((custom_explicit_sets:construct_avl_from_lists([(int(1),int(2)),(int(2),int(3)),(int(3),int(1152921504606846977))],avl_set(A)), |
| 39 | | clpfd_tables:can_translate_avl_to_table(A,_,wf))). |
| 40 | | :- assert_must_fail((custom_explicit_sets:construct_avl_from_lists([(int(-1152921504606846978),int(2)),(int(2),int(3)),(int(3),int(4))],avl_set(A)), |
| 41 | | clpfd_tables:can_translate_avl_to_table(A,_,wf))). |
| 42 | | :- assert_must_fail((custom_explicit_sets:construct_avl_from_lists([rec([field(a,int(1)),field(b,int(2))]),rec([field(a,int(2)),field(b,int(3))]), |
| 43 | | rec([field(a,int(3)),field(b,int(1152921504606846977))])],avl_set(A)), |
| 44 | | clpfd_tables:can_translate_avl_to_table(A,_,wf))). |
| 45 | | :- assert_must_fail((custom_explicit_sets:construct_avl_from_lists([int(1),int(2),int(1152921504606846977),int(1152921504606846978)],avl_set(A)), |
| 46 | | clpfd_tables:can_translate_avl_to_table(A,_,wf))). |
| 47 | | |
| 48 | | % can an AVL set be translated into a CLPFD table membership constraint |
| 49 | | can_translate_avl_to_table(AVL,SkeletonType,WF) :- |
| 50 | | WF \= no_wf_available, % WF needed for add_fd_variables_for_labeling below |
| 51 | | AVL = node(Element,_True,_,_,_), |
| 52 | | translate_to_fd_values(Element,SkeletonType,_ElVList,[]), |
| 53 | | SkeletonType \= boolean, % should we also disallow rec([field(_,boolean)]) % boolean treatment causes problems for 1624, 1627, 140, 1426 |
| 54 | | avl_no_overflow(AVL). % check that the table constraint will not generate an overflow, e.g., for test 1353 |
| 55 | | % we have to check all elements: checking the root or avl_min/avl_max is not sufficient |
| 56 | | % (e.g., the large integer could be in the second component of a pair); |
| 57 | | % table/3 would otherwise raise a representation error |
| 58 | | |
| 59 | | % check that no element of the AVL tree contains an integer that is too large for CLP(FD) |
| 60 | | avl_no_overflow(empty). |
| 61 | | avl_no_overflow(node(Element,_,_,L,R)) :- |
| 62 | | value_no_overflow(Element), |
| 63 | | avl_no_overflow(L), |
| 64 | | avl_no_overflow(R). |
| 65 | | |
| 66 | | :- use_module(tools_platform, [is_tagged_integer/1]). |
| 67 | | % only has to deal with the values accepted by translate_to_fd_values |
| 68 | | value_no_overflow(int(H)) :- !, is_tagged_integer(H). |
| 69 | | value_no_overflow((A,B)) :- !, value_no_overflow(A), value_no_overflow(B). |
| 70 | | value_no_overflow(rec(Fields)) :- !, l_field_no_overflow(Fields). |
| 71 | | value_no_overflow(_). |
| 72 | | |
| 73 | | l_field_no_overflow([]). |
| 74 | | l_field_no_overflow([field(_,V)|T]) :- value_no_overflow(V), l_field_no_overflow(T). |
| 75 | | |
| 76 | | translate_avl_to_table(AVL,TableList) :- |
| 77 | | avl_domain(AVL,List), |
| 78 | | maplist(translate_avl_element_to_fd_list,List,TableList). |
| 79 | | |
| 80 | | check_element_of_avl_with_table(X,SkeletonType,AVL,WF) :- |
| 81 | | translate_avl_to_table(AVL,TableList), |
| 82 | ? | check_element_of_table(X,SkeletonType,TableList,WF). |
| 83 | | |
| 84 | | check_element_of_table(X,SkeletonType,TableList,WF) :- |
| 85 | ? | translate_to_fd_values(X,SkeletonType,XList,[]), |
| 86 | ? | clpfd_in_table(XList,TableList), |
| 87 | | add_fd_variables_for_labeling(XList,WF). % important for test 1716 |
| 88 | | |
| 89 | | |
| 90 | | :- use_module(clpfd_interface,[force_clpfd_inlist/2]). |
| 91 | | get_unique_el([X],X). |
| 92 | | clpfd_in_table([X],TableList) :- !, maplist(get_unique_el,TableList,FDList), |
| 93 | | %print(posting_clpfd_inlist(X,FDList)),nl, |
| 94 | | force_clpfd_inlist(X,FDList). |
| 95 | | :- if(current_prolog_flag(dialect, sicstus)). |
| 96 | | :- if((current_prolog_flag(version_data, sicstus(4,VN,_,_,_)),VN>7)). |
| 97 | | clpfd_in_table(XList,TableList) :- |
| 98 | ? | table([XList],TableList,[order(id3)]). % use method(default) (SICStus choice handles propagation as it sees fit) |
| 99 | | :- else. |
| 100 | | clpfd_in_table(XList,TableList) :- |
| 101 | | table([XList],TableList,[method(aux)]). |
| 102 | | % we use aux to circumvent a bug in SICStus 4.4-4.7 |
| 103 | | % but method(aux) did slow down test 1753 considerably (15 secs down to 1-2 minutes, before not_exists improved) |
| 104 | | :- endif. |
| 105 | | :-else. |
| 106 | | clpfd_in_table(XList,TableList) :- |
| 107 | | table([XList],TableList). % translated to tuples_in in SWI |
| 108 | | :- endif. |
| 109 | | % from the SICStus Manual: |
| 110 | | % table(+Tuples,+Extension,+Options) |
| 111 | | % Defines an n-ary constraint by extension. Extension should be a list of lists of |
| 112 | | % integers, each of length n. Tuples should be a list of lists of domain variables, |
| 113 | | % each also of length n. The constraint holds if every Tuple in Tuples occurs in |
| 114 | | % the Extension. The constraint will maintain domain consistency. |
| 115 | | % For convenience, Extension may contain ConstantRange (see Section 10.10.13.1 |
| 116 | | % [Syntax of Indexicals], page 485) expressions in addition to integers. |
| 117 | | % |
| 118 | | % WARNING, SICSTUS 4.7 and earlier has a bug with negative numbers |
| 119 | | % | ?- clpfd:table([[X,Y]],[[1,0],[2,-1]]). |
| 120 | | % Y = 0, <-- this should not be !! |
| 121 | | % X in 1..2 |
| 122 | | % with aux as method we solve the issue |
| 123 | | % | ?- clpfd:table([[X,Y]],[[1,0],[2,-1]],[method(aux)]). |
| 124 | | % X in 1..2, |
| 125 | | % Y in-1..0 |
| 126 | | |
| 127 | | % TO DO: also allow partial FD infos, in which case we would use table/2 just for quick propagation |
| 128 | | % we could also translate variables into ConstantRange expressions (see above) |
| 129 | | translate_avl_element_to_fd_list(Val,FDList) :- translate_to_fd_values(Val,_,FDList,[]). |
| 130 | | |
| 131 | | translate_to_fd_values(FD,fd(T)) --> {var(FD),nonvar(T)},!, % setup FD bounds |
| 132 | | {get_global_type_value(FD,T,X)},[X]. |
| 133 | | translate_to_fd_values(fd(X,T),fd(T)) --> !, [X]. |
| 134 | | translate_to_fd_values(int(A),integer) --> !, [A]. % warning: could overflow, we check above ! |
| 135 | ? | translate_to_fd_values((A,B),(SkelA,SkelB)) --> !, translate_to_fd_values(A,SkelA), translate_to_fd_values(B,SkelB). |
| 136 | ? | translate_to_fd_values(rec(F),rec(SkelF)) --> !, l_translate_to_fd_values(F,SkelF). |
| 137 | | translate_to_fd_values(Var,Type) --> {var(Var),Type==boolean},!, {prop_01(Var,FD)},[FD]. |
| 138 | | translate_to_fd_values(pred_true,boolean) --> [1]. |
| 139 | | translate_to_fd_values(pred_false,boolean) --> [0]. |
| 140 | | % TO DO: also free-types |
| 141 | | |
| 142 | | l_translate_to_fd_values([],[]) --> []. |
| 143 | | l_translate_to_fd_values([field(N,F)|T],[field(N,SF)|ST]) --> |
| 144 | ? | translate_to_fd_values(F,SF),l_translate_to_fd_values(T,ST). |
| 145 | | |
| 146 | | :- block prop_01(-,-). |
| 147 | | prop_01(pred_true,1). |
| 148 | | prop_01(pred_false,0). |
| 149 | | |
| 150 | | % ---------------------- |
| 151 | | |
| 152 | | :- use_module(bsyntaxtree,[is_set_type/2]). |
| 153 | | :- use_module(preferences,[preference/2]). |
| 154 | | |
| 155 | | % can a list be translated into CLPFD element constraints for function application |
| 156 | | can_translate_function_to_element_constraint(List,FunctionType) :- |
| 157 | | FunctionType \= unknown, |
| 158 | | is_list_skeleton(List), % TO DO: allow tail AVL list + enable delaying treatment |
| 159 | | preference(find_abort_values,false), |
| 160 | | preference(use_clpfd_solver,true), |
| 161 | | % initially we checked if chr is false to avoid falling into the SICStus element/3 bug prior to 4.3.6 |
| 162 | | is_set_type(FunctionType,couple(Domain,Range)), |
| 163 | | is_simple_fd_type(Domain), % Domain must be translatable to simple FD value list |
| 164 | | is_fd_type(Range). |
| 165 | | |
| 166 | | is_list_skeleton(X) :- nonvar(X),is_list_skeleton_aux(X). |
| 167 | | is_list_skeleton_aux([]). |
| 168 | | is_list_skeleton_aux([_H|T]) :- is_list_skeleton(T). |
| 169 | | |
| 170 | | is_simple_fd_type(global(_)). |
| 171 | | is_simple_fd_type(integer). |
| 172 | | is_simple_fd_type(boolean). |
| 173 | | |
| 174 | | is_fd_type(couple(A,B)) :- !, is_fd_type(A), is_fd_type(B). |
| 175 | | is_fd_type(record(F)) :- !, is_fd_field_types(F). |
| 176 | | is_fd_type(X) :- is_simple_fd_type(X). |
| 177 | | |
| 178 | | is_fd_field_types([]). |
| 179 | | is_fd_field_types([field(_,FT)|T]) :- is_fd_type(FT), is_fd_field_types(T). |
| 180 | | |
| 181 | | :- use_module(b_global_sets,[get_global_type_value/3]). |
| 182 | | get_fd_type(couple(A,B),(VA,VB)) --> get_fd_type(A,VA), get_fd_type(B,VB). |
| 183 | | get_fd_type(global(T),FD) --> {get_global_type_value(FD,T,X)}, [X]. |
| 184 | | get_fd_type(integer,int(X)) --> [X]. |
| 185 | | get_fd_type(boolean,B) --> [FD], {prop_01(B,FD)}. |
| 186 | | get_fd_type(record(Fields),rec(VF)) --> get_field_fd_types(Fields,VF). |
| 187 | | |
| 188 | | get_field_fd_types([],[]) --> []. |
| 189 | | get_field_fd_types([field(Name,Type)|FT],[field(Name,VF)|VT]) --> |
| 190 | | get_fd_type(Type,VF), |
| 191 | | get_field_fd_types(FT,VT). |
| 192 | | |
| 193 | | get_fd_types(Domain,Range,(VD,VR),FDDom,FDRList) :- get_fd_type(Domain,VD,[FDDom],[]), get_fd_type(Range,VR,FDRList,[]). |
| 194 | | |
| 195 | | % Examples: |
| 196 | | % f = {x|->2, y|->3} & x:1..2 & y:4..5 & r = f(4) |
| 197 | | % f = {x|->(2,x+1), y|->(3,y+1)} & x:1..2 & y:4..5 & r = f(v) & v:3..4 --> deterministically sets v=4, r=(3,5),y=4 |
| 198 | | % from the SICStus manual: |
| 199 | | % element(?X,+List,?Y) |
| 200 | | % where X and Y are domain variables and List is a list of domain variables. True |
| 201 | | % if the X:th element of List is Y. Operationally, the domains of X and Y are |
| 202 | | % constrained so that for every element in the domain of X, there is a compatible |
| 203 | | % element in the domain of Y, and vice versa. |
| 204 | | % Maintains domain consistency in X and bounds consistency in List and Y. |
| 205 | | % Corresponds to nth1/3 in library(lists) and to element in MiniZinc |
| 206 | | |
| 207 | | :- use_module(library(clpfd),[all_distinct/1]). |
| 208 | | :- use_module(library(lists),[nth1/3]). |
| 209 | | :- assert_must_succeed_any((kernel_waitflags:init_wait_flags(WF,[unit_test]), clpfd:in(A,1..2), clpfd:in(B,3..4), |
| 210 | | clpfd_tables:check_apply_with_element_constraint([(int(A),int(5)),(int(B),int(6))],int(X),int(Y),set(couple(integer,integer)),WF,Posted), |
| 211 | | Posted==true, X=3, Y==6, B==3)). |
| 212 | | :- assert_must_fail((kernel_waitflags:init_wait_flags(WF,[unit_test]), clpfd:in(A,1..2), clpfd:in(B,3..4), |
| 213 | | clpfd_tables:check_apply_with_element_constraint([(int(A),int(5)),(int(B),int(6))],int(_),int(7),set(couple(integer,integer)),WF,_))). |
| 214 | | % integers which are too large for CLPFD: no exception and nothing is posted |
| 215 | | % only for SICStus: SWI's CLP(FD) library supports unbounded integers; there no overflow occurs and the constraints are posted |
| 216 | | :- if(current_prolog_flag(dialect,sicstus)). |
| 217 | | :- assert_must_succeed_any((kernel_waitflags:init_wait_flags(WF,[unit_test]), clpfd:in(A,1..2), clpfd:in(B,3..4), |
| 218 | | clpfd_tables:check_apply_with_element_constraint([(int(A),int(1152921504606846977)),(int(B),int(6))],int(X),int(Y),set(couple(integer,integer)),WF,Posted), |
| 219 | | Posted==false, var(X), var(Y))). |
| 220 | | :- assert_must_succeed_any((kernel_waitflags:init_wait_flags(WF,[unit_test]), clpfd:in(A,1..2), |
| 221 | | clpfd_tables:check_apply_with_element_constraint([(int(A),int(5)),(int(-1152921504606846978),int(6))],int(X),int(_),set(couple(integer,integer)),WF,Posted), |
| 222 | | Posted==false, var(X))). |
| 223 | | :- endif. |
| 224 | | :- use_module(clpfd_interface,[catch_clpfd_overflow_call3/3]). |
| 225 | | % a version which catches CLPFD overflows, e.g., when the function contains integers which are too large for CLPFD |
| 226 | | % Posted is false if an overflow occurred; in this case no constraints were posted and the caller has to use another treatment |
| 227 | | check_apply_with_element_constraint(List,X,Y,FunctionType,WF,Posted) :- |
| 228 | ? | catch_clpfd_overflow_call3( |
| 229 | | (check_apply_with_element_constraint(List,X,Y,FunctionType,WF), Posted=true), |
| 230 | | silent, |
| 231 | | Posted=false). |
| 232 | | |
| 233 | | check_apply_with_element_constraint(List,X,Y,FunctionType,WF) :- |
| 234 | | is_set_type(FunctionType,couple(Domain,Range)), |
| 235 | | get_fd_type(Domain,X,[FDX],[]), |
| 236 | | if(l_get_fd_types(List,DomainList,RangeLists,Domain,Range,FDX,FoundY),true, |
| 237 | | (add_internal_error('Get FD Types failed: ',get_fd_types(Domain,Range)),fail)), |
| 238 | | debug_println(4,element_domain_list(FDX,DomainList)), |
| 239 | | (FoundY \== y_not_found |
| 240 | | -> FoundY=Y % no need for equal_object: we only have FD types + Bool + couples (+ record of them) |
| 241 | | % TODO: check that for records we can also use unification |
| 242 | | ; element(Idx,DomainList,FDX), % no need to call equal_object for X as we have simple fd types |
| 243 | | % DomainList are the elements of the domain of the function |
| 244 | | (nonvar(Idx) |
| 245 | | -> nth1(Idx,List,(X,Y)) %relevant for tests 387, 1579 |
| 246 | | ; all_distinct(DomainList), % ensures that Idx will get bound eventually if we enumerate DomainList and X |
| 247 | | % (if multiple entries in DomainList then Idx would not get bound) |
| 248 | | % note: DomainList and RangeLists were already fully set up by l_get_fd_types above (as FoundY==y_not_found) |
| 249 | | get_fd_type(Range,Y,FDYList,[]), |
| 250 | | setup_element_range_constraints(FDYList,Idx,RangeLists), |
| 251 | | % used to call: add_fd_variables_for_labeling([FDX|FDYList],WF) but not sure if we need to enumerate FDYList |
| 252 | | add_fd_variables_for_labeling([FDX],WF) % FDX can have an infinite domain; e.g., test 1717 |
| 253 | | ) |
| 254 | | ). |
| 255 | | |
| 256 | | % an unfolded version of maplist(get_fd_types(Domain,Range),List,DomainList,RangeLists) |
| 257 | | % we also check if the lookup FD variable occurs in the list; if so we return the Y value in Found |
| 258 | | % relevant for performance, e.g., for NQueens60; we avoid traversing the list a second time |
| 259 | | l_get_fd_types([],[],[],_,_,_,y_not_found). |
| 260 | | l_get_fd_types([Pair|PT],[FDDom|DT],[FDRList|RT],Domain,Range,FDX,FoundY) :- |
| 261 | | get_fd_types(Domain,Range,Pair,FDDom,FDRList), |
| 262 | | (FDX==FDDom -> Pair=(_,FoundY), DT=[],RT=[] |
| 263 | | ; l_get_fd_types(PT,DT,RT,Domain,Range,FDX,FoundY)). |
| 264 | | |
| 265 | | |
| 266 | | setup_element_range_constraints([],_,_). |
| 267 | | setup_element_range_constraints([FDY|YT],Idx,RangeLists) :- |
| 268 | | maplist(get_next_fd,RangeLists,FDYList,RestLists), |
| 269 | | % print(setting_up_range_element_constraint(Idx,FDYList,FDY)),nl, |
| 270 | | element(Idx,FDYList,FDY), |
| 271 | | setup_element_range_constraints(YT,Idx,RestLists). |
| 272 | | |
| 273 | | get_next_fd([H|T],H,T). |