[m-users.] A question of style...

Sean Charles (emacstheviking) objitsu at gmail.com
Tue Mar 1 20:01:13 AEDT 2022

I am writing code lately that is heavily dependant upon the successful reading of a file, finding something in that that file, opening an input stream on a file, opening an output stream on that file and a few more key decisions before the actual file processing can begin.

The first incarnation of my code has thus suffered from what is often called 'stair-casing' where each nested disjunction is creeping the code more and more to the right.

So I refactored some code to bring it back into line but now I have lots of predicates...  

I guess what I am trying to ask is, is there a 'Mercury Way' to handling this. Haskell has the Maybe monad which can greatly help to chain operations, is there a similar thing with Mercury ? In my transpiler, I had this issue and I created a small structure that each successive

:- type control ---> control(...);
:- pred step1(control::in, control::out) is det.
:- pred step2(control::in, control::out) is det.
:- pred step3(control::in, control::out) is det.

some [!C] (
    !:C = control(...),

This feels cleaner but again, would this be the 'right' thing to do in Mercury. I am much better with the language now than a year ago, almost to the point of not having to think too hard now about anything other than the problem, which is great!

The only other code I've seen that inspired me to what may be possible is in the implementation of a "/" operator which takes in a pred() and returns a pred(), this code is from config.m of mmc-doc (such a useful program!)...

:- type maybeio == (pred(maybe(string), io, io)).
:- inst maybeio == (pred(out, di, uo) is det).
:- func (maybeio::in(maybeio)) // (maybeio::in(maybeio))
    = (maybeio::out(maybeio)).
A // B = C :-
    C = (pred(Res::out, !.IO::di, !:IO::uo) is det :-
        A(Res1, !IO),
            Res1 = Res @ yes(_)
            Res1 = no,
            B(Res, !IO)

I guess that, with lots of smaller predicates, the skill comes with choosing meaningful names and writing clear code, principles applicable to any  development project.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220301/8160f82b/attachment.html>

More information about the users mailing list