Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateHaskell and IO

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 ?

like image 208
voidlizard Avatar asked Apr 19 '11 08:04

voidlizard


1 Answers

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.

like image 110
Robin Green Avatar answered Sep 27 '22 18:09

Robin Green