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,
   17  dt_solve/2,
   18  % op(600,fx,'?'),
   19  op(600,xfy,'::'),
   20  op(500,fx,'?::'),
   21  op(600,xfx,'=>'),
   22  op(1150,fx,action),
   23  op(1200,fy,map_query),
   24  op(1200,fy,abducible),
   25  msw/4,
   26  msw/5
   27    ]).

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
   43:- reexport(library(cplint_util)).   44:- reexport(library(bddem)).   45
   46% :- prolog_debug(chk_secure).
   47
   48:-meta_predicate abd_prob(:,-,-).   49:-meta_predicate prob(:,-).   50:-meta_predicate prob(:,:,-).   51:-meta_predicate prob(:,:,-,+).   52:-meta_predicate prob_meta(:,-).   53:-meta_predicate prob_meta(:,:,+).   54:-meta_predicate bdd_dot_file(:,+,-).   55:-meta_predicate bdd_dot_string(:,-,-).   56:-meta_predicate abd_bdd_dot_string(:,-,-,-).   57:-meta_predicate abd_bdd_dot_string(:,-,-,-,-,-).   58:-meta_predicate map(:,-,-).   59:-meta_predicate map_bdd_dot_string(:,-,-,-,-,-).   60:-meta_predicate msw(:,-,-,-).   61:-meta_predicate msw(:,-,-,-,-).   62:-meta_predicate get_p(:,+,-).   63:-meta_predicate get_cond_p(:,:,+,-).   64:-meta_predicate get_node(:,+,-).   65:-meta_predicate get_cond_node(:,:,+,-,-).   66:-meta_predicate set_pita(:,+).   67:-meta_predicate setting_pita(:,-).   68:-meta_predicate set_sw(:,+).   69:-meta_predicate dt_solve(:,-).   70
   71% :- dynamic utility/2.
   72
   73:-use_module(library(lists)).   74:-use_module(library(apply)).   75:-use_module(library(assoc)).   76
   77:- style_check(-discontiguous).   78
   79:- thread_local rule_n/1,goal_n/1,pita_input_mod/1,local_pita_setting/2.   80
   81
   82
   83
   84default_setting_pita(epsilon_parsing, 1e-5).
   85/* on, off */
   86
   87default_setting_pita(bagof,false).
   88/* values: false, intermediate, all, extra */
   89
   90default_setting_pita(compiling,off).
   91
   92:-set_prolog_flag(unknown,warning).   93
   94default_setting_pita(depth_bound,false).  %if true, it limits the derivation of the example to the value of 'depth'
   95default_setting_pita(depth,5).
   96default_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.
   97
   98default_setting_pita(tabling,auto).
   99/* values:
  100  auto
  101  explicit
  102*/
  103default_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. /
  109load(File):-
  110  must_be(atom,File),
  111  atomic_concat(File,'.lpad',FileLPAD),
  112  (exists_file(FileLPAD)->
  113    load_file(FileLPAD)
  114  ;
  115    atomic_concat(File,'.cpl',FileCPL),
  116    (exists_file(FileCPL)->
  117      load_file(FileCPL)
  118    )
  119  ).
 load_file(++FileWithExtension:atom) is det
Loads FileWithExtension. /
  129load_file(File):-
  130  must_be(atom,File),
  131  begin_lpad_pred,
  132  user:consult(File),
  133  end_lpad_pred.
 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. /
  143dt_solve(M:Strategy,Cost):-
  144  must_be(var,Strategy),
  145  must_be(var,Cost),
  146  abolish_all_tables,
  147  findall([H,U],M:'$util'(H,U),LUtils),
  148  init(Env),
  149  % statistics(walltime,[Start|_]),
  150  generate_solution(Env,M,LUtils,[],St,Cost),
  151  % statistics(walltime,[Stop|_]),
  152  end(Env),
  153  % Runtime is Stop - Start,
  154  % format('Runtime: ~w~n',[Runtime]),
  155  maplist(pair(M),St,Strategy).
  156
  157pair(M,A,B):- M:rule_by_num(A,B,_,_).
  158split([A,B],A,B).
  159
  160get_bdd(_,_,[],L,L):- !.
  161get_bdd(M,Env,[G|T],L,LO):-
  162  get_node(M:G,Env,Out),
  163  % writeln(Out),
  164  Out=(_,BDD),
  165  append(L,[BDD],LT),
  166  get_bdd(M,Env,T,LT,LO).
  167
  168% compute the solution for dt problem
  169% generate_solution/6
  170% generate_solution(Env,M,GoalCostList,CurrentAdd,Solution,Cost)
  171% output Solution, Cost
  172generate_solution(Env,_,[],Add,Solution,Cost):- !,
  173  % create_dot(Env,Add,"final.dot"),
  174  ret_strategy(Env,Add,Solution,Cost).
  175
  176generate_solution(Env,M,[[G,Cost]|TC],CurrentAdd,Solution,OptCost):-
  177  get_node(M:G,Env,Out),
  178  Out=(_,BDD),
  179  probability_dd(Env,BDD,AddConv),
  180  add_prod(Env,AddConv,Cost,AddScaled),
  181  (CurrentAdd = [] ->
  182    AddOut = AddScaled ;
  183    % writeln(CurrentAdd),
  184    add_sum(Env,CurrentAdd,AddScaled,AddOut)
  185    % writeln("sum"),
  186  ),
  187  generate_solution(Env,M,TC,AddOut,Solution,OptCost).
 dt_evaluate_strategy(+Strategy:List, -Cost:float) is det
Computes the cost of the selected strategy /
  194% TODO
  195% 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) /
  202prob_meta(M:Goal,P):-
  203  must_be(nonvar,Goal),
  204  must_be(var,P),
  205  term_variables(Goal,VG),
  206  get_next_goal_number(M,GN),
  207  atomic_concat('$goal',GN,NewGoal),
  208  Goal1=..[NewGoal|VG],
  209  list2and(GoalL,Goal),
  210  ( M:local_pita_setting(depth_bound,true) *->
  211      ( process_body_db(GoalL,BDD,BDDAnd,DB,[],_Vars,BodyList2,Env,M),
  212        add_bdd_arg_db(Goal1,Env,BDDAnd,DB,M,Head1)
  213      )
  214    ;
  215      ( process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  216        add_bdd_arg(Goal1,Env,BDDAnd,M,Head1)
  217      )
  218  ),
  219  append([onec(Env,BDD)],BodyList2,BodyList3),
  220  list2and(BodyList3,Body2),
  221  M:(asserta((Head1 :- Body2),Ref)),
  222  init(Env),
  223  findall((Goal,P),get_p(M:Goal1,Env,P),L),
  224  end(Env),
  225  erase(Ref),
  226  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 /
  234abd_prob(M:Goal,P,Delta):-
  235  must_be(nonvar,Goal),
  236  must_be(var,P),
  237  must_be(var,Delta),
  238  abolish_all_tables,
  239  term_variables(Goal,VG),
  240  get_next_goal_number(M,GN),
  241  atomic_concat('$goal',GN,NewGoal),
  242  Goal1=..[NewGoal|VG],
  243  list2and(GoalL,Goal),
  244  process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  245  append([onec(Env,BDD)],BodyList2,BodyList3),
  246  list2and(BodyList3,Body2),
  247  add_bdd_arg(Goal1,Env,BDDAnd,M,Head1),
  248  M:(asserta((Head1 :- Body2),Ref)),
  249  init(Env),
  250  findall((Goal,P,Exp),get_abd_p(M:Goal1,M:'$constraints',Env,P,Exp),L),
  251  end(Env),
  252  erase(Ref),
  253  member((Goal,P,Exp),L),
  254  maplist(from_assign_to_exp(M),Exp,DeltaAll),
  255  simplify_delta(DeltaAll,Delta).
  256
  257subset_([], []).
  258subset_([E|Tail], [E|NTail]):-
  259  subset_(Tail, NTail).
  260subset_([_|Tail], NTail):-
  261  subset_(Tail, NTail).
  262
  263mycompare(<,L1,L2) :- length(L1,A1), length(L2,A2), A1 < A2.
  264mycompare(>, _, _).
  265
  266sub([A],[A]).
  267sub([H|T],[V|R]):-
  268  findall(X,(subset_(H,X), member(X,T)),LX),
  269  (   LX = [] ->
  270    V = H,
  271    sub(T,R) ;
  272    sub(T,[V|R]) 
  273  ).
  274
  275simplify_delta([],[]):- !.
  276simplify_delta(Din,Delta):-
  277  predsort(mycompare,Din,LS),
  278  reverse(LS,LNR),
  279  sub(LNR,Delta), !.
  280
  281
  282
  283from_assign_to_exp(_M,[],[]):- !.
  284from_assign_to_exp(M,[Var-Val|TA],[Abd|TDelta]):-
  285  M:av(R,S,Var),
  286  M:abd(R,S,H),
  287  (Val=1->
  288    Abd=H
  289  ;
  290    Abd= \+(H)
  291    % Abd= []
  292  ),
  293  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. /
  305bdd_dot_file(M:Goal,File,LV):-
  306  must_be(nonvar,Goal),
  307  must_be(string,File),
  308  must_be(var,LV),
  309  abolish_all_tables,
  310  init(Env),
  311  get_node(M:Goal,Env,Out),
  312  Out=(_,BDD),!,
  313  findall([V,R,S],M:v(R,S,V),LV),
  314  create_dot(Env,BDD,File),
  315  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. /
  326bdd_dot_string(M:Goal,DotString,LV):-
  327  must_be(nonvar,Goal),
  328  must_be(var,DotString),
  329  must_be(var,LV),
  330  DotString=dot(Dot),
  331  abolish_all_tables,
  332  init(Env),
  333  get_node(M:Goal,Env,Out),
  334  Out=(_,BDD),!,
  335  findall([V,R,S],M:v(R,S,V),LV),
  336  create_dot_string(Env,BDD,Dot),
  337  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. /
  350abd_bdd_dot_string(M:Goal,DotString,LV,LAV):-
  351  must_be(nonvar,Goal),
  352  must_be(var,DotString),
  353  must_be(var,LV),
  354  must_be(var,LAV),
  355  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. /
  369abd_bdd_dot_string(M:Goal,DotString,LV,LAV,P,Delta):-
  370  must_be(nonvar,Goal),
  371  must_be(var,DotString),
  372  must_be(var,LV),
  373  must_be(var,LAV),
  374  must_be(var,P),
  375  must_be(var,Delta),
  376  DotString=dot(Dot),
  377  abolish_all_tables,
  378  init(Env),
  379  get_cond_node(M:Goal,M:'$constraints',Env,Out,_),
  380  Out=(_,BDD), !,
  381  ret_abd_prob(Env,BDD,P,Exp),
  382  create_dot_string(Env,BDD,Dot),
  383  end(Env),
  384  maplist(from_assign_to_exp(M),Exp,Delta),
  385  findall([V,R,S],M:v(R,S,V),LV),
  386  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 /
  395map(M:Goal,P,MAP):-
  396  must_be(nonvar,Goal),
  397  must_be(var,P),
  398  must_be(var,MAP),
  399  map_int(Goal,M,_LV,_LAV,P,MAP,Env,_BDD),
  400  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. /
  415map_bdd_dot_string(M:Goal,DotString,LV,LAV,P,MAP):-
  416  must_be(nonvar,Goal),
  417  must_be(var,DotString),
  418  must_be(var,LV),
  419  must_be(var,LAV),
  420  must_be(var,P),
  421  must_be(var,MAP),
  422  DotString=dot(Dot),
  423  map_int(Goal,M,LV,LAV,P,MAP,Env,BDD),
  424  create_dot_string(Env,BDD,Dot),
  425  end(Env).
  426
  427
  428map_int(Goal,M,LV,LAV,P,MAP,Env,BDD):-
  429  abolish_all_tables,
  430  init(Env),
  431  get_node(M:Goal,Env,Out),
  432  (Out=(_,BDD0)),!,
  433  findall([V,R,S],M:v(R,S,V),LV),
  434  one(Env,One),
  435  make_query_vars(LV,M,Env,One,Cons,LAV),
  436  and(Env,BDD0,Cons,BDD),
  437  ret_map_prob(Env,BDD,P,Exp0),
  438  reverse(Exp0,Exp),
  439  from_assign_to_map(Exp,M,MAP).
  440
  441
  442make_query_vars([],_M,_Env,C,C,[]).
  443
  444make_query_vars([[V,R,S]|T],M,Env,Cons0,Cons,[[V,R,S]|TV]):-
  445  M:query_rule(R,_,_,_),!,
  446  make_query_var(Env,V,B),
  447  and(Env,Cons0,B,Cons1),
  448  make_query_vars(T,M,Env,Cons1,Cons,TV).
  449
  450make_query_vars([_H|T],M,Env,Cons0,Cons,LV):-
  451  make_query_vars(T,M,Env,Cons0,Cons,LV).
  452
  453from_assign_to_map([],_M,[]).
  454
  455from_assign_to_map([Var-Val|TA],M,[rule(R,Head,HeadList,Body)|TDelta]):-
  456  M:v(R,S,Var),
  457  M:query_rule(R,HeadList,Body,S),
  458  nth1(Val,HeadList,Head:_),
  459  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 /
  469prob(M:Goal,P):-
  470  must_be(nonvar,Goal),
  471  abolish_all_tables,
  472  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. /
  480prob(M:Goal,M:Evidence,P):-
  481  must_be(nonvar,Goal),
  482  must_be(nonvar,Evidence),
  483  must_be(var,P),
  484  abolish_all_tables,
  485  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) /
  492prob_meta(M:Goal,M:Evidence,P):-
  493  must_be(nonvar,Goal),
  494  must_be(nonvar,Evidence),
  495  must_be(var,P),
  496  get_next_goal_number(M,GN),
  497  atomic_concat('$ev',GN,NewEv),
  498  deal_with_ev(Evidence,M,NewEv,EvNoAct,UpdatedClausesRefs,ClausesToReAdd),
  499  term_variables(Goal,VG),
  500  atomic_concat('$goal',GN,NewGoal),
  501  Goal1=..[NewGoal|VG],
  502  list2and(GoalL,Goal),
  503  process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  504  append([onec(Env,BDD)],BodyList2,BodyList3),
  505  list2and(BodyList3,Body2),
  506  add_bdd_arg(Goal1,Env,BDDAnd,M,Head1),
  507  M:(asserta((Head1 :- Body2),Ref)),
  508  init(Env),
  509  (EvNoAct=true->
  510    findall((Goal,P),get_p(M:Goal1,Env,P),L)
  511  ;
  512    findall((Goal,P),get_cond_p(M:Goal1,M:EvNoAct,Env,P),L)
  513  ),
  514  end(Env),
  515  retractall(M:NewEv),
  516  maplist(erase,UpdatedClausesRefs),
  517  erase(Ref),
  518  maplist(M:assertz,ClausesToReAdd),
  519  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. /
  535prob(M:Goal,M:Evidence,P,Options):-
  536  must_be(nonvar,Goal),
  537  must_be(nonvar,Evidence),
  538  must_be(var,P),
  539  must_be(nonvar,Options),
  540  prob(M:Goal,M:Evidence,P),
  541  option(bar(Chart),Options,no),
  542  (nonvar(Chart)->
  543    true
  544  ;
  545    bar(P,Chart)
  546  ).
  547
  548
  549deal_with_ev(Ev,M,NewEv,EvGoal,UC,CA):-
  550  list2and(EvL,Ev),
  551  partition(ac,EvL,ActL,EvNoActL),
  552  deal_with_actions(ActL,M,UC0,CA),
  553  (EvNoActL=[]->
  554    EvGoal=true,
  555    UC=UC0
  556  ;
  557    process_body(EvNoActL,BDD,BDDAnd,[],_Vars,BodyList2,Env,M),
  558    append([onec(Env,BDD)],BodyList2,BodyList3),
  559    list2and(BodyList3,Body2),
  560    add_bdd_arg(NewEv,Env,BDDAnd,M,Head1),
  561    M:(asserta((Head1 :- Body2),Ref)),
  562    UC=[Ref|UC0],
  563    EvGoal=NewEv
  564  ).
  565
  566deal_with_actions(ActL,M,UC,CA):-
  567  empty_assoc(AP0),
  568  foldl(get_pred_const,ActL,AP0,AP),
  569  assoc_to_list(AP,LP),
  570  maplist(update_clauses(M),LP,UCL,CAL),
  571  partition(nac,ActL,_NActL,PActL),
  572  maplist(assert_actions(M),PActL,ActRefs),
  573  append([ActRefs|UCL],UC),
  574  append(CAL,CA).
  575
  576zero_clauses_actions(M,do(\+ A),Ref):-
  577  A=..[P|Args],
  578  append(Args,[Env,BDD],Args1),
  579  A1=..[P|Args1],
  580  M:assertz((A1:-zeroc(Env,BDD)),Ref).
  581
  582assert_actions(M,do(A),Ref):-
  583  A=..[P|Args],
  584  append(Args,[Env,BDD],Args1),
  585  A1=..[P|Args1],
  586  M:assertz((A1:-onec(Env,BDD)),Ref).
  587
  588update_clauses(M,P/0- _,[RefZ],[(H:-zeroc(Env,BDD))|LCA]):-!,
  589  functor(G1,P,2),
  590  findall(Ref,M:clause(G1,_B,Ref),UC),
  591  findall((G1:-B),M:clause(G1,B),LCA),
  592  H=..[P,Env,BDD],
  593  maplist(erase,UC),
  594  M:assertz((H:-zeroc(Env,BDD)),RefZ).
  595
  596update_clauses(M,P/A-Constants,UC,CA):-
  597  functor(G,P,A),
  598  A1 is A+2,
  599  functor(G1,P,A1),
  600  G=..[_|Args],
  601  G1=..[_|Args1],
  602  append(Args,[_,_],Args1),
  603  findall((G1,B,Ref),M:clause(G1,B,Ref),LC),
  604  maplist(get_const(Args),Constants,ConstraintsL),
  605  list2and(ConstraintsL,Constraints),
  606  maplist(add_cons(G1,Constraints,M),LC,UC,CA).
  607
  608add_cons(_G,_C,M,(H,zeroc(Env,Zero),Ref),Ref1,(H:-zeroc(Env,Zero))):-!,
  609  erase(Ref),
  610  M:assertz((H:-zeroc(Env,Zero)),Ref1).
  611
  612add_cons(G,C,M,(H,B,Ref),Ref1,(H:-B)):-
  613  copy_term((G,C),(G1,C1)),
  614  G1=H,
  615  erase(Ref),
  616  M:assertz((H:-(C1,B)),Ref1).
  617
  618
  619get_const(Args,Constants,Constraint):-
  620  maplist(constr,Args,Constants,ConstraintL),
  621  list2and(ConstraintL,Constraint).
  622
  623constr(V,C,dif(V,C)).
  624
  625get_pred_const(do(Do0),AP0,AP):-
  626  (Do0= (\+ Do)->
  627    true
  628  ;
  629    Do=Do0
  630  ),
  631  functor(Do,F,A),
  632  Do=..[_|Args],
  633  (get_assoc(F/A,AP0,V)->
  634    put_assoc(F/A,AP0,[Args|V],AP)
  635  ;
  636    put_assoc(F/A,AP0,[Args],AP)
  637  ).
  638
  639
  640ac(do(_)).
  641nac(do(\+ _)).
  642
  643
  644get_p(M:Goal,Env,P):-
  645  get_node(M:Goal,Env,BDD),
  646  ret_probc(Env,BDD,P).
  647
  648
  649get_abd_p(M:Goal,M:Evidence,Env,P,Exp):-
  650  % get_node_no_rec(M:Evidence,Env,OutIC),
  651  get_cond_node(M:Goal,M:Evidence,Env,Out,_),
  652  % get_node(M:Evidence,Env,OutIC),
  653  Out=(_,BDD),
  654  % OutIC = (_,BDDIC),
  655  ret_abd_prob(Env,BDD,P,Exp).
  656
  657get_cond_p(M:Goal,M:Evidence,Env,P):-
  658  get_cond_node(M:Goal,M:Evidence,Env,BDDGE,BDDE),
  659  ret_probc(Env,BDDE,PE),
  660  ret_probc(Env,BDDGE,PGE),
  661  ( PE =:= 0 -> 
  662      writeln("Undefined: probability of evidence 0.") ;
  663      P is PGE/PE
  664  ).
  665
  666
  667get_node(M:Goal,Env,BDD):-
  668  M:local_pita_setting(depth_bound,true),!,
  669  M:local_pita_setting(depth,DB),
  670  retractall(M:v(_,_,_)),
  671  retractall(M:av(_,_,_)),
  672  retractall(M:dec(_,_,_)),
  673  add_bdd_arg_db(Goal,Env,BDD,DB,M,Goal1),%DB=depth bound
  674  (M:Goal1*->
  675    true
  676  ;
  677    zeroc(Env,BDD)
  678  ).
  679
  680get_node(M:Goal,Env,BDD):- %with DB=false
  681  retractall(M:v(_,_,_)),
  682  retractall(M:av(_,_,_)),
  683  retractall(M:dec(_,_,_)),
  684  add_bdd_arg(Goal,Env,BDD,M,Goal1),
  685  (M:Goal1*->
  686    true
  687  ;
  688    zeroc(Env,BDD)
  689    % format("-------------------------Failed goal: ~w ~n",[M:Goal])
  690  ).
  691
  692  get_node_no_rec(M:Goal,Env,BDD):- %with DB=false
  693  retractall(M:v(_,_,_)),
  694  retractall(M:v(_,_,_)),
  695  retractall(M:av(_,_,_)),
  696  add_bdd_arg(Goal,Env,BDD,M,Goal1),
  697  (M:Goal1*->
  698    true
  699  ;
  700    zeroc(Env,BDD)
  701    % format("-------------------------Failed goal: ~w ~n",[M:Goal])
  702  ).
  703
  704get_cond_node(M:Goal,M:Ev,Env,BGE,BDDE):-
  705  M:local_pita_setting(depth_bound,true),!,
  706  M:local_pita_setting(depth,DB),
  707  retractall(M:v(_,_,_)),
  708  retractall(M:av(_,_,_)),
  709  retractall(M:dec(_,_,_)),
  710  add_bdd_arg_db(Goal,Env,BDD,DB,M,Goal1),%DB=depth bound
  711  (M:Goal1*->
  712    true
  713  ;
  714    zeroc(Env,BDD)
  715  ),
  716  add_bdd_arg_db(Ev,Env,BDDE,DB,M,Ev1),%DB=depth bound
  717  (M:Ev1*->
  718    true
  719  ;
  720    zeroc(Env,BDDE)
  721  ),
  722  andcnf(Env,BDD,BDDE,BGE).
  723
  724
  725
  726get_cond_node(M:Goal,M:Ev,Env,BGE,BDDE):- %with DB=false
  727  retractall(M:v(_,_,_)),
  728  retractall(M:av(_,_,_)),
  729  retractall(M:dec(_,_,_)),
  730  add_bdd_arg(Goal,Env,BDD,M,Goal1),
  731  (M:Goal1*->
  732    true
  733  ;
  734    zeroc(Env,BDD)
  735  ),
  736  add_bdd_arg(Ev,Env,BDDE,M,Ev1),
  737  (M:Ev1*->
  738    true
  739  ;
  740    zeroc(Env,BDDE)
  741  ),
  742  andcnf(Env,BDD,BDDE,BGE).
  743
  744
  745get_next_goal_number(PName,R):-
  746  retract(PName:goal_n(R)),
  747  R1 is R+1,
  748  assert(PName:goal_n(R1)).
  749
  750
  751get_next_rule_number(PName,R):-
  752  retract(PName:rule_n(R)),
  753  R1 is R+1,
  754  assert(PName:rule_n(R1)).
  755
  756
  757assert_all([],_M,[]).
  758
  759assert_all([H|T],M,[HRef|TRef]):-
  760  assertz(M:H,HRef),
  761  assert_all(T,M,TRef).
  762
  763
  764retract_all([]):-!.
  765
  766retract_all([H|T]):-
  767  erase(H),
  768  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. /
  777get_var_n(M,Env,R,S,Probs0,V):-
  778  M:query_rule(R,_H,_B,_S),!,
  779  (ground(Probs0)->
  780    maplist(is,Probs,Probs0),
  781    (M:v(R,S,V)->
  782      true
  783    ;
  784      add_query_var(Env,Probs,R,V),
  785      assert(M:v(R,S,V))
  786    )
  787  ;
  788    throw(error('Non ground probabilities not instantiated by the body'))
  789  ).
  790
  791get_var_n(M,Env,R,S,Probs0,V):-
  792  (ground(Probs0)->
  793    maplist(is,Probs,Probs0),
  794    (M:v(R,S,V)->
  795      true
  796    ;
  797      % format("P: ~w ~w ~n",[Probs,R]),
  798      add_var(Env,Probs,R,V),
  799      assert(M:v(R,S,V))
  800    )
  801  ;
  802    throw(error('Non ground probabilities not instantiated by the body'))
  803  ).
 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. /
  811 get_dec_var_n(M,Env,R,S,V):-
  812  % format('get_dec_var: R: ~w - S: ~w - V: ~w - M: ~w ~n', [R,S,V,M]),
  813  ( M:dec(R,S,V) ->
  814  % findall([A,B,C],M:dec(A,B,C),LD),
  815    % writeln(LD),
  816    true ;
  817    add_decision_var(Env,R,V),
  818    % writeln("New dec var"),
  819    asserta(M:dec(R,S,V))
  820  ).
  821  % (M:v(R,S,V)->
  822  %     true
  823  %   ;
  824  %     % add_var(Env,1,R,V),
  825  %     assert(M:v(R,S,V))
  826  % ).
 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. /
  835get_abd_var_n(M,Env,R,S,Probs0,V):-
  836  (ground(Probs0)->
  837    maplist(is,Probs,Probs0),
  838    (M:av(R,S,V)->
  839      true
  840    ;
  841      add_abd_var(Env,Probs,R,V),
  842      assert(M:av(R,S,V))
  843    )
  844  ;
  845    throw(error('Non ground probabilities not instantiated by the body'))
  846  ).
 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 /
  854msw(M:A,B,Env,BDD):-
  855  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 /
  864msw(M:A,B,Env,BDD,_DB):-
  865  msw_int(M,A,B,Env,BDD).
  866
  867msw_int(M,A,B,Env,BDD):-
  868  M:values(A,Values),
  869  M:sw(R,A,Probs0),
  870  (ground(Probs0)->
  871    maplist(is,Probs,Probs0),
  872    ((M:local_pita_setting(prism_memoization,true),M:v(R,A,V))->
  873      true
  874    ;
  875      add_var(Env,Probs,R,V)
  876    ),
  877    (M:local_pita_setting(prism_memoization,true)->
  878      assert(M:v(R,A,V))
  879    ;
  880      true
  881    ),
  882    nth0(N,Values,B),
  883    equalityc(Env,V,N,BDD)
  884  ;
  885    throw(error('Non ground probabilities not instantiated by the body'))
  886  ).
  887
  888
  889combine(V,P,V:P).
  890
  891add_bdd_arg(M:A,Env,BDD,M:A1):-
  892  A=..[P|Args],
  893  append(Args,[Env,BDD],Args1),
  894  A1=..[P|Args1].
  895
  896
  897add_bdd_arg_db(M:A,Env,BDD,DB,M:A1):-
  898  A=..[P|Args],
  899  append(Args,[Env,DB,BDD],Args1),
  900  A1=..[P|Args1].
  901
  902
  903add_bdd_arg(A,Env,BDD,_Module,A1):-
  904  A=..[P|Args],
  905  append(Args,[Env,BDD],Args1),
  906  A1=..[P|Args1].
  907
  908
  909add_bdd_arg_db(A,Env,BDD,DB,_Module,A1):-
  910  A=..[P|Args],
  911  append(Args,[Env,DB,BDD],Args1),
  912  A1=..[P|Args1].
  913
  914add_mod_arg(A,_Module,A1):-
  915  A=..[P|Args],
  916  A1=..[P|Args].
  917
  918
  919generate_rules_fact([],_Env,_VC,_R,_Probs,_N,[],_Module).
  920
  921generate_rules_fact([Head:_P1,'':_P2],Env,VC,R,Probs,N,[Clause],Module):-!,
  922  add_bdd_arg(Head,Env,BDD,Module,Head1),
  923  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))).
  924
  925generate_rules_fact([Head:_P|T],Env,VC,R,Probs,N,[Clause|Clauses],Module):-
  926  add_bdd_arg(Head,Env,BDD,Module,Head1),
  927  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))),
  928  N1 is N+1,
  929  generate_rules_fact(T,Env,VC,R,Probs,N1,Clauses,Module).
  930
  931
  932generate_rules_fact_vars([],_Env,_R,_Probs,_N,[],_Module).
  933
  934generate_rules_fact_vars([Head:_P1,'':_P2],Env,R,Probs,N,[Clause],Module):-!,
  935  term_variables([Head],VC),
  936  add_bdd_arg(Head,Env,BDD,Module,Head1),
  937  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))).
  938
  939generate_rules_fact_vars([Head:_P|T],Env,R,Probs,N,[Clause|Clauses],Module):-
  940  term_variables([Head],VC),
  941  add_bdd_arg(Head,Env,BDD,Module,Head1),
  942  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))),
  943  N1 is N+1,
  944  generate_rules_fact_vars(T,Env,R,Probs,N1,Clauses,Module).
  945
  946
  947generate_rules_fact_db([],_Env,_VC,_R,_Probs,_N,[],_Module).
  948
  949generate_rules_fact_db([Head:_P1,'':_P2],Env,VC,R,Probs,N,[Clause],Module):-!,
  950  add_bdd_arg_db(Head,Env,BDD,_DB,Module,Head1),
  951  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))).
  952
  953generate_rules_fact_db([Head:_P|T],Env,VC,R,Probs,N,[Clause|Clauses],Module):-
  954  add_bdd_arg_db(Head,Env,BDD,_DB,Module,Head1),
  955  Clause=(Head1:-(get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,BDD))),
  956  N1 is N+1,
  957  generate_rules_fact_db(T,Env,VC,R,Probs,N1,Clauses,Module).
  958
  959
  960generate_clause(Head,Env,Body,VC,R,Probs,BDDAnd,N,Clause,Module):-
  961  add_bdd_arg(Head,Env,BDD,Module,Head1),
  962  Clause=(Head1:-(Body,get_var_n(Module,Env,R,VC,Probs,V),equalityc(Env,V,N,B),andc(Env,BDDAnd,B,BDD))).
  963
  964
  965generate_clause_db(Head,Env,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module):-
  966  add_bdd_arg_db(Head,Env,BDD,DBH,Module,Head1),
  967  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))).
  968
  969
  970generate_rules([],_Env,_Body,_VC,_R,_Probs,_BDDAnd,_N,[],_Module).
  971
  972generate_rules([Head:_P1,'':_P2],Env,Body,VC,R,Probs,BDDAnd,N,[Clause],Module):-!,
  973  generate_clause(Head,Env,Body,VC,R,Probs,BDDAnd,N,Clause,Module).
  974
  975generate_rules([Head:_P|T],Env,Body,VC,R,Probs,BDDAnd,N,[Clause|Clauses],Module):-
  976  generate_clause(Head,Env,Body,VC,R,Probs,BDDAnd,N,Clause,Module),
  977  N1 is N+1,
  978  generate_rules(T,Env,Body,VC,R,Probs,BDDAnd,N1,Clauses,Module).
  979
  980
  981generate_rules_db([],_Env,_Body,_VC,_R,_Probs,_DB,_BDDAnd,_N,[],_Module):-!.
  982
  983generate_rules_db([Head:_P1,'':_P2],Env,Body,VC,R,Probs,DB,BDDAnd,N,[Clause],Module):-!,
  984  generate_clause_db(Head,Env,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module).
  985
  986generate_rules_db([Head:_P|T],Env,Body,VC,R,Probs,DB,BDDAnd,N,[Clause|Clauses],Module):-
  987  generate_clause_db(Head,Env,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module),!,%agg.cut
  988  N1 is N+1,
  989  generate_rules_db(T,Env,Body,VC,R,Probs,DB,BDDAnd,N1,Clauses,Module).
  990
  991
  992
  993process_body([],BDD,BDD,Vars,Vars,[],_Env,_Module).
  994
  995process_body([\+ H|T],BDD,BDD1,Vars,Vars1,[\+ H|Rest],Env,Module):-
  996  builtin(H),!,
  997  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
  998
  999process_body([\+ db(H)|T],BDD,BDD1,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1000  !,
 1001  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1002
 1003process_body([\+ H|T],BDD,BDD1,Vars,[BDDH,BDDN,BDD2|Vars1],
 1004[H1,bdd_notc(Env,BDDH,BDDN),
 1005  andc(Env,BDD,BDDN,BDD2)|Rest],Env,Module):-!,
 1006  add_bdd_arg(H,Env,BDDH,Module,H1),
 1007  process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Env,Module).
 1008
 1009process_body([H|T],BDD,BDD1,Vars,Vars1,[H1|Rest],Env,Module):-
 1010  transform(H,H1),!,
 1011  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1012
 1013process_body([H|T],BDD,BDD1,Vars,Vars1,[H|Rest],Env,Module):-
 1014  builtin(H),!,
 1015  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1016
 1017process_body([db(H)|T],BDD,BDD1,Vars,Vars1,[H|Rest],Env,Module):-
 1018  !,
 1019  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Env,Module).
 1020
 1021process_body([H|T],BDD,BDD1,Vars,[BDDH,BDD2|Vars1],
 1022[H1,andc(Env,BDD,BDDH,BDD2)|Rest],Env,Module):-
 1023  add_bdd_arg(H,Env,BDDH,Module,H1),
 1024  process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Env,Module).
 1025
 1026process_body_db([],BDD,BDD,_DB,Vars,Vars,[],_Env,_Module):-!.
 1027
 1028process_body_db([\+ H|T],BDD,BDD1,DB,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1029  builtin(H),!,
 1030  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1031
 1032process_body_db([\+ db(H)|T],BDD,BDD1,DB,Vars,Vars1,[\+ H|Rest],Env,Module):-
 1033  !,
 1034  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1035
 1036process_body_db([\+ H|T],BDD,BDD1,DB,Vars,[BDDH,BDDN,BDD2|Vars1],
 1037[H1,bdd_notc(Env,BDDH,BDDN),
 1038  andc(Env,BDD,BDDN,BDD2)|Rest],Env,Module):-!,
 1039  add_bdd_arg_db(H,Env,BDDH,DB,Module,H1),
 1040  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1041
 1042process_body_db([H|T],BDD,BDD1,DB,Vars,Vars1,[H1|Rest],Env,Module):-
 1043  transform(H,H1),!,
 1044  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1045
 1046process_body_db([H|T],BDD,BDD1,DB,Vars,Vars1,[H|Rest],Env,Module):-
 1047  builtin(H),!,
 1048  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1049
 1050process_body_db([db(H)|T],BDD,BDD1,DB,Vars,Vars1,[H|Rest],Env,Module):-
 1051  !,
 1052  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1053
 1054process_body_db([H|T],BDD,BDD1,DB,Vars,[BDDH,BDD2|Vars1],
 1055[H1,andc(Env,BDD,BDDH,BDD2)|Rest],Env,Module):-!, %agg. cut
 1056  add_bdd_arg_db(H,Env,BDDH,DB,Module,H1),
 1057  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Env,Module).
 1058
 1059
 1060process_head(HeadList, GroundHeadList1) :-
 1061  ground_prob(HeadList), !,
 1062  process_head_ground(HeadList, 0.0, GroundHeadList),
 1063  ( GroundHeadList = [V:P] -> 
 1064      P1 is 1.0 - P, 
 1065      GroundHeadList1 = [V:P,'':P1] ; 
 1066      GroundHeadList1 = GroundHeadList
 1067  ).
 1068
 1069process_head(HeadList0, HeadList):-
 1070  get_probs(HeadList0,PL),
 1071  foldl(minus,PL,1.0,PNull),
 1072  append(HeadList0,['':PNull],HeadList).
 1073
 1074minus(A,B,B-A).
 1075
 1076prob_ann(_:P0,P):-!, to_float(P0,P).
 1077prob_ann(P0::_,P):- to_float(P0, P).
 1078
 1079to_float(P0, P) :-
 1080  ground(P0), !,
 1081  P is float(P0).
 1082to_float(P, P).
 1083
 1084gen_head(H,P,VH,V,V1,H1:P):-copy_term((H,VH,V),(H1,VH,V1)).
 1085gen_head_disc(H,VH,V,V1:P,H1:P1):-copy_term((H,VH,V),(H1,VH,V1)),P1 is float(P).
 1086
 1087
 1088/* process_head_ground([Head:ProbHead], Prob, [Head:ProbHead|Null])
 1089 * ----------------------------------------------------------------
 1090 */
 1091process_head_ground([H], Prob, [Head:ProbHead1|Null]) :-
 1092  (H=Head:ProbHead;H=ProbHead::Head),!,
 1093  ProbHead1 is float(ProbHead),
 1094  ProbLast is 1.0 - Prob - ProbHead1,
 1095  prolog_load_context(module, M),pita_input_mod(M),
 1096  M:local_pita_setting(epsilon_parsing, Eps),
 1097  EpsNeg is - Eps,
 1098  ProbLast > EpsNeg,
 1099  (ProbLast > Eps ->
 1100    Null = ['':ProbLast]
 1101  ;
 1102    Null = []
 1103  ).
 1104
 1105process_head_ground([H|Tail], Prob, [Head:ProbHead1|Next]) :-
 1106  (H=Head:ProbHead;H=ProbHead::Head),
 1107  ProbHead1 is float(ProbHead),
 1108  ProbNext is Prob + ProbHead1,
 1109  process_head_ground(Tail, ProbNext, Next).
 1110
 1111
 1112ground_prob([]).
 1113
 1114ground_prob([_Head:ProbHead|Tail]) :-!,
 1115  ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
 1116  ground_prob(Tail).
 1117
 1118ground_prob([ProbHead::_Head|Tail]) :-
 1119  ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
 1120  ground_prob(Tail).
 1121
 1122
 1123get_probs(Head, PL):-
 1124  maplist(prob_ann,Head,PL).
 1125
 1126/*get_probs([], []).
 1127
 1128get_probs([_H:P|T], [P1|T1]) :-
 1129  P1 is P,
 1130  get_probs(T, T1).
 1131*/
 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/

/

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