Skip to content

Commit a0421dd

Browse files
authored
Merge pull request #557 from haskell/simplify-enumFromTo2
Simplify rewrite rules for enumFromTo
2 parents 9f6d19f + 0eb3116 commit a0421dd

2 files changed

Lines changed: 75 additions & 130 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,23 @@ jobs:
120120
run: |
121121
cabal bench all
122122
working-directory: unpacked/
123+
# Job for checking that vector is buildable and works correctly on 32-bit
124+
# systems. Adapted from os-string's CI setup
125+
i386:
126+
needs: cabal
127+
runs-on: ubuntu-24.04
128+
steps:
129+
- name: Checkout
130+
uses: actions/checkout@v1
131+
- name: Prepare script
132+
run: |
133+
echo > run.sh <<EOF
134+
cabal update
135+
cabal build all --write-ghc-environment-files=always
136+
cabal test all --enable-tests --test-show-details=direct
137+
EOF
138+
# ----------------
139+
- name: Test
140+
uses: docker://hasufell/i386-alpine-haskell:3.12
141+
with:
142+
args: sh run.sh

‎vector/src/Data/Vector/Fusion/Bundle/Monadic.hs‎

Lines changed: 55 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE MultiParamTypeClasses #-}
88
{-# LANGUAGE RankNTypes #-}
99
{-# LANGUAGE ScopedTypeVariables #-}
10+
{-# LANGUAGE TypeApplications #-}
1011
-- |
1112
-- Module : Data.Vector.Fusion.Bundle.Monadic
1213
-- Copyright : (c) Roman Leshchinskiy 2008-2010
@@ -105,16 +106,12 @@ import Prelude
105106
, return, fmap, otherwise, id, const, seq, max, maxBound, fromIntegral, truncate
106107
, (+), (-), (<), (<=), (>), (>=), (==), (/=), (&&), (.), ($), (<$), (/) )
107108

108-
import Data.Int ( Int8, Int16, Int32 )
109+
import Data.Int ( Int8, Int16, Int32, Int64 )
109110
import Data.Word ( Word8, Word16, Word32, Word64 )
110111

111112
#include "vector.h"
112113
#include "MachDeps.h"
113114

114-
#if WORD_SIZE_IN_BITS > 32
115-
import Data.Int ( Int64 )
116-
#endif
117-
118115
data Chunk v a = Chunk Int (forall m. (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m ())
119116

120117
-- | Monadic streams
@@ -750,7 +747,7 @@ scanl' f = scanlM' (\a b -> return (f a b))
750747
-- | Haskell-style scan with strict accumulator and a monadic operator
751748
scanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
752749
{-# INLINE scanlM' #-}
753-
scanlM' f z s = z `seq` (z `cons` postscanlM f z s)
750+
scanlM' f !z s = z `cons` postscanlM f z s
754751

755752
-- | Initial-value free scan over a 'Bundle'
756753
scanl1 :: Monad m => (a -> a -> a) -> Bundle m v a -> Bundle m v a
@@ -797,46 +794,27 @@ enumFromTo x y = fromList [x .. y]
797794
-- NOTE: We use (x+1) instead of (succ x) below because the latter checks for
798795
-- overflow which can't happen here.
799796

797+
stepEnumFromToNum
798+
:: (Monad m, Ord a, Num a)
799+
=> a -> Maybe a -> m (Step (Maybe a) a)
800+
{-# INLINE_INNER stepEnumFromToNum #-}
801+
stepEnumFromToNum y = \st -> case st of
802+
Nothing -> return $ Done
803+
Just z
804+
| z == y -> return $ Yield z Nothing
805+
| z < y -> return $ Yield z (Just (z+1))
806+
| otherwise -> return $ Done
807+
808+
800809
-- FIXME: add "too large" test for Int
801810
enumFromTo_small :: (Integral a, Monad m) => a -> a -> Bundle m v a
802811
{-# INLINE_FUSED enumFromTo_small #-}
803-
enumFromTo_small x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact n)
812+
enumFromTo_small !x !y
813+
= fromStream (Stream (stepEnumFromToNum y) (Just x)) (Exact n)
804814
where
805815
n = delay_inline max (fromIntegral y - fromIntegral x + 1) 0
806816

807-
{-# INLINE_INNER step #-}
808-
step Nothing = return $ Done
809-
step (Just z) | z == y = return $ Yield z Nothing
810-
| z < y = return $ Yield z (Just (z+1))
811-
| otherwise = return $ Done
812-
813-
{-# RULES
814-
815-
"enumFromTo<Int8> [Bundle]"
816-
enumFromTo = enumFromTo_small :: Monad m => Int8 -> Int8 -> Bundle m v Int8
817-
818-
"enumFromTo<Int16> [Bundle]"
819-
enumFromTo = enumFromTo_small :: Monad m => Int16 -> Int16 -> Bundle m v Int16
820-
821-
"enumFromTo<Word8> [Bundle]"
822-
enumFromTo = enumFromTo_small :: Monad m => Word8 -> Word8 -> Bundle m v Word8
823-
824-
"enumFromTo<Word16> [Bundle]"
825-
enumFromTo = enumFromTo_small :: Monad m => Word16 -> Word16 -> Bundle m v Word16 #-}
826-
827-
828-
829-
#if WORD_SIZE_IN_BITS > 32
830-
831-
{-# RULES
832817

833-
"enumFromTo<Int32> [Bundle]"
834-
enumFromTo = enumFromTo_small :: Monad m => Int32 -> Int32 -> Bundle m v Int32
835-
836-
"enumFromTo<Word32> [Bundle]"
837-
enumFromTo = enumFromTo_small :: Monad m => Word32 -> Word32 -> Bundle m v Word32 #-}
838-
839-
#endif
840818

841819
-- NOTE: We could implement a generic "too large" test:
842820
--
@@ -852,7 +830,8 @@ enumFromTo_small x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact
852830

853831
enumFromTo_int :: forall m v. (HasCallStack, Monad m) => Int -> Int -> Bundle m v Int
854832
{-# INLINE_FUSED enumFromTo_int #-}
855-
enumFromTo_int x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact (len x y))
833+
enumFromTo_int !x !y
834+
= fromStream (Stream (stepEnumFromToNum y) (Just x)) (Exact (len x y))
856835
where
857836
{-# INLINE [0] len #-}
858837
len :: HasCallStack => Int -> Int -> Int
@@ -861,15 +840,10 @@ enumFromTo_int x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact (l
861840
where
862841
n = v-u+1
863842

864-
{-# INLINE_INNER step #-}
865-
step Nothing = return $ Done
866-
step (Just z) | z == y = return $ Yield z Nothing
867-
| z < y = return $ Yield z (Just (z+1))
868-
| otherwise = return $ Done
869-
870843
enumFromTo_intlike :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
871844
{-# INLINE_FUSED enumFromTo_intlike #-}
872-
enumFromTo_intlike x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact (len x y))
845+
enumFromTo_intlike !x !y
846+
= fromStream (Stream (stepEnumFromToNum y) (Just x)) (Exact (len x y))
873847
where
874848
{-# INLINE [0] len #-}
875849
len :: HasCallStack => a -> a -> Int
@@ -880,34 +854,10 @@ enumFromTo_intlike x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exac
880854
where
881855
n = v-u+1
882856

883-
{-# INLINE_INNER step #-}
884-
step Nothing = return $ Done
885-
step (Just z) | z == y = return $ Yield z Nothing
886-
| z < y = return $ Yield z (Just (z+1))
887-
| otherwise = return $ Done
888-
889-
{-# RULES
890-
891-
"enumFromTo<Int> [Bundle]"
892-
enumFromTo = enumFromTo_int :: Monad m => Int -> Int -> Bundle m v Int
893-
894-
#if WORD_SIZE_IN_BITS > 32
895-
896-
"enumFromTo<Int64> [Bundle]"
897-
enumFromTo = enumFromTo_intlike :: Monad m => Int64 -> Int64 -> Bundle m v Int64 #-}
898-
899-
#else
900-
901-
"enumFromTo<Int32> [Bundle]"
902-
enumFromTo = enumFromTo_intlike :: Monad m => Int32 -> Int32 -> Bundle m v Int32 #-}
903-
904-
#endif
905-
906-
907-
908857
enumFromTo_big_word :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
909858
{-# INLINE_FUSED enumFromTo_big_word #-}
910-
enumFromTo_big_word x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact (len x y))
859+
enumFromTo_big_word !x !y
860+
= fromStream (Stream (stepEnumFromToNum y) (Just x)) (Exact (len x y))
911861
where
912862
{-# INLINE [0] len #-}
913863
len :: HasCallStack => a -> a -> Int
@@ -918,40 +868,13 @@ enumFromTo_big_word x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exa
918868
where
919869
n = v-u
920870

921-
{-# INLINE_INNER step #-}
922-
step Nothing = return $ Done
923-
step (Just z) | z == y = return $ Yield z Nothing
924-
| z < y = return $ Yield z (Just (z+1))
925-
| otherwise = return $ Done
926-
927-
{-# RULES
928-
929-
"enumFromTo<Word> [Bundle]"
930-
enumFromTo = enumFromTo_big_word :: Monad m => Word -> Word -> Bundle m v Word
931-
932-
"enumFromTo<Word64> [Bundle]"
933-
enumFromTo = enumFromTo_big_word
934-
:: Monad m => Word64 -> Word64 -> Bundle m v Word64
935871

936872
#if WORD_SIZE_IN_BITS == 32
937-
938-
"enumFromTo<Word32> [Bundle]"
939-
enumFromTo = enumFromTo_big_word
940-
:: Monad m => Word32 -> Word32 -> Bundle m v Word32
941-
942-
#endif
943-
944-
"enumFromTo<Integer> [Bundle]"
945-
enumFromTo = enumFromTo_big_word
946-
:: Monad m => Integer -> Integer -> Bundle m v Integer #-}
947-
948-
949-
#if WORD_SIZE_IN_BITS > 32
950-
951873
-- FIXME: the "too large" test is totally wrong
952874
enumFromTo_big_int :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
953875
{-# INLINE_FUSED enumFromTo_big_int #-}
954-
enumFromTo_big_int x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exact (len x y))
876+
enumFromTo_big_int !x !y
877+
= fromStream (Stream (stepEnumFromToNum y) (Just x)) (Exact (len x y))
955878
where
956879
{-# INLINE [0] len #-}
957880
len :: HasCallStack => a -> a -> Int
@@ -961,26 +884,11 @@ enumFromTo_big_int x y = x `seq` y `seq` fromStream (Stream step (Just x)) (Exac
961884
$ fromIntegral n
962885
where
963886
n = v-u+1
964-
965-
{-# INLINE_INNER step #-}
966-
step Nothing = return $ Done
967-
step (Just z) | z == y = return $ Yield z Nothing
968-
| z < y = return $ Yield z (Just (z+1))
969-
| otherwise = return $ Done
970-
971-
972-
{-# RULES
973-
974-
"enumFromTo<Int64> [Bundle]"
975-
enumFromTo = enumFromTo_big_int :: Monad m => Int64 -> Int64 -> Bundle m v Int64 #-}
976-
977-
978-
979887
#endif
980888

981889
enumFromTo_char :: Monad m => Char -> Char -> Bundle m v Char
982890
{-# INLINE_FUSED enumFromTo_char #-}
983-
enumFromTo_char x y = x `seq` y `seq` fromStream (Stream step xn) (Exact n)
891+
enumFromTo_char !x !y = fromStream (Stream step xn) (Exact n)
984892
where
985893
xn = ord x
986894
yn = ord y
@@ -991,11 +899,6 @@ enumFromTo_char x y = x `seq` y `seq` fromStream (Stream step xn) (Exact n)
991899
step zn | zn <= yn = return $ Yield (unsafeChr zn) (zn+1)
992900
| otherwise = return $ Done
993901

994-
{-# RULES
995-
996-
"enumFromTo<Char> [Bundle]"
997-
enumFromTo = enumFromTo_char #-}
998-
999902

1000903

1001904
------------------------------------------------------------------------
@@ -1005,7 +908,7 @@ enumFromTo_char x y = x `seq` y `seq` fromStream (Stream step xn) (Exact n)
1005908

1006909
enumFromTo_double :: forall m v a. (HasCallStack, Monad m, Ord a, RealFrac a) => a -> a -> Bundle m v a
1007910
{-# INLINE_FUSED enumFromTo_double #-}
1008-
enumFromTo_double n m = n `seq` m `seq` fromStream (Stream step ini) (Max (len n lim))
911+
enumFromTo_double !n !m = fromStream (Stream step ini) (Max (len n lim))
1009912
where
1010913
lim = m + 1/2 -- important to float out
1011914

@@ -1025,14 +928,36 @@ enumFromTo_double n m = n `seq` m `seq` fromStream (Stream step ini) (Max (len n
1025928
x' = x + n
1026929

1027930
{-# RULES
931+
"enumFromTo<Int8> [Bundle]" enumFromTo @Int8 = enumFromTo_small
932+
"enumFromTo<Int16> [Bundle]" enumFromTo @Int16 = enumFromTo_small
933+
"enumFromTo<Word8> [Bundle]" enumFromTo @Word8 = enumFromTo_small
934+
"enumFromTo<Word16> [Bundle]" enumFromTo @Word16 = enumFromTo_small
1028935

1029-
"enumFromTo<Double> [Bundle]"
1030-
enumFromTo = enumFromTo_double :: Monad m => Double -> Double -> Bundle m v Double
1031-
1032-
"enumFromTo<Float> [Bundle]"
1033-
enumFromTo = enumFromTo_double :: Monad m => Float -> Float -> Bundle m v Float #-}
936+
"enumFromTo<Int> [Bundle]" enumFromTo @Int = enumFromTo_int
937+
"enumFromTo<Word> [Bundle]" enumFromTo @Word = enumFromTo_big_word
938+
"enumFromTo<Word64> [Bundle]" enumFromTo @Word64 = enumFromTo_big_word
939+
"enumFromTo<Integer> [Bundle]" enumFromTo @Integer = enumFromTo_big_word
1034940

941+
"enumFromTo<Char> [Bundle]" enumFromTo @Char = enumFromTo_char
942+
"enumFromTo<Double> [Bundle]" enumFromTo @Double = enumFromTo_double
943+
"enumFromTo<Float> [Bundle]" enumFromTo @Float = enumFromTo_double
944+
#-}
1035945

946+
#if WORD_SIZE_IN_BITS > 32
947+
-- 64bit systems
948+
{-# RULES
949+
"enumFromTo<Int32> [Bundle]" enumFromTo @Int32 = enumFromTo_small
950+
"enumFromTo<Int64> [Bundle]" enumFromTo @Int64 = enumFromTo_intlike
951+
"enumFromTo<Word32> [Bundle]" enumFromTo @Word32 = enumFromTo_small
952+
#-}
953+
#else
954+
-- 32bit systems
955+
{-# RULES
956+
"enumFromTo<Int32> [Bundle]" enumFromTo @Int32 = enumFromTo_intlike
957+
"enumFromTo<Word32> [Bundle]" enumFromTo @Word32 = enumFromTo_big_word
958+
"enumFromTo<Int64> [Bundle]" enumFromTo @Int64 = enumFromTo_big_int
959+
#-}
960+
#endif
1036961

1037962
------------------------------------------------------------------------
1038963

@@ -1071,7 +996,7 @@ unsafeFromList sz xs = fromStream (S.fromList xs) sz
1071996

1072997
fromVector :: (Monad m, Vector v a) => v a -> Bundle m v a
1073998
{-# INLINE_FUSED fromVector #-}
1074-
fromVector v = v `seq` n `seq` Bundle (Stream step 0)
999+
fromVector !v = n `seq` Bundle (Stream step 0)
10751000
(Stream vstep True)
10761001
(Just v)
10771002
(Exact n)

0 commit comments

Comments
 (0)