6

The TemplateHaskell quoting documents two quotes ('') as the way to get the Name of a type:

> ''String
GHC.Base.String

This works fine for this type (name). However, I can't find a way to make it work nice for e.g. Maybe String:

> ''Maybe String -- interprets String as a data constructor
> ''Maybe ''String -- wants to apply ''String to the Name type

I know I can workaround via using [t| Maybe String |], but this is then in the Q monad, and requires type changes, and I think is not type-checked at the respective moment, only when spliced in.

I can also work around by first defining a type alias, type MaybeString = Maybe String, and then using ''MaybeString, but this is also cumbersome.

Any way to directly get what I want simply via the '' quotation?

iustin
  • 343
  • 1
  • 11

2 Answers2

6

'' is used to quote names, not types. Maybe is a name, Maybe String is not. It is therefore not too surprising that you have to give your type a name by defining a type alias, before you can quote that name.

[t| |] on the other hand, quotes types. Note the difference here.

Prelude> :t ''String
''String :: Language.Haskell.TH.Syntax.Name
Prelude> :t [t| String |]
[t| String |]
  :: Language.Haskell.TH.Syntax.Q Language.Haskell.TH.Syntax.Type

So I'm afraid you cannot use '' for what you're trying to do.

hammar
  • 138,522
  • 17
  • 304
  • 385
  • Thanks, then I guess I have to either use quasiquoting or find another solution as to not have to add [t| |] everywhere… – iustin Sep 20 '11 at 17:37
4

I think what you're looking for is:

ConT ''Maybe `AppT` ConT ''String
Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
  • Indeed, that works, but it is at cross with my purpose of trying to find a simple way to allow (other people) to write such a construct. Thanks in any case! – iustin Sep 21 '11 at 04:24
  • We have this predicament at a few places in Yesod. I think the result I came up with was just parsing a string, which works our fairly well most of the time. __Edit__ I'm guessing for your use case the [t|...|] would probably be better anyway, never mind. – Michael Snoyman Sep 21 '11 at 04:41
  • Yep, I thought about parsing the string too (I like how one declares the database in persistent, for example). However, it wouldn't help much in my case anyway. Maybe I'll find a better solution later, for now I can manage with the [t|…|] :) – iustin Sep 21 '11 at 15:23