-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Graph of the subtype relation -- @package th-typegraph @version 0.14 module Language.Haskell.TH.TypeGraph.Hints -- | When a VertexHint value is associated with a Type it describes -- alterations in the type graph from the usual default. data VertexHint -- | normal case Normal :: VertexHint -- | don't create this vertex, no in or out edges Hidden :: VertexHint -- | out degree zero - don't create any out edges Sink :: VertexHint -- | replace all out edges with an edge to an alternate type Divert :: Type -> VertexHint -- | add an extra out edge to the given type Extra :: Type -> VertexHint instance Eq VertexHint instance Ord VertexHint instance Show VertexHint instance Ppr VertexHint instance Lift VertexHint instance Default VertexHint -- | Abstract operations on Maps containing graph edges. module Language.Haskell.TH.TypeGraph.Graph type GraphEdges v = Map v (Set v) -- | Remove a node and all its in- and out-edges. cutVertex :: (Eq a, Ord a) => a -> GraphEdges a -> GraphEdges a -- | Cut vertices for which the predicate returns False cutVertices :: (Eq a, Ord a) => (a -> Bool) -> GraphEdges a -> GraphEdges a -- | Cut vertices for which the predicate returns False cutVerticesM :: (Monad m, Eq a, Ord a) => (a -> m Bool) -> GraphEdges a -> m (GraphEdges a) -- | Merge a node into the nodes that are its in-edges. mergeVertex :: (Eq a, Ord a) => a -> GraphEdges a -> GraphEdges a mergeVertices :: (Eq a, Ord a) => (a -> Bool) -> GraphEdges a -> GraphEdges a mergeVerticesM :: (Monad m, Ord a) => (a -> m Bool) -> GraphEdges a -> m (GraphEdges a) partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a]) flatten :: Ord a => Set (Set a) -> Set a -- | Build a graph from the result of typeGraphEdges, each edge goes from a -- type to one of the types it contains. Thus, each edge represents a -- primitive lens, and each path in the graph is a composition of lenses. graphFromMap :: (Ord a, node ~ a, key ~ a) => GraphEdges a -> (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex) -- | The Expanded class helps keep track of which Type values -- have been fully expanded to a canonical form. This lets us use the -- Eq and Ord relationships on Type and Pred -- values when reasoning about instance context. What the -- expandType function does is use the function from -- th-desugar to replace occurrences of ConT name with -- the associated Type if name is a declared type synonym -- TySynD name _ typ. For convenience, a wrapper type E -- is provided, along with the Expanded instances E Type -- and E Pred. Now the expandType and expandPred -- functions can be used to return values of type E Type and -- E Pred respectively. -- -- Instances Expanded Type Type and Expanded Pred Pred -- are provided in Language.Haskell.TH.Context.Unsafe, for when -- less type safety is required. module Language.Haskell.TH.TypeGraph.Expand -- | This class lets us use the same expand* functions to work with -- specially marked expanded types or with the original types. class Expanded un ex | ex -> un markExpanded :: Expanded un ex => un -> ex runExpanded :: Expanded un ex => ex -> un runExpanded' :: Expanded a (E a) => E a -> a -- | Apply the th-desugar expand function to a Type and mark it as -- expanded. expandType :: (DsMonad m, Expanded Type e) => Type -> m e -- | Apply the th-desugar expand function to a Pred and mark it as -- expanded. Note that the definition of Pred changed in -- template-haskell-2.10.0.0. expandPred :: (DsMonad m, Expanded Pred e) => Pred -> m e -- | Expand a list of Type and build an expanded ClassP -- Pred. expandClassP :: (DsMonad m, Expanded Pred e) => Name -> [Type] -> m e -- | A concrete type for which Expanded instances are declared below. newtype E a E :: a -> E a instance Eq a => Eq (E a) instance Ord a => Ord (E a) instance Show a => Show (E a) instance Ppr a => Ppr (E a) instance Expanded Pred (E Pred) instance Expanded Type (E Type) -- | Degenerate instances of Expanded that must be explicitly imported if -- you want to use them. They are fine for simple uses of expandType, but -- not if you are trying to use the return value of expandType as a Map -- key. module Language.Haskell.TH.TypeGraph.Unsafe instance Expanded Pred Pred instance Expanded Type Type -- | Helper functions for dealing with record fields, type shape, type -- arity, primitive types, and pretty printing. module Language.Haskell.TH.TypeGraph.Core data FieldType FieldType :: Int -> Either StrictType VarStrictType -> FieldType fPos :: FieldType -> Int fNameAndType :: FieldType -> Either StrictType VarStrictType fName :: FieldType -> Maybe Name -- | fType' with leading foralls stripped fType :: FieldType -> Type prettyField :: FieldType -> String constructorFields :: Con -> [FieldType] -- | Given the list of constructors from a Dec, dispatch on the different -- levels of complexity of the type they represent - a wrapper is a -- single arity one constructor, an enum is several arity zero -- constructors, and so on. foldShape :: Monad m => ([(Con, [FieldType])] -> m r) -> (Con -> [FieldType] -> m r) -> ([Con] -> m r) -> (Con -> FieldType -> m r) -> [Con] -> m r constructorName :: Con -> Name -- | Compute the arity of a type - the number of type parameters that must -- be applied to it in order to obtain a concrete type. typeArity :: Quasi m => Type -> m Int unlifted :: (IsUnlifted t, Quasi m) => t -> m Bool -- | Pretty print a Ppr value on a single line with each block of -- white space (newlines, tabs, etc.) converted to a single space. pprint' :: Ppr a => a -> [Char] instance Typeable FieldType instance Eq FieldType instance Ord FieldType instance Show FieldType instance Data FieldType instance Lift (E Type) instance (Lift a, Lift b) => Lift (Map a b) instance Lift a => Lift (Set a) instance IsUnlifted Info instance IsUnlifted Type instance IsUnlifted Con instance IsUnlifted Dec module Language.Haskell.TH.TypeGraph.Vertex -- | For simple type graphs always set _field and _synonyms to Nothing. data TypeGraphVertex TypeGraphVertex :: Maybe (Name, Name, Either Int Name) -> Set Name -> E Type -> TypeGraphVertex -- | The record filed which contains this type _field :: TypeGraphVertex -> Maybe (Name, Name, Either Int Name) -- | All the type synonyms that expand to this type _syns :: TypeGraphVertex -> Set Name -- | The fully expanded type _etype :: TypeGraphVertex -> E Type field :: Lens' TypeGraphVertex (Maybe (Name, Name, Either Int Name)) syns :: Lens' TypeGraphVertex (Set Name) etype :: Lens' TypeGraphVertex (E Type) -- | Return the set of Name of a type's synonyms, plus the name (if -- any) used in its data declaration. Note that this might return the -- empty set. typeNames :: TypeGraphVertex -> Set Name instance Lift TypeGraphVertex instance Eq TypeGraphVertex instance Ord TypeGraphVertex instance Show TypeGraphVertex instance Ppr TypeGraphVertex module Language.Haskell.TH.TypeGraph.Edges type TypeGraphEdges = GraphEdges TypeGraphVertex instance Ppr TypeGraphEdges module Language.Haskell.TH.TypeGraph.Info -- | Information collected about the graph implied by the structure of one -- or more Type values. data TypeGraphInfo emptyTypeGraphInfo :: TypeGraphInfo -- | Build a TypeGraphInfo value by scanning the supplied types and hints. typeGraphInfo :: DsMonad m => [(TypeGraphVertex, VertexHint)] -> [Type] -> m TypeGraphInfo expanded :: Lens' TypeGraphInfo (Map Type (E Type)) fields :: Lens' TypeGraphInfo (Map (E Type) (Set (Name, Name, Either Int Name))) hints :: Lens' TypeGraphInfo (Map TypeGraphVertex [VertexHint]) infoMap :: Lens' TypeGraphInfo (Map Name Info) synonyms :: Lens' TypeGraphInfo (Map (E Type) (Set Name)) typeSet :: Lens' TypeGraphInfo (Set Type) withTypeGraphInfo :: DsMonad m => [(TypeGraphVertex, VertexHint)] -> [Type] -> ReaderT TypeGraphInfo m a -> m a instance Lift TypeGraphInfo instance Show TypeGraphInfo instance Eq TypeGraphInfo instance Ord TypeGraphInfo instance Ppr TypeGraphInfo module Language.Haskell.TH.TypeGraph.Monad -- | Given the discovered set of types and maps of type synonyms and -- fields, build and return the GraphEdges relation on TypeGraphVertex. -- This is not a recursive function, it stops when it reaches the field -- types. findEdges :: MonadReader TypeGraphInfo m => m (GraphEdges TypeGraphVertex) -- | Build a TypeGraphVertex from an unexpanded type. typeVertex :: MonadReader TypeGraphInfo m => E Type -> m TypeGraphVertex -- | Build a TypeGraphVertex for a field of a record. This calls -- typeVertex and then sets the _field value. fieldVertex :: MonadReader TypeGraphInfo m => E Type -> (Name, Name, Either Int Name) -> m TypeGraphVertex -- | Start with the type graph on the known types with no field -- information, and build a new graph which incorporates the information -- from the vertex hints. This means splitting the nodes according to -- record fields, because hints can refer to particular fields of a -- record. typeGraphEdges :: MonadReader TypeGraphInfo m => m TypeGraphEdges -- | Return the set of types embedded in the given type. This is just the -- nodes of the type graph. The type synonymes are expanded by the -- th-desugar package to make them suitable for use as map keys. typeGraphVertices :: (DsMonad m, MonadReader TypeGraphInfo m) => m (Set TypeGraphVertex) -- | Build a graph from the result of typeGraphEdges, each edge goes from a -- type to one of the types it contains. Thus, each edge represents a -- primitive lens, and each path in the graph is a composition of lenses. typeGraph :: (DsMonad m, MonadReader TypeGraphInfo m, node ~ TypeGraphVertex, key ~ TypeGraphVertex) => m (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex) -- | Simplify a graph by throwing away the field information in each node. -- This means the nodes only contain the fully expanded Type value (and -- any type synonyms.) simpleEdges :: TypeGraphEdges -> TypeGraphEdges simpleVertex :: TypeGraphVertex -> TypeGraphVertex -- | Find all the reachable type synonyms and return then in a Map. typeSynonymMap :: (DsMonad m, MonadReader TypeGraphInfo m) => m (Map TypeGraphVertex (Set Name)) typeSynonymMapSimple :: (DsMonad m, MonadReader TypeGraphInfo m) => m (Map (E Type) (Set Name)) module Language.Haskell.TH.TypeGraph