File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ modules = Fathom
2727 , Fathom.Format.Record
2828
2929 , Playground
30+ , Playground.HeterogeneousSequences
3031 , Playground.OpenType.IndexedInductive
3132 , Playground.OpenType.InductiveRecursive
3233 , Playground.OpenType.Record
Original file line number Diff line number Diff line change 1+ -- Heterogeneous sequence example
2+
3+ module Playground.HeterogeneousSequences
4+
5+
6+ import Data.Vect
7+
8+ import Fathom.Data.Sing
9+ import Fathom.Format.Record
10+ -- import Fathom.Format.InductiveRecursiveCustom
11+
12+
13+ namespace Format
14+
15+ ||| Parse a value based on a type tag
16+ value : Nat -> Format
17+ value 1 = u8
18+ value 2 = u16Be
19+ value 4 = u32Be
20+ value _ = fail
21+
22+
23+ ||| Parse a sequence of values based on a list of type tags
24+ values : (ts : Vect len Nat ) -> Format
25+ values [] = pure ()
26+ values (t :: ts) = pair (value t) (values ts)
27+
28+
29+ ||| An annoying example from: https://github.com/yeslogic/fathom/issues/394
30+ ouch : Format
31+ ouch = do
32+ len <- u16Be
33+ types <- repeat len u16Be
34+ values <- values types
35+ pure ()
36+
37+
38+ ||| Access an element at index @i of the in-memory representation of @values.
39+ ||| The type of the returned element is dependent on the sequence of type tags.
40+ index : {ts : Vect len Nat } -> (i : Fin len) -> (values ts).Rep -> (value (index i ts)).Rep
41+ index {ts = _ :: _ } FZ (x, _ ) = x
42+ index {ts = _ :: _ } (FS i) (_ , xs) = Format . index i xs
You can’t perform that action at this time.
0 commit comments