first-class-families-0.8.1.0: First-class type families
Safe HaskellSafe-Inferred
LanguageHaskell2010

Fcf.Class.Foldable

Description

Foldable types.

A minimal implementation of this interface is given by either FoldMap or Foldr, but a default still needs to be given explicitly for the other.

data MyType a = ... {- Some custom Foldable type -}

-- Method 1: Implement Foldr, default FoldMap.
type instance Eval (Foldr f y xs) = ... {- Explicit implementation -}
type instance Eval (FoldMap f xs) = FoldMapDefault_ f xs {- Default -}

-- Method 2: Implement FoldMap, default Foldr.
type instance Eval (FoldMap f xs) = ... {- Explicit implementation -}
type instance Eval (Foldr f y xs) = FoldrDefault_ f y xs {- Default -}
Synopsis

Core interface

data Foldr :: (a -> b -> Exp b) -> b -> t a -> Exp b Source #

Right fold.

Example

Expand
>>> :kind! Eval (Foldr (+) 0 [1, 2, 3, 4])
Eval (Foldr (+) 0 [1, 2, 3, 4]) :: Natural
= 10

Instances

Instances details
type Eval (Foldr f y ('Left _a :: Either a3 a1) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Foldr f y ('Left _a :: Either a3 a1) :: a2 -> Type) = y
type Eval (Foldr f y ('Right x :: Either a3 a1) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Foldr f y ('Right x :: Either a3 a1) :: a2 -> Type) = Eval (f x y)
type Eval (Foldr f y ('Just x) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Foldr f y ('Just x) :: a2 -> Type) = Eval (f x y)
type Eval (Foldr f y ('Nothing :: Maybe a1) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Foldr f y ('Nothing :: Maybe a1) :: a2 -> Type) = y
type Eval (Foldr f y (x ': xs) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Foldr f y (x ': xs) :: a2 -> Type) = Eval (f x (Eval (Foldr f y xs)))
type Eval (Foldr f y ('[] :: [a1]) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Foldr f y ('[] :: [a1]) :: a2 -> Type) = y

data FoldMap :: (a -> Exp m) -> t a -> Exp m Source #

Type-level foldMap.

Instances

Instances details
type Eval (FoldMap f ('Left _a :: Either a3 a1) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (FoldMap f ('Left _a :: Either a3 a1) :: a2 -> Type) = MEmpty :: a2
type Eval (FoldMap f ('Right x :: Either a3 a1) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (FoldMap f ('Right x :: Either a3 a1) :: a2 -> Type) = Eval (f x)
type Eval (FoldMap f ('Just x) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (FoldMap f ('Just x) :: a2 -> Type) = Eval (f x)
type Eval (FoldMap f ('Nothing :: Maybe a1) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (FoldMap f ('Nothing :: Maybe a1) :: a2 -> Type) = MEmpty :: a2
type Eval (FoldMap f (x ': xs) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (FoldMap f (x ': xs) :: a2 -> Type) = Eval (f x) <> Eval (FoldMap f xs)
type Eval (FoldMap f ('[] :: [a1]) :: a2 -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (FoldMap f ('[] :: [a1]) :: a2 -> Type) = MEmpty :: a2

Default implementations

type FoldMapDefault_ f xs = Eval (Foldr (Bicomap f Pure (.<>)) MEmpty xs) Source #

Default implementation of FoldMap.

Usage

Expand

To define an instance of FoldMap for a custom MyType for which you already have an instance of Foldr:

type instance Eval (FoldMap f (xs :: MyType a)) = FoldMapDefault_ f xs

Example

>>> :kind! FoldMapDefault_ Pure [EQ, LT, GT]
FoldMapDefault_ Pure [EQ, LT, GT] :: Ordering
= LT

type FoldrDefault_ f y xs = Eval (UnEndo (Eval (FoldMap (Pure1 'Endo <=< Pure1 f) xs)) y) Source #

Default implementation of Foldr.

Usage

Expand

To define an instance of Foldr for a custom MyType for which you already have an instance of FoldMap:

type instance Eval (Foldr f y (xs :: MyType a)) = FoldrDefault_ f y xs

Example

>>> :kind! FoldrDefault_ (.<>) EQ [EQ, LT, GT]
FoldrDefault_ (.<>) EQ [EQ, LT, GT] :: Ordering
= LT

Derived operations

Predicates

data And :: t Bool -> Exp Bool Source #

Give True if all of the booleans in the list are True.

Example

Expand
>>> :kind! Eval (And [True, True])
Eval (And [True, True]) :: Bool
= True
>>> :kind! Eval (And [True, True, False])
Eval (And [True, True, False]) :: Bool
= False

Instances

Instances details
type Eval (And lst :: Bool -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (And lst :: Bool -> Type) = Eval (Foldr (&&) 'True lst)

data Or :: t Bool -> Exp Bool Source #

Give True if any of the booleans in the list are True.

Example

Expand
>>> :kind! Eval (Or [True, True])
Eval (Or [True, True]) :: Bool
= True
>>> :kind! Eval (Or [False, False])
Eval (Or [False, False]) :: Bool
= False

Instances

Instances details
type Eval (Or lst :: Bool -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Or lst :: Bool -> Type) = Eval (Foldr (||) 'False lst)

data All :: (a -> Exp Bool) -> t a -> Exp Bool Source #

Whether all elements of the list satisfy a predicate.

Note: this identifier conflicts with All (from Data.Monoid).

Example

Expand
>>> :kind! Eval (All (Flip (<) 6) [0,1,2,3,4,5])
Eval (All (Flip (<) 6) [0,1,2,3,4,5]) :: Bool
= True
>>> :kind! Eval (All (Flip (<) 5) [0,1,2,3,4,5])
Eval (All (Flip (<) 5) [0,1,2,3,4,5]) :: Bool
= False

Instances

Instances details
type Eval (All p lst :: Bool -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (All p lst :: Bool -> Type) = Eval (Foldr (Bicomap p (Pure :: Bool -> Bool -> Type) (&&)) 'True lst)

data Any :: (a -> Exp Bool) -> t a -> Exp Bool Source #

Whether any element of the list satisfies a predicate.

Note: this identifier conflicts with Any (from Fcf.Utils), Any (from Data.Monoid), and Any (from GHC.Exts).

Example

Expand
>>> :kind! Eval (Any (Flip (<) 5) [0,1,2,3,4,5])
Eval (Any (Flip (<) 5) [0,1,2,3,4,5]) :: Bool
= True
>>> :kind! Eval (Any (Flip (<) 0) [0,1,2,3,4,5])
Eval (Any (Flip (<) 0) [0,1,2,3,4,5]) :: Bool
= False

Instances

Instances details
type Eval (Any p lst :: Bool -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Any p lst :: Bool -> Type) = Eval (Foldr (Bicomap p (Pure :: Bool -> Bool -> Type) (||)) 'False lst)

Numbers

data Sum :: t Nat -> Exp Nat Source #

Sum a Nat-list.

Example

Expand
>>> :kind! Eval (Sum '[1,2,3])
Eval (Sum '[1,2,3]) :: Natural
= 6

Instances

Instances details
type Eval (Sum ns :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Sum ns :: Nat -> Type) = Eval (Foldr (+) 0 ns)

Lists

data Concat :: t m -> Exp m Source #

Concatenate a collection of elements from a monoid.

Example

Expand

For example, fold a list of lists.

Concat :: [[a]] -> Exp [a]
>>> :kind! Eval (Concat ([[1,2], [3,4], [5,6]]))
Eval (Concat ([[1,2], [3,4], [5,6]])) :: [Natural]
= [1, 2, 3, 4, 5, 6]
>>> :kind! Eval (Concat ([[Int, Maybe Int], [Maybe String, Either Double Int]]))
Eval (Concat ([[Int, Maybe Int], [Maybe String, Either Double Int]])) :: [*]
= [Int, Maybe Int, Maybe [Char], Either Double Int]

Instances

Instances details
type Eval (Concat xs :: a -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (Concat xs :: a -> Type) = Eval (FoldMap (Pure :: a -> a -> Type) xs)

data ConcatMap :: (a -> Exp [b]) -> t a -> Exp [b] Source #

Map a function and concatenate the results.

This is FoldMap specialized to the list monoid.

Instances

Instances details
type Eval (ConcatMap f xs :: [b] -> Type) Source # 
Instance details

Defined in Fcf.Class.Foldable

type Eval (ConcatMap f xs :: [b] -> Type) = Eval (FoldMap f xs)