Template Haskell type quoting problems

丶灬走出姿态 提交于 2020-01-03 09:57:29

问题


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?


回答1:


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




回答2:


I think what you're looking for is:

ConT ''Maybe `AppT` ConT ''String


来源:https://stackoverflow.com/questions/7489063/template-haskell-type-quoting-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!