File tree Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ package fathom
10
10
-- bugtracker =
11
11
12
12
-- packages to add to search path
13
- -- depends =
13
+ depends = contrib
14
14
15
15
-- modules to install
16
16
modules = Fathom
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ module Fathom.Format.Record
13
13
14
14
import Data.Colist
15
15
import Data.DPair
16
+ import Data.HVect
16
17
import Data.Vect
17
18
18
19
import Fathom.Base
@@ -107,6 +108,25 @@ namespace Format
107
108
go (S len) (x :: xs) = [| f. encode x <+> go len xs | ]
108
109
109
110
111
+ public export
112
+ hrepeat : {len : Nat } -> Vect len Format -> Format
113
+ hrepeat fs = MkFormat { Rep , decode, encode } where
114
+ Rep : Type
115
+ Rep = HVect (map (. Rep ) fs)
116
+
117
+ decode : DecodePart Rep ByteStream
118
+ decode = go fs where
119
+ go : {len : Nat } -> (fs : Vect len Format) -> DecodePart (HVect (map (.Rep) fs)) ByteStream
120
+ go {len = Z } [] = pure []
121
+ go {len = S _ } (f :: fs) = [| f. decode :: go fs | ]
122
+
123
+ encode : Encode Rep ByteStream
124
+ encode = go fs where
125
+ go : {len : Nat } -> (fs : Vect len Format) -> Encode (HVect (map (.Rep) fs)) ByteStream
126
+ go {len = Z } [] [] = pure []
127
+ go {len = S _ } (f :: fs) (x :: xs) = [| f. encode x <+> go fs xs | ]
128
+
129
+
110
130
public export
111
131
pair : Format -> Format -> Format
112
132
pair f1 f2 = MkFormat { Rep , decode, encode } where
Original file line number Diff line number Diff line change @@ -4,13 +4,15 @@ module Playground.HeterogeneousSequences
4
4
5
5
6
6
import Data.Vect
7
+ import Data.HVect
7
8
9
+ import Fathom.Base
8
10
import Fathom.Data.Sing
9
11
import Fathom.Format.Record
10
12
-- import Fathom.Format.InductiveRecursiveCustom
11
13
12
14
13
- namespace Format
15
+ namespace Format . Pairs
14
16
15
17
||| Construct a format based on a type tag
16
18
value : Nat -> Format
@@ -41,3 +43,23 @@ namespace Format
41
43
index : {ts : Vect len Nat } -> (i : Fin len) -> (values ts).Rep -> (value (index i ts)).Rep
42
44
index {ts = _ :: _ } FZ (x, _ ) = x
43
45
index {ts = _ :: _ } (FS i) (_ , xs) = Format . index i xs
46
+
47
+
48
+ namespace Format . HRepeat
49
+
50
+ ||| Construct a format based on a type tag
51
+ value : Nat -> Format
52
+ value 1 = u8
53
+ value 2 = u16Be
54
+ value 4 = u32Be
55
+ value _ = fail
56
+
57
+
58
+ ||| An annoying example from: https://github.com/yeslogic/fathom/issues/394
59
+ ouch : Format
60
+ ouch = do
61
+ len <- u16Be
62
+ types <- repeat len u16Be
63
+ values <- hrepeat (map value types)
64
+ -- ^^^^^^^ heterogeneous repetitions
65
+ pure ()
You can’t perform that action at this time.
0 commit comments