[m-users.] Building a serialiser with an additional state

Sean Charles (emacstheviking) objitsu at gmail.com
Sun May 9 01:33:01 AEST 2021


What line is line 33 Volker?

It smells like accumulator passing, I had a similar issue a few days ago porting old school(!) Prolog DCG rules… in Mercury you have to have an in and an out, if you haven’t read calculator.m, do so. My solution to my problem is in the mail list somewhere!


> On 8 May 2021, at 16:30, Volker Wysk <post at volker-wysk.de> wrote:
> 
> Hi!
> 
> I'm trying to build an example of a serialiser, using DCGs, which has an
> additional state, which needs to be threaded through. This is the code:
> 
> 
> -----
> % Number the elements of a list
> :- pred number(list(string)::in, 
>               int::in, int::out, 
>               list(string)::out, list(string)::in)
>   is det.
> 
> 
> number([], !_St) -->
>    [].
> 
> number([T|Ts], !St) -->
>    num(Nr, !St),
>    [ int_to_string(Nr), T ],
>    count(!St),
>    number(Ts, !St).
> 
> 
> % Get the number inside the state
> :- pred num(int::out, 
>               int::in, int::out, 
>               list(string)::out, list(string)::in)
>   is det.
> 
> num(StIn, StIn, StIn) -->
>    [].
> 
> 
> % Increase the number inside the state by 1
> :- pred count(int::in, int::out, 
>              list(string)::out, list(string)::in)
>   is det.
> 
> count(StIn, StIn+1) -->
>    [].
> -----
> 
> The result should be like this:
> 
> number(["foo","bar","baz"], 
>       0, 3, 
>       ["0","foo","1","bar","2","baz"], [])
> 
> In Prolog, exactly the same code does work (after expanding the state
> variable syntax).
> 
> But in Mercury, this won't compile. I get this compiler message:
> 
> -----
> number.m:033: In clause for `number(in, in, out, out, in)':
> number.m:033:   in argument 5 of call to predicate `number.num'/5:
> number.m:033:   mode error: variable `DCG_1' has instantiatedness `free',
> number.m:033:   expected instantiatedness was `ground'.
> -----
> 
> I vaguely understand the error message. When using "-E", the message gets
> really long. But I don't know how it should be done...
> 
> 
> Cheers,
> Volker
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users



More information about the users mailing list