Ticket #1369 (closed bug: wontfix)

Opened 5 years ago

Last modified 3 years ago

Type error when compiling ST-monad-like code

Reported by: koen@… Owned by:
Priority: normal Milestone:
Component: Compiler Version: 6.6.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description (last modified by simonpj) (diff)

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 Download (0.9 KB) - added by guest 5 years ago.

Change History

Changed 5 years ago by guest

Changed 5 years ago by simonpj

  • description modified (diff)

Changed 5 years ago 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

Changed 5 years ago 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.

Changed 3 years ago by simonmar

  • architecture changed from Unknown to Unknown/Multiple

Changed 3 years ago by simonmar

  • os changed from Unknown to Unknown/Multiple
Note: See TracTickets for help on using tickets.