Library for generating Haskell source files and code fragments.
Go to file
2019-07-09 13:58:46 -07:00
ghc-show-ast Initial commit. 2019-07-09 13:58:46 -07:00
src/GHC Initial commit. 2019-07-09 13:58:46 -07:00
tests Initial commit. 2019-07-09 13:58:46 -07:00
.gitignore Initial commit. 2019-07-09 13:58:46 -07:00
ChangeLog.md Initial commit. 2019-07-09 13:58:46 -07:00
check.sh Initial commit. 2019-07-09 13:58:46 -07:00
CONTRIBUTING.md Initial commit. 2019-07-09 13:58:46 -07:00
LICENSE Initial commit. 2019-07-09 13:58:46 -07:00
package.yaml Initial commit. 2019-07-09 13:58:46 -07:00
README.md Initial commit. 2019-07-09 13:58:46 -07:00
Setup.hs Initial commit. 2019-07-09 13:58:46 -07:00
stack-8.8.yaml Initial commit. 2019-07-09 13:58:46 -07:00
stack.yaml Initial commit. 2019-07-09 13:58:46 -07:00

ghc-source-gen

ghc-source-gen is a Haskell library for constructing Haskell syntax trees using the GHC API. This package is compatible with multiple versions of GHC: currently,= 8.2, 8.4, 8.6, and 8.8.

This is not an officially supported Google product.

Example

This example constructs and prints a module defining the const function:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
import GHC.SourceGen
import GHC.Paths (libdir)

constModule :: HsModule'
constModule =
    module' (Just "Const") (Just [var "const"]) []
        [ typeSig "const" $ a --> b --> a
        , funBind "const" $ matchRhs [wildP, x] x
        ]
  where
    a = var "a"
    b = var "b"
    x = var "x"

main = runGhc (Just libdir) $ putPpr constModule

Which will output:

module Const (
        const
    ) where
const :: a -> b -> a
const _ x = x

Syntax Types

GHC represents Haskell syntax trees with several parametrized datatypes; for example: HsExpr p for expressions, HsDecl p for declarations, etc. The parameter p determines which stage of compilation that data has last completed: parsing, renaming, or type-checking.

ghc-source-gen constructs values as GHC would represent them immediately after the parsing step. In ghc-8.6, that corresponds to p being GhcPs. It defines several type synonyms, such as:

type HsExpr' = HsExpr GhcPs
type HsType' = HsType GhcPs
type HsDecl' = HsDecl GhcPs
type HsModule' = HsModule GhcPs
-- etc.

GHC's datatypes generally contain location information in the form of SrcSpan values which point to their original location in a source file. ghc-source-gen constructs values at runtime, so it uses a dummy value for SrcSpan on using a dummy SrcSpan. (GHC does something similar for code written at the interactive GHCi prompt.)

ghc-source-gen aims to be a low-level wrapper around GHC's types. In particular, it does not explicitly help the user generate unique names like, for example, template-haskell's newName action. However, we may add support for that in future versions of this library.