-
Notifications
You must be signed in to change notification settings - Fork 40
Make List.zipWith as lazy as expected #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| {-# LANGUAGE LambdaCase #-} | ||||||
| {-# LANGUAGE BangPatterns #-} | ||||||
| {-# LANGUAGE LinearTypes #-} | ||||||
| {-# LANGUAGE ScopedTypeVariables #-} | ||||||
| {-# LANGUAGE NoImplicitPrelude #-} | ||||||
|
|
@@ -354,18 +354,34 @@ zip3 :: (Consumable a, Consumable b, Consumable c) => [a] %1 -> [b] %1 -> [c] %1 | |||||
| zip3 = zipWith3 (,,) | ||||||
|
|
||||||
| zipWith :: (Consumable a, Consumable b) => (a %1 -> b %1 -> c) -> [a] %1 -> [b] %1 -> [c] | ||||||
| zipWith f xs ys = | ||||||
| zipWith' f xs ys & \(ret, leftovers) -> | ||||||
| leftovers `lseq` ret | ||||||
| zipWith f = | ||||||
| zipWithk f (:) [] consume2 consume2 | ||||||
| where | ||||||
| consume2 :: forall x y z. (Consumable x, Consumable y) => x %1 -> y %1 -> [z] | ||||||
| consume2 x y = x `lseq` y `lseq` [] | ||||||
|
|
||||||
| -- | Same as 'zipWith', but returns the leftovers instead of consuming them. | ||||||
| -- Because the leftovers are returned at toplevel, @zipWith'@ is pretty strict: | ||||||
| -- forcing the first cons cell of the returned list forces all the recursive | ||||||
| -- calls. | ||||||
| zipWith' :: (a %1 -> b %1 -> c) -> [a] %1 -> [b] %1 -> ([c], Maybe (Either (NonEmpty a) (NonEmpty b))) | ||||||
| zipWith' _ [] [] = ([], Nothing) | ||||||
| zipWith' _ (a : as) [] = ([], Just (Left (a :| as))) | ||||||
| zipWith' _ [] (b : bs) = ([], Just (Right (b :| bs))) | ||||||
| zipWith' f (a : as) (b : bs) = | ||||||
| case zipWith' f as bs of | ||||||
| (cs, rest) -> (f a b : cs, rest) | ||||||
| zipWith' f = | ||||||
| zipWithk | ||||||
| f | ||||||
| (\c !(cs, rest) -> ((c : cs), rest)) | ||||||
| ([], Nothing) | ||||||
| (\a as -> ([], Just (Left (a :| as)))) | ||||||
| (\b bs -> ([], Just (Right (b :| bs)))) | ||||||
|
|
||||||
| zipWithk :: forall r a b c. (a %1 -> b %1 -> c) -> (c %1 -> r %1 -> r) -> r -> (a %1 -> [a] %1 -> r) -> (b %1 -> [b] %1 -> r) -> [a] %1 -> [b] %1 -> r | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be better to split the argument types on several lines and document them, if this function is exported? Which I think it should be. |
||||||
| zipWithk f cons nil lefta leftb = | ||||||
| go | ||||||
| where | ||||||
| go :: [a] %1 -> [b] %1 -> r | ||||||
| go [] [] = nil | ||||||
| go (a : as) [] = lefta a as | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not entierely clear why the |
||||||
| go [] (b : bs) = leftb b bs | ||||||
| go (a : as) (b : bs) = cons (f a b) (go as bs) | ||||||
|
|
||||||
| zipWith3 :: forall a b c d. (Consumable a, Consumable b, Consumable c) => (a %1 -> b %1 -> c %1 -> d) -> [a] %1 -> [b] %1 -> [c] %1 -> [d] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does |
||||||
| zipWith3 _ [] ys zs = (ys, zs) `lseq` [] | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| module Test.Data.List (listTests) where | ||
|
|
||
| import qualified Data.List.Linear as List | ||
| import qualified Data.Num.Linear as Num | ||
| import Hedgehog | ||
| import qualified Hedgehog.Gen as Gen | ||
| import qualified Hedgehog.Range as Range | ||
|
|
@@ -17,7 +18,9 @@ listTests = | |
| testGroup | ||
| "List tests" | ||
| [ testPropertyNamed "take n ++ drop n = id" "take_drop" take_drop, | ||
| testPropertyNamed "length . take n = const n" "take_length" take_length | ||
| testPropertyNamed "length . take n = const n" "take_length" take_length, | ||
| testPropertyNamed "zipWith is lazy" "zipWith_lazy" zipWith_lazy, | ||
| testPropertyNamed "zipWith3 is lazy" "zipWith3_lazy" zipWith3_lazy | ||
| ] | ||
|
|
||
| take_drop :: Property | ||
|
|
@@ -41,3 +44,19 @@ take_length = property $ do | |
| False -> do | ||
| annotate "Prelude.length xs < n" | ||
| Prelude.length (List.take n xs) === Prelude.length xs | ||
|
|
||
| zipWith_lazy :: Property | ||
| zipWith_lazy = property $ do | ||
| _ <- eval $ Prelude.head xs | ||
| Prelude.return () | ||
| where | ||
| xs :: [Word] | ||
| xs = List.zipWith (Num.+) (0 : error "bottom") [0 .. 42] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the motivation for using static bounds for ranges instead of property-supplied ones? |
||
|
|
||
| zipWith3_lazy :: Property | ||
| zipWith3_lazy = property $ do | ||
| _ <- eval $ Prelude.head xs | ||
| Prelude.return () | ||
| where | ||
| xs :: [Word] | ||
| xs = List.zipWith3 (\x y z -> x Num.+ y Num.+ z) (0 : error "bottom") [0 .. 42] [0 .. 57] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
zipWith3you do(x, y) `lseq` []instead, any reason to prefer one or the other?