Inferred type not most general?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Inferred type not most general?

Greg Buchholz

    I'm curious about a type inference oddity.  In the code below, if I
leave off the type signature for tmap, both GHC and Hugs infer that tmap
has type...

    tmap :: (b -> a, b -> a) -> Twist b b -> Twist a a

...I'm wondering why they couldn't infer the more general...

    tmap :: (a -> b, c -> d) -> Twist a c -> Twist b d

>data Twist a b = Nil | Cons a (Twist b a) deriving Show
>
>x = (Cons "foo" (Cons 1 (Cons "bar" (Cons 2 Nil))))
>
>tmap :: (a->b,c->d) -> Twist a c -> Twist b d
>tmap _ Nil = Nil
>tmap (f,g) (Cons x rest) = Cons (f x) (tmap (g,f) rest)

Thanks,

Greg Buchholz
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Inferred type not most general?

Malcolm Wallace
Greg Buchholz <[hidden email]> writes:

>     tmap :: (b -> a, b -> a) -> Twist b b -> Twist a a
>
> ...I'm wondering why they couldn't infer the more general...
>
>     tmap :: (a -> b, c -> d) -> Twist a c -> Twist b d

Because the latter type involves polymorphic recursion.  Standard H-M
cannot infer a poly recursive type, but Haskell is willing to check it
if you give it a signature.

Regards,
    Malcolm
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe