-
Notifications
You must be signed in to change notification settings - Fork 1
/
char.pl
executable file
·104 lines (93 loc) · 2.67 KB
/
char.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
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
99
100
101
102
103
104
/* characterisation */
char(InICs,PosIn,NegIn,OutICs,PosOut,NegOut):-
char1(InICs,PosIn,NegIn,[],TmpICs,PosOut,NegOut),
cleanup(TmpICs,[],OutICs).
char1([],P,N,Out,Out,P,N).
char1([IC|ICs],P0,N0,Acc,Out,P,N) :-
write_debug(['evaluating ',IC]),
evaluate(P0,N0,IC,Tuples,P1,N1,Answer),
( Answer = refine -> refinements(IC,Tuples,Spec),
append(ICs,Spec,NewICs),
NewAcc=Acc
; Answer = keep -> NewICs=ICs,NewAcc=[IC|Acc]
; Answer = ignore(E) -> NewICs=ICs,NewAcc=Acc
; Answer = keep(E) -> NewICs=ICs,insert_ic(Acc,IC,E,NewAcc)
),write_debug([' result: ',Answer]),
!,char1(NewICs,P1,N1,NewAcc,Out,P,N).
evaluate(P0,N0,IC,Tuples,P,N,Answer) :-
( evaluate1(P0,IC,Answer) -> P=P0,N=N0,Tuples=[]
; otherwise ->
% write_debug(['contr...']),
contr(P0,N0,IC,T,A),
( A = yes -> Answer = refine,P=P0,N=N0,Tuples=T
; A = no -> Answer = keep, P=P0,N=N0,Tuples=T
; A = possibly -> queries(P0,N0,T,P1,N1),
evaluate(P1,N1,IC,Tuples,P,N,Answer)
)
).
contr(P,N,IC,Tuples,Answer) :-
horn(IC,HornIC),
incons(HornIC,P,N,Tuples,Answer).
queries(P,N,[],P,N).
queries(P,N,[T|Ts],P1,N1):-
query(P,N,T,P2,N2),
queries(P2,N2,Ts,P1,N1).
query(P,N,-T,[T|P],N) :-
not switched_on(cwa),yesno(['Is ',T,' in the relation? ']),!.
query(P,N,-T,P,[T|N]):-
not switched_on(cwa).
query(P,N,-T,P,[T|N]):-
switched_on(cwa),write(-T),nl.
query(P,N,+T,P,N).
cleanup([X|In],Acc,Out):-
member(IC,In),subsumed(X,IC),!,
cleanup(In,Acc,Out).
cleanup([X|In],Acc,Out):-
member(IC,Acc),subsumed(X,IC),!,
cleanup(In,Acc,Out).
cleanup([X|In],Acc,Out):-
cleanup(In,[X|Acc],Out).
cleanup([],Out,Out).
incons((Head:-Body),P,N,Tuples,Answer):-
satisfied(Body,P,N,TuplesB),
falsified(Head,P,N,TuplesH),!,
Answer=yes,
append(TuplesB,TuplesH,Tuples).
incons((Head:-Body),P,N,Tuples,Answer):-
satisfied(Body,P,N,TuplesB),
unsatisfied(Head,P,N,TuplesH),!,
Answer=possibly,
append(TuplesB,TuplesH,Tuples).
incons((Head:-Body),P,N,[],Answer):-
Answer=no.
satisfied((A,B),P,N,Tuples):-
satisfied(A,P,N,TuplesA),
satisfied(B,P,N,TuplesB),
append(TuplesA,TuplesB,Tuples).
satisfied((A;B),P,N,Tuples):-
satisfied(A,P,N,Tuples)
; satisfied(B,P,N,Tuples).
satisfied(A,P,N,[+A]):-
member(A,P).
satisfied(A=A,P,N,[]). % fds only
satisfied(A,P,N,[]):-
proc(Rel,Proc),
exec_proc(A,Proc).
exec_proc(Goal,(P1,P2)):-
exec_proc(Goal,P1)
; exec_proc(Goal,P2).
exec_proc(Goal,(Goal:-Body)):-
call(Body).
falsified((A,B),P,N,Tuples):-!,
falsified(A,P,N,Tuples)
; falsified(B,P,N,Tuples).
falsified(A,P,N,[-A]):-
member(A,N).
falsified(A=B,P,N,[]):- % fds only
A \= B.
unsatisfied((A,B),P,N,Tuples):-!,
unsatisfied(A,P,N,Tuples)
; unsatisfied(B,P,N,Tuples).
unsatisfied(A,P,N,[-A]):-
\+ satisfied(A,P,N,_),
\+ falsified(A,P,N,_).