[m-dev.] for review: allow impurity on class methods

David Glen JEFFERY dgj at ender.cs.mu.oz.au
Mon Mar 27 14:51:28 AEST 2000


On 26-Mar-2000, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> 
> I've only got a couple of comments:
> 	- it would probably be a good idea to mention
> 	  somewhere in the language reference manual
> 	  that you can have impure methods
> 	  (and that you can't have impure higher-order types)

I think Tyse is making changes to that part of the reference manual, so I'll
leave that for him to add.

> 	- it would be a good idea to include some
> 	  `semipure' methods in the test case

Turns out when I looked more closely that one of the methods in the test case
I posted was semipure, so I've changed it as follows: (I'll commit this now...
let me know if you're unhappy with this test case).




:- module impure_methods.
:- interface.

:- import_module io.

:- pred main(io__state::di, io__state::uo) is det.

:- typeclass c(T) where [
	impure pred m1(T::in) is det,
	semipure pred m2(T::in, int::out) is det
].

:- type foo ---> foo.

	% impure implementations of impure methods
:- instance c(foo) where [
	pred(m1/1) is foo_m1,
	pred(m2/2) is foo_m2
].

:- type goo ---> goo.

	% pure implementations of impure methods
:- instance c(goo) where [
	pred(m1/1) is goo_m1,
	pred(m2/2) is goo_m2
].

:- impure pred foo_m1(foo::in) is det.
:- semipure pred foo_m2(foo::in, int::out) is det.

:- pred goo_m1(goo::in) is det.
:- pred goo_m2(goo::in, int::out) is det.

:- implementation.

:- pragma promise_pure(main/2). 

main -->
	{ impure m1(foo) },
	{ impure m1(foo) },
	{ impure m1(foo) },
	{ impure m1(foo) },
	{ semipure m2(foo, X) },
	io__write_int(X),
	io__nl,

	{ impure m1(goo) },
	{ impure m1(goo) },
	{ impure m1(goo) },
	{ impure m1(goo) },
	{ semipure m2(goo, Y) },
	io__write_int(Y),
	io__nl.

:- pragma c_header_code("int foo_counter = 0;").

:- pragma c_code(foo_m1(_F::in), "foo_counter++;").
:- pragma c_code(foo_m2(_F::in, Val::out), "Val = foo_counter;").

goo_m1(_).
goo_m2(_, 42).





dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) | If your thesis is utterly vacuous
PhD student,                    | Use first-order predicate calculus.
Dept. of Comp. Sci. & Soft. Eng.|     With sufficient formality
The University of Melbourne     |     The sheerist banality
Australia                       | Will be hailed by the critics: "Miraculous!"
                                |     -- Anon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list