HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-exactprint-1.10.0.0Haskell2010

Language.Haskell.GHC.ExactPrint.Transform

This module is currently under heavy development, and no promises are made about API stability. Use with care.

We welcome any feedback / contributions on this, as it is the main point of the library.

  • 3 types
  • 2 classes
  • 47 values

The Transform Monad

7 declarations

Monad type for updating the AST and managing the annotations at the same time. The W state is used to generate logging information if required.

newtypenewtype TransformT (m :: Type -> Type) a
#

Monad transformer version of Transform monad

Constructors

Instances9MonadTrans, MonadReader, MonadState, Monad, Functor, MonadFail, …

Transform monad operations

3 declarations

Managing declarations, in Transform monad

classclass Data t => HasDecls t where
#

Provide a means to get and process the immediate child declarations of a given AST element.

Methods

  • hsDecls :: t -> [LHsDecl GhcPs]

    Return the HsDecls that are directly enclosed in the given syntax phrase. They are always returned in the wrapped HsDecl form, even if orginating in local decls. This is safe, as annotations never attach to the wrapper, only to the wrapped item.

  • replaceDecls :: t -> [LHsDecl GhcPs] -> t

    Replace the directly enclosed decl list by the given decl list. As parto of replacing it will update list order annotations, and rebalance comments and other layout changes as needed.

    For example, a call on replaceDecls for a wrapped FunBind having no where clause will convert

    -- |This is a function
    foo = x -- comment1
    

    in to

    -- |This is a function
    foo = x -- comment1
      where
        nn = 2
    
Instances5HasDecls
valuemodifyValD
  1. :: SrcSpan
  2. -> Decl
  3. -> PMatch -> [Decl] -> ([Decl], Maybe t)
  4. -> (Decl, Maybe t)
#

Modify a LHsBind wrapped in a ValD. For a PatBind the declarations are extracted and returned after modification. For a FunBind the supplied SrcSpan is used to identify the specific Match to be transformed, for when there are multiple of them.

Utility, does not manage layout

New gen functions

Managing lists, Transform monad

Low level operations used in HasDecls

The GHC parser puts all comments appearing between the end of one AST item and the beginning of the next as annPriorComments for the second one. This function takes two adjacent AST items and moves any annPriorComments from the second one to the annFollowingComments of the first if they belong to it instead. This is typically required before deleting or duplicating either of the AST elements.

If we compile in haddock mode we get DocDecls, which we strip out while exact printing. Make sure we do not balance any comments on to them be stripping them out here already.

Managing lists, pure functions

Operations

1 declaration

Pure functions

9 declarations
valuedecl2Sig :: LHsDecl GhcPs -> [LSig GhcPs]
#

Pure function to convert a LSig to a LHsBind. This does nothing to any annotations that may be attached to either of the elements. It is used as a utility function in replaceDecls