|
| 1 | +||| A closed universe of format descriptions as an inductive type, where the |
| 2 | +||| in-memory representation is tracked as an index on the type. |
| 3 | + |
| 4 | +module Fathom.Closed.IndexedInductive |
| 5 | + |
| 6 | + |
| 7 | +import Data.Colist |
| 8 | +import Data.Vect |
| 9 | + |
| 10 | +import Fathom.Base |
| 11 | + |
| 12 | + |
| 13 | +------------------------- |
| 14 | +-- FORMAT DESCRIPTIONS -- |
| 15 | +------------------------- |
| 16 | + |
| 17 | + |
| 18 | +||| Universe of format descriptions indexed by their machine representations |
| 19 | +public export |
| 20 | +data FormatOf : (0 Rep : Type) -> Type where |
| 21 | + End : FormatOf Unit |
| 22 | + Fail : FormatOf Void |
| 23 | + Pure : {0 A : Type} -> (x : A) -> FormatOf (Sing x) |
| 24 | + Skip : {0 A : Type} -> (f : FormatOf A) -> (def : A) -> FormatOf Unit |
| 25 | + Repeat : {0 A : Type} -> (len : Nat) -> FormatOf A -> FormatOf (Vect len A) |
| 26 | + Bind : {0 A : Type} -> {0 B : A -> Type} -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x) |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +--------------------------- |
| 31 | +-- ENCODER/DECODER PAIRS -- |
| 32 | +--------------------------- |
| 33 | + |
| 34 | +export |
| 35 | +decode : {0 Rep : Type} -> (f : FormatOf Rep) -> Decode Rep (Colist a) |
| 36 | +decode End [] = Just ((), []) |
| 37 | +decode End (_::_) = Nothing |
| 38 | +decode Fail _ = Nothing |
| 39 | +decode (Pure x) buffer = |
| 40 | + Just (MkSing x, buffer) |
| 41 | +decode (Skip f _) buffer = do |
| 42 | + (x, buffer') <- decode f buffer |
| 43 | + Just ((), buffer') |
| 44 | +decode (Repeat 0 f) buffer = |
| 45 | + Just ([], buffer) |
| 46 | +decode (Repeat (S len) f) buffer = do |
| 47 | + (x, buffer') <- decode f buffer |
| 48 | + (xs, buffer'') <- decode (Repeat len f) buffer' |
| 49 | + Just (x :: xs, buffer'') |
| 50 | +decode (Bind f1 f2) buffer = do |
| 51 | + (x, buffer') <- decode f1 buffer |
| 52 | + (y, buffer'') <- decode (f2 x) buffer' |
| 53 | + Just ((x ** y), buffer'') |
| 54 | +
|
| 55 | +
|
| 56 | +export |
| 57 | +encode : {0 Rep : Type} -> (f : FormatOf Rep) -> Encode Rep (Colist a) |
| 58 | +encode End () _ = Just [] |
| 59 | +encode (Pure x) (MkSing _) buffer = Just buffer |
| 60 | +encode (Skip f def) () buffer = do |
| 61 | + encode f def buffer |
| 62 | +encode (Repeat Z f) [] buffer = Just buffer |
| 63 | +encode (Repeat (S len) f) (x :: xs) buffer = do |
| 64 | + buffer' <- encode (Repeat len f) xs buffer |
| 65 | + encode f x buffer' |
| 66 | +encode (Bind f1 f2) (x ** y) buffer = do |
| 67 | + buffer' <- encode (f2 x) y buffer |
| 68 | + encode f1 x buffer' |
| 69 | + |
| 70 | + |
| 71 | +----------------- |
| 72 | +-- EXPERIMENTS -- |
| 73 | +----------------- |
| 74 | + |
| 75 | + |
| 76 | +either : (cond : Bool) -> FormatOf a -> FormatOf b -> FormatOf (if cond then a else b) |
| 77 | +either True f1 _ = f1 |
| 78 | +either False _ f2 = f2 |
| 79 | + |
| 80 | +orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def) |
| 81 | +orPure True f _ = f |
| 82 | +orPure False _ def = Pure def |
0 commit comments