diff options
| author | Joshua Rahm <joshua.rahm@colorado.edu> | 2014-04-04 19:38:15 -0600 |
|---|---|---|
| committer | Joshua Rahm <joshua.rahm@colorado.edu> | 2014-04-04 19:38:15 -0600 |
| commit | e083553a455d30374f21aa0c34d9ae827470d490 (patch) | |
| tree | 0313b29e5ff36efa76a53dbe63169c9d18b4433f /TileShow.hs | |
| download | terralloc-e083553a455d30374f21aa0c34d9ae827470d490.tar.gz terralloc-e083553a455d30374f21aa0c34d9ae827470d490.tar.bz2 terralloc-e083553a455d30374f21aa0c34d9ae827470d490.zip | |
intiial commit
Diffstat (limited to 'TileShow.hs')
| -rw-r--r-- | TileShow.hs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/TileShow.hs b/TileShow.hs new file mode 100644 index 0000000..dd353dc --- /dev/null +++ b/TileShow.hs @@ -0,0 +1,27 @@ +{-# LANGUAGE TemplateHaskell #-} + +module TileShow where +import Language.Haskell.TH + +makeShow t = do + TyConI (DataD _ _ _ constructors _) <- reify t + -- Make `show` clause for one constructor: + -- show (A x1 x2) = "A "++show x1++" "++show x2 + let showClause (NormalC name fields) = do + -- Name of constructor, i.e. "A". Will become string literal in generated code + let constructorName = [(head $ nameBase name)] + -- Generate function clause for one constructor + clause [conP name []] -- (A x1 x2) + (normalB [| constructorName |]) [] -- "A "++show x1++" "++show x2 + -- Make body for function `show`: + -- show (A x1 x2) = "A "++show x1++" "++show x2 + -- show (B x1) = "B "++show x1 + -- show C = "C" + showbody <- mapM showClause constructors + -- Generate template instance declaration and then replace + -- type name (T1) and function body (\x -> "text") with our data + d <- [d| instance Show String where + show _x = "text" + |] + let [InstanceD [] (AppT showt (ConT _T1)) [FunD showf _text]] = d + return [InstanceD [] (AppT showt (ConT t )) [FunD showf showbody]] |