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