简体   繁体   English

Haskell相当于Python的“构造”

[英]Haskell equivalent of Python's “Construct”

Construct is a DSL implemented in Python used to describe data structures (binary and textual). Construct是一个用Python实现的DSL,用于描述数据结构(二进制和文本)。 Once you have the data structure described, construct can parse and build it for you. 一旦掌握了所描述的数据结构,构造就可以为您解析并构建它。 Which is good ("DRY", "Declarative", "Denotational-Semantics"...) 哪个好(“DRY”,“声明”,“Denotational-Semantics”......)

Usage example: 用法示例:

# code from construct.formats.graphics.png
itxt_info = Struct("itxt_info",
  CString("keyword"),
  UBInt8("compression_flag"),
  compression_method,
  CString("language_tag"),
  CString("translated_keyword"),
  OnDemand(
    Field("text",
      lambda ctx: ctx._.length - (len(ctx.keyword) + 
      len(ctx.language_tag) + len(ctx.translated_keyword) + 5),
    ),
  ),
)

I am in need for such a tool for Haskell and I wonder if something like this exists. 我需要Haskell这样的工具,我想知道这样的事情是否存在。

I know of: 我知道:

  • Data.Binary: User implements parsing and building seperately Data.Binary:用户单独实现解析和构建
  • Parsec: Only for parsing? Parsec:仅用于解析? Only for text? 仅限文字?

I guess one must use Template Haskell to achieve this? 我想必须使用Template Haskell来实现这一目标吗?

I'd say it depends what you want to do, and if you need to comply with any existing format. 我会说这取决于你想做什么,以及你是否需要遵守任何现有的格式。

Data.Binary will (surprise!) help you with binary data, both reading and writing. Data.Binary将(惊喜!)帮助您读取和写入二进制数据。 You can either write the code to read/write yourself, or let go of the details and generate the required code for your data structures using some additional tools like DrIFT or Derive . 您可以编写代码来自行读/写,也可以使用DrIFTDerive等其他工具释放详细信息并为数据结构生成所需的代码。 DrIFT works as a preprocessor, while Derive can work as a preprocessor and with TemplateHaskell. DrIFT可用作预处理器,而Derive可用作预处理器和TemplateHaskell。

Parsec will only help you with parsing text. Parsec只会帮助您解析文本。 No binary data (as easily), and no writing. 没有二进制数据(很容易),也没有写作。 Work is done with regular String s. 使用常规String完成工作。 There are ByteString equivalents on hackage. hackage上有ByteString等价物。

For your example above I'd use Data.Binary and write custom put / get ers myself. 对于上面的示例,我将使用Data.Binary并自己编写自定义put / get ers。 Have a look at the parser category at hackage for more options. 有关更多选项,请查看hackage中的解析器类别

I don't know anything about Python or Construct, so this is probably not what you are searching for, but for simple data structures you can always just derive read: 我对Python或Construct一无所知,所以这可能不是你要搜索的内容,但对于简单的数据结构,你总是可以得到read:

data Test a = I Int | S a deriving (Read,Show)

Now, for the expression 现在,为了表达

read "S 123" :: Test Double

GHCi will emit: S 123.0 GHCi将排放:S 123.0

For anything more complex, you can make an instance of Read using Parsec. 对于任何更复杂的事情,您可以使用Parsec创建Read实例。

Currently (afaik) there is no equivalent to Construct in Haskell. 目前(afaik)没有相当于Haskell中的Construct。

One can be implemented using Template Haskell. 可以使用Template Haskell实现一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM