Skip to content

Commit 9e15d99

Browse files
committed
Add more type aliases for encoder/decoder pairs
1 parent 7289176 commit 9e15d99

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

experiments/idris/src/Fathom/Base.idr

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ import Data.List
1717
--
1818
-- TODO: Add support for [Narcissus-style stores](https://github.com/mit-plv/fiat/tree/master/src/Narcissus/Stores)
1919

20-
parameters (Source : Type, Target : Type)
20+
parameters (Source, Target : Type)
2121

2222
||| Decoders consume a _target value_ and produce either:
2323
|||
2424
||| - a _source value_
2525
||| - or nothing if in error occurred
2626
|||
2727
||| @ Source The type of source values (usually an in-memory data structure)
28+
||| @ Target The type of target values (usually a byte-stream)
2829
public export
2930
Decode : Type
3031
Decode = Target -> Maybe Source
3132

33+
3234
||| Encoders take a _source value_ and produce either:
3335
|||
3436
||| - a _target value_
@@ -41,6 +43,47 @@ parameters (Source : Type, Target : Type)
4143
Encode = Source -> Maybe Target
4244

4345

46+
parameters (Source, Target : Type)
47+
48+
||| Decode a portion of a _target value_, leaving some remaining for
49+
||| subsequent decoding.
50+
|||
51+
||| @ Source The type of source values (usually an in-memory data structure)
52+
||| @ Target The type of target values (usually a byte-stream)
53+
public export
54+
DecodePart : Type
55+
DecodePart = Decode (Source, Target) Target
56+
57+
58+
||| Consumes a _source value_ and the remaining _target value_, returning
59+
||| a fully encoded target value.
60+
|||
61+
||| @ Source The type of source values (usually an in-memory data structure)
62+
||| @ Target The type of target values (usually a byte-stream)
63+
public export
64+
EncodePart : Type
65+
EncodePart = Encode (Source, Target) Target
66+
67+
68+
parameters {0 Source, Target : Type}
69+
70+
public export
71+
toDecodeFull : (Monoid Target, Eq Target) => DecodePart Source Target -> Decode Source Target
72+
toDecodeFull decode target = do
73+
(source, target') <- decode target
74+
if target == neutral then Just source else Nothing
75+
76+
77+
public export
78+
toEncodeFull : Monoid Target => EncodePart Source Target -> Encode Source Target
79+
toEncodeFull encode source = encode (source, neutral)
80+
81+
82+
public export
83+
toEncodePart : Monoid Target => Encode Source Target -> EncodePart Source Target
84+
toEncodePart encode (source, target) = [| encode source <+> Just target |]
85+
86+
4487
----------------------
4588
-- ENCODING TARGETS --
4689
----------------------

0 commit comments

Comments
 (0)