src/Pure/Syntax/source.ML
author wenzelm
Mon, 09 Nov 1998 15:42:08 +0100
changeset 5840 e2d2b896c717
parent 5019 b6363fa0564f
child 5943 576a7f5e5e39
permissions -rw-r--r--
Object logic specific operations.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
     1
(*  Title:      Pure/Syntax/source.ML
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     4
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     5
Co-algebraic data sources.
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     6
*)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     7
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     8
signature SOURCE =
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
     9
sig
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    10
  type ('a, 'b) source
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    11
  val set_prompt: string -> ('a, 'b) source -> ('a, 'b) source
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    12
  val get: ('a, 'b) source -> 'a list * ('a, 'b) source
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    13
  val unget: 'a list * ('a, 'b) source -> ('a, 'b) source
4983
2c567fcdb36d changed get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option;
wenzelm
parents: 4976
diff changeset
    14
  val get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    15
  val exhaust: ('a, 'b) source -> 'a list
4976
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    16
  val mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c) source) source
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    17
  val filter: ('a -> bool) -> ('a, 'b) source -> ('a, ('a, 'b) source) source
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    18
  val of_list: 'a list -> ('a, 'a list) source
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    19
  val of_string: string -> (string, string list) source
5019
b6363fa0564f added of_file;
wenzelm
parents: 4983
diff changeset
    20
  val of_file: string -> (string, string list) source
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    21
  val of_stream: TextIO.instream -> TextIO.outstream -> (string, unit) source
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    22
  val tty: (string, unit) source
4960
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
    23
  val source': 'a -> 'b * ('b -> bool) -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
    24
    ('a * 'b list -> 'd * ('a * 'b list)) option ->
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
    25
    ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
    26
  val source: 'a * ('a -> bool) -> ('a list -> 'b list * 'a list) ->
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
    27
    ('a list -> 'c * 'a list) option ->
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
    28
    ('a, 'd) source -> ('b, ('a, 'd) source) source
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    29
end;
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    30
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    31
structure Source: SOURCE =
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    32
struct
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    33
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    34
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    35
(** datatype source **)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    36
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    37
datatype ('a, 'b) source =
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    38
  Source of
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    39
   {buffer: 'a list,
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    40
    info: 'b,
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
    41
    prompt: string,
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    42
    drain: string -> 'b -> 'a list * 'b};
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    43
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    44
fun make_source buffer info prompt drain =
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    45
  Source {buffer = buffer, info = info, prompt = prompt, drain = drain};
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    46
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    47
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    48
(* prompt *)
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    49
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    50
val default_prompt = "> ";
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    51
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    52
fun set_prompt prompt (Source {buffer, info, prompt = _, drain}) =
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
    53
  make_source buffer info prompt drain;
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    54
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    55
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    56
(* get / unget *)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    57
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    58
fun get (Source {buffer = [], info, prompt, drain}) =
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
    59
      let val (xs, info') = drain prompt info
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    60
      in (xs, make_source [] info' prompt drain) end
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    61
  | get (Source {buffer, info, prompt, drain}) =
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    62
      (buffer, make_source [] info prompt drain);
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    63
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    64
fun unget (xs, Source {buffer, info, prompt, drain}) =
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    65
  make_source (xs @ buffer) info prompt drain;
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    66
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    67
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    68
(* variations on get *)
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    69
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    70
fun get_prompt prompt src = get (set_prompt prompt src);
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    71
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    72
fun get_single src =
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    73
  (case get src of
4983
2c567fcdb36d changed get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option;
wenzelm
parents: 4976
diff changeset
    74
    ([], _) => None
2c567fcdb36d changed get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option;
wenzelm
parents: 4976
diff changeset
    75
  | (x :: xs, src') => Some (x, unget (xs, src')));
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    76
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    77
fun exhaust src =
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    78
  (case get src of
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    79
    ([], _) => []
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
    80
  | (xs, src') => xs @ exhaust src');
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    81
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    82
4976
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    83
(* (map)filter *)
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    84
4976
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    85
fun drain_mapfilter f prompt src =
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    86
  let
4976
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    87
    val (xs, src') = get_prompt prompt src;
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    88
    val xs' = Library.mapfilter f xs;
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    89
  in
4976
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    90
    if null xs orelse not (null xs') then (xs', src')
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    91
    else drain_mapfilter f prompt src'
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    92
  end;
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    93
4976
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    94
fun mapfilter f src = make_source [] src default_prompt (drain_mapfilter f);
19f48dafe5d3 added mapfilter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c)
wenzelm
parents: 4960
diff changeset
    95
fun filter pred = mapfilter (fn x => if pred x then Some x else None);
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    96
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    97
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    98
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
    99
(** build sources **)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   100
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   101
(* list source *)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   102
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   103
(*limiting the input buffer considerably improves performance*)
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
   104
val limit = 4000;
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   105
4946
d8e5c6e31854 prompt made part of source;
wenzelm
parents: 4939
diff changeset
   106
fun drain_list _ xs = (take (limit, xs), drop (limit, xs));
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   107
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   108
fun of_list xs = make_source [] xs default_prompt drain_list;
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   109
val of_string = of_list o explode;
5019
b6363fa0564f added of_file;
wenzelm
parents: 4983
diff changeset
   110
val of_file = of_string o File.read;
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   111
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   112
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   113
(* stream source *)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   114
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   115
fun drain_stream instream outstream prompt () =
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   116
  (TextIO.output (outstream, prompt);
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   117
    TextIO.flushOut outstream;
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   118
    (explode (TextIO.inputLine instream), ()));
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   119
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   120
fun of_stream instream outstream =
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   121
  make_source [] () default_prompt (drain_stream instream outstream);
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   122
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   123
val tty = of_stream TextIO.stdIn TextIO.stdOut;
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   124
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   125
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   126
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   127
(** compose sources **)
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   128
4960
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   129
fun drain_source source stopper scan recover prompt src =
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   130
  source prompt get_prompt unget stopper scan recover src;
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   131
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   132
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   133
(* state-based *)
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   134
4960
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   135
fun source' init_state stopper scan recover src =
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   136
  make_source [] (init_state, src) default_prompt
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   137
    (drain_source Scan.source' stopper scan recover);
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   138
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   139
4954
cf1404c3f7bb changed get_single: ('a, 'b) source -> 'a option * ('a, 'b) source;
wenzelm
parents: 4946
diff changeset
   140
(* non state-based *)
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   141
4960
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   142
fun source stopper scan recover src =
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   143
  make_source [] src default_prompt
e07823c1ebff renamed state_source to source';
wenzelm
parents: 4954
diff changeset
   144
    (drain_source Scan.source stopper scan recover);
4939
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   145
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   146
33af5d3dae1f Co-algebraic data sources.
wenzelm
parents:
diff changeset
   147
end;