[mercury-users] Maths and contexts.

Mark Brown mark at csse.unimelb.edu.au
Sat Apr 21 01:43:27 AEST 2012


On 21-Apr-2012, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
>
> 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 ;-)

It worked for me.

>
> ...
>
>> :- 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.

There were two such types exported.

In something like this:

    :- func test1(string) = dec(basic_default_context).
    test(S) = to_number(S).

the context is determined by the signature of test1.

In other cases a type qualifier may be needed:

    :- func test2(string) = string.
    test2(S) = to_engineering_string(
                   abs(to_number(S)) : dec(basic_default_context)
               ).

(without the type qualifier, the compiler will try to use dec(void),
which won't satisfy the typeclass constraints).

Cheers,
Mark.

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