[m-dev.] for review: rewrite of check_typeclass.m
David Glen JEFFERY
dgj at cs.mu.OZ.AU
Thu May 21 12:30:03 AEST 1998
On 21-May-1998, Peter Schachte <pets at students.cs.mu.oz.au> wrote:
> Hi DJ,
>
> On Wed, 20 May 1998, David Glen JEFFERY wrote:
>
> > A re-work on check_typeclass.m. Now, rather than the ad-hoc approach of
> > checking each method of each instance declaration for *exact* type and mode
> > correctness, we generate a new predicate that gets inserted into the HLDS.
> > This predicate is checked for type, mode, uniqueness and determinism
> > correctness in the appropriate compiler passes.
>
> Does this mean that type class methods can now be impure or semipure? That
> should probably be allowed. Instances should be checked that the
> corresponding predicates are no less pure than the class specifies (but more
> pure should be ok, I think).
Hmmm. I don't think it will work at the moment, but it shouldn't be too much
work to get it working.
Actually, I just tried it. For the following:
:- module x.
:- interface.
:- typeclass c(T) where [
pred p(T::in) is det
].
:- instance c(int) where [
pred(p/1) is my_p
].
:- impure pred my_p(int::in) is det.
:- implementation.
:- pragma c_code(my_p(X::in), "fprintf(stderr, ""Hello\n""); X = X;").
We get no error message. Now that I think about it, the reason would be
that the "optimisation" that I talked about before gets turned on. So... if
you fool the compiler like this:
:- module x.
:- interface.
:- typeclass c(T) where [
pred p(T::in) is det
].
:- instance c(int) where [
pred(p/1) is my_p
].
:- impure pred my_p(T::in) is det.
:- implementation.
:- pragma c_code(my_p(X::in), "fprintf(stderr, ""Hello\n""); X = X;").
Then we get the error message:
In call to impure predicate `x:my_p/1':
error: call must be preceded by `impure' indicator.
In predicate `x:Introduced predicate for x:c(int_0_) method x:p/1':
error: predicate is impure.
It must be declared `impure' or promised pure.
The lesson? The optimisation is evil. The source-to-source transformation is
good.
love and cuddles,
dgj
--
David Jeffery (dgj at cs.mu.oz.au) | Marge: Did you just call everyone "chicken"?
MEngSc student, | Homer: Noooo. I swear on this Bible!
Department of Computer Science | Marge: That's not a Bible; that's a book of
University of Melbourne | carpet samples!
Australia | Homer: Ooooh... Fuzzy.
More information about the developers
mailing list