Skip to content

Suggested pattern signature doesn't account for provided constraints #1975

Closed
@sheaf

Description

@sheaf

It seems that the suggested pattern signature is not always correct in the presence of provided constraints. For instance:

{-# LANGUAGE PatternSynonyms #-}
import GHC.TypeNats

pattern MySomeNat a = SomeNat a

HLS suggests the pattern signature

pattern MySomeNat :: KnownNat n => Proxy n -> SomeNat

which is incorrect, as KnownNat n is not a required constraint (that is needed in order to write down the pattern synonym) but a provided one (which becomes available once pattern matching succeeds).

The pattern signature that HLS should suggest is the following:

pattern MySomeNat :: () => KnownNat n => Proxy n -> SomeNat

which uses the peculiar pattern signature syntax wherein the first set of constraints denotes those that are required, and the second the provided ones.

Another example:

class MyClass (n :: Nat)

pattern MySomeNat1 :: MyClass n => KnownNat n => Proxy n -> SomeNat
pattern MySomeNat1 a = SomeNat a

pattern MySomeNat2 a = MySomeNat1 a

HLS suggests the pattern signature:

pattern MySomeNat2 :: (MyClass n, KnownNat n) => Proxy n -> SomeNat

Not understanding that

pattern MySomeNat2 :: MyClass n => KnownNat n => Proxy n -> SomeNat

means something different.

It seems like there is some logic that rewrites a constraint involving multiple constraint arrows. For instance, if one writes:

{-# LANGUAGE NoMonomorphismRestriction #-}

x :: () => Num a => Ord a => Bounded a => a
x = 1

y = x

Then HLS suggests the following type for y:

y :: (Num a, Ord a, Bounded a) => a

This is obviously fine in this case, but the same rewriting is invalid for pattern signatures.

Version: Haskell Language Server 1.2.0 with GHC 9.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't right: doesn't work as intended, documentation is missing/outdated, etc..

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions