Find constraints over type
Ben Gamari
ben at smart-cactus.org
Thu Jan 16 18:06:16 UTC 2020
Alejandro Serrano Mena <trupill at gmail.com> writes:
> Dear GHC devs,
> I am trying to figure out a way to obtain the constraints that hold over a
> type. Let me give you an example: suppose that I write the following
> function:
>
> f :: Eq a => [a] -> Bool
> f xs = xs == []
>
> If I ask for the type of the Var ' xs', I get back '[a]'. This is correct,
> but I am missing the crucial information that '[a]' must be Eq.
>
> Is there an easy way to get it? It seems that 'varType' doesn't give me
> enough information.
>
Indeed `varType` of `xs` will only give you the type `a`. How to get
back to the constraints that mention `xs` is a bit trick and will depend
upon where in the compiler you are. As far as I can tell, getting
constraints after desugaring will be quite difficult since they will
have been lowered to dictionaries at that point.
If you have access to hsSyn then you can of course easily compute the
constraints explicitly provided by the signature for `xs` (assuming
there is one). However, this will of course miss any inferred
constraints.
During typechecking it may be possible to compute some sort of
constraint set by looking at the typechecking environment, although I'll
admit this sounds quite dodgy.
Can you provide more details on what you are trying to do?
Cheers,
- Ben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20200116/45dd9bc4/attachment.sig>
More information about the ghc-devs
mailing list