From 8e217256520adc148ccb78e9c518c80cff92525f Mon Sep 17 00:00:00 2001
From: Mike Pilgrem <mpilgrem@users.noreply.github.com>
Date: Wed, 9 Oct 2024 23:31:11 +0100
Subject: [PATCH] Re CLC #293 - Don't specify Data.List.NonEmpty in terms of
 partial

See https://github.com/haskell/core-libraries-committee/issues/293

`List.init` had already been driven out of `tails1` by 21fc180bec93d964a7f4ffdf2429ef6f74b49ab6 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`.
---
 libraries/base/changelog.md              | 1 +
 libraries/base/src/Data/List/NonEmpty.hs | 8 +++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 6ecf3049827..a6ae2389aac 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -4,6 +4,7 @@
   * Restrict `Data.List.NonEmpty.unzip` to `NonEmpty (a, b) -> (NonEmpty a, NonEmpty b)`. ([CLC proposal #86](https://github.com/haskell/core-libraries-committee/issues/86))
   * Modify the implementation of `Control.Exception.throw` to avoid call-sites being inferred as diverging via precise exception.
     ([GHC #25066](https://gitlab.haskell.org/ghc/ghc/-/issues/25066), [CLC proposal #290](https://github.com/haskell/core-libraries-committee/issues/290))
+  * `Data.List.NonEmpty.{init,last,tails1}` are now defined using only total functions (rather than partial ones). ([CLC proposal #293](https://github.com/haskell/core-libraries-committee/issues/293))
 
 ## 4.21.0.0 *TBA*
   * `GHC.Desugar` has been deprecated and should be removed in GHC 9.14. ([CLC proposal #216](https://github.com/haskell/core-libraries-committee/issues/216))
diff --git a/libraries/base/src/Data/List/NonEmpty.hs b/libraries/base/src/Data/List/NonEmpty.hs
index 6e4bf638739..50a1ecb5343 100644
--- a/libraries/base/src/Data/List/NonEmpty.hs
+++ b/libraries/base/src/Data/List/NonEmpty.hs
@@ -206,11 +206,13 @@ tail (_ :| as) = as
 
 -- | Extract the last element of the stream.
 last :: NonEmpty a -> a
-last ~(a :| as) = List.last (a : as)
+last (a :| []) = a
+last (_ :| (a : as)) = last (a :| as)
 
 -- | Extract everything except the last element of the stream.
 init :: NonEmpty a -> [a]
-init ~(a :| as) = List.init (a : as)
+init (_ :| []) = []
+init (a1 :| (a2 : as)) = a1 : init (a2 :| as)
 
 -- | Construct a 'NonEmpty' list from a single element.
 --
@@ -324,7 +326,7 @@ tails = fromList . List.tails . Foldable.toList
 --
 -- @since 4.18
 tails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
-tails1 = fromList . List.tails1 . Foldable.toList
+tails1 xs = xs :| List.tails1 (tail xs)
 
 -- | @'insert' x xs@ inserts @x@ into the last position in @xs@ where it
 -- is still less than or equal to the next element. In particular, if the
-- 
GitLab