Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Haskell type quoting problems

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?

like image 209
iustin Avatar asked Sep 20 '11 17:09

iustin


2 Answers

'' 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.

like image 185
hammar Avatar answered Oct 16 '22 21:10

hammar


I think what you're looking for is:

ConT ''Maybe `AppT` ConT ''String
like image 4
Michael Snoyman Avatar answered Oct 16 '22 19:10

Michael Snoyman