[mercury-users] Mercury example programs

brogoff brogoff at speakeasy.net
Fri Jun 17 08:03:02 AEST 2005

On Wed, 15 Jun 2005, Ralph Becket wrote:

> brogoff, Tuesday, 14 June 2005:
> >     Is there a repository of Mercury sample programs available somewhere?
> > I know about the examples dir in the distribution, but it's a bit lean.
> > I tend to learn by hacking a working example.
> There's the extras part of the distribution which you have to download
> separately from the web site.  That contains code written in different
> styles as the language has evolved.  The library code provides some
> good, fairly modern coding examples.

Doh! I should have remembered that. When I downloaded the release (on May 29,
2005) I don't remember seeing an extras file, but I see one now that I looked.

> >     In particular, I'm interested in small, well documented  examples of the
> > standard transformational programs that everyone writes (parse a file, maybe
> > build an AST, emit transformed data) that often involve lex and yacc, and
> > which make use of Mercury's IO libraries. Anything like that available?
> Sure.  Look at lexer.m and term_io.m and parser.m and pprint.m in the
> library directory.  The samples directory contains lex.m, which is a
> pretty decent generic lexer, and xml.m, which is a direct
> transliteration of the XML spec. into Mercury (it won't win any prizes
> for speed, but it is as correct as can be given the state of the XML
> `spec').

Perfect, I'll take a look there, thanks. Some comments on a few things I

xml.m uses parser combinators. Is that the idiomatic Mercury approach? I
ask because even though I've seen parser combinator libraries for ML, in
my experience they aren't used; rather lex/yacc or (in OCaml) stream pattern
matching (just a nice syntax for hand coded recursive descent parsing)
is used.

lex.m is a library module rather than a lexer generator. Yet there is a
yacc like parser generator, moose.

> If you have specific examples in mind, I'd like to hear about them.  I
> think the tutorial could be usefully extended with some more realistic
> example programs.

I'll try and come up with some data munging examples in the field that
I work in (EDA) as I develop familiarity with Mercury, but you're
certainly right about extending the tutorial. Mercury is unfamiliar to those
without a Prolog background, so that tutorial is where many of us will go

As a specific tutorial example, I am partial to ones that include a simple
or medium complexity recursive descent parser. Interpreters are great, as
they are simple enough to be easily understood, invoke enough of the
IO features of the language to be broadly interesting, and they provide a
skeletal framework for modification. The parser in xml.m is a nice example, but
I wonder if the parser combinator approach is what the Mercury programmer in the
trenches would use.

-- Brian

mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe

More information about the users mailing list