Skip to content

Commit 6e8660a

Browse files
committed
Add support for do notation
1 parent 505f286 commit 6e8660a

File tree

4 files changed

+100
-23
lines changed

4 files changed

+100
-23
lines changed

experiments/idris/src/Fathom/Closed/IndexedInductive.idr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ data FormatOf : (0 Rep : Type) -> Type where
2727
Bind : {0 A : Type} -> {0 B : A -> Type} -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
2828

2929

30+
-- Support for do notation
31+
32+
public export
33+
pure : {0 A : Type} -> (x : A) -> FormatOf (Sing x)
34+
pure = Pure
35+
36+
public export
37+
(>>=) : {0 A : Type} -> {0 B : A -> Type} -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
38+
(>>=) = Bind
39+
3040

3141
---------------------------
3242
-- ENCODER/DECODER PAIRS --

experiments/idris/src/Fathom/Closed/InductiveRecursive.idr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ mutual
7878
-- Rep (Custom f) = f.Rep
7979

8080

81+
-- Support for do notation
82+
83+
public export
84+
pure : {0 A : Type} -> A -> Format
85+
pure = Pure
86+
87+
public export
88+
(>>=) : (f : Format) -> (Rep f -> Format) -> Format
89+
(>>=) = Bind
90+
91+
8192
---------------------------
8293
-- ENCODER/DECODER PAIRS --
8394
---------------------------

experiments/idris/src/Fathom/Closed/InductiveRecursiveCustom.idr

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ encode (Bind f1 f2) (x ** y) =
9696
encode (Custom f) x = f.encode x
9797
9898
99+
--------------
100+
-- NOTATION --
101+
--------------
102+
103+
-- Support for do notation
104+
105+
public export
106+
pure : {0 A : Type} -> A -> Format
107+
pure = Pure
108+
109+
public export
110+
(>>=) : (f : Format) -> (Rep f -> Format) -> Format
111+
(>>=) = Bind
112+
113+
99114
--------------------
100115
-- CUSTOM FORMATS --
101116
--------------------
@@ -164,33 +179,37 @@ foo cond f def = case orPure cond (toFormatOf f) def of
164179
-- },
165180
-- };
166181
flag : Format
167-
flag =
168-
Bind u8 (\flag =>
169-
if flag == 0 then u8 else Pure {A = Bits8} 0)
182+
flag = do
183+
flag <- u8
184+
if flag == 0 then u8 else
185+
Pure {A = Bits8} 0
170186

171187
-- def simple_glyph = fun (number_of_contours : U16) => {
172188
-- ...
173189
-- let flag_repeat = fun (f : Repr flag) => f.repeat + (1 : U8),
174190
-- ...
175191
-- };
176192
simple_glyph : Format
177-
simple_glyph =
178-
-- ...
179-
Bind flag (\(flag ** repeat) =>
180-
let
181-
repeat' : Bits8
182-
repeat' = case flag of
183-
0 => repeat
184-
x => ?todo4
185-
186-
-- repeat' : Bits8
187-
-- repeat' with (MkSingEq flag)
188-
-- repeat' | MkSingEq 0 {prf} = rewrite sym prf in repeat
189-
-- repeat' | MkSingEq x {prf} = ?todo4
190-
191-
-- repeat' : Bits8
192-
-- repeat' = case MkSingEq flag of
193-
-- MkSingEq 0 {prf} => ?todo3
194-
-- MkSingEq x {prf} => ?todo4
195-
in
196-
Pure (repeat' + 1))
193+
simple_glyph = do
194+
(flag ** repeat) <- flag
195+
let
196+
repeat' : Bits8
197+
repeat' = case flag of
198+
0 => repeat
199+
x => ?todo4
200+
201+
-- repeat' : Bits8
202+
-- repeat' with (flag)
203+
-- repeat' | 0 = ?todo3
204+
-- repeat' | x = ?todo4
205+
206+
-- repeat' : Bits8
207+
-- repeat' with (MkSingEq flag)
208+
-- repeat' | MkSingEq 0 {prf} = ?help
209+
-- repeat' | MkSingEq x {prf} = ?todo4
210+
211+
-- repeat' : Bits8
212+
-- repeat' = case MkSingEq flag of
213+
-- MkSingEq 0 {prf} => ?todo3
214+
-- MkSingEq x {prf} => ?todo4
215+
Pure (repeat' + 1)

experiments/idris/src/Fathom/Open/Record.idr

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ import Data.Colist
1515
import Data.Vect
1616

1717
import Fathom.Base
18+
import Fathom.Data.Refine
1819
import Fathom.Data.Sing
1920

2021

22+
-------------------------
23+
-- FORMAT DESCRIPTIONS --
24+
-------------------------
25+
26+
2127
public export
2228
record Format where
2329
constructor MkFormat
@@ -26,6 +32,11 @@ record Format where
2632
encode : Encode Rep BitStream
2733

2834

35+
--------------
36+
-- FORMATS --
37+
--------------
38+
39+
2940
public export
3041
end : Format
3142
end = MkFormat { Rep, decode, encode } where
@@ -39,6 +50,7 @@ end = MkFormat { Rep, decode, encode } where
3950
encode : Encode Rep BitStream
4051
encode () = Just []
4152

53+
4254
public export
4355
fail : Format
4456
fail = MkFormat { Rep, decode, encode } where
@@ -51,6 +63,7 @@ fail = MkFormat { Rep, decode, encode } where
5163
encode : Encode Rep BitStream
5264
encode x = void x
5365

66+
5467
public export
5568
pure : {0 A : Type} -> A -> Format
5669
pure x = MkFormat { Rep, decode, encode } where
@@ -63,6 +76,7 @@ pure x = MkFormat { Rep, decode, encode } where
6376
encode : Encode Rep BitStream
6477
encode (MkSing _) = Just []
6578

79+
6680
public export
6781
skip : (f : Format) -> (def : f.Rep) -> Format
6882
skip f def = MkFormat { Rep, decode, encode } where
@@ -116,3 +130,26 @@ bind f1 f2 = MkFormat { Rep, decode, encode } where
116130
encode : Encode Rep BitStream
117131
encode (x ** y) =
118132
[| f1.encode x <+> (f2 x).encode y |]
133+
134+
135+
-- Support for do notation
136+
137+
public export
138+
(>>=) : (f : Format) -> (Rep f -> Format) -> Format
139+
(>>=) = bind
140+
141+
142+
143+
-----------------
144+
-- EXPERIMENTS --
145+
-----------------
146+
147+
148+
||| A format description refined with a fixed representation
149+
public export
150+
FormatOf : (0 Rep : Type) -> Type
151+
FormatOf rep = Refine Format (\f => f.Rep = rep)
152+
153+
154+
toFormatOf : (f : Format) -> FormatOf f.Rep
155+
toFormatOf f = MkRefine f

0 commit comments

Comments
 (0)