inj

A class for injective (one-to-one) functions

LTS Haskell 24.58:1.0
Stackage Nightly 2026-09-07:2.0
Latest on Hackage:2.0

See all snapshots inj appears in

BSD-3-Clause licensed by Vladislav Zavialov
Maintained by Vladislav Zavialov
This version can be pinned in stack with:inj-2.0@sha256:5d40dbc87978ac877087a9e2219be48ea2e17d59707ab3df29b3d98ae341e6a5,1170

Module documentation for 2.0

Depends on 1 package(full list with versions):

inj

A class for injective (one-to-one) functions.

An injection is a function that never maps distinct elements of the domain to the same element of the codomain. For example, \x -> x + 1 is an injection, but \x -> min x 0 is not.

class Inj p a where
  inj :: p -> a

The instances compose, so inj can construct nested structures from singleton elements, wrapping and converting as needed:

ghci> inj 'a' :: Maybe [Char]
Just "a"

ghci> inj True :: Maybe [Bool]
Just [True]

ghci> inj (5 :: Int) :: Maybe Double
Just 5.0

ghci> inj [1, 2, 3 :: Int] :: [Double]
[1.0,2.0,3.0]

ghci> inj (True, 2 :: Int) :: (Maybe Bool, [Double])
(Just True,[2.0])

By convention, the instances of Inj never match on p and always match on a. This guarantees that users will not encounter overlapping instances.

Instances for base types are provided by this package. Before version 2.0 they lived in a separate inj-base package; see the changelog for migration notes.

Changes

Changelog

2.0

  • Merged inj-base into inj. The instances for base types are no longer orphans and no longer require a separate package.
  • Renamed the Inj module to Control.Inj. Users of inj-1.0 should update their imports from Inj to Control.Inj; users of inj-base-0.2 should replace Inj.Base with Control.Inj.
  • Removed the instance for Data.Semigroup.Option, which no longer exists in base.
  • Added the missing identity instance for Char, so that inj 'a' :: String and similar now work.
  • Changed the license from PublicDomain to BSD-3-Clause.

1.0

  • Initial release.