我正在寻找一种在Haskell和Java/Scala/C#代码之间交换数据的解决方案。目前,我正在考虑使用XML。理想情况下,我希望从我的Haskell数据类型生成XML模式。我的第一次尝试是HaXml 1.22.2,DrIFT 2.2.2。全部在GHC 7.0.3上。这里有以下代码片段:
import Data.List (isPrefixOf)
import Text.XML.HaXml.XmlContent
import Text.XML.HaXml.Types
import Text.XML.HaXml.Pretty (document)
data MyType = A | B String deriving (Eq, Show)
{-! derive : XmlContent !-} -- this line is for DrIFT在这个文件中,DrIFT生成:
{- Generated by DrIFT (Automatic class derivations for Haskell) -}
{-# LINE 1 "ts.hs" #-}
import Data.List (isPrefixOf)
import Text.XML.HaXml.XmlContent
import Text.XML.HaXml.Types
import Text.XML.HaXml.Pretty (document)
data MyType = A | B String deriving (Eq, Show)
{-! derive : XmlContent !-} -- this line is for DrIFT
{-* Generated by DrIFT : Look, but Don't Touch. *-}
instance HTypeable MyType where
toHType v =
Defined "MyType" [] [Constr "A" [] [],Constr "B" [] [toHType aa]]
where
(B aa) = v
instance XmlContent MyType where
parseContents = do
{ e@(Elem t _ _) <- elementWith (flip isPrefixOf) ["B","A"]
; case t of
_ | "B" `isPrefixOf` t -> interior e $ fmap B parseContents
| "A" `isPrefixOf` t -> interior e $ return A
}
toContents v@A =
[mkElemC (showConstr 0 (toHType v)) []]
toContents v@(B aa) =
[mkElemC (showConstr 1 (toHType v)) (toContents aa)]
-- Imported from other files :-使用GHC编译此代码会产生一条错误消息:
19:32:
Couldn't match expected type `[Char]' with actual type `QName'
In the second argument of `isPrefixOf', namely `t'
In the expression: "B" `isPrefixOf` t
In a stmt of a pattern guard for
a case alternative:
"B" `isPrefixOf` t是工具有问题还是我做错了什么?如何解决这个问题?
发布于 2011-09-21 22:24:06
在HaXml旧版本的黑客文档中发现,在1.20.2版和更早版本中,Elem数据构造函数used to take a Name,它只是String的一个类型同义词。但是,在1.22.3和版本1.22.3之间,它被更改为采用a custom data type QName。
因此,对元素名称使用isPrefixOf对于旧版本是有效的,但对于新版本是无效的,这是有意义的。
从这些版本的上传日期来看,这发生在去年的某个时候,而DrIFT似乎自2009年以来就没有更新过。
您可能应该将此情况通知DrIFT维护者。同时,您可以通过使用旧版本的HaXml或自己编写实例来解决此问题。您应该能够使用不正确的生成实例作为起点。
发布于 2011-09-22 04:37:03
我最近使用Apache Thrift项目成功地集成了Haskell和Python。Thrift编译器根据数据和服务定义文件生成必要语言的代码,从而创建无缝地相互传递消息的客户端和服务器。
发布于 2011-09-22 18:46:07
未经测试的修复:
1)将显示导入Data.List(isPrefixOf)的代码行更改为
import qualified Data.List(isPrefixOf) as List2)添加以下代码:
isPrefixOf (N n) = List.isPrefixOf n
isPrefixOf (QN _ n) = List.isPrefixOf n不过,我不确定这是否给出了限定名称的预期行为。
https://stackoverflow.com/questions/7501037
复制相似问题