Copyright | (c) 2020 Thomas Tuegel |
---|---|
License | BSD-3-Clause |
Maintainer | Thomas Tuegel <ttuegel@mailbox.org> |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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.
Instances
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.
Instances
Retraction Natural Integer Source # | |
Typeable a => Retraction a Dynamic Source # | |
Retraction a a Source # | |
Retraction Integer (Ratio Integer) Source # | |
Retraction a (First a) Source # | |
Retraction a (Last a) Source # | |
(Eq a, Num a) => Retraction a (Complex a) Source # | |
Retraction a b => Retraction a (Maybe b) Source # | |
HasResolution a => Retraction Integer (Fixed a) Source # | |
Retraction a b => Retraction (Maybe a) [b] Source # | |
Retraction (NonEmpty a) [a] Source # | |