-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play around with heterogeneous sequences
- Loading branch information
1 parent
a082638
commit bf45ee3
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
experiments/idris/src/Playground/HeterogeneousSequences.idr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-- Heterogeneous sequence example | ||
|
||
module Playground.HeterogeneousSequences | ||
|
||
|
||
import Data.Vect | ||
|
||
import Fathom.Data.Sing | ||
import Fathom.Format.Record | ||
-- import Fathom.Format.InductiveRecursiveCustom | ||
|
||
|
||
namespace Format | ||
|
||
||| Parse a value based on a type tag | ||
value : Nat -> Format | ||
value 1 = u8 | ||
value 2 = u16Be | ||
value 4 = u32Be | ||
value _ = fail | ||
|
||
|
||
||| Parse a sequence of values based on a list of type tags | ||
values : (ts : Vect len Nat) -> Format | ||
values [] = pure () | ||
values (t :: ts) = pair (value t) (values ts) | ||
|
||
|
||
||| An annoying example from: https://github.com/yeslogic/fathom/issues/394 | ||
ouch : Format | ||
ouch = do | ||
len <- u16Be | ||
types <- repeat len u16Be | ||
values <- values types | ||
pure () | ||
|
||
|
||
||| Access an element at index @i of the in-memory representation of @values. | ||
||| The type of the returned element is dependent on the sequence of type tags. | ||
index : {ts : Vect len Nat} -> (i : Fin len) -> (values ts).Rep -> (value (index i ts)).Rep | ||
index {ts = _ :: _} FZ (x, _) = x | ||
index {ts = _ :: _} (FS i) (_, xs) = Format.index i xs |