prologtransitive-closureknowledge-management

In Prolog, how to make comparison when order is defined in a chain of predicates?


Having the following definition:

biggerThan(a,b).
biggerThan(b,c).
biggerThan(c,d).

How to define a rule is_bigger(X,Y), such that is_bigger(a,c) and is_bigger(a,d) will return true.

Besides, I am very new to Prolog. Did the title properly addressed the problem, if not, how should I say is?


Solution

  • Simply define is_bigger as the transitive closure of the biggerThan relation:

    biggerThan(a,b).
    biggerThan(b,c).
    biggerThan(c,d).
    
    is_bigger(X, Y) :- biggerThan(X, Y).
    is_bigger(X, Y) :- biggerThan(X, Z), is_bigger(Z, Y).
    

    The transitive closure of a relation R happens to be the smallest relation X such that R is contained in X (this is the first clause of the definition), and such that X = R o X (this is the second part). So, the above is basically one possible definition written out.