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

Modulegenerics-sop-0.5.1.4Haskell2010

Generics.SOP

Main module of generics-sop

In most cases, you will probably want to import just this module, and possibly Generics.SOP.TH if you want to use Template Haskell to generate Generic instances for you.

Generic programming with sums of products

You need this library if you want to define your own generic functions in the sum-of-products SOP style. Generic programming in the SOP style follows the following idea:

  1. A large class of datatypes can be viewed in a uniform, structured way: the choice between constructors is represented using an n-ary sum (called NS), and the arguments of each constructor are represented using an n-ary product (called NP).

  2. The library captures the notion of a datatype being representable in the following way. There is a class Generic, which for a given datatype A, associates the isomorphic SOP representation with the original type under the name Rep A. The class also provides functions from and to that convert between A and Rep A and witness the isomorphism.

  3. Since all Rep types are sums of products, you can define functions over them by performing induction on the structure, or by using predefined combinators that the library provides. Such functions then work for all Rep types.

  4. By combining the conversion functions from and to with the function that works on Rep types, we obtain a function that works on all types that are in the Generic class.

  5. Most types can very easily be made an instance of Generic. For example, if the datatype can be represented using GHC's built-in approach to generic programming and has an instance for the Generic class from module GHC.Generics, then an instance of the SOP Generic can automatically be derived. There is also Template Haskell code in Generics.SOP.TH that allows to auto-generate an instance of Generic for most types.

Example

Instantiating a datatype for use with SOP generics

Let's assume we have the datatypes:

data A   = C Bool | D A Int | E (B ())
data B a = F | G a Char Bool

To create Generic instances for A and B via GHC.Generics, we say

{-# LANGUAGE DeriveGeneric #-}

import qualified GHC.Generics as GHC
import Generics.SOP

data A   = C Bool | D A Int | E (B ())
  deriving (Show, GHC.Generic)
data B a = F | G a Char Bool
  deriving (Show, GHC.Generic)

instance Generic A     -- empty
instance Generic (B a) -- empty

Now we can convert between A and Rep A (and between B and Rep B). For example,

Example2 expressions
from (D (C True) 3) :: Rep ASOP (S (Z (I (C True) :* I 3 :* Nil)))to it :: AD (C True) 3

Note that the transformation is shallow: In D (C True) 3, the inner value C True of type A is not affected by the transformation.

For more details about Rep A, have a look at the Generics.SOP.Universe module.

Defining a generic function

As an example of a generic function, let us define a generic version of rnf from the deepseq package.

The type of rnf is

NFData a => a -> ()

and the idea is that for a term x of type a in the NFData class, rnf x forces complete evaluation of x (i.e., evaluation to normal form), and returns ().

We call the generic version of this function grnf. A direct definition in SOP style, making use of structural recursion on the sums and products, looks as follows:

grnf :: (Generic a, All2 NFData (Code a)) => a -> ()
grnf x = grnfS (from x)

grnfS :: (All2 NFData xss) => SOP I xss -> ()
grnfS (SOP (Z xs))  = grnfP xs
grnfS (SOP (S xss)) = grnfS (SOP xss)

grnfP :: (All NFData xs) => NP I xs -> ()
grnfP Nil         = ()
grnfP (I x :* xs) = x `deepseq` (grnfP xs)

The grnf function performs the conversion between a and Rep a by applying from and then applies grnfS. The type of grnf indicates that a must be in the Generic class so that we can apply from, and that all the components of a (i.e., all the types that occur as constructor arguments) must be in the NFData class (All2).

The function grnfS traverses the outer sum structure of the sum of products (note that Rep a = SOP I (Code a)). It encodes which constructor was used to construct the original argument of type a. Once we've found the constructor in question (Z), we traverse the arguments of that constructor using grnfP.

The function grnfP traverses the product structure of the constructor arguments. Each argument is evaluated using the deepseq function from the NFData class. This requires that all components of the product must be in the NFData class (All) and triggers the corresponding constraints on the other functions. Once the end of the product is reached (Nil), we return ().

Defining a generic function using combinators

In many cases, generic functions can be written in a much more concise way by avoiding the explicit structural recursion and resorting to the powerful combinators provided by this library instead.

For example, the grnf function can also be defined as a one-liner as follows:

grnf :: (Generic a, All2 NFData (Code a)) => a -> ()
grnf = rnf . hcollapse . hcmap (Proxy :: Proxy NFData) (mapIK rnf) . from

mapIK and friends (mapII, mapKI, etc.) are small helpers for working with I and K functors, for example mapIK is defined as mapIK f = \ (I x) -> K (f x)

The following interaction should provide an idea of the individual transformation steps:

Example5 expressions
let x = G 2.5 'A' False :: B Doublefrom xSOP (S (Z (I 2.5 :* I 'A' :* I False :* Nil)))hcmap (Proxy :: Proxy NFData) (mapIK rnf) itSOP (S (Z (K () :* K () :* K () :* Nil)))hcollapse it[(),(),()]rnf it()

The from call converts into the structural representation. Via hcmap, we apply rnf to all the components. The result is a sum of products of the same shape, but the components are no longer heterogeneous (I), but homogeneous (K ()). A homogeneous structure can be collapsed (hcollapse) into a normal Haskell list. Finally, rnf actually forces evaluation of this list (and thereby actually drives the evaluation of all the previous steps) and produces the final result.

Using a generic function

We can directly invoke grnf on any type that is an instance of class Generic.

Example2 expressions
grnf (G 2.5 'A' False)()grnf (G 2.5 undefined False)*** Exception: Prelude.undefined...

Note that the type of grnf requires that all components of the type are in the NFData class. For a recursive datatype such as B, this means that we have to make A (and in this case, also B) an instance of NFData in order to be able to use the grnf function. But we can use grnf to supply the instance definitions:

instance NFData A where rnf = grnf
instance NFData a => NFData (B a) where rnf = grnf

More examples

The best way to learn about how to define generic functions in the SOP style is to look at a few simple examples. Examples are provided by the following packages:

The generic functions in these packages use a wide variety of the combinators that are offered by the library.

Paper

A detailed description of the ideas behind this library is provided by the paper:

  • 33 types
  • 18 classes
  • 80 values

Codes and interpretations

16 declarations
classclass All SListI (Code a) => Generic a where
#

The class of representable datatypes.

The SOP approach to generic programming is based on viewing datatypes as a representation (Rep) built from the sum of products of its components. The components of a datatype are specified using the Code type family.

The isomorphism between the original Haskell datatype and its representation is witnessed by the methods of this class, from and to. So for instances of this class, the following laws should (in general) hold:

to . from === id :: a -> a
from . to === id :: Rep a -> Rep a

You typically don't define instances of this class by hand, but rather derive the class instance automatically.

Option 1: Derive via the built-in GHC-generics. For this, you need to use the DeriveGeneric extension to first derive an instance of the Generic class from module GHC.Generics. With this, you can then give an empty instance for Generic, and the default definitions will just work. The pattern looks as follows:

import qualified GHC.Generics as GHC
import Generics.SOP

...

data T = ... deriving (GHC.Generic, ...)

instance Generic T -- empty
instance HasDatatypeInfo T -- empty, if you want/need metadata

Option 2: Derive via Template Haskell. For this, you need to enable the TemplateHaskell extension. You can then use deriveGeneric from module Generics.SOP.TH to have the instance generated for you. The pattern looks as follows:

import Generics.SOP
import Generics.SOP.TH

...

data T = ...

deriveGeneric ''T -- derives HasDatatypeInfo as well

Tradeoffs: Whether to use Option 1 or 2 is mainly a matter of personal taste. The version based on Template Haskell probably has less run-time overhead.

Non-standard instances: It is possible to give Generic instances manually that deviate from the standard scheme, as long as at least

to . from === id :: a -> a

still holds.

Associated types

  • type family Code a :: [[Type]]

    The code of a datatype.

    This is a list of lists of its components. The outer list contains one element per constructor. The inner list contains one element per constructor argument (field).

    Example: The datatype

    data Tree = Leaf Int | Node Tree Tree

    is supposed to have the following code:

    type instance Code (Tree a) =
      '[ '[ Int ]
       , '[ Tree, Tree ]
       ]

Methods

  • from :: a -> Rep a

    Converts from a value to its structural representation.

  • to :: Rep a -> a

    Converts from a structural representation back to the original value.

Instances193Generic, …
  • Generic E0Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic E1Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic E12Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic E2Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic E3Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic E6Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic E9Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FieldFormatDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FormatAdjustmentDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FormatParseDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FormatSignDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic VoidDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ByteOrderDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic BlockReasonDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ThreadStatusDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic NestedAtomicallyDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic NoMethodErrorDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic NonTerminationDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic PatternMatchFailDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RecConErrorDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RecSelErrorDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RecUpdErrorDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic TypeErrorDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ConstrRepDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DataRepDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FixityDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic AllDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic AnyDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic VersionDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ErrorCallDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ArithExceptionDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic LocationDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic SrcLocDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FingerprintDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FFFormatDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ErrnoDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CCharDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CClockDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CDoubleDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CFloatDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CIntDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CIntMaxDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CIntPtrDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CLLongDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CLongDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CPtrdiffDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CSCharDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CSUSecondsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CShortDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CSigAtomicDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CSizeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CTimeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CUCharDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CUIntDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CUIntMaxDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CUIntPtrDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CULLongDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CULongDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CUSecondsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CUShortDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CWcharDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic AssociativityDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DecidedStrictnessDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FixityDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic SDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic SourceStrictnessDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic SourceUnpackednessDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic MaskingStateDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic BufferStateDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic IODeviceTypeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic SeekModeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CodingFailureModeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CodingProgressDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic AllocationLimitExceededDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ArrayExceptionDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic AssertionFailedDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic AsyncExceptionDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic BlockedIndefinitelyOnMVarDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic BlockedIndefinitelyOnSTMDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DeadlockDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ExitCodeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic FixIOExceptionDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic IOErrorTypeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic IOExceptionDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic HandlePosnDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic LockModeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic BufferModeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic NewlineDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic NewlineModeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic IOModeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CCFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ConcFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DebugFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DoCostCentresDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DoHeapProfileDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic DoTraceDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic GCFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic GiveGCStatsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic MiscFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ParFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ProfFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RTSFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic TickyFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic TraceFlagsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic CallStackDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic SrcLocDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic StaticPtrInfoDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic GCDetailsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RTSStatsDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic LexemeDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic NumberDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic GeneralCategoryDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic BoolDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic OrderingDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic RuntimeRepDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic VecCountDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic VecElemDefined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ()Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Complex a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (First a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Last a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Max a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Min a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (WrappedMonoid m)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (ArgDescr a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (ArgOrder a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (OptDescr a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (NonEmpty a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Identity a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (First a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Last a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Down a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Dual a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Endo a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Product a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Sum a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Par1 p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Buffer e)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Maybe a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (I a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic [a]Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Fixed a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Arg a b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Either a b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Proxy t)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (U1 p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (V1 p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Const a b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Alt f a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (BufferCodec from to state)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (K a b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Product f g a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Sum f g a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (K1 i c p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ((:*:) f g p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ((:+:) f g p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ((-.->) f g a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (Compose f g a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (M1 i c f p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ((:.:) f g p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic ((:.:) f g p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, t26)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, t26, t27)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, t26, t27, t28)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • Generic (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, t26, t27, t28, t29)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
typetype Rep a = SOP I (Code a)
#

The (generic) representation of a datatype.

A datatype is isomorphic to the sum-of-products of its code. The isomorphism is witnessed by from and to from the Generic class.

typetype IsProductType a (xs :: [Type]) = (Generic a, Code a ~ '[xs])
#

Constraint that captures that a datatype is a product type, i.e., a type with a single constructor.

It also gives access to the code for the arguments of that constructor.

typetype ProductCode a = Head (Code a)
#

Direct access to the part of the code that is relevant for a product type.

typetype IsEnumType a = (Generic a, All ((~) '[]) (Code a))
#

Constraint that captures that a datatype is an enumeration type, i.e., none of the constructors have any arguments.

typetype IsWrappedType a x = (Generic a, Code a ~ '['[x]])
#

Constraint that captures that a datatype is a single-constructor, single-field datatype. This always holds for newtype-defined types, but it can also be true for data-defined types.

The constraint also gives access to the type that is wrapped.

typetype WrappedCode a = Head (Head (Code a))
#

Direct access to the part of the code that is relevant for wrapped types and newtypes.

typetype IsNewtype a x = (IsWrappedType a x, Coercible a x)
#

Constraint that captures that a datatype is a newtype. This makes use of the fact that newtypes are always coercible to the type they wrap, whereas datatypes are not.

n-ary datatypes

6 declarations
datadata NP (a :: k -> Type) (b :: [k]) where
#

An n-ary product.

The product is parameterized by a type constructor f and indexed by a type-level list xs. The length of the list determines the number of elements in the product, and if the i-th element of the list is of type x, then the i-th element of the product is of type f x.

The constructor names are chosen to resemble the names of the list constructors.

Two common instantiations of f are the identity functor I and the constant functor K. For I, the product becomes a heterogeneous list, where the type-level list describes the types of its components. For K a, the product becomes a homogeneous list, where the contents of the type-level list are ignored, but its length still specifies the number of elements.

In the context of the SOP approach to generic programming, an n-ary product describes the structure of the arguments of a single data constructor.

Examples:

I 'x'    :* I True  :* Nil  ::  NP I       '[ Char, Bool ]
K 0      :* K 1     :* Nil  ::  NP (K Int) '[ Char, Bool ]
Just 'x' :* Nothing :* Nil  ::  NP Maybe   '[ Char, Bool ]

Constructors

  • Nil :: NP a '[]
  • (:*) :: a x -> NP a xs -> NP a (x ': xs)infixr 5
Instances19HTrans, HAp, HCollapse, HPure, HSequence, HTraverse_, …
datadata NS (a :: k -> Type) (b :: [k]) where
#

An n-ary sum.

The sum is parameterized by a type constructor f and indexed by a type-level list xs. The length of the list determines the number of choices in the sum and if the i-th element of the list is of type x, then the i-th choice of the sum is of type f x.

The constructor names are chosen to resemble Peano-style natural numbers, i.e., Z is for "zero", and S is for "successor". Chaining S and Z chooses the corresponding component of the sum.

Examples:

Z         :: f x -> NS f (x ': xs)
S . Z     :: f y -> NS f (x ': y ': xs)
S . S . Z :: f z -> NS f (x ': y ': z ': xs)
...

Note that empty sums (indexed by an empty list) have no non-bottom elements.

Two common instantiations of f are the identity functor I and the constant functor K. For I, the sum becomes a direct generalization of the Either type to arbitrarily many choices. For K a, the result is a homogeneous choice type, where the contents of the type-level list are ignored, but its length specifies the number of options.

In the context of the SOP approach to generic programming, an n-ary sum describes the top-level structure of a datatype, which is a choice between all of its constructors.

Examples:

Z (I 'x')      :: NS I       '[ Char, Bool ]
S (Z (I True)) :: NS I       '[ Char, Bool ]
S (Z (K 1))    :: NS (K Int) '[ Char, Bool ]

Constructors

  • Z :: a x -> NS a (x ': xs)
  • S :: NS a xs -> NS a (x ': xs)
Instances17HTrans, HAp, HApInjs, HCollapse, HExpand, HIndex, …
  • HTrans NS NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HAp NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HApInjs NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HCollapse NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HExpand NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HIndex NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HSequence NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HTraverse_ NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • All (Compose Eq f) xs => Eq (NS f xs)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • (All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NS f xs)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • All (Compose Show f) xs => Show (NS f xs)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • All (Compose NFData f) xs => NFData (NS f xs)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • type CollapseTo NS a = aDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type Prod NS = NPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type AllN NS c = All cDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type Same NS = NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type SListIN NS = SListIDefined in sop-core-0.5.0.2 · Data.SOP.NS
newtypenewtype SOP (f :: k -> Type) (xss :: [[k]])
#

A sum of products.

This is a newtype for an NS of an NP. The elements of the (inner) products are applications of the parameter f. The type SOP is indexed by the list of lists that determines the sizes of both the (outer) sum and all the (inner) products, as well as the types of all the elements of the inner products.

A SOP I reflects the structure of a normal Haskell datatype. The sum structure represents the choice between the different constructors, the product structure represents the arguments of each constructor.

Constructors

Instances17HTrans, HAp, HApInjs, HCollapse, HExpand, HIndex, …
  • HTrans SOP SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HAp SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HApInjs SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HCollapse SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HExpand SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HIndex SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HSequence SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HTraverse_ SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • Eq (NS (NP f) xss) => Eq (SOP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • Ord (NS (NP f) xss) => Ord (SOP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • Show (NS (NP f) xss) => Show (SOP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • NFData (NS (NP f) xss) => NFData (SOP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • type CollapseTo SOP a = [a]Defined in sop-core-0.5.0.2 · Data.SOP.NS
  • type Prod SOP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type AllN SOP c = All2 cDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type Same SOP = SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type SListIN SOP = SListI2Defined in sop-core-0.5.0.2 · Data.SOP.NS
valueunSOP :: SOP f xss -> NS (NP f) xss
#

Unwrap a sum of products.

newtypenewtype POP (f :: k -> Type) (xss :: [[k]])
#

A product of products.

This is a newtype for an NP of an NP. The elements of the inner products are applications of the parameter f. The type POP is indexed by the list of lists that determines the lengths of both the outer and all the inner products, as well as the types of all the elements of the inner products.

A POP is reminiscent of a two-dimensional table (but the inner lists can all be of different length). In the context of the SOP approach to generic programming, a POP is useful to represent information that is available for all arguments of all constructors of a datatype.

Constructors

Instances19HTrans, HAp, HCollapse, HPure, HSequence, HTraverse_, …
  • HTrans POP POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HAp POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HCollapse POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HPure POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HSequence POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HTraverse_ POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • Eq (NP (NP f) xss) => Eq (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Ord (NP (NP f) xss) => Ord (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Show (NP (NP f) xss) => Show (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Semigroup (NP (NP f) xss) => Semigroup (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Monoid (NP (NP f) xss) => Monoid (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • NFData (NP (NP f) xss) => NFData (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • type CollapseTo POP a = [[a]]Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • type Prod POP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type UnProd POP = SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS · orphan
  • type AllN POP c = All2 cDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type AllZipN POP c = AllZip2 cDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type Same POP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type SListIN POP = SListI2Defined in sop-core-0.5.0.2 · Data.SOP.NP
valueunPOP :: POP f xss -> NP (NP f) xss
#

Unwrap a product of products.

Metadata

15 declarations
datadata DatatypeInfo (a :: [[Type]]) where
#

Metadata for a datatype.

A value of type DatatypeInfo c contains the information about a datatype that is not contained in Code c. This information consists primarily of the names of the datatype, its constructors, and possibly its record selectors.

The constructor indicates whether the datatype has been declared using newtype or not.

Instances3Eq, Ord, Show
datadata ConstructorInfo (a :: [Type]) where
#

Metadata for a single constructor.

This is indexed by the product structure of the constructor components.

Instances3Eq, Ord, Show
datadata FieldInfo a where
#

For records, this functor maps the component to its selector name.

Constructors

Instances4Functor, Eq, Ord, Show
  • Functor FieldInfoDefined in generics-sop-0.5.1.4 · Generics.SOP.Metadata
  • Eq (FieldInfo a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Metadata
  • Ord (FieldInfo a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Metadata
  • Show (FieldInfo a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Metadata
classclass Generic a => HasDatatypeInfo a where
#

A class of datatypes that have associated metadata.

It is possible to use the sum-of-products approach to generic programming without metadata. If you need metadata in a function, an additional constraint on this class is in order.

You typically don't define instances of this class by hand, but rather derive the class instance automatically. See the documentation of Generic for the options.

Associated types

Methods

  • datatypeInfo :: proxy a -> DatatypeInfo (Code a)

    Term-level datatype info; by default, the term-level datatype info is produced from the type-level info.

Instances193HasDatatypeInfo, …
datadata Associativity
#

Datatype to represent the associativity of a constructor

Instances20Bounded, Enum, Eq, Data, Ord, Read, …
typetype Fixity = Int
#

The fixity of an infix constructor.

Combinators

0 declarations

Constructing products

classclass HPure (h :: (k -> Type) -> l -> Type) where
#

A generalization of pure or return to higher kinds.

Methods

  • hpure :: SListIN h xs => (forall (a :: k). f a) -> h f xs

    Corresponds to pure directly.

    Instances:

    hpure, pure_NP  :: Data.SOP.Sing.SListI  xs  => (forall a. f a) -> NP  f xs
    hpure, pure_POP :: SListI2 xss => (forall a. f a) -> POP f xss
    
  • hcpure :: AllN h c xs => proxy c -> (forall (a :: k). c a => f a) -> h f xs

    A variant of hpure that allows passing in a constrained argument.

    Calling hcpure f s where s :: h f xs causes f to be applied at all the types that are contained in xs. Therefore, the constraint c has to be satisfied for all elements of xs, which is what AllN h c xs states.

    Instances:

    hcpure, cpure_NP  :: (All  c xs ) => proxy c -> (forall a. c a => f a) -> NP  f xs
    hcpure, cpure_POP :: (All2 c xss) => proxy c -> (forall a. c a => f a) -> POP f xss
    
Instances2HPure
  • HPure NPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HPure POPDefined in sop-core-0.5.0.2 · Data.SOP.NP

Destructing products

valuehd :: NP f (x ': xs) -> f x
#

Obtain the head of an n-ary product.

valuetl :: NP f (x ': xs) -> NP f xs
#

Obtain the tail of an n-ary product.

typetype Projection (f :: k -> Type) (xs :: [k]) = K (NP f xs) -.-> f
#

The type of projections from an n-ary product.

A projection is a function from the n-ary product to a single element.

valueprojections :: SListI xs => NP (Projection f xs) xs
#

Compute all projections from an n-ary product.

Each element of the resulting product contains one of the projections.

Application

newtypenewtype (-.->) (f :: k -> Type) (g :: k -> Type) (a :: k)
#

Lifted functions.

Constructors

Instances4Generic, HasDatatypeInfo, Code, DatatypeInfoOf
  • Generic ((-.->) f g a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • HasDatatypeInfo ((-.->) f g a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type Code ((-.->) f g a) = '['[f a -> g a]]Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type DatatypeInfoOf ((-.->) f g a) = 'Newtype "Data.SOP.Classes" "-.->" ('Record "Fn" '['FieldInfo "apFn"])Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
valuefn :: (f a -> f' a) -> (-.->) f f' a
#

Construct a lifted function.

Same as Fn. Only available for uniformity with the higher-arity versions.

valuefn_2 :: (f a -> f' a -> f'' a) -> (-.->) f (f' -.-> f'') a
#

Construct a binary lifted function.

valuefn_3
  1. :: f a -> f' a -> f'' a -> f''' a
  2. -> (-.->) f (f' -.-> (f'' -.-> f''')) a
#

Construct a ternary lifted function.

valuefn_4
  1. :: f a -> f' a -> f'' a -> f''' a -> f'''' a
  2. -> (-.->) f (f' -.-> (f'' -.-> (f''' -.-> f''''))) a
#

Construct a quarternary lifted function.

familytype family Prod (h :: (k -> Type) -> l -> Type) :: (k -> Type) -> l -> Type
#

Maps a structure containing sums to the corresponding product structure.

Instances4Prod
  • type Prod NP = NPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type Prod POP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type Prod NS = NPDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type Prod SOP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NS
classclass (Prod (Prod h) ~ Prod h, HPure (Prod h)) => HAp (h :: (k -> Type) -> l -> Type) where
#

A generalization of <*>.

Methods

  • hap :: Prod h (f -.-> g) xs -> h f xs -> h g xs

    Corresponds to <*>.

    For products (NP) as well as products of products (POP), the correspondence is rather direct. We combine a structure containing (lifted) functions and a compatible structure containing corresponding arguments into a compatible structure containing results.

    The same combinator can also be used to combine a product structure of functions with a sum structure of arguments, which then results in another sum structure of results. The sum structure determines which part of the product structure will be used.

    Instances:

    hap, ap_NP  :: NP  (f -.-> g) xs  -> NP  f xs  -> NP  g xs
    hap, ap_NS  :: Data.SOP.NS.NP  (f -.-> g) xs  -> NS  f xs  -> NS  g xs
    hap, ap_POP :: POP (f -.-> g) xss -> POP f xss -> POP g xss
    hap, ap_SOP :: Data.SOP.NS.POP (f -.-> g) xss -> SOP f xss -> SOP g xss
    
Instances4HAp
  • HAp NPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HAp POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HAp NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HAp SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS

Lifting / mapping

valuehliftA
  1. :: (SListIN (Prod h) xs, HAp h)
  2. => forall (a :: k). f a -> f' a
  3. -> h f xs
  4. -> h f' xs
#

A generalized form of liftA, which in turn is a generalized map.

Takes a lifted function and applies it to every element of a structure while preserving its shape.

Specification:

hliftA f xs = hpure (fn f) ` hap ` xs

Instances:

hliftA, liftA_NP  :: Data.SOP.Sing.SListI  xs  => (forall a. f a -> f' a) -> NP  f xs  -> NP  f' xs
hliftA, liftA_NS  :: Data.SOP.Sing.SListI  xs  => (forall a. f a -> f' a) -> NS  f xs  -> NS  f' xs
hliftA, liftA_POP :: SListI2 xss => (forall a. f a -> f' a) -> POP f xss -> POP f' xss
hliftA, liftA_SOP :: SListI2 xss => (forall a. f a -> f' a) -> SOP f xss -> SOP f' xss
valuehliftA2
  1. :: (SListIN (Prod h) xs, HAp h, HAp (Prod h))
  2. => forall (a :: k). f a -> f' a -> f'' a
  3. -> Prod h f xs
  4. -> h f' xs
  5. -> h f'' xs
#

A generalized form of liftA2, which in turn is a generalized zipWith.

Takes a lifted binary function and uses it to combine two structures of equal shape into a single structure.

It either takes two product structures to a product structure, or one product and one sum structure to a sum structure.

Specification:

hliftA2 f xs ys = hpure (fn_2 f) ` hap ` xs ` hap ` ys

Instances:

hliftA2, liftA2_NP  :: Data.SOP.Sing.SListI  xs  => (forall a. f a -> f' a -> f'' a) -> NP  f xs  -> NP  f' xs  -> NP  f'' xs
hliftA2, liftA2_NS  :: Data.SOP.Sing.SListI  xs  => (forall a. f a -> f' a -> f'' a) -> NP  f xs  -> NS  f' xs  -> NS  f'' xs
hliftA2, liftA2_POP :: SListI2 xss => (forall a. f a -> f' a -> f'' a) -> POP f xss -> POP f' xss -> POP f'' xss
hliftA2, liftA2_SOP :: SListI2 xss => (forall a. f a -> f' a -> f'' a) -> POP f xss -> SOP f' xss -> SOP f'' xss
valuehliftA3
  1. :: (SListIN (Prod h) xs, HAp h, HAp (Prod h))
  2. => forall (a :: k). f a -> f' a -> f'' a -> f''' a
  3. -> Prod h f xs
  4. -> Prod h f' xs
  5. -> h f'' xs
  6. -> h f''' xs
#

A generalized form of liftA3, which in turn is a generalized zipWith3.

Takes a lifted ternary function and uses it to combine three structures of equal shape into a single structure.

It either takes three product structures to a product structure, or two product structures and one sum structure to a sum structure.

Specification:

hliftA3 f xs ys zs = hpure (fn_3 f) ` hap ` xs ` hap ` ys ` hap ` zs

Instances:

hliftA3, liftA3_NP  :: Data.SOP.Sing.SListI  xs  => (forall a. f a -> f' a -> f'' a -> f''' a) -> NP  f xs  -> NP  f' xs  -> NP  f'' xs  -> NP  f''' xs
hliftA3, Data.SOP.NS.liftA3_NS  :: Data.SOP.Sing.SListI  xs  => (forall a. f a -> f' a -> f'' a -> f''' a) -> NP  f xs  -> NP  f' xs  -> NS  f'' xs  -> NS  f''' xs
hliftA3, liftA3_POP :: SListI2 xss => (forall a. f a -> f' a -> f'' a -> f''' a) -> POP f xss -> POP f' xss -> POP f'' xss -> POP f''' xs
hliftA3, Data.SOP.NS.liftA3_SOP :: SListI2 xss => (forall a. f a -> f' a -> f'' a -> f''' a) -> POP f xss -> POP f' xss -> SOP f'' xss -> Data.SOP.NP.SOP f''' xs
valuehcliftA
  1. :: (AllN (Prod h) c xs, HAp h)
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> f' a
  4. -> h f xs
  5. -> h f' xs
#

Variant of hliftA that takes a constrained function.

Specification:

hcliftA p f xs = hcpure p (fn f) ` hap ` xs
valuehcmap
  1. :: (AllN (Prod h) c xs, HAp h)
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> f' a
  4. -> h f xs
  5. -> h f' xs
#

Another name for hcliftA.

Constructing sums

typetype Injection (f :: k -> Type) (xs :: [k]) = f -.-> K (NS f xs)
#

The type of injections into an n-ary sum.

If you expand the type synonyms and newtypes involved, you get

Injection f xs a = (f -.-> K (NS f xs)) a ~= f a -> K (NS f xs) a ~= f a -> NS f xs

If we pick a to be an element of xs, this indeed corresponds to an injection into the sum.

valueinjections :: SListI xs => NP (Injection f xs) xs
#

Compute all injections into an n-ary sum.

Each element of the resulting product contains one of the injections.

valueshift :: Injection f xs a2 -> Injection f (x ': xs) a2
#

Deprecated. Use shiftInjection instead.

Shift an injection.

Given an injection, return an injection into a sum that is one component larger.

valueshiftInjection :: Injection f xs a2 -> Injection f (x ': xs) a2
#

Shift an injection.

Given an injection, return an injection into a sum that is one component larger.

familytype family UnProd (h :: (k -> Type) -> l -> Type) :: (k -> Type) -> l -> Type
#

Maps a structure containing products to the corresponding sum structure.

Instances2UnProd
  • type UnProd NP = NSDefined in sop-core-0.5.0.2 · Data.SOP.NS · orphan
  • type UnProd POP = SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS · orphan
classclass UnProd (Prod h) ~ h => HApInjs (h :: (k -> Type) -> l -> Type) where
#

A class for applying all injections corresponding to a sum-like structure to a table containing suitable arguments.

Methods

  • hapInjs :: SListIN h xs => Prod h f xs -> [h f xs]

    For a given table (product-like structure), produce a list where each element corresponds to the application of an injection function into the corresponding sum-like structure.

    Instances:

    hapInjs, apInjs_NP  :: Data.SOP.Sing.SListI  xs  => NP  f xs -> [NS  f xs ]
    hapInjs, Data.SOP.NS.apInjs_SOP :: SListI2 xss => POP f xs -> [SOP f xss]
    

    Examples:

    Example1 expression
    hapInjs (I 'x' :* I True :* I 2 :* Nil) :: [NS I '[Char, Bool, Int]][Z (I 'x'),S (Z (I True)),S (S (Z (I 2)))]
    Example1 expression
    hapInjs (POP ((I 'x' :* Nil) :* (I True :* I 2 :* Nil) :* Nil)) :: [SOP I '[ '[Char], '[Bool, Int]]][SOP (Z (I 'x' :* Nil)),SOP (S (Z (I True :* I 2 :* Nil)))]

    Unfortunately the type-signatures are required in GHC-7.10 and older.

Instances2HApInjs
  • HApInjs NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HApInjs SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
valueapInjs_NP :: SListI xs => NP f xs -> [NS f xs]
#

Apply injections to a product.

Given a product containing all possible choices, produce a list of sums by applying each injection to the appropriate element.

Example:

Example1 expression
apInjs_NP (I 'x' :* I True :* I 2 :* Nil)[Z (I 'x'),S (Z (I True)),S (S (Z (I 2)))]
valueapInjs_POP :: SListI xss => POP f xss -> [SOP f xss]
#

Apply injections to a product of product.

This operates on the outer product only. Given a product containing all possible choices (that are products), produce a list of sums (of products) by applying each injection to the appropriate element.

Example:

Example1 expression
apInjs_POP (POP ((I 'x' :* Nil) :* (I True :* I 2 :* Nil) :* Nil))[SOP (Z (I 'x' :* Nil)),SOP (S (Z (I True :* I 2 :* Nil)))]

Destructing sums

valueunZ :: NS f '[x] -> f x
#

Extract the payload from a unary sum.

For larger sums, this function would be partial, so it is only provided with a rather restrictive type.

Example:

Example1 expression
unZ (Z (I 'x'))I 'x'
classclass HIndex (h :: (k -> Type) -> l -> Type) where
#

A class for determining which choice in a sum-like structure a value represents.

Methods

  • hindex :: h f xs -> Int

    If h is a sum-like structure representing a choice between n different options, and x is a value of type h f xs, then hindex x returns a number between 0 and n - 1 representing the index of the choice made by x.

    Instances:

    hindex, index_NS  :: NS  f xs -> Int
    hindex, index_SOP :: SOP f xs -> Int
    

    Examples:

    Example3 expressions
    hindex (S (S (Z (I False))))2hindex (Z (K ()))0hindex (SOP (S (Z (I True :* I 'x' :* Nil))))1
Instances2HIndex
  • HIndex NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HIndex SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS
typetype Ejection (f :: k -> Type) (xs :: [k]) = K (NS f xs) -.-> (Maybe :.: f)
#

The type of ejections from an n-ary sum.

An ejection is the pattern matching function for one part of the n-ary sum.

It is the opposite of an Injection.

valueejections :: SListI xs => NP (Ejection f xs) xs
#

Compute all ejections from an n-ary sum.

Each element of the resulting product contains one of the ejections.

Dealing with All c

valuehcliftA'
  1. :: (All2 c xss, Prod h ~ NP, HAp h)
  2. => proxy c
  3. -> forall (xs :: [k]). All c xs => f xs -> f' xs
  4. -> h f xss
  5. -> h f' xss
#

Deprecated. Use hcliftA or hcmap instead.

Lift a constrained function operating on a list-indexed structure to a function on a list-of-list-indexed structure.

This is a variant of hcliftA.

Specification:

hcliftA' p f xs = hpure (fn_2 $ \ AllDictC -> f) ` hap ` allDict_NP p ` hap ` xs

Instances:

hcliftA' :: All2 c xss => proxy c -> (forall xs. All c xs => f xs -> f' xs) -> NP f xss -> NP f' xss
hcliftA' :: All2 c xss => proxy c -> (forall xs. All c xs => f xs -> f' xs) -> NS f xss -> NS f' xss

Comparison

valuecompare_NS
  1. :: r

    what to do if first is smaller

  2. -> (forall (x :: k). f x -> g x -> r)

    what to do if both are equal

  3. -> r

    what to do if first is larger

  4. -> NS f xs
  5. -> NS g xs
  6. -> r
#

Compare two sums with respect to the choice they are making.

A value that chooses the first option is considered smaller than one that chooses the second option.

If the choices are different, then either the first (if the first is smaller than the second) or the third (if the first is larger than the second) argument are called. If both choices are equal, then the second argument is called, which has access to the elements contained in the sums.

valueccompare_NS
  1. :: All c xs
  2. => proxy c
  3. -> r

    what to do if first is smaller

  4. -> (forall (x :: k). c x => f x -> g x -> r)

    what to do if both are equal

  5. -> r

    what to do if first is larger

  6. -> NS f xs
  7. -> NS g xs
  8. -> r
#

Constrained version of compare_NS.

valuecompare_SOP
  1. :: r

    what to do if first is smaller

  2. -> (forall (xs :: [k]). NP f xs -> NP g xs -> r)

    what to do if both are equal

  3. -> r

    what to do if first is larger

  4. -> SOP f xss
  5. -> SOP g xss
  6. -> r
#

Compare two sums of products with respect to the choice in the sum they are making.

Only the sum structure is used for comparison. This is a small wrapper around ccompare_NS for a common special case.

valueccompare_SOP
  1. :: All2 c xss
  2. => proxy c
  3. -> r

    what to do if first is smaller

  4. -> (forall (xs :: [k]). All c xs => NP f xs -> NP g xs -> r)

    what to do if both are equal

  5. -> r

    what to do if first is larger

  6. -> SOP f xss
  7. -> SOP g xss
  8. -> r
#

Constrained version of compare_SOP.

Collapsing

familytype family CollapseTo (h :: (k -> Type) -> l -> Type) x
#

Maps products to lists, and sums to identities.

Instances4CollapseTo
  • type CollapseTo NP a = [a]Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • type CollapseTo POP a = [[a]]Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • type CollapseTo NS a = aDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type CollapseTo SOP a = [a]Defined in sop-core-0.5.0.2 · Data.SOP.NS
classclass HCollapse (h :: (k -> Type) -> l -> Type) where
#

A class for collapsing a heterogeneous structure into a homogeneous one.

Methods

  • hcollapse :: SListIN h xs => h (K a) xs -> CollapseTo h a

    Collapse a heterogeneous structure with homogeneous elements into a homogeneous structure.

    If a heterogeneous structure is instantiated to the constant functor K, then it is in fact homogeneous. This function maps such a value to a simpler Haskell datatype reflecting that. An Data.SOP.NS (K a) contains a single a, and an Data.SOP.NP (K a) contains a list of as.

    Instances:

    hcollapse, collapse_NP  :: NP  (K a) xs  ->  [a]
    hcollapse, collapse_NS  :: NS  (K a) xs  ->   a
    hcollapse, collapse_POP :: POP (K a) xss -> [[a]]
    hcollapse, collapse_SOP :: Data.SOP.NP.SOP (K a) xss ->  [a]
    
Instances4HCollapse

Folding and sequencing

classclass HTraverse_ (h :: (k -> Type) -> l -> Type) where
#

A generalization of traverse_ or foldMap.

Methods

Instances4HTraverse_
classclass HAp h => HSequence (h :: (k -> Type) -> l -> Type) where
#

A generalization of sequenceA.

Methods

Instances4HSequence

Expanding sums to products

classclass HExpand (h :: (k -> Type) -> l -> Type) where
#

A class for expanding sum structures into corresponding product structures, filling in the slots not targeted by the sum with default values.

Methods

  • hexpand :: SListIN (Prod h) xs => (forall (x :: k). f x) -> h f xs -> Prod h f xs

    Expand a given sum structure into a corresponding product structure by placing the value contained in the sum into the corresponding position in the product, and using the given default value for all other positions.

    Instances:

    hexpand, expand_NS  :: Data.SOP.Sing.SListI xs   => (forall x . f x) -> NS  f xs  -> Data.SOP.NS.NP  f xs
    hexpand, expand_SOP :: SListI2 xss => (forall x . f x) -> SOP f xss -> POP f xss
    

    Examples:

    Example2 expressions
    hexpand Nothing (S (Z (Just 3))) :: NP Maybe '[Char, Int, Bool]Nothing :* Just 3 :* Nothing :* Nilhexpand [] (SOP (S (Z ([1,2] :* "xyz" :* Nil)))) :: POP [] '[ '[Bool], '[Int, Char] ]POP (([] :* Nil) :* ([1,2] :* "xyz" :* Nil) :* Nil)
  • hcexpand :: AllN (Prod h) c xs => proxy c -> (forall (x :: k). c x => f x) -> h f xs -> Prod h f xs

    Variant of hexpand that allows passing a constrained default.

    Instances:

    hcexpand, cexpand_NS  :: All  c xs  => proxy c -> (forall x . c x => f x) -> NS  f xs  -> NP  f xs
    hcexpand, cexpand_SOP :: All2 c xss => proxy c -> (forall x . c x => f x) -> SOP f xss -> POP f xss
    

    Examples:

    Example2 expressions
    hcexpand (Proxy :: Proxy Bounded) (I minBound) (S (Z (I 20))) :: NP I '[Bool, Int, Ordering]I False :* I 20 :* I LT :* Nilhcexpand (Proxy :: Proxy Num) (I 0) (SOP (S (Z (I 1 :* I 2 :* Nil)))) :: POP I '[ '[Double], '[Int, Int] ]POP ((I 0.0 :* Nil) :* (I 1 :* I 2 :* Nil) :* Nil)
Instances2HExpand
  • HExpand NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HExpand SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS

Transformation of index lists and coercions

classclass (Same h1 ~ h2, Same h2 ~ h1) => HTrans (h1 :: (k1 -> Type) -> l1 -> Type) (h2 :: (k2 -> Type) -> l2 -> Type) where
#

A class for transforming structures into related structures with a different index list, as long as the index lists have the same shape and the elements and interpretation functions are suitably related.

Methods

  • htrans :: AllZipN (Prod h1) c xs ys => proxy c -> (forall (x :: k1) (y :: k2). c x y => f x -> g y) -> h1 f xs -> h2 g ys

    Transform a structure into a related structure given a conversion function for the elements.

  • hcoerce :: AllZipN (Prod h1) (LiftedCoercible f g) xs ys => h1 f xs -> h2 g ys

    Safely coerce a structure into a representationally equal structure.

    This is a special case of htrans, but can be implemented more efficiently; for example in terms of unsafeCoerce.

    Examples:

    Example2 expressions
    hcoerce (I (Just LT) :* I (Just 'x') :* I (Just True) :* Nil) :: NP Maybe '[Ordering, Char, Bool]Just LT :* Just 'x' :* Just True :* Nilhcoerce (SOP (Z (K True :* K False :* Nil))) :: SOP I '[ '[Bool, Bool], '[Bool] ]SOP (Z (I True :* I False :* Nil))
Instances4HTrans
  • HTrans NP NPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HTrans POP POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HTrans NS NSDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • HTrans SOP SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS

Partial operations

valuefromList :: SListI xs => [a] -> Maybe (NP (K a) xs)
#

Construct a homogeneous n-ary product from a normal Haskell list.

Returns Nothing if the length of the list does not exactly match the expected size of the product.

Utilities

0 declarations

Basic functors

newtypenewtype K a (b :: k)
#

The constant type functor.

Like Constant, but kind-polymorphic in its second argument and with a shorter name.

Constructors

Instances27Eq2, Ord2, Read2, Show2, NFData2, Functor, …
  • Eq2 KDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Ord2 KDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Read2 KDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Show2 KDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • NFData2 KDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Functor (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Monoid a => Applicative (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Foldable (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Traversable (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Eq a => Eq1 (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Ord a => Ord1 (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Read a => Read1 (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Show a => Show1 (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • NFData a => NFData1 (K a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Eq a => Eq (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Ord a => Ord (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Read a => Read (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Show a => Show (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Generic (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Semigroup a => Semigroup (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Monoid a => Monoid (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • NFData a => NFData (K a b)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Generic (K a b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • HasDatatypeInfo (K a b)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type Rep (K a b) = D1 ('MetaData "K" "Data.SOP.BasicFunctors" "sop-core-0.5.0.2-H2UXoHYAvEiC36SsvaWSBU" 'True) (C1 ('MetaCons "K" 'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • type Code (K a b) = '['[a]]Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type DatatypeInfoOf (K a b) = 'Newtype "Data.SOP.BasicFunctors" "K" ('Constructor "K")Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
valueunK :: K a b -> a
#

Extract the contents of a K value.

newtypenewtype I a
#

The identity type functor.

Like Identity, but with a shorter name.

Constructors

Instances23Monad, Functor, Applicative, Foldable, Traversable, Eq1, …
  • Monad IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Functor IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Applicative IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Foldable IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Traversable IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Eq1 IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Ord1 IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Read1 IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Show1 IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • NFData1 IDefined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Eq a => Eq (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Ord a => Ord (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Read a => Read (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Show a => Show (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Generic (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Semigroup a => Semigroup (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Monoid a => Monoid (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • NFData a => NFData (I a)Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • Generic (I a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • HasDatatypeInfo (I a)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type Rep (I a) = D1 ('MetaData "I" "Data.SOP.BasicFunctors" "sop-core-0.5.0.2-H2UXoHYAvEiC36SsvaWSBU" 'True) (C1 ('MetaCons "I" 'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))Defined in sop-core-0.5.0.2 · Data.SOP.BasicFunctors
  • type Code (I a) = '['[a]]Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type DatatypeInfoOf (I a) = 'Newtype "Data.SOP.BasicFunctors" "I" ('Constructor "I")Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
valueunI :: I a -> a
#

Extract the contents of an I value.

newtypenewtype (:.:) (f :: l -> Type) (g :: k -> l) (p :: k)
#

Composition of functors.

Like Compose, but kind-polymorphic and with a shorter name.

Constructors

Instances22Functor, Applicative, Foldable, Traversable, Eq1, Ord1, …
valueunComp :: (:.:) f g p -> f (g p)
#

Extract the contents of a Comp value.

Mapping functions

valuemapII :: (a -> b) -> I a -> I b
#

Lift the given function.

valuemapIK :: (a -> b) -> I a -> K b c
#

Lift the given function.

valuemapKI :: (a -> b) -> K a c -> I b
#

Lift the given function.

valuemapKK :: (a -> b) -> K a c -> K b d
#

Lift the given function.

valuemapIII :: (a -> b -> c) -> I a -> I b -> I c
#

Lift the given function.

valuemapIIK :: (a -> b -> c) -> I a -> I b -> K c d
#

Lift the given function.

valuemapIKI :: (a -> b -> c) -> I a -> K b d -> I c
#

Lift the given function.

valuemapIKK :: (a -> b -> c) -> I a -> K b d -> K c e
#

Lift the given function.

valuemapKII :: (a -> b -> c) -> K a d -> I b -> I c
#

Lift the given function.

valuemapKIK :: (a -> b -> c) -> K a d -> I b -> K c e
#

Lift the given function.

valuemapKKI :: (a -> b -> c) -> K a d -> K b e -> I c
#

Lift the given function.

valuemapKKK :: (a -> b -> c) -> K a d -> K b e -> K c f
#

Lift the given function.

Mapping constraints

classclass (AllF c xs, SListI xs) => All (c :: k -> Constraint) (xs :: [k]) where
#

Require a constraint for every element of a list.

If you have a datatype that is indexed over a type-level list, then you can use All to indicate that all elements of that type-level list must satisfy a given constraint.

Example: The constraint

All Eq '[ Int, Bool, Char ]

is equivalent to the constraint

(Eq Int, Eq Bool, Eq Char)

Example: A type signature such as

f :: All Eq xs => NP I xs -> ...

means that f can assume that all elements of the n-ary product satisfy Eq.

Note on superclasses: ghc cannot deduce superclasses from All constraints. You might expect the following to compile

class (Eq a) => MyClass a

foo :: (All Eq xs) => NP f xs -> z
foo = [..]

bar :: (All MyClass xs) => NP f xs -> x
bar = foo

but it will fail with an error saying that it was unable to deduce the class constraint AllF Eq xs (or similar) in the definition of bar. In cases like this you can use Dict from Data.SOP.Dict to prove conversions between constraints. See this answer on SO for more details.

Instances2All
  • All c '[]Defined in sop-core-0.5.0.2 · Data.SOP.Constraint
  • (c x, All c xs) => All c (x ': xs)Defined in sop-core-0.5.0.2 · Data.SOP.Constraint
typetype All2 (c :: k -> Constraint) = All (All c)
#

Require a constraint for every element of a list of lists.

If you have a datatype that is indexed over a type-level list of lists, then you can use All2 to indicate that all elements of the inner lists must satisfy a given constraint.

Example: The constraint

All2 Eq '[ '[ Int ], '[ Bool, Char ] ]

is equivalent to the constraint

(Eq Int, Eq Bool, Eq Char)

Example: A type signature such as

f :: All2 Eq xss => SOP I xs -> ...

means that f can assume that all elements of the sum of product satisfy Eq.

Since 0.4.0.0, this is merely a synonym for 'All (All c)'.

methodcpara_SList
  1. :: proxy c
  2. -> r '[]
  3. -> forall (y :: k) (ys :: [k]). (c y, All c ys) => r ys -> r (y ': ys)
  4. -> r xs
#

Constrained paramorphism for a type-level list.

The advantage of writing functions in terms of cpara_SList is that they are then typically not recursive, and can be unfolded statically if the type-level list is statically known.

valueccase_SList
  1. :: All c xs
  2. => proxy c
  3. -> r '[]
  4. -> forall (y :: a) (ys :: [a]). (c y, All c ys) => r (y ': ys)
  5. -> r xs
#

Constrained case distinction on a type-level list.

classclass (SListI xs, SListI ys, SameShapeAs xs ys, SameShapeAs ys xs, AllZipF c xs ys) => AllZip (c :: a -> b -> Constraint) (xs :: [a]) (ys :: [b])
#

Require a constraint pointwise for every pair of elements from two lists.

Example: The constraint

AllZip (~) '[ Int, Bool, Char ] '[ a, b, c ]

is equivalent to the constraint

(Int ~ a, Bool ~ b, Char ~ c)
Instances1AllZip
familytype family AllN (h :: (k -> Type) -> l -> Type) (c :: k -> Constraint) :: l -> Constraint
#

A generalization of All and All2.

The family AllN expands to All or All2 depending on whether the argument is indexed by a list or a list of lists.

Instances4AllN
  • type AllN NP c = All cDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type AllN POP c = All2 cDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type AllN NS c = All cDefined in sop-core-0.5.0.2 · Data.SOP.NS
  • type AllN SOP c = All2 cDefined in sop-core-0.5.0.2 · Data.SOP.NS

Other constraints

classclass f (g x) => Compose (f :: k -> Constraint) (g :: k1 -> k) (x :: k1)
#

Composition of constraints.

Note that the result of the composition must be a constraint, and therefore, in Compose f g, the kind of f is k -> Constraint. The kind of g, however, is l -> k and can thus be a normal type constructor.

A typical use case is in connection with All on an Data.SOP.NP or an Data.SOP.NS. For example, in order to denote that all elements on an Data.SOP.NP f xs satisfy Show, we can say All (Compose Show f) xs.

Instances1Compose
  • f (g x) => Compose f g xDefined in sop-core-0.5.0.2 · Data.SOP.Constraint
classclass (f x, g x) => And (f :: k -> Constraint) (g :: k -> Constraint) (x :: k)
#

Pairing of constraints.

Instances1And
  • (f x, g x) => And f g xDefined in sop-core-0.5.0.2 · Data.SOP.Constraint
classclass Top (x :: k)
#

A constraint that can always be satisfied.

Instances1Top
  • Top xDefined in sop-core-0.5.0.2 · Data.SOP.Constraint
familytype family SameShapeAs (xs :: [a]) (ys :: [b]) :: Constraint where
#

Type family that forces a type-level list to be of the same shape as the given type-level list.

Since 0.5.0.0, this only tests the top-level structure of the list, and is intended to be used in conjunction with a separate construct (such as the AllZip, AllZipF combination to tie the recursive knot). The reason is that making SameShapeAs directly recursive leads to quadratic compile times.

The main use of this constraint is to help type inference to learn something about otherwise unknown type-level lists.

Equations

Singletons

datadata SList (a :: [k]) where
#

Explicit singleton list.

A singleton list can be used to reveal the structure of a type-level list argument that the function is quantified over. For every type-level list xs, there is one non-bottom value of type SList xs.

Note that these singleton lists are polymorphic in the list elements; we do not require a singleton representation for them.

Constructors

Instances3Eq, Ord, Show
  • Eq (SList xs)Defined in sop-core-0.5.0.2 · Data.SOP.Sing
  • Ord (SList xs)Defined in sop-core-0.5.0.2 · Data.SOP.Sing
  • Show (SList xs)Defined in sop-core-0.5.0.2 · Data.SOP.Sing
typetype SListI = All Top
#

Implicit singleton list.

A singleton list can be used to reveal the structure of a type-level list argument that the function is quantified over.

Since 0.4.0.0, this is now defined in terms of All. A singleton list provides a witness for a type-level list where the elements need not satisfy any additional constraints.

typetype SListI2 = All SListI
#

Require a singleton for every inner list in a list of lists.

valuesList :: SListI xs => SList xs
#

Get hold of an explicit singleton (that one can then pattern match on) for a type-level list

valuepara_SList
  1. :: SListI xs
  2. => r '[]
  3. -> forall (y :: a) (ys :: [a]). SListI ys => r ys -> r (y ': ys)
  4. -> r xs
#

Paramorphism for a type-level list.

valuecase_SList
  1. :: SListI xs
  2. => r '[]
  3. -> forall (y :: a) (ys :: [a]). SListI ys => r (y ': ys)
  4. -> r xs
#

Case distinction on a type-level list.

Shape of type-level lists

Re-exports

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
Instances33Generic1, Monad, Functor, Applicative, Foldable, Traversable, …
  • Generic1 ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Monad ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Functor ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Applicative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Foldable ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Foldable
  • Traversable ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Traversable
  • Alternative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • MonadPlus ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • MonadZip ProxyDefined in base-4.20.2.0 · Control.Monad.Zip
  • Eq1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Contravariant ProxyDefined in base-4.20.2.0 · Data.Functor.Contravariant
  • NFData1 ProxyDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • Bounded (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Enum (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Eq (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Data t => Data (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Read (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Show (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Ix (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Generic (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Monoid (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • NFData (Proxy a)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Generic (Proxy t)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • HasDatatypeInfo (Proxy t)Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • 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.1003.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.1003.0 · GHC.Internal.Generics
  • type Code (Proxy t) = '['[]]Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan
  • type DatatypeInfoOf (Proxy t) = 'ADT "GHC.Internal.Data.Proxy" "Proxy" '['Constructor "Proxy"] '['[]]Defined in generics-sop-0.5.1.4 · Generics.SOP.Instances · orphan