[mercury-users] Pred defns

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Mar 26 09:42:32 AEDT 1998


On 26-Mar-1998, Thomas Charles CONWAY <conway at cs.mu.OZ.AU> wrote:
> 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.

Indeed, and even though the DCG syntax is documented in the
reference manual, Peter Phelan's confusion here is understandable. 
The reference manual explains the syntax and semantics for DCGs, but it
doesn't really explain how they can be used.  What we need is a good
tutorial-level introduction to Mercury which doesn't assume any prior
knowledge of Prolog; I'm afraid that as yet we don't have one.

(However, DCGs in Mercury are very similar to DCGs in Prolog,
and any good book on Prolog programming, e.g. [1,2], will
include a chapter on the use of DCGs.)

	[1] "The Art of Prolog", Sterling and Shapiro.
	[2] "The Craft of Prolog", O'Keefe.

Incidentally, the "D" in "DCG" is a bit misleading.  "DCG" stands for
"Definite Clause Grammar", and in this context I'm sure "Definite" is
supposed to mean "containing no negation".  However DCGs in Prolog and
Mercury do allow negation (and if-then-else).  So plain "CG" would
probably have been a better name.  But nevertheless the name "DCG"
seems to have stuck.

> If we do the DCG expansion by hand on your code, we get:
> 
> fullexpr(X, DCG0, DCG2) :-
> 	expr(X, DCG0, DCG1),
> 	DCG1 = ['\n'|DCG2].

You can get the Mercury compiler to do the DCG expansion using the
`--convert-to-mercury' option, e.g.  `mmc --convert-to-mercury
calculator.m'.  This will produce a file `calculator.ugly' which
contains the original source code with all DCGs expanded. 
So you don't need to do it by hand ;-)
Looking at the expanded code may help you to understand how DCGs work.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the users mailing list