Incomplete implicit type inference in Haskell -


let's consider following example:

data = a{x::int} deriving(show)  instance func_f (a -> string)     f _ = "ala"  class func_f     f ::  main :: io () main =      let          = 5         x = f      print 5 

compiled ghc -xflexibleinstances main.hs

(i've tried -xextendeddefaultrules, without progress)

why while compiling error?:

main.hs:25:21:     no instance (func_f (a -> t0)) arising use of `f'     type variable `t0' ambiguous     possible fix: add type signature fixes these type variable(s)     note: there potential instance available:       instance func_f (a -> string) -- defined @ main.hs:7:10     possible fix: add instance declaration (func_f (a -> t0))     in expression: f     in equation `x': x = f     in expression:       { let = 5                x = f a;            print 5 } 

there 1 instance func_f, haskell should able know result of x = f a. can fix error providing type manually, this: x = f :: string, not suitable case, because i'm generating haskell code , love haskell's type inferencer job me.

you're running open world assumption. basic idea ghc assumes add more class instances code. moreover, cannot control how instances exported , imported between modules. relying on having 1 instance of particular class not going work , lead weird bugs.

essentially, nothing stops you--or else, matter--from writing instance like:

 instance func_f (a -> int)    f _ = 10 

and impossible figure out 1 wanted in code. cause code break linking against module!

however, if used value, chances type constrained other parameter , ambiguity go away. example, following works:

main :: io () main =    let = 5       x = f   putstr x 

basically, 1 of cases (similar read . show) type signature unavoidable because of how ghc treats typeclass instances.


Comments