template-haskell

How to examine a quoted data constructor name in Template Haskell?

风流意气都作罢 提交于 2019-12-01 06:13:51
问题 I'm trying to learn some Template Haskell. As an exercise, I wrote a function that can generate things like isLeft and isRight (inspired by this question). Here's my humble attempt: isA connam = do ConE nam <- connam nn <- newName "p" lamE [varP nn] $ caseE (varE nn) [ match (conP nam [wildP]) ( normalB [| True |] ) [], match wildP ( normalB [| False |] ) [] ] The problem is that it only works with one-argument constructors. The culprit is the conP nam [wildP] pattern. Ideally, it should look

Haskell: Template Haskell and the scope

放肆的年华 提交于 2019-12-01 03:27:11
This code is compiled fine: data None = None { _f :: Int } type Simpl = Env type Env = Int However, I got an error with this code: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data None = None { _f :: Int } type Simpl = Env makeLenses ''None type Env = Int Error: Not in scope: type constructor or class `Env' I just added a single line makeLenses ''None between type declarations. This means TemplateHaskell code could change the scope of type constructor? Does anyone know the detail about this issue(or how to avoid this problem)? If you reorder your code as follows, it works: {-#

Haskell: Template Haskell and the scope

让人想犯罪 __ 提交于 2019-11-30 23:22:38
问题 This code is compiled fine: data None = None { _f :: Int } type Simpl = Env type Env = Int However, I got an error with this code: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data None = None { _f :: Int } type Simpl = Env makeLenses ''None type Env = Int Error: Not in scope: type constructor or class `Env' I just added a single line makeLenses ''None between type declarations. This means TemplateHaskell code could change the scope of type constructor? Does anyone know the detail

Local variables in Template Haskell declarations

只愿长相守 提交于 2019-11-30 09:16:52
问题 I'm reading through pozorvlak's baby steps post on Template Haskell in an attempt to understand it myself, and I came across this section: Recall that we were trying to programmatically produce declarations of the form data Fred = Fred . Let's try it with quasiquoting. Because of the restrictions on calling TH code, we'll have to put it in its own module, so let's put the following in Keyword.hs so the compiler can find it: module Keyword (keyword) where import Language.Haskell.TH.Syntax

Boilerplate-free annotation of ASTs in Haskell?

独自空忆成欢 提交于 2019-11-29 22:19:03
I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I know I can use SYB or uniplate to do the traversal, but I'm wondering if there's a boilerplate-free way to deal with the types. So, suppose we have a bunch of algebraic types for our AST: data Expr = PlusExpr Expr Expr ... data Def = TypeAlias String [String] Type ... If I were writing the boilerplate, I'd make new types like this: data

Local variables in Template Haskell declarations

白昼怎懂夜的黑 提交于 2019-11-29 13:44:28
I'm reading through pozorvlak's baby steps post on Template Haskell in an attempt to understand it myself, and I came across this section: Recall that we were trying to programmatically produce declarations of the form data Fred = Fred . Let's try it with quasiquoting. Because of the restrictions on calling TH code, we'll have to put it in its own module, so let's put the following in Keyword.hs so the compiler can find it: module Keyword (keyword) where import Language.Haskell.TH.Syntax keyword name = [d| data $(name) = $(name) |] Now compile: Prelude> :l Keyword.hs [1 of 1] Compiling Keyword

Template Haskell: Is there a function (or special syntax) that parses a String and returns Q Exp?

五迷三道 提交于 2019-11-29 01:16:19
I am trying to learn a bit of Template Haskell and Quasi Quotation, and I am looking for a function that takes a String and parses it to Q Exp , so the type is: String -> Q Exp Tried searching hoogle, but the results I saw had to do with lifting String literals to Q Exp , and the closest I found was Language.Haskell.TH.dyn which does quite what I want, but only for a single variable. Are there other options? E.g. a special syntax? I'm just in the process of familiarizing myself with [||] and $() , so maybe there is something for this purpose too? An example of how I imagine it would work: runQ

Boilerplate-free annotation of ASTs in Haskell?

核能气质少年 提交于 2019-11-28 19:56:30
问题 I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I know I can use SYB or uniplate to do the traversal, but I'm wondering if there's a boilerplate-free way to deal with the types. So, suppose we have a bunch of algebraic types for our AST: data Expr = PlusExpr Expr Expr ... data Def = TypeAlias

What's so bad about Template Haskell?

梦想的初衷 提交于 2019-11-28 14:59:44
It seems that Template Haskell is often viewed by the Haskell community as an unfortunate convenience. It's hard to put into words exactly what I have observed in this regard, but consider these few examples Template Haskell listed under "The Ugly (but necessary)" in response to the question Which Haskell (GHC) extensions should users use/avoid? Template Haskell considered a temporary/inferior solution in Unboxed Vectors of newtype'd values thread (libraries mailing list) Yesod is often criticized for relying too much on Template Haskell (see the blog post in response to this sentiment) I've

Preferred method for viewing code generated by Template Haskell

吃可爱长大的小学妹 提交于 2019-11-28 06:12:22
As you know, Template Haskell is used to generate various kinds of AST splices programmatically at compile-time. However, a splice can often be very opaque, and it is often difficult to discern what a splice actually generates. If you run the Q monad for a splice, and the splice is well-typed, you get a show able representation of the generated piece of AST, but this representation can be very difficult to understand, because of its unstructured layout. What is the preferred method for converting a piece of TH-generated AST into something akin to normal Haskell code, so that the code can be