[mercury-users] Partially instantiated modes
Thomas Conway
conway at cs.mu.OZ.AU
Thu Nov 11 15:23:03 AEDT 1999
Hi
A couple of months ago there was a brief threadlet about whether partially
instantiated modes were really useful, and/or what they were useful for.
The other day I came across a situation where they would be really useful:
If you have a mercury program preprocessor that has to create a wrapper
procedure around a given goal, and has to introduce some variables. I
ended up with some code that looked like:
...
varset__new_named_var(VS7, "XX1", XX1, VS8),
varset__new_named_var(VS8, "XX2", XX2, VS9),
varset__new_named_var(VS9, "XX3", XX3, VS10),
varset__new_named_var(VS10, "XX4", XX4, VS11),
varset__new_named_var(VS11, "XX5", XX5, VS12),
CLAUSE = functor(atom(":-"), [HEAD, BODY], Ctxt),
HEAD = functor(atom("thingumy"), [variable(XX1), variable(XX2), ...],
Ctxt),
BODY = ...
mentioning the created variables and some other bits and
pieces that were the read in terms WRT the varset VSnn
It's horrible to maintain because adding more variables is difficult
because of all those VSnns that have to be changed, and higher order
doesn't really help:
map_foldl((pred(Name::in, Var::out, VSx::in, VSy::out) is det :-
varset__new_named_var(VSx, Name, Var, VSy)
), ["XX1", "XX2", ...],
[XX1, XX2, ...], VS7, VS8)
The problem is that unification of the output with the list of variables
is semidet, so putting the problem _could_ be fixed with an if-then-else,
but that's kind of awful too.
However, if I could write:
:- mode fill_in_snd = (listskel(pair(ground, free)) -> ground).
:- pred makevars(list(pair(string, var)), varset, varset).
:- mode makevars(fill_in_snd, in, out) is det.
makevars([], VS, VS).
makevars([Name - Var|Rest], VS0, VS) :-
varset__new_named_var(VS0, Name, Var, VS1),
makevars(Rest, VS1, VS).
Then I can use it in the following way:
makevars(["XX1" - XX1, "XX2" - XX2, ...], VS7, VS8)
This is good because it is easily extensible, where the original
was not.
Here the win is not in runtime efficiency, but programmer efficiency.
Comments?
--
Thomas Conway )O+ Every sword has two edges.
Mercurian <conway at cs.mu.oz.au>
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list