Haskell Python equivalent of "Construct"

Construct is a DSL implemented in Python used to describe data structures (binary and text). After you describe the data structure, the designer can analyze and build it for you. What is good ("DRY", "Declarative", "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 need such a tool for Haskell and I wonder if something like that exists.

I know:

  • Data.Binary: user implements parsing and building separately
  • Parsec: Only for parsing? For text only?

I think you need to use Template Haskell for this?

+3
3

(afaik) Construct Haskell.

Haskell.

0

, , , .

Data.Binary (!) , , . / , , , DrIFT Derive. Drift , Derive TemplateHaskell.

Parsec . ( ), . String s. hackage ByteString.

Data.Binary put/get. hackage .

+1

Python Construct, , , , , :

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

,

read "S 123" :: Test Double

GHCi : S 123.0

- Read Parsec.

0

Source: https://habr.com/ru/post/1714267/


All Articles