-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_d.pl
50 lines (41 loc) · 1.11 KB
/
model_d.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
cl((append([],Y,Y):-list(Y))).
cl((append([X|Xs],Ys,[X|Zs]):-thing(X),append(Xs,Ys,Zs))).
cl((list([]):-true)).
cl((list([X|Y]):-thing(X),list(Y))).
cl((thing(a):-true)).
cl((thing(b):-true)).
%cl((thing(c):-true)).
model_d(0,M,M).
model_d(D,M0,M):-
D>0,D1 is D-1,
findall(H,is_violated(H,M0),Heads),
satisfy_clauses(Heads,M0,M1),
model_d(D1,M1,M).
satisfy_clauses([],M,M).
satisfy_clauses([H|Hs],M0,M):-
disj_element(L,H),
satisfy_clauses(Hs,[L|M0],M).
is_violated(H,M):-
cl((H:-B)),
satisfied_body(B,M), % this will ground the variables
not(satisfied_head(H,M)).
satisfied_body(true,_M). % body is a conjunction
satisfied_body(A,M):-
member(A,M).
satisfied_body((A,B),M):-
member(A,M),
satisfied_body(B,M).
satisfied_head(A,M):- % head is a disjunction
member(A,M).
satisfied_head((A;_B),M):-
member(A,M).
satisfied_head((_A;B),M):-
satisfied_head(B,M).
disj_element(X,X):- % single-element disjunction
not(X=false),not(X=(_;_)).
disj_element(X,(X;_Ys)).
disj_element(X,(_Y;Ys)):-
disj_element(X,Ys).
/** <examples>
?- model_d(4,[],M).
*/