Skip to content

Commit b5b4439

Browse files
committed
Get OpenType examples to work for all approaches
1 parent 6ea96ed commit b5b4439

File tree

6 files changed

+107
-108
lines changed

6 files changed

+107
-108
lines changed

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,3 @@ toFormatOfEqIso = MkIso
164164
, toFrom = \(Evidence _ _) => Refl
165165
, fromTo = \(Evidence _ ((MkFormat _ _) ** Refl)) => Refl
166166
}
167-
168-
169-
-----------------
170-
-- EXPERIMENTS --
171-
-----------------
172-
173-
174-
either : (cond : Bool) -> FormatOf a -> FormatOf b -> FormatOf (if cond then a else b)
175-
either True f1 _ = f1
176-
either False _ f2 = f2
177-
178-
orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
179-
orPure True f _ = f
180-
orPure False _ def = Pure def

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.Vect
1010

1111
import Fathom.Base
1212
import Fathom.Data.Iso
13+
import Fathom.Data.Sing
1314

1415

1516
---------------------------------
@@ -35,7 +36,7 @@ public export
3536
data FormatOf : (A : Type) -> Type where
3637
End : FormatOf Unit
3738
Fail : FormatOf Void
38-
Pure : {0 A : Type} -> (x : A) -> FormatOf A
39+
Pure : {0 A : Type} -> (x : A) -> FormatOf (Sing x)
3940
Skip : {0 A : Type} -> (f : FormatOf A) -> (def : A) -> FormatOf Unit
4041
Repeat : {0 A : Type} -> (len : Nat) -> FormatOf A -> FormatOf (Vect len A)
4142
Bind : {0 A : Type} -> {0 B : A -> Type} -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
@@ -45,7 +46,7 @@ data FormatOf : (A : Type) -> Type where
4546
-- Support for do notation
4647

4748
public export
48-
pure : {0 A : Type} -> (x : A) -> FormatOf A
49+
pure : {0 A : Type} -> (x : A) -> FormatOf (Sing x)
4950
pure = Pure
5051

5152
public export
@@ -64,7 +65,7 @@ decode End [] = Just ((), [])
6465
decode End (_::_) = Nothing
6566
decode Fail _ = Nothing
6667
decode (Pure x) buffer =
67-
Just (x, buffer)
68+
Just (MkSing x, buffer)
6869
decode (Skip f _) buffer = do
6970
(x, buffer') <- decode f buffer
7071
Just ((), buffer')
@@ -84,7 +85,7 @@ decode (Custom f) buffer = f.decode buffer
8485
export
8586
encode : {0 A : Type} -> (f : FormatOf A) -> Encode A (ByteStream)
8687
encode End () = Just []
87-
encode (Pure x) _ = Just []
88+
encode (Pure x) (MkSing _) = Just []
8889
encode (Skip f def) () = encode f def
8990
encode (Repeat Z f) [] = Just []
9091
encode (Repeat (S len) f) (x :: xs) =
@@ -194,24 +195,22 @@ u16Be = Custom (MkCustomFormat
194195
-- EXPERIMENTS --
195196
-----------------
196197
197-
198-
either : (cond : Bool) -> FormatOf a -> FormatOf b -> FormatOf (if cond then a else b)
199-
either True f1 _ = f1
200-
either False _ f2 = f2
201-
202-
orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf a
203-
orPure True f _ = f
204-
orPure False _ def = Pure def
205-
206-
207198
-- Reproduction of difficulties in OpenType format
208199
209200
210201
Flag : Type
211-
Flag = (flag : Nat ** repeat : Nat ** ())
202+
Flag =
203+
( id : Nat
204+
** repeat :
205+
case id of
206+
0 => Nat
207+
S n => Sing {A = Nat} 0
208+
** Sing ()
209+
)
212210
213211
(.repeat) : Flag -> Nat
214-
(.repeat) (_ ** repeat ** _) = repeat
212+
(.repeat) (0 ** repeat ** _) = repeat
213+
(.repeat) (S _ ** repeat ** _) = val repeat
215214
216215
217216
-- def flag = {
@@ -226,12 +225,15 @@ flag = do
226225
flag <- u8
227226
repeat <- case flag of
228227
0 => u8
229-
_ => Pure {A = Nat} 0
228+
S _ => Pure {A = Nat} 0
230229
Pure ()
231230
232231
233232
SimpleGlyph : Type
234-
SimpleGlyph = (flag : Flag ** Nat)
233+
SimpleGlyph =
234+
( flag : Flag
235+
** Sing (flag.repeat + 1)
236+
)
235237
236238
237239
-- def simple_glyph = fun (number_of_contours : U16) => {

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@ mutual
4949
Repeat : Nat -> Format -> Format
5050
Bind : (f : Format) -> (Rep f -> Format) -> Format
5151

52-
-- Questionable format descriptions
53-
-- OrPure : (cond : Bool) -> (f : Format) -> (def : Rep f) -> Format
54-
-- OfSing : (f : Format) -> Sing (Rep f) -> Format
55-
-- OfEq : (f : Format) -> (r : Type) -> {auto 0 prf : Rep f = r} -> Format
56-
57-
-- Broken stuff
58-
-- Let : (f : Format) -> (Rep f -> Format) -> Format
59-
-- Custom : (f : Record.Format) -> Format
60-
6152

6253
||| The in-memory representation of format descriptions
6354
public export
@@ -69,15 +60,6 @@ mutual
6960
Rep (Pure x) = Sing x
7061
Rep (Bind f1 f2) = (x : Rep f1 ** Rep (f2 x))
7162

72-
-- Questionable format descriptions
73-
-- Rep (OrPure _ f _) = Rep f
74-
-- Rep (OfSing f r) = value r
75-
-- Rep (OfEq f r) = r
76-
77-
-- Broken stuff
78-
-- Rep (Let f1 f2) = Rep (f2 ?halp)
79-
-- Rep (Custom f) = f.Rep
80-
8163

8264
-- Support for do notation
8365

@@ -187,19 +169,3 @@ toFormatOfEqIso = MkIso
187169
, toFrom = \(Evidence _ (MkFormatOf _)) => Refl
188170
, fromTo = \(Evidence _ (f ** Refl)) => Refl
189171
}
190-
export
191-
either : (cond : Bool) -> (f1 : Format) -> (f2 : Format) -> FormatOf (if cond then Rep f1 else Rep f2)
192-
either True f1 _ = MkFormatOf f1
193-
either False _ f2 = MkFormatOf f2
194-
195-
196-
public export
197-
orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
198-
orPure True f _ = f
199-
orPure False _ def = MkFormatOf (Pure def)
200-
201-
202-
public export
203-
orPure' : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
204-
orPure' True f _ = f
205-
orPure' False _ def = MkFormatOf (Pure def)

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Data.Vect
1212
import Fathom.Base
1313
import Fathom.Data.Iso
1414
import Fathom.Data.Refine
15+
import Fathom.Data.Sing
1516

1617

1718
public export
@@ -43,7 +44,7 @@ mutual
4344
data Format : Type where
4445
End : Format
4546
Fail : Format
46-
Pure : {A : Type} -> A -> Format
47+
Pure : {0 A : Type} -> A -> Format
4748
Skip : (f : Format) -> (def : Rep f) -> Format
4849
Repeat : Nat -> Format -> Format
4950
Bind : (f : Format) -> (Rep f -> Format) -> Format
@@ -57,7 +58,7 @@ mutual
5758
Rep Fail = Void
5859
Rep (Skip _ _) = Unit
5960
Rep (Repeat len f) = Vect len (Rep f)
60-
Rep (Pure x) = typeOf x
61+
Rep (Pure x) = Sing x
6162
Rep (Bind f1 f2) = (x : Rep f1 ** Rep (f2 x))
6263
Rep (Custom f) = f.Rep
6364

@@ -73,7 +74,7 @@ decode End [] = Just ((), [])
7374
decode End (_::_) = Nothing
7475
decode Fail _ = Nothing
7576
decode (Pure x) buffer =
76-
Just (x, buffer)
77+
Just (MkSing x, buffer)
7778
decode (Skip f _) buffer = do
7879
(x, buffer') <- decode f buffer
7980
Just ((), buffer')
@@ -93,7 +94,7 @@ decode (Custom f) buffer = f.decode buffer
9394
export
9495
encode : (f : Format) -> Encode (Rep f) ByteStream
9596
encode End () = Just []
96-
encode (Pure x) _ = Just []
97+
encode (Pure x) (MkSing _) = Just []
9798
encode (Skip f def) () = encode f def
9899
encode (Repeat Z f) [] = Just []
99100
encode (Repeat (S len) f) (x :: xs) =
@@ -110,7 +111,7 @@ encode (Custom f) x = f.encode x
110111
-- Support for do notation
111112
112113
public export
113-
pure : {A : Type} -> A -> Format
114+
pure : {0 A : Type} -> A -> Format
114115
pure = Pure
115116
116117
public export
@@ -214,25 +215,6 @@ toFormatOfEqIso = MkIso
214215
-- EXPERIMENTS --
215216
-----------------
216217
217-
218-
export
219-
either : (cond : Bool) -> (f1 : Format) -> (f2 : Format) -> FormatOf (if cond then Rep f1 else Rep f2)
220-
either True f1 _ = MkFormatOf f1
221-
either False _ f2 = MkFormatOf f2
222-
223-
224-
export
225-
orPure : {A : Type} -> (cond : Bool) -> FormatOf A -> (def : A) -> FormatOf A
226-
orPure True f _ = f
227-
orPure False _ def = MkFormatOf (Pure def)
228-
229-
230-
export
231-
orPure' : {A : Type} -> (cond : Bool) -> FormatOf A -> (def : A) -> FormatOf A
232-
orPure' True f _ = f
233-
orPure' False _ def = MkFormatOf (Pure def)
234-
235-
236218
-- Reproduction of difficulties in OpenType format
237219
238220
-- def flag = {
@@ -244,23 +226,24 @@ orPure' False _ def = MkFormatOf (Pure def)
244226
-- };
245227
flag : Format
246228
flag = do
247-
flag <- u8
248-
repeat <- case flag of
229+
id <- u8
230+
repeat <- case id of
249231
0 => u8
250-
_ => Pure {A = Nat} 0
232+
S n => Pure {A = Nat} 0
251233
Pure ()
252234
235+
253236
-- def simple_glyph = fun (number_of_contours : U16) => {
254237
-- ...
255238
-- let flag_repeat = fun (f : Repr flag) => f.repeat + (1 : U8),
256239
-- ...
257240
-- };
258241
simple_glyph : Format
259242
simple_glyph = do
260-
(flag ** repeat ** ()) <- flag
243+
flag <- flag
261244
let
262-
repeat' : Nat
263-
repeat' = case flag of
264-
0 => repeat
265-
repeat => ?todo_repeat
266-
Pure (repeat' + 1)
245+
repeat : Nat
246+
repeat = case flag of
247+
(0 ** repeat ** MkSing ()) => repeat
248+
(S n ** repeat ** MkSing ()) => repeat
249+
Pure (repeat + 1)

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

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,65 @@ public export
182182
public export
183183
u8 : Format
184184
u8 = MkFormat
185-
{ Rep = Bits8
186-
, decode = decodeU8
187-
, encode = encodeU8
185+
{ Rep = Nat
186+
, decode = map cast decodeU8
187+
, encode = encodeU8 . cast {to = Bits8}
188188
}
189189
190190
191191
public export
192192
u16Le : Format
193193
u16Le = MkFormat
194-
{ Rep = Bits16
195-
, decode = decodeU16 LE
196-
, encode = encodeU16 LE
194+
{ Rep = Nat
195+
, decode = map cast (decodeU16 LE)
196+
, encode = encodeU16 LE . cast {to = Bits16}
197197
}
198198
199199
200200
public export
201201
u16Be : Format
202202
u16Be = MkFormat
203-
{ Rep = Bits16
204-
, decode = decodeU16 BE
205-
, encode = encodeU16 BE
203+
{ Rep = Nat
204+
, decode = map cast (decodeU16 BE)
205+
, encode = encodeU16 BE . cast {to = Bits16}
206206
}
207+
208+
209+
-----------------
210+
-- EXPERIMENTS --
211+
-----------------
212+
213+
214+
-- Reproduction of difficulties in OpenType format
215+
216+
217+
-- def flag = {
218+
-- flag <- u8,
219+
-- repeat <- match ((u8_and flag 8) != (0 : U8)) {
220+
-- true => u8,
221+
-- false => succeed U8 0,
222+
-- },
223+
-- };
224+
flag : Format
225+
flag = do
226+
id <- u8
227+
repeat <- case id of
228+
0 => u8
229+
S _ => pure {A = Nat} 0
230+
pure ()
231+
232+
233+
(.repeat) : Record.flag.Rep -> Nat
234+
(.repeat) (0 ** repeat ** _) = repeat
235+
(.repeat) (S _ ** repeat ** _) = val repeat
236+
237+
238+
-- def simple_glyph = fun (number_of_contours : U16) => {
239+
-- ...
240+
-- let flag_repeat = fun (f : Repr flag) => f.repeat + (1 : U8),
241+
-- ...
242+
-- };
243+
simple_glyph : Format
244+
simple_glyph = do
245+
flag <- flag
246+
pure (flag.repeat + 1)

experiments/idris/src/Playground.idr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,25 @@ mutual
101101
indexedToIndRecFormatOf (Bind f1 f2) with (indexedToIndRecFormatOf f1)
102102
_ | MkFormatOf f1' =
103103
?indexedToIndRecFormatOfBind
104+
105+
106+
-- Reproduction of difficulties in OpenType format, drawing parallels to
107+
-- Tarski-style universes.
108+
109+
repeatWithId : Nat -> Type
110+
repeatWithId 0 = Nat
111+
repeatWithId (S _) = Sing {A = Nat} 0
112+
113+
record Flag where
114+
constructor MkFlag
115+
id : Nat
116+
repeat : case id of
117+
0 => Nat
118+
(S n) => Sing {A = Nat} 0
119+
120+
record SimpleGlyph where
121+
constructor MkSimpleGlyph
122+
flag : Flag
123+
flag_repeat : Sing {A = Nat} (case flag of
124+
MkFlag 0 repeat => repeat
125+
MkFlag (S n) repeat => val repeat)

0 commit comments

Comments
 (0)