polymorphic instances for typeclasses in Mercury language

256 views Asked by At

Consider next declaration:

:- type wrap(T) ---> wrap(T).
:- inst wrap(I) ---> wrap(I).

:- typeclass infer_wrap(A, B) <= ((A -> B)).
:- instance infer_wrap(A, wrap(A)).

Mercury (10.04) produces:

monad.m:011: In instance declaration for `monad.infer_wrap(A, monad.wrap(A))':
monad.m:011:   the first arg is a type variable

Even without functional dependency it will not allow to declare instance with type variables.
Am I missing something or Mercury doesn't allow instances which doesn't end with ground instance?
Is there any way to declare instance for polymorphic type which is wrapper for any type (without constraints)? Maybe there exists some special typeclass like all_types(T)?

1

There are 1 answers

0
Paul Bone On BEST ANSWER

Short answer:

Mercury doesn't support type variables in instance declarations. (which is what the compiler is complaining about).

Long answer:

There is no way create such a typeclass instance. Since the typeclass will be valid for all values of A maybe you don't need a typeclass at all. But it looks like you're trying to create Monads, currently this is impossible and we have no plans to support it in the immediate future