Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Singletons.Dict
Synopsis
- mkTotalDictGetter :: Name -> Name -> Q [Dec]
- mkPartialDictGetter :: Name -> Name -> Q [Dec]
Documentation
Generates Dict
getters for the promoted nullary data constructors corresponding to
a singletons
-like type.
All the promoted data constructors must be instances of the given type class.
The names of the getters result from the concatenation of:
- the camel-cased name of the base type,
- the name of the type class,
- the "
Dict
" keyword, - the "A" suffix, for the contextual getter.
Example:
Given this type:
data Example = Foo | Bar | Baz
and the corresponding singletons
-like type:
data SExample (example :: Example) where SFoo :: SExample 'Foo SBar :: SExample 'Bar SBaz :: SExample 'Baz
this line:
$(mkTotalDictGetter ''SExample ''Typeable
)
generates those getters:
exampleTypeableDict :: SExample example ->Dict
(Typeable
example) exampleTypeableDict sing = case sing of SFoo ->Dict
SBar ->Dict
SBaz ->Dict
exampleTypeableDictA ::Applicative
f => SExample example -> f (Dict
(Typeable
example)) exampleTypeableDictA sing = case sing of SFoo ->pure
Dict
SBar ->pure
Dict
SBaz ->pure
Dict
Generates Dict
getters for the promoted nullary data constructors corresponding to
a singletons
-like type.
Not all the promoted data constructors must be instances of the given type class.
The name of the getters results from the concatenation of:
- the camel-cased name of the base type,
- the name of the type class,
- the "
Dict
" keyword, - the "A" suffix, for the contextual getter.
Example:
Given this type:
data Example = Foo | Bar | Baz
the corresponding singletons
-like type:
data SExample (example :: Example) where SFoo :: SExample 'Foo SBar :: SExample 'Bar SBaz :: SExample 'Baz
and this type class and instance:
class IsBar (a :: k) where instance IsBar 'Bar where
this line:
$(mkPartialDictGetter ''SExample ''IsBar)
generates those getters:
exampleIsBarDict :: SExample example ->Maybe
(Dict
(IsBar example)) exampleIsBarDict sing = case sing of SFoo ->Nothing
SBar ->Just
Dict
SBaz ->Nothing
exampleIsBarDictA ::Applicative
f => SExample example -> f (Maybe
(Dict
(IsBar example))) exampleIsBarDictA sing = case sing of SFoo ->pure
Nothing
SBar ->pure
(Just
Dict
) SBaz ->pure
Nothing