Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Small code snippet causes ghc to not terminate

Tags:

haskell

ghc

This small haskell code snippet causes ghc to not terminate during compilation:

{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}

import qualified Data.Vector.Unboxed.Mutable as MV
import Data.Vector.Unboxed ((!))
import qualified Data.Vector.Unboxed as V
import Control.Monad (forM_)

similar :: V.Vector Char -> Int
similar v = l + sum (map (similar' 1 1) checks)
  where
    (l,checks) = let h = V.head v in V.foldl'  
        (\(i,is) c -> if c == h then (i+1,i:is) else (i+1,is)) (1,[]) (V.tail v)
    similar' !r !n !i = if i < l-1 && v!(n) == v!(i+1) then similar' (r+1) (n+1) (i+1)
        else r

main :: IO ()
main = do
    n <- getLine
    v <- MV.replicate (read n) 0
    forM_ [1..read n] $ \n' -> do
      v' <- getLine
      MV.unsafeWrite v (n'-1) (similar . V.fromList $ v')
    V.unsafeFreeze v >>= V.mapM_ print

I tried ghc 7.4.1 and 7.6.1 and both do not terminate. The code worked fine when I used ByteStrings instead of Vectors. Is this a ghc problem or a library problem? or is there a problem somewhere in my code?

like image 837
haskelline Avatar asked Jan 06 '13 22:01

haskelline


1 Answers

It seems to be a GHC issue.

The compilation goes through with ghc-7.0.2 using vector-0.10.0.1 and ghc-7.0.4 using vector-0.7.0.1, and hangs with ghc-7.2.1 with vector-0.7.1, ghc-7.2.2 with vector-0.9, ghc-7.4.1 with vector-0.9.1, ghc-7.4.2 with vector-0.9.1 and ghc-7.6.1 with vector-0.9.1 and vector-0.10.0.1.

The compilation hangs in the SpecConstr pass, which the vector package makes very heavy use of. I'm going to open a ticket. Well, someone was faster.

like image 120
Daniel Fischer Avatar answered Nov 09 '22 10:11

Daniel Fischer