smash-0.1.1.0: Combinators for Maybe types

Copyright (c) 2020 Emily Pillmore BSD-3-Clause Emily Pillmore Experimental CPP, RankNTypes, TypeApplications Safe Haskell2010

Data.Wedge

Description

This module contains the definition for the Wedge datatype. In practice, this type is isomorphic to 'Maybe (Either a b)' - the type with two possibly non-exclusive values and an empty case.

Synopsis

# Datatypes

Categorically, the Wedge datatype represents the coproduct (like, Either) in the category Hask* of pointed Hask types, called a wedge sum. The category Hask* consists of Hask types affixed with a dedicated base point along with an object. In Hask, this is equivalent to 1 + a, also known as 'Maybe a'. Because we can conflate basepoints of different types (there is only one Nothing type), the wedge sum is can be viewed as the type 1 + a + b, or Maybe (Either a b) in Hask. Pictorially, one can visualize this as:

Wedge:
a
|
Nowhere +-------+
|
b


The fact that we can think about Wedge as a coproduct gives us some reasoning power about how a Wedge will interact with the product in Hask*, called Can. Namely, we know that a product of a type and a coproduct, a * (b + c), is equivalent to (a + b) * (a + c). Additioally, we may derive other facts about its associativity, distributivity, commutativity, and any more. As an exercise, think of soemthing Either can do. Now do it with Wedge!

data Wedge a b Source #

The Wedge data type represents values with two exclusive possibilities, and an empty case. This is a coproduct of pointed types - i.e. of Maybe values. The result is a type, 'Wedge a b', which is isomorphic to 'Maybe (Either a b)'.

Constructors

 Nowhere Here a There b
Instances

# Combinators

quotWedge :: Either (Maybe a) (Maybe b) -> Wedge a b Source #

Given two possible pointed types, produce a Wedge by considering the left case, the right case, and mapping their Nothing cases to Nowhere. This is a pushout of pointed types A * - B.

wedgeLeft :: Maybe a -> Wedge a b Source #

Inject a Maybe value into the Here case of a Wedge, or Nowhere if the empty case is given. This is analogous to the Left constructor for Either.

wedgeRight :: Maybe b -> Wedge a b Source #

Inject a Maybe value into the There case of a Wedge, or Nowhere if the empty case is given. This is analogous to the Right constructor for Either.

fromWedge :: Wedge a b -> Maybe (Either a b) Source #

Convert a 'Wedge a b' into a 'Maybe (Either a b)' value.

toWedge :: Maybe (Either a b) -> Wedge a b Source #

Convert a 'Maybe (Either a b)' value into a Wedge

isHere :: Wedge a b -> Bool Source #

Detect if a Wedge is a Here case.

isThere :: Wedge a b -> Bool Source #

Detect if a Wedge is a There case.

isNowhere :: Wedge a b -> Bool Source #

Detect if a Wedge is a Nowhere empty case.

## Eliminators

wedge :: c -> (a -> c) -> (b -> c) -> Wedge a b -> c Source #

Case elimination for the Wedge datatype.

## Filtering

heres :: Foldable f => f (Wedge a b) -> [a] Source #

Given a Foldable of Wedges, collect the Here cases, if any.

theres :: Foldable f => f (Wedge a b) -> [b] Source #

Given a Foldable of Wedges, collect the There cases, if any.

filterHeres :: Foldable f => f (Wedge a b) -> [Wedge a b] Source #

Filter the Here cases of a Foldable of Wedges.

filterTheres :: Foldable f => f (Wedge a b) -> [Wedge a b] Source #

Filter the There cases of a Foldable of Wedges.

filterNowheres :: Foldable f => f (Wedge a b) -> [Wedge a b] Source #

Filter the Nowhere cases of a Foldable of Wedges.

## Folding

foldHeres :: Foldable f => (a -> m -> m) -> m -> f (Wedge a b) -> m Source #

Fold over the Here cases of a Foldable of Wedges by some accumulating function.

foldTheres :: Foldable f => (b -> m -> m) -> m -> f (Wedge a b) -> m Source #

Fold over the There cases of a Foldable of Wedges by some accumulating function.

gatherWedges :: Wedge [a] [b] -> [Wedge a b] Source #

Given a Wedge of lists, produce a list of wedges by mapping the list of as to Here values, or the list of bs to There values.

## Partitioning

partitionWedges :: forall f t a b. (Foldable t, Alternative f) => t (Wedge a b) -> (f a, f b) Source #

Given a Foldable of Wedges, partition it into a tuple of alternatives their parts.

mapWedges :: forall f t a b c. (Alternative f, Traversable t) => (a -> Wedge b c) -> t a -> (f b, f c) Source #

Partition a structure by mapping its contents into Wedges, and folding over '(|)'.

## Distributivity

distributeWedge :: Wedge (a, b) c -> (Wedge a c, Wedge b c) Source #

Distribute a Wedge over a product.

codistributeWedge :: Either (Wedge a c) (Wedge b c) -> Wedge (Either a b) c Source #

Codistribute Wedges over a coproduct

## Associativity

reassocLR :: Wedge (Wedge a b) c -> Wedge a (Wedge b c) Source #

Re-associate a Wedge of Wedges from left to right.

reassocRL :: Wedge a (Wedge b c) -> Wedge (Wedge a b) c Source #

Re-associate a Wedge of Wedges from left to right.

## Symmetry

swapWedge :: Wedge a b -> Wedge b a Source #

Swap the positions of the a's and the b's in a Wedge.