Ticket #1369 (closed bug: wontfix)

Opened 2 years ago

Last modified 9 months ago

Type error when compiling ST-monad-like code

Reported by: koen@chalmers.se Assigned to:
Priority: normal Milestone:
Component: Compiler Version: 6.6.1
Severity: normal Keywords:
Cc: Difficulty: Unknown
Test Case: Operating System: Unknown/Multiple
Architecture: Unknown/Multiple

Description (Last modified by simonpj)

Compiling the module below works fine in GHC 6.4.2. In GHC 6.6 and 6.6.1, it gives a type error.

/Koen

{-# OPTIONS -fglasgow-exts #-}
module Bug where

import Control.Monad.ST
import Data.STRef

newtype M s a =
  MkM (STRef s Int -> ST s a)

runM :: (forall s . M s a) -> a
runM mm =
  runST (
    do ref <- newSTRef 0
       m ref
  )
 where
  MkM m = mm

-- the instance declaration and function definition 
-- of "inc" are just here for giving context; 
-- removing them still makes runM not type check in GHC 6.6

instance Monad (M s) where
  return x =
    MkM (\_ -> return x)

  MkM m >>= k =
    MkM (\ref ->
      do x <- m ref
         let MkM m' = k x
         m' ref
    )

inc :: M s Int
inc = MkM (\ref ->
  do n <- readSTRef ref
     writeSTRef ref (n+1)
     return n
  )

Attachments

Bug.hs (0.9 kB) - added by guest on 05/21/07 07:14:23.

Change History

05/21/07 07:14:23 changed by guest

  • attachment Bug.hs added.

05/22/07 05:08:25 changed by simonpj

  • description changed.

05/22/07 05:18:26 changed by simonpj

  • status changed from new to closed.
  • resolution set to wontfix.

Aha. You are the second person to discover that pattern bindings are monomorphic by default.

See http://hackage.haskell.org/trac/haskell-prime/wiki/MonomorphicPatternBindings

I've added this report to the feedback there.

Meanwhile, you can use -fno-mono-pat-binds or don't use a pattern binding. I'll mark this 'wont-fix', because the above page is the place for feedback.

Simon

05/22/07 13:23:09 changed by Isaac Dupree

Interesting, that makes two of three be newtype-unwrapping. It'd be ugly to have an exception for that, though.... In this case could

 where
  MkM m = mm

easily become

 where
  m = unM mm

for the usual newtype-unwrapping "un" function

unM :: M s a -> (STRef s Int -> ST s a)
unM (MkM m) = m

? It compiles for me with that technique, unM either as a function or a record selector works fine.

09/30/08 08:40:14 changed by simonmar

  • architecture changed from Unknown to Unknown/Multiple.

09/30/08 08:51:19 changed by simonmar

  • os changed from Unknown to Unknown/Multiple.