-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesercizio63.pl
81 lines (74 loc) · 2.46 KB
/
esercizio63.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
% ------------------
% ** ESERCIZIO 63 **
% ------------------
% Sia dato un database di fatti del tipo costante(C), variabile(V), funzione(F,Arità) e predicato(P,Arità),
% dove C, V, F e P sono istanziabili ad atomi qualsiasi e Arità é istanziabile ad
% un intero qualsiasi. L’interpretazione ovvia é che detti atomi costituiscono l’alfabeto di un linguaggio
% del 1° ordine L. Scrivere un programma PROLOG termine(X) che prenda in X una struttura e
% termini con successo se essa rappresenta un termine di L. Rappresentando inoltre i connettivi logici
% non, or, and e -> come operatori PROLOG (con le dovute precedenze) ed i quantificatori all(V,F) e
% exists(V,F) come funtori PROLOG, dove V ed F sono rispettivamente indice e campo d’azione del
% quantificatore, si scriva un programma PROLOG fbf(X) che prenda in X una struttura e termini con
% successo se essa rappresenta una formula ben formata di L. Ad esempio, con il seguente database si
% ottengono le risposte riportate di seguito:
costante(a).
costante(b).
costante(c).
costante(d).
funzione(f,1).
funzione(g,2).
funzione(h,3).
funzione(i,4).
predicato(p,1).
predicato(q,2).
predicato(r,3).
predicato(s,4).
variabile(x).
variabile(y).
variabile(z).
% ?- fbf(all(x,p(x) -> q(a,g(x,b))))
% true
% ?- fbf(exists(y,all(x,p(x) -> q(a,g(x,b)))))
% true
% ?- fbf(r(a,b,c) or exists(y,all(x,p(x) -> q(a,g(x,b)))))
% true
% ?- fbf(r(a,b,c,d) or exists(y,all(x,p(x) -> q(a,g(x,b)))))
% false
% ?- fbf(all(x,r(a,b,c)) or exists(y,all(x,p(x) -> q(a,g(x,b)))))
% true
% ?- fbf(all(x,h(a,b,c)) or exists(y,all(x,p(x) -> q(a,g(x,b)))))
% false
% ?- fbf(h(a,b,c) or exists(y,all(x,p(x) -> q(a,g(x,b)))))
% false
% ?- fbf(r(a,b,c) or exists(y,all(x,p(x) -> q(a,q(x,b)))))
% false
:- op(30, fx, non).
:- op(100, yfx, or).
:- op(100, yfx, and).
:- op(1200, yfx, ->).
:- op(1200, yfx, <->).
termine(X) :-
costante(X).
termine(X) :-
variabile(X).
termine(X) :-
X =.. [T|C],
functor(X, T, N),
funzione(T, N),
termini(C).
termini([]).
termini([T|C]) :-
termine(T),
termini(C).
fbf(X) :-
X =.. [T|C],
functor(X, T, N),
predicato(T, N),
termini(C), !.
fbf(all(V, F)) :- variabile(V), fbf(F).
fbf(exists(V, F)) :- variabile(V), fbf(F).
fbf(non X) :- fbf(X).
fbf(X or Y) :- fbf(X), fbf(Y).
fbf(X and Y) :- fbf(X), fbf(Y).
fbf(X -> Y) :- fbf(X), fbf(Y).
fbf(X <-> Y) :- fbf(X), fbf(Y).