Comments (3)
It's true, this is confusing. They seem similar - but they aren't similar at all. Carefully note the exported types we get:
-- Term
encodeTerm :: Term -> Encoding
decodeTerm :: Decoder Term
-- FlatTerm
toFlatTerm :: Encoding -> FlatTerm
fromFlatTerm :: Decoder a -> FlatTerm -> Either String a
Term
is a Haskell ADT representation of any arbitrary CBOR value. This means there is a direct mapping between any arbitrary CBOR value and Term
. You can use decodeTerm
to decode any arbitrary CBOR value into a Haskell ADT, whether or not that thing was originally serialized in Haskell or Java or any number of languages. You simply decode any CBOR data using the decodeTerm :: Decoder Term
and get back a general AST for traversing/analysis.
FlatTerm
, however, is simply an alternative representation for encodings. Note that this package uses a "deep embedding". Whenever you create an Encoder out of the primitives in .Encoding
module, you are really just building up a simplistic AST that lists the actions to perform. Once we have this Encoding
, we can serialize it however we want. (This is the fundamental difference between this library and binary
).
So if we have an AST, we can really have multiple 'backends'. We can take the Encoding
and traverse the AST, and turn it into CBOR (this is what the .Write
module does). Decoder
is a similar type, which tells us what actions to perform when decoding (and what continuations to call, with the values we got back). So the .Read
module takes a Decoder
and some CBOR value and gives us back what the Decoder specified.
But .Read
and .Write
simply expose CBOR-based backends. FlatTerm
is a completely different 'backend'. Instead we simply take some Encoding
, and serialize it into a different, simpler Haskell data type. This type is FlatTerm
, and it is a "blob" that is a degenerate list. Then, when we have a Decoder
, and the given FlatTerm
(which is really, again, a list), we just traverse the list one by one, feeding the decoder with the values it expects. Or we error. Look through the source for FlatTerm
and you'll see what I mean.
With this decoupling of the backend from the actual encoders/decoders in mind, this module separation makes much more sense. FlatTerm
is an alternative backend, while Term
is almost like an alternative frontend, that's only built on top of our existing .Encoding
and .Decoding
modules.
Ultimately these are meant to serve different purposes. The FlatTerm
module is purely a module for people who write instances, to create properties they can check - they want to make sure their encoders and decoders work properly (there's no real reason to do a full round-trip to CBOR just for that, since you're only specifying the logic of your encoders and decoders.) It gives a simpler backend that will make debugging bad instances easier.
The Term
module is so we can read arbitrary CBOR and write arbitrary CBOR as needed in an easy way. You'd use it more for doing things like analysis, debugging or interoperability (like if you needed to encode some value slightly differently than the instance specifies, for interop - this would be an easy way to do it.)
from cborg.
(FWIW: someone should basically copy/paste something like this into the .Term
module... ;) I've added appropriate labels for this so it's made clear.)
from cborg.
Oh I see, thanks for a nice explanation! :-)
from cborg.
Related Issues (20)
- Support GHC 9.4 HOT 1
- cborg-0.2.6+ fails to build for 32bit: Couldn't match expected type βInt64#β with actual type βInt#β etc HOT 3
- Alternative tags have now been standardised
- Build error on ghc 9.2.2 HOT 2
- `serialise` `versus` benchmark fails to parse internal cabal libraries
- Allow vector-0.13 (in serialise) HOT 2
- Add instances for wide-word
- cborg-0.2.8.0 fails to build with 32bit ghc9 HOT 2
- Support base-4.18, strict-0.5, these-1.2, criterion-1.6 (i.e. GHC-9.6) HOT 2
- ghc-9.4 build error on M1 HOT 6
- Test `decodeValue` against `Reference.termToJson`.
- hPutSerialise should use hPutBuilder? HOT 1
- Encoding allocates too many blocks in old generation HOT 3
- Remove dependency on `aeson-pretty`
- fromFlatTerm impl of PeekTokenType is insufficently precise
- Support ghc-9.8.1 HOT 4
- serialise: Usage without requiring `Serialise` instances HOT 3
- serialise: allow tar-0.6
- serialise: allow zlib-0.7
- Versions on master have fallen behind Hackage
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cborg.