-
Notifications
You must be signed in to change notification settings - Fork 16
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
Don't specify Data.List.NonEmpty.{init,last} in terms of partial
... .List.{init,last}`
#293
Comments
There is one other use of -- | The 'tails1' function takes a 'NonEmpty' stream @xs@ and returns all the
-- non-empty suffixes of @xs@, starting with the longest.
--
-- > tails1 (1 :| [2,3]) == (1 :| [2,3]) :| [2 :| [3], 3 :| []]
-- > tails1 (1 :| []) == (1 :| []) :| []
--
-- @since 4.18
tails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
tails1 =
-- fromList is an unsafe function, but this usage should be safe, since:
-- * `tails xs = [xs, tail xs, tail (tail xs), ..., []]`
-- * If `xs` is nonempty, it follows that `tails xs` contains at least one nonempty
-- list, since `head (tails xs) = xs`.
-- * The only empty element of `tails xs` is the last one (by the definition of `tails`)
-- * Therefore, if we take all but the last element of `tails xs` i.e.
-- `init (tails xs)`, we have a nonempty list of nonempty lists
fromList . Prelude.map fromList . List.init . List.tails . Foldable.toList To eliminate that also, I think you could specify: tails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
tails1 (a :| []) = (a :| []) :| []
tails1 as@(_ :| (a2 : as')) = as <| tails1 (a2 :| as') |
I'm open to removing uses of partial functions from |
@mixphix, am I doing something that is not in accordance with |
It would be simpler to use In the same vein, one can use |
EDIT: The following is a bad point - I had not understood correctly what was meant (see below). If you use |
I meant something like Data.List.NonEmpty.tails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
Data.List.NonEmpty.tails1 xs = xs :| Data.List.tails1 (Data.List.NonEmpty.tail xs) |
@mpilgrem I think it would be helpful for futher evaluation if you prepare a GHC MR. |
@Bodigrim, I've tried to so so with https://gitlab.haskell.org/ghc/ghc/-/merge_requests/13402. I targetted the |
Let's compare Core using
|
Dear CLC members, let's vote on the proposal to avoid partial functions in the definitions of @tomjaguarpaw @mixphix @velveteer @parsonsmatt @angerman @hasufell +1 from me, see my analysis above. |
+1, this is great! |
+1 |
1 similar comment
+1 |
Thanks all, that's enough votes to approve. |
See haskell/core-libraries-committee#293 `List.init` had already been driven out of `tails1` by 21fc180 but this specification also avoided partial `fromList`, so I preferred it. The `changelog.md` for `base` is updated, with an entry added under `base-4.22.0.0`.
See haskell/core-libraries-committee#293 `List.init` had already been driven out of `tails1` by 21fc180 but this specification also avoided partial `fromList`, so I preferred it. The `changelog.md` for `base` is updated, with an entry added under `base-4.22.0.0`.
See haskell/core-libraries-committee#293 `List.init` had already been driven out of `tails1` by 21fc180 but this specification also avoided partial `fromList`, so I preferred it. The `changelog.md` for `base` is updated, with an entry added under `base-4.22.0.0`.
Motivation:
Data.List.NonEmpty
is consistent with the warning)init
andlast
] have irrefutable patterns, but they're actually strict.")At present,
Data.List.NonEmpty
has:I propose:
Although this could be written more succinctly with the functions promised by the
Data.Foldable1.Foldable1
class (see here), moduleData.Foldable1
itself importsData.List.NonEmpty
.The text was updated successfully, but these errors were encountered: