| 1 | % (c) 2025-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(b2asp_code_gen,[gen_clingo_fact/4, | |
| 6 | register_clingo_predicate/5, | |
| 7 | gen_clingo_pred_clause/5, | |
| 8 | gen_clingo_clause/5, | |
| 9 | gen_clingo_ic_constraint/1, | |
| 10 | gen_clingo_set_identifier/4, gen_clingo_scalar_identifier/4, | |
| 11 | gen_clingo_count_aggregate/4, | |
| 12 | gen_base_type_constraint/3, make_finite/3, % TODO: remove from export, move add_local_typed_ids inside | |
| 13 | conjoin_clingo_calls/3, | |
| 14 | clingo_format_comment/2, | |
| 15 | generated_id/2, deterministic_id_value/2, | |
| 16 | gensym_if_necessary/2, | |
| 17 | add_translation_error/2, add_translation_error/3, translation_error_occurred/0, | |
| 18 | type_restricted/1, | |
| 19 | reset_b2asp_code_gen/0]). | |
| 20 | ||
| 21 | :- if(current_module(module_information)). | |
| 22 | :- use_module(probsrc(module_information),[module_info/2]). | |
| 23 | :- module_info(group,b2asp). | |
| 24 | :- module_info(description,'Generate Clingo Predicates and Clauses.'). | |
| 25 | :- endif. | |
| 26 | ||
| 27 | :- use_module(library(lists)). | |
| 28 | :- use_module(library(ordsets),[ord_member/2, ord_intersection/3]). | |
| 29 | :- use_module(library(codesio), [format_to_codes/3]). | |
| 30 | :- use_module(clingo_interface,[register_clause_head/2, register_clingo_generated_id/4, clingo_generated_id/4, | |
| 31 | get_nr_of_registered_strings/1]). | |
| 32 | :- use_module(probsrc(error_manager)). | |
| 33 | :- use_module(probsrc(debug)). | |
| 34 | :- use_module(probsrc(preferences),[get_preference/2]). | |
| 35 | :- use_module(probsrc(b_global_sets),[b_get_fd_type_bounds/3, lookup_global_constant/2]). | |
| 36 | ||
| 37 | ||
| 38 | % Errors during the translation are recorded, so that clingo is not run on an incomplete translation: | |
| 39 | % the clingo program would then not encode the B formula and could produce wrong solutions or contradictions | |
| 40 | :- dynamic translation_error_occurred/0. | |
| 41 | add_translation_error(Msg,Args) :- add_error(b2asp,Msg,Args), set_translation_error. | |
| 42 | add_translation_error(Msg,Args,Span) :- add_error(b2asp,Msg,Args,Span), set_translation_error. | |
| 43 | set_translation_error :- (translation_error_occurred -> true ; assertz(translation_error_occurred)). | |
| 44 | ||
| 45 | % type_restricted(Kind) is true if an infinite base type was restricted to finitely many values: | |
| 46 | % Kind=string: STRING restricted to the strings of the formula and some fresh strings | |
| 47 | % Kind=integer: INTEGER restricted to MININT..MAXINT (for identifiers without bounds) | |
| 48 | % a contradiction found by clingo may then be spurious (e.g., for s<:STRING & card(s)=5) | |
| 49 | :- dynamic type_restricted/1. | |
| 50 | set_type_restricted(Kind) :- (type_restricted(Kind) -> true ; assertz(type_restricted(Kind))). | |
| 51 | ||
| 52 | % call before generating first clause of a Clingo predicate | |
| 53 | % BPred is the B predicate for which this clause is generated | |
| 54 | % this predicate will try and filter unused local identifiers away | |
| 55 | register_clingo_predicate(SourcePrefix,BPred,FPred,Env,NewEnv) :- | |
| 56 | var(FPred), | |
| 57 | Env = env(LocalEnv,Bounds), LocalEnv = [_|_], % at least one local variable to possible filter away | |
| 58 | gensym_if_necessary(SourcePrefix,Pred), | |
| 59 | bsyntaxtree:find_identifier_uses(BPred, [], UsedIds), | |
| 60 | maplist(get_local_id,LocalEnv,EnvVars), sort(EnvVars,SE), | |
| 61 | ord_intersection(SE,UsedIds,UsefulIds), | |
| 62 | include(local_id_is_used(UsefulIds),LocalEnv,FilteredEnv), | |
| 63 | %format(user_output,'ids for ~w : ~w : used ~w : local ~w~n',[Pred,UsefulIds,UsedIds,EnvVars]), | |
| 64 | !, | |
| 65 | FPred = filter_pred(Pred,UsefulIds), | |
| 66 | NewEnv = env(FilteredEnv,Bounds). | |
| 67 | register_clingo_predicate(SourcePrefix,_BPred,Pred,Env,Env) :- | |
| 68 | gensym_if_necessary(SourcePrefix,Pred). | |
| 69 | ||
| 70 | ||
| 71 | ||
| 72 | ||
| 73 | % generate a Clingo clause and predicate name if necessary, at the moment we just print it onto user_output | |
| 74 | gen_clingo_pred_clause(SourcePrefix,Pred,ArgList,Env,Body) :- | |
| 75 | add_env_local_ids_to_arg_list(Pred,ArgList,Env,NewArgList), | |
| 76 | env_setup_code(Env,Pred,SetupCode), | |
| 77 | gen_clingo_pred_clause4(SourcePrefix,Pred,NewArgList,(SetupCode,Body)). | |
| 78 | ||
| 79 | gen_clingo_pred_clause4(SourcePrefix,Pred,ArgList,Body) :- | |
| 80 | gensym_if_necessary(SourcePrefix,Pred), | |
| 81 | %nl, | |
| 82 | gen_clingo_clause3(Pred,ArgList,Body). | |
| 83 | ||
| 84 | ||
| 85 | gen_clingo_clause(SourcePrefix,Pred,ArgList,Env,Body) :- | |
| 86 | gensym_if_necessary(SourcePrefix,Pred), | |
| 87 | gen_clingo_clause4(Pred,ArgList,Env,Body). | |
| 88 | ||
| 89 | % generate a Clingo clause for a predicate that has already been generated | |
| 90 | gen_clingo_clause4(Pred,ArgList,Env,Body) :- | |
| 91 | add_env_local_ids_to_arg_list(Pred,ArgList,Env,NewArgList), | |
| 92 | env_setup_code(Env,Pred,SetupCode), | |
| 93 | gen_clingo_clause3(Pred,NewArgList,(SetupCode,Body)). | |
| 94 | gen_clingo_clause3(Pred,ArgList,Body) :- | |
| 95 | construct_clingo_pred_call(Pred,ArgList,Head), | |
| 96 | register_clause_head(Pred,ArgList), | |
| 97 | simplify_body(Body, SimplifiedBody), | |
| 98 | my_portray_clause( Head , SimplifiedBody ). | |
| 99 | ||
| 100 | gen_clingo_ic_constraint(Body) :- | |
| 101 | simplify_body(Body, SimplifiedBody), | |
| 102 | nl, | |
| 103 | my_portray_clause( ( :- SimplifiedBody ) ). | |
| 104 | ||
| 105 | % ----------- | |
| 106 | ||
| 107 | % generate a Clingo fact | |
| 108 | gen_clingo_fact(Prefix,Pred,ArgList,Env) :- | |
| 109 | gensym_if_necessary(Prefix,Pred), | |
| 110 | add_env_local_ids_to_arg_list(Pred,ArgList,Env,NewArgList), | |
| 111 | env_setup_code(Env,Pred,Code), simplify_body(Code,SCode), | |
| 112 | construct_clingo_pred_call(Pred,NewArgList,Head), | |
| 113 | register_clause_head(Pred,NewArgList), | |
| 114 | my_portray_clause( Head, SCode ). | |
| 115 | ||
| 116 | ||
| 117 | % ----------- | |
| 118 | ||
| 119 | ||
| 120 | % filter out true literals and rewrite call/2 and call/3 literals | |
| 121 | simplify_body(X,R) :- var(X),!,R=X. | |
| 122 | simplify_body((A,B),Res) :- !, | |
| 123 | ( A=true -> simplify_body(B,Res) | |
| 124 | ; B=true -> simplify_body(A,Res) | |
| 125 | ; simplify_body(A,SA), simplify_body(B,SB), conjoin_clingo_calls(SA,SB,Res)). | |
| 126 | simplify_body(call(Pred,Arg),Call) :- !, Call =.. [Pred,Arg]. | |
| 127 | simplify_body(call(Pred,Arg1,Arg2),Call) :- !, Call =.. [Pred,Arg1,Arg2]. | |
| 128 | simplify_body(not(B),not(SB)) :- !, simplify_body(B,SB). % TODO: remove double not ? | |
| 129 | simplify_body(ecall(Pred,ArgList,Env),Call) :- !, | |
| 130 | (var(Pred) -> add_internal_error('Unknown ecall predicate: ',simplify_body(ecall(Pred,ArgList,Env),Call)) ; true), | |
| 131 | % ecall are calls where we add the local variables from the environment | |
| 132 | add_env_local_ids_to_arg_list(Pred,ArgList,Env,NewArgList), | |
| 133 | construct_clingo_pred_call(Pred,NewArgList,Call). | |
| 134 | simplify_body(ecall(Pred,Env),Call) :- !, | |
| 135 | add_env_local_ids_to_arg_list(Pred,[],Env,NewArgList), | |
| 136 | construct_clingo_pred_call(Pred,NewArgList,Call). | |
| 137 | simplify_body('='(A,B),'='(SA,SB)) :- !, simplify_expr(A,SA), simplify_expr(B,SB). | |
| 138 | simplify_body('=='(A,B),'=='(SA,SB)) :- !, simplify_expr(A,SA), simplify_expr(B,SB). | |
| 139 | simplify_body('!='(A,B),'!='(SA,SB)) :- !, simplify_expr(A,SA), simplify_expr(B,SB). | |
| 140 | simplify_body(B,B). | |
| 141 | ||
| 142 | % we need to flatten conjunctions, clingo can not deal with goals with arbitrary use of ( , ) | |
| 143 | conjoin_clingo_calls(true,B,Res) :- !, Res=B. | |
| 144 | conjoin_clingo_calls((A,B),C,Res) :- !, Res=(A,BC), conjoin_clingo_calls(B,C,BC). | |
| 145 | conjoin_clingo_calls(A,true,Res) :- !, Res=A. | |
| 146 | conjoin_clingo_calls(A,B,(A,B)). | |
| 147 | ||
| 148 | simplify_expr(X,R) :- var(X),!,R=X. | |
| 149 | simplify_expr(call(Pred,Arg),Call) :- !, Call =.. [Pred,Arg]. | |
| 150 | simplify_expr(call(Pred,Arg1,Arg2),Call) :- !, Call =.. [Pred,Arg1,Arg2]. | |
| 151 | simplify_expr(B,B). | |
| 152 | ||
| 153 | % ----------------------------- | |
| 154 | ||
| 155 | construct_clingo_pred_call(Pred,NewArgList,Call) :- %format(user_output,'Gen: ~w ~w~n',[Pred,NewArgList]), | |
| 156 | var(Pred),!, | |
| 157 | add_internal_error('Unknown Clingo predicate: ',construct_clingo_pred_call(Pred,NewArgList,Call)), | |
| 158 | set_translation_error, | |
| 159 | Call = fail. | |
| 160 | construct_clingo_pred_call(filter_pred(Pred,_Ids),NewArgList,Call) :- !, | |
| 161 | Call =.. [Pred|NewArgList]. | |
| 162 | construct_clingo_pred_call(Pred,NewArgList,Call) :- | |
| 163 | Call =.. [Pred|NewArgList]. | |
| 164 | ||
| 165 | % add environment local variables at end of argument list of a Clingo Predicate | |
| 166 | % these local quantified variables need to be passed as extra parameters (e.g., inside forall) | |
| 167 | :- mode add_env_local_ids_to_arg_list(+ClingoPred,+Args,+Env,-NewArgList). | |
| 168 | add_env_local_ids_to_arg_list(Pred,A,_,R) :- | |
| 169 | nonvar(Pred), top_level_or_auxiliary_clingo_pred(Pred), !, | |
| 170 | R=A. % At the moment we do not support exists inside forall,...: do not add anything | |
| 171 | add_env_local_ids_to_arg_list(Pred,ArgList,env(Env,_),NewArgList) :- | |
| 172 | nonvar(Pred), Pred = filter_pred(_,UsefulIds), | |
| 173 | include(local_id_is_used(UsefulIds),Env,FilteredEnv), | |
| 174 | maplist(get_var,FilteredEnv,EnvVars),!, | |
| 175 | append(ArgList,EnvVars,NewArgList). | |
| 176 | add_env_local_ids_to_arg_list(_,ArgList,env(Env,_),NewArgList) :- | |
| 177 | maplist(get_var,Env,EnvVars),!, | |
| 178 | append(ArgList,EnvVars,NewArgList). | |
| 179 | add_env_local_ids_to_arg_list(_,_,Env,_) :- | |
| 180 | add_translation_error('Illegal environment: ',Env),fail. | |
| 181 | ||
| 182 | get_var(local_id(_,_,_,VAR),VAR). | |
| 183 | get_local_id(local_id(Id,_,_,_),Id). | |
| 184 | ||
| 185 | local_id_is_used(UsedIds,local_id(Id,_,_,_)) :- ord_member(Id,UsedIds). | |
| 186 | ||
| 187 | top_level_or_auxiliary_clingo_pred(Pred) :- clingo_generated_id(Pred,_,_,_ID). | |
| 188 | top_level_or_auxiliary_clingo_pred(Pred) :- auxiliary_clingo_pred(Pred,_). | |
| 189 | ||
| 190 | ||
| 191 | % ------------------------------ | |
| 192 | ||
| 193 | ||
| 194 | % code to setup bounded possible values for local ids in environment | |
| 195 | % required to make Clingo clauses safe | |
| 196 | env_setup_code(env(Local,_),Pred,Code) :- env_setup_code2(Local,Pred,Code). | |
| 197 | env_setup_code2([],_,true). | |
| 198 | env_setup_code2([local_id(_ID,_Type,BaseType,Var)|T],Pred,(CodeToSetupID,RestCode)) :- | |
| 199 | % todo: filter out local variables that are not needed by Pred | |
| 200 | gen_base_type_constraint(BaseType,Var,CodeToSetupID), | |
| 201 | env_setup_code2(T,Pred,RestCode). | |
| 202 | ||
| 203 | % creates base-type constraints that can be used in body of a clause to constrain the possible values of a local id | |
| 204 | % see also gen_clingo_base_set | |
| 205 | gen_base_type_constraint(integer,Var, Constraint) :- !, | |
| 206 | get_min_int(MinInt), get_max_int(MaxInt), | |
| 207 | add_warning(b2asp,'Restricting infinite integer base type of local id to:',(MinInt,MaxInt)), | |
| 208 | set_type_restricted(integer), | |
| 209 | gen_base_type_constraint(integer_in_range(MinInt,MaxInt,integer),Var,Constraint). | |
| 210 | gen_base_type_constraint(integer_in_range(From,To,_),Var, Constraint) :- !, | |
| 211 | make_interval_finite(From,To,'?',From2,To2),Constraint = (Var = '..'(From2,To2)). | |
| 212 | gen_base_type_constraint(string,Var, Constraint) :- !, | |
| 213 | get_nr_of_registered_strings(Nr), | |
| 214 | add_warning(b2asp,'Restricting infinite string base type to:',(0,Nr)), | |
| 215 | set_type_restricted(string), | |
| 216 | Constraint = (Var = '..'(0,Nr)). | |
| 217 | gen_base_type_constraint(string_in_list(List),Var, Constraint) :- !, | |
| 218 | (List=[] -> Constraint = (1=2) ; list_to_disj(List,Disj), Constraint = (Var=Disj)). | |
| 219 | gen_base_type_constraint(boolean,Var, Constraint) :- !, Constraint = (Var=(pred_true ; pred_false)). | |
| 220 | gen_base_type_constraint(global(GS),Var, Constraint) :- | |
| 221 | b_get_fd_type_bounds(GS,Low,Up), !, | |
| 222 | gen_base_type_constraint(integer_in_range(Low,Up,integer),Var,Constraint). | |
| 223 | %TODO: couples | |
| 224 | gen_base_type_constraint(empty_set,_Var, Constraint) :- !, Constraint = (1=2). | |
| 225 | gen_base_type_constraint(couple(TA,TB),Var,Constraint) :- !, | |
| 226 | Constraint = ('='(Var,(VarA,VarB)), CA, CB), | |
| 227 | gen_base_type_constraint(TA,VarA,CA), gen_base_type_constraint(TB,VarB,CB). | |
| 228 | %gen_base_type_constraint(set(integer_in_range(From,To)),Var, Constraint) :- !, | |
| 229 | % gen_base_type_constraint(integer_in_range(From,To),Var, Constraint) | |
| 230 | gen_base_type_constraint(seq(BaseType),_Var,true) :- !, | |
| 231 | add_translation_error('Seq base type not supported for local ids: ',seq(BaseType)). | |
| 232 | gen_base_type_constraint(set(BaseType),_Var,true) :- !, | |
| 233 | add_translation_error('Set base type not supported for local ids: ',set(BaseType)). | |
| 234 | gen_base_type_constraint(BaseType,_Var,true) :- | |
| 235 | add_translation_error('Unknown or unsupported base type for local id: ',BaseType). | |
| 236 | ||
| 237 | list_to_disj([X],R) :- !, R=X. | |
| 238 | list_to_disj([X,Y|T],';'(X,R)) :- list_to_disj([Y|T],R). | |
| 239 | ||
| 240 | ||
| 241 | get_min_int(X) :- get_preference(minint,X). | |
| 242 | get_max_int(X) :- get_preference(maxint,X). | |
| 243 | ||
| 244 | % ------------------------------------------------------- | |
| 245 | ||
| 246 | % Insert a comment into the generated code | |
| 247 | clingo_format_comment(FormatStr,Args) :- (debug_mode(on) -> nl ; true), | |
| 248 | format(FormatStr,Args). | |
| 249 | ||
| 250 | % the portray hook is only added while printing generated clingo code (see my_portray_clause); | |
| 251 | % otherwise it would change how terms such as not(_) or '..'(_,_) are printed everywhere in ProB | |
| 252 | % note: user:portray/1 is declared dynamic in kodkod.pl; a declaration here would redefine it | |
| 253 | ||
| 254 | :- public b2asp_portray/1. | |
| 255 | % TODO: implement a proper pretty_printing for clingo; rather than using portray/1 hook | |
| 256 | b2asp_portray('#clingo_range'(Low,Call,Up)) :- format('~w{~w}~w',[Low,Call,Up]). | |
| 257 | %b2asp_portray('#count'(Pred)) :- format('#count{Var : ~w(Var)}',[Pred]). | |
| 258 | b2asp_portray('#aggregate'(ClingoAggr,Pred,Env)) :- | |
| 259 | add_env_local_ids_to_arg_list(Pred,['Var'],Env,NewArgList), | |
| 260 | construct_clingo_pred_call(Pred,NewArgList,Call), | |
| 261 | format('~w{Var : ~w}',[ClingoAggr,Call]). | |
| 262 | b2asp_portray('#string'(S)) :- format('"~w"',[S]). | |
| 263 | b2asp_portray(not(P)) :- write(' not '), print(P). | |
| 264 | b2asp_portray(BOP) :- clingo_bin_op(BOP,A,B,COP),print_clingo_val(A), format(' ~w ',[COP]), print_clingo_val(B). | |
| 265 | clingo_bin_op('<='(A,B),A,B,'<='). | |
| 266 | clingo_bin_op('!='(A,B),A,B,'!='). | |
| 267 | clingo_bin_op(':='(A,B),A,B,':='). | |
| 268 | clingo_bin_op('..'(A,B),A,B,'..'). | |
| 269 | print_clingo_val((A,B)) :- !, write('('), print(A), write(','), print(B), write(')'). % we need to print parentheses | |
| 270 | print_clingo_val(A) :- print(A). | |
| 271 | ||
| 272 | my_portray_clause(Head,true) :- !, my_portray_clause(Head). | |
| 273 | my_portray_clause(Head,Body) :- my_portray_clause((Head :- Body)). | |
| 274 | ||
| 275 | my_portray_clause(C) :- numbervars(C,0,_), | |
| 276 | asserta((user:portray(X) :- b2asp_code_gen:b2asp_portray(X)),Ref), | |
| 277 | call_cleanup(print(C), erase(Ref)), | |
| 278 | write('.'),nl,fail. | |
| 279 | my_portray_clause(_). | |
| 280 | ||
| 281 | % generate #count{Var : P(Var)} for a predicate P (also works for sum,...) | |
| 282 | gen_clingo_count_aggregate(Aggr,Pred,Env,'#aggregate'(Aggr,Pred,Env)). | |
| 283 | ||
| 284 | ||
| 285 | % ------------------------------------------------------- | |
| 286 | ||
| 287 | ||
| 288 | ||
| 289 | % ------------------------------ | |
| 290 | % generating CLINGO clauses,... | |
| 291 | ||
| 292 | :- dynamic generated_id/2, auxiliary_clingo_pred/2, deterministic_id_value/2. | |
| 293 | ||
| 294 | ||
| 295 | ||
| 296 | reset_b2asp_code_gen :- | |
| 297 | retractall(translation_error_occurred), | |
| 298 | retractall(type_restricted(_)), | |
| 299 | retractall(counter(_)), assert(counter(0)), | |
| 300 | retractall(generated_id(_,_)), | |
| 301 | retractall(deterministic_id_value(_,_)), | |
| 302 | retractall(auxiliary_clingo_pred(_,_)). | |
| 303 | ||
| 304 | ||
| 305 | :- mode gen_clingo_scalar_identifier(+ID,+BaseType,+LocalID_Env,-ClingoPred). | |
| 306 | % generate a Clingo predicate to represent a B identifier whose value is a scalar | |
| 307 | gen_clingo_scalar_identifier(IDB,BaseType,Env,PredB) :- | |
| 308 | gen_clingo_scalar_identifier(IDB,BaseType,Env,PredB,top_level). | |
| 309 | ||
| 310 | gen_clingo_scalar_identifier(ID,_,_,Pred,_) :- nonvar(Pred), | |
| 311 | add_warning(b2asp,'Identifier Clingo Result should be unbound: ',ID:Pred),fail. | |
| 312 | gen_clingo_scalar_identifier(ID,_,_,Pred,_) :- % check if we have already created a Clingo predicate for ID | |
| 313 | generated_id(ID,P),!, Pred=P. % TODO: pass as environment and check bounds unchanged | |
| 314 | gen_clingo_scalar_identifier(ID,couple(A,B),Env,Pred,TopLevel) :- !, | |
| 315 | % improved translation of pairs, instead of 1 { id_pair_scalar_0((-127..128,-127..128))} 1. we generate: | |
| 316 | % id_pair_scalar_0((A,B)) :- id_A(A), id_B(B). | |
| 317 | % 1{id_A(-127..128)}1. 1{id_B(-127..128)}1. | |
| 318 | gensym(ID,IDA), | |
| 319 | gen_clingo_scalar_identifier(IDA,A,Env,PredA,left_pair), | |
| 320 | gensym(ID,IDB), | |
| 321 | gen_clingo_scalar_identifier(IDB,B,Env,PredB,right_pair), | |
| 322 | gensym_if_necessary(pair,Pred), | |
| 323 | assert_gen_id(ID,Pred,scalar,couple(A,B),TopLevel), | |
| 324 | clingo_format_comment('~n% clingo encoding of scalar identifier ~w of type ~w:~n',[ID,couple(A,B)]), | |
| 325 | gen_clingo_clause4(Pred,[(XA,XB)],Env, (ecall(PredA,[XA],Env), | |
| 326 | ecall(PredB,[XB],Env)) ). | |
| 327 | % create clause Pred((XA,XB)) :- PredA(XA), PredB(XB)). | |
| 328 | gen_clingo_scalar_identifier(ID,BaseType,_Env,Pred,TL) :- % generate: 1{Pred(Low..Up)}1. | |
| 329 | gen_clingo_base_set(BaseType,ID,ClingoType,_MaxCard,Prefix), | |
| 330 | atom_concat(Prefix,'_scalar',Prefix2), | |
| 331 | gensym_if_necessary(Prefix2,Pred), | |
| 332 | clingo_format_comment('~n% clingo encoding of scalar identifier ~w of type ~w:~n',[ID,BaseType]), | |
| 333 | format('1 { ~w(~w)} 1.~n',[Pred,ClingoType]), % TODO: add ENV !! | |
| 334 | assert_gen_id(ID,Pred,scalar,BaseType,TL), | |
| 335 | register_clause_head(Pred,[ClingoType]). % at the moment these identifiers all have arity of 1 (TODO: enable nesting) | |
| 336 | ||
| 337 | assert_gen_id(ID,Pred,Kind,BaseType,TL) :- | |
| 338 | (deterministic_value(BaseType,Value) -> assert(deterministic_id_value(ID,Value)) ; true), | |
| 339 | (TL=top_level | |
| 340 | -> assert(generated_id(ID,Pred)), | |
| 341 | register_clingo_generated_id(Pred,Kind,BaseType,ID), % for back-translating models from clingo | |
| 342 | debug_format(19,'Generating clingo pred. ~w for B ~w id ~w (type ~w)~n',[Pred,Kind,ID,BaseType]) | |
| 343 | ; assert(auxiliary_clingo_pred(Pred,TL)) % only register; no need to back-translate in model | |
| 344 | % these are intermediate ids, e.g., generated for pair left/right hand sides | |
| 345 | % TODO: check that we do not need to add other arguments; pass environment to asser_gen_id | |
| 346 | ). | |
| 347 | ||
| 348 | % detect deterministic values, so that we can inline their values | |
| 349 | % this can be important for #count cardinality constraints for clingo performance | |
| 350 | deterministic_value(integer_in_range(Low,Up,_),Low) :- number(Low), Low=Up. | |
| 351 | ||
| 352 | ||
| 353 | :- mode gen_clingo_set_identifier(+ID,+BaseType,-ClingoPred,+TopLevel). | |
| 354 | % generate a Clingo predicate to represent a B identifier whose value is a set | |
| 355 | %gen_clingo_set_identifier(IDB,BaseType,PredB) :- gen_clingo_set_identifier(IDB,BaseType,PredB,top_level). | |
| 356 | ||
| 357 | gen_clingo_set_identifier(ID,_,Pred,_) :- % check if we have already created a Clingo predicate for ID | |
| 358 | generated_id(ID,P),!, | |
| 359 | % TODO: pass as environment and check bounds unchanged | |
| 360 | (Pred=P -> true ; add_translation_error('Identifier already instantiated: ',ID:Pred),fail). | |
| 361 | % Note: we cannot use similar code as for scalars for pairs (the relation is not necessarily tuple-distributive) | |
| 362 | gen_clingo_set_identifier(ID,BaseType,Pred,TL) :- % generate: 1{Pred(Low..Up)}1. | |
| 363 | gen_clingo_base_set(BaseType,ID,ClingoType,MaxCard,Prefix), | |
| 364 | atom_concat(Prefix,'_set',Prefix2), | |
| 365 | gensym_if_necessary(Prefix2,Pred), | |
| 366 | clingo_format_comment('~n% clingo encoding of set identifier ~w of type ~w:~n',[ID,BaseType]), | |
| 367 | format('0 { ~w(~w)} ~w.~n',[Pred,ClingoType,MaxCard]), | |
| 368 | assert_gen_id(ID,Pred,set,BaseType,TL), | |
| 369 | register_clause_head(Pred,[ClingoType]). % at the moment these ids all have arity of 1 (TODO: enable nesting). | |
| 370 | ||
| 371 | :- use_module(probsrc(tools), [ajoin_with_sep/3]). | |
| 372 | ||
| 373 | % convert base type into a Clingo expression that can be put into the head of a fact, | |
| 374 | % together with information about the maximal cardinality of the base type and a prefix for generated identifiers | |
| 375 | gen_clingo_base_set(integer,ID,Atom,MaxCard,Prefix) :- !, | |
| 376 | gen_clingo_base_set(integer_in_range(inf,sup,integer),ID,Atom,MaxCard,Prefix). | |
| 377 | gen_clingo_base_set(string,ID,Atom,MaxCard,Prefix) :- !, | |
| 378 | gen_clingo_base_set(integer_in_range(inf,sup,string),ID,Atom,MaxCard,Prefix). | |
| 379 | %gen_clingo_base_set(integer_set_in_range(Low,Up),ID,Atom,MaxCard,Prefix) :- !, | |
| 380 | % gen_clingo_base_set(integer_in_range(Low,Up),ID,Atom,MaxCard,Prefix). | |
| 381 | gen_clingo_base_set(integer_in_range(Low0,Up0,Type),ID,Atom,MaxCard,Prefix) :- !, | |
| 382 | (Type=string | |
| 383 | -> atom_concat(id_string_,ID,Prefix), | |
| 384 | make_string_interval_finite(Low0,Up0,ID,Low,Up) | |
| 385 | ; atom_concat(id_int_,ID,Prefix), | |
| 386 | make_interval_finite(Low0,Up0,ID,Low,Up) | |
| 387 | ), | |
| 388 | (Up >= Low -> MaxCard is 1+Up-Low ; MaxCard=0), | |
| 389 | format_to_codes('~w..~w',[Low,Up],Codes), | |
| 390 | atom_codes(Atom,Codes). | |
| 391 | gen_clingo_base_set(string_in_list(List),ID,Atom,MaxCard,Prefix) :- !, atom_concat(id_string_,ID,Prefix), | |
| 392 | length(List,MaxCard), | |
| 393 | ajoin_with_sep(List,';',Atom). | |
| 394 | gen_clingo_base_set(boolean,ID,Atom,2,Prefix) :- !, atom_concat(id_bool_,ID,Prefix), | |
| 395 | Atom = 'pred_true;pred_false'. | |
| 396 | gen_clingo_base_set(global(GS),ID,Atom,MaxCard,Prefix) :- % user-defined enumerated sets or deferred sets | |
| 397 | b_get_fd_type_bounds(GS,Low,Up), !, atom_concat(id_glob_,ID,Prefix), | |
| 398 | gen_clingo_base_set(integer_in_range(Low,Up,integer),ID,Atom,MaxCard,_). % we translate those sets as integers | |
| 399 | gen_clingo_base_set(couple(A,B),ID,Atom,MaxCard,Prefix) :- !, atom_concat(id_pair_,ID,Prefix), | |
| 400 | gen_clingo_base_set(A,ID,AAtom,CardA,_), | |
| 401 | gen_clingo_base_set(B,ID,BAtom,CardB,_), | |
| 402 | MaxCard is CardA*CardB, | |
| 403 | format_to_codes('((~w),(~w))',[AAtom,BAtom],Codes), % TODO: this is not very efficient for scalars, but necessary for sets; there seems to be a parsing problem when we use booleans below | |
| 404 | atom_codes(Atom,Codes). | |
| 405 | gen_clingo_base_set(empty_set,ID,Atom,0,Prefix) :- !, atom_concat(no_solution_,ID,Prefix), | |
| 406 | Atom = '2..1'. | |
| 407 | gen_clingo_base_set(set(BaseType),ID,error,1,id_error) :- !, | |
| 408 | add_translation_error('Expecting scalar base type: ',ID:set(BaseType)). | |
| 409 | gen_clingo_base_set(seq(BaseType),ID,error,1,id_error) :- !, | |
| 410 | add_translation_error('Expecting scalar base type: ',ID:seq(BaseType)). | |
| 411 | gen_clingo_base_set(BaseType,ID,error,1,id_error) :- add_translation_error('Unknown or unsupported base type: ',ID:BaseType). | |
| 412 | ||
| 413 | make_finite(integer,ID,FiniteType) :- !, make_finite(integer_in_range(inf,sup,integer),ID,FiniteType). | |
| 414 | make_finite(string,ID,FiniteType) :- !, | |
| 415 | make_finite(integer_in_range(inf,sup,string),ID,FiniteType). | |
| 416 | make_finite(integer_in_range(Low0,Up0,string),ID,integer_in_range(Low,Up,string)) :- !, | |
| 417 | make_string_interval_finite(Low0,Up0,ID,Low,Up). | |
| 418 | make_finite(integer_in_range(Low0,Up0,Type),ID,integer_in_range(Low,Up,Type)) :- !, | |
| 419 | make_interval_finite(Low0,Up0,ID,Low,Up). | |
| 420 | make_finite(couple(A,B),ID,couple(FA,FB)) :- !, make_finite(A,ID,FA), make_finite(B,ID,FB). | |
| 421 | make_finite(Type,_ID,Type). | |
| 422 | ||
| 423 | make_interval_finite(inf,sup,ID,X,Y) :- !, | |
| 424 | get_min_int(X), | |
| 425 | get_max_int(Y), | |
| 426 | add_warning(b2asp,'Restricting bounds of integer base type to: ',ID:(X,Y)), | |
| 427 | set_type_restricted(integer). | |
| 428 | make_interval_finite(inf,Y,ID,X,Y) :- !, | |
| 429 | get_min_int(Min), (Min =< Y -> X=Min ; X=Y), | |
| 430 | add_warning(b2asp,'Restricting lower-bound of integer base type to: ',ID:(X,Y)), | |
| 431 | set_type_restricted(integer). | |
| 432 | make_interval_finite(X,sup,ID,X,Y) :- !, | |
| 433 | get_max_int(Mx), (Mx>= X -> Y=Mx ; Y=X), | |
| 434 | add_warning(b2asp,'Restricting upper-bound of integer base type to: ',ID:(X,Y)), | |
| 435 | set_type_restricted(integer). | |
| 436 | make_interval_finite(A,B,ID,X,Y) :- (\+ integer(A) ; \+ integer(B)), !, | |
| 437 | add_translation_error('Illegal integer base type:',ID:(A,B)), | |
| 438 | make_interval_finite(inf,sup,ID,X,Y). | |
| 439 | make_interval_finite(X,Y,_,X,Y). | |
| 440 | ||
| 441 | % strings are represented by the numbers 0..N of the registered strings (see register_formula_strings in b2asp); | |
| 442 | % other numbers do not correspond to a string | |
| 443 | make_string_interval_finite(Low0,Up0,ID,Low,Up) :- | |
| 444 | get_nr_of_registered_strings(N), | |
| 445 | (number(Low0) -> Low is max(0,Low0) ; Low=0), | |
| 446 | (number(Up0) -> Up is min(N,Up0) | |
| 447 | ; Up=N, add_warning(b2asp,'Restricting infinite string base type to the strings of the formula and fresh strings: ',ID:(Low,Up)), | |
| 448 | set_type_restricted(string)). | |
| 449 | ||
| 450 | ||
| 451 | ||
| 452 | % ------------------- | |
| 453 | % gensym: generate a fresh symbol (used for Clingo predicates for intermediate values) | |
| 454 | ||
| 455 | :- dynamic counter/1. | |
| 456 | counter(0). | |
| 457 | gensym(Prefix,Symbol) :- retract(counter(N)), N1 is N+1, | |
| 458 | assert(counter(N1)), | |
| 459 | atom_codes(Prefix,PC), | |
| 460 | number_codes(N,NC), | |
| 461 | append(PC,[0'_|NC],PCNC), | |
| 462 | atom_codes(Symbol,PCNC). | |
| 463 | ||
| 464 | gensym_if_necessary(Prefix,Symbol) :- var(Symbol), !, gensym(Prefix,Symbol). | |
| 465 | gensym_if_necessary(_,_). | |
| 466 |