HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulecapability-0.5.0.1Haskell2010

Capability.Reflection

Use this module to provide an ad-hoc interpreter for a capability using type class reflection.

Use the functions interpret_ or interpret for ad-hoc interpretation of capabilities.

Refer to Reflected if you would like to enable reflection for a new capability.

More complex examples using this module can be found in the Reflection example module.

For details on reflection refer to the tutorial at https://www.tweag.io/posts/2017-12-21-reflection-tutorial.html and the reflection library at https://hackage.haskell.org/package/reflection.

  • 2 types
  • 1 class
  • 4 values
  • Packagecapability-0.5.0.1
  • Exports9
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceReflection.hs

Reflection

5 declarations
valueinterpret_
  1. :: (Monad m, forall s. Reifies s (Reified tag c m) => c (Reflected s c m))
  2. => Reified tag c m
  3. -> forall (m' :: Type -> Type). c m' => m' a
  4. -> m a
#
interpret_ @tag dict action

Execute action using the ad-hoc interpretation of a capability c under tag defined by dict, where dict is a value of type Reified tag c, i.e. a record providing the implementation of the methods of capability c.

For example, the following provides an ad-hoc interpretation for the Capability.Source.HasSource capability.

Example1 expression
:{  interpret_ @"my-source"    ReifiedSource { _await = pure "capabilities" }    (replicateM 3 (await @"my-source"))  :}["capabilities", "capabilities", "capabilities"]
valueinterpret
  1. :: (Monad m, All cs m, forall s. Reifies s (Reified tag c m) => c (Reflected s c m))
  2. => Reified tag c m
  3. -> forall (m' :: Type -> Type). All (c ': cs) m' => m' a
  4. -> m a
#
interpret @tag @ambient dict action

Like interpret_ but forwards the ambient capabilities ambient into the context of action as well.

For example, the following provides an ad-hoc interpretation for the Capability.Source.HasSource capability, while using an ambient Capability.Sink.HasSink capability.

Example1 expression
:{  interpret @"my-source" @'[HasSink "my-sink" String]    ReifiedSource { _await = pure "capabilities" }    (replicateM_ 3 (await @"my-source" >>= yield @"my-sink"))  :}
data familydata family Reified (tag :: k) (c :: Capability) (m :: Type -> Type)
#
Reified tag capability m

Defines the dictionary type for the methods of capability under tag in the monad m. Refer to interpret_ for an example use-case.

For example, the Capability.Sink.HasSink capability has the method Capability.Sink.yield :: a -> m (). The corresponding dictionary type is defined as follows.

Example1 expression
:{  data instance Reified tag (HasSink tag a) m =    ReifiedSink { _yield :: forall a. a -> m () }  :}

Superclass dictionaries are represented as nested records. For example, the Capability.State.HasState capability has the superclasses Capability.Source.HasSource and Capability.Sink.HasSink and the method Capability.State.state :: (s -> (a, s)) -> m a. The corresponding dictionary type is defined as follows.

Example1 expression
:{  data instance Reified tag (HasState tag s) m =    ReifiedState      { _stateSource :: Reified tag (HasSource tag s) m,        _stateSink :: Reified tag (HasSink tag s) m,        _state :: forall a. (s -> (a, s)) -> m a      }  :}
Instances7Reified, …
newtypenewtype Reflected s (c :: Capability) (m :: Type -> Type) a
#
Reflected s capability m

Carries the type class instance for capability in the monad m defined by the dictionary reflected in s in the type system.

For most use-cases it is not necessary to use this type directly. Use interpret_ or interpret instead.

If you wish to enable reflection for a new capability, then you will need to define a type class instance for Reflected for the new capability. Note, you will also need to define an instance of Reified which defines the dictionary type of the new capability. Hint, you can use reified @s to obtain the dictionary from the context in the instance implementation.

For example, the Reflected instance for the Capability.Sink.HasSink capability can be defined as follows. Assuming the dictionary described in Reified.

Example1 expression
:{  instance    (Monad m, Reifies s (Reified tag (HasSink tag a) m)) =>    HasSink tag a (Reflected s (HasSink tag a) m)    where    yield a = Reflect $ _yield (reified @s) a  :}

Constructors

Instances15HasCatch, HasThrow, HasReader, HasSource, HasState, HasSink, …
valuereified :: Reifies s (Reified tag c m) => Reified tag c m
#
reified @s

Obtain the dictionary that is reflected in the type system under s.

This is a convenience wrapper around reflect.

Re-exported

4 declarations
classclass Reifies (s :: k) a | s -> a where
#
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.

methodreflect :: proxy s -> a
#

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

datadata Proxy (t :: k)
#

Proxy is a type that holds no data, but has a phantom parameter of arbitrary type (or even kind). Its use is to provide type information, even though there is no value available of that type (or it may be too costly to create one).

Historically, Proxy :: Proxy a is a safer alternative to the undefined :: a idiom.

Example1 expression
Proxy :: Proxy (Void, Int -> Int)Proxy

Proxy can even hold types of higher kinds,

Example1 expression
Proxy :: Proxy EitherProxy
Example1 expression
Proxy :: Proxy FunctorProxy
Example1 expression
Proxy :: Proxy complicatedStructureProxy
Instances57Generic1, FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Monad, Functor, …
  • Generic1 ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Generics
  • FoldableWithIndex Void ProxyDefined in indexed-traversable-0.1.5 · WithIndex
  • FunctorWithIndex Void ProxyDefined in indexed-traversable-0.1.5 · WithIndex
  • TraversableWithIndex Void ProxyDefined in indexed-traversable-0.1.5 · WithIndex
  • Monad ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Functor ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Applicative ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Foldable ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Foldable
  • Traversable ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Traversable
  • Alternative ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • MonadPlus ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • MonadZip ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.Zip
  • Eq1 ProxyDefined in base-4.22.0.0 · Data.Functor.Classes
  • Ord1 ProxyDefined in base-4.22.0.0 · Data.Functor.Classes
  • Read1 ProxyDefined in base-4.22.0.0 · Data.Functor.Classes
  • Show1 ProxyDefined in base-4.22.0.0 · Data.Functor.Classes
  • Contravariant ProxyDefined in base-4.22.0.0 · Data.Functor.Contravariant
  • NFData1 ProxyDefined in deepseq-1.5.1.0 · Control.DeepSeq
  • Hashable1 ProxyDefined in hashable-1.5.1.0 · Data.Hashable.Class
  • Distributive ProxyDefined in distributive-0.6.3 · Data.Distributive
  • Decidable ProxyDefined in contravariant-1.5.6 · Data.Functor.Contravariant.Divisible
  • Divisible ProxyDefined in contravariant-1.5.6 · Data.Functor.Contravariant.Divisible
  • Alt ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Alt
  • Apply ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Bind.Class
  • Bind ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Bind.Class
  • Extend ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Extend
  • Conclude ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Contravariant.Conclude
  • Decide ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Contravariant.Decide
  • Divise ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Contravariant.Divise
  • Plus ProxyDefined in semigroupoids-6.0.2 · Data.Functor.Plus
  • Representable ProxyDefined in adjunctions-4.4.4 · Data.Functor.Rep
  • Representable ProxyDefined in adjunctions-4.4.4 · Data.Functor.Contravariant.Rep
  • Invariant ProxyDefined in invariant-0.6.5 · Data.Functor.Invariant
  • Cosieve Tagged ProxyDefined in profunctors-5.6.3 · Data.Profunctor.Sieve
  • Bounded (Proxy t)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Enum (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Eq (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Data t => Data (Proxy t)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Data
  • Ord (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Read (Proxy t)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Show (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Ix (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Generic (Proxy t)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics
  • Semigroup (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • Monoid (Proxy s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Proxy
  • NFData (Proxy a)Defined in deepseq-1.5.1.0 · Control.DeepSeq
  • Hashable (Proxy a)Defined in hashable-1.5.1.0 · Data.Hashable.Class
  • Boring (Proxy a)Defined in boring-0.2.2.1 · Data.Boring
  • MonoFoldable (Proxy a)Defined in mono-traversable-1.0.21.0 · Data.MonoTraversable
  • MonoFunctor (Proxy a)Defined in mono-traversable-1.0.21.0 · Data.MonoTraversable
  • MonoTraversable (Proxy a)Defined in mono-traversable-1.0.21.0 · Data.MonoTraversable
  • MonoPointed (Proxy a)Defined in mono-traversable-1.0.21.0 · Data.MonoTraversable
  • type Rep (Proxy t) = D1 ('MetaData "Proxy" "GHC.Internal.Data.Proxy" "ghc-internal" 'False) (C1 ('MetaCons "Proxy" 'PrefixI 'False) U1)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics
  • type Rep1 Proxy = D1 ('MetaData "Proxy" "GHC.Internal.Data.Proxy" "ghc-internal" 'False) (C1 ('MetaCons "Proxy" 'PrefixI 'False) U1)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics
  • type Rep Proxy = VoidDefined in adjunctions-4.4.4 · Data.Functor.Rep
  • type Rep Proxy = ()Defined in adjunctions-4.4.4 · Data.Functor.Contravariant.Rep
  • type Element (Proxy a) = aDefined in mono-traversable-1.0.21.0 · Data.MonoTraversable