src/Pure/General/source.ML
author wenzelm
Thu, 31 May 2007 01:25:24 +0200
changeset 23139 aa899bce7c3b
parent 20739 8df08902da6f
child 23661 b3f05bc680b6
permissions -rw-r--r--
TextIO.inputLine: use present SML B library version;

(*  Title:      Pure/General/source.ML
    ID:         $Id$
    Author:     Markus Wenzel, TU Muenchen

Coalgebraic data sources -- efficient purely functional input streams.
*)

signature SOURCE =
sig
  type ('a, 'b) source
  val default_prompt: string
  val set_prompt: string -> ('a, 'b) source -> ('a, 'b) source
  val get: ('a, 'b) source -> 'a list * ('a, 'b) source
  val unget: 'a list * ('a, 'b) source -> ('a, 'b) source
  val get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option
  val exhaust: ('a, 'b) source -> 'a list
  val map_filter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c) source) source
  val filter: ('a -> bool) -> ('a, 'b) source -> ('a, ('a, 'b) source) source
  val of_list: 'a list -> ('a, 'a list) source
  val of_string: string -> (string, string list) source
  val exhausted: ('a, 'b) source -> ('a, 'a list) source
  val of_stream: TextIO.instream -> TextIO.outstream -> (string, unit) source
  val tty: (string, unit) source
  val source': 'a -> 'b * ('b -> bool) -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
    ('a * 'b list -> 'c list * ('a * 'b list)) option ->
    ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
  val source: 'a * ('a -> bool) -> ('a list -> 'b list * 'a list) ->
    ('a list -> 'b list * 'a list) option ->
    ('a, 'd) source -> ('b, ('a, 'd) source) source
end;

structure Source: SOURCE =
struct


(** datatype source **)

datatype ('a, 'b) source =
  Source of
   {buffer: 'a list,
    info: 'b,
    prompt: string,
    drain: string -> 'b -> 'a list * 'b};

fun make_source buffer info prompt drain =
  Source {buffer = buffer, info = info, prompt = prompt, drain = drain};


(* prompt *)

val default_prompt = "> ";

fun set_prompt prompt (Source {buffer, info, prompt = _, drain}) =
  make_source buffer info prompt drain;


(* get / unget *)

fun get (Source {buffer = [], info, prompt, drain}) =
      let val (xs, info') = drain prompt info
      in (xs, make_source [] info' prompt drain) end
  | get (Source {buffer, info, prompt, drain}) =
      (buffer, make_source [] info prompt drain);

fun unget (xs, Source {buffer, info, prompt, drain}) =
  make_source (xs @ buffer) info prompt drain;


(* variations on get *)

fun get_prompt prompt src = get (set_prompt prompt src);

fun get_single src =
  (case get src of
    ([], _) => NONE
  | (x :: xs, src') => SOME (x, unget (xs, src')));

fun exhaust src =
  (case get src of
    ([], _) => []
  | (xs, src') => xs @ exhaust src');


(* (map)filter *)

fun drain_map_filter f prompt src =
  let
    val (xs, src') = get_prompt prompt src;
    val xs' = map_filter f xs;
  in
    if null xs orelse not (null xs') then (xs', src')
    else drain_map_filter f prompt src'
  end;

fun map_filter f src = make_source [] src default_prompt (drain_map_filter f);
fun filter pred = map_filter (fn x => if pred x then SOME x else NONE);



(** build sources **)

(* list source *)

fun of_list xs = make_source [] xs default_prompt (fn _ => fn xs => (xs, []));
val of_string = of_list o explode;

fun exhausted src = of_list (exhaust src);


(* stream source *)

fun slurp_input instream =
  let
    fun slurp () =
      (case TextIO.canInput (instream, 1) handle IO.Io _ => NONE of
        NONE => []
      | SOME 0 => []
      | SOME _ => TextIO.input instream :: slurp ());
  in maps explode (slurp ()) end;

fun drain_stream instream outstream prompt () =
  let val input = slurp_input instream in
    if exists (fn c => c = "\n") input then (input, ())
    else
      (TextIO.output (outstream, prompt);
        TextIO.flushOut outstream;
        (case TextIO.inputLine instream of
          SOME line => (input @ explode line, ())
        | NONE => (input, ())))
  end;

fun of_stream instream outstream =
  make_source [] () default_prompt (drain_stream instream outstream);

val tty = of_stream TextIO.stdIn TextIO.stdOut;



(** compose sources **)

fun drain_source source stopper scan recover prompt src =
  source prompt get_prompt unget stopper scan recover src;


(* state-based *)

fun source' init_state stopper scan recover src =
  make_source [] (init_state, src) default_prompt
    (drain_source Scan.source' stopper scan recover);


(* non state-based *)

fun source stopper scan recover src =
  make_source [] src default_prompt
    (drain_source Scan.source stopper scan recover);


end;