Skip to content

Commit a89eab5

Browse files
authored
Merge pull request #486 from exaexa/patch-1
do not drop extra elements in Data.List.Linear drop and take
2 parents 9a7dc1b + 4ab7dc7 commit a89eab5

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

.github/workflows/ci.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
ghc-version: [96, 98, 910]
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v4
1717
- uses: cachix/install-nix-action@v15
1818
with:
1919
nix_path: "${{ env.nixpkgs-url }}"
2020
- name: Cache Cabal dependencies
21-
uses: actions/cache@v2
21+
uses: actions/cache@v4
2222
with:
2323
path: |
2424
~/.cabal/packages
@@ -45,7 +45,7 @@ jobs:
4545
- name: Run benchmarks
4646
run: nix-shell --arg ghcVersion '"${{ matrix.ghc-version }}"' --arg installHls 'false' --pure --run "cabal bench 2>&1 | tee benchmark_ghc${{ matrix.ghc-version }}.txt"
4747
- name: Upload benchmark results
48-
uses: actions/upload-artifact@v3
48+
uses: actions/upload-artifact@v4
4949
with:
5050
name: linear-base_benchmarks_ghc${{ matrix.ghc-version }}
5151
path: |
@@ -56,12 +56,12 @@ jobs:
5656
name: check formatting with ormolu
5757
runs-on: ubuntu-latest
5858
steps:
59-
- uses: actions/checkout@v2
59+
- uses: actions/checkout@v4
6060
- uses: cachix/install-nix-action@v15
6161
with:
6262
nix_path: "${{ env.nixpkgs-url }}"
6363
- name: Cache Stack dependencies
64-
uses: actions/cache@v2
64+
uses: actions/cache@v4
6565
with:
6666
path: ~/.stack
6767
key: stack-deps-ormolu-${{ runner.os }}-${{ hashFiles('nix/sources.json') }}-v${{ env.cache-invalidation-key }}-${{ hashFiles('stack.yaml.lock') }}-${{ github.sha }}
@@ -75,12 +75,12 @@ jobs:
7575
name: stack build
7676
runs-on: ubuntu-latest
7777
steps:
78-
- uses: actions/checkout@v2
78+
- uses: actions/checkout@v4
7979
- uses: cachix/install-nix-action@v15
8080
with:
8181
nix_path: "${{ env.nixpkgs-url }}"
8282
- name: Cache Stack dependencies
83-
uses: actions/cache@v2
83+
uses: actions/cache@v4
8484
with:
8585
path: ~/.stack
8686
key: stack-deps-${{ runner.os }}-${{ hashFiles('nix/sources.json') }}-v${{ env.cache-invalidation-key }}-${{ hashFiles('stack.yaml.lock', 'linear-base.cabal') }}-${{ github.sha }}

linear-base.cabal

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ test-suite test
170170
hs-source-dirs: test
171171
other-modules:
172172
Test.Data.Destination
173+
Test.Data.Functor.Linear
174+
Test.Data.List
173175
Test.Data.Mutable.Array
174-
Test.Data.Mutable.Vector
175176
Test.Data.Mutable.HashMap
176177
Test.Data.Mutable.Set
178+
Test.Data.Mutable.Vector
177179
Test.Data.Polarized
178-
Test.Data.Functor.Linear
179-
Test.Data.V
180180
Test.Data.Replicator
181+
Test.Data.V
181182
default-language: Haskell2010
182183
build-depends:
183184
inspection-testing,

src/Data/List/Linear.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ dropWhile p (x : xs) =
191191
take :: (Consumable a) => Int -> [a] %1 -> [a]
192192
take _ [] = []
193193
take i (x : xs)
194-
| i Prelude.< 0 = (x, xs) `lseq` []
194+
| i Prelude.<= 0 = (x, xs) `lseq` []
195195
| otherwise = x : take (i - 1) xs
196196

197197
drop :: (Consumable a) => Int -> [a] %1 -> [a]
198198
drop _ [] = []
199199
drop i (x : xs)
200-
| i Prelude.< 0 = x : xs
200+
| i Prelude.<= 0 = x : xs
201201
| otherwise = x `lseq` drop (i - 1) xs
202202

203203
-- | The intersperse function takes an element and a list and

test/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Main where
55

66
import Test.Data.Destination (destArrayTests)
77
import Test.Data.Functor.Linear (genericTests)
8+
import Test.Data.List (listTests)
89
import Test.Data.Mutable.Array (mutArrTests)
910
import Test.Data.Mutable.HashMap (mutHMTests)
1011
import Test.Data.Mutable.Set (mutSetTests)
@@ -29,6 +30,7 @@ allTests =
2930
mutSetTests,
3031
destArrayTests,
3132
polarizedArrayTests,
33+
listTests,
3234
genericTests
3335
],
3436
testGroup

test/Test/Data/List.hs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
4+
module Test.Data.List (listTests) where
5+
6+
import qualified Data.List.Linear as List
7+
import Hedgehog
8+
import qualified Hedgehog.Gen as Gen
9+
import qualified Hedgehog.Range as Range
10+
import Prelude.Linear
11+
import Test.Tasty
12+
import Test.Tasty.Hedgehog (testPropertyNamed)
13+
import qualified Prelude
14+
15+
listTests :: TestTree
16+
listTests =
17+
testGroup
18+
"List tests"
19+
[ testPropertyNamed "take n ++ drop n = id" "take_drop" take_drop,
20+
testPropertyNamed "length . take n = const n" "take_length" take_length
21+
]
22+
23+
take_drop :: Property
24+
take_drop = property $ do
25+
n <- forAll $ Gen.int (Range.linear 0 50)
26+
classify "0" $ n == 0
27+
xs <- forAll $ Gen.list (Range.linear 0 1000) (Gen.int (Range.linear 0 40))
28+
classify "length > n" $ Prelude.length xs > n
29+
List.take n xs ++ List.drop n xs === xs
30+
31+
take_length :: Property
32+
take_length = property $ do
33+
n <- forAll $ Gen.int (Range.linear 0 50)
34+
classify "0" $ n == 0
35+
xs <- forAll $ Gen.list (Range.linear 0 1000) (Gen.int (Range.linear 0 40))
36+
classify "length > n" $ Prelude.length xs > n
37+
case Prelude.length xs > n of
38+
True -> do
39+
annotate "Prelude.length xs > n"
40+
Prelude.length (List.take n xs) === n
41+
False -> do
42+
annotate "Prelude.length xs < n"
43+
Prelude.length (List.take n xs) === Prelude.length xs

0 commit comments

Comments
 (0)