[m-users.] Import only notations from a module?

Mark Brown mark at mercurylang.org
Mon Jul 15 13:17:21 AEST 2024


On Sat, Jul 13, 2024 at 9:53 PM Anders Lundstedt
<mercury-users at anderslundstedt.se> wrote:
>
> Dear all,
>
>
> I like using ‘use_module’ instead of ‘import_module’ since this forces
> me to properly qualify names. However, some notations would be nice to
> have—in particular, list notations.

I would be happy for the list constructors to be treated as builtins.
They are already treated specially when parsing, and are intended for
use as lists. Making them builtins would also stop people from trying
to redefine them.

I'd also like to be able to define name equivalences for things other
than types, modes and insts, but I imagine that would be a significant
amount of effort to work out the details of, let alone implement.

>
>
> I would be fine with having to redefine notations, as for example in
> the following non-compiling attempt (which does compile if one imports
> the list module and removes my attempt at redefining the notations).

Redefining them as functions loses some information, since there's
things we know about constructors that don't apply to functions
generally. In particular, when constructors are viewed as functions:
 - they are injective
 - their ranges are disjoint

These two assumptions tell us that "L = [_ | TL]" is a deconstruction
unification, meaning that we can just pull TL out of the second
argument of L. On the other hand if '[|]'/2 is an arbitrary function,
as you've declared it below, then this inference is not valid.

That's why you get a mode error - the function doesn't provide a mode
that is equivalent to deconstruction, and the compiler can't treat it
as such.

Cheers,
Mark

>
>
> Code:
>
> :- module list_notations.
>
> :- interface.
>
> :- use_module list.
>
> :- func '[]'                    = list.list(T).
> :- func '[|]'(T, list.list(T))  = list.list(T).
>
> :- func replace_everything(T, list.list(T)) = list.list(T).
>
> :- implementation.
>
> []        = list.'[]'.
> [HD | TL] = list.'[|]'(HD, TL).
>
> replace_everything(A, L) = RES :- (
>   if L = [_ | TL] then
>     RES = [A | replace_everything(A, TL)]
>   else
>     RES = []
> ).
>
>
> Compilation error message:
>
> list_notations.m:018: In clause for `replace_everything(in, in) = out':
> list_notations.m:018:   in argument 1 of call to function
> list_notations.m:018:   `list_notations.[|]'/2:
> list_notations.m:018:   mode error: variable `V_7' has instantiatedness `free',
> list_notations.m:018:   expected instantiatedness was `ground'.
>
>
>
> Best,
>
> Anders Lundstedt
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users


More information about the users mailing list