Higher order goals? #183
-
Suppose I have objects of type room, which in turn can be connected to other rooms via a door predicate. To keep queries from going into infinite loops, we view these doors as directional, and then write a rule that determines if these rooms are connected. You may recognize this situation from Amzi! prolog's nanisrch example. :) In vanilla prolog, this is very straight forward. room(kitchen).
room(cellar).
door(kitchen, cellar).
connect(X, Y) :-
door(X, Y).
connect(X, Y) :-
door(Y, X). Rendering these ideas into logtalk, I get something like :- object(room).
:- public(door_to/1).
:- mode(door_to(?room),zero_or_more).
:- public(connected_to/1).
connected_to(To) :-
::door_to(To).
connected_to(To) :-
self(From),
To::door_to(From).
:- end_object.
:- object(cellar, extends(room) ).
:- end_object.
:- object(kitchen, extends(room) ).
door_to(cellar).
:- end_object. yielding [debug] 5 ?- kitchen::door_to(cellar).
true.
[debug] 6 ?- kitchen::connected_to(cellar).
true .
[debug] 7 ?- cellar::connected_to(kitchen).
true.
[debug] 8 ?- cellar::connected_to(X).
! Instantiation error
! in goal: A::door_to(cellar)
! with execution context:
! entity: room
! sender: user
! this: room
! self: cellar
! meta-call context: []
! coinduction stack: [] In prolog this doesn't require any sort of higher order goal, but in logtalk I don't see a straight forward way of querying X::door_to(Y). I've probably missed something, so I thought I'd check in with the maestro. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Can you keep rooms and doors as Prolog facts? Or do you have more to say about rooms and doors? |
Beta Was this translation helpful? Give feedback.
P.S. I was thinking about a solution using parametric objects and parametric objects proxies. See the
proxies
example.But going back to your code, the usual solution is to define a marker protocol that your rooms will implement and then call
implements_protocol(Room, room_protocol)
in yourconnected_to/1
predicate to enumerate rooms and thus avoid that instantiation error.