[mercury-users] How efficient is mercury?

David Overton dmo at cs.mu.OZ.AU
Tue Apr 20 07:32:02 AEST 2004


On Tue, Apr 13, 2004 at 11:00:24AM +1000, Ralph Becket wrote:
> The main difference between OCaml and Mercury is that Mercury is a
> purely declarative language whereas OCaml makes it easy to mix
> imperative and functional programming styles.  The Mercury philosophy is
> that combining the two styles makes it more likely that programs will
> contain bugs (it's much harder for the compiler to automatically
> identify errors), can inhibit optimisation (for similar reasons), and
> can reduce reusability (shared state has a tendency to spread throughout
> a program.)  That said, Mercury does allow you to embed impure code in
> your program provided you explicitly label it as such.  Mercury also has
> a world class foreign language interface and works very well with a
> number of C compilers, including Visual C.  You can use imperative
> structures such as arrays and pointer graphs in Mercury, but being
> purely declarative means that doing so incurrs some syntactic overhead
> (i.e. your programs will be a little longer, but should run just as
> quickly.)

After being a Mercury developer for several years I've recently started
a new job where I'm programming in Ocaml.  I can't comment on the
efficiency difference between them because I haven't done any
comparisons.  I'll give a few comments on my experience moving to OCaml.

The main thing I've noticed is that programming in OCaml feels a lot
less "safe" that programming in Mercury.  Even sticking to the purely
functional subset of OCaml (which I've mainly done so far), the OCaml
compiler catches many fewer errors than the Mercury compiler.  E.g. the
OCaml compiler does not give warnings for singleton variables, which
often indicate a typo (if anyone knows how to turn this on, please let
me know!).  Also, OCaml will not tell you whether a pattern match is
exhaustive, like the Mercury determinism analysis is able to do for
switches.  This makes it much harder to update all affected places if
you add an alternative to a discriminated union type.

I've gained a new appreciation for the Mercury type checker.  It seems
(qualitatively) to be much better at reporting type errors at the point
at which the error occurs, rather than giving a confusing message that
points to something several lines later.  I also miss Mercury's type
class system and have even come across cases where Mercury's backtracking
search abilities would have come in handy for significantly simplifying
the expression of an algorithm.

Some aspects of OCaml just feel archaic after Mercury.  E.g. the
inability to overload function names and term constructors, and the fact
that you must generally define a function before you use it (leading to
programs that feel like they're written "upside down").  But maybe this is
just because of what I'm use to.

On the plus side for OCaml, its syntax is much more terse than Mercury's
(although probably not as terse as Haskell), leading to much more
compact and readable code, especially if you use a lot of higher-order
functions.  The module system is also excellent.


David
-- 
David Overton
WWW: http://www.overtons.id.au/
Mobile Phone (UK): +44 7799 344 322
--------------------------------------------------------------------------
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