prologswi-prolog

Prolog Syntax for Dependent Conditions


First off, I am new to Prolog, so please go easy on me... Also, I am using SWI-Prolog, so I want to use the correct syntax for SWI.

In this case, I want to define step-parents (specifically step-mother and step-father). My current syntax is this:

isStepFather(X, Y):-
    dif(X, Y),
    isMale(X),
    not(isParent(X, Y)),
    isMother(Z, Y), isHusband(X, Z).

First off, I know that sometimes "not" is "\=" or some other slash combination, so is there a better version of my "not" statement? Also, the last line is supposed to read like: "X is a StepFather of Y, if Z is a mother of Y, and X is a Husband of Z."

I want to specify the 2 Z-statements as a single idea/condition, so I put them on the same row, but does that matter? Or is there a better way of chaining two conditions that are linked to each other, but not necessarily to the other conditions?

Finally, would it be better/more elegant to use isWife(Z, X) instead of isHusband(X, Z) so that Z stays in the front of both statements?

Please let me know your feedback!


Solution

  • 2 reasonable ways spring to mind:

    stepfather_child(SF, C) :-
        mother_child(M, C),
        husband_wife(SF, M),
        \+ father_child(SF, C).
    

    ... and:

    stepfather_child_alt(SF, C) :-
        dif(F, SF),
        mother_father_child(M, F, C),
        husband_wife(SF, M).
    

    Some programming notes: