agreeing-0.2.2.0: Idiomatic data structure for agreement
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Agreement

Description

A simple data structure helping us ask questions of the following sort: "does all this data have the same BLANK and if so what is it?"

For example:

doTheseHaveTheSameLength :: [String] -> String
doTheseHaveTheSameLength l = case foldMap (Somebody . length) of
  Somebody n -> "They all have length " <> show n
  Nobody     -> "The lengths differ"
  Anybody    -> "You didn't give me any strings"

This can of course be done with `Maybe (Maybe x)` instead, but doing so runs the risk of getting confused: which is Nothing and which is `Just Nothing`?

Unfortunately, there are two Applicative instances.

One is easy to motivate intrinsically. If we think of Anybody as an empty list, Somebody as a singleton list, and Nobody as a multi-element list, and think of the applicative instance on as corresponding to the cartesian product, then we get an Applicative instance with

Anybody <*> Anybody = Anybody
Anybody <*> Nobody = Nobody

This however cannot possibly correspond to a Monad instance (if the first argument of >>= is Anybody, there's no way of inspecting the second). We thus choose another, which does.

Synopsis

Documentation

data Agreement a Source #

We have the following constructors:

  • Somebody is a consistent choice of an element.
  • Nobody is an inconsistent choice.
  • Anybody is a failure to make any choice.

Constructors

Anybody 
Somebody a 
Nobody 

Instances

Instances details
Applicative Agreement Source #

Not the only possible instance: see introduction

Instance details

Defined in Data.Agreement

Methods

pure :: a -> Agreement a #

(<*>) :: Agreement (a -> b) -> Agreement a -> Agreement b #

liftA2 :: (a -> b -> c) -> Agreement a -> Agreement b -> Agreement c #

(*>) :: Agreement a -> Agreement b -> Agreement b #

(<*) :: Agreement a -> Agreement b -> Agreement a #

Functor Agreement Source # 
Instance details

Defined in Data.Agreement

Methods

fmap :: (a -> b) -> Agreement a -> Agreement b #

(<$) :: a -> Agreement b -> Agreement a #

Monad Agreement Source # 
Instance details

Defined in Data.Agreement

Methods

(>>=) :: Agreement a -> (a -> Agreement b) -> Agreement b #

(>>) :: Agreement a -> Agreement b -> Agreement b #

return :: a -> Agreement a #

Eq a => Monoid (Agreement a) Source # 
Instance details

Defined in Data.Agreement

Eq a => Semigroup (Agreement a) Source # 
Instance details

Defined in Data.Agreement

Methods

(<>) :: Agreement a -> Agreement a -> Agreement a #

sconcat :: NonEmpty (Agreement a) -> Agreement a #

stimes :: Integral b => b -> Agreement a -> Agreement a #

Show a => Show (Agreement a) Source # 
Instance details

Defined in Data.Agreement

Eq a => Eq (Agreement a) Source # 
Instance details

Defined in Data.Agreement

Methods

(==) :: Agreement a -> Agreement a -> Bool #

(/=) :: Agreement a -> Agreement a -> Bool #

Ord a => Ord (Agreement a) Source # 
Instance details

Defined in Data.Agreement

getSomebody :: Agreement a -> Maybe a Source #

This picks out consistent choices as Just.