[mercury-users] Maths and contexts.

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


On 20-Apr-2012, Michael Richter <ttmrichter at gmail.com> wrote:
> On 20 April 2012 23:33, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> 
> >   :- module test.
> >   :- interface.
> >   :- import_module io.
> >   :- pred main(io::di, io::uo) is det.
> >   :- implementation.
> >   :- import_module decimal2.
> >   :- import_module bool, integer.
> >
> >   main(!IO) :-
> >      Context1 = make_context(integer(30), round_floor, signal(no, no)),
> >      Context2 = make_context(integer(40), round_up, signal(no, no)),
> >      X = to_number(Context1, "1000000"),
> >      Y = to_number(Context2, "2900000"),
> >      Z = X `add` Y,
> >      io.write_string(to_scientific_**string(Z), !IO),
> >      io.nl(!IO).
> >
> > Attempting to compile the above will result in the following compilation
> > error:
> >
> >   Making Mercury/cs/test.c
> >   test.m:013: In clause for predicate `main'/2:
> >   test.m:013:   in unification of variable `Z'
> >   test.m:013:   and term `add(X, Y)':
> >   test.m:013:   type error in argument(s) of functor `add/2'.
> >   test.m:013:   Argument 1 (X) has type `(some [C] decimal2.decimal(C))',
> >   test.m:013:   expected type was `decimal2.decimal(C)';
> >   test.m:013:   argument 2 (Y) has type `(some [C] decimal2.decimal(C))',
> >   test.m:013:   expected type was `decimal2.decimal(C)'.
> >   ** Error making `Mercury/cs/test.c'.
> >
> 
> OK, thanks, Julien.  That's looking like a very promising line of inquiry.
>  Adding the context to the creation of a number was the line I was going to
> originally investigate but I was concerned at the need for run-time
> checking.  Your solution eliminates that problem for me.

Julien said it gives you compile-time checking, not that it eliminates
run-time checking.  You'll still need run-time checks if two contexts
don't originate from the same call to make_context.

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