1:- module(pita,[
    2  prob/2,
    3  prob/3,
    4  prob_meta/2,
    5  prob_meta/3,
    6  abd_prob/3,
    7  bdd_dot_file/3,
    8  bdd_dot_string/3,
    9  abd_bdd_dot_string/4,
   10  abd_bdd_dot_string/6,
   11  map_bdd_dot_string/6,
   12  map/3,
   13  set_pita/2,setting_pita/2,
   14  get_var_n/6,get_abd_var_n/6,
   15  get_dec_var_n/5,
   16  load/1,load_file/1,  dt_solve/2,  parse/2,  op(600,xfy,'::'),  op(600,xfx,'=>'),  op(1150,fx,action),  op(1200,fy,map_query),  op(1200,fy,abducible),  msw/4,  msw/5    ]).

pita

This module performs reasoning over Logic Programs with Annotated Disjunctions and CP-Logic programs. It reads probabilistic program and computes the probability of queries.

See https://friguzzi.github.io/cplint/ for details.

Reexports cplint_util and bddem

author
- Fabrizio Riguzzi
license
- Artistic License 2.0 https://opensource.org/licenses/Artistic-2.0
   42:- reexport(library(cplint_util)).   43:- reexport(library(bddem)).   44
   45% :- prolog_debug(chk_secure).
   46
   47:-meta_predicate abd_prob(:,-,-).   48:-meta_predicate prob(:,-).   49:-meta_predicate prob(:,:,-).   50:-meta_predicate prob(:,:,-,+).   51:-meta_predicate prob_meta(:,-).   52:-meta_predicate prob_meta(:,:,+).   53:-meta_predicate bdd_dot_file(:,+,-).   54:-meta_predicate bdd_dot_string(:,-,-).   55:-meta_predicate abd_bdd_dot_string(:,-,-,-).   56:-meta_predicate abd_bdd_dot_string(:,-,-,-,-,-).   57:-meta_predicate map(:,-,-).   58:-meta_predicate map_bdd_dot_string(:,-,-,-,-,-).   59:-meta_predicate msw(:,-,-,-).   60:-meta_predicate msw(:,-,-,-,-).   61:-meta_predicate get_p(:,+,-).   62:-meta_predicate get_cond_p(:,:,+,-).   63:-meta_predicate get_node(:,+,-).   64:-meta_predicate get_cond_node(:,:,+,-,-).   65:-meta_predicate set_pita(:,+).   66:-meta_predicate setting_pita(:,-).   67:-meta_predicate set_sw(:,+).   68:-meta_predicate dt_solve(:,-).   69
   70% :- dynamic utility/2.
   71
   72:-use_module(library(lists)).   73:-use_module(library(apply)).   74:-use_module(library(assoc)).   75
   76:- style_check(-discontiguous).   77
   78:- thread_local rule_n/1,goal_n/1,pita_input_mod/1,local_pita_setting/2.   79
   80
   81
   82
   83default_setting_pita(epsilon_parsing, 1e-5).
   84/* on, off */
   85
   86default_setting_pita(bagof,false).
   87/* values: false, intermediate, all, extra */
   88
   89default_setting_pita(compiling,off).
   90
   91:-set_prolog_flag(unknown,warning).   92
   93default_setting_pita(depth_bound,false).  %if true, it limits the derivation of the example to the value of 'depth'
   94default_setting_pita(depth,5).
   95default_setting_pita(single_var,false). %false:1 variable for every grounding of a rule; true: 1 variable for rule (even if a rule has more groundings),simpler.
   96
   97default_setting_pita(tabling,auto).
   98/* values:
   99  auto
  100  explicit
  101*/
  102default_setting_pita(prism_memoization,false). %false: original prism semantics, true: semantics with memoization
 load ++File:atom is det
Loads File.lpad if it exists, otherwise loads File.cpl if it exists. /
  108load(File):-
  109  must_be(atom,File),
  110  atomic_concat(File,'.lpad',FileLPAD),
  111  (exists_file(FileLPAD)->
  112    load_file(FileLPAD)
  113  ;
  114    atomic_concat(File,'.cpl',FileCPL),
  115    (exists_file(FileCPL)->
  116      load_file(FileCPL)
  117    )
  118  ).
 load_file(++FileWithExtension:atom) is det
Loads FileWithExtension. /
  128load_file(File):-
  129  must_be(atom,File),
  130  begin_lpad_pred,
  131  user:consult(File),
  132  end_lpad_pred.
 parse(++FileIn:atom, ++FileOut:atom) is det
applies the pita transformation to FileIn and writes the result to FileOut /
  139parse(FileIn,FileOut):-
  140  prolog_load_context(module, M),
  141  assert(M:pita_on),
  142  initialize_pita,
  143  open(FileIn,read,SI),
  144	read_clauses(SI,C),
  145	close(SI),
  146	process_clauses(C,[],C1),
  147  findall(LZ,M:zero_clauses(LZ),L0),
  148  retractall(M:zero_clauses(_)),
  149  retractall(M:tabled(_)),
  150  append(C1,L0,Cl0),
  151	open(FileOut,write,SO),
  152  divide_tab_dyn_dir(Cl0,T,Dyn,Cl),
  153  writeln(SO,':- use_module(library(pita)).'),
  154  writeln(SO,':- style_check(-discontiguous).'),
  155  writeln(SO,':- pita.'),  
  156  write_clauses([(:- dynamic query_rule/4)|Dyn],SO),
  157	write_tab_dir(T,SO),
  158	write_clauses(Cl,SO),
  159	close(SO).
  160
  161divide_tab_dyn_dir([],[],[],[]).
  162
  163divide_tab_dyn_dir([(:- table A)|T],[(:- table A)|TT],Dyn,Cl):-!,
  164  divide_tab_dyn_dir(T,TT,Dyn,Cl).
  165
  166divide_tab_dyn_dir([(:- dynamic A)|T],Tab,[(:- dynamic A)|Dyn],Cl):-!,
  167  divide_tab_dyn_dir(T,Tab,Dyn,Cl).
  168
  169divide_tab_dyn_dir([H|T],TT,Dyn,[H|Cl]):-
  170  divide_tab_dyn_dir(T,TT,Dyn,Cl).
  171
  172/* output predicates */
  173write_tab_dir([],S):-
  174	nl(S).
  175
  176write_tab_dir([H|T],S):-
  177	format(S,"~q.",[H]),
  178	nl(S),
  179	write_tab_dir(T,S).
  180
  181
  182write_clauses([],_).
  183
  184write_clauses([:- use_module(library(pita))|T],S):-!,
  185  write_clauses(T,S).
  186
  187write_clauses([:- pita|T],S):-!,
  188  write_clauses(T,S).
  189
  190write_clauses([H|T],S):-
  191  copy_term(H,H1),
  192  numbervars(H1,0,_),
  193	format(S,"~q.",[H1]),
  194	nl(S),
  195	write_clauses(T,S).
  196
  197read_clauses(S,[Cl|Out]):-
  198        read_term(S,Cl,[]),
  199	(Cl=end_of_file->
  200		Out=[]
  201	;
  202		read_clauses(S,Out)
  203	).
  204/* clause processing */
  205process_clauses([end_of_file],C,C):-!.
  206
  207process_clauses([H|T],C0,C1):-
  208	(pita_expansion(H,H1)->
  209		true
  210	;
  211		H1=H
  212	),
  213	(is_list(H1)->
  214		append(C0,H1,C2)
  215	;
  216		append(C0,[H1],C2)
  217	),
  218	process_clauses(T,C2,C1).
  219
  220initialize_pita:-
  221  prolog_load_context(module, M),
  222  retractall(M:local_pita_setting(_,_)),
  223  findall(local_pita_setting(P,V),default_setting_pita(P,V),L),
  224  assert_all(L,M,_),
  225  assert(pita_input_mod(M)),
  226  retractall(M:rule_n(_)),
  227  retractall(M:goal_n(_)),
  228  assert(M:rule_n(0)),
  229  assert(M:goal_n(0)),
  230  (dynamic M:v/3, M:av/3,  %M:rule_by_num/4,
  231    M:zero_clauses/1, M:pita_on/0, M:if_on/0, M:tabled/1),
  232  retractall(M:query_rule(_,_,_,_)).
 dt_solve(-Strategy:list, -Cost:float) is det
The predicate computes the best solution for the decision theory problem. It returns the best strategy in Strategy and it cost in Cost. Complete solution without pruning. /
  241dt_solve(M:Strategy,Cost):-
  242  must_be(var,Strategy),
  243  must_be(var,Cost),
  244  abolish_all_tables,
  245  findall([H,U],M:'$util'(H,U),LUtils),
  246  init(Env),
  247  % statistics(walltime,[Start|_]),
  248  generate_solution(Env,M,LUtils,[],St,Cost),
  249  % statistics(walltime,[Stop|_]),
  250  end(Env),
  251  % Runtime is Stop - Start,
  252  % format('Runtime: ~w~n',[Runtime]),
  253  maplist(pair(M),St,Strategy).
  254
  255pair(M,A,B):- M:rule_by_num(A,B,_,_).
  256split([A,B],A,B).
  257
  258get_bdd(_,_,[],L,L):- !.
  259get_bdd(M,Env,[G|T],L,LO):-
  260  get_node(M:G,Env,Out),
  261  % writeln(Out),
  262  Out=(_,BDD),
  263  append(L,[BDD],LT),
  264  get_bdd(M,Env,T,LT,LO).
  265
  266% compute the solution for dt problem
  267% generate_solution/6
  268% generate_solution(Env,M,GoalCostList,CurrentAdd,Solution,Cost)
  269% output Solution, Cost
  270generate_solution(Env,_,[],Add,Solution,Cost):- !,
  271  % create_dot(Env,Add,"final.dot"),
  272  ret_strategy(Env,Add,Solution,Cost).
  273
  274generate_solution(Env,M,[[G,Cost]|TC],CurrentAdd,Solution,OptCost):-
  275  get_node(M:G,Env,Out),
  276  Out=(_,BDD),
  277  probability_dd(Env,BDD,AddConv),
  278  add_prod(Env,AddConv,Cost,AddScaled),
  279  (CurrentAdd = [] ->
  280    AddOut = AddScaled ;
  281    % writeln(CurrentAdd),
  282    add_sum(Env,CurrentAdd,AddScaled,AddOut)
  283    % writeln("sum"),
  284  ),
  285  generate_solution(Env,M,TC,AddOut,Solution,OptCost).
 dt_evaluate_strategy(+Strategy:List, -Cost:float) is det
Computes the cost of the selected strategy /
  292% TODO
  293% dt_evaluate_strategy(LS,Cost).
 prob_meta(:Query:conjunction_of_literals, -Probability:float) is nondet
To be used in place of prob/2 for meta calls (doesn't abolish tables) /
  300prob_meta(M:Goal,P):-
  301  must_be(nonvar,Goal),
  302  must_be(var,P),
  303  term_variables(Goal,VG),
  304  get_next_goal_number(M,GN),
  305  atomic_concat('$goal',GN,NewGoal),
  306  Goal1=..[NewGoal|VG],
  307  list2and(GoalL,Goal),
  308  ( M:local_pita_setting(depth_bound,true) *->
  309      ( process_body_db(GoalL,BDD,BDDAnd,DB,[],_Vars,BodyList2,Env,M),
  310        add_bdd_arg_db(Goal1,Env,BDDAnd,DB,M,Head1)
  311      )
  312    ;
  313      ( process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  314        add_bdd_arg(Goal1,Env,BDDAnd,M,Head1)
  315      )
  316  ),
  317  append([onec(Env,BDD)],BodyList2,BodyList3),
  318  list2and(BodyList3,Body2),
  319  M:(asserta((Head1 :- Body2),Ref)),
  320  init(Env),
  321  findall((Goal,P),get_p(M:Goal1,Env,P),L),
  322  end(Env),
  323  erase(Ref),
  324  member((Goal,P),L).
 abd_prob(:Query:conjunction_of_literals, -Probability:float, -Delta:list) is nondet
The predicate computes the most probable abductive explanation of the ground query Query. It returns the explanation in Delta together with its Probability /
  332abd_prob(M:Goal,P,Delta):-
  333  must_be(nonvar,Goal),
  334  must_be(var,P),
  335  must_be(var,Delta),
  336  abolish_all_tables,
  337  term_variables(Goal,VG),
  338  get_next_goal_number(M,GN),
  339  atomic_concat('$goal',GN,NewGoal),
  340  Goal1=..[NewGoal|VG],
  341  list2and(GoalL,Goal),
  342  process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  343  append([onec(Env,BDD)],BodyList2,BodyList3),
  344  list2and(BodyList3,Body2),
  345  add_bdd_arg(Goal1,Env,BDDAnd,M,Head1),
  346  M:(asserta((Head1 :- Body2),Ref)),
  347  init(Env),
  348  findall((Goal,P,Exp),get_abd_p(M:Goal1,M:'$constraints',Env,P,Exp),L),
  349  end(Env),
  350  erase(Ref),
  351  member((Goal,P,Exp),L),
  352  maplist(from_assign_to_exp(M),Exp,DeltaAll),
  353  simplify_delta(DeltaAll,Delta).
  354
  355subset_([], []).
  356subset_([E|Tail], [E|NTail]):-
  357  subset_(Tail, NTail).
  358subset_([_|Tail], NTail):-
  359  subset_(Tail, NTail).
  360
  361mycompare(<,L1,L2) :- length(L1,A1), length(L2,A2), A1 < A2.
  362mycompare(>, _, _).
  363
  364sub([A],[A]).
  365sub([H|T],[V|R]):-
  366  findall(X,(subset_(H,X), member(X,T)),LX),
  367  (   LX = [] ->
  368    V = H,
  369    sub(T,R) ;
  370    sub(T,[V|R]) 
  371  ).
  372
  373simplify_delta([],[]):- !.
  374simplify_delta(Din,Delta):-
  375  predsort(mycompare,Din,LS),
  376  reverse(LS,LNR),
  377  sub(LNR,Delta), !.
  378
  379
  380
  381from_assign_to_exp(_M,[],[]):- !.
  382from_assign_to_exp(M,[Var-Val|TA],[Abd|TDelta]):-
  383  M:av(R,S,Var),
  384  M:abd(R,S,H),
  385  (Val=1->
  386    Abd=H
  387  ;
  388    Abd= \+(H)
  389    % Abd= []
  390  ),
  391  from_assign_to_exp(M,TA,TDelta).
 bdd_dot_file(:Query:atom, +FileName:string, -LV:list) is det
The predicate builds the BDD for Query and writes its dot representation to file FileName and a list in LV with the association of variables to rules. LV is a list of list, each sublist has three elements: the multivalued variable number, the rule number and the grounding substitution. /
  403bdd_dot_file(M:Goal,File,LV):-
  404  must_be(nonvar,Goal),
  405  must_be(string,File),
  406  must_be(var,LV),
  407  abolish_all_tables,
  408  init(Env),
  409  get_node(M:Goal,Env,Out),
  410  Out=(_,BDD),!,
  411  findall([V,R,S],M:v(R,S,V),LV),
  412  create_dot(Env,BDD,File),
  413  end(Env).
 bdd_dot_string(:Query:atom, -DotString:string, -LV:list) is det
The predicate builds the BDD for Query and returns its dot representation in DotString and a list in LV with the association of variables to rules. LV is a list of list, each sublist has three elements: the multivalued variable number, the rule number and the grounding substitution. /
  424bdd_dot_string(M:Goal,DotString,LV):-
  425  must_be(nonvar,Goal),
  426  must_be(var,DotString),
  427  must_be(var,LV),
  428  DotString=dot(Dot),
  429  abolish_all_tables,
  430  init(Env),
  431  get_node(M:Goal,Env,Out),
  432  Out=(_,BDD),!,
  433  findall([V,R,S],M:v(R,S,V),LV),
  434  create_dot_string(Env,BDD,Dot),
  435  end(Env).
 abd_bdd_dot_string(:Query:atom, -DotString:string, -LV:list, -LAV:list) is det
The predicate builds the BDD for the abductive explanations for Query and returns its dot representation in DotString and lists LV and LAV, the association of variables to rules and to abductive variables to rules respectively. LV and LAV are lists of list, each sublist has three elements: the multivalued variable number, the rule number and the grounding substitution. /
  448abd_bdd_dot_string(M:Goal,DotString,LV,LAV):-
  449  must_be(nonvar,Goal),
  450  must_be(var,DotString),
  451  must_be(var,LV),
  452  must_be(var,LAV),
  453  abd_bdd_dot_string(M:Goal,DotString,LV,LAV,_P,_Delta).
 abd_bdd_dot_string(:Query:atom, -DotString:string, -LV:list, -LAV:list, -Probability:float, -Delta:list) is det
The predicate builds the BDD for the abductive explanations for Query It returns the explanation in Delta together with its Probability. The predicate builds the BDD for Query and returns its dot representation in DotString and lists LV and LAV, the association of variables to rules and to abductive variables to rules respectively. LV and LAV are lists of list, each sublist has three elements: the multivalued variable number, the rule number and the grounding substitution. /
  467abd_bdd_dot_string(M:Goal,DotString,LV,LAV,P,Delta):-
  468  must_be(nonvar,Goal),
  469  must_be(var,DotString),
  470  must_be(var,LV),
  471  must_be(var,LAV),
  472  must_be(var,P),
  473  must_be(var,Delta),
  474  DotString=dot(Dot),
  475  abolish_all_tables,
  476  init(Env),
  477  get_cond_node(M:Goal,M:'$constraints',Env,Out,_),
  478  Out=(_,BDD), !,
  479  ret_abd_prob(Env,BDD,P,Exp),
  480  create_dot_string(Env,BDD,Dot),
  481  end(Env),
  482  maplist(from_assign_to_exp(M),Exp,Delta),
  483  findall([V,R,S],M:v(R,S,V),LV),
  484  findall([V,R,S],M:av(R,S,V),LAV).
 map(:Query:atom, -Probability:float, -Delta:list) is nondet
The predicate computes the explanation of the ground query Query with Maximum A Posteriori (MAP) probability. It returns the explanation in Delta together with its Probability /
  493map(M:Goal,P,MAP):-
  494  must_be(nonvar,Goal),
  495  must_be(var,P),
  496  must_be(var,MAP),
  497  map_int(Goal,M,_LV,_LAV,P,MAP,Env,_BDD),
  498  end(Env).
 map_bdd_dot_string(:Query:atom, -DotString:string, -LV:list, -LAV:list, -Probability:float, -Delta:list) is nondet
The predicate computes the explanation of the ground query Query with Maximum A Posteriori (MAP) probability. It returns the explanation in Delta together with its Probability The predicate builds the BDD for Query and returns its dot representation in DotString and lists LV and LAV, the association of variables to rules and of query variables to rules respectively. LV and LAV are lists of list, each sublist has three elements: the multivalued variable number, the rule number and the grounding substitution. /
  513map_bdd_dot_string(M:Goal,DotString,LV,LAV,P,MAP):-
  514  must_be(nonvar,Goal),
  515  must_be(var,DotString),
  516  must_be(var,LV),
  517  must_be(var,LAV),
  518  must_be(var,P),
  519  must_be(var,MAP),
  520  DotString=dot(Dot),
  521  map_int(Goal,M,LV,LAV,P,MAP,Env,BDD),
  522  create_dot_string(Env,BDD,Dot),
  523  end(Env).
  524
  525
  526map_int(Goal,M,LV,LAV,P,MAP,Env,BDD):-
  527  abolish_all_tables,
  528  init(Env),
  529  get_node(M:Goal,Env,Out),
  530  (Out=(_,BDD0)),!,
  531  findall([V,R,S],M:v(R,S,V),LV),
  532  one(Env,One),
  533  make_query_vars(LV,M,Env,One,Cons,LAV),
  534  and(Env,BDD0,Cons,BDD),
  535  ret_map_prob(Env,BDD,P,Exp0),
  536  reverse(Exp0,Exp),
  537  from_assign_to_map(Exp,M,MAP).
  538
  539
  540make_query_vars([],_M,_Env,C,C,[]).
  541
  542make_query_vars([[V,R,S]|T],M,Env,Cons0,Cons,[[V,R,S]|TV]):-
  543  M:query_rule(R,_,_,_),!,
  544  make_query_var(Env,V,B),
  545  and(Env,Cons0,B,Cons1),
  546  make_query_vars(T,M,Env,Cons1,Cons,TV).
  547
  548make_query_vars([_H|T],M,Env,Cons0,Cons,LV):-
  549  make_query_vars(T,M,Env,Cons0,Cons,LV).
  550
  551from_assign_to_map([],_M,[]).
  552
  553from_assign_to_map([Var-Val|TA],M,[rule(R,Head,HeadList,Body)|TDelta]):-
  554  M:v(R,S,Var),
  555  M:query_rule(R,HeadList,Body,S),
  556  nth1(Val,HeadList,Head:_),
  557  from_assign_to_map(TA,M,TDelta).
 prob(:Query:atom, -Probability:float) is nondet
The predicate computes the probability of Query If Query is not ground, it returns in backtracking all ground instantiations of Query together with their probabilities /
  567prob(M:Goal,P):-
  568  must_be(nonvar,Goal),
  569  abolish_all_tables,
  570  prob_meta(M:Goal,P).
 prob(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, -Probability:float) is nondet
Equivalent to prob/4 with an empty option list. /
  578prob(M:Goal,M:Evidence,P):-
  579  must_be(nonvar,Goal),
  580  must_be(nonvar,Evidence),
  581  must_be(var,P),
  582  abolish_all_tables,
  583  prob_meta(M:Goal,M:Evidence,P).
 prob_meta(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, -Probability:float) is nondet
To be used in place of prob/3 for meta calls (doesn't abolish tables) /
  590prob_meta(M:Goal,M:Evidence,P):-
  591  must_be(nonvar,Goal),
  592  must_be(nonvar,Evidence),
  593  must_be(var,P),
  594  get_next_goal_number(M,GN),
  595  atomic_concat('$ev',GN,NewEv),
  596  deal_with_ev(Evidence,M,NewEv,EvNoAct,UpdatedClausesRefs,ClausesToReAdd),
  597  term_variables(Goal,VG),
  598  atomic_concat('$goal',GN,NewGoal),
  599  Goal1=..[NewGoal|VG],
  600  list2and(GoalL,Goal),
  601  process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  602  append([onec(Env,BDD)],BodyList2,BodyList3),
  603  list2and(BodyList3,Body2),
  604  add_bdd_arg(Goal1,Env,BDDAnd,M,Head1),
  605  M:(asserta((Head1 :- Body2),Ref)),
  606  init(Env),
  607  (EvNoAct=true->
  608    findall((Goal,P),get_p(M:Goal1,Env,P),L)
  609  ;
  610    findall((Goal,P),get_cond_p(M:Goal1,M:EvNoAct,Env,P),L)
  611  ),
  612  end(Env),
  613  retractall(M:NewEv),
  614  maplist(erase,UpdatedClausesRefs),
  615  erase(Ref),
  616  maplist(M:assertz,ClausesToReAdd),
  617  member((Goal,P),L).
 prob(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, -Probability:float, +Options:list) is nondet
The predicate computes the probability of Query given Evidence If Query/Evidence are not ground, it returns in backtracking all ground instantiations of Query/Evidence together with their probabilities Options is a list of options, the following are recognised by mc_prob/3:
bar(-BarChart:dict)
BarChart is a dict for rendering with c3 as a bar chart with a bar for the probability of success and a bar for the probability of failure. /
  633prob(M:Goal,M:Evidence,P,Options):-
  634  must_be(nonvar,Goal),
  635  must_be(nonvar,Evidence),
  636  must_be(var,P),
  637  must_be(nonvar,Options),
  638  prob(M:Goal,M:Evidence,P),
  639  option(bar(Chart),Options,no),
  640  (nonvar(Chart)->
  641    true
  642  ;
  643    bar(P,Chart)
  644  ).
  645
  646
  647deal_with_ev(Ev,M,NewEv,EvGoal,UC,CA):-
  648  list2and(EvL,Ev),
  649  partition(ac,EvL,ActL,EvNoActL),
  650  deal_with_actions(ActL,M,UC0,CA),
  651  (EvNoActL=[]->
  652    EvGoal=true,
  653    UC=UC0
  654  ;
  655    process_body(EvNoActL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  656    append([onec(Env,BDD)],BodyList2,BodyList3),
  657    list2and(BodyList3,Body2),
  658    add_bdd_arg(NewEv,Env,BDDAnd,M,Head1),
  659    M:(asserta((Head1 :- Body2),Ref)),
  660    UC=[Ref|UC0],
  661    EvGoal=NewEv
  662  ).
  663
  664deal_with_actions(ActL,M,UC,CA):-
  665  empty_assoc(AP0),
  666  foldl(get_pred_const,ActL,AP0,AP),
  667  assoc_to_list(AP,LP),
  668  maplist(update_clauses(M),LP,UCL,CAL),
  669  partition(nac,ActL,_NActL,PActL),
  670  maplist(assert_actions(M),PActL,ActRefs),
  671  append([ActRefs|UCL],UC),
  672  append(CAL,CA).
  673
  674zero_clauses_actions(M,do(\+ A),Ref):-
  675  A=..[P|Args],
  676  append(Args,[Env,BDD],Args1),
  677  A1=..[P|Args1],
  678  M:assertz((A1:-zeroc(Env,BDD)),Ref).
  679
  680assert_actions(M,do(A),Ref):-
  681  A=..[P|Args],
  682  append(Args,[Env,BDD],Args1),
  683  A1=..[P|Args1],
  684  M:assertz((A1:-onec(Env,BDD)),Ref).
  685
  686update_clauses(M,P/0- _,[RefZ],[(H:-zeroc(Env,BDD))|LCA]):-!,
  687  functor(G1,P,2),
  688  findall(Ref,M:clause(G1,_B,Ref),UC),
  689  findall((G1:-B),M:clause(G1,B),LCA),
  690  H=..[P,Env,BDD],
  691  maplist(erase,UC),
  692  M:assertz((H:-zeroc(Env,BDD)),RefZ).
  693
  694update_clauses(M,P/A-Constants,UC,CA):-
  695  functor(G,P,A),
  696  A1 is A+2,
  697  functor(G1,P,A1),
  698  G=..[_|Args],
  699  G1=..[_|Args1],
  700  append(Args,[_,_],Args1),
  701  findall((G1,B,Ref),M:clause(G1,B,Ref),LC),
  702  maplist(get_const(Args),Constants,ConstraintsL),
  703  list2and(ConstraintsL,Constraints),
  704  maplist(add_cons(G1,Constraints,M),LC,UC,CA).
  705
  706add_cons(_G,_C,M,(H,zeroc(Env,Zero),Ref),Ref1,(H:-zeroc(Env,Zero))):-!,
  707  erase(Ref),
  708  M:assertz((H:-zeroc(Env,Zero)),Ref1).
  709
  710add_cons(G,C,M,(H,B,Ref),Ref1,(H:-B)):-
  711  copy_term((G,C),(G1,C1)),
  712  G1=H,
  713  erase(Ref),
  714  M:assertz((H:-(C1,B)),Ref1).
  715
  716
  717get_const(Args,Constants,Constraint):-
  718  maplist(constr,Args,Constants,ConstraintL),
  719  list2and(ConstraintL,Constraint).
  720
  721constr(V,C,dif(V,C)).
  722
  723get_pred_const(do(Do0),AP0,AP):-
  724  (Do0= (\+ Do)->
  725    true
  726  ;
  727    Do=Do0
  728  ),
  729  functor(Do,F,A),
  730  Do=..[_|Args],
  731  (get_assoc(F/A,AP0,V)->
  732    put_assoc(F/A,AP0,[Args|V],AP)
  733  ;
  734    put_assoc(F/A,AP0,[Args],AP)
  735  ).
  736
  737
  738ac(do(_)).
  739nac(do(\+ _)).
  740
  741
  742get_p(M:Goal,Env,P):-
  743  get_node(M:Goal,Env,BDD),
  744  ret_probc(Env,BDD,P).
  745
  746
  747get_abd_p(M:Goal,M:Evidence,Env,P,Exp):-
  748  % get_node_no_rec(M:Evidence,Env,OutIC),
  749  get_cond_node(M:Goal,M:Evidence,Env,Out,_),
  750  % get_node(M:Evidence,Env,OutIC),
  751  Out=(_,BDD),
  752  % OutIC = (_,BDDIC),
  753  ret_abd_prob(Env,BDD,P,Exp).
  754
  755get_cond_p(M:Goal,M:Evidence,Env,P):-
  756  get_cond_node(M:Goal,M:Evidence,Env,BDDGE,BDDE),
  757  ret_probc(Env,BDDE,PE),
  758  ret_probc(Env,BDDGE,PGE),
  759  ( PE =:= 0 -> 
  760      writeln("Undefined: probability of evidence 0.") ;
  761      P is PGE/PE
  762  ).
  763
  764
  765get_node(M:Goal,Env,BDD):-
  766  M:local_pita_setting(depth_bound,true),!,
  767  M:local_pita_setting(depth,DB),
  768  retractall(M:v(_,_,_)),
  769  retractall(M:av(_,_,_)),
  770  retractall(M:dec(_,_,_)),
  771  add_bdd_arg_db(Goal,Env,BDD,DB,M,Goal1),%DB=depth bound
  772  (M:Goal1*->
  773    true
  774  ;
  775    zeroc(Env,BDD)
  776  ).
  777
  778get_node(M:Goal,Env,BDD):- %with DB=false
  779  retractall(M:v(_,_,_)),
  780  retractall(M:av(_,_,_)),
  781  retractall(M:dec(_,_,_)),
  782  add_bdd_arg(Goal,Env,BDD,M,Goal1),
  783  (M:Goal1*->
  784    true
  785  ;
  786    zeroc(Env,BDD)
  787    % format("-------------------------Failed goal: ~w ~n",[M:Goal])
  788  ).
  789
  790  get_node_no_rec(M:Goal,Env,BDD):- %with DB=false
  791  retractall(M:v(_,_,_)),
  792  retractall(M:v(_,_,_)),
  793  retractall(M:av(_,_,_)),
  794  add_bdd_arg(Goal,Env,BDD,M,Goal1),
  795  (M:Goal1*->
  796    true
  797  ;
  798    zeroc(Env,BDD)
  799    % format("-------------------------Failed goal: ~w ~n",[M:Goal])
  800  ).
  801
  802get_cond_node(M:Goal,M:Ev,Env,BGE,BDDE):-
  803  M:local_pita_setting(depth_bound,true),!,
  804  M:local_pita_setting(depth,DB),
  805  retractall(M:v(_,_,_)),
  806  retractall(M:av(_,_,_)),
  807  retractall(M:dec(_,_,_)),
  808  add_bdd_arg_db(Goal,Env,BDD,DB,M,Goal1),%DB=depth bound
  809  (M:Goal1*->
  810    true
  811  ;
  812    zeroc(Env,BDD)
  813  ),
  814  add_bdd_arg_db(Ev,Env,BDDE,DB,M,Ev1),%DB=depth bound
  815  (M:Ev1*->
  816    true
  817  ;
  818    zeroc(Env,BDDE)
  819  ),
  820  andcnf(Env,BDD,BDDE,BGE).
  821
  822
  823
  824get_cond_node(M:Goal,M:Ev,Env,BGE,BDDE):- %with DB=false
  825  retractall(M:v(_,_,_)),
  826  retractall(M:av(_,_,_)),
  827  retractall(M:dec(_,_,_)),
  828  add_bdd_arg(Goal,Env,BDD,M,Goal1),
  829  (M:Goal1*->
  830    true
  831  ;
  832    zeroc(Env,BDD)
  833  ),
  834  add_bdd_arg(Ev,Env,BDDE,M,Ev1),
  835  (M:Ev1*->
  836    true
  837  ;
  838    zeroc(Env,BDDE)
  839  ),
  840  andcnf(Env,BDD,BDDE,BGE).
  841
  842
  843get_next_goal_number(PName,R):-
  844  retract(PName:goal_n(R)),
  845  R1 is R+1,
  846  assert(PName:goal_n(R1)).
  847
  848
  849get_next_rule_number(PName,R):-
  850  retract(PName:rule_n(R)),
  851  R1 is R+1,
  852  assert(PName:rule_n(R1)).
  853
  854
  855assert_all([],_M,[]).
  856
  857assert_all([H|T],M,[HRef|TRef]):-
  858  assertz(M:H,HRef),
  859  assert_all(T,M,TRef).
  860
  861
  862retract_all([]):-!.
  863
  864retract_all([H|T]):-
  865  erase(H),
  866  retract_all(T).
 get_var_n(++M:atomic, ++Environment:int, ++Rule:int, ++Substitution:term, ++Probabilities:list, -Variable:int) is det
Returns the index Variable of the random variable associated to rule with index Rule, grounding substitution Substitution and head distribution Probabilities in environment Environment. /
  875get_var_n(M,Env,R,S,Probs0,V):-
  876  M:query_rule(R,_H,_B,_S),!,
  877  (ground(Probs0)->
  878    maplist(is,Probs,Probs0),
  879    (M:v(R,S,V)->
  880      true
  881    ;
  882      add_query_var(Env,Probs,R,V),
  883      assert(M:v(R,S,V))
  884    )
  885  ;
  886    throw(error('Non ground probabilities not instantiated by the body'))
  887  ).
  888
  889get_var_n(M,Env,R,S,Probs0,V):-
  890  (ground(Probs0)->
  891    maplist(is,Probs,Probs0),
  892    (M:v(R,S,V)->
  893      true
  894    ;
  895      % format("P: ~w ~w ~n",[Probs,R]),
  896      add_var(Env,Probs,R,V),
  897      assert(M:v(R,S,V))
  898    )
  899  ;
  900    throw(error('Non ground probabilities not instantiated by the body'))
  901  ).
 get_dec_var_n(++M:atomic, ++Environment:int, ++Rule:int, ++Substitution:term, -Variable:int) is det
Returns the index Variable of the random variable associated to rule with index Rule in environment Environment. /
  909 get_dec_var_n(M,Env,R,S,V):-
  910  % format('get_dec_var: R: ~w - S: ~w - V: ~w - M: ~w ~n', [R,S,V,M]),
  911  ( M:dec(R,S,V) ->
  912  % findall([A,B,C],M:dec(A,B,C),LD),
  913    % writeln(LD),
  914    true ;
  915    add_decision_var(Env,R,V),
  916    % writeln("New dec var"),
  917    asserta(M:dec(R,S,V))
  918  ).
  919  % (M:v(R,S,V)->
  920  %     true
  921  %   ;
  922  %     % add_var(Env,1,R,V),
  923  %     assert(M:v(R,S,V))
  924  % ).
 get_abd_var_n(++M:atomic, ++Environment:int, ++Rule:int, ++Substitution:term, ++Probabilities:list, -Variable:int) is det
Returns the index Variable of the random variable associated to rule with index Rule, grounding substitution Substitution and head distribution Probabilities in environment Environment. /
  933get_abd_var_n(M,Env,R,S,Probs0,V):-
  934  (ground(Probs0)->
  935    maplist(is,Probs,Probs0),
  936    (M:av(R,S,V)->
  937      true
  938    ;
  939      add_abd_var(Env,Probs,R,V),
  940      assert(M:av(R,S,V))
  941    )
  942  ;
  943    throw(error('Non ground probabilities not instantiated by the body'))
  944  ).
 msw(:Var:term, ?Value:term, ++Environment:int, --BDD:int) is det
Returns a BDD representing Var=Value. This is a predicate for programs in the PRISM syntax /
  952msw(M:A,B,Env,BDD):-
  953  msw_int(M,A,B,Env,BDD).
 msw(:Var:term, ?Value:term, ++Environment:int, --BDD:int, ?DB:int) is det
Returns a BDD representing Var=Value when there is a depth bound on derivations. This is a predicate for programs in the PRISM syntax /
  962msw(M:A,B,Env,BDD,_DB):-
  963  msw_int(M,A,B,Env,BDD).
  964
  965msw_int(M,A,B,Env,BDD):-
  966  M:values(A,Values),
  967  M:sw(R,A,Probs0),
  968  (ground(Probs0)->
  969    maplist(is,Probs,Probs0),
  970    ((M:local_pita_setting(prism_memoization,true),M:v(R,A,V))->
  971      true
  972    ;
  973      add_var(Env,Probs,R,V)
  974    ),
  975    (M:local_pita_setting(prism_memoization,true)->
  976      assert(M:v(R,A,V))
  977    ;
  978      true
  979    ),
  980    nth0(N,Values,B),
  981    equalityc(Env,V,N,BDD)
  982  ;
  983    throw(error('Non ground probabilities not instantiated by the body'))
  984  ).
  985
  986
  987combine(V,P,V:P).
  988
  989add_bdd_arg(M:A,Env,BDD,M:A1):-
  990  A=..[P|Args],
  991  append(Args,[Env,BDD],Args1),
  992  A1=..[P|Args1].
  993
  994
  995add_bdd_arg_db(M:A,Env,BDD,DB,M:A1):-
  996  A=..[P|Args],
  997  append(Args,[Env,DB,BDD],Args1),
  998  A1=..[P|Args1].
  999
 1000
 1001add_bdd_arg(A,Env,BDD,_Module,A1):-
 1002  A=..[P|Args],
 1003  append(Args,[Env,BDD],Args1),
 1004  A1=..[P|Args1].
 1005
 1006
 1007add_bdd_arg_db(A,Env,BDD,DB,_Module,A1):-
 1008  A=..[P|Args],
 1009  append(Args,[Env,DB,BDD],Args1),
 1010  A1=..[P|Args1].
 1011
 1012add_mod_arg(A,_Module,A1):-
 1013  A=..[P|Args],
 1014  A1=..[P|Args].
 1015
 1016
 1017generate_rules_fact([],_Env,_VC,_R,_Probs,_N,[],_Module).
 1018
 1019generate_rules_fact([Head:_P1,'':_P2],Env,VC,R,Probs,N,[Clause],Module):-!,
 1020  add_bdd_arg(Head,Env,BDD,Module,Head1),
 1021  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))).
 1022
 1023generate_rules_fact([Head:_P|T],Env,VC,R,Probs,N,[Clause|Clauses],Module):-
 1024  add_bdd_arg(Head,Env,BDD,Module,Head1),
 1025  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))),
 1026  N1 is N+1,
 1027  generate_rules_fact(T,Env,VC,R,Probs,N1,Clauses,Module).
 1028
 1029
 1030generate_rules_fact_vars([],_Env,_R,_Probs,_N,[],_Module).
 1031
 1032generate_rules_fact_vars([Head:_P1,'':_P2],Env,R,Probs,N,[Clause],Module):-!,
 1033  term_variables([Head],VC),
 1034  add_bdd_arg(Head,Env,BDD,Module,Head1),
 1035  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))).
 1036
 1037generate_rules_fact_vars([Head:_P|T],Env,R,Probs,N,[Clause|Clauses],Module):-
 1038  term_variables([Head],VC),
 1039  add_bdd_arg(Head,Env,BDD,Module,Head1),
 1040  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))),
 1041  N1 is N+1,
 1042  generate_rules_fact_vars(T,Env,R,Probs,N1,Clauses,Module).
 1043
 1044
 1045generate_rules_fact_db([],_Env,_VC,_R,_Probs,_N,[],_Module).
 1046
 1047generate_rules_fact_db([Head:_P1,'':_P2],Env,VC,R,Probs,N,[Clause],Module):-!,
 1048  add_bdd_arg_db(Head,Env,BDD,_DB,Module,Head1),
 1049  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))).
 1050
 1051generate_rules_fact_db([Head:_P|T],Env,VC,R,Probs,N,[Clause|Clauses],Module):-
 1052  add_bdd_arg_db(Head,Env,BDD,_DB,Module,Head1),
 1053  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))),
 1054  N1 is N+1,
 1055  generate_rules_fact_db(T,Env,VC,R,Probs,N1,Clauses,Module).
 1056
 1057
 1058generate_clause(Head,Env,Body,VC,R,Probs,BDDAnd,N,Clause,Module):-
 1059  add_bdd_arg(Head,Env,BDD,Module,Head1),
 1060  Clause=(Head1:-(Body,get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,B),andc(Env,BDDAnd,B,BDD))).
 1061
 1062
 1063generate_clause_db(Head,Env,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module):-
 1064  add_bdd_arg_db(Head,Env,BDD,DBH,Module,Head1),
 1065  Clause=(Head1:-(DBH>=1,DB is DBH-1,Body,get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,B),andc(Env,BDDAnd,B,BDD))).
 1066
 1067
 1068generate_rules([],_Env,_Body,_VC,_R,_Probs,_BDDAnd,_N,[],_Module).
 1069
 1070generate_rules([Head:_P1,'':_P2],Env,Body,VC,R,Probs,BDDAnd,N,[Clause],Module):-!,
 1071  generate_clause(Head,Env,Body,VC,R,Probs,BDDAnd,N,Clause,Module).
 1072
 1073generate_rules([Head:_P|T],Env,Body,VC,R,Probs,BDDAnd,N,[Clause|Clauses],Module):-
 1074  generate_clause(Head,Env,Body,VC,R,Probs,BDDAnd,N,Clause,Module),
 1075  N1 is N+1,
 1076  generate_rules(T,Env,Body,VC,R,Probs,BDDAnd,N1,Clauses,Module).
 1077
 1078
 1079generate_rules_db([],_Env,_Body,_VC,_R,_Probs,_DB,_BDDAnd,_N,[],_Module):-!.
 1080
 1081generate_rules_db([Head:_P1,'':_P2],Env,Body,VC,R,Probs,DB,BDDAnd,N,[Clause],Module):-!,
 1082  generate_clause_db(Head,Env,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module).
 1083
 1084generate_rules_db([Head:_P|T],Env,Body,VC,R,Probs,DB,BDDAnd,N,[Clause|Clauses],Module):-
 1085  generate_clause_db(Head,Env,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module),!,%agg.cut
 1086  N1 is N+1,
 1087  generate_rules_db(T,Env,Body,VC,R,Probs,DB,BDDAnd,N1,Clauses,Module).
 1088
 1089
 1090
 1091process_body([],BDD,BDD,Vars,Vars,[],_Env,_Module).
 1092
 1093process_body([\+ H|T],BDD,BDD1,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1094  builtin(H),!,
 1095  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1096
 1097process_body([\+ db(H)|T],BDD,BDD1,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1098  !,
 1099  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1100
 1101process_body([\+ H|T],BDD,BDD1,Vars,[BDDH,BDDN,BDD2|Vars1],
 1102[H1,bdd_notc(Env,BDDH,BDDN),
 1103  andc(Env,BDD,BDDN,BDD2)|Rest],Env,Module):-!,
 1104  add_bdd_arg(H,Env,BDDH,Module,H1),
 1105  process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Env,Module).
 1106
 1107process_body([H|T],BDD,BDD1,Vars,Vars1,[H1|Rest],Env,Module):-
 1108  transform(H,H1),!,
 1109  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1110
 1111process_body([H|T],BDD,BDD1,Vars,Vars1,[H|Rest],Env,Module):-
 1112  builtin(H),!,
 1113  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1114
 1115process_body([db(H)|T],BDD,BDD1,Vars,Vars1,[H|Rest],Env,Module):-
 1116  !,
 1117  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1118
 1119process_body([H|T],BDD,BDD1,Vars,[BDDH,BDD2|Vars1],
 1120[H1,andc(Env,BDD,BDDH,BDD2)|Rest],Env,Module):-
 1121  add_bdd_arg(H,Env,BDDH,Module,H1),
 1122  process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Env,Module).
 1123
 1124process_body_db([],BDD,BDD,_DB,Vars,Vars,[],_Env,_Module):-!.
 1125
 1126process_body_db([\+ H|T],BDD,BDD1,DB,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1127  builtin(H),!,
 1128  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1129
 1130process_body_db([\+ db(H)|T],BDD,BDD1,DB,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1131  !,
 1132  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1133
 1134process_body_db([\+ H|T],BDD,BDD1,DB,Vars,[BDDH,BDDN,BDD2|Vars1],
 1135[H1,bdd_notc(Env,BDDH,BDDN),
 1136  andc(Env,BDD,BDDN,BDD2)|Rest],Env,Module):-!,
 1137  add_bdd_arg_db(H,Env,BDDH,DB,Module,H1),
 1138  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1139
 1140process_body_db([H|T],BDD,BDD1,DB,Vars,Vars1,[H1|Rest],Env,Module):-
 1141  transform(H,H1),!,
 1142  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1143
 1144process_body_db([H|T],BDD,BDD1,DB,Vars,Vars1,[H|Rest],Env,Module):-
 1145  builtin(H),!,
 1146  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1147
 1148process_body_db([db(H)|T],BDD,BDD1,DB,Vars,Vars1,[H|Rest],Env,Module):-
 1149  !,
 1150  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1151
 1152process_body_db([H|T],BDD,BDD1,DB,Vars,[BDDH,BDD2|Vars1],
 1153[H1,andc(Env,BDD,BDDH,BDD2)|Rest],Env,Module):-!, %agg. cut
 1154  add_bdd_arg_db(H,Env,BDDH,DB,Module,H1),
 1155  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1156
 1157
 1158process_head(HeadList, GroundHeadList1) :-
 1159  ground_prob(HeadList), !,
 1160  process_head_ground(HeadList, 0.0, GroundHeadList),
 1161  ( GroundHeadList = [V:P] -> 
 1162      P1 is 1.0 - P, 
 1163      GroundHeadList1 = [V:P,'':P1] ; 
 1164      GroundHeadList1 = GroundHeadList
 1165  ).
 1166
 1167process_head(HeadList0, HeadList):-
 1168  get_probs(HeadList0,PL),
 1169  foldl(minus,PL,1.0,PNull),
 1170  append(HeadList0,['':PNull],HeadList).
 1171
 1172minus(A,B,B-A).
 1173
 1174prob_ann(_:P0,P):-!, to_float(P0,P).
 1175prob_ann(P0::_,P):- to_float(P0, P).
 1176
 1177to_float(P0, P) :-
 1178  ground(P0), !,
 1179  P is float(P0).
 1180to_float(P, P).
 1181
 1182gen_head(H,P,VH,V,V1,H1:P):-copy_term((H,VH,V),(H1,VH,V1)).
 1183gen_head_disc(H,VH,V,V1:P,H1:P1):-copy_term((H,VH,V),(H1,VH,V1)),P1 is float(P).
 1184
 1185
 1186/* process_head_ground([Head:ProbHead], Prob, [Head:ProbHead|Null])
 1187 * ----------------------------------------------------------------
 1188 */
 1189process_head_ground([H], Prob, [Head:ProbHead1|Null]) :-
 1190  (H=Head:ProbHead;H=ProbHead::Head),!,
 1191  ProbHead1 is float(ProbHead),
 1192  ProbLast is 1.0 - Prob - ProbHead1,
 1193  prolog_load_context(module, M),pita_input_mod(M),
 1194  M:local_pita_setting(epsilon_parsing, Eps),
 1195  EpsNeg is - Eps,
 1196  ProbLast > EpsNeg,
 1197  (ProbLast > Eps ->
 1198    Null = ['':ProbLast]
 1199  ;
 1200    Null = []
 1201  ).
 1202
 1203process_head_ground([H|Tail], Prob, [Head:ProbHead1|Next]) :-
 1204  (H=Head:ProbHead;H=ProbHead::Head),
 1205  ProbHead1 is float(ProbHead),
 1206  ProbNext is Prob + ProbHead1,
 1207  process_head_ground(Tail, ProbNext, Next).
 1208
 1209
 1210ground_prob([]).
 1211
 1212ground_prob([_Head:ProbHead|Tail]) :-!,
 1213  ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
 1214  ground_prob(Tail).
 1215
 1216ground_prob([ProbHead::_Head|Tail]) :-
 1217  ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
 1218  ground_prob(Tail).
 1219
 1220
 1221get_probs(Head, PL):-
 1222  maplist(prob_ann,Head,PL).
 1223
 1224/*get_probs([], []).
 1225
 1226get_probs([_H:P|T], [P1|T1]) :-
 1227  P1 is P,
 1228  get_probs(T, T1).
 1229*/
 set_pita(:Parameter:atom, +Value:term) is det
The predicate sets the value of a parameter For a list of parameters see https://friguzzi.github.io/cplint/

/

 1241set_pita(M:Parameter,Value):-
 1242  must_be(atom,Parameter),
 1243  must_be(nonvar,Value),
 1244  retract(M:local_pita_setting(Parameter,_)),
 1245  assert(M:local_pita_setting(Parameter,Value)).
 setting_pita(:Parameter:atom, ?Value:term) is det
The predicate returns the value of a parameter For a list of parameters see https://friguzzi.github.io/cplint/ /
 1254setting_pita(M:P,V):-
 1255  must_be(atom,P),
 1256  M:local_pita_setting(P,V).
 1257
 1258delete_equal([],_,[]).
 1259
 1260delete_equal([H|T],E,T):-
 1261  H == E,!.
 1262
 1263delete_equal([H|T],E,[H|T1]):-
 1264  delete_equal(T,E,T1).
 1265
 1266set_sw(M:A,B):-
 1267  get_next_rule_number(M,R),
 1268  assert(M:sw(R,A,B)).
 1269
 1270act(M,A/B):-
 1271  (M:local_pita_setting(depth_bound,true)->
 1272    B1 is B + 3
 1273  ;
 1274    B1 is B + 2
 1275  ),
 1276  M:(dynamic A/B1).
 1277
 1278tab(M,A/B,P):-
 1279  length(Args0,B),
 1280  (M:local_pita_setting(depth_bound,true)->
 1281    ExtraArgs=[_,_,lattice(orc/3)]
 1282  ;
 1283    ExtraArgs=[_,lattice(orc/3)]
 1284  ),
 1285  append(Args0,ExtraArgs,Args),
 1286  P=..[A|Args],
 1287  PT=..[A|Args0],
 1288  assert(M:tabled(PT)).
 1289
 1290zero_clause(M,A/B,(H:-maplist(nonvar,Args0),zeroc(Env,BDD))):-
 1291  length(Args0,B),
 1292  (M:local_pita_setting(depth_bound,true)->
 1293    ExtraArgs=[Env,_,BDD]
 1294  ;
 1295    ExtraArgs=[Env,BDD]
 1296  ),
 1297  append(Args0,ExtraArgs,Args),
 1298  H=..[A|Args].
 1299
 1300to_table(M,Heads,[],Heads):-
 1301  M:local_pita_setting(tabling,explicit),!.
 1302
 1303to_table(M,Heads,TabDir,Heads1):-
 1304  maplist(tab_dir(M),Heads,TabDirList,Heads1L),
 1305  append(TabDirList,TabDir),
 1306%  maplist(system:term_expansion,TabDir,ProcTabDirL),
 1307%  append(ProcTabDirL,ProcTabDir),
 1308  append(Heads1L,Heads1).
 1309
 1310tab_dir(_M,'':_,[],[]):-!.
 1311
 1312% tab dir for decision variables
 1313% merge with the previous one?
 1314% the predicates are equal except
 1315% (?)::H and H:P
 1316tab_dir(M,D::H,[],[H]):-
 1317  (D == ? ; D == (?)),
 1318  M:tabled(H),!.
 1319% tab dir for decision variables
 1320% merge with the previous one?
 1321% the predicates are equal except
 1322% (?)::H and '$util'(A,B)
 1323tab_dir(M,H,[],[H]):-
 1324  H=..[F|_],
 1325  F = utility,
 1326  M:tabled(H),!.
 1327
 1328tab_dir(M,H:P,[],[H:P]):-
 1329  M:tabled(H),!.
 1330
 1331tab_dir(M,P::H,[],[H:P]):-
 1332  P \== ?,
 1333  M:tabled(H),!.
 1334
 1335
 1336
 1337% tab dir for decision variables
 1338% merge with the previous one?
 1339% the predicates are equal
 1340% except variable n 2 and 4.
 1341tab_dir(M,P::H,[(:- table HT)],[H1]):-
 1342  (P== ?;P == (?)),!,
 1343  functor(H,F,A0),
 1344  functor(PT,F,A0),
 1345  PT=..[F|Args0],
 1346  (M:local_pita_setting(depth_bound,true)->
 1347    ExtraArgs=[_,_,lattice(orc/3)]
 1348  ;
 1349    ExtraArgs=[_,lattice(orc/3)]
 1350  ),
 1351  append(Args0,ExtraArgs,Args),
 1352  HT=..[F|Args],
 1353  H=..[_|ArgsH],
 1354  H1=..[F|ArgsH],
 1355  assert(M:tabled(PT)),
 1356  zero_clause(M,F/A0,LZ),
 1357  assert(M:zero_clauses(LZ)).
 1358
 1359% tab dir for utility variables
 1360% merge with the previous one?
 1361% the predicates are equal
 1362% except variable n 2 and 4.
 1363tab_dir(M,H,[(:- table HT)],[H1]):-
 1364  H=..[F|_],
 1365  F = utility,!,
 1366  functor(H,F,A0),
 1367  functor(PT,F,A0),
 1368  PT=..[F|Args0],
 1369  (M:local_pita_setting(depth_bound,true)->
 1370    ExtraArgs=[_,_,lattice(orc/3)]
 1371  ;
 1372    ExtraArgs=[_,lattice(orc/3)]
 1373  ),
 1374  append(Args0,ExtraArgs,Args),
 1375  HT=..[F|Args],
 1376  H=..[_|ArgsH],
 1377  H1=..[F|ArgsH],
 1378  assert(M:tabled(PT)),
 1379  zero_clause(M,F/A0,LZ),
 1380  assert(M:zero_clauses(LZ)).
 1381
 1382
 1383tab_dir(M,Head,[(:- table HT)],[H1:P]):-
 1384  (Head=H:P;Head=P::H),!,
 1385  functor(H,F,A0),
 1386  functor(PT,F,A0),
 1387  PT=..[F|Args0],
 1388  (M:local_pita_setting(depth_bound,true)->
 1389    ExtraArgs=[_,_,lattice(orc/3)]
 1390  ;
 1391    ExtraArgs=[_,lattice(orc/3)]
 1392  ),
 1393  append(Args0,ExtraArgs,Args),
 1394  HT=..[F|Args],
 1395  H=..[_|ArgsH],
 1396  H1=..[F|ArgsH],
 1397  assert(M:tabled(PT)),
 1398  zero_clause(M,F/A0,LZ),
 1399  assert(M:zero_clauses(LZ)).
 1400
 1401
 1402pita_expansion(begin_of_file,_):-
 1403  !,
 1404  fail.
 1405
 1406pita_expansion((:- action Conj), []) :-!,
 1407  prolog_load_context(module, M),
 1408  pita_input_mod(M),!,
 1409  list2and(L,Conj),
 1410  maplist(act(M),L).
 1411
 1412pita_expansion((:- begin_plp), []) :-
 1413  prolog_load_context(module, M),
 1414  pita_input_mod(M),!,
 1415  assert(M:pita_on).
 1416
 1417pita_expansion((:- end_plp), []) :-
 1418  prolog_load_context(module, M),
 1419  pita_input_mod(M),!,
 1420  retractall(M:pita_on).
 1421
 1422pita_expansion((:- begin_lpad), []) :-
 1423  prolog_load_context(module, M),
 1424  pita_input_mod(M),!,
 1425  assert(M:pita_on).
 1426
 1427pita_expansion((:- end_lpad), []) :-
 1428  prolog_load_context(module, M),
 1429  pita_input_mod(M),!,
 1430  retractall(M:pita_on).
 1431
 1432pita_expansion(values(A,B), values(A,B)) :-
 1433  prolog_load_context(module, M),
 1434  pita_input_mod(M),M:pita_on,!.
 1435
 1436pita_expansion((:- Constraint), Clauses) :-
 1437  % constraint for abduction
 1438  prolog_load_context(module, M),
 1439  pita_input_mod(M),
 1440  M:pita_on,
 1441  Constraint\= (table _),
 1442  Constraint\=(multifile _),
 1443  Constraint\=set_sw(_,_),!,
 1444  Constraint\= (use_module(_)),
 1445  Constraint\= (pita),
 1446  Constraint\= (set_pita(_,_)),
 1447  Constraint\= (if(_)),
 1448  Constraint\= (endif),
 1449  Constraint\= (use_rendering(_)),
 1450  list2and(BodyList, Constraint),
 1451  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
 1452  append([onec(Env,BDD)],BodyList2,BodyList3),
 1453  list2and(BodyList3,Body2),
 1454  to_table(M,['$cons':_],TabDir,[Head1:_]),
 1455  add_bdd_arg(Head1,Env,BDDAnd,M,Head2),
 1456  append(TabDir,[(Head2 :- Body2)],Clauses).
 1457
 1458pita_expansion((Prob:- Constraint), Clauses) :-
 1459  % probabilistic constraint for abduction
 1460  prolog_load_context(module, M),
 1461  pita_input_mod(M),
 1462  M:pita_on,
 1463  float(Prob),
 1464  Constraint\= (table _),
 1465  Constraint\=(multifile _),
 1466  Constraint\=set_sw(_,_),!,
 1467  list2or(HeadListOr, '$cons':Prob),
 1468  process_head(HeadListOr, HeadList),
 1469  list2and(BodyList, Constraint),
 1470  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
 1471  append([onec(Env,BDD)],BodyList2,BodyList3),
 1472  list2and(BodyList3,Body2),
 1473  append(HeadList,BodyList,List),
 1474  term_variables(List,VC),
 1475  get_next_rule_number(M,R),
 1476  get_probs(HeadList,Probs),%***test single_vars
 1477  (M:local_pita_setting(single_var,true)->
 1478    VC1 = []
 1479  ;
 1480    VC1 = VC
 1481  ),
 1482  to_table(M,HeadList,TabDir,[H1:_]),
 1483  generate_clause(H1,Env,Body2,VC1,R,Probs,BDDAnd,0,Clauses0,M),
 1484  append(TabDir,[Clauses0],Clauses).
 1485
 1486
 1487
 1488pita_expansion(map_query(Clause),[query_rule(R,HeadList,Body,VC)|Clauses]):-
 1489  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,!,
 1490  M:rule_n(R),
 1491  pita_expansion(Clause, Clauses0),
 1492  (Clause=(Head:-Body)->
 1493    true
 1494  ;
 1495    Head=Clause,
 1496    Body=true
 1497  ),
 1498  (is_list(Clauses0)->
 1499    Clauses=Clauses0
 1500  ;
 1501    Clauses=[Clauses0]
 1502  ),
 1503  term_variables(Clause,VC),
 1504  list2or(HeadListOr, Head),
 1505  process_head(HeadListOr, HeadList).
 1506
 1507pita_expansion(abducible(Head),[Clause,abd(R,S,H)]) :-
 1508  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,!,
 1509  ((Head=(H:P);Head=(P::H))->
 1510    P1 is P,
 1511    P0 is 1.0-P,
 1512    Probs=[P1,P0]
 1513  ;
 1514    H=Head,
 1515    Probs=[1.0,1.0]
 1516  ),
 1517  term_variables([H],VC),
 1518  get_next_rule_number(M,R),
 1519  add_bdd_arg(H,Env,BDD,M,Head1), %***test single_var
 1520  (M:local_pita_setting(single_var,true)->
 1521    S=[]
 1522  ;
 1523    S=VC
 1524  ),
 1525  Clause=(Head1:-(get_abd_var_n(M,Env,R,S,Probs,V),equalityc(Env,V,0,BDD))).
 1526
 1527% decision facts with body and ground variables
 1528% ?::a:- b.
 1529pita_expansion(Head:-Body,[Clause,rule_by_num(R,H,Body1,[]),TabDir]) :-
 1530  prolog_load_context(module, M),
 1531  pita_input_mod(M),
 1532  M:pita_on,
 1533  ((Head:- Body) \= ((pita_expansion(_,_)) :- _ )),
 1534  (Head \= ((pita_expansion(_,_)) :- _ )),
 1535  (Head = ((?) :: H) ; Head = decision(H)), ground(H), !,
 1536  list2and(BodyList, Body),
 1537  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList1,Env,M),
 1538  append([onec(Env,BDD)],BodyList1,BodyList2),
 1539  list2and(BodyList2,Body1),
 1540  append([Head],BodyList,List),
 1541  term_variables(List,VC),
 1542  get_next_rule_number(M,R),
 1543  to_table(M,[Head],TabDir,HeadList1),
 1544  HeadList1 = [H1],
 1545  add_bdd_arg(H1,Env,BO,M,Head1),
 1546  Clause = (Head1:-(Body1,get_dec_var_n(M,Env,R,VC,V), equalityc(Env,V,0,B), andc(Env,BDDAnd,B,BO))).
 1547
 1548% decision facts without body and ground variables
 1549% ?::a.
 1550pita_expansion(Head,[Clause,rule_by_num(R,[H],[],VC),TabDir]) :-
 1551  prolog_load_context(module, M),
 1552  pita_input_mod(M),
 1553  M:pita_on,
 1554  (Head \= ((pita_expansion(_,_)) :- _ )),
 1555  (Head = ((?) :: H) ; Head = decision(H)), ground(H), !,
 1556  term_variables([Head],VC), % VC is [] so maybe avoid the computation
 1557  get_next_rule_number(M,R),
 1558  to_table(M,[Head],TabDir,HeadList1),
 1559  HeadList1 = [H1],
 1560  add_bdd_arg(H1,Env,BDD,M,Head1),
 1561  Clause = (Head1:-(get_dec_var_n(M,Env,R,VC,V),equalityc(Env,V,0,BDD))).
 1562
 1563% utility attributes with body
 1564% utility(a,N):- b.
 1565pita_expansion(Head:-Body,[Clause,TabDir,'$util'(H,U)]) :-
 1566  prolog_load_context(module, M),
 1567  pita_input_mod(M),
 1568  M:pita_on,
 1569  (Head \= ((pita_expansion(_,_)) :- _ )),
 1570  (Head = (H => U) ; Head = utility(H,U)),
 1571  ( ground(H) -> true ; throw(error("Expected ground decision fact in utility/2"))),
 1572  ( number(U) -> true ; throw(error("Expected a number for utility in utility/2"))), !,
 1573  list2and(BodyList, Body),
 1574  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList1,Env,M),
 1575  append([onec(Env,BDD)],BodyList1,BodyList2),
 1576  list2and(BodyList2,Body1),
 1577  append([Head],BodyList,List),
 1578  term_variables(List,VC),
 1579  get_next_rule_number(M,R),
 1580  to_table(M,[Head],TabDir,HeadList1),  % <---------------------- if HEAD = H => U does NOT WORKS
 1581  HeadList1 = [H1],
 1582  add_bdd_arg(H1,Env,BO,M,Head2),
 1583  Clause = (Head2:-(Body1,get_var_n(M,Env,R,VC,V),equalityc(Env,V,0,B),andc(Env,BDDAnd,B,BO))).
 1584
 1585% utility attributes without body
 1586% utility(a,N).
 1587pita_expansion(Head,'$util'(H,U)) :-
 1588  prolog_load_context(module, M),
 1589  pita_input_mod(M),
 1590  M:pita_on,
 1591  (Head \= ((pita_expansion(_,_)) :- _ )),
 1592  (Head = (H => U) ; Head = utility(H,U)),
 1593  ( ground(H) -> true ; throw(error("Expected ground decision fact in utility/2"))),
 1594  ( number(U) -> true ; throw(error("Expected a number for utility in utility/2"))), !.
 1595
 1596pita_expansion(Head:-Body,[rule_by_num(R,HeadList,BodyList,VC1)|Clauses]) :-
 1597  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1598% disjunctive clause with uniform distr
 1599  (Head \= ((pita_expansion(_,_)) :- _ )),
 1600  Head = (_:P),
 1601  nonvar(P),
 1602  Head=(H:uniform(Var,D0)),!,
 1603  (var(D0)->
 1604     throw(error('Non ground list of values in uniform(Var,Values)'))
 1605  ;
 1606    true
 1607  ),
 1608  length(D0,Len),
 1609  Prob is 1.0/Len,
 1610  term_variables([H],VH),
 1611  delete_equal(VH,Var,VH1),
 1612  maplist(gen_head(H,Prob,VH1,Var),D0,HeadList),
 1613  get_next_rule_number(M,R),
 1614  get_probs(HeadList,Probs), %**** test single_var
 1615  list2and(BodyList, Body),
 1616  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList1,Env,M),
 1617  append([onec(Env,BDD)],BodyList1,BodyList2),
 1618  list2and(BodyList2,Body1),
 1619  append(HeadList,BodyList,List),
 1620  term_variables(List,VC),
 1621  (M:local_pita_setting(single_var,true)->
 1622    VC1 = []
 1623  ;
 1624    VC1 = VC
 1625  ),
 1626  to_table(M,HeadList,TabDir,HeadList1),
 1627  generate_rules(HeadList1,Env,Body1,VC1,R,Probs,BDDAnd,0,Clauses0,M),
 1628  append(TabDir,Clauses0,Clauses).
 1629
 1630
 1631pita_expansion(Head:-Body,[rule_by_num(R,HeadList,BodyList,VC1)|Clauses]) :-
 1632  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1633% disjunctive clause with discrete distr
 1634  (Head \= ((pita_expansion(_,_)) :- _ )),
 1635  Head = (_:P),
 1636  nonvar(P),
 1637  (Head=(H:discrete(Var,D));Head=(H:finite(Var,D))),!,
 1638  (var(D)->
 1639     throw(error('Non ground list of values in discrete(Var,Values) or finite(Var,Values)'))
 1640  ;
 1641    true
 1642  ),
 1643  term_variables([H],VH),
 1644  delete_equal(VH,Var,VH1),
 1645  maplist(gen_head_disc(H,VH1,Var),D,HeadList),
 1646  get_next_rule_number(M,R),
 1647  get_probs(HeadList,Probs), %**** test single_var
 1648  list2and(BodyList, Body),
 1649  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList1,Env,M),
 1650  append([onec(Env,BDD)],BodyList1,BodyList2),
 1651  list2and(BodyList2,Body1),
 1652  append(HeadList,BodyList,List),
 1653  term_variables(List,VC),
 1654  (M:local_pita_setting(single_var,true)->
 1655    VC1 = []
 1656  ;
 1657    VC1 = VC
 1658  ),
 1659  to_table(M,HeadList,TabDir,HeadList1),
 1660  generate_rules(HeadList1,Env,Body1,VC1,R,Probs,BDDAnd,0,Clauses0,M),
 1661  append(TabDir,Clauses0,Clauses).
 1662
 1663pita_expansion((Head :- Body),
 1664  [rule_by_num(R,HeadList,BodyList,VC1)|Clauses]):-
 1665  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1666  M:local_pita_setting(depth_bound,true),
 1667% disjunctive clause with more than one head atom e depth_bound
 1668  Head = (_;_), !,
 1669  list2or(HeadListOr, Head),
 1670  process_head(HeadListOr, HeadList),
 1671  list2and(BodyList, Body),
 1672  process_body_db(BodyList,BDD,BDDAnd, DB,[],_Vars,BodyList1,Env,M),
 1673  append([onec(Env,BDD)],BodyList1,BodyList2),
 1674  list2and(BodyList2,Body1),
 1675  append(HeadList,BodyList,List),
 1676  term_variables(List,VC),
 1677  get_next_rule_number(M,R),
 1678  get_probs(HeadList,Probs),
 1679  (M:local_pita_setting(single_var,true)->
 1680    VC1 = []
 1681  ;
 1682    VC1 = VC
 1683  ),
 1684  to_table(M,HeadList,TabDir,HeadList1),
 1685  generate_rules_db(HeadList1,Env,Body1,VC1,R,Probs,DB,BDDAnd,0,Clauses0,M),
 1686  append(TabDir,Clauses0,Clauses).
 1687
 1688
 1689pita_expansion((Head :- Body),
 1690  [rule_by_num(R,HeadList,BodyList,VC1)|Clauses]):-
 1691	  %trace,
 1692    ((Head:- Body) \= ((pita_expansion(_,_)) :- _ )),
 1693  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1694% disjunctive clause with more than one head atom senza depth_bound
 1695  Head = (_;_), !,
 1696  list2or(HeadListOr, Head),
 1697  process_head(HeadListOr, HeadList),
 1698  list2and(BodyList, Body),
 1699  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList1,Env,M),
 1700  append([onec(Env,BDD)],BodyList1,BodyList2),
 1701  list2and(BodyList2,Body1),
 1702  append(HeadList,BodyList,List),
 1703  term_variables(List,VC),
 1704  get_next_rule_number(M,R),
 1705  get_probs(HeadList,Probs),
 1706  (M:local_pita_setting(single_var,true)->
 1707    VC1 = []
 1708  ;
 1709    VC1 = VC
 1710  ),
 1711  to_table(M,HeadList,TabDir,HeadList1),
 1712  generate_rules(HeadList1,Env,Body1,VC1,R,Probs,BDDAnd,0,Clauses0,M),
 1713  append(TabDir,Clauses0,Clauses).
 1714
 1715pita_expansion((Head :- Body), []) :-
 1716% disjunctive clause with a single head atom con prob. 0 senza depth_bound --> la regola non e' caricata nella teoria e non e' conteggiata in NR
 1717  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1718  ((Head:-Body) \= ((pita_expansion(_,_) ):- _ )),
 1719  (Head = (_:P);Head=(P::_)),
 1720  ground(P),
 1721  P=:=0.0, !.
 1722
 1723pita_expansion((Head :- Body), Clauses) :-
 1724% disjunctive clause with a single head atom e depth_bound
 1725  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1726  M:local_pita_setting(depth_bound,true),
 1727  ((Head:-Body) \= ((pita_expansion(_,_) ):- _ )),
 1728  list2or(HeadListOr, Head),
 1729  process_head(HeadListOr, HeadList),
 1730  HeadList=[_H:_],!,
 1731  list2and(BodyList, Body),
 1732  process_body_db(BodyList,BDD,BDDAnd,DB,[],_Vars,BodyList2,Env,M),
 1733  append([onec(Env,BDD)],BodyList2,BodyList3),
 1734  list2and([DBH>=1,DB is DBH -1|BodyList3],Body1),
 1735  to_table(M,HeadList,TabDir,[H1:_]),
 1736  add_bdd_arg_db(H1,Env,BDDAnd,DBH,M,Head1),
 1737  append(TabDir,[(Head1 :- Body1)],Clauses).
 1738
 1739pita_expansion((Head :- Body), Clauses) :-
 1740% disjunctive clause with a single head atom senza depth_bound con prob =1
 1741  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1742  ((Head:-Body) \= ((pita_expansion(_,_) ):- _ )),
 1743  list2or(HeadListOr, Head),
 1744  process_head(HeadListOr, HeadList),
 1745  HeadList=[_H:_],!,
 1746  list2and(BodyList, Body),
 1747  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
 1748  append([onec(Env,BDD)],BodyList2,BodyList3),
 1749  list2and(BodyList3,Body1),
 1750  to_table(M,HeadList,TabDir,[H1:_]),
 1751  add_bdd_arg(H1,Env,BDDAnd,M,Head1),
 1752  append(TabDir,[(Head1 :- Body1)],Clauses).
 1753
 1754pita_expansion((Head :- Body), Clauses) :-
 1755% disjunctive clause with a single head atom e DB, con prob. diversa da 1
 1756  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1757  M:local_pita_setting(depth_bound,true),
 1758  ((Head:-Body) \= ((pita_expansion(_,_) ):- _ )),
 1759  (Head = (_H:_);Head=(_::_H)), !,
 1760  list2or(HeadListOr, Head),
 1761  process_head(HeadListOr, HeadList),
 1762  list2and(BodyList, Body),
 1763  process_body_db(BodyList,BDD,BDDAnd,DB,[],_Vars,BodyList2,Env,M),
 1764  append([onec(Env,BDD)],BodyList2,BodyList3),
 1765  list2and(BodyList3,Body2),
 1766  append(HeadList,BodyList,List),
 1767  term_variables(List,VC),
 1768  get_next_rule_number(M,R),
 1769  get_probs(HeadList,Probs),%***test single_var
 1770  (M:local_pita_setting(single_var,true)->
 1771    VC1 = []
 1772  ;
 1773    VC1 = VC
 1774  ),
 1775  to_table(M,HeadList,TabDir,[H1:_]),
 1776  generate_clause_db(H1,Env,Body2,VC1,R,Probs,DB,BDDAnd,0,Clauses0,M),
 1777  append(TabDir,[Clauses0],Clauses).
 1778
 1779pita_expansion((Head :- Body), [rule_by_num(R,HeadList,BodyList,VC1)|Clauses]) :-
 1780% disjunctive clause with a single head atom senza DB, con prob. diversa da 1
 1781  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1782  ((Head:-Body) \= ((pita_expansion(_,_) ):- _ )),
 1783  (Head = (_H:_);Head = (_::_H)), !,
 1784  list2or(HeadListOr, Head),
 1785  process_head(HeadListOr, HeadList),
 1786  list2and(BodyList, Body),
 1787  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
 1788  append([onec(Env,BDD)],BodyList2,BodyList3),
 1789  list2and(BodyList3,Body2),
 1790  append(HeadList,BodyList,List),
 1791  term_variables(List,VC),
 1792  get_next_rule_number(M,R),
 1793  get_probs(HeadList,Probs),%***test single_vars
 1794  (M:local_pita_setting(single_var,true)->
 1795    VC1 = []
 1796  ;
 1797    VC1 = VC
 1798  ),
 1799  to_table(M,HeadList,TabDir,[H1:_]),
 1800  generate_clause(H1,Env,Body2,VC1,R,Probs,BDDAnd,0,Clauses0,M),
 1801  append(TabDir,[Clauses0],Clauses).
 1802
 1803/*pita_expansion((Head :- Body),Clauses) :-
 1804% definite clause for db facts
 1805  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1806  ((Head:-Body) \= ((pita_expansion(_,_)) :- _ )),
 1807  Head=db(Head1),!,
 1808  Clauses=(Head1 :- Body).
 1809*/
 1810pita_expansion((Head :- Body),Clauses) :-
 1811% definite clause with depth_bound
 1812  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1813  M:local_pita_setting(depth_bound,true),
 1814  ((Head:-Body) \= ((pita_expansion(_,_)) :- _ )),!,
 1815  list2and(BodyList, Body),
 1816  process_body_db(BodyList,BDD,BDDAnd,DB,[],_Vars,BodyList2,Env,M),
 1817  append([onec(Env,BDD)],BodyList2,BodyList3),
 1818  list2and([DBH>=1,DB is DBH-1|BodyList3],Body1),
 1819  to_table(M,[Head:_],TabDir,[Head1:_]),
 1820  add_bdd_arg_db(Head1,Env,BDDAnd,DBH,M,Head2),
 1821  append(TabDir,[(Head2 :- Body1)],Clauses).
 1822
 1823pita_expansion((Head :- Body),Clauses) :-
 1824%  writeln((Head:-Body)),
 1825 % trace,
 1826% definite clause senza DB
 1827  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1828  ((Head:-Body) \= ((pita_expansion(_,_)) :- _ )),!,
 1829  list2and(BodyList, Body),
 1830  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
 1831  append([onec(Env,BDD)],BodyList2,BodyList3),
 1832  list2and(BodyList3,Body2),
 1833  to_table(M,[Head:_],TabDir,[Head1:_]),
 1834  add_bdd_arg(Head1,Env,BDDAnd,M,Head2),
 1835  append(TabDir,[(Head2 :- Body2)],Clauses).
 1836
 1837pita_expansion(Head,
 1838  [rule_by_num(R,HeadList,[],VC1)|Clauses]) :-
 1839  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1840  M:local_pita_setting(depth_bound,true),
 1841% disjunctive FACT with more than one head atom e db
 1842  Head=(_;_), !,
 1843  list2or(HeadListOr, Head),
 1844  process_head(HeadListOr, HeadList),
 1845  term_variables(HeadList,VC),
 1846  get_next_rule_number(M,R),
 1847  get_probs(HeadList,Probs),
 1848  (M:local_pita_setting(single_var,true)->
 1849    VC1 = []
 1850  ;
 1851    VC1 = VC
 1852  ),
 1853  to_table(M,HeadList,TabDir,HeadList1),
 1854  generate_rules_fact_db(HeadList1,_Env,VC1,R,Probs,0,Clauses0,M),
 1855  append(TabDir,Clauses0,Clauses).
 1856
 1857pita_expansion(Head,[rule_by_num(R,HeadList,[],VC1)|Clauses]) :-
 1858  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1859% disjunctive fact with more than one head atom senza db
 1860  Head=(_;_), !,
 1861  list2or(HeadListOr, Head),
 1862  process_head(HeadListOr, HeadList),
 1863  term_variables(HeadList,VC),
 1864  get_next_rule_number(M,R),
 1865  get_probs(HeadList,Probs), %**** test single_var
 1866  (M:local_pita_setting(single_var,true)->
 1867    VC1 = []
 1868  ;
 1869    VC1 = VC
 1870  ),
 1871  to_table(M,HeadList,TabDir,HeadList1),
 1872  generate_rules_fact(HeadList1,_Env,VC1,R,Probs,0,Clauses0,M),
 1873  append(TabDir,Clauses0,Clauses).
 1874
 1875pita_expansion(Head,[rule_by_num(R,HeadList,[],VC1)|Clauses]) :-
 1876  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1877% disjunctive fact with uniform distr
 1878  (Head \= ((pita_expansion(_,_)) :- _ )),
 1879  Head = (_:P),
 1880  nonvar(P),
 1881  Head=(H:uniform(Var,D0)),!,
 1882  (var(D0)->
 1883     throw(error('Non ground list of values in uniform(Var,Values)'))
 1884  ;
 1885    true
 1886  ),
 1887  length(D0,Len),
 1888  Prob is 1.0/Len,
 1889  term_variables([H],VH),
 1890  delete_equal(VH,Var,VH1),
 1891  maplist(gen_head(H,Prob,VH1,Var),D0,HeadList),
 1892  get_next_rule_number(M,R),
 1893  get_probs(HeadList,Probs), %**** test single_var
 1894  term_variables(HeadList,VC),
 1895  (M:local_pita_setting(single_var,true)->
 1896    VC1 = []
 1897  ;
 1898    VC1 = VC
 1899  ),
 1900  to_table(M,HeadList,TabDir,HeadList1),
 1901  generate_rules_fact(HeadList1,_Env,VC1,R,Probs,0,Clauses0,M),
 1902  append(TabDir,Clauses0,Clauses).
 1903
 1904
 1905pita_expansion(Head,[rule_by_num(R,HeadList,[],VC1)|Clauses]) :-
 1906  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1907  % disjunctive fact with discrete distr
 1908  (Head \= ((pita_expansion(_,_)) :- _ )),
 1909  Head = (_:P),
 1910  nonvar(P),
 1911  (Head=(H:discrete(Var,D));Head=(H:finite(Var,D))),!,
 1912  (var(D)->
 1913     throw(error('Non ground list of values in discrete(Var,Values) or finite(Var,Values)'))
 1914  ;
 1915    true
 1916  ),
 1917  term_variables([H],VH),
 1918  delete_equal(VH,Var,VH1),
 1919  maplist(gen_head_disc(H,VH1,Var),D,HeadList),
 1920  get_next_rule_number(M,R),
 1921  get_probs(HeadList,Probs), %**** test single_var
 1922  term_variables(HeadList,VC),
 1923  (M:local_pita_setting(single_var,true)->
 1924    VC1 = []
 1925  ;
 1926    VC1 = VC
 1927  ),
 1928  to_table(M,HeadList,TabDir,HeadList1),
 1929  generate_rules_fact(HeadList1,_Env,VC1,R,Probs,0,Clauses0,M),
 1930  append(TabDir,Clauses0,Clauses).
 1931
 1932pita_expansion(Head,[]) :-
 1933  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1934% disjunctive fact with a single head atom con prob. 0
 1935  (Head \= ((pita_expansion(_,_)) :- _ )),
 1936  (Head = (_:P); Head = (P::_)),
 1937  ground(P),
 1938  P=:=0.0, !.
 1939
 1940pita_expansion(Head,Clauses) :-
 1941  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1942  M:local_pita_setting(depth_bound,true),
 1943% disjunctive fact with a single head atom con prob.1 e db
 1944  (Head \= ((pita_expansion(_,_)) :- _ )),
 1945  (Head = (_H:P); Head = (P::_H)),
 1946  ground(P),
 1947  P=:=1.0, !,
 1948  list2and([onec(Env,BDD)],Body1),
 1949  to_table(M,[Head:_],TabDir,[H1:_]),
 1950  add_bdd_arg_db(H1,Env,BDD,_DB,M,Head1),
 1951  append(TabDir,[(Head1 :- Body1)],Clauses).
 1952
 1953pita_expansion(Head,Clauses) :-
 1954  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1955% disjunctive fact with a single head atom con prob. 1, senza db
 1956  (Head \= ((pita_expansion(_,_)) :- _ )),
 1957  (Head = (_H:P);Head =(P::_H)),
 1958  ground(P),
 1959  P=:=1.0, !,
 1960  list2and([onec(Env,BDD)],Body1),
 1961  to_table(M,[Head:_],TabDir,[H1:_]),
 1962  add_bdd_arg(H1,Env,BDD,M,Head1),
 1963  append(TabDir,[(Head1 :- Body1)],Clauses).
 1964
 1965pita_expansion(Head,[rule_by_num(R,HeadList,[],VC1)|Clauses]) :-
 1966  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1967  M:local_pita_setting(depth_bound,true),
 1968% disjunctive fact with a single head atom e prob. generiche, con db
 1969  (Head \= ((pita_expansion(_,_)) :- _ )),
 1970  (Head=(_H:_);Head=(_::_H)), !,
 1971  list2or(HeadListOr, Head),
 1972  process_head(HeadListOr, HeadList),
 1973  term_variables(HeadList,VC),
 1974  get_next_rule_number(M,R),
 1975  get_probs(HeadList,Probs),
 1976  to_table(M,HeadList,TabDir,[H1:_]),
 1977  add_bdd_arg_db(H1,Env,BDD,_DB,M,Head1),
 1978  (M:local_pita_setting(single_var,true)->
 1979    VC1 = []
 1980  ;
 1981    VC1 = VC
 1982  ),
 1983  Clauses0=[(Head1:-(get_var_n(M,Env,R,VC1,Probs,V),equalityc(Env,V,0,BDD)))],
 1984  append(TabDir,Clauses0,Clauses).
 1985
 1986pita_expansion(Head,[rule_by_num(R,HeadList,[],VC1)|Clauses]) :-
 1987  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 1988% disjunctive fact with a single head atom e prob. generiche, senza db
 1989  (Head \= ((pita_expansion(_,_)) :- _ )),
 1990  (Head=(_H:_);Head=(_::_H)), !,
 1991  list2or(HeadListOr, Head),
 1992  process_head(HeadListOr, HeadList),
 1993  term_variables(HeadList,VC),
 1994  get_next_rule_number(M,R),
 1995  get_probs(HeadList,Probs),
 1996  to_table(M,HeadList,TabDir,[H1:_]),
 1997  % write('headlist: '), writeln(HeadList),
 1998  % write('h1: '), writeln(H1),
 1999  add_bdd_arg(H1,Env,BDD,M,Head1),%***test single_var
 2000  % write('head1: '), writeln(Head1),
 2001  % write('vc: '), writeln(VC),
 2002  (M:local_pita_setting(single_var,true)->
 2003    VC1 = []
 2004  ;
 2005    VC1 = VC
 2006  ),
 2007  Clauses0=[(Head1:-(get_var_n(M,Env,R,VC1,Probs,V),equalityc(Env,V,0,BDD)))],
 2008  append(TabDir,Clauses0,Clauses).
 2009
 2010pita_expansion((:- set_sw(A,B)), []) :-!,
 2011  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 2012  set_sw(M:A,B).
 2013
 2014
 2015pita_expansion(Head, Clauses) :-
 2016  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 2017  M:local_pita_setting(depth_bound,true),
 2018% definite fact with db
 2019  (Head \= ((pita_expansion(_,_) ):- _ )),
 2020  (Head\= end_of_file),!,
 2021  to_table(M,[Head:_],TabDir,[Head1:_]),
 2022  add_bdd_arg_db(Head1,Env,One,_DB,M,Head2),
 2023  append(TabDir,[(Head2:-onec(Env,One))],Clauses).
 2024
 2025pita_expansion(Head, Clauses) :-
 2026  prolog_load_context(module, M),pita_input_mod(M),M:pita_on,
 2027% definite fact without db
 2028  (Head \= ((pita_expansion(_,_) ):- _ )),
 2029  (Head\= end_of_file),
 2030  to_table(M,[Head:_],TabDir,[Head1:_]),
 2031  add_bdd_arg(Head1,Env,One,M,Head2),
 2032  append(TabDir,[(Head2:-onec(Env,One))],Clauses).
 begin_lpad_pred is det
Initializes LPAD loading. /
 2040begin_lpad_pred:-
 2041  assert(pita_input_mod(user)),
 2042  assert(user:pita_on).
 end_lpad_pred is det
Terminates the cplint inference module. /
 2049end_lpad_pred:-
 2050  retractall(pita_input_mod(_)),
 2051  retractall(user:pita_on).
 2052
 2053list2or([],true):-!.
 2054
 2055list2or([X],X):-
 2056    X\=;(_,_),!.
 2057
 2058list2or([H|T],(H ; Ta)):-!,
 2059    list2or(T,Ta).
 2060
 2061
 2062list2and([],true):-!.
 2063
 2064list2and([X],X):-
 2065    X\=(_,_),!.
 2066
 2067list2and([H|T],(H,Ta)):-!,
 2068    list2and(T,Ta).
 2069
 2070transform(H,H1):-
 2071  H=..[prob|Args],
 2072  H1=..[prob_meta|Args].
 2073
 2074builtin(average(_L,_Av)) :- !.
 2075builtin(G) :-
 2076  swi_builtin(G).
 2077
 2078
 2079:- multifile sandbox:safe_meta/2. 2080
 2081sandbox:safe_meta(pita:s(_,_), []).
 2082sandbox:safe_meta(pita:prob(_,_), []).
 2083sandbox:safe_meta(pita:prob(_,_,_), []).
 2084sandbox:safe_meta(pita:prob(_,_,_,_), []).
 2085sandbox:safe_meta(pita:prob_meta(_,_), []).
 2086sandbox:safe_meta(pita:prob_meta(_,_,_), []).
 2087sandbox:safe_meta(pita:abd_prob(_,_,_), []).
 2088sandbox:safe_meta(pita:bdd_dot_file(_,_,_), []).
 2089sandbox:safe_meta(pita:bdd_dot_string(_,_,_), []).
 2090sandbox:safe_meta(pita:abd_bdd_dot_string(_,_,_,_), []).
 2091sandbox:safe_meta(pita:abd_bdd_dot_string(_,_,_,_,_,_), []).
 2092sandbox:safe_meta(pita:map(_,_,_), []).
 2093sandbox:safe_meta(pita:map_bdd_dot_string(_,_,_,_,_,_), []).
 2094sandbox:safe_meta(pita:msw(_,_,_,_), []).
 2095sandbox:safe_meta(pita:msw(_,_,_,_,_), []).
 2096sandbox:safe_meta(pita:set_pita(_,_),[]).
 2097sandbox:safe_meta(pita:setting_pita(_,_),[]).
 2098sandbox:safe_meta(pita:dt_solve(_,_),[]).
 2099
 2100
 2101
 2102
 2103:- license(artisticv2). 2104
 2105:- thread_local pita_file/1. 2106
 2107user:term_expansion(:-pita, Clauses) :-!,
 2108  prolog_load_context(source, Source),
 2109  asserta(pita_file(Source)),
 2110  prolog_load_context(module, M),
 2111  retractall(M:local_pita_setting(_,_)),
 2112  findall(local_pita_setting(P,V),default_setting_pita(P,V),L),
 2113  assert_all(L,M,_),
 2114  assert(pita_input_mod(M)),
 2115  retractall(M:rule_n(_)),
 2116  retractall(M:goal_n(_)),
 2117  assert(M:rule_n(0)),
 2118  assert(M:goal_n(0)),
 2119  M:(dynamic v/3, av/3, query_rule/4, rule_by_num/4, dec/3,
 2120    zero_clauses/1, pita_on/0, tabled/1, '$cons'/2),
 2121  retractall(M:query_rule(_,_,_,_)),
 2122  style_check(-discontiguous),
 2123  process_body([\+ '$cons'],BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
 2124  append([onec(Env,BDD)],BodyList2,BodyList3),
 2125  list2and(BodyList3,Body2),
 2126  to_table(M,['$constraints':_],TabDir,[Head1:_]),
 2127  to_table(M,['$cons':_],TabDirCons,_),
 2128  add_bdd_arg(Head1,Env,BDDAnd,M,Head2),
 2129  append([TabDir,TabDirCons,[(Head2 :- Body2)]],Clauses).
 2130
 2131user:term_expansion(end_of_file, C) :-
 2132  pita_file(Source),
 2133  prolog_load_context(source, Source),
 2134  retractall(pita_file(Source)),
 2135  prolog_load_context(module, M),
 2136  pita_input_mod(M),!,
 2137  retractall(pita_input_mod(M)),
 2138  findall(LZ,M:zero_clauses(LZ),L),
 2139  retractall(M:zero_clauses(_)),
 2140  retractall(M:tabled(_)),
 2141  append(L,[(:- style_check(+discontiguous)),end_of_file],C).
 2142
 2143user:term_expansion(In, Out) :-
 2144   \+ current_prolog_flag(xref, true),
 2145   pita_file(Source),
 2146   prolog_load_context(source, Source),
 2147   pita_expansion(In, Out)