src/Pure/General/source.ML
author wenzelm
Thu Apr 03 18:42:36 2008 +0200 (2008-04-03)
changeset 26538 d65504ffb47d
parent 25846 f5fb187ae10d
child 27732 8dbf5761a24a
permissions -rw-r--r--
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
     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_list_limited: int -> 'a list -> ('a, 'a list) source
    21   val of_string: string -> (string, string list) source
    22   val exhausted: ('a, 'b) source -> ('a, 'a list) source
    23   val tty: (string, unit) source
    24   val source': 'a -> 'b * ('b -> bool) -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
    25     (bool * (string -> '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     (bool * (string -> '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 fun of_list_limited n xs = make_source [] xs default_prompt (fn _ => chop n);
   106 
   107 val of_string = of_list o explode;
   108 
   109 fun exhausted src = of_list (exhaust src);
   110 
   111 
   112 (* stream source *)
   113 
   114 fun slurp_input instream =
   115   let
   116     fun slurp () =
   117       (case TextIO.canInput (instream, 1) handle IO.Io _ => NONE of
   118         NONE => []
   119       | SOME 0 => []
   120       | SOME _ => TextIO.input instream :: slurp ());
   121   in maps explode (slurp ()) end;
   122 
   123 val tty = make_source [] () default_prompt (fn prompt => fn () =>
   124   let val input = slurp_input TextIO.stdIn in
   125     if exists (fn c => c = "\n") input then (input, ())
   126     else
   127       (case (Output.prompt prompt; TextIO.inputLine TextIO.stdIn) of
   128         SOME line => (input @ explode line, ())
   129       | NONE => (input, ()))
   130   end);
   131 
   132 
   133 
   134 (** cascade sources **)
   135 
   136 (* state-based *)
   137 
   138 fun drain_source' stopper scan opt_recover prompt (state, src) =
   139   let
   140     val drain = Scan.drain prompt get_prompt stopper;
   141     val (xs, s) = get_prompt prompt src;
   142     val inp = ((state, xs), s);
   143     val ((ys, (state', xs')), src') =
   144       if null xs then (([], (state, [])), s)
   145       else
   146         (case opt_recover of
   147           NONE => drain (Scan.error scan) inp
   148         | SOME (interactive, recover) =>
   149             (drain (Scan.catch scan) inp handle Fail msg =>
   150               (if interactive then Output.error_msg msg else ();
   151                 drain (Scan.unless (Scan.lift (Scan.one (#2 stopper))) (recover msg)) inp)));
   152   in (ys, (state', unget (xs', src'))) end;
   153 
   154 fun source' init_state stopper scan recover src =
   155   make_source [] (init_state, src) default_prompt (drain_source' stopper scan recover);
   156 
   157 
   158 (* non state-based *)
   159 
   160 fun drain_source stopper scan opt_recover prompt =
   161   Scan.unlift (drain_source' stopper (Scan.lift scan)
   162     (Option.map (fn (int, r) => (int, Scan.lift o r)) opt_recover) prompt);
   163 
   164 fun source stopper scan recover src =
   165   make_source [] src default_prompt (drain_source stopper scan recover);
   166 
   167 
   168 end;