Repository navigation
Expand file tree
/
Copy pathBERT.lean
More file actions
296 lines (245 loc) · 9.77 KB
/
Copy pathBERT.lean
File metadata and controls
296 lines (245 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import N2O.Data.Bytes
import N2O.Data.Sum
import N2O.Data.Put
import N2O.Data.Parser
def Char.isAscii (c : Char) : Bool :=
c.val ≤ 127
def mapM {m : Type → Type} [Monad m] {α β : Type} (f : α → m β) : List α → m (List β)
| [] => pure []
| x :: xs => do
let ys ← mapM f xs;
let y ← f x;
pure (y :: ys)
abbrev Dict (α β : Type) := List (α × β)
inductive Term
| byte : UInt8 → Term
-- there is now no Int32 in Lean
| int : UInt32 → Term
--| float : Float → Term
| atom : String → Term
| tuple : List Term → Term
| string : String → Term
| list : List Term → Term
| binary : ByteArray → Term
| bigint : Int → Term
| dict : List (Term × Term) → Term
partial def showTerm : Term → String
| Term.byte x => toString x
| Term.int x => toString x
| Term.atom (String.mk []) => ""
| Term.atom s@(String.mk $ x :: xs) =>
if x.isLower then s
else "'" ++ s ++ "'"
| Term.tuple ts => "{" ++ String.intercalate ", " (List.map showTerm ts) ++ "}"
| Term.string s => "\"" ++ s ++ "\""
| Term.list ts => "[" ++ String.intercalate ", " (List.map showTerm ts) ++ "]"
| Term.binary s => "<<" ++ String.intercalate ", " (List.map toString s.toList) ++ ">>"
| Term.bigint x => toString x
| Term.dict x =>
let printPair := λ (pair : Term × Term) =>
"{" ++ showTerm pair.1 ++ ", " ++ showTerm pair.2 ++ "}";
"[" ++ String.intercalate ", " (List.map printPair x) ++ "]"
instance : ToString Term := ⟨showTerm⟩
class BERT (α : Type) :=
(toTerm : α → Term)
(fromTerm : Term → Sum String α)
instance Term.BERT : BERT Term :=
{ toTerm := id, fromTerm := Sum.inr }
instance Int.BERT : BERT UInt32 :=
{ toTerm := Term.int,
fromTerm := λ t => match t with
| Term.int val => Sum.inr val
| _ => Sum.inl "invalid integer type" }
instance Integer.BERT : BERT Int :=
{ toTerm := Term.bigint,
fromTerm := λ t => match t with
| Term.bigint x => Sum.inr x
| _ => Sum.inl "invalid integer type" }
instance String.BERT : BERT String :=
{ toTerm := Term.string,
fromTerm := λ t => match t with
| Term.string x => Sum.inr x
| Term.atom x => Sum.inr x
| _ => Sum.inl "invalid string type" }
section
variable {α : Type} [BERT α]
def listToTerm := Term.list ∘ @List.map α Term BERT.toTerm
def listFromTerm : Term → Sum String (List α)
| Term.list xs => mapM BERT.fromTerm xs
| _ => Sum.inl "given BERT term is not a list"
instance List.BERT : BERT (List α) := ⟨listToTerm, listFromTerm⟩
end
section
variable {α β : Type} [BERT α] [BERT β]
def dictToTerm :=
Term.dict ∘ @List.map (α × β) (Term × Term) (Prod.map BERT.toTerm BERT.toTerm)
def termFromPair (p : Term × Term) : Sum String (α × β) := do
let fst ← BERT.fromTerm p.1;
let snd ← BERT.fromTerm p.2;
pure (fst, snd)
def dictFromTerm : Term → Sum String (Dict α β)
| Term.dict xs => mapM termFromPair xs
| _ => Sum.inl "given BERT term is not a dictionary"
instance Dict.BERT : BERT (Dict α β) := ⟨dictToTerm, dictFromTerm⟩
end
section
variable {α β : Type} [BERT α] [BERT β]
def pairToTerm : α × β → Term :=
λ (a, b) => Term.tuple [BERT.toTerm a, BERT.toTerm b]
def pairFromTerm : Term → Sum String (α × β)
| Term.tuple [a, b] => do
let x ← BERT.fromTerm a;
let y ← BERT.fromTerm b;
pure (x, y)
| _ => Sum.inl "given BERT term is not a cartesian product"
instance Prod.BERT : BERT (α × β) := ⟨pairToTerm, pairFromTerm⟩
end
def word : ByteParser UInt16 := do
let res ← Parser.count Parser.byte 2;
match res with
| (a, b, _) =>
let a' := UInt8.shiftl16 a (8 * 1);
let b' := UInt8.shiftl16 b (8 * 0);
pure (a' + b')
def dword : ByteParser UInt32 := do
let res ← Parser.count Parser.byte 4;
match res with
| (a, b, c, d, _) =>
let a' := UInt8.shiftl32 a (8 * 3);
let b' := UInt8.shiftl32 b (8 * 2);
let c' := UInt8.shiftl32 c (8 * 1);
let d' := UInt8.shiftl32 d (8 * 0);
pure (a' + b' + c' + d')
-- decode
def readByte : ByteParser Term :=
do Parser.tok 97; let val ← Parser.byte; pure (Term.byte val)
def readDword : ByteParser Term :=
do Parser.tok 98; let res ← dword; pure (Term.int res)
def readASCIIString {α : Type} (p : ByteParser α)
(toNat : α → Nat) (tok : UInt8) : ByteParser String := do
Parser.tok tok; let N ← p;
let chars ← Parser.count ByteParser.ch (toNat N);
pure chars.toList.asString
def readAtom : ByteParser Term :=
Parser.decorateError "<atom>"
(Term.atom <$> readASCIIString word UInt16.toNat 100)
def readSmallAtom : ByteParser Term :=
Parser.decorateError "<small atom>"
(Term.atom <$> readASCIIString Parser.byte UInt8.toNat 115)
def readUTF8String {α : Type} (p : ByteParser α)
(toNat : α → Nat) (tok : UInt8) : ByteParser String :=
do Parser.tok tok; let N ← p; ByteParser.utf8.stringWithLength (toNat N)
def readUTF8Atom : ByteParser Term :=
Parser.decorateError "<UTF-8 atom>"
(Term.atom <$> readUTF8String word UInt16.toNat 118)
def readUTF8SmallAtom : ByteParser Term :=
Parser.decorateError "<UTF-8 small atom>"
(Term.atom <$> readUTF8String Parser.byte UInt8.toNat 119)
def readString : ByteParser Term :=
Parser.decorateError "<UTF-8 string>"
(Term.string <$> readUTF8String word UInt16.toNat 107)
def readTuple (readTerm : ByteParser Term) : ByteParser Term := do
Parser.tok 104; let N ← Parser.byte;
let elems ← Parser.count readTerm N.toNat;
pure (Term.tuple elems.toList)
def readLargeTuple (readTerm : ByteParser Term) : ByteParser Term := do
Parser.tok 105; let N ← dword;
let elems ← Parser.count readTerm N.toNat;
pure (Term.tuple elems.toList)
def readNil : ByteParser Term := do Parser.tok 106; pure (Term.list [])
def readList (readTerm : ByteParser Term) : ByteParser Term := do
Parser.tok 108; let N ← dword;
let elems ← Parser.count readTerm N.toNat;
let _ ← optional readNil;
pure (Term.list elems.toList)
def readBinary : ByteParser Term := do
Parser.tok 109; let N ← dword;
let elems ← Parser.count Parser.byte N.toNat;
pure (Term.binary elems.toList.toByteArray)
def IsMinus : ByteParser Bool :=
(do Parser.tok 0; pure false) <|>
(do Parser.tok 1; pure true)
def readBignum' {α : Type}
(p : ByteParser α) (toNat : α → Nat) (tok : UInt8) :
ByteParser Term := do
Parser.tok 110; let n ← p;
let isMinus ← IsMinus; let d ← Parser.count Parser.byte (toNat n);
let x := Int.ofNat
(List.foldl Nat.add 0 $
List.map (λ (pair : Nat × UInt8) =>
pair.2.toNat * (256 ^ pair.1))
d.toList.enum);
pure (Term.bigint $ if isMinus then -x else x)
def readSmallBignum := readBignum' Parser.byte UInt8.toNat 110
def readLargeBignum := readBignum' dword UInt32.toNat 111
def readDict (readTerm : ByteParser Term) : ByteParser Term := do
Parser.tok 116; let L ← dword;
let pairs ← Parser.count (Prod.mk <$> readTerm <*> readTerm) L.toNat;
pure (Term.dict pairs.toList)
def readTerm' (readTerm : ByteParser Term) : ByteParser Term :=
readByte <|> readDword <|> readAtom <|>
readUTF8SmallAtom <|> readUTF8Atom <|>
readTuple readTerm <|> readLargeTuple readTerm <|>
readList readTerm <|> readBinary <|> readSmallAtom <|>
readSmallBignum <|> readLargeBignum <|> readString <|>
readDict readTerm
def readTerm : ByteParser Term := do Parser.tok 131; Parser.fix readTerm'
-- encode
partial def natToBytesAux (x : Nat) (buff : ByteArray) : ByteArray :=
if x > 256 then natToBytesAux (x / 256) (buff.push (UInt8.ofNat $ x % 256))
else buff.push (UInt8.ofNat x)
def Nat.toBytes (x : Nat) : ByteArray :=
natToBytesAux x ByteArray.empty
def writeBigint (x : Int) : Put :=
let enc := Nat.toBytes x.natAbs;
let sign := if x > 0 then Put.byte 0 else Put.byte 1;
if enc.size < UInt8.size then
Put.byte 110 >> Put.byte enc.size >> sign >> Put.tell enc
else if enc.size < UInt32.size then
Put.byte 111 >> Put.dword enc.size >> sign >> Put.tell enc
else Put.fail "BERT integer too big"
def writeAtom (x : String) : Put :=
if x.utf8ByteSize < UInt8.size then
if x.all Char.isAscii then
Put.byte 115 >> Put.byte x.length >> Put.tell x.bytes
else Put.byte 119 >> Put.byte x.utf8ByteSize >> Put.unicode x
else if x.utf8ByteSize < UInt16.size then
if x.all Char.isAscii then
Put.byte 100 >> Put.word x.length >> Put.tell x.bytes
else Put.byte 118 >> Put.word x.utf8ByteSize >> Put.unicode x
else Put.fail "BERT atom too long (≥ 65536)"
def writeString (x : String) : Put :=
if x.utf8ByteSize < UInt16.size then
Put.byte 107 >> Put.word x.utf8ByteSize >> Put.unicode x
else Put.fail "BERT bytelist too long (≥ 65536)"
partial def writeTerm' : Term → Put
| Term.byte x => Put.byte 97 >> Put.raw x
| Term.int x => Put.byte 98 >> Put.tell x.toBytes
| Term.atom s => writeAtom s
| Term.tuple x =>
let tuple := List.foldr (λ τ fn => writeTerm' τ >> fn) Put.nope;
if x.length < UInt8.size then
Put.byte 104 >> Put.byte x.length >> tuple x
else if x.length < UInt32.size then
Put.byte 105 >> Put.dword x.length >> tuple x
else Put.fail "BERT tuple too long (≥ 4294967296)"
| Term.list x =>
if x.length < UInt32.size then
Put.byte 108 >> Put.dword x.length >>
List.foldr (λ τ fn => writeTerm' τ >> fn) (Put.byte 106) x
else Put.fail "BERT list too long (≥ 4294967296)"
| Term.binary x =>
if x.size < UInt32.size then
Put.byte 109 >> Put.dword x.size >> Put.tell x
else Put.fail "BERT binary too long (≥ 4294967296)"
| Term.bigint x => writeBigint x
| Term.string s => writeString s
| Term.dict x =>
let writePair :=
λ (x : Term × Term) => writeTerm' x.1 >> writeTerm' x.2;
if x.length < UInt32.size then
Put.byte 116 >> Put.dword x.length >>
List.foldr (λ π fn => writePair π >> fn) Put.nope x
else Put.fail "BERT dictionary too long (≥ 4294967296)"
def writeTerm (x : Term) : Sum String ByteArray :=
Put.run (Put.byte 131 >> writeTerm' x)