HORIZON HASKELLDocslts/ghc-10.0.x8174fa42026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8174fa4 · 2026-10-11

ModuleAgda-2.8.0.2Haskell2010

Agda.Utils.List1

Non-empty lists.

Better name List1 for non-empty lists, plus missing functionality.

Import: @

{-# LANGUAGE PatternSynonyms #-}

import Agda.Utils.List1 (List1, pattern (:|)) import qualified Agda.Utils.List1 as List1

@

  • 3 types
  • 1 class
  • 103 values
  • PackageAgda-2.8.0.2
  • Exports108
  • LanguageHaskell2010
  • LicenceMIT
  • SourceList1.hs
valueallEqual :: Eq a => List1 a -> Bool
#

Checks if all the elements in the list are equal. Assumes that the Eq instance stands for an equivalence relation. O(n).

valuebreakAfter :: (a -> Bool) -> List1 a -> (List1 a, [a])
#

Breaks a list just after an element satisfying the predicate is found.

Example1 expression
breakAfter even [1,3,5,2,4,7,8](1 :| [3,5,2],[4,7,8])
valueconcat :: [List1 a] -> [a]
#

Concatenate one or more non-empty lists.

valuefoldr :: (a -> b -> b) -> (a -> b) -> List1 a -> b
#

List Data.List.foldr but with a base case for the singleton list.

valuefromListSafe
  1. :: List1 a

    Default value if convertee is empty.

  2. -> [a]

    List to convert, supposedly non-empty.

  3. -> List1 a

    Converted list.

#

Safe version of fromList.

valuegroupBy' :: (a -> a -> Bool) -> [a] -> [List1 a]
#

More precise type for Agda.Utils.List.groupBy'.

A variant of groupBy which applies the predicate to consecutive pairs. O(n).

valuegroupByFst :: Eq a => [(a, b)] -> [(a, List1 b)]
#

Group consecutive items that share the same first component.

valueinitLast :: List1 a -> ([a], a)
#

Return the last element and the rest.

valueliftList1 :: (List1 a -> List1 b) -> [a] -> [b]
#

Lift a function on non-empty lists to a function on lists.

This is in essence fmap for Maybe, if we take [a] = Maybe (List1 a).

valuesnoc :: [a] -> a -> List1 a
#

Build a list with one element.

More precise type for snoc.

valueunion :: Eq a => List1 a -> List1 a -> List1 a
#

Like Data.List.union. Duplicates in the first list are not removed. O(nm).

valueunlessNull :: Applicative m => [a] -> (List1 a -> m ()) -> m ()
#

The more general type Null m => [a] -> (List1 a -> m) -> m often causes type inference to fail, as we do not in general have instance Applicative m => Null (m ()).

valueupdateHead :: (a -> a) -> List1 a -> List1 a
#

Update the first element of a non-empty list. O(1).

valueupdateLast :: (a -> a) -> List1 a -> List1 a
#

Update the last element of a non-empty list. O(n).

valuewordsBy :: (a -> Bool) -> [a] -> [List1 a]
#

Split a list into sublists. Generalisation of the prelude function words. Same as Data.List.Split.wordsBy and Data.List.Extra.wordsBy, but with the non-emptyness guarantee on the chunks. O(n).

words xs == wordsBy isSpace xs
value(!!) :: HasCallStack => NonEmpty a -> Int -> a
#

xs !! n returns the element of the stream xs at index n. Note that the head of the stream has index 0.

Beware: a negative or out-of-bounds index will cause an error.

valueappendList :: NonEmpty a -> [a] -> NonEmpty a
#

Attach a list at the end of a NonEmpty.

Example1 expression
appendList (1 :| [2,3]) []1 :| [2,3]
Example1 expression
appendList (1 :| [2,3]) [4,5]1 :| [2,3,4,5]
valuecompareLength :: NonEmpty a -> Int -> Ordering
#

Use compareLength xs n as a safer and faster alternative to compare (length xs) n. Similarly, it's better to write compareLength xs 10 == LT instead of length xs < 10.

While length would force and traverse the entire spine of xs (which could even diverge if xs is infinite), compareLength traverses at most n elements to determine its result.

Example5 expressions
compareLength ('a' :| []) 1EQcompareLength ('a' :| ['b']) 3LTcompareLength (0 :| [1..]) 100GTcompareLength undefined 0GTcompareLength ('a' :| 'b' : undefined) 1GT
valuedrop :: Int -> NonEmpty a -> [a]
#

drop n xs drops the first n elements off the front of the sequence xs.

valuegroup :: (Foldable f, Eq a) => f a -> [NonEmpty a]
#

The group function takes a stream and returns a list of streams such that flattening the resulting list is equal to the argument. Moreover, each stream in the resulting list contains only equal elements, and consecutive equal elements of the input end up in the same stream of the output list. For example, in list notation:

Example1 expression
group "Mississippi"['M' :| "",'i' :| "",'s' :| "s",'i' :| "",'s' :| "s",'i' :| "",'p' :| "p",'i' :| ""]
valuehead :: NonEmpty a -> a
#

Extract the first element of the stream.

valueinit :: NonEmpty a -> [a]
#

Extract everything except the last element of the stream.

valueinits :: Foldable f => f a -> NonEmpty [a]
#

The inits function takes a stream xs and returns all the finite prefixes of xs, starting with the shortest. The result is NonEmpty because the result always contains the empty list as the first element.

inits [1,2,3] == [] :| [[1], [1,2], [1,2,3]]
inits [1] == [] :| [[1]]
inits [] == [] :| []
valueinits1 :: NonEmpty a -> NonEmpty (NonEmpty a)
#

The inits1 function takes a NonEmpty stream xs and returns all the NonEmpty finite prefixes of xs, starting with the shortest.

inits1 (1 :| [2,3]) == (1 :| []) :| [1 :| [2], 1 :| [2,3]]
inits1 (1 :| []) == (1 :| []) :| []
valueinsert :: (Foldable f, Ord a) => a -> f a -> NonEmpty a
#

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 list is sorted beforehand, the result will also be sorted.

valueintersperse :: a -> NonEmpty a -> NonEmpty a
#

'intersperse x xs' alternates elements of the list with copies of x.

intersperse 0 (1 :| [2,3]) == 1 :| [0,2,0,3]
valueiterate :: (a -> a) -> a -> NonEmpty a
#

iterate f x produces the infinite sequence of repeated applications of f to x.

iterate f x = x :| [f x, f (f x), ..]
valuelast :: NonEmpty a -> a
#

Extract the last element of the stream.

valuenub :: Eq a => NonEmpty a -> NonEmpty a
#

The nub function removes duplicate elements from a list. In particular, it keeps only the first occurrence of each element. (The name nub means 'essence'.) It is a special case of nubBy, which allows the programmer to supply their own inequality test.

This function knows too little about the elements to be efficient. Its asymptotic complexity is O(n ⋅ d), where d is the number of distinct elements in the list.

If there exists instance Ord a, it's faster to use nubOrd.

valuenubBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
#

The nubBy function behaves just like nub, except it uses a user-supplied equality predicate instead of the overloaded == function.

valuenubOrd :: Ord a => NonEmpty a -> NonEmpty a
#

Same as nub, but asymptotically faster, taking only O(n log d) time. where d is the number of distinct elements in the list.

valuepartition :: (a -> Bool) -> NonEmpty a -> ([a], [a])
#

The partition function takes a predicate p and a stream xs, and returns a pair of lists. The first list corresponds to the elements of xs for which p holds; the second corresponds to the elements of xs for which p does not hold.

'partition' p xs = ('filter' p xs, 'filter' (not . p) xs)
valueprependList :: [a] -> NonEmpty a -> NonEmpty a
#

Attach a list at the beginning of a NonEmpty.

Example1 expression
prependList [] (1 :| [2,3])1 :| [2,3]
Example1 expression
prependList [negate 1, 0] (1 :| [2, 3])-1 :| [0,1,2,3]
valuerepeat :: a -> NonEmpty a
#

repeat x returns a constant stream, where all elements are equal to x.

valuescanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b
#

scanl is similar to foldl, but returns a stream of successive reduced values from the left:

scanl f z [x1, x2, ...] == z :| [z `f` x1, (z `f` x1) `f` x2, ...]

Note that

last (scanl f z xs) == foldl f z xs.
valuescanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
#

scanl1 is a variant of scanl that has no starting value argument:

scanl1 f [x1, x2, ...] == x1 :| [x1 `f` x2, x1 `f` (x2 `f` x3), ...]
valuescanr :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b
#

scanr is the right-to-left dual of scanl. Note that

head (scanr f z xs) == foldr f z xs.
valuesortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
#

Sort a NonEmpty on a user-supplied projection of its elements. See sortOn for more detailed information.

Examples
Example1 expression
sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")](1,"Hello") :| [(2,"world"),(4,"!")]
Example1 expression
sortOn List.length ("jim" :| ["creed", "pam", "michael", "dwight", "kevin"])"jim" :| ["pam","creed","kevin","dwight","michael"]
Performance notes

This function minimises the projections performed, by materialising the projections in an intermediate list.

For trivial projections, you should prefer using sortBy with comparing, for example:

Example1 expression
sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]

Or, for the exact same API as sortOn, you can use `sortBy . comparing`:

Example1 expression
(sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]

However, sortOn may still be faster for instances with a more efficient implementation of (>) than compare.

sortWith is an alias for `sortBy . comparing`.

valuespan :: (a -> Bool) -> NonEmpty a -> ([a], [a])
#

span p xs returns the longest prefix of xs that satisfies p, together with the remainder of the stream.

'span' p xs == ('takeWhile' p xs, 'dropWhile' p xs)
xs == ys ++ zs where (ys, zs) = 'span' p xs
valuesplitAt :: Int -> NonEmpty a -> ([a], [a])
#

splitAt n xs returns a pair consisting of the prefix of xs of length n and the remaining stream immediately following this prefix.

'splitAt' n xs == ('take' n xs, 'drop' n xs)
xs == ys ++ zs where (ys, zs) = 'splitAt' n xs
valuetail :: NonEmpty a -> [a]
#

Extract the possibly-empty tail of the stream.

valuetails :: Foldable f => f a -> NonEmpty [a]
#

The tails function takes a stream xs and returns all the suffixes of xs, starting with the longest. The result is NonEmpty because the result always contains the empty list as the last element.

tails [1,2,3] == [1,2,3] :| [[2,3], [3], []]
tails [1] == [1] :| [[]]
tails [] == [] :| []
valuetails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
#

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 :| []) :| []
valueunfold :: (a -> (b, Maybe a)) -> a -> NonEmpty b
#

Deprecated. Use unfoldr

unfold produces a new stream by repeatedly applying the unfolding function to the seed value to produce an element of type b and a new seed value. When the unfolding function returns Nothing instead of a new seed value, the stream ends.

valuezipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
#

The zipWith function generalizes zip. Rather than tupling the elements, the elements are combined using the function passed as the first argument.

datadata NonEmpty a
#

Non-empty (and non-strict) list type.

Constructors

  • a :| [a]infixr 5
Instances111Monad, Functor, MonadFix, IsString, Applicative, Foldable, …
classclass IsList l where
#

The IsList class and its methods are intended to be used in conjunction with the OverloadedLists extension.

Associated types

  • type family Item l

    The Item type function returns the type of items of the structure l.

Methods

  • fromList :: [Item l] -> l

    The fromList function constructs the structure l from the given list of Item l

  • fromListN :: Int -> [Item l] -> l

    The fromListN function takes the input list's length and potentially uses it to construct the structure l more efficiently compared to fromList. If the given number does not equal to the input list's length the behaviour of fromListN is not specified.

    Property
    fromListN (length xs) xs == fromList xs
  • toList :: l -> [Item l]

    The toList function extracts a list of Item l from the structure l. It should satisfy fromList . toList = id.

Instances47IsList, …
  • IsList CharStringDefined in ListLike-4.7.8.4 · Data.ListLike.CharString
  • IsList CharStringLazyDefined in ListLike-4.7.8.4 · Data.ListLike.CharString
  • IsList CharsDefined in ListLike-4.7.8.4 · Data.ListLike.Chars
  • IsList ByteArrayDefined in base-4.23.0.0 · Data.Array.Byte
  • IsList BuilderDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.Internal

    For long or infinite lists use fromList because it uses LazyByteString otherwise use fromListN which uses StrictByteString.

  • IsList ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • IsList ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Lazy.Internal
  • IsList ShortByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Short.Internal
  • IsList IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • IsList VersionDefined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • IsList CallStackDefined in ghc-internal-10.0.0 · GHC.Internal.IsList

    Be aware that 'fromList . toList = id' only for unfrozen CallStacks, since toList removes frozenness information.

  • IsList NEIntSetDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • IsList TextDefined in text-2.1.4 · Data.Text · orphan

    Performs replacement on invalid scalar values:

    Example2 expressions
    :set -XOverloadedLists['\55555'] :: Text"\65533"
  • IsList BuilderDefined in ListLike-4.7.8.4 · Data.ListLike.Text.Builder · orphan
  • IsList TextDefined in text-2.1.4 · Data.Text.Lazy · orphan

    Performs replacement on invalid scalar values:

    Example2 expressions
    :set -XOverloadedLists['\55555'] :: Data.Text.Lazy.Text"\65533"
  • IsList ShortTextDefined in text-short-0.1.6.1 · Data.Text.Short.Internal

    Note: Surrogate pairs ([U+D800 .. U+DFFF]) character literals are replaced by U+FFFD.

  • Ord a => IsList (Set a)Defined in containers-0.8 · Data.Set.Internal
  • Ord a => IsList (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Enum a => IsList (EnumSet a)Defined in enummapset-0.7.3.0 · Data.EnumSet
  • Storable a => IsList (Vector a)Defined in vector-0.13.2.0 · Data.Vector.Storable
  • IsList (List2 a)Defined in Agda-2.8.0.2 · Agda.Utils.List2

    fromList is unsafe.

  • IsList (KeyMap v)Defined in aeson-2.3.2.0 · Data.Aeson.KeyMap
  • IsList (IntMap a)Defined in containers-0.8 · Data.IntMap.Internal
  • IsList (Seq a)Defined in containers-0.8 · Data.Sequence.Internal
  • IsList (DNonEmpty a)Defined in dlist-1.0 · Data.DList.DNonEmpty.Internal
  • IsList (DList a)Defined in dlist-1.0 · Data.DList.Internal
  • IsList (FMList a)Defined in ListLike-4.7.8.4 · Data.ListLike.FMList · orphan
  • IsList (NonEmpty a)Defined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • IsList (ZipList a)Defined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • IsList (NEIntMap a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • IsList (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • IsList (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • IsList (SmallArray a)Defined in primitive-0.9.1.0 · Data.Primitive.SmallArray
  • IsList (UTF8 ByteString)Defined in ListLike-4.7.8.4 · Data.ListLike.UTF8 · orphan
  • IsList (UTF8 ByteString)Defined in ListLike-4.7.8.4 · Data.ListLike.UTF8 · orphan
  • IsList (Vector a)Defined in vector-0.13.2.0 · Data.Vector
  • IsList (Vector a)Defined in vector-0.13.2.0 · Data.Vector.Strict
  • IsList [a]Defined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • Hashable a => IsList (HashSet a)Defined in unordered-containers-0.2.21 · Data.HashSet.Internal
  • Prim a => IsList (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • Prim a => IsList (Vector a)Defined in vector-0.13.2.0 · Data.Vector.Primitive
  • Unbox e => IsList (Vector e)Defined in vector-0.13.2.0 · Data.Vector.Unboxed · orphan
  • Ord k => IsList (Map k v)Defined in containers-0.8 · Data.Map.Internal
  • Ord k => IsList (NEMap k a)Defined in nonempty-containers-0.3.6.0 · Data.Map.NonEmpty.Internal
  • Enum k => IsList (EnumMap k a)Defined in enummapset-0.7.3.0 · Data.EnumMap.Base
  • Hashable k => IsList (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Integral i, Ix i) => IsList (Array i e)Defined in ListLike-4.7.8.4 · Data.ListLike.Instances · orphan
familytype family Item l
#

The Item type function returns the type of items of the structure l.

Instances47Item, …
  • type Item CharString = CharDefined in ListLike-4.7.8.4 · Data.ListLike.CharString
  • type Item CharStringLazy = CharDefined in ListLike-4.7.8.4 · Data.ListLike.CharString
  • type Item Chars = CharDefined in ListLike-4.7.8.4 · Data.ListLike.Chars
  • type Item ByteArray = Word8Defined in base-4.23.0.0 · Data.Array.Byte
  • type Item Builder = Word8Defined in bytestring-0.12.2.0 · Data.ByteString.Builder.Internal
  • type Item ByteString = Word8Defined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • type Item ByteString = Word8Defined in bytestring-0.12.2.0 · Data.ByteString.Lazy.Internal
  • type Item ShortByteString = Word8Defined in bytestring-0.12.2.0 · Data.ByteString.Short.Internal
  • type Item IntSet = KeyDefined in containers-0.8 · Data.IntSet.Internal
  • type Item Version = IntDefined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • type Item CallStack = (String, SrcLoc)Defined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • type Item NEIntSet = KeyDefined in nonempty-containers-0.3.6.0 · Data.IntSet.NonEmpty.Internal
  • type Item Text = CharDefined in text-2.1.4 · Data.Text · orphan
  • type Item Builder = CharDefined in ListLike-4.7.8.4 · Data.ListLike.Text.Builder · orphan
  • type Item Text = CharDefined in text-2.1.4 · Data.Text.Lazy · orphan
  • type Item ShortText = CharDefined in text-short-0.1.6.1 · Data.Text.Short.Internal
  • type Item (List2 a) = aDefined in Agda-2.8.0.2 · Agda.Utils.List2
  • type Item (KeyMap v) = (Key, v)Defined in aeson-2.3.2.0 · Data.Aeson.KeyMap
  • type Item (IntMap a) = (Key, a)Defined in containers-0.8 · Data.IntMap.Internal
  • type Item (Map k v) = (k, v)Defined in containers-0.8 · Data.Map.Internal
  • type Item (Seq a) = aDefined in containers-0.8 · Data.Sequence.Internal
  • type Item (Set a) = aDefined in containers-0.8 · Data.Set.Internal
  • type Item (DNonEmpty a) = aDefined in dlist-1.0 · Data.DList.DNonEmpty.Internal
  • type Item (DList a) = aDefined in dlist-1.0 · Data.DList.Internal
  • type Item (EnumMap k a) = (k, a)Defined in enummapset-0.7.3.0 · Data.EnumMap.Base
  • type Item (EnumSet a) = aDefined in enummapset-0.7.3.0 · Data.EnumSet
  • type Item (FMList a) = aDefined in ListLike-4.7.8.4 · Data.ListLike.FMList · orphan
  • type Item (Array i e) = eDefined in ListLike-4.7.8.4 · Data.ListLike.Instances · orphan
  • type Item (NonEmpty a) = aDefined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • type Item (ZipList a) = aDefined in ghc-internal-10.0.0 · GHC.Internal.IsList
  • type Item (NEIntMap a) = (Key, a)Defined in nonempty-containers-0.3.6.0 · Data.IntMap.NonEmpty.Internal
  • type Item (NEMap k a) = (k, a)Defined in nonempty-containers-0.3.6.0 · Data.Map.NonEmpty.Internal
  • type Item (NESeq a) = aDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • type Item (NESet a) = aDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • type Item (Array a) = aDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • type Item (PrimArray a) = aDefined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • type Item (SmallArray a) = aDefined in primitive-0.9.1.0 · Data.Primitive.SmallArray
  • type Item (HashMap k v) = (k, v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • type Item (HashSet a) = aDefined in unordered-containers-0.2.21 · Data.HashSet.Internal
  • type Item (UTF8 ByteString) = CharDefined in ListLike-4.7.8.4 · Data.ListLike.UTF8 · orphan
  • type Item (UTF8 ByteString) = CharDefined in ListLike-4.7.8.4 · Data.ListLike.UTF8 · orphan
  • type Item (Vector a) = aDefined in vector-0.13.2.0 · Data.Vector
  • type Item (Vector a) = aDefined in vector-0.13.2.0 · Data.Vector.Primitive
  • type Item (Vector a) = aDefined in vector-0.13.2.0 · Data.Vector.Storable
  • type Item (Vector a) = aDefined in vector-0.13.2.0 · Data.Vector.Strict
  • type Item (Vector e) = eDefined in vector-0.13.2.0 · Data.Vector.Unboxed · orphan
  • type Item [a] = aDefined in ghc-internal-10.0.0 · GHC.Internal.IsList
valueunzip :: Functor f => f (a, b) -> (f a, f b)
#

Generalization of Data.List.unzip.

Examples
Example1 expression
unzip (Just ("Hello", "World"))(Just "Hello",Just "World")
Example1 expression
unzip [("I", "love"), ("really", "haskell")](["I","really"],["love","haskell"])