HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulereflection-2.1.9Haskell98

Data.Reflection

Reifies arbitrary terms at the type level. Based on the Functional Pearl: Implicit Configurations paper by Oleg Kiselyov and Chung-chieh Shan.

http://okmij.org/ftp/Haskell/tr-15-04.pdf

The approach from the paper was modified to work with Data.Proxy and to cheat by using knowledge of GHC's internal representations by Edward Kmett and Elliott Hird.

Usage comes down to two combinators, reify and reflect.

Example1 expression
reify 6 (\p -> reflect p + reflect p)12

The argument passed along by reify is just a data Proxy t = Proxy, so all of the information needed to reconstruct your value has been moved to the type level. This enables it to be used when constructing instances (see examples/Monoid.hs).

In addition, a simpler API is offered for working with singleton values such as a system configuration, etc.

  • 8 types
  • 2 classes
  • 13 values
  • Packagereflection-2.1.9
  • Exports23
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceReflection.hs

Reflection

5 declarations
classclass Reifies (s :: k) a | s -> a where
#

Methods

  • reflect :: proxy s -> a

    Recover a value inside a reify context, given a proxy for its reified type.

Instances8Reifies, …
  • KnownSymbol n => Reifies n StringDefined in reflection-2.1.9 · Data.Reflection
  • KnownNat n => Reifies n IntegerDefined in reflection-2.1.9 · Data.Reflection
  • Reifies Z IntDefined in reflection-2.1.9 · Data.Reflection
  • Reifies n Int => Reifies (D n) IntDefined in reflection-2.1.9 · Data.Reflection
  • Reifies n Int => Reifies (PD n) IntDefined in reflection-2.1.9 · Data.Reflection
  • Reifies n Int => Reifies (SD n) IntDefined in reflection-2.1.9 · Data.Reflection
  • Reifies (StableBox w0 w1 a) (Box b) => Reifies (Stable w0 w1 a) bDefined in reflection-2.1.9 · Data.Reflection
  • (B b0, B b1, B b2, B b3, B b4, B b5, B b6, B b7, w0 ~ W b0 b1 b2 b3, w1 ~ W b4 b5 b6 b7) => Reifies (StableBox w0 w1 a) (Box a)Defined in reflection-2.1.9 · Data.Reflection
valuereify :: a -> (forall s. Reifies s a => Proxy s -> r) -> r
#

Reify a value at the type level, to be recovered with reflect.

valuereifyNat :: Integer -> (forall (n :: Nat). KnownNat n => Proxy n -> r) -> r
#

This upgraded version of reify can be used to generate a KnownNat suitable for use with other APIs.

Attemping to pass a negative Integer as an argument will result in an Underflow exception.

Available only on GHC 7.8+

Example1 expression
import GHC.TypeLits
Example1 expression
reifyNat 4 natVal4
Example1 expression
reifyNat 4 reflect4
valuereifySymbol
  1. :: String
  2. -> forall (n :: Symbol). KnownSymbol n => Proxy n -> r
  3. -> r
#

This upgraded version of reify can be used to generate a KnownSymbol suitable for use with other APIs.

Available only on GHC 7.8+

Example1 expression
import GHC.TypeLits
Example1 expression
reifySymbol "hello" symbolVal"hello"
Example1 expression
reifySymbol "hello" reflect"hello"

Given

2 declarations
classclass Given a where
#

This is a version of Reifies that allows for only a single value.

This is easier to work with than Reifies and permits extended defaulting, but it only offers a single reflected value of a given type at a time.

Methods

  • given :: a

    Recover the value of a given type previously encoded with give.

valuegive :: a -> (Given a => r) -> r
#

Reify a value into an instance to be recovered with given.

You should only give a single value for each type. If multiple instances are in scope, then the behavior is implementation defined.

Template Haskell reflection

2 declarations
valueint :: Int -> TypeQ
#

This can be used to generate a template haskell splice for a type level version of a given int.

This does not use GHC TypeLits, instead it generates a numeric type by hand similar to the ones used in the "Functional Pearl: Implicit Configurations" paper by Oleg Kiselyov and Chung-Chieh Shan.

instance Num (Q Exp) provided in this package allows writing $(3) instead of $(int 3).

valuenat :: Int -> TypeQ
#

This is a restricted version of int that can only generate natural numbers. Attempting to generate a negative number results in a compile time error. Also the resulting sequence will consist entirely of Z, D, and SD constructors representing the number in zeroless binary.

Useful compile time naturals

4 declarations
datadata Z
#

0

Instances1Reifies
  • Reifies Z IntDefined in reflection-2.1.9 · Data.Reflection
datadata D n
#

2n

Instances1Reifies

Reified Monoids

5 declarations
valuefoldBy :: Foldable t => (a -> a -> a) -> a -> t a -> a
#

Fold a value using its Foldable instance using explicitly provided Monoid operations. This is like fold where the Monoid instance can be manually specified.

foldBy mappend mempty ≡ fold
Example1 expression
foldBy (++) [] ["hello","world"]"helloworld"

Reified Applicatives

5 declarations

Orphan instances

4 instances