Skip to content

Commit bf45ee3

Browse files
committed
Play around with heterogeneous sequences
1 parent a082638 commit bf45ee3

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

experiments/idris/fathom.ipkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

0 commit comments

Comments
 (0)