Why doesn't this work?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Why doesn't this work?

Andrew Coppin
colour_grid :: (Particle -> IO ()) -> Grid ph -> IO ()
colour_grid fn g = sequence_ $ runST $ do
  ps <- grid_coords g

  mapM
    (\pix -> do
      particle <- read_grid g pix
      return $ fn particle
    )
    ps

When I attempt to run this, GHCi just gives me a very cryptic type
checker error. I can't figure out what's wrong here. As far as I can
tell, this should run...

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

Re: Why doesn't this work?

Bugzilla from alfonso.acosta@gmail.com
I haven't tried to run the code, but my first bet is that, due to the
rank-2 polymorphism of ST, you should use parenthesis instead of $ in
the case of runST.

On Sun, Aug 24, 2008 at 3:25 PM, Andrew Coppin
<[hidden email]> wrote:

> colour_grid :: (Particle -> IO ()) -> Grid ph -> IO ()
> colour_grid fn g = sequence_ $ runST $ do
>  ps <- grid_coords g
>
>  mapM
>   (\pix -> do
>     particle <- read_grid g pix
>     return $ fn particle
>   )
>   ps
>
> When I attempt to run this, GHCi just gives me a very cryptic type checker
> error. I can't figure out what's wrong here. As far as I can tell, this
> should run...
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this work?

Yitzchak Gale
Alfonso Acosta wrote:
> I haven't tried to run the code, but my first bet is that, due to the
> rank-2 polymorphism of ST, you should use parenthesis instead of $ in
> the case of runST.

Perhaps if Andrew is using an old compiler.
That is no longer a problem in recent versions of GHC.

A more basic issue is that fn is in the IO monad,
but its use inside the mapM will need it to be in the ST
monad.

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

Re: Why doesn't this work?

Daniel Fischer-4
Am Sonntag, 24. August 2008 17:21 schrieb Yitzchak Gale:

> Alfonso Acosta wrote:
> > I haven't tried to run the code, but my first bet is that, due to the
> > rank-2 polymorphism of ST, you should use parenthesis instead of $ in
> > the case of runST.
>
> Perhaps if Andrew is using an old compiler.
> That is no longer a problem in recent versions of GHC.
>
> A more basic issue is that fn is in the IO monad,
> but its use inside the mapM will need it to be in the ST
> monad.

No,
return (fn particle) :: ST s (IO ())
, so that's fine.

Indeed, filling in a few dummies, the code compiles with the 6.8 branch.

>
> Regards,
> Yitz

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

Re: Why doesn't this work?

Emil Axelsson-2
In reply to this post by Andrew Coppin
BTW, this is a case where it may be more convenient to use forM:

  forM ps $ \pix -> do
    particle <- read_grid g pix
    return $ fn particle

(untested...)

forM is just another way of saying (flip mapM).

/ Emil



Andrew Coppin skrev:

> colour_grid :: (Particle -> IO ()) -> Grid ph -> IO ()
> colour_grid fn g = sequence_ $ runST $ do
>  ps <- grid_coords g
>
>  mapM
>    (\pix -> do
>      particle <- read_grid g pix
>      return $ fn particle
>    )
>    ps
>
> When I attempt to run this, GHCi just gives me a very cryptic type
> checker error. I can't figure out what's wrong here. As far as I can
> tell, this should run...
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this work?

Yitzchak Gale
In reply to this post by Daniel Fischer-4
I wrote:
>> A more basic issue is that fn is in the IO monad,
>> but its use inside the mapM will need it to be in the ST
>> monad.

Daniel Fischer wrote:
> No,
> return (fn particle) :: ST s (IO ())
> , so that's fine.

Ah, true. But I doubt that Andrew really meant to
do the calculation in ST s (IO ()).

> Indeed, filling in a few dummies, the code compiles
> with the 6.8 branch.

So then the only way to know why it didn't compile
for Andrew is if we see more of the code.

But that's only a technical issue. I hope that now
Andrew has a better idea about what's wrong.
If not - please let us know.

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