[Haskell] Partially applied type class functions

Paul Govereau govereau at eecs.harvard.edu
Sat Aug 6 12:05:12 EDT 2005



Thanks alot for the clarifications. If I understand correctly, I ran
into the monomorphism restriction because of the constraint on the
type variable. The Haskell report says this in Section 4.5.5:

  In addition, the constrained type variables of a restricted
  declaration group may not be generalized in the generalization step
  for that group.

Thus, this is perfectly fine:

> f1 :: Int -> a -> String
> f1 i a = ""
> x = let g = f1 1 in (g 1, g "a")

Whereas this definition, with the additional (Show a) constraint, runs
in to the monomorphism restriction:

> f2 :: Show a => Int -> a -> String
> f2 i a = show a
> y = let g = f2 1 in (g 1, g "a")

  No instance for (Num [Char])
    arising from the literal `1' at Syntax.lhs:16:24
  Probable fix: add an instance declaration for (Num [Char])
  In the first argument of `g', namely `1'
  In the definition of `y': y = let g = f2 1 in (g 1, g "a")

I think my original program was a case similar to this last one.

Thanks again for the quick response.
Paul

On Aug 05, Roberto Zunino wrote:
> Paul Govereau wrote:
> [snip]
> >>instance AbSyn Constraint where
> >>    subst e n constr =
> >>        let sub = subst e n   -- :: AbSyn a => a -> a
> >>        in case constr
> >>           of Zero expr -> Zero (sub expr)
> >>              AndL cs   -> AndL (sub cs)
> >
> >It looks sort of like sub is being monomorphised -- or something?
> 
> I think you just hit the monomorphism restriction. For the details, see:
> http://www.haskell.org/onlinereport/decls.html   -  Sect. 4.5.5
> http://haskell.org/hawiki/MonomorphismRestriction
> 
> Possible solutions for your specific case are:
> 
> 1) add an explicit type signature for sub
> 
>     let sub :: AbSyn a => a -> a
>          sub = subst e n
>     in ...
> 
> 2) eta-expand sub, so its definition becomes a function binding
> 
>     let sub c = subst e n c
>     in ...
> 
> 
> Regards,
> Roberto Zunino.
> 




More information about the Haskell mailing list