haskell / text

Haskell library for space- and time-efficient operations over Unicode text.

Home Page:http://hackage.haskell.org/package/text

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

filter/filter rules are incorrect

meooow25 opened this issue · comments

import qualified Data.Text as T
main = print $ T.filter (const undefined) $ T.filter (const False) $ T.singleton 'a'

Without optimizations, prints

""

With optimizations,

tfilter: Prelude.undefined
CallStack (from HasCallStack):
  undefined, called at tfilter.hs:2:32 in main:Main

text/src/Data/Text.hs

Lines 1660 to 1663 in 456783a

{-# RULES
"TEXT filter/filter -> filter" forall p q t.
filter p (filter q t) = filter (\c -> p c && q c) t
#-}

text/src/Data/Text/Lazy.hs

Lines 1691 to 1694 in 456783a

{-# RULES
"TEXT filter/filter -> filter" forall p q t.
filter p (filter q t) = filter (\c -> p c && q c) t
#-}

The problem is that the predicate of the inner filter is not checked first.

The rule in the stream code is, however, correct

{-# RULES
"STREAM filter/filter fusion" forall p q s.
filter p (filter q s) = filter (\x -> q x && p x) s
#-}

For the record, I happened to notice this in the code. I did not find this out the hard way.