[Haskell-cafe] Re: Top Level etc.

Ben Rudiak-Gould Benjamin.Rudiak-Gould at cl.cam.ac.uk
Thu Jan 20 11:38:01 EST 2005


Jim Apple wrote:

 > Does anyone have examples of these? This one scares the foo out of me:
 >
 >>> * It's not even safe in general to add a signature giving the same type
 >>> that the compiler would infer anyway

Here's an example:

    > len :: [a] -> Int
    >
    > len xs = let ?accum = 0 in len' xs
    >
    > len' [] = ?accum
    > len' (x:xs) = let ?accum = ?accum + (1::Int) in len' xs

    *Main> :t len'
    len' :: forall a. (?accum :: Int) => [a] -> Int
    *Main> len "hello"
    0

    > len :: [a] -> Int
    >
    > len xs = let ?accum = 0 in len' xs
    >
    > len' :: forall a. (?accum :: Int) => [a] -> Int
    >
    > len' [] = ?accum
    > len' (x:xs) = let ?accum = ?accum + (1::Int) in len' xs

    *Main> :t len'
    len' :: forall a. (?accum :: Int) => [a] -> Int
    *Main> len "hello"
    5

This happens as a side effect of the way that type inference currently 
works on recursive binding groups. It happens with typeclass 
dictionaries too, but it isn't observable because they can't be rebound 
in a local scope.

-- Ben



More information about the Haskell-Cafe mailing list