Apache-2.0 licensed by XT
Maintained by [email protected]
This version can be pinned in stack with:from-1.0.0.1@sha256:ff53dbec684d26a673bbc1ac0f376dbb78bac5235a3aa9cf86d00f15daf82573,3126

Module documentation for 1.0.0.1

Depends on 1 package(full list with versions):
Used by 1 package in nightly-2025-10-26(full list with versions):

from

Note that this package is a part of the “project” that includes from and from-string. For the general rationale behind this idea, see README.md under the project root.

This Haskell library package provides the From and TryFrom typeclasses.

The From typeclass provides from :: a -> b. It is an interface for types that can be converted from/to each other.

The TryFrom typeclass provides tryFrom :: a -> Maybe b. It is an interface for types that can be converted from/to each other, with the possibility of a failure.

This package has 1 dependency: base.

Instances

It is difficult and often impossible to “selectively” import/export typeclass instances. Therefore, we need to be careful about what instances to provide by default.

There are base functions, some of them even in Prelude, that basically serve as a less general version of from. Examples include fromIntegral and fromEnum. However, naively defining

  • instance (Integral a, Num b) => From a b and
  • instance (Enum a) => From a Int

will quickly lead us to trouble:

    • Overlapping instances for From Int16 Int
        arising from a use of ‘from’
      Matching instances:
        instance (Integral a, Num b) => From a b
        instance Enum a => From a Int
      ...

Therefore, this package does not provide such “contextual” instances.

Instead, concrete instances are provided. For example, all pairs (excluding self to self) of these integral types are instantiated using fromIntegral:

  • Int
  • Integer
  • Int8
  • Int16
  • Int32
  • Int64
  • Word8
  • Word16
  • Word32
  • Word64

Each type can be both source and destination of from. Additional types are instantiated as destination-only:

  • Float
  • Double

Changes

Changelog for from

All notable changes to this project should be documented in this file.

The format is based on Keep a Changelog, and this project adheres to the Haskell Package Versioning Policy.

1.0.0.1 - 2025-10-24

Changed

  • Split the test code into ordinary test and code generation test. The code generation test will not be run by default (by Hackage or Stackage, for example).

1.0.0.0 - 2025-10-24

Initial release.

Added

  • Typeclasses From and TryFrom.
  • Instances of From for some fundamental type-mappings in base:
    • All types in Data.Int and Data.Word can be both the source and the destination of the conversion.
    • The types Float and Double can be the destination, but not the source. So from :: Int32 -> Float exists, while from :: Float -> Int32 does not.
    • That’s it. We begin with minimal instances intentionally. For example, there is no instance From Int String. This is because it is tricky (often impossible) to selectively import instances, especially when the module dependency graph is complicated. We’d need to be careful about adding instances.