alias to rename objects

Suggestions for new Logtalk features

Moderator: Paulo Moura

Post Reply
Parker
Posts: 33
Joined: Wed Feb 27, 2008 2:51 pm

alias to rename objects

Post by Parker » Mon Mar 08, 2010 3:54 pm

Here is a quick idea, not at all urgent.

I recently used two tree data-structure implementations (rbtree and bintree) that both satisfy the dictionaryp protocol. When swapping from one to the other, each call had to be renamed:

e.g.

Code: Select all

rbtree::lookup(Point, Value, ClassificationsTree)
had to be renamed to

Code: Select all

bintree::lookup(Point, Value, ClassificationsTree)
Is there a mechanism that avoids renaming each call? If not it might be helpful to have a one, for example an alias/2 directive:

Code: Select all

% switch tree implementation by uncommenting appropriate line
:- alias(mytree, bintree).
%:- alias(mytree, rbtree).
[...]
mytree::lookup(Point, Value, ClassificationsTree)
Cheers,
Parker

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Mon Mar 08, 2010 5:45 pm

Hi Parker,

I have toyed with the idea of object aliases several times in the past. Your example illustrates using object aliases within an entity but it might also be interesting to support global object aliases. Reusing your example, you could define a global object alias, e.g. in your code loader file:

Code: Select all

:- alias(bintree, tree).
This alias would allow all references to the object "tree", in all loaded code, to be translated to references to the object "bintree". One advantage would be that we could write an application using e.g. generic names such as "tree" or "queue" and easily experiment with different implementations of the aliased objects.

Note, however, that you could simplify your example by using instead a uses/2 directive. In this solution, you only need to change the first argument to get the same functionality of object aliases.

I have just implemented a first version of the alias/2 entity directive you suggested. This first version doesn't support global object aliases (which require a different compilation and implementation solution). In addition, it doesn't cover the case where a message sending call, constructed at runtime, targets the object alias. Doing so would require retaining the alias information in the compiled code (as with the alias/3 predicate directive). I need to think a bit more on the consequences of adding the alias/2 before committing a final implementation but I can give you an updated Logtalk compiler if you want to try this first version.

Thanks for your suggestion. I will post updates on this thread. Cheers,

Paulo
Paulo Moura
Logtalk developer

Parker
Posts: 33
Joined: Wed Feb 27, 2008 2:51 pm

Re: alias to rename objects

Post by Parker » Mon Mar 08, 2010 6:55 pm

Hi Paulo,

I did say it wasn't urgent, but wow you've already got an initial implementation! Hmm... maybe I should try some reverse psychology for the features I really want. I'll be sending you a list of things I really really don't want very soon. :lol:

Object aliases within an entity make sense to me. But if **global** object aliases are only for switching between implementations of an ADT, I think they are a bit of a strange idea and I can't grasp their usefulness. Choosing an implementation for an ADT is normally a local choice, not an application-wide one.

Cheers,
Parker

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Mon Mar 08, 2010 7:27 pm

Well... implementing local object aliases (with the restrictions I talked earlier) is quite trivial: six small predicate clauses and minor modifications to some compiler internal predicates. However, I'm not fully convinced that adding yet another directive to Logtalk is the best course. Consider (from your example):

Code: Select all

:- alias(bintree, tree).
% :- alias(rbtree, tree).

..., tree::new(Tree), ...
versus:

Code: Select all

:- uses(bintree, [lookup/4, insert/4, new/1, empty/1]).

..., new(Tree), ...
In the first case, you need to edit the code (or use conditional compilation directives) in order to switch from "bintree" to "rbtree". In the second case, you need to edit the code in order to change the first argument of the uses/2 directive from "bintree" to "rbtree".

Cheers,

Paulo
Paulo Moura
Logtalk developer

Parker
Posts: 33
Joined: Wed Feb 27, 2008 2:51 pm

Re: alias to rename objects

Post by Parker » Tue Mar 09, 2010 8:16 am

The downside of the uses/2 directive is that you have to track which predicates are being used.

I think object aliases are an elegant way to swap between ADT implementations, but I also understand that you don't want to have too many directives.

Thanks for considering it, Paulo.

Parker

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Tue Mar 09, 2010 12:38 pm

I have still not found a good solution to retain the object alias information in the code that is generated when object are compiled. This is necessary to avoid problems when calling meta-predicates with arguments such as Alias::Closure or Alias::Goal. If the object holding the meta-predicate definitions is compiled *after* the object defining the object aliases (so, no static binding for the meta-predicate calls) and using the explicit qualified meta-arguments above, the result will be a runtime object existence error. Or worse, if Alias happens to be also the identifier of a third object (given that aliases are local, this is actually possible). I have not given up the idea of an alias/2 directive but I need to avoid these potential bugs.
Paulo Moura
Logtalk developer

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Tue Mar 09, 2010 5:53 pm

The obvious solution for *runtime* handling of object aliases is to catch object existence errors when sending a message and to check if the sender defines an object alias before throwing an exception. The problem with this solution is that it doesn't work for object aliases defined in categories (accessing category-defined object aliases at runtime would be cumbersome and inefficient).

Making object aliases only available at compile time is always a solution but I don't like this kind of restrictions and half-backed solutions.
Paulo Moura
Logtalk developer

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Wed Mar 10, 2010 3:09 pm

On the other hand, the restriction of object aliases to compile time is exactly the same restriction found on the uses/2 predicate directive... currently I'm looking to the alias/3 predicate directive. I may prefer to use an alias/2 to simplify some uses of the the alias/3 directive.
Paulo Moura
Logtalk developer

Parker
Posts: 33
Joined: Wed Feb 27, 2008 2:51 pm

Re: alias to rename objects

Post by Parker » Wed Mar 10, 2010 4:04 pm

Compile-time aliases sound good to me. I can't think of any (sensible) situation where it would be useful to change the implementation of an ADT at run-time; or any other application of run-time object aliases for that matter.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Sat Mar 13, 2010 2:20 pm

Implemented in r5357. Feedback appreciated.
Paulo Moura
Logtalk developer

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Sun Mar 28, 2010 11:47 pm

Paulo Moura wrote:On the other hand, the restriction of object aliases to compile time is exactly the same restriction found on the uses/2 predicate directive... currently I'm looking to the alias/3 predicate directive. I may prefer to use an alias/2 to simplify some uses of the the alias/3 directive.
The restriction to compile-time only for the uses/2 directive is lifted in r5388 and it will be in the Logtalk 2.39.1 stable version.
Paulo Moura
Logtalk developer

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: alias to rename objects

Post by Paulo Moura » Mon Mar 29, 2010 12:11 am

Paulo Moura wrote:Implemented in r5357. Feedback appreciated.
Support for the experimental alias/2 directive is removed in r5392. This directive could be made safe for runtime use (with message sending performance penalty) when used in objects but not when used in categories. It would also be the only directive restricted to compile-time use, now that uses/2 is safe for runtime use.
Paulo Moura
Logtalk developer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest