# Find constraints over type

5 messages
Open this post in threaded view
|

## Find constraints over type

 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] -> Boolf 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.Regards,Alejandro _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Open this post in threaded view
|

## Re: Find constraints over type

 Alejandro Serrano Mena <[hidden email]> 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 _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs signature.asc (497 bytes) Download Attachment
Open this post in threaded view
|

## RE: Find constraints over type

 In reply to this post by Alejandro Serrano Mena There is definitely no pure way to get from ‘a’ to its constraints. It is far from clear what “its constraints” are.   Is (C a b) such a constraint?  C [a] b?  What about superclasses?Constraints vary depending on where  you are.  GADT matches can bring into scope extra constraints on existing type variables.   So as Ben says, to give a sensible response you’ll need to explain more about your goal   Simon   From: ghc-devs <[hidden email]> On Behalf Of Alejandro Serrano Mena Sent: 16 January 2020 16:19 To: GHC developers <[hidden email]> Subject: Find constraints over type   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.   Regards, Alejandro _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs