
I'd expect that anyone who uses mfix with NonEmpty as result would use explicit (and irrefutable) pattern matching. But yes, changing these might make some code break. I'm not confident at all it won't make some code less efficient too, by forcing the structure of NonEmpty too early. So I would like that this thread is only about changing `head` and `tail` and not let scope creep. OR we hold this and let Keith come up with more complete NonEmpty implementation change. - Oleg On 8.1.2021 21.50, Viktor Dukhovni wrote:
On Fri, Jan 08, 2021 at 09:44:31PM +0200, Oleg Grenrus wrote:
Note also
-- | @since 4.9.0.0 instance Foldable NonEmpty where foldr f z ~(a :| as) = f a (List.foldr f z as) foldl f z (a :| as) = List.foldl f (f z a) as foldl1 f (a :| as) = List.foldl f a as
-- GHC isn't clever enough to transform the default definition -- into anything like this, so we'd end up shuffling a bunch of -- Maybes around. foldr1 f (p :| ps) = foldr go id ps p where go x r prev = f prev (r x)
-- We used to say -- -- length (_ :| as) = 1 + length as -- -- but the default definition is better, counting from 1. -- -- The default definition also works great for null and foldl'. -- As usual for cons lists, foldr' is basically hopeless.
foldMap f ~(a :| as) = f a `mappend` foldMap f as fold ~(m :| ms) = m `mappend` fold ms toList ~(a :| as) = a : as
Plenty of irrefutable patterns. Do any of these make "mfix" more usable for NonEmpty? Or are they just superfluous? With just one constructor, is there any downside to an irrefutable pattern?