8

Is there any proper way to make TH's functions safe if they use side effects? Say, I want to have a function that calls git in compile time and generates a version string:

{-# LANGUAGE TemplateHaskell #-}

module Qq where

import System.Process
import Language.Haskell.TH

version =  $( [| (readProcess "git" ["rev-parse", "HEAD"] "")  |] )

the type of version is IO String. But version is completely free of side effects in runtime, it has side effects only in compile time. Is there any way to make it pure in runtime without using unsafePerformIO ?

voidlizard
  • 805
  • 5
  • 10
  • jfyi, this might be slightly related to your question: If you want to infer a version string from git via Cabal you might be interested in https://gist.github.com/656738 – hvr Apr 19 '11 at 09:14

1 Answers1

7

First: normally, the runtime type of the generated code is independent of the compile-time type of the Template Haskell subexpressions, so the runtime type doesn't have to be in IO.

Now, to run this command without using unsafePerformIO, use runIO. You will then have to construct the Expr yourself, without using [| |] (this also solves the type problem).

Actually, if you use [| |] to insert an IO computation, I think it will only insert the computation, not run it, anyway. But that's an irrelevant aside, because regardless of what it does, that's not the right way to do what you want to do.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • 5
    As an exercise I tried to come up with the code that is described here. Maybe it's also useful for others, so here it goes: `version = (stringE . init) =<< (runIO $ readProcess "git" ["rev-parse", "HEAD"] "")` and use `$(version)` in a different module anywhere a String expression can be. – copton Jul 17 '11 at 09:09
  • Instead of constructing the `Expr` by hand, it's easier to use [`lift`](http://hackage.haskell.org/package/template-haskell-2.16.0.0/docs/Language-Haskell-TH-Syntax.html#v:lift). – Turion Nov 19 '20 at 20:02