[m-dev.] Is this a bug?

Peter Schachte schachte at csse.unimelb.edu.au
Mon Jun 18 15:52:52 AEST 2007

Jonathan Morgan wrote:

> If I write something like the following:
>    Map = default_map.init(map.init),
>    _ = Map ^ elem(42).
> and the default value is used, then does Map now have an element 42,
> set to map.init?

No, Map is unchanged.  In fact, I'd hope Mercury would optimise this code away
altogether, since it's det and has no outputs.

> In either case, I think that the result is a little
> strange.  Also, are you expecting the default to be a value, or a
> function taking the value of the element we wish to have a default
> value for?

A value.

> The second method is more general, but is also a little
> more complicated to use.

Agreed.  I can't think of a case where I'd want the extra flexibility, but I
didn't try very hard.

> All these problems (and more, because of its imperative nature and
> lack of a type system) occur with Python's default_map, such that it
> is generally advised that users be extremely careful when using it.
> On the Mercury side, there is no danger of accidentally adding
> additional values to a map when looking up a value, but there is the
> potential problem of creating redundant data structures and using more
> memory.

A default map would use at most 1 extra word, and possibly arbitrarily much
less memory, since storing the default value into the map could be special
cased to remove that key altogether.

> In many ways I think that a getter with default values is
> more transparent, though not necessarily as nice.  How this integrates
> with returning the map altered I'm not really sure.

Yeah, that could be a solution, too.  It's not a nice, though, since you'd have
to mention the default for every fetch and store.

Peter Schachte              One thing's for sure: if we keep doing what
schachte at cs.mu.OZ.AU        we're doing, we're going to keep getting what
www.cs.mu.oz.au/~schachte/  we're getting.
Phone: +61 3 8344 1338          -- Stephen Covey, et al.
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au

More information about the developers mailing list