[mercury-users] Maths and contexts.

Julien Fischer juliensf at csse.unimelb.edu.au
Sat Apr 21 00:43:38 AEST 2012


Hi Paul,

On Fri, 20 Apr 2012, Paul Bone wrote:

> On Fri, Apr 20, 2012 at 01:25:42PM +0800, Michael Richter wrote:
>> I'm trying to implement the General Decimal
>> Arithmetic<http://speleotrove.com/decimal> (henceforth
>> GDA) specification in Mercury as a learning project.  So far it's been a
>> lot of fun and I've been learning quite a bit (as well as getting
>> frequently humbled by Boney and ski in #mercury as they gently suggest
>> obvious ways to improve my code).  There is one problem I've been facing
>> that I can't resolve, however.  Boney suggested the use of type classes to
>> solve it, but I just can't see how they'd help.
>>
>
> Hi Michael,
>
> This is what I ment when I said you could use a typeclass.  (In IRC I use the
> name Boney, for those following along).
>
> I've given this a quick test and the compiler is happy with what's there.  (it
> only complains about the bits that I havn't written).

With the interface given below, you'll find that it will complain when
you write those bits too ;-)

...

> :- func to_number(string)              = (dec(C))
>    <= context(C).

Specifically, how does a given context type get associated with the result
value here?  Since C is universally quantified it must be passed in
by the caller, but nothing else in this API that will provide a binding
for it.

The signature for to_number actually needs to be something like:

   :- func to_number(C, string) = dec(C) <= context(C).

i.e. at some point you need to actually associate a particular context
with a (set of) dec/1 value(s).

The other limitation of this approach is that you are limited to the
set of context types that are defined in your program.  You cannot
construct new contexts at runtime.

Julien.
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list