2013-03-02 at

Template Haskell: Hello World

Template Haskell is one of the standard tools for meta-programming in Haskell. I have no idea why it's so hard to find an example like this. (That's a lie - Haskell is an esoteric research language for people who have better things to do. And Template Haskell is an order beyond.) So I'm blogging what seemed to work for me.
  1. You need two files, th.hs and Thlib.hs. Put them in the same directory.
  2. Put this in th.hs:
    {-# LANGUAGE TemplateHaskell #-}

    import Thlib

    main :: IO [Char]
    main = return $(hello)
  3. Put this in Thlib.hs:
    {-# LANGUAGE TemplateHaskell #-}
    module Thlib where

    import Language.Haskell.TH

    hello :: Q Exp
    hello = [|"hello world"|]
  4. Go to your command line UI, and with the directory containing the two files above as the current working directory, type runghc th.hs
  5. Let me know if this doesn't work.

No comments :

Post a Comment