@@ -29,77 +29,56 @@ data FormatOf : Type -> Type where
2929 Bind : {0 A : Type } -> {0 B : A -> Type } -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
3030
3131
32- -- Support for do notation
32+ namespace FormatOf
3333
34- public export
35- pure : {0 A : Type } -> (x : A) -> FormatOf (Sing x)
36- pure = Pure
34+ -- Support for do notation
3735
38- public export
39- (>>= ) : {0 A : Type } -> {0 B : A -> Type } -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
40- (>>= ) = Bind
41-
42-
43- -- -------------------------
44- -- ENCODER/DECODER PAIRS --
45- -- -------------------------
46-
47-
48- export
49- decode : {0 A, S : Type } -> (f : FormatOf A) -> Decode (A, Colist S) (Colist S)
50- decode End [] = Just (() , [])
51- decode End (_ :: _ ) = Nothing
52- decode Fail _ = Nothing
53- decode (Pure x) buffer =
54- Just (MkSing x, buffer)
55- decode (Skip f _ ) buffer = do
56- (x, buffer') <- decode f buffer
57- Just (() , buffer')
58- decode (Repeat 0 f) buffer =
59- Just ([], buffer)
60- decode (Repeat (S len) f) buffer = do
61- (x, buffer') <- decode f buffer
62- (xs, buffer'' ) <- decode (Repeat len f) buffer'
63- Just (x :: xs, buffer'' )
64- decode (Bind f1 f2) buffer = do
65- (x, buffer' ) <- decode f1 buffer
66- (y, buffer'' ) <- decode (f2 x) buffer'
67- Just ((x ** y), buffer'' )
68-
69- -- export
70- -- decode : {0 A, S : Type} -> (f : FormatOf A) -> Decode (A, Colist S) (Colist S)
71- -- decode End
72- -- = \b uffer => case buffer of
73- -- [] => Just ((), [])
74- -- _::_ => Nothing
75- -- decode Fail
76- -- = const Nothing
77- -- decode (Pure x)
78- -- = pure (MkSing x)
79- -- decode (Skip f _)
80- -- = do _ <- decode f
81- -- pure ()
82- -- decode (Repeat 0 f) = pure []
83- -- decode (Repeat (S len) f)
84- -- = do x <- decode f
85- -- xs <- decode (Repeat len f)
86- -- pure (x :: xs)
87- -- decode (Bind f1 f2)
88- -- = do x <- decode f1
89- -- y <- decode (f2 x)
90- -- pure (x ** y)
91-
92-
93- export
94- encode : {0 A, S : Type} -> (f : FormatOf A) -> Encode A (Colist S)
95- encode End () = Just []
96- encode (Pure x) (MkSing _) = Just []
97- encode (Skip f def) () = encode f def
98- encode (Repeat Z f) [] = Just []
99- encode (Repeat (S len) f) (x :: xs) =
100- [| encode f x <+> encode (Repeat len f) xs |]
101- encode (Bind f1 f2) (x ** y) =
102- [| encode f1 x <+> encode (f2 x) y |]
36+ public export
37+ pure : {0 A : Type } -> (x : A) -> FormatOf (Sing x)
38+ pure = Pure
39+
40+ public export
41+ (>>= ) : {0 A : Type } -> {0 B : A -> Type } -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
42+ (>>= ) = Bind
43+
44+
45+ -- -------------------------
46+ -- ENCODER/DECODER PAIRS --
47+ -- -------------------------
48+
49+
50+ export
51+ decode : {0 A, S : Type } -> (f : FormatOf A) -> Decode (A, Colist S) (Colist S)
52+ decode End [] = Just (() , [])
53+ decode End (_ :: _ ) = Nothing
54+ decode Fail _ = Nothing
55+ decode (Pure x) buffer =
56+ Just (MkSing x, buffer)
57+ decode (Skip f _ ) buffer = do
58+ (x, buffer') <- decode f buffer
59+ Just (() , buffer')
60+ decode (Repeat 0 f) buffer =
61+ Just ([], buffer)
62+ decode (Repeat (S len) f) buffer = do
63+ (x, buffer') <- decode f buffer
64+ (xs, buffer'' ) <- decode (Repeat len f) buffer'
65+ Just (x :: xs, buffer'' )
66+ decode (Bind f1 f2) buffer = do
67+ (x, buffer' ) <- decode f1 buffer
68+ (y, buffer'' ) <- decode (f2 x) buffer'
69+ Just ((x ** y), buffer'' )
70+
71+
72+ export
73+ encode : {0 A, S : Type} -> (f : FormatOf A) -> Encode A (Colist S)
74+ encode End () = Just []
75+ encode (Pure x) (MkSing _) = Just []
76+ encode (Skip f def) () = encode f def
77+ encode (Repeat Z f) [] = Just []
78+ encode (Repeat (S len) f) (x :: xs) =
79+ [| encode f x <+> encode (Repeat len f) xs |]
80+ encode (Bind f1 f2) (x ** y) =
81+ [| encode f1 x <+> encode (f2 x) y |]
10382
10483
10584-------------------------
@@ -122,14 +101,32 @@ record Format where
122101------------------------------------
123102
124103
125- public export
126- toFormatOf : (f : Format) -> FormatOf f.Rep
127- toFormatOf (MkFormat _ f) = f
104+ namespace Format
128105
106+ public export
107+ toFormatOf : (f : Format) -> FormatOf f.Rep
108+ toFormatOf (MkFormat _ f) = f
109+
110+
111+ ||| Convert a format description into an indexed format description with an
112+ ||| equality proof that the representation is the same as the index.
113+ public export
114+ toFormatOfEq : {0 A : Type} -> (Subset Format (\f => f.Rep = A)) -> FormatOf A
115+ toFormatOfEq (Element f prf) = rewrite sym prf in f.format
129116
130- public export
131- toFormat : {0 A : Type} -> FormatOf A -> Format
132- toFormat f = MkFormat A f
117+
118+ namespace FormatOf
119+
120+ public export
121+ toFormat : {0 A : Type} -> FormatOf A -> Format
122+ toFormat f = MkFormat A f
123+
124+
125+ ||| Convert an indexed format description to a existential format description,
126+ ||| along with a proof that the representation is the same as the index.
127+ public export
128+ toFormatEq : {0 A : Type} -> FormatOf A -> (Subset Format (\f => f.Rep = A))
129+ toFormatEq f = Element (MkFormat A f) Refl
133130
134131
135132public export
@@ -142,20 +139,6 @@ toFormatOfIso = MkIso
142139 }
143140
144141
145- ||| Convert a format description into an indexed format description with an
146- ||| equality proof that the representation is the same as the index.
147- public export
148- toFormatOfEq : {0 A : Type} -> (Subset Format (\f => f.Rep = A)) -> FormatOf A
149- toFormatOfEq (Element f prf) = rewrite sym prf in f.format
150-
151-
152- ||| Convert an indexed format description to a existential format description,
153- ||| along with a proof that the representation is the same as the index.
154- public export
155- toFormatEq : {0 A : Type} -> FormatOf A -> (Subset Format (\f => f.Rep = A))
156- toFormatEq f = Element (MkFormat A f) Refl
157-
158-
159142public export
160143toFormatOfEqIso : Iso (Exists (\a => (Subset Format (\f => f.Rep = a)))) (Exists FormatOf)
161144toFormatOfEqIso = MkIso
0 commit comments