Search code examples
variablesprolog

Do Prolog variables within 'nested' predicates co-refer?


In the following code, I have two occurrences of the 'state' predicate within a clause for the 'can_do' predicate:

can_do(state(Photographer_location, Camera_location, Holding_camera_or_not, CameraState, Last_place_photographed),
       go(Photographer_location, Place),
       state(Place, Camera_location, Holding_camera_or_not, CameraState, Last_place_photographed)) :-
    place(Place),
    Photographer_location \= Place.

Now, I know that within a given clause two identical variables (two occurrences of X, say) refer to the same thing. Does the same thing hold, though, when we have a more complicated predicate like this? On the one hand I'm tempted to think 'yes' because, for example, both instances of 'Holding_camera_or_not' are within the can_do clause, but on the other hand I'm tempted to think 'no' because, in theory, we have two different clauses (even if occurring within a larger clause) for state, so Prolog won't recognise the variables as the same. It will be terribly inconvenient if it doesn't: I'll have to enforce it all one by one!


Solution

  • Yes, they do refer to the same thing. Test it like this:

     can_do(state(a,b,c,d,e),go(a,p),X).
    

    having first asserted place(p).

    It would indeed be a disaster if a variable in a clause didn't mean the same thing from one reference to another.