microlens-pro-0.1.0: Prisms and isomorphisms for microlens
Copyright(C) 2014-2016 Eric Mertens Edward Kmett; 2018 Monadfix
LicenseBSD-style (see the file LICENSE)
Safe HaskellTrustworthy
LanguageHaskell2010

Lens.Micro.Pro.TH

Description

Template Haskell functions to automatically define prisms.

Synopsis

Documentation

makePrisms Source #

Arguments

:: Name

Type constructor name

-> DecsQ 

Generate a Prism for each constructor of a data type. Isos generated when possible. Reviews are created for constructors with existentially quantified constructors and GADTs.

e.g.

data FooBarBaz a
  = Foo Int
  | Bar a
  | Baz Int Char
makePrisms ''FooBarBaz

will create

_Foo :: Prism' (FooBarBaz a) Int
_Bar :: Prism (FooBarBaz a) (FooBarBaz b) a b
_Baz :: Prism' (FooBarBaz a) (Int, Char)

makeClassyPrisms Source #

Arguments

:: Name

Type constructor name

-> DecsQ 

Generate a Prism for each constructor of a data type and combine them into a single class. No Isos are created. Reviews are created for constructors with existentially quantified constructors and GADTs.

e.g.

data FooBarBaz a
  = Foo Int
  | Bar a
  | Baz Int Char
makeClassyPrisms ''FooBarBaz

will create

class AsFooBarBaz s a | s -> a where
  _FooBarBaz :: Prism' s (FooBarBaz a)
  _Foo :: Prism' s Int
  _Bar :: Prism' s a
  _Baz :: Prism' s (Int,Char)

  _Foo = _FooBarBaz . _Foo
  _Bar = _FooBarBaz . _Bar
  _Baz = _FooBarBaz . _Baz

instance AsFooBarBaz (FooBarBaz a) a

Generate an As class of prisms. Names are selected by prefixing the constructor name with an underscore. Constructors with multiple fields will construct Prisms to tuples of those fields.

In the event that the name of a data type is also the name of one of its constructors, the name of the Prism generated for the data type will be prefixed with an extra _ (if the data type name is prefix) or . (if the name is infix) to disambiguate it from the Prism for the corresponding constructor. For example, this code:

data Quux = Quux Int | Fred Bool
makeClassyPrisms ''Quux

will create:

class AsQuux s where
  __Quux :: Prism' s Quux -- Data type prism
  _Quux :: Prism' s Int   -- Constructor prism
  _Fred :: Prism' s Bool

  _Quux = __Quux . _Quux
  _Fred = __Quux . _Fred

instance AsQuux Quux