Skip to content

Bad code with type families

{-# LANGUAGE TypeFamilies, ScopedTypeVariables #-}
module Foo (foo) where

import Control.Monad.ST
import Data.STRef

class V v where
  type M v :: * -> *

  rd :: v -> Int -> M v Int

data Vec s = Vec (STRef s Int)

instance V (Vec s) where
  type M (Vec s) = ST s

  rd (Vec r) n = do
                   m <- readSTRef r
                   return (m+n)

foo :: forall s. Vec s -> Int -> ST s Int
foo v n = go n
  where
    go 0 = return 0
    go n = do
             m <- rd v n -- :: ST s Int
             go m

ghc -O2 generates rather bad code for this (look at $wfoo). If I uncomment the type signature in the second to last line, the code becomes much better. It would be nice if this worked without the signature since I can't add it in the real code.

Trac metadata
Trac field Value
Version 6.9
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Unknown
Architecture Unknown
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information