[Haskell-cafe] Re: [Haskell] question about a failure to generalize

Tim Chevalier catamorphism at gmail.com
Sat Sep 15 16:56:56 EDT 2007


[replying to haskell-cafe]

On 9/15/07, Norman Ramsey <nr at eecs.harvard.edu> wrote:
> Dear Haskellers,
>
> I've had a Haskell program rejected for reasons I don't understand.
> Here's the relevant bit of code; the problem is that I expected the
> type-inference engine to generalize the abbreviation 'fold' to an
> overloaded function, but it doesn't---to make the code work, I had to
> expand 'fold' into 'foldRegsUsed' everywhere it appears.  I'm baffled
> because 'fold' isn't mutually recursive with anything, so I would have
> expected ordinary Hindley-Milner style inference to generalize it to
> something of the type
>
>   UserOfLocalRegs a => (b -> LocalReg -> b) -> b -> a -> b
>
> But that's not what happens.  This code fails to compile because the
> compiler is willing to use 'fold' at only one type (CmmExpr as it happens):
>
>   class UserOfLocalRegs a where
>     foldRegsUsed :: (b -> LocalReg -> b) -> b -> a -> b
>
>   instance UserOfLocalRegs Middle where
>       foldRegsUsed f z m = middle m
>         where middle (MidComment {})                = z
>               middle (MidAssign _lhs expr)          = fold f z expr
>               middle (MidStore addr rval)           = fold f (fold f z addr) rval
>               middle (MidUnsafeCall tgt _ress args) = fold f (fold f z tgt) args
>               middle (CopyIn _ _formals _)          = z
>               middle (CopyOut _ actuals)            = fold f z actuals
>               fold = foldRegsUsed
>
>
> What rule of the language have I overlooked?
>

Monomorphism restriction? Replacing fold with foldRegsUsed would work
because there's a type signature for foldRegsUsed.

Cheers,
Tim

-- 
Tim Chevalier * catamorphism.org * Often in error, never in doubt
"...There is no mystery; there is only paradox, the incontrovertible
union of contradictory truths." -- Edward Abbey


More information about the Haskell-Cafe mailing list