Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make GHC provide the type class constraints of typed holes?

Tags:

types

haskell

ghc

Current behavior

Prelude> show _  <interactive>:7:6:     Found hole ‘_’ with type: a0     Where: ‘a0’ is an ambiguous type variable     Relevant bindings include it :: String (bound at <interactive>:7:1)     In the first argument of ‘show’, namely ‘_’     In the expression: show _     In an equation for ‘it’: it = show _ 

Desired behavior

It would be nice if GHC would also tell me that the typed hole has the Show type class constraint.

Misc

GHC Version 7.8.1

like image 343
Wizek Avatar asked Apr 12 '14 08:04

Wizek


2 Answers

This is now fixed in GHC 8.0 thanks to @DominiqueDevriese's GHC ticket.

Due to extended type defaulting, this isn't immediately obvious in GHCi. With your example,

> show _    <interactive>:7:6: error:     • Found hole: _h :: ()       Or perhaps ‘_h’ is mis-spelled, or not in scope     • In the first argument of ‘show’, namely ‘_h’       In the expression: show _h       In an equation for ‘it’: it = show _h     • Relevant bindings include         it :: String (bound at <interactive>:7:1) 

the type of the hole is defaulted to (). This is apparently the desired behavior, though there's an argument to be made that extended defaulting shouldn't apply to holes (as a common use for them is to get the compiler to tell you the inferred type).

Nevertheless, if you compile with GHC or disable extended default rules in GHCi (via :set -XNoExtendedDefaultRules), we see the result of the improvements:

<interactive>:3:1: error:     • Ambiguous type variable ‘a0’ arising from a use of ‘show’       prevents the constraint ‘(Show a0)’ from being solved.       Probable fix: use a type annotation to specify what ‘a0’ should be.       These potential instances exist:         instance Show Ordering -- Defined in ‘GHC.Show’         instance Show Integer -- Defined in ‘GHC.Show’         instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’         ...plus 22 others         ...plus 11 instances involving out-of-scope types         (use -fprint-potential-instances to see them all)     • In the expression: show _       In an equation for ‘it’: it = show _  <interactive>:3:6: error:     • Found hole: _ :: a0       Where: ‘a0’ is an ambiguous type variable     • In the first argument of ‘show’, namely ‘_’       In the expression: show _       In an equation for ‘it’: it = show _     • Relevant bindings include         it :: String (bound at <interactive>:3:1) 
like image 106
crockeea Avatar answered Oct 17 '22 07:10

crockeea


No currently its not possible.But it may be added to GHC as per the speculations.

like image 27
Vikas Anand Avatar answered Oct 17 '22 08:10

Vikas Anand