Re: [Haskell] question about a failure to generalize

2007-09-17 Thread Norman Ramsey
 > If so, try working around the monomorphism restriction by changing  
 > from a pattern binding to a function binding.
 > 
 >  fold f = foldRegsUsed f

Brilliant.  I hadn't known about the monomorphism restriction.
Now I know it's a 'necessary evil'.  Thanks!


Norman
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] question about a failure to generalize

2007-09-16 Thread Tom Pledger

Quoting Stefan O'Rear <[EMAIL PROTECTED]>:


On Mon, Sep 17, 2007 at 04:15:10PM +1200, Tom Pledger wrote:

Norman Ramsey wrote:
 :
 | This code fails to compile because the compiler is willing to
 | use 'fold' at only one type (CmmExpr as it happens)
 :

When it failed to compile, was

fold = foldRegsUsed

a top-level declaration in the module, rather than local to foldRegsUsed?

If so, try working around the monomorphism restriction by changing from a
pattern binding to a function binding.

fold f = foldRegsUsed f


The monomorphism restriction is not affected by top-level-or-not, see
sections 4.5.1 and 4.5.5 in the Haskell 98 Language and Libraries
Report.

Stefan



Aargh!  Sorry, I'm having a Bad Details Day.  3 in 1 message:

  - Overlooking the "to make the code work, I had to expand 'fold'
into 'foldRegsUsed' everywhere it appears" part in the original
question,

  - Chopping a letter out of Norman's email address, and

  - Mixing up the MR with other issues, possibly something from the
binding groups section of Typing Haskell In Haskell.

The one saving grace is that I tested my suggestion before posting,  
and it worked.  :-)


- Tom


___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] question about a failure to generalize

2007-09-16 Thread Stefan O'Rear
On Mon, Sep 17, 2007 at 04:15:10PM +1200, Tom Pledger wrote:
> Norman Ramsey wrote:
>  :
>  | This code fails to compile because the compiler is willing to
>  | use 'fold' at only one type (CmmExpr as it happens)
>  :
>
> When it failed to compile, was
>
> fold = foldRegsUsed
>
> a top-level declaration in the module, rather than local to foldRegsUsed?
>
> If so, try working around the monomorphism restriction by changing from a 
> pattern binding to a function binding.
>
> fold f = foldRegsUsed f

The monomorphism restriction is not affected by top-level-or-not, see
sections 4.5.1 and 4.5.5 in the Haskell 98 Language and Libraries
Report.

Stefan


signature.asc
Description: Digital signature
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[Haskell] question about a failure to generalize

2007-09-16 Thread Tom Pledger

Norman Ramsey wrote:
 :
 | This code fails to compile because the compiler is willing to
 | use 'fold' at only one type (CmmExpr as it happens)
 :

When it failed to compile, was

fold = foldRegsUsed

a top-level declaration in the module, rather than local to foldRegsUsed?

If so, try working around the monomorphism restriction by changing  
from a pattern binding to a function binding.


fold f = foldRegsUsed f

Regards,
Tom


___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[Haskell] question about a failure to generalize

2007-09-15 Thread Norman Ramsey
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?



Norman

___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell