ghcjs / ghcjs-base

base library for GHCJS for JavaScript interaction and marshalling, used by higher level libraries like JSC
MIT License
45 stars 67 forks source link

Data.JSString.pack breaks when using irrefutable patterns #128

Open matthewbauer opened 4 years ago

matthewbauer commented 4 years ago

Using Data.JSString.pack gives incorrect results. For example:

import Data.JSString (pack)

main :: IO ()
main = print $ pack $ fst $ foldr (select (== 'a')) ([], []) "ababababa" -- "a"
  where
    -- taken from Data.OldList
    select p x ~(ts, fs) | p x       = (x:ts, fs  )
                         | otherwise = (ts  , x:fs)

Outputs "a", and it should output "aaaaa". But if we remove the ~ for irrefutable patterns, we get correct results:

import Data.JSString (pack)

main :: IO ()
main = print $ pack $ fst $ foldr (select (== 'a')) ([], []) "ababababa" -- "aaaaa"
  where
    -- taken from Data.OldList
    select p x (ts, fs) | p x       = (x:ts, fs  )
                        | otherwise = (ts  , x:fs)

I believe this issue has something to do with GHCJS packing the lazy String too early, before the fold is complete.

/cc @eskimor @luite

luite commented 4 years ago

I think this may be a bug related to the handling of selector thunks. It looks like they're not always correctly updated