[m-users.] Useful predicates for loops of IO actions

Zoltan Somogyi zoltan.somogyi at runbox.com
Wed Dec 22 02:51:28 AEDT 2021



On Tue, 21 Dec 2021 16:18:48 +0100, Volker Wysk <post at volker-wysk.de> wrote:
> I'm using four meta-predicates which deal with loops and IO, and I find them
> useful. Maybe you want to include them in the Mercury libraries?

Three out of the four are already there, in more general forms. Instead of
the last pair of arguments required to be I/O states, they can be any type,
and their modes do not have to be di/uo; they can be e.g. in/out.

> :- pred map_io(pred(T, U, io, io)::in(pred(in, out, di, uo) is det),
>                list(T)::in,
>                list(U)::out,
>                io::di, io::uo)
>    is det.

This is list.map_foldl.

> :- pred map_io(pred(T, io, io)::in(pred(in, di, uo) is det),
>                list(T)::in,
>                io::di, io::uo)
>    is det.

This is list.foldl.
 
> This is the IO-version of foldl:
> 
> :- pred fold_io(pred(T, A, A, io, io)::in(pred(in, in, out, di, uo) is det),
>                 list(T)::in,
>                 A::in,
>                 A::out,
>                 io::di, io::uo)
>    is det.

This is list.foldl2.

> And the IO version of list.filter/4:
> 
> :- pred filter_io(
>     pred(T, bool, io, io)::in(pred(in, out, di, uo) is det),
>     list(T)::in,
>     list(T)::out,
>     list(T)::out,
>     io::di, io::uo)
>    is det.

This is not in the Mercury standard library, and I don't think
I have ever needed a predicate like this. (If I had, I *would* have
added it to the library.)

> If you'd like to include these predicates, I'd provide proper comments and,
> of course, the implementation.

Thanks, but that is not necessary; for predicate signatures like this,
the code practically writes itself.

Zoltan.





More information about the users mailing list