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