(*--------------------------------------------------------------------------- Copyright (c) 2015 Daniel C. Bünzli. All rights reserved. Distributed under the BSD3 license, see license at the end of the file. %%NAME%% release %%VERSION%% ---------------------------------------------------------------------------*) (* Simple generators according to: Kiselyov, Peyton-Jones, Sabry Lazy v. Yield: Incremental, Linear Pretty-printing Springer's LNCS 7705, pp. 190-206 http://okmij.org/ftp/continuations/PPYield/ Rewrite of http://okmij.org/ftp/continuations/PPYield/simple_gen.ml Composition is not statically type checked. *) module G : sig type 'a gen = unit -> 'a type producer = unit gen type consumer = exn -> unit type 'a transducer = 'a gen -> 'a gen val yield : exn -> unit val iter : consumer -> producer -> unit val map : (exn -> exn) -> unit transducer val fold : ('a -> exn -> 'a) -> 'a -> producer -> 'a gen end = struct type 'a gen = unit -> 'a type producer = unit gen type consumer = exn -> unit type 'a transducer = 'a gen -> 'a gen let yield_handler = ref (fun _ -> invalid_arg "no yield handler") let yield v = !yield_handler v let iter consumer producer = let old_handler = !yield_handler in let rec new_handler v = try yield_handler := old_handler; consumer v; yield_handler := new_handler; with | e -> yield_handler := new_handler; raise e in try yield_handler := new_handler; let v = producer () in yield_handler := old_handler; v with | e -> yield_handler := old_handler; raise e let map f gen = fun () -> iter (fun x -> yield (f x)) gen let fold f acc producer = fun () -> let acc = ref acc in iter (fun v -> acc := f !acc v) producer; !acc end module P : sig exception Eline of string val lines_of_file : string -> unit G.gen exception Echar of char val chars_of_file : string -> unit G.gen end = struct let close_in ic = Printf.printf "trace: closing!%!\n"; close_in ic let with_inf fname loop = let ic = open_in fname in let v = try loop ic with e -> close_in ic; raise e in close_in ic; v exception Eline of string let lines_of_file fname = fun () -> let rec loop ic = match (try Some (input_line ic) with End_of_file -> None) with | None -> () | Some line -> G.yield (Eline line); loop ic in with_inf fname loop exception Echar of char let chars_of_file fname = fun () -> let rec loop ic = match (try Some (input_char ic) with End_of_file -> None) with | None -> () | Some c -> G.yield (Echar c); loop ic in with_inf fname loop end module C : sig val output_char : exn -> unit val output_line : exn -> unit end = struct let output_char = function | P.Echar c -> print_char c | e -> raise e let output_line = function | P.Eline l -> print_endline l | e -> raise e end let test_file = "/tmp/testf.txt" let make_test = let h = open_out test_file in output_string h "2\n3\n5\n7\n11\n13\nINVALID"; close_out h; Printf.printf "\nWrote test file %s\n" test_file let lines_of_char : unit G.transducer = fun gen () -> let tr b = function | P.Echar '\n' -> Printf.printf "trace: line %S\n%!" (Buffer.contents b); G.yield (P.Eline (Buffer.contents b)); Buffer.clear b; b | P.Echar c -> Buffer.add_char b c; b | e -> raise e in let eog = function | b when Buffer.length b = 0 -> () | b -> G.yield (P.Eline (Buffer.contents b)); () in eog (G.fold tr (Buffer.create 80) gen ()) let cat fname = G.iter C.output_char (P.chars_of_file fname) let cat_lines fname = G.iter C.output_line (lines_of_char (P.chars_of_file fname)) exception Ebool of bool exception Exists let exists gen = let is_true = function | Ebool true -> raise Exists | Ebool false -> () | e -> raise e in try G.iter is_true gen; false with Exists -> true let any pred gen = exists (G.map (fun v -> Ebool (pred v)) gen) exception Eint of int let t2_gen () = any (function Eint x -> x > 3 | e -> raise e) (G.map (function P.Eline s -> Eint (int_of_string s) | e -> raise e) (lines_of_char (P.chars_of_file test_file))) let () = Printf.printf "\nCat by chars\n"; cat test_file; Printf.printf "\nCat by lines\n"; cat_lines test_file; assert (t2_gen ()); () (*--------------------------------------------------------------------------- Copyright (c) 2015 Daniel C. Bünzli. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Daniel C. Bünzli nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------------*)