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

Volker Wysk post at volker-wysk.de
Sun May 9 01:30:11 AEST 2021


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20210508/d59c1f71/attachment.sig>


More information about the users mailing list