extract-cons-1.0.0.0
Safe HaskellNone
LanguageHaskell2010

Data.Constructor.Extract

Description

This package allows _extracting_ constructors from a given ADT.

data MathE = Add MathE MathE | ...

$(extractConstructorsOf 'MathE defaultOptions)

-- ==> The following gets generated, for each constructor
data Add = MkAdd MathE MathE

instance ExtractedConstructor Add MathE where
 fromEC (MkAdd left right) = Add left right
 toEC mathE = case mathE of
     (Add left right) -> Just (MkAdd left right)
     _ -> Nothing
Synopsis

Extract Constructors

extractConstructor :: Name -> ExtractOptions -> DecsQ Source #

Using a constructor's Name, generates a new data type with only this constructor.

extractConstructorsOf :: Name -> ExtractOptions -> DecsQ Source #

Calls extractConstructor for each constructor of the data type whone Name is passed as parameter.

Options

data ExtractOptions Source #

Constructors

MkExtractOptions 

Fields

  • newDataName :: String -> String

    Build the name of the `data` to generate using the constructor's name given as parameter

  • newConName :: String -> String

    Build the name of the constructor to generate using the original constructor's name

  • deriveClasses :: [Name]

    Add _deriving_ declaration when generating the new data type. **WARNING**: This is not guaranteed to type-check. Basic classes like Eq and Ord will work.

Conversion

class ExtractedConstructor con ty where Source #

Type classes (whose instances will be derived using TH) that allows going back and forth between the extracted constructor type (con) and the source data type (ty)

Methods

fromEC :: con -> ty Source #

toEC :: ty -> Maybe con Source #