injections-0.1.0.0: Canonical categorical conversions (injections and projections)
Copyright(c) 2020 Thomas Tuegel
LicenseBSD-3-Clause
MaintainerThomas Tuegel <ttuegel@mailbox.org>
Safe HaskellSafe-Inferred
LanguageHaskell2010

Injection

Description

 
Synopsis

Documentation

class Injection from into where Source #

Injection describes a lossless conversion that includes one type in another.

The sole method of this class,

inject :: from -> into

takes a value input :: from and returns a value output :: into which preserves all the information contained in the input. Specifically, each input is mapped to a unique output. In mathematical terminology, inject is injective:

inject a ≡ inject b → a ≡ b

The name of the class is derived from the mathematical term.

Injection models the "is-a" relationship used in languages with subtypes (such as in object-oriented programming), but an explicit cast with inject is required in Haskell.

Although it is often possible to infer the type parameters of this class, it is advisable to specify one or both of the parameters to inject using a type signature or the TypeApplications language extension. Specifying the type parameters will give clearer error messages from the type checker in any case.

Methods

inject :: from -> into Source #

Instances

Instances details
Injection Bool All Source # 
Instance details

Defined in Injection

Methods

inject :: Bool -> All Source #

Injection Bool Any Source # 
Instance details

Defined in Injection

Methods

inject :: Bool -> Any Source #

Injection Natural Integer Source # 
Instance details

Defined in Injection

Typeable a => Injection a Dynamic Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Dynamic Source #

Injection a a Source # 
Instance details

Defined in Injection

Methods

inject :: a -> a Source #

Injection Void any Source # 
Instance details

Defined in Injection

Methods

inject :: Void -> any Source #

Injection All Bool Source # 
Instance details

Defined in Injection

Methods

inject :: All -> Bool Source #

Injection Any Bool Source # 
Instance details

Defined in Injection

Methods

inject :: Any -> Bool Source #

Injection Text String Source #

unpack is the canonical injection Text -> String. There is no injection in the other direction because String can represent invalid surrogate code points, but Text cannot; for details, see Data.Text.Lazy.

Instance details

Defined in Injection

Methods

inject :: Text -> String Source #

Injection Text Text Source # 
Instance details

Defined in Injection

Methods

inject :: Text0 -> Text Source #

Injection Text String Source #

unpack is the canonical injection Text -> String. There is no injection in the other direction because String can represent invalid surrogate code points, but Text cannot; for details, see Data.Text.

Instance details

Defined in Injection

Methods

inject :: Text -> String Source #

Injection Text Text Source # 
Instance details

Defined in Injection

Methods

inject :: Text -> Text0 Source #

Injection Integer (Ratio Integer) Source # 
Instance details

Defined in Injection

Injection a (Min a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Min a Source #

Injection a (Max a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Max a Source #

Injection a (Last a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Last a Source #

Injection a (First a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> First a Source #

Injection a (First a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> First a Source #

Injection a (Last a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Last a Source #

Injection a (Dual a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Dual a Source #

Injection a (Sum a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Sum a Source #

Injection a (Product a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Product a Source #

Injection a (Down a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Down a Source #

Injection a (Identity a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Identity a Source #

Num a => Injection a (Complex a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Complex a Source #

Injection a b => Injection a (Maybe b) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Maybe b Source #

HasResolution a => Injection Integer (Fixed a) Source # 
Instance details

Defined in Injection

Methods

inject :: Integer -> Fixed a Source #

Injection a (r -> a) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> r -> a Source #

Injection a (Const a b) Source # 
Instance details

Defined in Injection

Methods

inject :: a -> Const a b Source #

Injection (Min a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Min a -> a Source #

Injection (Max a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Max a -> a Source #

Injection (First a) a Source # 
Instance details

Defined in Injection

Methods

inject :: First a -> a Source #

Injection (Last a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Last a -> a Source #

Injection (Identity a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Identity a -> a Source #

Injection (Dual a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Dual a -> a Source #

Injection (Sum a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Sum a -> a Source #

Injection (Product a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Product a -> a Source #

Injection (Down a) a Source # 
Instance details

Defined in Injection

Methods

inject :: Down a -> a Source #

Injection a b => Injection (Maybe a) [b] Source # 
Instance details

Defined in Injection

Methods

inject :: Maybe a -> [b] Source #

Injection (NonEmpty a) [a] Source # 
Instance details

Defined in Injection

Methods

inject :: NonEmpty a -> [a] Source #

Injection (Const a b) a Source # 
Instance details

Defined in Injection

Methods

inject :: Const a b -> a Source #

class Injection from into => Retraction from into where Source #

Retraction undoes an Injection.

Because Injection is a lossless conversion, we can define a Retraction which undoes it. The method

retract :: into -> Maybe from

is the (left) inverse of inject:

retract (inject x) = Just x

retract is partial (returns Maybe) because the type into may be larger than the type from; that is, there may be values in into which are not inject-ed from from, and in that case retract may return Nothing.

Although it is often possible to infer the type parameters of this class, it is advisable to specify one or both of the parameters to retract using a type signature or the TypeApplications language extension. Specifying the type parameters will give clearer error messages from the type checker in any case.

Methods

retract :: into -> Maybe from Source #

Instances

Instances details
Retraction Natural Integer Source # 
Instance details

Defined in Injection

Typeable a => Retraction a Dynamic Source # 
Instance details

Defined in Injection

Methods

retract :: Dynamic -> Maybe a Source #

Retraction a a Source # 
Instance details

Defined in Injection

Methods

retract :: a -> Maybe a Source #

Retraction Integer (Ratio Integer) Source # 
Instance details

Defined in Injection

Retraction a (First a) Source # 
Instance details

Defined in Injection

Methods

retract :: First a -> Maybe a Source #

Retraction a (Last a) Source # 
Instance details

Defined in Injection

Methods

retract :: Last a -> Maybe a Source #

(Eq a, Num a) => Retraction a (Complex a) Source # 
Instance details

Defined in Injection

Methods

retract :: Complex a -> Maybe a Source #

Retraction a b => Retraction a (Maybe b) Source # 
Instance details

Defined in Injection

Methods

retract :: Maybe b -> Maybe a Source #

HasResolution a => Retraction Integer (Fixed a) Source # 
Instance details

Defined in Injection

Retraction a b => Retraction (Maybe a) [b] Source # 
Instance details

Defined in Injection

Methods

retract :: [b] -> Maybe (Maybe a) Source #

Retraction (NonEmpty a) [a] Source # 
Instance details

Defined in Injection

Methods

retract :: [a] -> Maybe (NonEmpty a) Source #