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
1010-- bugtracker =
1111
1212-- packages to add to search path
13- -- depends =
13+ depends = contrib
1414
1515-- modules to install
1616modules = Fathom
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ module Fathom.Format.Record
1313
1414import Data.Colist
1515import Data.DPair
16+ import Data.HVect
1617import Data.Vect
1718
1819import Fathom.Base
@@ -107,6 +108,25 @@ namespace Format
107108 go (S len) (x :: xs) = [| f. encode x <+> go len xs | ]
108109
109110
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+
110130 public export
111131 pair : Format -> Format -> Format
112132 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
44
55
66import Data.Vect
7+ import Data.HVect
78
9+ import Fathom.Base
810import Fathom.Data.Sing
911import Fathom.Format.Record
1012-- import Fathom.Format.InductiveRecursiveCustom
1113
1214
13- namespace Format
15+ namespace Format . Pairs
1416
1517 ||| Construct a format based on a type tag
1618 value : Nat -> Format
@@ -41,3 +43,23 @@ namespace Format
4143 index : {ts : Vect len Nat } -> (i : Fin len) -> (values ts).Rep -> (value (index i ts)).Rep
4244 index {ts = _ :: _ } FZ (x, _ ) = x
4345 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