Skip to content

Simplify rewrite rules for enumFromTo - #557

Merged
Shimuuar merged 8 commits into
masterfrom
simplify-enumFromTo2
Oct 5, 2026
Merged

Shimuuar merged 8 commits into
masterfrom
simplify-enumFromTo2

Conversation

@Shimuuar

Copy link
Copy Markdown
Contributor

This is a simplification of rewrite rules: use type application for compactness and group them all together.

Also I found that one of them is wrong. Rule clearly intended for 32-bit builds was enabled for 64-bit instead.

#if WORD_SIZE_IN_BITS > 32
-- FIXME: the "too large" test is totally wrong
enumFromTo_big_int :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
{-# INLINE_FUSED enumFromTo_big_int #-}
enumFromTo_big_int x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact (len x y))
where
{-# INLINE [0] len #-}
len :: HasCallStack => a -> a -> Int
len u v | u > v = 0
| otherwise = check Bounds "vector too large"
(n > 0 && n <= fromIntegral (maxBound :: Int))
$ fromIntegral n
where
n = v-u+1
{-# INLINE_INNER step #-}
step Nothing = return $ Done
step (Just z) | z == y = return $ Yield z Nothing
| z < y = return $ Yield z (Just (z+1))
| otherwise = return $ Done
{-# RULES
"enumFromTo<Int64> [Bundle]"
enumFromTo = enumFromTo_big_int :: Monad m => Int64 -> Int64 -> Bundle m v Int64 #-}
#endif

Compare

#if WORD_SIZE_IN_BITS > 32
"enumFromTo<Int64> [Bundle]"
enumFromTo = enumFromTo_intlike :: Monad m => Int64 -> Int64 -> Bundle m v Int64 #-}
#else

That made me think. We notionally support 32-bit platforms, we have CPP for them, but we don't even test buildability on CI. Does anyone has ideas how to add such tests?

@konsumlamm

Copy link
Copy Markdown
Contributor

That made me think. We notionally support 32-bit platforms, we have CPP for them, but we don't even test buildability on CI. Does anyone has ideas how to add such tests?

GitHub actions provide an i386 container, see for example https://github.com/Bodigrim/bitvec/blob/95cdcf0c046b94af284ab4e7903da4d435fbf5c5/.github/workflows/ci.yaml#L49-L67. It's also possible to emulate various architectures (see below that), but that is significantly slower. You could also consider testing the JS and WASM backends (see for example https://github.com/Bodigrim/bitvec/blob/95cdcf0c046b94af284ab4e7903da4d435fbf5c5/.github/workflows/ci.yaml#L96-L159).

@Shimuuar
Shimuuar force-pushed the simplify-enumFromTo2 branch from 3f76893 to 763368e Compare January 18, 2026 20:51
@Shimuuar

Copy link
Copy Markdown
Contributor Author

And surely enough I did break build on 32 bit platform.

GHC9.0 & 9.2 fail on CI again with identical symptoms. I reset cache again. It helped but it can break again. I'm not sure what to do about it in that case

I need to recheck enumFromTo_big_int and friends. Whole situation looks suspicious,

Note that #ifdef condition was wrong! We had 2 rules on 64 bits and none at 32
bit.

Lesson: #ifdef spaghetti is bad. It's better to keep conditions few and local.
Setup is copied from bitvec

Also reset CI cache again. GHC 9.0 is failing again with same symptoms
@Shimuuar
Shimuuar force-pushed the simplify-enumFromTo2 branch from 763368e to ba76084 Compare October 4, 2026 18:55
@Shimuuar

Shimuuar commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

I think this PR is as good as it going to be. It does simplify code and does fix rewrites on 32-bit systems. Maybe it's possible to make better. Maybe

@Shimuuar
Shimuuar force-pushed the simplify-enumFromTo2 branch 2 times, most recently from e15d91b to 19b1a2b Compare October 4, 2026 20:29
Previous setup used Ubuntu bionic (18.04) which is too old and ghcup started
failing to install GHC (libc is too old)

Instead copy setup from os-string. It will hopefully work
@Shimuuar
Shimuuar force-pushed the simplify-enumFromTo2 branch from 1408f2f to 0eb3116 Compare October 4, 2026 22:04
@Shimuuar
Shimuuar merged commit a0421dd into master Oct 5, 2026
23 checks passed
@Shimuuar
Shimuuar deleted the simplify-enumFromTo2 branch October 5, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants