[m-users.] Building a serialiser with an additional state
Volker Wysk
post at volker-wysk.de
Sun May 9 01:45:24 AEST 2021
Am Samstag, den 08.05.2021, 16:33 +0100 schrieb Sean Charles
(emacstheviking):
> What line is line 33 Volker?
It's the line "num(Nr, !St)," at the beginning of the second clause of
"number".
> 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!
This is from calculator.m:
:- pred fullexpr(expr::out, list(char)::in, list(char)::out) is semidet.
This is just a regular parser. I have no problems with that (wrote a really
big one lately). What I'm trying to build is a serialiser, which needs to
have an additional state.
Regards,
Volker
> > 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
-------------- 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/86ca061b/attachment.sig>
More information about the users
mailing list