Skip to content

Commit d11c5d4

Browse files
committed
Enable applicative notation for decoders
1 parent 0f475d3 commit d11c5d4

File tree

6 files changed

+51
-65
lines changed

6 files changed

+51
-65
lines changed

experiments/idris/src/Fathom/Base.idr

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,51 @@ parameters (Source, Target : Type)
7373
EncodePart = Encode (Source, Target) Target
7474

7575

76+
parameters {0 Source, Target : Type}
77+
78+
public export
79+
toDecodeFull : (Monoid Target, Eq Target) => DecodePart Source Target -> Decode Source Target
80+
toDecodeFull decode target = Prelude.do
81+
(source, target') <- decode target
82+
if target == neutral then Just source else Nothing
83+
84+
85+
public export
86+
toEncodeFull : Monoid Target => EncodePart Source Target -> Encode Source Target
87+
toEncodeFull encode source = encode (source, neutral)
88+
89+
90+
public export
91+
toEncodePart : Monoid Target => Encode Source Target -> EncodePart Source Target
92+
toEncodePart encode (source, target) = [| encode source <+> Just target |]
93+
94+
7695
namespace DecodePart
7796

7897
-- TODO: Should probably implement functor, applicative, or monad here. or use
7998
-- the reader, writer or state monad transformers
8099

100+
public export
101+
map : {0 S1, S2, T : Type} -> (S1 -> S2) -> DecodePart S1 T -> DecodePart S2 T
102+
map f decode target =
103+
Prelude.map (\(source, target') => (f source, target)) (decode target)
104+
105+
81106
public export
82107
pure : {0 S, T : Type} -> S -> DecodePart S T
83108
pure source target = Just (source, target)
84109

85110

86111
public export
87-
map : {0 S1, S2, T : Type} -> (S1 -> S2) -> DecodePart S1 T -> DecodePart S2 T
88-
map f decode target =
89-
Prelude.map (\(source, target') => (f source, target)) (decode target)
112+
(<*>) : {0 S1, S2, T : Type} -> DecodePart (S1 -> S2) T -> DecodePart S1 T -> DecodePart S2 T
113+
(<*>) decodeFun decode target = do
114+
(fun, target1) <- decodeFun target
115+
(source, target2) <- decode target1
116+
Just (fun source, target2)
90117

91118

92119
public export
93-
ignore :{0 S, T : Type} -> DecodePart S T -> DecodePart () T
120+
ignore : {0 S, T : Type} -> DecodePart S T -> DecodePart () T
94121
ignore = map (const ())
95122

96123

@@ -106,25 +133,6 @@ namespace DecodePart
106133
(>>=) = bind
107134

108135

109-
parameters {0 Source, Target : Type}
110-
111-
public export
112-
toDecodeFull : (Monoid Target, Eq Target) => DecodePart Source Target -> Decode Source Target
113-
toDecodeFull decode target = Prelude.do
114-
(source, target') <- decode target
115-
if target == neutral then Just source else Nothing
116-
117-
118-
public export
119-
toEncodeFull : Monoid Target => EncodePart Source Target -> Encode Source Target
120-
toEncodeFull encode source = encode (source, neutral)
121-
122-
123-
public export
124-
toEncodePart : Monoid Target => Encode Source Target -> EncodePart Source Target
125-
toEncodePart encode (source, target) = [| encode source <+> Just target |]
126-
127-
128136
----------------------
129137
-- ENCODING TARGETS --
130138
----------------------

experiments/idris/src/Fathom/Format/IndexedInductive.idr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ namespace FormatOf
5757
decode (Pure x) = pure (MkSing x)
5858
decode (Ignore f _) = ignore (decode f)
5959
decode (Repeat 0 f) = pure []
60-
decode (Repeat (S len) f) = do
61-
x <- decode f
62-
xs <- decode (Repeat len f)
63-
pure (x :: xs)
64-
decode (Pair f1 f2) = do
65-
x <- decode f1
66-
y <- decode f2
67-
pure (x, y)
60+
decode (Repeat (S len) f) =
61+
[| decode f :: decode (Repeat len f) |]
62+
decode (Pair f1 f2) =
63+
[| (,) (decode f1) (decode f2) |]
6864
decode (Bind f1 f2) = do
6965
x <- decode f1
7066
y <- decode (f2 x)

experiments/idris/src/Fathom/Format/IndexedInductiveCustom.idr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ namespace FormatOf
7171
decode (Pure x) = pure (MkSing x)
7272
decode (Ignore f _) = ignore (decode f)
7373
decode (Repeat 0 f) = pure []
74-
decode (Repeat (S len) f) = do
75-
x <- decode f
76-
xs <- decode (Repeat len f)
77-
pure (x :: xs)
78-
decode (Pair f1 f2) = do
79-
x <- decode f1
80-
y <- decode f2
81-
pure (x, y)
74+
decode (Repeat (S len) f) =
75+
[| decode f :: decode (Repeat len f) |]
76+
decode (Pair f1 f2) =
77+
[| (,) (decode f1) (decode f2) |]
8278
decode (Bind f1 f2) = do
8379
x <- decode f1
8480
y <- decode (f2 x)

experiments/idris/src/Fathom/Format/InductiveRecursive.idr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,10 @@ namespace Format
9494
decode (Pure x) = pure (MkSing x)
9595
decode (Ignore f _) = ignore (decode f)
9696
decode (Repeat 0 f) = pure []
97-
decode (Repeat (S len) f) = do
98-
x <- decode f
99-
xs <- decode (Repeat len f)
100-
pure (x :: xs)
101-
decode (Pair f1 f2) = do
102-
x <- decode f1
103-
y <- decode f2
104-
pure (x, y)
97+
decode (Repeat (S len) f) =
98+
[| decode f :: decode (Repeat len f) |]
99+
decode (Pair f1 f2) =
100+
[| (,) (decode f1) (decode f2) |]
105101
decode (Bind f1 f2) = do
106102
x <- decode f1
107103
y <- decode (f2 x)

experiments/idris/src/Fathom/Format/InductiveRecursiveCustom.idr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,10 @@ namespace Format
9292
decode (Pure x) = pure (MkSing x)
9393
decode (Ignore f _) = ignore (decode f)
9494
decode (Repeat 0 f) = pure []
95-
decode (Repeat (S len) f) = do
96-
x <- decode f
97-
xs <- decode (Repeat len f)
98-
pure (x :: xs)
99-
decode (Pair f1 f2) = do
100-
x <- decode f1
101-
y <- decode f2
102-
pure (x, y)
95+
decode (Repeat (S len) f) =
96+
[| decode f :: decode (Repeat len f) |]
97+
decode (Pair f1 f2) =
98+
[| (,) (decode f1) (decode f2) |]
10399
decode (Bind f1 f2) = do
104100
x <- decode f1
105101
y <- decode (f2 x)

experiments/idris/src/Fathom/Format/Record.idr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,13 @@ namespace Format
9898
decode = go len where
9999
go : (len : Nat) -> DecodePart (Vect len f.Rep) ByteStream
100100
go 0 = pure []
101-
go (S len) = do
102-
x <- f.decode
103-
xs <- go len
104-
pure (x :: xs)
101+
go (S len) = [| f.decode :: go len |]
105102

106103
encode : Encode Rep ByteStream
107104
encode = go len where
108105
go : (len : Nat) -> Encode (Vect len f.Rep) ByteStream
109106
go 0 [] = pure []
110-
go (S len) (x :: xs) =
111-
[| f.encode x <+> go len xs |]
107+
go (S len) (x :: xs) = [| f.encode x <+> go len xs |]
112108

113109

114110
public export
@@ -118,10 +114,8 @@ namespace Format
118114
Rep = (f1.Rep, f2.Rep)
119115

120116
decode : DecodePart Rep ByteStream
121-
decode = do
122-
x <- f1.decode
123-
y <- f2.decode
124-
pure (x, y)
117+
decode =
118+
[| (,) f1.decode f2.decode |]
125119

126120
encode : Encode Rep ByteStream
127121
encode (x, y) =

0 commit comments

Comments
 (0)