[mercury-users] Optional fields in a datatype - how to represent?

Ralph Becket rbeck at microsoft.com
Mon Apr 23 21:09:21 AEST 2001


> From: Terrence Brannon [mailto:princepawn at earthlink.net]
> Sent: 23 April 2001 01:58
> 
> 1- And what happens if you attempt to access it and it is not bound.

Referring back to your original message, it looks like you may be
getting types and insts confused (forgive me if I'm wrong).

Every variable and value in a Mercury program has a type.

Each variable also has an instantiation state (i.e. an inst) at any
point in time.  Initially variables have inst `free', which is to say
they have not yet been initialised.  When a variable is bound by
unification its inst is refined - typically it will go from being free
to being bound to some ground value (i.e. something with no `free' holes
in it).  This is what the `out' mode means, which is defined as

	:- mode out == free >> ground.

So an `out' argument to a pred is required to be free (uninitialised) at
the start of the call and will be bound to a ground value when the call
returns.

While the language spec. does allow for partial insts (e.g. a variable
might be bound to one of your employee/3 records, but some of its
fields may still have inst free - this sort of thing happens quite often
in Prolog programs), to the best of my knowledge the current compiler
cannot handle such things.  Even if it could, the mode system would
still require that you *know* at compile time the inst of every variable
at every point in your program - that is, you would know when you wrote
the predicate that accessed the unbound field that it was free.

My personal opinion is that partial insts a la Prolog lead to more
confusion than is worth it, even if they do allow some optimized coding
styles that would otherwise not be possible.

So, as has been said, it's best to use the maybe/1 type which tells you
when you access it whether there is a value to be considered or not.

Cheers,

Ralph
--------------------------------------------------------------------------
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