[GHC] #4372: Extending quasiquotation support
GHC
ghc-devs at haskell.org
Sat Feb 21 13:58:59 UTC 2015
#4372: Extending quasiquotation support
-------------------------------------+-------------------------------------
Reporter: simonpj | Owner:
Type: bug | Status: new
Priority: normal | Milestone: 7.12.1
Component: Template Haskell | Version: 6.12.3
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
Type of failure: None/Unknown | Unknown/Multiple
Blocked By: | Test Case:
Related Tickets: | Blocking:
| Differential Revisions:
-------------------------------------+-------------------------------------
Comment (by songzh):
Replying to [comment:16 thoughtpolice]:
> Moving to 7.12.1 milestone; if you feel this is an error and should be
addressed sooner, please move it back to the 7.10.1 milestone.
Why this is being moved to 7.12? I was excited that it could be resolving
in 7.10. I am only a newbie of Haskell, but I want to address something
about this problem. I want to write type provider for Haskell that
generates data type definition from JSON or OData sources. For example:
{{{
{"name" : "Ciambellone Cake",
"ingredients": [{ "name": "Flour",
"quantity": 250,
"measure": "gr" }]}
}}}
should generate
{{{
data Recipe = Recipe { recipeName :: String,
recipeIngredients: [Ingredient]}
data Ingredient = Ingredient { ingredientName :: String,
ingredientQuantity :: Int,
ingredientMeasure :: String}
}}}
However, you need to give a type name when you do it (Here, it is Recipe).
In F#, just write:
{{{
type Recipe = JsonProvider<"JSONSample.json">
}}}
However, it seems that we cannot do this in TH (I tried but cannot do it,
if we can do tell me please!):
{{{
data Recipe = $(templateHaskellpart)
}}}
which means we have to have a parameterized quoter
{{{
quotejson :: String -> QuasiQuoter
quotejson name = QuasiQuoter {
quoteDec = \jsonStr -> gen name (getJSON jsonStr)
}
getJSON :: String -> JValue
gen :: String -> JValue -> Q [Dec]
}}}
Being unable to do this means that we have to define a json quoter for
each we need in a separated module, which is painful. Well, we can use
quoteExp so solve this, however, the quotation marks and other special
characters in JSON or other data sources can be very annoying sometimes.
Another problem is that I suppose that the 4 quoters t, p, e, d are
implemented in the layer of syntax, i.e. they do not have QuasiQuoter
type, so if I want to defined a quoter called e or d, it seems to be
impossible. I personally suppose that it might be better if the 4 quoters
are provided in library layer instead of syntax level, just like other
regular quoters. By which I mean we can define the lexer as following:
{{{
'[' quoterExp '|' strings '|]'
}}}
and hand quoterExp afterwards. I do not understand why it is implemented
in current way. Apologize if I said something stupid. :-)
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/4372#comment:17>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list