wiki:GhcApiAstTraversals

Version 4 (modified by claus, 6 years ago) (diff)

add performance issues/possible improvements

Support for Generic Ghc Api Ast Traversals

This Wiki page is a spin off of GhcApiStatus, focussing on information related to generic traversals (transformations/queries) over the Ghc Ast types.

Data.Traversable

David Waern has been working on deriving Data.Traversable for the Ast types, to get mapM for use in Haddock 2. He points out that, by default, SYB-style traversals do not seem to support type-changing maps.

Data.Generics

Since Ghc does support (standalone) deriving of Data and Typeable, it seems natural to want those classes instantiated for the Ghc Api types. In case that would lead to blowup of standard releases, it would be even better if those instances could be provided as an add-on to a release.

  • until recently, standalone deriving of Data/Typeable was broken (#2378, fixed in HEAD, 07/01/08)
  • standalone deriving of Typeable fails if data constructors are not in scope - this looks like a bug (#2433)
  • standalone deriving of Data fails if data constructors are not in scope - this means that Data instances are in conflict with the intended data abstraction for some Ghc Ast types!
  • using the internal CPP hackery of Data.Generics for Typeable, standalone deriving of Data for non-abstract types, and the traditional manual dummy Data instances for abstract types, it seems we can get initial Data.Generics support over the Ghc Ast types!
  • since I want to be able to identify the abstract types, I've replaced the traditional toConstr _ = error "toConstr":
    abstractConstr   n = mkConstr (abstractDataType n) ("{abstract:"++n++"}") [] Prefix
    abstractDataType n = mkDataType n [abstractConstr n]
    
    -- TypeableINSTANCE_TYPEABLE0(SrcSpan,srcSpanTc,"SrcSpan")
    instance Data SrcSpan where
      toConstr _   = abstractConstr "SrcSpan"
      gunfold _ _  = error "gunfold"
      dataTypeOf _ = mkNorepType "SrcSpan"
    
    TODO Is that ok, or is it going to cause problems?
  • putting all those instances in a single file seems to trigger a memory bug in the compiler (#2438), but splitting them into two files appears to avoid that. See the attached Instances and Instances0
  • for starters, here's a Data-based show that shows the constructors/abstract types instead of pretty-printing them:
    -- generic Data-based show, with special cases for GHC Ast types,
    -- showing abstract types abstractly and avoiding known potholes
    showData :: Data a => Stage -> Int -> a -> String
    showData stage n = 
      generic `ext1Q` list `extQ` string `extQ` fastString `extQ` srcSpan 
              `extQ` name `extQ` occName `extQ` moduleName `extQ` var `extQ` dataCon
              `extQ` bagName `extQ` bagRdrName `extQ` bagVar `extQ` nameSet
              `extQ` postTcType `extQ` fixity
      where generic :: Data a => a -> String
            generic t = indent n ++ "(" ++ showConstr (toConstr t)
                     ++ space (concat (intersperse " " (gmapQ (showData stage (n+1)) t))) ++ ")"
            space "" = ""
            space s  = ' ':s
            indent n = "\n" ++ replicate n ' ' 
            string     = show :: String -> String
            fastString = ("{FastString: "++) . (++"}") . show :: FastString -> String
            list l     = indent n ++ "[" 
                                  ++ concat (intersperse "," (map (showData stage (n+1)) l)) ++ "]"
    
            name       = ("{Name: "++) . (++"}") . showSDoc . ppr :: Name -> String
            occName    = ("{OccName: "++) . (++"}") .  OccName.occNameString 
            moduleName = ("{ModuleName: "++) . (++"}") . showSDoc . ppr :: ModuleName -> String
            srcSpan    = ("{"++) . (++"}") . showSDoc . ppr :: SrcSpan -> String
            var        = ("{Var: "++) . (++"}") . showSDoc . ppr :: Var -> String
            dataCon    = ("{DataCon: "++) . (++"}") . showSDoc . ppr :: DataCon -> String
    
            bagRdrName:: Bag (Located (HsBind RdrName)) -> String
            bagRdrName = ("{Bag(Located (HsBind RdrName)): "++) . (++"}") . list . bagToList 
            bagName   :: Bag (Located (HsBind Name)) -> String
            bagName    = ("{Bag(Located (HsBind Name)): "++) . (++"}") . list . bagToList 
            bagVar    :: Bag (Located (HsBind Var)) -> String
            bagVar     = ("{Bag(Located (HsBind Var)): "++) . (++"}") . list . bagToList 
    
            nameSet | stage `elem` [Parser,TypeChecker] 
                    = const ("{!NameSet placeholder here!}") :: NameSet -> String
                    | otherwise     
                    = ("{NameSet: "++) . (++"}") . list . nameSetToList 
    
            postTcType | stage<TypeChecker = const "{!type placeholder here?!}" :: PostTcType -> String
                       | otherwise     = showSDoc . ppr :: Type -> String
    
            fixity | stage<Renamer = const "{!fixity placeholder here?!}" :: GHC.Fixity -> String
                   | otherwise     = ("{Fixity: "++) . (++"}") . showSDoc . ppr :: GHC.Fixity -> String
    
    For example usage, see the attached APISybTesting: it parses a TestModule, prettyprints and shows, does an identity transform, and an example query (extract classes and family declarations).
  • it seems as if the issue of type-changing traversals can be addressed within SYB, although those dummy instances cause trouble here.
  • gMap in SYB1, a technique based on gfoldl, gunfoldl, and some Dynamic types in the middle. Avoids unsafeCoerce, and uses the fact that the dummy instances are slightly more honest about gunfoldl than about gfoldl, but gMap cannot distinguish functor parameters from equivalent types (no fmap) and its type is too general for direct use (runtime type errors instead of compile time type errors)
  • performance: Syb traversals are sometimes associated with bad performance. Unfortunately, none of the anecdotes I'm aware of really investigates these performance issues in any detail (if you have useful data to add here, please do!). So I can only say here that
    • there are various more or less obvious ways in which naive use of Syb traversal schemes can lead to very inefficient traversals
    • until proven wrong, I'll assume that all of these traps can be avoided by careful tuning of traversal schemes!-)
    • three example issues are listed in Data.Generics with GPS (using Maps to avoid getting lost in Data), which also provides a generalisation of the main Uniplate PlateData (Uniplate over Syb) optimisation, in a form that can be used to address one of the performance issues of everywhere and everything (traversal of irrelevant substructures)
    • I've been trying to address another of those three issues (too many runtime type checks), but am currently stuck because merely mentioning the necessary auxilary structures seems to disable some GHC optimisation, leading to a drastical loss of performance (as I'm not even using those structures yet, this might be a GHC bug? see #2463)

Attachments (8)

Download all attachments as: .zip