···2323 let w = 40 in
2424 ToBuffer.pretty 1.0 w b d;
2525 let text = Bytes.to_string (Buffer.to_bytes b) in
2626- let ws = Str.regexp "[ \t\n\r]*" in
2727- (* Printf.printf "doc2{\n%s\n}%!" text; *)
2828- let del_ws = Str.global_replace ws "" in
2929- (* Printf.printf "[%s] = [%s]\n%!" (del_ws s) (del_ws text);*)
3030- Str.split (Str.regexp "\n") text |> List.iter (fun s ->
3131- let mspace = Str.regexp "[^ ] " in
2626+ let ws = Re.(compile (rep (set " \t\n\r"))) in
2727+ let del_ws s = Re.replace_string ws ~by:"" s in
2828+ let mspace = Re.(compile (seq [compl [char ' ']; char ' '])) in
2929+ String.split_on_char '\n' text |> List.iter (fun s ->
3230 if String.length s > w then
3333- match Str.search_forward mspace s w with
3434- | _ -> assert false
3535- | exception Not_found -> ());
3131+ if Re.execp ~pos:w mspace s then assert false);
3632 check_eq (del_ws s) (del_ws text)
37333834let () =
···1010 | List : 'a ty -> 'a list ty
11111212let rec pp_ty : type a . _ -> a ty -> unit = fun ppf ->
1313- let printf fmt = Format.fprintf ppf fmt in
1413 function
1515- | Int -> printf "Int"
1616- | Bool -> printf "Bool"
1717- | Prod(ta, tb) -> printf "Prod(%a,%a)" pp_ty ta pp_ty tb
1818- | List t -> printf "List(%a)" pp_ty t
1414+ | Int -> Fmt.pf ppf "Int"
1515+ | Bool -> Fmt.pf ppf "Bool"
1616+ | Prod(ta, tb) -> Fmt.pf ppf "Prod(%a,%a)" pp_ty ta pp_ty tb
1717+ | List t -> Fmt.pf ppf "List(%a)" pp_ty t
19182019let rec serialize : type a . a ty -> a -> data = function
2120 | Int -> fun n -> Datum (string_of_int n)
···2524 | List t -> fun vs ->
2625 Block("list", List.map (serialize t) vs)
27262828-let rec deserialize : type a . a ty -> data -> a = function[@warning "-8"]
2929- | Int -> fun (Datum s) -> int_of_string s
3030- | Bool -> fun (Datum s) -> bool_of_string s
3131- | Prod (ta, tb) -> fun (Block("pair", [sa; sb])) ->
3232- (deserialize ta sa, deserialize tb sb)
3333- | List t -> fun (Block("list", ss)) ->
3434- List.map (deserialize t) ss
2727+let rec deserialize : type a . a ty -> data -> a = function
2828+ | Int -> (function
2929+ | Datum s -> int_of_string s
3030+ | Block _ -> failwith "expected Datum for Int")
3131+ | Bool -> (function
3232+ | Datum s -> bool_of_string s
3333+ | Block _ -> failwith "expected Datum for Bool")
3434+ | Prod (ta, tb) -> (function
3535+ | Block ("pair", [sa; sb]) -> (deserialize ta sa, deserialize tb sb)
3636+ | _ -> failwith "expected Block(pair, [_;_]) for Prod")
3737+ | List t -> (function
3838+ | Block ("list", ss) -> List.map (deserialize t) ss
3939+ | _ -> failwith "expected Block(list, _) for List")