Skip to content

Commit

Permalink
Create a hetrogeneous repetition format with HVect
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Sep 12, 2022
1 parent 1599762 commit 1c37a1c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion experiments/idris/fathom.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package fathom
-- bugtracker =

-- packages to add to search path
-- depends =
depends = contrib

-- modules to install
modules = Fathom
Expand Down
20 changes: 20 additions & 0 deletions experiments/idris/src/Fathom/Format/Record.idr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Fathom.Format.Record

import Data.Colist
import Data.DPair
import Data.HVect
import Data.Vect

import Fathom.Base
Expand Down Expand Up @@ -107,6 +108,25 @@ namespace Format
go (S len) (x :: xs) = [| f.encode x <+> go len xs |]


public export
hrepeat : {len : Nat} -> Vect len Format -> Format
hrepeat fs = MkFormat { Rep, decode, encode } where
Rep : Type
Rep = HVect (map (.Rep) fs)

decode : DecodePart Rep ByteStream
decode = go fs where
go : {len : Nat} -> (fs : Vect len Format) -> DecodePart (HVect (map (.Rep) fs)) ByteStream
go {len = Z} [] = pure []
go {len = S _} (f :: fs) = [| f.decode :: go fs |]

encode : Encode Rep ByteStream
encode = go fs where
go : {len : Nat} -> (fs : Vect len Format) -> Encode (HVect (map (.Rep) fs)) ByteStream
go {len = Z} [] [] = pure []
go {len = S _} (f :: fs) (x :: xs) = [| f.encode x <+> go fs xs |]


public export
pair : Format -> Format -> Format
pair f1 f2 = MkFormat { Rep, decode, encode } where
Expand Down
24 changes: 23 additions & 1 deletion experiments/idris/src/Playground/HeterogeneousSequences.idr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module Playground.HeterogeneousSequences


import Data.Vect
import Data.HVect

import Fathom.Base
import Fathom.Data.Sing
import Fathom.Format.Record
-- import Fathom.Format.InductiveRecursiveCustom


namespace Format
namespace Format.Pairs

||| Construct a format based on a type tag
value : Nat -> Format
Expand Down Expand Up @@ -41,3 +43,23 @@ namespace Format
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


namespace Format.HRepeat

||| Construct a format based on a type tag
value : Nat -> Format
value 1 = u8
value 2 = u16Be
value 4 = u32Be
value _ = fail


||| An annoying example from: https://github.com/yeslogic/fathom/issues/394
ouch : Format
ouch = do
len <- u16Be
types <- repeat len u16Be
values <- hrepeat (map value types)
-- ^^^^^^^ heterogeneous repetitions
pure ()

0 comments on commit 1c37a1c

Please sign in to comment.