3:- module(pitaind,[prob_ind/2, prob_bar/2, prob_ind/3, prob_bar/3,
    4  set_pitaind/2,setting_pitaind/2,
    5  onec/1,zeroc/1,andc/3,notc/2,andcnf/3,
    6  orc_ind/3,orc_exc/3,
    7  get_var_n/5,or_list_pitaind/3,
    8  or_list_ind/2,or_list_exc/2,
    9  equalityc/3,
   10  op(600,xfy,'::'),
   11  op(1150,fx,action)
   12    ]).

pitaind

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

author
- Fabrizio Riguzzi
license
- Artistic License 2.0
   26:-meta_predicate s(:,-).   27:-meta_predicate prob_ind(:,-).   28:-meta_predicate prob_bar(:,-).   29:-meta_predicate prob_ind(:,:,-).   30:-meta_predicate prob_bar(:,:,-).   31:-meta_predicate get_p(:,-).   32:-meta_predicate get_cond_p(:,:,-).   33:-meta_predicate get_node(:,-).   34:-meta_predicate get_cond_node(:,:,-,-).   35:-meta_predicate set_pitaind(:,+).   36:-meta_predicate setting_pitaind(:,-).   37:-meta_predicate set_sw(:,+).   38
   39:-use_module(library(lists)).   40:-use_module(library(apply)).   41:-use_module(library(assoc)).   42
   43:- style_check(-discontiguous).   44
   45:- thread_local rule_n/1,goal_n/1,pitaind_input_mod/1,local_pitaind_setting/2.   46
   47%  remove/3.
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60default_setting_pitaind(epsilon_parsing, 1e-5).
   61/* on, off */
   62
   63default_setting_pitaind(bagof,false).
   64/* values: false, intermediate, all, extra */
   65
   66default_setting_pitaind(compiling,off).
   67
   68:-set_prolog_flag(unknown,warning).   69
   70default_setting_pitaind(depth_bound,false).  %if true, it limits the derivation of the example to the value of 'depth'
   71default_setting_pitaind(depth,5).
   72default_setting_pitaind(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.
   73default_setting_pitaind(or,ind).
   74/* values: ind, exc
   75how or is computed: by assuming independence or exclusion
   76*/
 orc_ind(++A:float, ++B:float, --AorB:float) is det
Returns A + B - A*B in AorB (or in case of independence) /
   86orc_ind(A,B,C):-
   87        C is 1-(1-A)*(1-B).
 orc_exc(++A:float, ++B:float, --AorB:float) is det
Returns A + B in AorB (or in case of exclusion) /
   94orc_exc(A,B,C):-
   95        C is A+B.
 onec(--One:float) is det
Returns 1.0 /
  102onec(1.0).
 zeroc(--Zero:float) is det
Returns 0.0 /
  109zeroc(0.0).
 andc(++A:float, ++B:float, --AandB:float) is det
Returns A*B in AandB (and in case of idependence). Fails if either A or B is 0.0 /
  117andc(A,B,C):-
  118  ((A=0.0;B=0.0)->
  119    %C=and(A,B)
  120    fail
  121  ;
  122    (A=1.0->
  123      C=B
  124    ;
  125      (B=1.0->
  126        C=A
  127      ;
  128        C is A*B
  129      )
  130    )
  131  ).
 andcnf(++A:float, ++B:float, --AandB:float) is det
Returns A*B in AandB (and in case of idependence). /
  138andcnf(A,B,C):-
  139  (A=1.0->
  140    C=B
  141  ;
  142    (B=1.0->
  143      C=A
  144    ;
  145      C is A*B
  146    )
  147  ).
 notc(++A:float, --NotA:float) is det
Returns 1-A in NotA (negation) /
  155notc(A,B):-
  156  (A=0.0->
  157    B=1.0
  158  ;
  159    (A=1.0->
  160      B=0.0
  161    ;
  162      B is 1.0-A
  163    )
  164  ).
 equalityc(++Variable:int, ++Value:int, --P:float) is det
Returns in P the probability that Variable takes Value. /
  171equalityc(Probs,N,P):-
  172  nth0(N,Probs,P).
 s(:Query:conjunction_of_literals, -Probability:float) is nondet
The predicate computes the probability of the ground query Query. If Query is not ground, it returns in backtracking all instantiations of Query together with their probabilities /
  184s(M:Goal,P):-
  185  term_variables(Goal,VG),
  186  get_next_goal_number(M,GN),
  187  atomic_concat('$goal',GN,NewGoal),
  188  Goal1=..[NewGoal|VG],
  189  list2and(GoalL,Goal),
  190  process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,M),
  191  append([onec(BDD)],BodyList2,BodyList3),
  192  list2and(BodyList3,Body2),
  193  add_bdd_arg(Goal1,BDDAnd,M,Head1),
  194  M:(asserta((Head1 :- Body2),Ref)),
  195  findall((Goal,P),get_p(M:Goal1,P),L),
  196  erase(Ref),
  197  member((Goal,P),L).
 prob_ind(:Query:conjunction_of_literals, -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 /
  211prob_ind(M:Goal,P):-
  212  must_be(nonvar,Goal),
  213  must_be(var,P),
  214  s(M:Goal,P).
 prob_bar(:Query:conjunction_of_literals, -Probability:dict) is nondet
The predicate computes the probability of Query and returns it as a dict for rendering with c3 as a bar chart with a bar for the probability of Query true and a bar for the probability of Query false. If Query is not ground, it returns in backtracking all ground instantiations of Query together with their probabilities /
  227prob_bar(M:Goal,Chart):-
  228  must_be(nonvar,Goal),
  229  must_be(var,Chart),
  230  s(M:Goal,P),
  231  PF is 1.0-P,
  232  Chart = c3{data:_{x:elem, rows:[elem-prob,'T'-P,'F' -PF], type:bar},
  233          axis:_{x:_{type:category}, rotated: true,
  234                 y:_{min:0.0,max:1.0,padding:_{bottom:0.0,top:0.0},
  235             tick:_{values:[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]}}},
  236	           size:_{height: 100},
  237	          legend:_{show: false}}.
 prob_ind(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, -Probability:float) 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 /
  248prob_ind(M:Goal,M:Evidence,P):-
  249  must_be(nonvar,Goal),
  250  must_be(nonvar,Evidence),
  251  must_be(var,P),
  252  get_next_goal_number(M,GN),
  253  atomic_concat('$ev',GN,NewEv),
  254  deal_with_ev(Evidence,M,NewEv,EvNoAct,UpdatedClausesRefs,ClausesToReAdd),
  255  term_variables(Goal,VG),
  256  atomic_concat('$goal',GN,NewGoal),
  257  Goal1=..[NewGoal|VG],
  258  list2and(GoalL,Goal),
  259  process_body(GoalL,BDD,BDDAnd,[],_Vars,BodyList2,M),
  260  append([onec(BDD)],BodyList2,BodyList3),
  261  list2and(BodyList3,Body2),
  262  add_bdd_arg(Goal1,BDDAnd,M,Head1),
  263  M:(asserta((Head1 :- Body2),Ref)),
  264  (EvNoAct=true->
  265    findall((Goal,P),get_p(M:Goal1,P),L)
  266  ;
  267    findall((Goal,P),get_cond_p(M:Goal1,M:EvNoAct,P),L)
  268  ),
  269  retractall(M:NewEv),
  270  maplist(erase,UpdatedClausesRefs),
  271  erase(Ref),
  272  maplist(M:assertz,ClausesToReAdd),
  273  member((Goal,P),L).
  274
  275deal_with_ev(Ev,M,NewEv,EvGoal,UC,CA):-
  276  list2and(EvL,Ev),
  277  partition(ac,EvL,ActL,EvNoActL),
  278  deal_with_actions(ActL,M,UC0,CA),
  279  (EvNoActL=[]->
  280    EvGoal=true,
  281    UC=UC0
  282  ;
  283    process_body(EvNoActL,BDD,BDDAnd,[],_Vars,BodyList2,M),
  284    append([onec(BDD)],BodyList2,BodyList3),
  285    list2and(BodyList3,Body2),
  286    add_bdd_arg(NewEv,BDDAnd,M,Head1),
  287    M:(asserta((Head1 :- Body2),Ref)),
  288    UC=[Ref|UC0],
  289    EvGoal=NewEv
  290  ).
  291
  292deal_with_actions(ActL,M,UC,CA):-
  293  empty_assoc(AP0),
  294  foldl(get_pred_const,ActL,AP0,AP),
  295  assoc_to_list(AP,LP),
  296  maplist(update_clauses(M),LP,UCL,CAL),
  297  partition(nac,ActL,_NActL,PActL),
  298  maplist(assert_actions(M),PActL,ActRefs),
  299  append([ActRefs|UCL],UC),
  300  append(CAL,CA).
  301
  302zero_clauses_actions(M,do(\+ A),Ref):-
  303  A=..[P|Args],
  304  append(Args,[BDD],Args1),
  305  A1=..[P|Args1],
  306  M:assertz((A1:-zeroc(BDD)),Ref).
  307
  308assert_actions(M,do(A),Ref):-
  309  A=..[P|Args],
  310  append(Args,[BDD],Args1),
  311  A1=..[P|Args1],
  312  M:assertz((A1:-onec(BDD)),Ref).
  313
  314update_clauses(M,P/0- _,[RefZ],[(H:-zeroc(BDD))|LCA]):-!,
  315  functor(G1,P,2),
  316  findall(Ref,M:clause(G1,_B,Ref),UC),
  317  findall((G1:-B),M:clause(G1,B),LCA),
  318  H=..[P,BDD],
  319  maplist(erase,UC),
  320  M:assertz((H:-zeroc(BDD)),RefZ).
  321
  322update_clauses(M,P/A-Constants,UC,CA):-
  323  functor(G,P,A),
  324  A1 is A+2,
  325  functor(G1,P,A1),
  326  G=..[_|Args],
  327  findall((G1,B,Ref),M:clause(G1,B,Ref),LC),
  328  maplist(get_const(Args),Constants,ConstraintsL),
  329  list2and(ConstraintsL,Constraints),
  330  maplist(add_cons(G1,Constraints,M),LC,UC,CA).
  331
  332add_cons(_G,_C,M,(H,zeroc(Zero),Ref),Ref1,(H:-zeroc(Zero))):-!,
  333  erase(Ref),
  334  M:assertz((H:-zeroc(Zero)),Ref1).
  335
  336add_cons(G,C,M,(H,B,Ref),Ref1,(H:-B)):-
  337  copy_term((G,C),(G1,C1)),
  338  G1=H,
  339  erase(Ref),
  340  M:assertz((H:-(C1,B)),Ref1).
  341
  342
  343get_const(Args,Constants,Constraint):-
  344  maplist(constr,Args,Constants,ConstraintL),
  345  list2and(ConstraintL,Constraint).
  346
  347constr(V,C,dif(V,C)).
  348
  349get_pred_const(do(Do0),AP0,AP):-
  350  (Do0= (\+ Do)->
  351    true
  352  ;
  353    Do=Do0
  354  ),
  355  functor(Do,F,A),
  356  Do=..[_|Args],
  357  (get_assoc(F/A,AP0,V)->
  358    put_assoc(F/A,AP0,[Args|V],AP)
  359  ;
  360    put_assoc(F/A,AP0,[Args],AP)
  361  ).
  362
  363
  364ac(do(_)).
  365nac(do(\+ _)).
 prob_bar(:Query:conjunction_of_literals, :Evidence:conjunction_of_literals, -Probability:dict) is nondet
The predicate computes the probability of the Query given Evidence and returns it as a dict for rendering with c3 as a bar chart with a bar for the probability of Query true and a bar for the probability of Query false given Evidence. If Query /Evidence are not ground, it returns in backtracking all ground instantiations of Query/Evidence together with their probabilities /
  378prob_bar(M:Goal,M:Evidence,Chart):-
  379  must_be(nonvar,Goal),
  380  must_be(nonvar,Evidence),
  381  must_be(var,Chart),
  382  prob_ind(M:Goal,M:Evidence,P),
  383  PF is 1.0-P,
  384  Chart = c3{data:_{x:elem, rows:[elem-prob,'T'-P,'F' -PF], type:bar},
  385          axis:_{x:_{type:category}, rotated: true,
  386                 y:_{min:0.0,max:1.0,padding:_{bottom:0.0,top:0.0},
  387             tick:_{values:[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]}}},
  388	           size:_{height: 100},
  389	          legend:_{show: false}}.
  390
  391
  392get_p(M:Goal,P):-
  393  get_node(M:Goal,P).
  394
  395get_cond_p(M:Goal,M:Evidence,P):-
  396  get_cond_node(M:Goal,M:Evidence,PGE,PE),
  397  P is PGE/PE.
  398
  399
  400get_node(M:Goal,B):-
  401  M:local_pitaind_setting(depth_bound,true),!,
  402  M:local_pitaind_setting(depth,DB),
  403  retractall(M:v(_,_,_)),
  404  abolish_all_tables,
  405  add_bdd_arg_db(Goal,BDD,DB,M,Goal1),%DB=depth bound
  406  (bagof(BDD,M:Goal1,L)*->
  407    or_list_pitaind(L,M,B)
  408  ;
  409    zeroc(B)
  410  ).
  411
  412get_node(M:Goal,B):- %with DB=false
  413  retractall(M:v(_,_,_)),
  414  abolish_all_tables,
  415  add_bdd_arg(Goal,BDD,M,Goal1),
  416  (bagof(BDD,M:Goal1,L)*->
  417    or_list_pitaind(L,M,B)
  418  ;
  419    zeroc(B)
  420  ).
  421
  422get_cond_node(M:Goal,M:Ev,BGE,BE):-
  423  M:local_pitaind_setting(depth_bound,true),!,
  424  M:local_pitaind_setting(depth,DB),
  425  retractall(M:v(_,_,_)),
  426  abolish_all_tables,
  427  add_bdd_arg_db(Goal,BDD,DB,M,Goal1),%DB=depth bound
  428  (bagof(BDD,M:Goal1,L)*->
  429    or_list_pitaind(L,M,BG)
  430  ;
  431    zeroc(BG)
  432  ),
  433  add_bdd_arg_db(Ev,BDDE,DB,M,Ev1),%DB=depth bound
  434  (bagof(BDDE,M:Ev1,LE)*->
  435    or_list_pitaind(LE,M,BE)
  436  ;
  437    zeroc(BE)
  438  ),
  439  andcnf(BG,BE,BGE).
  440
  441
  442
  443get_cond_node(M:Goal,M:Ev,BGE,BE):- %with DB=false
  444  retractall(M:v(_,_,_)),
  445  abolish_all_tables,
  446  add_bdd_arg(Goal,BDD,M,Goal1),
  447  (bagof(BDD,M:Goal1,L)*->
  448    or_list_pitaind(L,M,BG)
  449  ;
  450    zeroc(BG)
  451  ),
  452  add_bdd_arg(Ev,BDDE,M,Ev1),
  453  (bagof(BDDE,M:Ev1,LE)*->
  454    or_list_pitaind(LE,M,BE)
  455  ;
  456    zeroc(BE)
  457  ),
  458  andcnf(BG,BE,BGE).
  459
  460
  461get_next_goal_number(PName,R):-
  462  retract(PName:goal_n(R)),
  463  R1 is R+1,
  464  assert(PName:goal_n(R1)).
  465
  466
  467get_next_rule_number(PName,R):-
  468  retract(PName:rule_n(R)),
  469  R1 is R+1,
  470  assert(PName:rule_n(R1)).
  471
  472
  473assert_all([],_M,[]).
  474
  475assert_all([H|T],M,[HRef|TRef]):-
  476  assertz(M:H,HRef),
  477  assert_all(T,M,TRef).
  478
  479
  480retract_all([]):-!.
  481
  482retract_all([H|T]):-
  483  erase(H),
  484  retract_all(T).
 get_var_n(++M:atomic, ++Rule:int, ++Substitution:term, ++Probabilities:list, -Variable:int) is det
Returns the index Variable of the random variable associated to rule with index Rule, grouding substitution Substitution and head distribution Probabilities in environment Environment. /
  493get_var_n(_M,_R,_S,Probs0,Probs):-
  494  (ground(Probs0)->
  495    maplist(is,Probs,Probs0)
  496  ;
  497    throw(error('Non ground probailities not instantiated by the body'))
  498  ).
  499
  500
  501
  502combine(V,P,V:P).
  503
  504add_bdd_arg(M:A,BDD,M:A1):-
  505  A=..[P|Args],
  506  append(Args,[BDD],Args1),
  507  A1=..[P|Args1].
  508
  509
  510add_bdd_arg_db(M:A,BDD,DB,M:A1):-
  511  A=..[P|Args],
  512  append(Args,[DB,BDD],Args1),
  513  A1=..[P|Args1].
  514
  515
  516add_bdd_arg(A,BDD,_Module,A1):-
  517  A=..[P|Args],
  518  append(Args,[BDD],Args1),
  519  A1=..[P|Args1].
  520
  521
  522add_bdd_arg_db(A,BDD,DB,_Module,A1):-
  523  A=..[P|Args],
  524  append(Args,[DB,BDD],Args1),
  525  A1=..[P|Args1].
  526
  527add_mod_arg(A,_Module,A1):-
  528  A=..[P|Args],
  529  A1=..[P|Args].
  530
  531
  532generate_rules_fact([],_VC,_R,_Probs,_N,[],_Module).
  533
  534generate_rules_fact([Head:_P1,'':_P2],VC,R,Probs,N,[Clause],Module):-!,
  535  add_bdd_arg(Head,BDD,Module,Head1),
  536  Clause=(Head1:-(get_var_n(Module,R,VC,Probs,V),equalityc(V,N,BDD))).
  537
  538generate_rules_fact([Head:_P|T],VC,R,Probs,N,[Clause|Clauses],Module):-
  539  add_bdd_arg(Head,BDD,Module,Head1),
  540  Clause=(Head1:-(get_var_n(Module,R,VC,Probs,V),equalityc(V,N,BDD))),
  541  N1 is N+1,
  542  generate_rules_fact(T,VC,R,Probs,N1,Clauses,Module).
  543
  544
  545generate_rules_fact_vars([],__R,_Probs,_N,[],_Module).
  546
  547generate_rules_fact_vars([Head:_P1,'':_P2],R,Probs,N,[Clause],Module):-!,
  548  term_variables([Head],VC),
  549  add_bdd_arg(Head,BDD,Module,Head1),
  550  Clause=(Head1:-(get_var_n(Module,R,VC,Probs,V),equalityc(V,N,BDD))).
  551
  552generate_rules_fact_vars([Head:_P|T],R,Probs,N,[Clause|Clauses],Module):-
  553  term_variables([Head],VC),
  554  add_bdd_arg(Head,BDD,Module,Head1),
  555  Clause=(Head1:-(get_var_n(Module,R,VC,Probs,V),equalityc(V,N,BDD))),
  556  N1 is N+1,
  557  generate_rules_fact_vars(T,R,Probs,N1,Clauses,Module).
  558
  559
  560generate_rules_fact_db([],__VC,_R,_Probs,_N,[],_Module).
  561
  562generate_rules_fact_db([Head:_P1,'':_P2],VC,R,Probs,N,[Clause],Module):-!,
  563  add_bdd_arg_db(Head,BDD,_DB,Module,Head1),
  564  Clause=(Head1:-(get_var_n(Module,R,VC,Probs,V),equalityc(V,N,BDD))).
  565
  566generate_rules_fact_db([Head:_P|T],VC,R,Probs,N,[Clause|Clauses],Module):-
  567  add_bdd_arg_db(Head,BDD,_DB,Module,Head1),
  568  Clause=(Head1:-(get_var_n(Module,R,VC,Probs,V),equalityc(V,N,BDD))),
  569  N1 is N+1,
  570  generate_rules_fact_db(T,VC,R,Probs,N1,Clauses,Module).
  571
  572
  573generate_clause(Head,Body,VC,R,Probs,BDDAnd,N,Clause,Module):-
  574  add_bdd_arg(Head,BDD,Module,Head1),
  575  Clause=(Head1:-(Body,get_var_n(Module,R,VC,Probs,V),equalityc(V,N,B),andc(BDDAnd,B,BDD))).
  576
  577
  578generate_clause_db(Head,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module):-
  579  add_bdd_arg_db(Head,BDD,DBH,Module,Head1),
  580  Clause=(Head1:-(DBH>=1,DB is DBH-1,Body,get_var_n(Module,R,VC,Probs,V),equalityc(V,N,B),andc(BDDAnd,B,BDD))).
  581
  582
  583generate_rules([],__Body,_VC,_R,_Probs,_BDDAnd,_N,[],_Module).
  584
  585generate_rules([Head:_P1,'':_P2],Body,VC,R,Probs,BDDAnd,N,[Clause],Module):-!,
  586  generate_clause(Head,Body,VC,R,Probs,BDDAnd,N,Clause,Module).
  587
  588generate_rules([Head:_P|T],Body,VC,R,Probs,BDDAnd,N,[Clause|Clauses],Module):-
  589  generate_clause(Head,Body,VC,R,Probs,BDDAnd,N,Clause,Module),
  590  N1 is N+1,
  591  generate_rules(T,Body,VC,R,Probs,BDDAnd,N1,Clauses,Module).
  592
  593
  594generate_rules_db([],__Body,_VC,_R,_Probs,_DB,_BDDAnd,_N,[],_Module):-!.
  595
  596generate_rules_db([Head:_P1,'':_P2],Body,VC,R,Probs,DB,BDDAnd,N,[Clause],Module):-!,
  597  generate_clause_db(Head,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module).
  598
  599generate_rules_db([Head:_P|T],Body,VC,R,Probs,DB,BDDAnd,N,[Clause|Clauses],Module):-
  600  generate_clause_db(Head,Body,VC,R,Probs,DB,BDDAnd,N,Clause,Module),!,%agg.cut
  601  N1 is N+1,
  602  generate_rules_db(T,Body,VC,R,Probs,DB,BDDAnd,N1,Clauses,Module).
  603
  604
  605process_body([],BDD,BDD,Vars,Vars,[],__Module).
  606
  607process_body([\+ H|T],BDD,BDD1,Vars,Vars1,[\+ H|Rest],Module):-
  608  builtin(H),!,
  609  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Module).
  610
  611process_body([\+ db(H)|T],BDD,BDD1,Vars,Vars1,[\+ H|Rest],Module):-
  612  !,
  613  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Module).
  614
  615process_body([\+ H|T],BDD,BDD1,Vars,[BDDH,BDDN,L,BDDL,BDD2|Vars1],
  616[(bagof(BDDH,H1,L)*->or_list_ind(L,BDDL),notc(BDDL,BDDN);onec(BDDN)),
  617  andc(BDD,BDDN,BDD2)|Rest],Module):-
  618  Module:local_pitaind_setting(or,ind),!,
  619  add_bdd_arg(H,BDDH,Module,H1),
  620  process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Module).
  621
  622process_body([\+ H|T],BDD,BDD1,Vars,[BDDH,BDDN,L,BDDL,BDD2|Vars1],
  623  [(bagof(BDDH,H1,L)*->or_list_exc(L,BDDL),notc(BDDL,BDDN);onec(BDDN)),
  624    andc(BDD,BDDN,BDD2)|Rest],Module):-!,
  625    add_bdd_arg(H,BDDH,Module,H1),
  626    process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Module).
  627  
  628process_body([H|T],BDD,BDD1,Vars,Vars1,[H|Rest],Module):-
  629  builtin(H),!,
  630  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Module).
  631
  632process_body([db(H)|T],BDD,BDD1,Vars,Vars1,[H|Rest],Module):-
  633  !,
  634  process_body(T,BDD,BDD1,Vars,Vars1,Rest,Module).
  635
  636process_body([H|T],BDD,BDD1,Vars,[BDDH,BDD2|Vars1],
  637[bagof(BDDH,H1,L),or_list_ind(L,BDDL),andc(BDD,BDDL,BDD2)|Rest],Module):-
  638  Module:local_pitaind_setting(or,ind),!,
  639  add_bdd_arg(H,BDDH,Module,H1),
  640  process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Module).
  641
  642process_body([H|T],BDD,BDD1,Vars,[BDDH,BDD2|Vars1],
  643  [bagof(BDDH,H1,L),or_list_exc(L,BDDL),andc(BDD,BDDL,BDD2)|Rest],Module):-
  644    add_bdd_arg(H,BDDH,Module,H1),
  645    process_body(T,BDD2,BDD1,Vars,Vars1,Rest,Module).
  646  
  647process_body_db([],BDD,BDD,_DB,Vars,Vars,[],__Module):-!.
  648
  649process_body_db([\+ H|T],BDD,BDD1,DB,Vars,Vars1,[\+ H|Rest],Module):-
  650  builtin(H),!,
  651  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Module).
  652
  653process_body_db([\+ db(H)|T],BDD,BDD1,DB,Vars,Vars1,[\+ H|Rest],Module):-
  654  !,
  655  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Module).
  656
  657process_body_db([\+ H|T],BDD,BDD1,DB,Vars,[BDDH,BDDN,L,BDDL,BDD2|Vars1],
  658[(bagof(BDDH,H1,L)*->or_list_ind(L,BDDL),notc(BDDL,BDDN);onec(BDDN)),
  659  andc(BDD,BDDN,BDD2)|Rest],Module):-
  660  Module:local_pitaind_setting(or,ind),!,
  661  add_bdd_arg_db(H,BDDH,DB,Module,H1),
  662  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Module).
  663
  664process_body_db([\+ H|T],BDD,BDD1,DB,Vars,[BDDH,BDDN,L,BDDL,BDD2|Vars1],
  665  [(bagof(BDDH,H1,L)*->or_list_exc(L,BDDL),notc(BDDL,BDDN);onec(BDDN)),
  666    andc(BDD,BDDN,BDD2)|Rest],Module):-!,
  667    add_bdd_arg_db(H,BDDH,DB,Module,H1),
  668    process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Module).
  669  
  670process_body_db([H|T],BDD,BDD1,DB,Vars,Vars1,[H|Rest],Module):-
  671  builtin(H),!,
  672  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Module).
  673
  674process_body_db([db(H)|T],BDD,BDD1,DB,Vars,Vars1,[H|Rest],Module):-
  675  !,
  676  process_body_db(T,BDD,BDD1,DB,Vars,Vars1,Rest,Module).
  677
  678process_body_db([H|T],BDD,BDD1,DB,Vars,[BDDH,BDD2|Vars1],
  679[bagof(BDDH,H1,L),or_list_ind(L,BDDL),andc(BDD,BDDL,BDD2)|Rest],Module):-
  680  Module:local_pitaind_setting(or,ind),!,
  681  add_bdd_arg_db(H,BDDH,DB,Module,H1),
  682  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Module).
  683
  684process_body_db([H|T],BDD,BDD1,DB,Vars,[BDDH,BDD2|Vars1],
  685  [bagof(BDDH,H1,L),or_list_exc(L,BDDL),andc(BDD,BDDL,BDD2)|Rest],Module):-!, %agg. cut
  686  add_bdd_arg_db(H,BDDH,DB,Module,H1),
  687  process_body_db(T,BDD2,BDD1,DB,Vars,Vars1,Rest,Module).
  688  
  689process_head(HeadList, GroundHeadList) :-
  690  ground_prob(HeadList), !,
  691  process_head_ground(HeadList, 0, GroundHeadList).
  692
  693process_head(HeadList0, HeadList):-
  694  get_probs(HeadList0,PL),
  695  foldl(minus,PL,1,PNull),
  696  append(HeadList0,['':PNull],HeadList).
  697
  698minus(A,B,B-A).
  699
  700prob_ann(_:P,P):-!.
  701prob_ann(P::_,P).
  702
  703
  704gen_head(H,P,V,V1,H1:P):-copy_term((H,V),(H1,V1)).
  705gen_head_disc(H,V,V1:P,H1:P):-copy_term((H,V),(H1,V1)).
  706
  707
  708/* process_head_ground([Head:ProbHead], Prob, [Head:ProbHead|Null])
  709 * ----------------------------------------------------------------
  710 */
  711process_head_ground([H], Prob, [Head:ProbHead1|Null]) :-
  712  (H=Head:ProbHead;H=ProbHead::Head),!,
  713  ProbHead1 is ProbHead,
  714  ProbLast is 1 - Prob - ProbHead1,
  715  prolog_load_context(module, M),pitaind_input_mod(M),
  716  M:local_pitaind_setting(epsilon_parsing, Eps),
  717  EpsNeg is - Eps,
  718  ProbLast > EpsNeg,
  719  (ProbLast > Eps ->
  720    Null = ['':ProbLast]
  721  ;
  722    Null = []
  723  ).
  724
  725process_head_ground([H|Tail], Prob, [Head:ProbHead1|Next]) :-
  726  (H=Head:ProbHead;H=ProbHead::Head),
  727  ProbHead1 is ProbHead,
  728  ProbNext is Prob + ProbHead1,
  729  process_head_ground(Tail, ProbNext, Next).
  730
  731
  732ground_prob([]).
  733
  734ground_prob([_Head:ProbHead|Tail]) :-!,
  735  ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
  736  ground_prob(Tail).
  737
  738ground_prob([ProbHead::_Head|Tail]) :-
  739  ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
  740  ground_prob(Tail).
  741
  742
  743get_probs(Head, PL):-
  744  maplist(prob_ann,Head,PL).
  745
  746/*get_probs([], []).
  747
  748get_probs([_H:P|T], [P1|T1]) :-
  749  P1 is P,
  750  get_probs(T, T1).
  751*/
 or_list_pitaind(++ListOfProbs:list, ++Module:module, --P:float) is det
Returns in P the probability of the disjunction of the random variables whose probabilities are in ListOfProbs. Module is used to check the setting for disjunction, either independent or exclusive. /
  760or_list_pitaind(L,M,O):-
  761  M:local_pitaind_setting(or,ind),!,
  762  or_list_ind(L,O).
  763
  764or_list_pitaind(L,_M,O):-
  765  or_list_exc(L,O).
 or_list_ind(++ListOfProbs:list, --P:float) is det
Returns in P the probability of the disjunction of the random variables whose probabilities are in ListOfProbs assuming independence. /
  773or_list_ind([H],H):-!.
  774
  775or_list_ind([H|T],B):-
  776  or_list1_ind(T,H,B).
  777
  778
  779or_list1_ind([],B,B).
  780
  781or_list1_ind([H|T],B0,B1):-
  782  orc_ind(B0,H,B2),
  783  or_list1_ind(T,B2,B1).
 or_list_exc(++ListOfProbs:list, --P:float) is det
Returns in P the probability of the disjunction of the random variables whose probabilities are in ListOfProbs assuming exclusiveness. /
  791or_list_exc([H],H):-!.
  792
  793or_list_exc([H|T],B):-
  794  or_list1_exc(T,H,B).
  795
  796
  797or_list1_exc([],B,B).
  798
  799or_list1_exc([H|T],B0,B1):-
  800  orc_exc(B0,H,B2),
  801  or_list1_exc(T,B2,B1).
 set_pitaind(: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/

/

  811set_pitaind(M:Parameter,Value):-
  812  must_be(atom,Parameter),
  813  must_be(nonvar,Value),
  814  retract(M:local_pitaind_setting(Parameter,_)),
  815  assert(M:local_pitaind_setting(Parameter,Value)).
 setting_pitaind(: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/ /
  824setting_pitaind(M:P,V):-
  825  must_be(atom,P),
  826  M:local_pitaind_setting(P,V).
  827
  828
  829
  830delete_equal([],_,[]).
  831
  832delete_equal([H|T],E,T):-
  833  H == E,!.
  834
  835delete_equal([H|T],E,[H|T1]):-
  836  delete_equal(T,E,T1).
  837
  838set_sw(M:A,B):-
  839  get_next_rule_number(M,R),
  840  assert(M:sw(R,A,B)).
  841
  842act(M,A/B):-
  843  B1 is B + 2,
  844  M:(dynamic A/B1).
  845
  846tab(A/B,A/B1):-
  847  B1 is B + 2.
  848
  849
  850
  851pitaind_expansion((:- action Conj), []) :-!,
  852  prolog_load_context(module, M),
  853  list2and(L,Conj),
  854  maplist(act(M),L).
  855
  856pitaind_expansion((:- table(Conj)), [:- table(Conj1)]) :-!,
  857  prolog_load_context(module, M),
  858  pitaind_input_mod(M),!,
  859  list2and(L,Conj),
  860  maplist(tab,L,L1),
  861  list2and(L1,Conj1).
  862
  863pitaind_expansion((:- begin_plp), []) :-
  864  prolog_load_context(module, M),
  865  pitaind_input_mod(M),!,
  866  assert(M:pitaind_on).
  867
  868pitaind_expansion((:- end_plp), []) :-
  869  prolog_load_context(module, M),
  870  pitaind_input_mod(M),!,
  871  retractall(M:pitaind_on).
  872
  873pitaind_expansion((:- begin_lpad), []) :-
  874  prolog_load_context(module, M),
  875  pitaind_input_mod(M),!,
  876  assert(M:pitaind_on).
  877
  878pitaind_expansion((:- end_lpad), []) :-
  879  prolog_load_context(module, M),
  880  pitaind_input_mod(M),!,
  881  retractall(M:pitaind_on).
  882
  883pitaind_expansion(values(A,B), values(A,B)) :-
  884  prolog_load_context(module, M),
  885  pitaind_input_mod(M),M:pitaind_on,!.
  886
  887pitaind_expansion((Head :- Body), Clauses):-
  888  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  889  M:local_pitaind_setting(depth_bound,true),
  890% disjunctive clause with more than one head atom e depth_bound
  891  Head = (_;_), !,
  892  list2or(HeadListOr, Head),
  893  process_head(HeadListOr, HeadList),
  894  list2and(BodyList, Body),
  895  process_body_db(BodyList,BDD,BDDAnd, DB,[],_Vars,BodyList1,M),
  896  append([onec(BDD)],BodyList1,BodyList2),
  897  list2and(BodyList2,Body1),
  898  append(HeadList,BodyList,List),
  899  term_variables(List,VC),
  900  get_next_rule_number(M,R),
  901  get_probs(HeadList,Probs),
  902  (M:local_pitaind_setting(single_var,true)->
  903    generate_rules_db(HeadList,Body1,[],R,Probs,DB,BDDAnd,0,Clauses,M)
  904  ;
  905    generate_rules_db(HeadList,Body1,VC,R,Probs,DB,BDDAnd,0,Clauses,M)
  906   ).
  907
  908pitaind_expansion((Head :- Body), Clauses):-
  909  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  910% disjunctive clause with more than one head atom senza depth_bound
  911  Head = (_;_), !,
  912  list2or(HeadListOr, Head),
  913  process_head(HeadListOr, HeadList),
  914  list2and(BodyList, Body),
  915  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList1,M),
  916  append([onec(BDD)],BodyList1,BodyList2),
  917  list2and(BodyList2,Body1),
  918  append(HeadList,BodyList,List),
  919  term_variables(List,VC),
  920  get_next_rule_number(M,R),
  921  get_probs(HeadList,Probs),
  922  (M:local_pitaind_setting(single_var,true)->
  923    generate_rules(HeadList,Body1,[],R,Probs,BDDAnd,0,Clauses,M)
  924  ;
  925    generate_rules(HeadList,Body1,VC,R,Probs,BDDAnd,0,Clauses,M)
  926  ).
  927
  928pitaind_expansion((Head :- Body), []) :-
  929% 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
  930  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  931  ((Head:-Body) \= ((pitaind_expansion(_,_) ):- _ )),
  932  (Head = (_:P);Head=(P::_)),
  933  ground(P),
  934  P=:=0.0, !.
  935
  936pitaind_expansion((Head :- Body), Clauses) :-
  937% disjunctive clause with a single head atom e depth_bound
  938  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  939  M:local_pitaind_setting(depth_bound,true),
  940  ((Head:-Body) \= ((pitaind_expansion(_,_) ):- _ )),
  941  list2or(HeadListOr, Head),
  942  process_head(HeadListOr, HeadList),
  943  HeadList=[H:_],!,
  944  list2and(BodyList, Body),
  945  process_body_db(BodyList,BDD,BDDAnd,DB,[],_Vars,BodyList2,M),
  946  append([onec(BDD)],BodyList2,BodyList3),
  947  list2and([DBH>=1,DB is DBH -1|BodyList3],Body1),
  948  add_bdd_arg_db(H,BDDAnd,DBH,M,Head1),
  949  Clauses=(Head1 :- Body1).
  950
  951pitaind_expansion((Head :- Body), Clauses) :-
  952% disjunctive clause with a single head atom senza depth_bound con prob =1
  953  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  954   ((Head:-Body) \= ((pitaind_expansion(_,_) ):- _ )),
  955  list2or(HeadListOr, Head),
  956  process_head(HeadListOr, HeadList),
  957  HeadList=[H:_],!,
  958  list2and(BodyList, Body),
  959  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,M),
  960  append([onec(BDD)],BodyList2,BodyList3),
  961  list2and(BodyList3,Body1),
  962  add_bdd_arg(H,BDDAnd,M,Head1),
  963  Clauses=(Head1 :- Body1).
  964
  965pitaind_expansion((Head :- Body), Clauses) :-
  966% disjunctive clause with a single head atom e DB, con prob. diversa da 1
  967  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  968  M:local_pitaind_setting(depth_bound,true),
  969  ((Head:-Body) \= ((pitaind_expansion(_,_) ):- _ )),
  970  (Head = (H:_);Head=(_::H)), !,
  971  list2or(HeadListOr, Head),
  972  process_head(HeadListOr, HeadList),
  973  list2and(BodyList, Body),
  974  process_body_db(BodyList,BDD,BDDAnd,DB,[],_Vars,BodyList2,M),
  975  append([onec(BDD)],BodyList2,BodyList3),
  976  list2and(BodyList3,Body2),
  977  append(HeadList,BodyList,List),
  978  term_variables(List,VC),
  979  get_next_rule_number(M,R),
  980  get_probs(HeadList,Probs),%***test single_var
  981  (M:local_pitaind_setting(single_var,true)->
  982    generate_clause_db(H,Body2,[],R,Probs,DB,BDDAnd,0,Clauses,M)
  983  ;
  984    generate_clause_db(H,Body2,VC,R,Probs,DB,BDDAnd,0,Clauses,M)
  985  ).
  986
  987pitaind_expansion((Head :- Body), Clauses) :-
  988% disjunctive clause with a single head atom senza DB, con prob. diversa da 1
  989  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
  990  ((Head:-Body) \= ((pitaind_expansion(_,_) ):- _ )),
  991  (Head = (H:_);Head = (_::H)), !,
  992  list2or(HeadListOr, Head),
  993  process_head(HeadListOr, HeadList),
  994  list2and(BodyList, Body),
  995  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,M),
  996  append([onec(BDD)],BodyList2,BodyList3),
  997  list2and(BodyList3,Body2),
  998  append(HeadList,BodyList,List),
  999  term_variables(List,VC),
 1000  get_next_rule_number(M,R),
 1001  get_probs(HeadList,Probs),%***test single_vars
 1002  (M:local_pitaind_setting(single_var,true)->
 1003    generate_clause(H,Body2,[],R,Probs,BDDAnd,0,Clauses,M)
 1004  ;
 1005    generate_clause(H,Body2,VC,R,Probs,BDDAnd,0,Clauses,M)
 1006  ).
 1007
 1008/*pitaind_expansion((Head :- Body),Clauses) :-
 1009% definite clause for db facts
 1010  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1011  ((Head:-Body) \= ((pitaind_expansion(_,_)) :- _ )),
 1012  Head=db(Head1),!,
 1013  Clauses=(Head1 :- Body).
 1014*/
 1015pitaind_expansion((Head :- Body),Clauses) :-
 1016% definite clause with depth_bound
 1017  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1018  M:local_pitaind_setting(depth_bound,true),
 1019   ((Head:-Body) \= ((pitaind_expansion(_,_)) :- _ )),!,
 1020  list2and(BodyList, Body),
 1021  process_body_db(BodyList,BDD,BDDAnd,DB,[],_Vars,BodyList2,M),
 1022  append([onec(BDD)],BodyList2,BodyList3),
 1023  list2and([DBH>=1,DB is DBH-1|BodyList3],Body1),
 1024  add_bdd_arg_db(Head,BDDAnd,DBH,M,Head1),
 1025  Clauses=(Head1 :- Body1).
 1026
 1027pitaind_expansion((Head :- Body),Clauses) :-
 1028% definite clause senza DB
 1029  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1030  ((Head:-Body) \= ((pitaind_expansion(_,_)) :- _ )),!,
 1031  list2and(BodyList, Body),
 1032  process_body(BodyList,BDD,BDDAnd,[],_Vars,BodyList2,M),
 1033  append([onec(BDD)],BodyList2,BodyList3),
 1034  list2and(BodyList3,Body2),
 1035  add_bdd_arg(Head,BDDAnd,M,Head1),
 1036  Clauses=(Head1 :- Body2).
 1037
 1038pitaind_expansion(Head,Clauses) :-
 1039  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1040  M:local_pitaind_setting(depth_bound,true),
 1041% disjunctive FACT with more than one head atom e db
 1042  Head=(_;_), !,
 1043  list2or(HeadListOr, Head),
 1044  process_head(HeadListOr, HeadList),
 1045  term_variables(HeadList,VC),
 1046  get_next_rule_number(M,R),
 1047  get_probs(HeadList,Probs),
 1048  (M:local_pitaind_setting(single_var,true)->
 1049    generate_rules_fact_db(HeadList,[],R,Probs,0,Clauses,M)
 1050  ;
 1051    generate_rules_fact_db(HeadList,VC,R,Probs,0,Clauses,M)
 1052  ).
 1053
 1054pitaind_expansion(Head,Clauses) :-
 1055  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1056% disjunctive fact with more than one head atom senza db
 1057  Head=(_;_), !,
 1058  list2or(HeadListOr, Head),
 1059  process_head(HeadListOr, HeadList),
 1060  term_variables(HeadList,VC),
 1061  get_next_rule_number(M,R),
 1062  get_probs(HeadList,Probs), %**** test single_var
 1063  (M:local_pitaind_setting(single_var,true)->
 1064    generate_rules_fact(HeadList,[],R,Probs,0,Clauses,M)
 1065  ;
 1066    generate_rules_fact(HeadList,VC,R,Probs,0,Clauses,M)
 1067  ).
 1068
 1069pitaind_expansion(Head,Clauses) :-
 1070  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1071% disjunctive fact with uniform distr
 1072  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1073  Head = (_:P),
 1074  nonvar(P),
 1075  Head=(H:uniform(Var,D0)),!,
 1076  length(D0,Len),
 1077  Prob is 1.0/Len,
 1078  maplist(gen_head(H,Prob,Var),D0,HeadList),
 1079  get_next_rule_number(M,R),
 1080  get_probs(HeadList,Probs), %**** test single_var
 1081  (M:local_pitaind_setting(single_var,true)->
 1082    generate_rules_fact(HeadList,[],R,Probs,0,Clauses,M)
 1083  ;
 1084    generate_rules_fact_vars(HeadList,R,Probs,0,Clauses,M)
 1085  ).
 1086
 1087
 1088pitaind_expansion(Head,Clauses) :-
 1089  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1090% disjunctive fact with guassia distr
 1091  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1092  Head = (_:P),
 1093  nonvar(P),
 1094  (Head=(H:discrete(Var,D));Head=(H:finite(Var,D))),!,
 1095  maplist(gen_head_disc(H,Var),D,HeadList),
 1096  get_next_rule_number(M,R),
 1097  get_probs(HeadList,Probs), %**** test single_var
 1098  (M:local_pitaind_setting(single_var,true)->
 1099    generate_rules_fact(HeadList,[],R,Probs,0,Clauses,M)
 1100  ;
 1101    generate_rules_fact_vars(HeadList,R,Probs,0,Clauses,M)
 1102  ).
 1103
 1104pitaind_expansion(Head,[]) :-
 1105  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1106% disjunctive fact with a single head atom con prob. 0
 1107  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1108  (Head = (_:P); Head = (P::_)),
 1109  ground(P),
 1110  P=:=0.0, !.
 1111
 1112pitaind_expansion(Head,Clause) :-
 1113  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1114  M:local_pitaind_setting(depth_bound,true),
 1115% disjunctive fact with a single head atom con prob.1 e db
 1116  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1117  (Head = (H:P); Head = (P::H)),
 1118  ground(P),
 1119  P=:=1.0, !,
 1120  list2and([onec(BDD)],Body1),
 1121  add_bdd_arg_db(H,BDD,_DB,M,Head1),
 1122  Clause=(Head1 :- Body1).
 1123
 1124pitaind_expansion(Head,Clause) :-
 1125  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1126% disjunctive fact with a single head atom con prob. 1, senza db
 1127  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1128  (Head = (H:P);Head =(P::H)),
 1129  ground(P),
 1130  P=:=1.0, !,
 1131  list2and([onec(BDD)],Body1),
 1132  add_bdd_arg(H,BDD,M,Head1),
 1133  Clause=(Head1 :- Body1).
 1134
 1135pitaind_expansion(Head,Clause) :-
 1136  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1137  M:local_pitaind_setting(depth_bound,true),
 1138% disjunctive fact with a single head atom e prob. generiche, con db
 1139  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1140  (Head=(H:_);Head=(_::H)), !,
 1141  list2or(HeadListOr, Head),
 1142  process_head(HeadListOr, HeadList),
 1143  term_variables(HeadList,VC),
 1144  get_next_rule_number(M,R),
 1145  get_probs(HeadList,Probs),
 1146  add_bdd_arg_db(H,BDD,_DB,M,Head1),
 1147  (M:local_pitaind_setting(single_var,true)->
 1148    Clause=(Head1:-(get_var_n(M,R,[],Probs,V),equality(V,0,BDD)))
 1149  ;
 1150    Clause=(Head1:-(get_var_n(M,R,VC,Probs,V),equality(V,0,BDD)))
 1151  ).
 1152
 1153pitaind_expansion(Head,Clause) :-
 1154  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1155% disjunctive fact with a single head atom e prob. generiche, senza db
 1156  (Head \= ((pitaind_expansion(_,_)) :- _ )),
 1157  (Head=(H:_);Head=(_::H)), !,
 1158  list2or(HeadListOr, Head),
 1159  process_head(HeadListOr, HeadList),
 1160  term_variables(HeadList,VC),
 1161  get_next_rule_number(M,R),
 1162  get_probs(HeadList,Probs),
 1163  add_bdd_arg(H,BDD,M,Head1),%***test single_var
 1164  (M:local_pitaind_setting(single_var,true)->
 1165    Clause=(Head1:-(get_var_n(M,R,[],Probs,V),equality(V,0,BDD)))
 1166  ;
 1167    Clause=(Head1:-(get_var_n(M,R,VC,Probs,V),equality(V,0,BDD)))
 1168  ).
 1169
 1170pitaind_expansion((:- set_pitaind(P,V)), []) :-!,
 1171  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1172  set_pitaind(P,V).
 1173
 1174pitaind_expansion((:- set_sw(A,B)), []) :-!,
 1175  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1176  set_sw(M:A,B).
 1177
 1178
 1179pitaind_expansion(Head, (Head1:-onec(One))) :-
 1180  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1181  M:local_pitaind_setting(depth_bound,true),
 1182% definite fact with db
 1183  (Head \= ((pitaind_expansion(_,_) ):- _ )),
 1184  (Head\= end_of_file),!,
 1185  add_bdd_arg_db(Head,One,_DB,M,Head1).
 1186
 1187pitaind_expansion(Head, (Head1:-onec(One))) :-
 1188  prolog_load_context(module, M),pitaind_input_mod(M),M:pitaind_on,
 1189% definite fact without db
 1190  (Head \= ((pitaind_expansion(_,_) ):- _ )),
 1191  (Head\= end_of_file),
 1192  add_bdd_arg(Head,One,M,Head1).
 begin_lpad_pred is det
Initializes LPAD loading. /
 1199begin_lpad_pred:-
 1200  assert(pitaind_input_mod(user)),
 1201  assert(user:pitaind_on).
 end_lpad_pred is det
Terminates the cplint inference module. /
 1208end_lpad_pred:-
 1209  retractall(pitaind_input_mod(_)),
 1210  retractall(user:pitaind_on).
 1211
 1212list2or([],true):-!.
 1213
 1214list2or([X],X):-
 1215    X\=;(_,_),!.
 1216
 1217list2or([H|T],(H ; Ta)):-!,
 1218    list2or(T,Ta).
 1219
 1220
 1221list2and([],true):-!.
 1222
 1223list2and([X],X):-
 1224    X\=(_,_),!.
 1225
 1226list2and([H|T],(H,Ta)):-!,
 1227    list2and(T,Ta).
 1228
 1229
 1230builtin(average(_L,_Av)).
 1231builtin(prob(_,_)).
 1232builtin(G):-
 1233  predicate_property(G,built_in).
 1234builtin(G):-
 1235  predicate_property(G,imported_from(lists)).
 1236
 1237average(L,Av):-
 1238        sum_list(L,Sum),
 1239        length(L,N),
 1240        Av is Sum/N.
 1241
 1242:- multifile sandbox:safe_primitive/1. 1243
 1244
 1245:- multifile sandbox:safe_meta/2. 1246
 1247sandbox:safe_meta(pitaind:s(_,_), []).
 1248sandbox:safe_meta(pitaind:prob_ind(_,_), []).
 1249sandbox:safe_meta(pitaind:prob_bar(_,_), []).
 1250sandbox:safe_meta(pitaind:prob_ind(_,_,_), []).
 1251sandbox:safe_meta(pitaind:prob_bar(_,_,_), []).
 1252sandbox:safe_meta(pitaind:bdd_dot_file(_,_,_), []).
 1253sandbox:safe_meta(pitaind:bdd_dot_string(_,_,_), []).
 1254sandbox:safe_meta(pitaind:set_pitaind(_,_),[]).
 1255sandbox:safe_meta(pitaind:setting_pitaind(_,_),[]).
 1256
 1257
 1258:- thread_local pitaind_file/1. 1259
 1260user:term_expansion((:- pitaind), []) :-!,
 1261  prolog_load_context(source, Source),
 1262  asserta(pitaind_file(Source)),
 1263  prolog_load_context(module, M),
 1264  retractall(M:local_pitaind_setting(_,_)),
 1265  findall(local_pitaind_setting(P,V),default_setting_pitaind(P,V),L),
 1266  assert_all(L,M,_),
 1267  assert(pitaind_input_mod(M)),
 1268  retractall(M:rule_n(_)),
 1269  retractall(M:goal_n(_)),
 1270  assert(M:rule_n(0)),
 1271  assert(M:goal_n(0)),
 1272  M:(dynamic v/3),
 1273  style_check(-discontiguous).
 1274
 1275user:term_expansion(end_of_file, end_of_file) :-
 1276  pitaind_file(Source),
 1277  prolog_load_context(source, Source),
 1278  retractall(pitaind_file(Source)),
 1279  prolog_load_context(module, M),
 1280  pitaind_input_mod(M),!,
 1281  retractall(pitaind_input_mod(M)),
 1282  style_check(+discontiguous).
 1283
 1284user:term_expansion(In, Out) :-
 1285  \+ current_prolog_flag(xref, true),
 1286  pitaind_file(Source),
 1287  prolog_load_context(source, Source),
 1288  pitaind_expansion(In, Out)