[m-dev.] EDCGs and Higher Order Programming
Simon Taylor
stayl at cs.mu.OZ.AU
Thu Jan 27 11:33:43 AEDT 2000
Fergus wrote:
> On 26-Jan-2000, Ralph Becket <rbeck at microsoft.com> wrote:
> > now we have the record
> > syntax, surely that (plus DCGs) should be sufficient to
> > achieve the same thing?
>
> Good question. But I don't think it works out quite so nicely.
> For example, suppose you want to pass the io__state and something
> else, so you put them both in a record. Now you want to call
> something simple like `io__write_string' or `io__write_int'.
> To do that, you need to extract the io__state from the record,
> pass it to io__write_string and/or io__write_int,
> and then set the resulting io__state:
> > > write_strings(Strs) -->>
> > > WriteStringPred = (pred(S::in, A0::di, A::uo) is det :-
> > > (io is changed(A0, A) -->> io__write_string(S))),
> > > list__foldl(WriteStringPred, Strs, $io, $=io).
> Well, you can do a little better by abstracting out the conversion
> function:
>
> :- func io2p((pred)+hidden(changed(io))) = pred(io__state, io__state).
> :- mode io2p(in(pred)) = out(pred(di, uo) is det).
> io2p(P) = (pred(IO0::di, IO::uo) is det :-
> (io is changed(IO0, IO) -->> P)).
>
> Then you can write `write_strings' as
>
> write_strings(Strs) -->>
> list__foldl(io2p(io__write_string), Strs, $io, $=io).
If the mode system were a bit more expressive, you could do a similar
thing for records containing `io__state's. With the current mode
system filling in the `XXX's is a bit difficult.
:- typeclass contains_io_state(T) where [
func io_state(T) = io__state,
mode io_state((XXX -> XXX_no_io)) = uo,
func 'io_state:='(T, io__state) = T,
mode 'io_state:='(XXX_no_io, di) = out(XXX)
].
:- pred io(pred(io__state, io__state), T, T) <= contains_io_state(T).
:- mode io(pred(di, uo) is det, di(XXX), uo(XXX)) is det.
io(P) -->
IO0 =^ io_state,
P(IO0, IO),
^io_state := IO.
write_strings(Strs) -->
list__foldl(io(io__write_string), Strs).
Simon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list