/* EMBLEM and SLIPCASE Copyright (c) 2011, Fabrizio Riguzzi, Nicola di Mauro and Elena Bellodi */ :- use_module(library(terms)). :- use_module(library(lists)). :- set_prolog_flag(discontiguous_warnings,on). :- set_prolog_flag(single_var_warnings,on). theory_revisions_op(Theory,TheoryRevs):- setof(RevOp, Theory^revise_theory(Theory,RevOp), TheoryRevs),!. theory_revisions_op(_Theory,[]). theory_revisions(Theory,TheoryRevs):- theory_revisions_op(Theory,TheoryRevs1), apply_operators(TheoryRevs1,Theory,TheoryRevs). apply_operators([],_Theory,[]). apply_operators([add(Rule)|RestOps],Theory,[NewTheory|RestTheory]) :- append(Theory, [Rule], NewTheory), % nl,write(NewTheory), apply_operators(RestOps,Theory,RestTheory). apply_operators([add_body(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :- delete_matching(Theory,Rule1,Theory1), append(Theory1, [Rule2], NewTheory), % nl,write(NewTheory), apply_operators(RestOps,Theory,RestTheory). apply_operators([remove_body(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :- delete_matching(Theory,Rule1,Theory1), append(Theory1, [Rule2], NewTheory), % nl,write(NewTheory), apply_operators(RestOps,Theory,RestTheory). apply_operators([add_head(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :- delete_matching(Theory,Rule1,Theory1), append(Theory1, [Rule2], NewTheory), % nl,write(NewTheory), apply_operators(RestOps,Theory,RestTheory). apply_operators([remove_head(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :- delete_matching(Theory,Rule1,Theory1), append(Theory1, [Rule2], NewTheory), % nl,write(NewTheory), apply_operators(RestOps,Theory,RestTheory). apply_operators([remove(Rule)|RestOps],Theory,[NewTheory|RestTheory]) :- delete_matching(Theory,Rule,NewTheory), % nl,write(NewTheory), apply_operators(RestOps,Theory,RestTheory). revise_theory(Theory,Ref):- specialize_theory(Theory,Ref). revise_theory(Theory,Ref):- generalize_theory(Theory,Ref). generalize_theory(Theory,Ref):- Theory \== [], choose_rule(Theory,Rule), generalize_rule(Rule,Ref). generalize_theory(Theory,Ref):- length(Theory,LT), setting(max_rules,MR), LT delete_matching(LH,'':PNull,LH0), append(LH0,[PH:0.0,'':PNull],LH1) ; length(LH,NH), add_to_head(LH,NH,PH,LH1) ). generalize_head2([_X|R],LH,LH1) :- generalize_head2(R,LH,LH1). add_to_head(['':PN],NH,At,[At:PA,'':PN1]):-!, PN1 is PN*NH/(NH+1), PA is 1/(NH+1). add_to_head([H:PH|T],NH,At,[H:PH1|T1]):- PH1 is PH*NH/(NH+1), add_to_head(T,NH,At,T1). get_module_var(LH,Module):- member(H:_,LH),!, H=..[_F,Module|_]. generalize_body(Rule,Ref):- Rule = rule(ID,LH,BL), delete_one(BL,BL1,A), remove_prob(LH,LH1), delete(LH1,'',LH2), linked_clause(BL1,LH2), Ref = remove_body(Rule,rule(ID,LH,BL1),A). specialize_theory(Theory,Ref):- Theory \== [], choose_rule(Theory,Rule), specialize_rule(Rule,SpecRule,Lit), Ref = add_body(Rule,SpecRule,Lit). %used by cycle_clauses in slipcover.pl specialize_rule(Rule,SpecRule,Lit):- setting(specialization,bottom), Rule = rule(ID,LH,BL,Lits), delete_one(Lits,RLits,Lit), \+ lookahead_cons(Lit,_), \+ lookahead_cons_var(Lit,_), \+ member_eq(Lit,BL), append(BL,[Lit],BL1), remove_prob(LH,LH1), delete(LH1,'',LH2), append(LH2,BL1,ALL2), dv(LH2,BL1,DList), %-DList: list of couples (variable,depth) extract_fancy_vars(ALL2,Vars1), length(Vars1,NV), setting(max_var,MV), NV=2, delete_one(LH,LH1,Lit), %deletes Lit Lit\=' ', update_head1(LH1,L-1,LH2). %updates parameters update_head1([],_N,[]):-!. update_head1([H:_P|T],N,[H:P|T1]):- P is 1/N, update_head1(T,N,T1). write_list([A]):-!, format("\t~p.~n~n",[A]). write_list([A|T]):- format("\t~p,",[A]), write_list(T). banned_clause(H,B):- numbervars((H,B),0,_N), banned(H2,B2), mysublist(H2,H), mysublist(B2,B). mysublist([],_). mysublist([H|T],L):- member(H,L), mysublist(T,L). check_ref(H,B):- copy_term((H,B),(H1,B1)), numbervars((H1,B1),0,_N), (ref(H1,B1)-> fail ; assert(ref(H1,B1)) ). specialize_rule([Lit|_RLit],Rule,SpecRul,SLit):- Rule = rule(ID,LH,BL,true), remove_prob(LH,LH1), append(LH1,BL,ALL), specialize_rule1(Lit,ALL,SLit), append(BL,[SLit],BL1), (lookahead(SLit,LLit1);lookahead_cons(SLit,LLit1)), specialize_rule_la(LLit1,LH1,BL1,BL2), append(LH1,BL2,ALL2), extract_fancy_vars(ALL2,Vars1), length(Vars1,NV), setting(max_var,MV), NV=MD0,!, get_max(T,MD,DsH,Ds). get_max([_H|T],MD,Ds0,Ds):- get_max(T,MD,Ds0,Ds). output_vars(OutVars,[],OutVars):-!. output_vars(BodyAtomVars,[I|InputVars],OutVars):- delete(BodyAtomVars, I, Residue), output_vars(Residue,InputVars, OutVars). % returns D as the maximum depth of the variables in the list (first argument) depth_InputVars([],_,D,D). depth_InputVars([I|Input],PrevDs,D0,D):- member_l(PrevDs,I,MD), (MD>D0-> D1=MD ; D1=D0 ), depth_InputVars(Input,PrevDs,D1,D). member_l([[L,D]|_P],I,D):- I==L,!. member_l([_|P],I,D):- member_l(P,I,D). compute_depth([],_,PD,PD):-!. compute_depth([O|Output],D,PD,RestO):- member_l(PD,O,_),!, compute_depth(Output,D,PD,RestO). compute_depth([O|Output],D,PD,[[O,D]|RestO]):- compute_depth(Output,D,PD,RestO). %checks if a variable's depth exceeds the setting exceed_depth([],_):-!. exceed_depth([H|T],MD):- nth1(2,H,Dep), Dep