[comp.lang.prolog] Syllogisms

cdsm@doc.ic.ac.uk (Chris Moss) (10/26/89)

I have never seen a Prolog program for reasoning with syllogisms, and my
previous attempts to express Venn diagrams failed, so I was pleased to
find an algorithm in Merrilee Salmon's well illustrated book. If one has
been published I'd be interested to hear of it. Anyway here is my version
based on that book. 

It's fun to use, particularly backwards, to supply missing premises. 
It can be run with open arguments. Obviously if one was using it seriously
one would simply run it once and assert the resulting templates. There's
a predicate "macroSyllogism" to do that. There's also a "test" predicate
at the end with a few examples.

Chris Moss.
------------------------------------------------------------------

%syllogism links 3 categorical sentences of the form: 
%   all(a,b), no(a,b), some(a,b) or someNot(a,b) 
% C Chris Moss, Imperial College,1989 cdsm@doc.ic.ac.uk
% Permission given to copy, keeping  notices

syllogism(PremiseA, PremiseB, Conclusion) :-
    syllogisticForm(PremiseA, PremiseB, Conclusion, S, P, M),
    valid(PremiseA, PremiseB, Conclusion, S, P, M).

%identify the subject and predicate of the conclusion 
%of a syllogism and the middle term
syllogisticForm(PremiseA, PremiseB, Conclusion, S, P, M) :-
    categorical(PremiseA, S1, P1),
    categorical(PremiseB, S2, P2),
    categorical(Conclusion, S, P),
    figure(S1,P1,S2,P2, S, P, M).

%test for validity given in M.H. Salmon: Introduction to Logic and
%    Critical thinking. Harcourt Brace Jovanovich, 1984.
%adapted from J.T.Culbertson: Mathematics and Logic for Digital Devices 1958
valid(PremiseA, PremiseB, Conclusion, S, P, M) :-
    distributedOnce(M, PremiseA, PremiseB),
    distributedIff(S, PremiseA, PremiseB, Conclusion),
    distributedIff(P, PremiseA, PremiseB, Conclusion),
    checkQuality(PremiseA, PremiseB, Conclusion).

%the four types of categorial statement - A, E, I, O
categorical(all(S,P), S, P).
categorical(no(S,P), S, P).
categorical(some(S,P), S,P).
categorical(someNot(S,P), S, P).

%check there are 3 terms and identify the middle term
figure(S,M, M,P, S,P, M).
figure(S,M, P,M, S,P, M).
figure(M,S, M,P, S,P, M).
figure(M,S, P,M, S,P, M).
figure(M,P, S,M, S,P, M).
figure(P,M, S,M, S,P, M).
figure(M,P, M,S, S,P, M).
figure(P,M, M,S, S,P, M).

%an argument is distributed if it applies to all members of the class
%identities are used to allow queries with variables
distributed(S, all(S1,_)) :- S==S1.
distributed(S, no(S1, _)) :- S==S1.
distributed(P, no(_, P1)) :- P==P1.
distributed(P, someNot(_, P1)) :- P==P1.

undistributed(P, all(_,P1)) :- P==P1.
undistributed(P, some(_,P1)) :- P==P1.
undistributed(S, some(S1,_)) :- S==S1.
undistributed(S, someNot(S1,_)) :- S==S1.

distributedOnce(Term, T1,T2) :-
    distributed(Term,T1), undistributed(Term,T2)
    ; distributed(Term,T2), undistributed(Term,T1).

distributedIff(Term, A,B,C) :-
    distributed(Term, C),
    (distributed(Term,A) ; distributed(Term,B)).
distributedIff(Term,A,B,C) :-
    undistributed(Term,C), 
    (undistributed(Term,B) ; undistributed(Term, A)).

checkQuality(A,B,C) :-
    negative(C), 
       (negative(A), positive(B)
        ; negative(B), positive(A)).
checkQuality(A,B,C) :-
   positive(C), positive(B), positive(A).

negative(no(_,_)).
negative(someNot(_,_)).

positive(all(_,_)).
positive(some(_,_)).

%use this to macroprocess the program for faster action
macroSyllogism :-
    forall(syllogism(A,B,C), assert(syl(A,B,C))).

test(Pass,A,B,C,Result) :-
    example(A,B,C, Result),
    (syllogism(A,B,C) -> 
       (Result=valid -> Pass=ok ; Pass = fails)
    ; syllogisticForm(A,B,C,_,_,_) -> 
       (Result=invalid -> Pass=ok ; Pass = fails)
    ; (Result= notSyllogism-> Pass=ok ; Pass=fails)).

%taken from Merrilee Salmon
example(all(sensitivePeople,dreamers), all(dreamers, poets),
     all(sensitivePeople, poets), valid).
example(all(statesmen, honorable), some(honorable,politicians),
    some(statesmen, politicians), invalid).
example(no(unicorns, black), some(black, dogs), no(unicorns, dogs), invalid).
example(no(intelligent, gluttons), some(famous, gluttons),
    someNot(famous, intelligent), valid).
example(some(animals, furry), some(furry, cats),
    some(animals, cats), invalid).
example(some(runners,vegetarians),someNot(vegetarians,runners), 
    someNot(runners,vegetarians),notSyllogism).
------------------------------------------------------------------