Template Haskellを使ってみる1

何で動いたのかは分からないけど、取りあえず次のコードが動いたのでペタリ

import Th_Test1

main = print (th_test 10)

th_test_main.hsとしてでも保存しておく。

module Th_Test1 where

import Language.Haskell.TH

-- th_test a = a
$(do return [FunD (mkName "th_test") [Clause [(VarP $ mkName "a")] (NormalB $ VarE $ mkName "a") []]])

th_test1.hsとして保存。


コンパイルは次の様に行う。

ghc -fth --make th_test_main.hs
[1 of 2] Compiling Th_Test1         ( Th_Test1.hs, Th_Test1.o )
Loading package base ... linking ... done.
Loading package array-0.1.0.0 ... linking ... done.
Loading package packedstring-0.1.0.0 ... linking ... done.
Loading package pretty-1.0.0.0 ... linking ... done.
Loading package containers-0.1.0.2 ... linking ... done.
Loading package template-haskell ... linking ... done.
[2 of 2] Compiling Main             ( th_test_main.hs, th_test_main.o )
Linking th_test_main ...

コンパイルは力技(型合わしただけ)で通した。


生成物を実行してみると・・・。

./th_test_main                 
10

出来た!!