-
Notifications
You must be signed in to change notification settings - Fork 4
/
canonical_horn.pro
99 lines (68 loc) · 1.61 KB
/
canonical_horn.pro
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
:-op(600,xfx,(<-)).
% prover for nested Horn, compiled from all except disjunction
holds(A):-A<-[].
A<-Vs:-memberchk(A,Vs),!.
(B<-As)<-Vs1:-!,append(As,Vs1,Vs2),B<-Vs2.
G<-Vs1:- % atomic(G), G not on Vs1
memberchk((G<-_),Vs1),
select((B<-As),Vs1,Vs2), % outer select loop
select(A,As,Bs), % inner select loop
holds_imp(A,B,Vs2), % A element of the body of B
!,
trimmed((B<-Bs),NewB), % trim empty bodies
G<-[NewB|Vs2].
holds_imp((D<-Cs),B,Vs):-!,(D<-Cs)<-[(B<-[D])|Vs].
holds_imp(A,_B,Vs):-memberchk(A,Vs).
trimmed((B<-[]),R):-!,R=B.
trimmed(BBs,BBs).
%%%%%%%%%
simple(X):-atomic(X),!.
simple(X):-var(X).
nvar(_,Xs,Xs).
hcan(A,R):-simple(A),!,R=A.
hcan((H<-Bs),(H<-Ds)):-hcans(0,Bs,Cs,Ds,Cs).
hcans(_,[],[])-->[].
hcans(K,[B|Bs],[C|Cs])-->
heq(K,B,C),
hcans(K,Bs,Cs).
heq(_,A,R)-->{simple(A)},!,{R=A}.
heq(K,(H<-Bs),R)-->
{K1 is K+1},
hcans(K1,Bs,Cs),
maybe_shallow(K,H,Cs,R).
maybe_shallow(K,H,Cs,R)-->
{T=(H<-Cs),depth(T,D),K+D<3},!,{R=T}.
maybe_shallow(_K,H,Cs,N)-->
nvar(N),
[(N<-(H<-Cs)),(H<-[N|Cs])].
depth(A,0):-simple(A),!.
depth(_<-Bs,D):-
maplist(depth,Bs,Ds),
max_list(Ds,D1),
D is 1+D1.
ppp(H<-Bs):-!,portray_clause((H:-Bs)).
ppp(X):-portray_clause(X).
ht(T):-
ppp('PROBLEM:'),
ppp(T),
hcan(T,C),
nl,ppp('ANSWER:'),
ppp(C),
nl,ppp('END!'),nl,
fail.
cht1:-
T=f<-[a,g<-[b<-[c,d<-[e,q],x],m],r<-[p,x]],
ht(T).
cht2:-
T=f<-[a,g<-[b<-[c,d,x],m],r<-[p,x]],
ht(T).
cht3:-
T=f<-[a,g<-[b<-[c,d<-[e<-[i,j],f<-[k]]],m],r<-[p,x]],
ht(T).
cht4:-
T= 0<-[0<-[0<-[0<-[0]]]],
ht(T).
chgo:-cht1;cht2;cht3;cht4.
go:-
T=x<-[g<-[g,h]],
holds(T).