[m-users.] Reasoning behind a compile failure on list destructuring

Zoltan Somogyi zoltan.somogyi at runbox.com
Sat May 22 00:22:54 AEST 2021


2021-05-21 23:23 GMT+10:00 "Sean Charles (emacstheviking)" <objitsu at gmail.com>:
> It took me a while to figure it out, but I reasoned that despite the fact I know
> I passed in six variable names, the compiler could not know how many values
> might well be in the returned list, despite me knowing that the internal implementation
> would always return the same number of strings, even a blank one.

That reasoning is correct. There are type systems that can express invariants such as
"the length of the output list is the same as the length of the input list", but
the plain old Hindley Milner system that Mercury uses cannot do so.

> That then made sense of the error message, my final code then is:
> 
>     utils.get_env_str([
>         "FELT_INST", "FELT_PAGER", "FELT_EDITOR",
>         "FELT_REPL", "FELT_LEX", "FELT_AST"],
>         Vars, !IO),
>     ( Vars = [Inst, Pager, Editor, Repl, Lex, Ast]
>     -> do stuff
>     ; apologise to user
>     ).

That being said, I would not use code like the above in this simple case.
I would just call get_env_str six times directly (i.e. not in a loop), and then
gather up the results. But instead of a list such as [Inst, Pager, etc].
I would gather them up in a structure, as Dirk suggested. That will be
simpler to use and easier to read, as well as faster (though speed is
usually irrelevant in cases like this).

Zoltan.


More information about the users mailing list