InstanceSigs -- rationale for the "must be more polymorphic than"

Anthony Clayden anthony.d.clayden at gmail.com
Sun Aug 8 07:04:11 UTC 2021


I can't help but feel InstanceSigs are either superfluous or upside-down.
It's this bit in the User Guide:

> The type signature in the instance declaration must be
> more polymorphic than (or the same as) the one in the class declaration,
> instantiated with the instance type.

Usually if you give a signature, it must be _less_ polymorphic (or the same
as) the type inferred from the term:

>    lessPolyPlus :: Integral a => a -> a -> a
>    lessPolyPlus x y = x + y

Or

>    lessPolyPlus (x :: a) y = x + y :: Integral a => a

The examples in the User Guide aren't helping: you could just drop the
InstanceSigs, and all is well-typed. (Even the example alleging to use
-XScopedTypeVariables in a where sub-decl: you could just put random `xs ::
[b]` without scoping `b`.)

Dropping the Sigs altogether works because the type from the class decl,
suitably instantiated, is less polymorphic than inferred from the term. IOW
the suitably instantiated type restricts what would otherwise be inferred.
Situation normal.

I suppose it might be helpful to give an explicit InstanceSig as 'belt and
braces' for the instantiated -- possibly because the instantiation is hard
to figure out; possibly because you want to use -XScopedTypeVariables
within a where-bound sub-decl, as an extra piece of string.

I can see you mustn't make the InstanceSig _less_ polymorphic than the
suitably instantiated.

But the docos don't give any example where it's essential to provide an
InstanceSig _and_  make it strictly more polymorphic. Here all the sigs and
annotations are just superfluous:

>    maxPolyPlus :: Num a => a -> a -> a
>    maxPolyPlus = (+)
>
>    class C a  where foo :: a -> T a
>    instance Integral a => C a  where
>      foo :: Num a => a -> T a
>      foo (x :: a) = MkT (maxPolyPlus x x :: Num a => a)

Is there a persuasive example (to put in the User Guide)?

AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/glasgow-haskell-users/attachments/20210808/9d4709ba/attachment.html>


More information about the Glasgow-haskell-users mailing list