FAQ Database Discussion Community


Obtaining TH.Name for '[] without -XTemplateHaskell

haskell,template-haskell
Is there a way to obtain (import from base modules or write expression) a value of type Language.Haskell.TH.Name that represents '[] without enabling -XTemplateHaskell? A good reason to do so is that tools like hlint do not play well with TH and being able to avoid it therefore has a...

How does one “run” a data declaration from a quasiquoter

haskell,template-haskell
I'm trying to write a quasiquoter for some type declarations. I've written something along the lines of {-# LANGUAGE TemplateHaskell #-} import Language.Haskell.TH as TH import Language.Haskell.TH.Quote sample :: QuasiQuoter sample = let tName = TH.mkName "GenType" conName = TH.mkName "GetType" nameName = TH.mkName "name" creator _ = return [TH.DataD...

Generate a function using Template Haskell

haskell,template-haskell
Is it possible to define a function using Template Haskell? For example convertStringToValue :: String -> Int convertStringToValue "three" = 3 convertStringToValue "four" = 4 I also have a Map [Char] Int. fromList [("five",5),("six",6)] How can I add functions convertStringToValue "six" = 6 convertStringToValue "five" = 5 at compile time...

Missing imports from Hamlet libraries

haskell,yesod,template-haskell,hamlet
This is the code snippet from O reilly - Yesod - Widgets, getRootR = defaultLayout $ do setTitle "My Page Title" toWidget [lucius| h1 { color: green; } |] addScriptRemote "https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" toWidget [julius| $(function() { $("h1").click(function(){ alert("You clicked on the heading!"); }); }); |] toWidgetHead [hamlet| <meta name=keywords content="some sample...

Composite Primary Key in Yesod

haskell,yesod,template-haskell
I am fairly new to Haskell and have been experimenting with yesod for about a week now. I have been trying to connect to an existing database that has a composite primary key in sqlite. I managed to get the code to work with Database.Persist.Sqlite as a standalone application. Here...

Custom deriving(Read,Show) for enum type

haskell,template-haskell,deriving
Let's say I have this enumeration type: data TVShow = BobsBurgers | MrRobot | BatmanTAS and I want to define instances for Read and Show with the following behavior: show BobsBurgers = "Bob's Burgers" show MrRobot = "Mr. Robot" show BatmanTAS = "Batman: The Animated Series" read "Bob's Burgers" =...

Dynamically add routes at compile time in Scotty

haskell,template-haskell,scotty
Is it possible to add routes dynamically at compile time through a config file by using Template Haskell or any other way. Scotty has a function addRoute but I want to use it dynamically. Example import qualified Data.Text.Lazy as LTB sampleRoutes :: [(String, LTB.Text)] sampleRoutes = [("hello", LTB.pack "hello"), ("world",...

Why does Template Haskell add unexpected parens?

haskell,template-haskell
I have the following: import Control.Applicative import Control.Monad import Language.Haskell.TH mkExp :: [Name] -> ExpQ mkExp (name:[]) = [| ZipList $(varE name) |] mkExp (name:names) = [| ZipList $(varE name) <*> $(mkExp names) |] zipN :: Int -> ExpQ zipN n = do names <- mapM newName $ replicate n...

Is this expected behavior of Template Haskell?

haskell,template-haskell,lens
Can anyone tell why this code doesn't compile data A = A { _b :: B } makeLenses ''A type B = String with message Not in scope: type constructor or class B and this does: type B = String data A = A { _b :: B } makeLenses...