【发布时间】:2017-06-26 20:19:30
【问题描述】:
我想创建一个表达式,使我有编译时错误或URI。
[uri|http://stackoverflow.com|]
应该编译,但是
[uri|foo:/bar:\|]
不应该。
我遇到过 QuasiQuotes,它显然是针对这类问题的。但是,我似乎无法从解析的URI 创建Q Exp。
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax
import Language.Haskell.TH
import URI.ByteString
import Data.ByteString.Char8
uri = QuasiQuoter { quoteExp = \s ->
let
uri = either (\err -> error $ show err) id (parseURI laxURIParserOptions (pack s))
in
[| uri |]
}
不编译,因为它需要URI 的Lift 实例。但是,由于 GADT 的性质,我不确定如何创建。
deriving instance Lift (URIRef a)
抱怨没有Lift ByteString,但我不知道写一个。另一种方法是Data URI,但失败了
85 1 error • Couldn't match type ‘a’ with ‘Absolute’
‘a’ is a rigid type variable bound by
the instance declaration at uri-bytestring/src/URI/ByteString/Types.hs:85:1
Expected type: c (URIRef a)
Actual type: c (URIRef Absolute)
• In the expression: k (k (k (k (k (z URI)))))
In a case alternative:
ghc-prim-0.5.0.0:GHC.Types.I# 1# -> k (k (k (k (k (z URI)))))
In the expression:
case constrIndex c of {
ghc-prim-0.5.0.0:GHC.Types.I# 1# -> k (k (k (k (k (z URI)))))
_ -> k (k (k (k (z RelativeRef)))) }
When typechecking the code for ‘gunfold’
in a derived instance for ‘Data (URIRef a)’:
To see the code I am typechecking, use -ddump-deriv
• Relevant bindings include
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (URIRef a)
(bound at uri-bytestring/src/URI/ByteString/Types.hs:85:1) (haskell-stack-ghc)
我更喜欢使用Generics,但我不确定如何将它们与 QQ API 一起使用。
【问题讨论】:
标签: haskell types ghc template-haskell