File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-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+   |||  Construct a format 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+   |||  A heterogeneous sequence of values where the element formats depend on a
24+   |||  sequence of type tags
25+   values  :  (ts  : Vect  len Nat ) ->  Format
26+   values [] =  pure () 
27+   values (t ::  ts) =  pair (value t) (values ts)
28+ 
29+ 
30+   |||  An annoying example from: https://github.com/yeslogic/fathom/issues/394
31+   ouch  :  Format
32+   ouch =  do 
33+     len <-  u16Be
34+     types <-  repeat  len u16Be
35+     values <-  values types
36+     pure () 
37+ 
38+ 
39+   |||  Access an element at index @i of the in-memory representation of @values.
40+   |||  The type of the returned element is dependent on the sequence of type tags.
41+   index  :  {ts  : Vect  len Nat } ->  (i  : Fin  len) ->  (values ts).Rep ->  (value (index i ts)).Rep
42+   index {ts =  _  ::  _ } FZ  (x, _ ) =  x
43+   index {ts =  _  ::  _ } (FS  i) (_ , xs) =  Format . index i xs
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments