src/Pure/General/source.ML
author wenzelm
Mon Jul 09 11:44:22 2007 +0200 (2007-07-09)
changeset 23661 b3f05bc680b6
parent 23139 aa899bce7c3b
child 23675 2d618c190466
permissions -rw-r--r--
prompt: plain string, not output;
     1 (*  Title:      Pure/General/source.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Coalgebraic data sources -- efficient purely functional input streams.
     6 *)
     7 
     8 signature SOURCE =
     9 sig
    10   type ('a, 'b) source
    11   val default_prompt: string
    12   val set_prompt: string -> ('a, 'b) source -> ('a, 'b) source
    13   val get: ('a, 'b) source -> 'a list * ('a, 'b) source
    14   val unget: 'a list * ('a, 'b) source -> ('a, 'b) source
    15   val get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option
    16   val exhaust: ('a, 'b) source -> 'a list
    17   val map_filter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c) source) source
    18   val filter: ('a -> bool) -> ('a, 'b) source -> ('a, ('a, 'b) source) source
    19   val of_list: 'a list -> ('a, 'a list) source
    20   val of_string: string -> (string, string list) source
    21   val exhausted: ('a, 'b) source -> ('a, 'a list) source
    22   val of_stream: TextIO.instream -> TextIO.outstream -> (string, unit) source
    23   val tty: (string, unit) source
    24   val source': 'a -> 'b * ('b -> bool) -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
    25     ('a * 'b list -> 'c list * ('a * 'b list)) option ->
    26     ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
    27   val source: 'a * ('a -> bool) -> ('a list -> 'b list * 'a list) ->
    28     ('a list -> 'b list * 'a list) option ->
    29     ('a, 'd) source -> ('b, ('a, 'd) source) source
    30 end;
    31 
    32 structure Source: SOURCE =
    33 struct
    34 
    35 
    36 (** datatype source **)
    37 
    38 datatype ('a, 'b) source =
    39   Source of
    40    {buffer: 'a list,
    41     info: 'b,
    42     prompt: string,
    43     drain: string -> 'b -> 'a list * 'b};
    44 
    45 fun make_source buffer info prompt drain =
    46   Source {buffer = buffer, info = info, prompt = prompt, drain = drain};
    47 
    48 
    49 (* prompt *)
    50 
    51 val default_prompt = "> ";
    52 
    53 fun set_prompt prompt (Source {buffer, info, prompt = _, drain}) =
    54   make_source buffer info prompt drain;
    55 
    56 
    57 (* get / unget *)
    58 
    59 fun get (Source {buffer = [], info, prompt, drain}) =
    60       let val (xs, info') = drain prompt info
    61       in (xs, make_source [] info' prompt drain) end
    62   | get (Source {buffer, info, prompt, drain}) =
    63       (buffer, make_source [] info prompt drain);
    64 
    65 fun unget (xs, Source {buffer, info, prompt, drain}) =
    66   make_source (xs @ buffer) info prompt drain;
    67 
    68 
    69 (* variations on get *)
    70 
    71 fun get_prompt prompt src = get (set_prompt prompt src);
    72 
    73 fun get_single src =
    74   (case get src of
    75     ([], _) => NONE
    76   | (x :: xs, src') => SOME (x, unget (xs, src')));
    77 
    78 fun exhaust src =
    79   (case get src of
    80     ([], _) => []
    81   | (xs, src') => xs @ exhaust src');
    82 
    83 
    84 (* (map)filter *)
    85 
    86 fun drain_map_filter f prompt src =
    87   let
    88     val (xs, src') = get_prompt prompt src;
    89     val xs' = map_filter f xs;
    90   in
    91     if null xs orelse not (null xs') then (xs', src')
    92     else drain_map_filter f prompt src'
    93   end;
    94 
    95 fun map_filter f src = make_source [] src default_prompt (drain_map_filter f);
    96 fun filter pred = map_filter (fn x => if pred x then SOME x else NONE);
    97 
    98 
    99 
   100 (** build sources **)
   101 
   102 (* list source *)
   103 
   104 fun of_list xs = make_source [] xs default_prompt (fn _ => fn xs => (xs, []));
   105 val of_string = of_list o explode;
   106 
   107 fun exhausted src = of_list (exhaust src);
   108 
   109 
   110 (* stream source *)
   111 
   112 fun slurp_input instream =
   113   let
   114     fun slurp () =
   115       (case TextIO.canInput (instream, 1) handle IO.Io _ => NONE of
   116         NONE => []
   117       | SOME 0 => []
   118       | SOME _ => TextIO.input instream :: slurp ());
   119   in maps explode (slurp ()) end;
   120 
   121 fun drain_stream instream outstream prompt () =
   122   let val input = slurp_input instream in
   123     if exists (fn c => c = "\n") input then (input, ())
   124     else
   125       (TextIO.output (outstream, Output.output prompt);
   126         TextIO.flushOut outstream;
   127         (case TextIO.inputLine instream of
   128           SOME line => (input @ explode line, ())
   129         | NONE => (input, ())))
   130   end;
   131 
   132 fun of_stream instream outstream =
   133   make_source [] () default_prompt (drain_stream instream outstream);
   134 
   135 val tty = of_stream TextIO.stdIn TextIO.stdOut;
   136 
   137 
   138 
   139 (** compose sources **)
   140 
   141 fun drain_source source stopper scan recover prompt src =
   142   source prompt get_prompt unget stopper scan recover src;
   143 
   144 
   145 (* state-based *)
   146 
   147 fun source' init_state stopper scan recover src =
   148   make_source [] (init_state, src) default_prompt
   149     (drain_source Scan.source' stopper scan recover);
   150 
   151 
   152 (* non state-based *)
   153 
   154 fun source stopper scan recover src =
   155   make_source [] src default_prompt
   156     (drain_source Scan.source stopper scan recover);
   157 
   158 
   159 end;