linear-base-0.4.0: Standard library for linear types.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Prelude.Linear.GenericUtil

Description

FixupMetaData a g copies the metadata from the GHC.Generics.Generic representation of a to the representation g.

FixupMetaData1 f g does something similar when f Any is an instance of Generic and g is a Rep1. See the individual type documentation for details.

This is intended to help users instantiate Rep and Rep1 for types with nonlinear or multiplicity-polymorphic fields.

Suggested use

You will need to derive a GHC.Generics.Generic instance for the type. This is used to obtain the correct metadata.

Next you need to construct a Rep and/or Rep1 for your type, ignoring the metadata.

Constructing the actual representations can be a bit annoying, but GHC can help.

For Rep

Once you have derived GHC.Generics.Generic for your type, define a value like

test :: Rep T a
test = _

Then compile. The stripped representation you need will be in the error message.

For Rep1

Construct a type with the same shape as the one you wish to instantiate, but with only linear fields. Strictness annotations and UNPACK pragmas are irrelevant here.

Instantiate Generics.Linear.Generic1 for the lookalike using deriveGeneric1 and follow the same procedure as above (but with Rep1 T, of course) to get a metadata-stripped representation.

For either

To avoid confusion, replace at least the package and module names in the representation with Any. Wrap MP1 around any nonlinear/representation polymorphic fields, just under the S1 type constructor. The first type argument of MP1 will indicate the multiplicity.

Synopsis

Documentation

type FixupMetaData (a :: Type) (g :: Type -> Type) = Fixup (Rep a) g Source #

FixupMetaData a g copies the metadata from the GHC.Generics.Generic representation of a to the representation g. It also checks that the structure of Rep a is the same as g, except that g may have MP1 applications under some S1 constructors.

Example

instance Generic (Ur a) where
  type Rep (Ur a) = FixupMetaData (Ur a)
        (D1 Any
          (C1 Any
            (S1 Any
              (MP1 'Many (Rec0 a)))))

type FixupMetaData1 (f :: k -> Type) (g :: k -> Type) = Fixup1 (Rep (f Any)) g Source #

FixupMetaData1 f g copies the metadata from the GHC.Generics.Generic representation of f Any to the representation g. It also checks that the overall structure of Rep (f Any) is the same as g, but does not check that their fields match.

Example

instance Generic1 Ur where
  type Rep1 Ur = FixupMetaData1 Ur
         (D1 Any
            (C1 Any
               (S1 Any
                  (MP1 'Many Par1))))

type family RemoveMetaData (f :: k -> Type) :: k -> Type where ... Source #