[m-users.] To learn Mercury

Paul Bone paul at bone.id.au
Mon Mar 20 11:57:31 AEDT 2017

Hi Charles, thanks for answering Echedey's question.

On Sun, Mar 19, 2017 at 08:08:29AM -0500, Charles Shuller wrote:
> You need to know that Variables NEVER get reassigned to (remember that !IO
> is compiler magic) and that this is a very good thing, though it'll
> probably drive you mad if you think your wasting a bunch of RAM with all
> the variables (I've not ever seen this cause any noticeable memory
> consumption, and I've stopped reassigning variables in all my other code as
> well whenever possible).

There's two cases here.


    X0 = 3,
    X1 = X0 + 1,
    x2 = X1 * 7,
    write(X2, !IO).

This does not waste memory.  The old variable is "dropped" after each
update, and so the compiler knows it can re-use the same register for all
the values of Xn.


    X0 = [1, 2, 3],
    X1 = map(add(1), X0),
    X2 = map(mul(7), X1),
    write(X2, !IO).

This can be said to "churn" memory.  The list cells (cons cells) must be
allocated on the heap.  Lines 2 and 3 create a new list and "drop" the old
one, however they almost always use fresh memory for the new list and rely
on the garbage collector to free the old list.  Even if the GC is prefect
(fast + accurate) this type of code can "touch" more memory than a similar
program in an imperative language.  This means that it can be more
inefficient for the processor's caches and the memory bus.

Of course this is probably not what Echedey needs to know right now.  But
you may be interested yourself.

> If you know nothing at all about programing in general, Mercury may not be
> the best language to learn in.   It's got a bit of "magic" you need to know
> about up front (the !IO stuff for instance) and you'll need to know the
> difference between a modules public interface and it's implementation.
> There is also a lot of "chatter" that isn't needed for very simple programs
> that gets in the way of learning.

The number of new concepts introduced at the same time when learning
something can make it a lot more difficult to learn.  Mercury introduces a
lot of concepts very quickly, which will make it challenging without
experience or at least exposure to some of these things before.  Eg: people
find it easier to learn Prolog or Haskell first.  Of course that can reuqire
_unlearning_ also.

> Personally, I've found Python to be the easiest language for a beginner to
> learn in, just don't get caught up in the idea that it's the right solution
> to every problem.   In my experience Python has been very bad at any
> program of scale.   But beginners need to write lots of small programs
> anyway.   Once you get variables, and functions well understood in Python,
> they'll make a lot more sense in mercury (though they aren't quite the same
> thing they are based on the same underlying concepts).


Paul Bone

More information about the users mailing list