[mercury-users] Pred defns
Thomas Charles CONWAY
conway at cs.mu.OZ.AU
Thu Mar 26 08:00:02 AEDT 1998
Peter Phelan, you write:
[message rearranged]
> I'm confused by pred definitions in Mercury.
>
> It's something to do with input/output, but this is also
> difficult to understand.
What seems to be confusing you is DCG syntax. DCGs are a
layer of syntactic sugar which was developed to help with
writing parsers. The fact that they are useful for I/O and
other situations where you want to thread an argument is
coincidental.
> The following is snipped from calculator.m in samples.
>
> :- pred fullexpr(expr::out, list(char)::in, list(char)::out)
> is semidet.
> fullexpr(X) -->
> expr(X),
> ['\n'].
>
> How come the declarative definition takes just one argument,
> whilst there are 3 in the type declaration?
>
Notice that the form of the definition is
Head --> Body
not
Head :- Body.
The use of --> inicates that this definition is a DCG definition.
It is expanded by the compiler as I explained breifly in a previous
message, and is fully documented in the language reference manual.
The definition for fullexpr HAS three arguments - two of them
(the last two) are made implicit by the use of DCG notation.
If we do the DCG expantion by hand on your code, we get:
fullexpr(X, DCG0, DCG2) :-
expr(X, DCG0, DCG1),
DCG1 = ['\n'|DCG2].
Thomas
--
Thomas Conway || conway at cs.mu.oz.au
AD DEUM ET VINUM || Nail here [] for new monitor.
More information about the users
mailing list