[Template-haskell] Defining a function and using it in a splice within the same module

Simon Peyton-Jones simonpj at microsoft.com
Thu Oct 25 05:37:15 EDT 2007


| As a (simplified) example, in my System Modelling DSL I have to create
| system definition. That is done by providing the function Name
| together with the identifiers of its inputs and outputs.
|
| inputs = ["in1", "in2"]
|
| outputs = ["out1", "out2"]
|
| f :: Signal Int -> Signal Int -> (Signal Int, Signal Int)
| f = ...
|
| Since it is not possible to do
|
| system :: SysDef
| system = $(mkSysDef 'f inputs outputs)
|
| due to the limitation we mentioned
|
| I simply do
|
| system = $(mkSysDef 'f) inputs outputs
|
| where mkSysDef :: Name -> [String] -> [String] -> SysDef

Ah I see.  Although you presumably meant
        mkSysDef :: Name -> Q ([String] -> [String] -> SysDef)
else it'd be ill-typed.

| Excuse my ignorance (I'm not familiar with the internals of GHC) but,
| wouldn't it be possible to compute a dependency graph for each splice
| before executing them? Based on that, the compiler could decide
| whether to generate bytecode for a function or not.

Indeed it could. It's just a bit more complicated.

| Since what I propossed is more "bypassing the problem" than a
| workaround I don't know if its significant for the ticket.

It's a nice illustration of why the proposed feature is useful.  I'd be happy if you added it.

Simon


More information about the template-haskell mailing list