| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Language.Haskell.TypeTree
- data ReifyOpts = ReifyOpts {}
- defaultOpts :: ReifyOpts
- ttReifyOpts :: IsDatatype a => ReifyOpts -> a -> Q (Forest Leaf)
- ttReify :: IsDatatype a => a -> Q (Forest Leaf)
- ttLitOpts :: IsDatatype a => ReifyOpts -> a -> ExpQ
- ttLit :: IsDatatype a => a -> ExpQ
- ttDescribeOpts :: IsDatatype a => ReifyOpts -> a -> ExpQ
- ttDescribe :: IsDatatype a => a -> ExpQ
- ttConnCompOpts :: IsDatatype a => ReifyOpts -> a -> ExpQ
- ttConnComp :: IsDatatype a => a -> ExpQ
- data Leaf
- class IsDatatype a where
GHCi setup
>>>:set -XTemplateHaskell -XGADTs -XTypeFamilies
Usage
type-tree provides a way to build tree structures from datatypes.
Basic usage
>>>data Foo a = Foo { field1 :: a, field2 :: Either String Int }>>>putStr $(ttDescribe ''Foo)Foo :: * -> * | +- Either :: * -> * -> * | +- [] :: * -> * | | | `- ...[] :: * -> * | +- Char :: * | `- Int :: *
ttReify passes through type synonyms by default:
>>>putStr $(ttDescribe ''FilePath) -- FilePath --> String --> [Char][] :: * -> * | `- ...[] :: * -> * Char :: *
but this behavior can be disabled:
>>>putStr $(ttDescribeOpts defaultOpts { synonyms = True } ''FilePath)FilePath :: * | `- String :: * | +- [] :: * -> * | | | `- ...[] :: * -> * | `- Char :: *
Constructors
| ReifyOpts | |
Fields | |
defaultOpts :: ReifyOpts Source #
defaultOpts = ReifyOpts
{ stop = S.fromList []
, prim = False
, synonyms = False
}
Building trees
ttReifyOpts :: IsDatatype a => ReifyOpts -> a -> Q (Forest Leaf) Source #
Build a Forest of constructor names contained in the given type.
ttReify :: IsDatatype a => a -> Q (Forest Leaf) Source #
ttReifyOpts with default options.
ttLitOpts :: IsDatatype a => ReifyOpts -> a -> ExpQ Source #
Embed the produced Forest as an expression.
ttDescribeOpts :: IsDatatype a => ReifyOpts -> a -> ExpQ Source #
Produce a string representation of the forest generated by
$(ttReifyOpts opts ''SomeName). Useful for debugging purposes.
ttDescribe :: IsDatatype a => a -> ExpQ Source #
ttDescribeOpts with default options.
Graph utilities
ttConnCompOpts :: IsDatatype a => ReifyOpts -> a -> ExpQ Source #
ttConnCompOpts is useful for the usecase which I had in mind when
I originally wrote this package, namely:
Given some datatype, I need a topologically sorted list of all types contained in that datatype for which an instance of some class must be defined if I wish to define an instance for that datatype (and likewise for each subtype, etc.)
Here's an example using CondTree,
which is a useful datatype for an example, as it's both mutually recursive
and refers to other recursive types.
>>>:m +Language.Haskell.TypeTree.ExampleDatatypes>>>mapM_ print $(ttConnComp ''CondTree)AcyclicSCC ([] :: * -> *,[]) AcyclicSCC (Bool :: *,[]) AcyclicSCC (Condition :: * -> *,[Bool :: *]) AcyclicSCC (Maybe :: * -> *,[]) CyclicSCC [(CondBranch :: * -> * -> * -> *,[Condition :: * -> *,CondTree :: * -> * -> * -> *,Maybe :: * -> *]),(CondTree :: * -> * -> * -> *,[[] :: * -> *,CondBranch :: * -> * -> * -> *])]
ttConnComp :: IsDatatype a => a -> ExpQ Source #
ttConnCompOpts with default opts