Questions tagged [ghc-api]
39 questions
25
votes
4 answers
Need a tutorial for using GHC to parse and typecheck Haskell
I'm working on a project for analyzing Haskell code. I decided to use GHC to parse the source and infer types rather than write my own code to do that. Right now, I'm slogging through the Haddock docs, but it's slow going. Does anyone know of a…

Adam Crume
- 15,614
- 8
- 46
- 50
24
votes
2 answers
GHC API - How to dynamically load Haskell code from a compiled module using GHC 7.2?
I have an existing Haskell function that uses the GHC API to dynamically load compiled code from a module. It is based on the code from the blog post Dynamic Compilation and Loading of Modules in Haskell.
The code works fine in GHC 7.0, but had to…

Justin Ethier
- 131,333
- 52
- 229
- 284
20
votes
2 answers
Using GHC API to compile Haskell sources to CORE and CORE to binary
The Idea
Hello! I want to create a program, that will generate Haskell Core and will use GHC API to compile it further into an executable. But before I will do it I want to construct a very basic example, showing how can we just compile Haskell…

Wojciech Danilo
- 11,573
- 17
- 66
- 132
19
votes
3 answers
Evaluation of Haskell Statements/Expressions using GHC API
For a tool I'm writing ( http://hackage.haskell.org/package/explore ) I need a way to read haskell function definitions at run-time, apply them to values from my tool and retrieve the results of their application.
Can anyone give me a very basic…

Cetin Sert
- 4,497
- 5
- 38
- 76
12
votes
1 answer
Haskell, GHC 8: dynamically load/import module
I need to have something like
-- Main.hs
module Main where
main :: IO ()
main = do
print Plugin.computation
With a Plugin like
-- Plugin.hs
module Plugin where
computation :: Int
computation = 4
However, I need the plugin to…

eugenk
- 472
- 4
- 11
12
votes
1 answer
Dynamically loading compiled Haskell module - GHC 7.6
I'm trying to dynamically compile and load Haskell modules using GHC API. I understand the API fluctuates quite a bit from on one version to another so I'm specifically talking about GHC 7.6.*.
I have tried running the same code on MacOS and Linux.…

roldugin
- 922
- 5
- 19
11
votes
1 answer
Simple way to have the GHC API for application deployed on Windows
I want to deploy an application on Windows that needs to access the GHC API. Using the first simple example from the Wiki:
http://www.haskell.org/haskellwiki/GHC/As_a_library
results in the following error (compiled on one machine with haskell…

mentics
- 6,852
- 5
- 39
- 93
11
votes
2 answers
Is it possible to use the GHC API to modify a program while compiling it?
I want to test the implementation a compiler optimization by piggybacking into the GHC compilation process and altering its Core representation. The idea would be to have something like:
runGhc (Just libdir) $ do
...
c <- compileToCoreModule…

matiash
- 54,791
- 16
- 125
- 154
11
votes
2 answers
Is it possible to generate and run TemplateHaskell generated code at runtime?
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.…

fadedbee
- 42,671
- 44
- 178
- 308
10
votes
1 answer
Infer type of a string containing a Haskell expression
I need a (quick and dirty) way to get some representation of the type of a Haskell expression that is given as a string.
I currently see 3 options:
Use GHC API -- however, the documentation loses me pretty quickly.
Use some other type inference…

xcvii
- 450
- 3
- 17
10
votes
1 answer
Reify a module into a record
Suppose I have an arbitrary module
module Foo where
foo :: Moo -> Goo
bar :: Car -> Far
baz :: Can -> Haz
where foo, bar, and baz are correctly implemented, etc.
I'd like to reify this module into an automatically-generated data type and…

Dan Burton
- 53,238
- 27
- 117
- 198
9
votes
1 answer
How to compile Haskell into the untyped lambda calculus (or GHC core)?
I'm looking for ways how to convert a simple Haskell program (no imported libraries, just data types and pure functions) into a term of the untyped lambda calculus. A promising approach seems to be to use GHC API to compile a program into GHC core,…

Petr
- 62,528
- 13
- 153
- 317
8
votes
1 answer
Can I compile a haskell function from a string at runtime (using plugins)?
I have an application where, for various reasons, I need to run arbitrary, user supplied code. (SafeHaskell makes this nice and secure). I've looked at the plugins package, which is really nice for loading from a .hi file on disc.
However, for my…

jmite
- 8,171
- 6
- 40
- 81
7
votes
1 answer
How to handle "panic: the impossible happened" and continue in Haskell
I have the following code that uses the GHC API to load modules and get the type of an expression:
typeObjects :: [String] -> [String] -> IO [Type]
typeObjects modules objects = do
defaultErrorHandler defaultDynFlags $ do
runGhc (Just libdir)…

mentics
- 6,852
- 5
- 39
- 93
7
votes
2 answers
Compiling to GHC Core
I would like to create a frontend for a simple language that would produce GHC Core. I would like to then take this output and run it through the normal GHC pipeline. According to this page, it is not directly possible from the ghc command. I am…

aelguindy
- 3,703
- 24
- 31