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