ghc-source-gen

Memo

  • As far as I can tell, it’s just a way to generate a Haskell module from a syntax tree. You can then pretty-print the generated module, or you can run it through GHC during runtime.

  • GHC API を使ってる

  • 動的にコード生成が可能なので面白いかもしれない

使い方

{-# 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

リソース

Last updated