Skip to content

Commit

Permalink
Play around with heterogeneous sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Sep 12, 2022
1 parent a082638 commit bf45ee3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions experiments/idris/fathom.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ modules = Fathom
, Fathom.Format.Record

, Playground
, Playground.HeterogeneousSequences
, Playground.OpenType.IndexedInductive
, Playground.OpenType.InductiveRecursive
, Playground.OpenType.Record
Expand Down
42 changes: 42 additions & 0 deletions experiments/idris/src/Playground/HeterogeneousSequences.idr
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

0 comments on commit bf45ee3

Please sign in to comment.