[mercury-users] List instantiation question
Peter Hawkins
hawkinsp at cs.stanford.edu
Thu Mar 2 17:33:01 AEDT 2006
Hi...
On Mar 1, 2006, at 9:51 PM, Ralph Becket wrote:
>
>> Is there some list mode that I am not aware of that solves this
>> issue?
>
> Not really.
>
> Whenever someone tries to do something Prologesque like this it's
> usually an efficiency hack.
Actually, my personal desire for partial instantiation comes when
trying to be excessively tricky. My most recent example was when
trying to write a grammar that can be used for both parsing and
generation. The idea was something like this (and I thought it was
one of my more evil ideas for some time =) ):
:- type stream ---> input(...) ; output(...).
:- inst input_stream ---> input(...).
:- inst output_stream ---> output(...).
:- type thing ---> foo(int) ; bar(string).
:- inst partial_thing ---> foo(free) ; bar(free).
:- pred read_or_write_thing(thing, stream, stream).
:- mode read_or_write_thing(out, in(input_stream), out(input_stream))
is det.
:- mode read_or_write_thing(in, in(output_stream), out
(output_stream)) is det.
read_or_write_thing(X) -->
read_or_write_which_of_foo_or_bar(X),
(
{ X = foo(A) },
read_or_write_int(A)
;
{ X = bar(B) },
read_or_write_string(B)
).
:- pred read_or_write_which_of_foo_or_bar(thing, stream, stream).
:- mode read_or_write_which_of_foo_or_bar(in, in(output_stream), out
(output_stream)) is det.
:- mode read_or_write_which_of_foo_or_bar(out(partial_thing), in
(input_stream), out(input_stream)) is det.
Unfortunately you can't actually write the predicate
read_or_write_which_of_foo_or_bar/3 because it would have to output a
partially instantiated structure as its first argument in the input
case.
Ok, maybe this is a gross abuse of the mode system. But it would have
saved me duplicating my grammar for input and output.
I'll go back to my hole now.
Cheers,
Peter
--------------------------------------------------------------------------
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