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