Source

dumbstreaming / dumbstreaming_it.ml

Full commit
  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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
module type IT_TYPE
 =
  sig
    module It_IO
     :
      sig
        type m +'a;
        value return : 'a -> m 'a;
        value bind_rev : m 'a -> ('a -> m 'b) -> m 'b;
        value error : exn -> m 'a;
      end
    ;

    type stream 'el;
    value empty_stream : stream 'el;

    type err_msg = exn;

    type iteratee 'el 'a =
      [ IE_done of 'a
      | IE_cont of option err_msg and
          ( stream 'el
            -> It_IO.m (iteratee 'el 'a * stream 'el)
          )
      ]
    ;

    value return : 'a -> iteratee 'el 'a
    ;

    value throw_err : err_msg -> iteratee 'el 'a
    ;

    value peek : iteratee 'el (option 'el)
    ;

    value bindI :
      iteratee 'el 'a -> ('a -> iteratee 'el 'b) -> iteratee 'el 'b
    ;

    value drop_while : ('el -> bool) -> iteratee 'el unit
    ;

    value break_chars : (char -> bool) -> iteratee char string
    ;

    type enumeratee 'elo 'eli 'a = 
      iteratee 'eli 'a -> iteratee 'elo (iteratee 'eli 'a)
    ;

    value limit : int -> enumeratee 'el 'el 'a
    ;

  end
;


type ntotal = int
 and npart = int
 and nbytes = int
;


module Make (I : IT_TYPE)
 :
  sig
    (* None of EOF *)

    value read : (ntotal -> npart -> nbytes -> I.iteratee char 'i)
              -> I.iteratee 'i 'a
              -> I.iteratee char (option 'a)
    ;
  end
 =
  struct

    module It_add
     :
      sig
        value itlist_anyresult_lasterror
         : list (I.iteratee 'el 'a) -> I.iteratee 'el 'a
        ;

        (* leading zeroes accepted *)
        value read_uint30 : I.iteratee char int
        ;

        (* leading zeroes = error *)
        value read_uint30nz : I.iteratee char int
        ;

      end
     =
      struct

        exception Itlist_empty;

        module IO = I.It_IO;

        value ( & ) f x = f x;

        value ( >>% ) = IO.bind_rev;

        open I;
        value ( >>= ) = bindI;
 
        value itlist_step_firstresult_lasterror
          (lst : list (iteratee 'el 'a))
          (s : stream 'el)
         :
          IO.m [= `First_result of (iteratee 'el 'a * stream 'el)
               |  `Last_error of err_msg
               |  `Cont of list (iteratee 'el 'a)
               ]
         =
          let rec loop lasterropt acc lst =
            match lst with
            [ [] ->
                if acc = []
                then
                  match lasterropt with
                  [ None -> assert False
                  | Some err -> IO.return & `Last_error err
                  ]
                else
                  IO.return & `Cont (List.rev acc)
            | [hd :: tl] ->
                match hd with
                [ (IE_done _) as it -> IO.return & `First_result (it, s)
                | IE_cont ((Some _) as someerr) _ ->
                    loop someerr acc tl
                | IE_cont None k ->
                    k s >>% fun
                    [ (IE_done _, _) as r ->
                        IO.return & `First_result r
                    | (IE_cont (Some _ as someerr) _, _) ->
                        loop someerr acc tl
                    | ((IE_cont None _ as hd'), _s) ->
                        loop lasterropt [hd' :: acc] tl
                    ]
                ]
            ]
          in
            if lst = []
            then IO.return & `Last_error Itlist_empty
            else loop None [] lst
        ;

        value get_any_done lst =
          loop lst
          where rec loop lst =
            match lst with
            [ [ ((IE_done _) as x) :: _ ] -> Some x
            | [] -> None
            | [ (IE_cont _ _) :: _ ] -> loop lst
            ]
        ;

        value ie_cont k = IE_cont None k
        ;

        value ie_contM k = IO.return (IE_cont None k, empty_stream)
        ;


        value itlist_anyresult_lasterror
          (lst : list (iteratee 'el 'a))
         :
          iteratee 'el 'a
         =
          match get_any_done lst with
          [ Some x -> x
          | None -> ie_cont & step lst
              where rec step lst s =
                itlist_step_firstresult_lasterror lst s >>% fun
                [ `First_result r -> IO.return r
                | `Last_error e -> IO.error e
                | `Cont lst -> ie_contM & step lst
                ]
          ]
        ;


        module type SIGNED_INT
         =
          sig
            type t;
            value max_int : t;
            (* min_int = -max_int - 1 *)
            value of_int : int -> t;
            value to_int : t -> int;

            (* may overflow silently: *)
            value ( + ) : t -> t -> t;
            value ( - ) : t -> t -> t;
            value ( * ) : t -> t -> t;

            (* should truncate towards zero: *)
            value ( / ) : t -> t -> t;

            value ( <? ) : t -> t -> bool;
            value ( =? ) : t -> t -> bool;
          end
        ;

        module SInt_T : SIGNED_INT with type t = int
         =
          struct
            type t = int;
            value max_int = Pervasives.max_int;
            value of_int x = x;
            value to_int x = x;
            value ( + ) = Pervasives.( + );
            value ( - ) = Pervasives.( - );
            value ( * ) = Pervasives.( * );
            value ( / ) = Pervasives.( / );
            value ( <? ) = Pervasives.( < );
            value ( =? ) = Pervasives.( == );
          end
        ;

        exception SInt_overflow;
        exception SInt_not_a_number of string;

        module SInt_F (S : SIGNED_INT)
         :
          sig
            (* value digits : S.t -> int; *)

            value read_unsigned_noleadingzeroes : iteratee char S.t;
          end
         =
          struct
            open S;

            value zero = of_int 0
            ;

            value ( ~- ) n = zero - n
              and ( >? ) a b = not (a <? b) && not (a =? b)
              and ( >=? ) a b = not (a <? b)
              and ( <>? ) a b = not (a =? b)
            ;

            value ten = of_int 10
            ;

            value rec digits n =
              Pervasives.( + )
                1
                (let n' = n / ten in
                 if n' =? zero
                 then 0
                 else digits n'
                )
            ;

            value rem a b = (a - (a / b) * b)
            ;

            module P = Pervasives;

            value string_reverse_inplace str =
              let len = String.length str in
              let len1 = P.( - ) len 1 in
              let len2 = P.( / ) len 2 in
              ( for i = 0 to len2
                do
                  let j = P.( - ) len1 i in
                  let tmp = str.[i] in
                  ( str.[i] := str.[j]
                  ; str.[j] := tmp
                  )
                done
              ; str
              )
            ;

            (* value min_int_digits = digits min_int; *)
            value max_int_digits = digits max_int;

            value to_base b n =
              let buf = Buffer.create max_int_digits in
              let intb = to_int b in
              let digit n =
                let n = to_int n in
                ( assert (n >= 0 && n < intb)
                ; assert (n < 10)
                ; Char.chr (P.( + ) n (Char.code '0'))
                ) in
              let rec loop n =
                let d = rem n b in
                let () = Buffer.add_char buf (digit d) in
                let n' = n / b in
                if n' =? zero
                then string_reverse_inplace (Buffer.contents buf)
                else loop n'
              in
                loop n
            ;

            value to_dec = to_base ten
            ;

            value max_int_dec = to_dec max_int;

            value from_base_neg b maxstr str =
              let rec loop acc i =
                if i < 0
                then acc
                else
                  let digit ch =
                    if (ch >= '0' && ch <= '9')
                    then
                      let d = of_int (P.( - ) (Char.code ch) (Char.code '0')) in
                      if d >=? b
                      then raise (SInt_not_a_number "")
                      else d
                    else assert False
                  in
                  let ch = str.[i] in
                  let acc' = acc * b - digit ch in
                  loop acc' (P.( - ) i 1)
              in
                let len = String.length str in
                if len = 0
                then `Empty
                else
                let maxlen = String.length maxstr in
                if len > maxlen
                then `Overflow
                else if len = maxlen && str > maxstr
                then `Overflow
                else
                  try
                    `Ok (loop zero (P.( - ) len 1))
                  with
                  [ SInt_not_a_number _ -> `Not_a_number ]
            ;

            value from_dec_neg = from_base_neg ten max_int_dec
            ;

            value read_unsigned_noleadingzeroes
             :
              iteratee char S.t
             =
              let is_digit c = (c <= '9' && c >= '0') in
              (I.limit max_int_digits & I.break_chars is_digit) >>= fun
              [ I.IE_done str ->
                  let () = assert (String.length str <= max_int_digits) in
                  peek >>= fun optch ->
                  let next_is_digit = match optch with
                    [ None -> False | Some ch -> is_digit ch ] in
                  if next_is_digit
                  then I.throw_err SInt_overflow
                  else
                    match from_dec_neg str with
                    [ `Empty | `Not_a_number -> I.throw_err (SInt_not_a_number
                        "read_unsigned_noleadingzeroes")
                    | `Ok r -> I.return (-r)
                    | `Overflow -> I.throw_err SInt_overflow
                    ]
              | I.IE_cont (Some e) _ -> I.throw_err e
              | I.IE_cont None _ -> I.throw_err SInt_overflow
              ]
            ;

          end
        ;

        module SInt = SInt_F(SInt_T)
        ;


        (* leading zeroes = error *)
        value read_uint30nz
         : I.iteratee char int
         =
          SInt.read_unsigned_noleadingzeroes
        ;

        value drop_zeroes = I.drop_while ( ( = ) '0' )
        ;

        value read_uint30 =
          drop_zeroes >>= fun () ->
          read_uint30nz
        ;

      end
    ;

    open It_add
    ;


    value itpair_anyresult_lasterror it1 it2 =
      itlist_anyresult_lasterror [it1; it2]
    ;

    value read_packet _get_piece_it _combine_it _firstchar =
      failwith "kva"
    ;



    value read _get_piece_it _combine_it =
      
(*
      (some /* mapI */ read_packet get_piece_it combine_it)
      /* itpair_anyresult_lasterror */
      (read_eos >>= fun () -> None)
*)
      failwith "kva"

    ;

  end
;