Fergus' hack (was Re: [mercury-users] polymorphism)

Lee Naish lee at cs.mu.oz.au
Mon Sep 29 19:01:47 AEST 1997


In message <199709290651.IAA16904 at raavak.cs.kuleuven.ac.be>you write:
>
>> Consider my example again:
>
>sorry I missed that if you wrote that earlier already;
>an impressive hack, it leaves me almost speecheless;
>as for giving fuel to people with fire in them against Mercury, it
>is unsurpassed;
>I hope it is not recommended seriously

My initial reaction was somewhat negative also.  It reminded me of code
using var/1 to get things working in multiple modes.  However, I think
that (with some getting used to) its not a bad idea.

Imagine that you are programming in a logic language where you didn't
have to specify modes (the system automagically figures things out for
you).  Having an extra argument is useful for "almost reversible"
operations like the parser/pretty printer example Fergus gave.  It
allows you to have more code reuse, greater maintainability etc.
Logically its fine - the extra argument says whether whitespace is
significant etc (and prevents infinite number of solutions for pretty
printing).

I think the "hack" comes in with the way this technique can be supported
by the Mercury mode/inst scheme.  Its natural to think at about the same
level as you have to with the var/1 technique and you have to know
Mercury very well.

I'm also a bit concerned about what happens when you really push this
technique, especially with partially instantiated modes and other more
flexible things.  You may end up with modes which make no sense such as
"if the first argument is foo then we can output the second argument and
if the second argument is bar then we can output the first argument".

In my work on modes a while back I made the simplifying assumption that
the mode associated with a node in a type tree depended only on the
parent node (not siblings etc), to avoid this technical problem of
nonsensical modes.  I came up with a more flexible definition which
avoids the problem a while ago.  Mercury solves the problem in some way,
but I'm not quite sure how, and any extensions of the mode system will
have to bear this in mind.

	lee



More information about the users mailing list