Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I have two names for the same atom in Prolog?

Tags:

prolog

I want to have two names for the same atom in Prolog:

player(thomas).
player(william).
teamOfPlayer(thomas, redSocks).
teamOfPlayer(william, redSocks).
tom :- thomas.
will :- william.

teamOfPlayer(will, X).

I'd like to be able to refer to William using "william" atom and using "will" atom.

I know I could use a functor to define nicknames:

nick(tom, thomas).
nick(will, william).

and then,

nick(tom,X), teamOfPlayer(X, Y).

But I'd like to avoid all this verbosity.

like image 891
Ramillete Avatar asked Mar 14 '18 22:03

Ramillete


1 Answers

Implementation specific facilities are available for this purpose - and much more. Indeed, given that identity it's 'at heart' of logic, rewriting rules to fit different identification strategies is not an uncommon problem.

In SWI-Prolog, you can use the expansion hooks, more specifically goal_expansion/2.

In your module, add near the end-of-file (just a convention, though)

:- multifile user:goal_expansion/2.
user:goal_expansion(will, william).
user:goal_expansion(tom, thomas).

edit

Sorry, I didn't debugged my suggestion, and it turns out to be incorrect. A possible correction could be:

alias(will,william).
alias(tom,thomas).

:- multifile user:goal_expansion/2.
user:goal_expansion(player(X), player(Y)) :-
    alias(X,Y).
user:goal_expansion(teamOfPlayer(X,T), teamOfPlayer(Y,T)) :-
    alias(X,Y).

We could make the overloading rules more generic, but the problem is deeply rooted in language core. Atoms carry the fundamental property (from the relational data model viewpoint) of identity, then we can 'overload' an atom only in specific contexts.

like image 90
CapelliC Avatar answered Nov 06 '22 14:11

CapelliC