Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9176893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:12:43+00:00 2026-06-17T17:12:43+00:00

Is it possible to generate and run TemplateHaskell generated code at runtime? Using C,

  • 0

Is it possible to generate and run TemplateHaskell generated code at runtime?

Using C, at runtime, I can:

  • create the source code of a function,
  • call out to gcc to compile it to a .so (linux) (or use llvm, etc.),
  • load the .so and
  • call the function.

Is a similar thing possible with Template Haskell?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:12:44+00:00Added an answer on June 17, 2026 at 5:12 pm

    Yes, it’s possible. The GHC API will compile Template Haskell. A proof-of-concept is available at https://github.com/JohnLato/meta-th, which, although not very sophisticated, shows one general technique that even provides a modicum of type safety. Template Haskell expressions are build using the Meta type, which can then be compiled and loaded into a usable function.

    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE TemplateHaskell #-}
    
    {-# OPTIONS_GHC -Wall #-}
    module Data.Meta.Meta (
    -- * Meta type
      Meta (..)
    
    -- * Functions
    , metaCompile
    ) where
    
    import Language.Haskell.TH
    
    import Data.Typeable as Typ
    import Control.Exception (bracket)
    
    import System.Plugins -- from plugins
    import System.IO
    import System.Directory
    
    newtype Meta a = Meta { unMeta :: ExpQ }
    
    -- | Super-dodgy for the moment, the Meta type should register the
    -- imports it needs.
    metaCompile :: forall a. Typeable a => Meta a -> IO (Either String a)
    metaCompile (Meta expr) = do
      expr' <- runQ expr
    
      -- pretty-print the TH expression as source code to be compiled at
      -- run-time
      let interpStr = pprint expr'
          typeTypeRep = Typ.typeOf (undefined :: a)
    
      let opener = do
            (tfile, h) <- openTempFile "." "fooTmpFile.hs"
            hPutStr h (unlines
                  [ "module TempMod where"
                  , "import Prelude"
                  , "import Language.Haskell.TH"
                  , "import GHC.Num"
                  , "import GHC.Base"
                  , ""
                  , "myFunc :: " ++ show typeTypeRep
                  , "myFunc = " ++ interpStr] )
            hFlush h
            hClose h
            return tfile
      bracket opener removeFile $ \tfile -> do
    
          res <- make tfile ["-O2", "-ddump-simpl"]
          let ofile = case res of
                        MakeSuccess _ fp -> fp
                        MakeFailure errs -> error $ show errs
          print $ "loading from: " ++ show ofile
          r2 <- load (ofile) [] [] "myFunc"
          print "loaded"
    
          case r2 of
            LoadFailure er -> return (Left (show er))
            LoadSuccess _ (fn :: a) -> return $ Right fn
    

    This function takes an ExpQ, and first runs it in IO to create a plain Exp. The Exp is then pretty-printed into source code, which is compiled and loaded at run-time. In practice, I’ve found that one of the more difficult obstacles is specifying the correct imports in the generated TH code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to run a script using Microsoft.SqlServer.Management.Smo? I am able to generate
with ant it is possible to run JUnit tests and generate test reports in
While it is possible to generate PowerPoint presentations automatically using Office Automation , this
Is it possible to generate subclasses at runtime or while an application is running?
I'm trying to get this code to run as fast as possible and at
I am trying to work out the best way to generate all possible permutations
We're using POV-Ray to generate some 80 images per run, which we stitch together
When I attempt to run below maven plugin using this command : com.wordpress.pomfromjarmavenplugin:generate:0.0.1-SNAPSHOT:generatepom I
I have an app that uses some code scripts to generate dll's at run-time
I'm using rails (3.0.4) and rspec-rails (2.5.0). When I run rails generate rspec:install it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.