author | skalberg |
Thu, 03 Mar 2005 12:43:01 +0100 | |
changeset 15570 | 8d8c70b41bab |
parent 15531 | 08c8dad8e399 |
child 15670 | 963cd3f7976c |
permissions | -rw-r--r-- |
41
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
1 |
(* Title: Pure/library.ML |
0 | 2 |
ID: $Id$ |
233 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
12284 | 4 |
Author: Markus Wenzel, TU Munich |
0 | 5 |
|
233 | 6 |
Basic library: functions, options, pairs, booleans, lists, integers, |
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
7 |
rational numbers, strings, lists as sets, association lists, generic |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
8 |
tables, balanced trees, orders, current directory, misc. |
0 | 9 |
*) |
10 |
||
8418 | 11 |
infix |> |>> |>>> ~~ \ \\ ins ins_string ins_int orf andf prefix upto downto |
4212 | 12 |
mem mem_int mem_string union union_int union_string inter inter_int |
13 |
inter_string subset subset_int subset_string; |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
14 |
|
6510 | 15 |
infix 3 oo ooo oooo; |
5893 | 16 |
|
4621 | 17 |
signature LIBRARY = |
18 |
sig |
|
19 |
(*functions*) |
|
20 |
val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c |
|
21 |
val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c |
|
22 |
val I: 'a -> 'a |
|
23 |
val K: 'a -> 'b -> 'a |
|
24 |
val |> : 'a * ('a -> 'b) -> 'b |
|
8418 | 25 |
val |>> : ('a * 'b) * ('a -> 'c) -> 'c * 'b |
26 |
val |>>> : ('a * 'b) * ('a -> 'c * 'd) -> 'c * ('b * 'd) |
|
4621 | 27 |
val apl: 'a * ('a * 'b -> 'c) -> 'b -> 'c |
28 |
val apr: ('a * 'b -> 'c) * 'b -> 'a -> 'c |
|
29 |
val funpow: int -> ('a -> 'a) -> 'a -> 'a |
|
5893 | 30 |
val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b |
31 |
val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b |
|
6510 | 32 |
val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
33 |
|
4621 | 34 |
(*stamps*) |
35 |
type stamp |
|
36 |
val stamp: unit -> stamp |
|
37 |
||
38 |
(*options*) |
|
39 |
val is_none: 'a option -> bool |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
40 |
exception ERROR |
6959 | 41 |
val try: ('a -> 'b) -> 'a -> 'b option |
4621 | 42 |
val can: ('a -> 'b) -> 'a -> bool |
14868 | 43 |
datatype 'a result = Result of 'a | Exn of exn |
44 |
val capture: ('a -> 'b) -> 'a -> 'b result |
|
45 |
val release: 'a result -> 'a |
|
46 |
val get_result: 'a result -> 'a option |
|
47 |
val get_exn: 'a result -> exn option |
|
4621 | 48 |
|
49 |
(*pairs*) |
|
50 |
val pair: 'a -> 'b -> 'a * 'b |
|
51 |
val rpair: 'a -> 'b -> 'b * 'a |
|
52 |
val fst: 'a * 'b -> 'a |
|
53 |
val snd: 'a * 'b -> 'b |
|
54 |
val eq_fst: (''a * 'b) * (''a * 'c) -> bool |
|
55 |
val eq_snd: ('a * ''b) * ('c * ''b) -> bool |
|
56 |
val swap: 'a * 'b -> 'b * 'a |
|
57 |
val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c |
|
58 |
val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b |
|
59 |
val pairself: ('a -> 'b) -> 'a * 'a -> 'b * 'b |
|
60 |
||
61 |
(*booleans*) |
|
62 |
val equal: ''a -> ''a -> bool |
|
63 |
val not_equal: ''a -> ''a -> bool |
|
64 |
val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool |
|
65 |
val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool |
|
66 |
val exists: ('a -> bool) -> 'a list -> bool |
|
67 |
val forall: ('a -> bool) -> 'a list -> bool |
|
68 |
val set: bool ref -> bool |
|
69 |
val reset: bool ref -> bool |
|
70 |
val toggle: bool ref -> bool |
|
9118 | 71 |
val change: 'a ref -> ('a -> 'a) -> unit |
4621 | 72 |
val setmp: 'a ref -> 'a -> ('b -> 'c) -> 'b -> 'c |
11853 | 73 |
val conditional: bool -> (unit -> unit) -> unit |
4621 | 74 |
|
75 |
(*lists*) |
|
15570 | 76 |
exception UnequalLengths |
4621 | 77 |
val cons: 'a -> 'a list -> 'a list |
5285 | 78 |
val single: 'a -> 'a list |
4629 | 79 |
val append: 'a list -> 'a list -> 'a list |
5904 | 80 |
val apply: ('a -> 'a) list -> 'a -> 'a |
4621 | 81 |
val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a |
14792 | 82 |
val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b |
15035
8c57751cd43f
added fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b;
wenzelm
parents:
14981
diff
changeset
|
83 |
val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b |
4956
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
84 |
val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list |
11002 | 85 |
val foldln: ('a * int -> 'b -> 'b) -> 'a list -> 'b -> 'b |
15570 | 86 |
val unflat: 'a list list -> 'b list -> 'b list list |
13629 | 87 |
val splitAt: int * 'a list -> 'a list * 'a list |
4713 | 88 |
val dropwhile: ('a -> bool) -> 'a list -> 'a list |
11773 | 89 |
val map_nth_elem: int -> ('a -> 'a) -> 'a list -> 'a list |
4621 | 90 |
val split_last: 'a list -> 'a list * 'a |
4893 | 91 |
val nth_update: 'a -> int * 'a list -> 'a list |
4621 | 92 |
val find_index: ('a -> bool) -> 'a list -> int |
93 |
val find_index_eq: ''a -> ''a list -> int |
|
94 |
val find_first: ('a -> bool) -> 'a list -> 'a option |
|
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
95 |
val get_first: ('a -> 'b option) -> 'a list -> 'b option |
4621 | 96 |
val separate: 'a -> 'a list -> 'a list |
97 |
val replicate: int -> 'a -> 'a list |
|
98 |
val multiply: 'a list * 'a list list -> 'a list list |
|
14792 | 99 |
val product: 'a list -> 'b list -> ('a * 'b) list |
4621 | 100 |
val filter_out: ('a -> bool) -> 'a list -> 'a list |
101 |
val map2: ('a * 'b -> 'c) -> 'a list * 'b list -> 'c list |
|
102 |
val exists2: ('a * 'b -> bool) -> 'a list * 'b list -> bool |
|
103 |
val forall2: ('a * 'b -> bool) -> 'a list * 'b list -> bool |
|
4956
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
104 |
val seq2: ('a * 'b -> unit) -> 'a list * 'b list -> unit |
4621 | 105 |
val ~~ : 'a list * 'b list -> ('a * 'b) list |
106 |
val split_list: ('a * 'b) list -> 'a list * 'b list |
|
7468
6ce03d2f7d91
equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool;
wenzelm
parents:
7090
diff
changeset
|
107 |
val equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool |
4621 | 108 |
val prefix: ''a list * ''a list -> bool |
109 |
val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list |
|
110 |
val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list |
|
12249 | 111 |
val prefixes1: 'a list -> 'a list list |
112 |
val suffixes1: 'a list -> 'a list list |
|
4621 | 113 |
|
114 |
(*integers*) |
|
10692 | 115 |
val gcd: int * int -> int |
116 |
val lcm: int * int -> int |
|
4621 | 117 |
val inc: int ref -> int |
118 |
val dec: int ref -> int |
|
119 |
val upto: int * int -> int list |
|
120 |
val downto: int * int -> int list |
|
121 |
val downto0: int list * int -> bool |
|
122 |
val radixpand: int * int -> int list |
|
123 |
val radixstring: int * string * int -> string |
|
124 |
val string_of_int: int -> string |
|
125 |
val string_of_indexname: string * int -> string |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
126 |
val read_radixint: int * string list -> int * string list |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
127 |
val read_int: string list -> int * string list |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
128 |
val oct_char: string -> string |
4621 | 129 |
|
10692 | 130 |
(*rational numbers*) |
131 |
type rat |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
132 |
exception RAT of string |
10692 | 133 |
val rep_rat: rat -> int * int |
134 |
val ratadd: rat * rat -> rat |
|
135 |
val ratmul: rat * rat -> rat |
|
136 |
val ratinv: rat -> rat |
|
137 |
val int_ratdiv: int * int -> rat |
|
138 |
val ratneg: rat -> rat |
|
139 |
val rat_of_int: int -> rat |
|
140 |
||
4621 | 141 |
(*strings*) |
6312 | 142 |
val nth_elem_string: int * string -> string |
6282 | 143 |
val foldl_string: ('a * string -> 'a) -> 'a * string -> 'a |
6312 | 144 |
val exists_string: (string -> bool) -> string -> bool |
4621 | 145 |
val enclose: string -> string -> string -> string |
6642 | 146 |
val unenclose: string -> string |
4621 | 147 |
val quote: string -> string |
148 |
val space_implode: string -> string list -> string |
|
149 |
val commas: string list -> string |
|
150 |
val commas_quote: string list -> string |
|
151 |
val cat_lines: string list -> string |
|
152 |
val space_explode: string -> string -> string list |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
153 |
val split_lines: string -> string list |
5942 | 154 |
val prefix_lines: string -> string -> string |
7712 | 155 |
val untabify: string list -> string list |
5285 | 156 |
val suffix: string -> string -> string |
157 |
val unsuffix: string -> string -> string |
|
15060 | 158 |
val unprefix: string -> string -> string |
10951 | 159 |
val replicate_string: int -> string -> string |
14926 | 160 |
val translate_string: (string -> string) -> string -> string |
4621 | 161 |
|
162 |
(*lists as sets*) |
|
163 |
val mem: ''a * ''a list -> bool |
|
164 |
val mem_int: int * int list -> bool |
|
165 |
val mem_string: string * string list -> bool |
|
166 |
val gen_mem: ('a * 'b -> bool) -> 'a * 'b list -> bool |
|
167 |
val ins: ''a * ''a list -> ''a list |
|
168 |
val ins_int: int * int list -> int list |
|
169 |
val ins_string: string * string list -> string list |
|
170 |
val gen_ins: ('a * 'a -> bool) -> 'a * 'a list -> 'a list |
|
171 |
val union: ''a list * ''a list -> ''a list |
|
172 |
val union_int: int list * int list -> int list |
|
173 |
val union_string: string list * string list -> string list |
|
174 |
val gen_union: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list |
|
7090 | 175 |
val gen_inter: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list |
4621 | 176 |
val inter: ''a list * ''a list -> ''a list |
177 |
val inter_int: int list * int list -> int list |
|
178 |
val inter_string: string list * string list -> string list |
|
179 |
val subset: ''a list * ''a list -> bool |
|
180 |
val subset_int: int list * int list -> bool |
|
181 |
val subset_string: string list * string list -> bool |
|
182 |
val eq_set: ''a list * ''a list -> bool |
|
183 |
val eq_set_string: string list * string list -> bool |
|
184 |
val gen_subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool |
|
185 |
val \ : ''a list * ''a -> ''a list |
|
186 |
val \\ : ''a list * ''a list -> ''a list |
|
187 |
val gen_rem: ('a * 'b -> bool) -> 'a list * 'b -> 'a list |
|
188 |
val gen_rems: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list |
|
189 |
val gen_distinct: ('a * 'a -> bool) -> 'a list -> 'a list |
|
190 |
val distinct: ''a list -> ''a list |
|
191 |
val findrep: ''a list -> ''a list |
|
192 |
val gen_duplicates: ('a * 'a -> bool) -> 'a list -> 'a list |
|
193 |
val duplicates: ''a list -> ''a list |
|
194 |
||
195 |
(*association lists*) |
|
196 |
val assoc: (''a * 'b) list * ''a -> 'b option |
|
197 |
val assoc_int: (int * 'a) list * int -> 'a option |
|
198 |
val assoc_string: (string * 'a) list * string -> 'a option |
|
199 |
val assoc_string_int: ((string * int) * 'a) list * (string * int) -> 'a option |
|
200 |
val assocs: (''a * 'b list) list -> ''a -> 'b list |
|
201 |
val assoc2: (''a * (''b * 'c) list) list * (''a * ''b) -> 'c option |
|
202 |
val gen_assoc: ('a * 'b -> bool) -> ('b * 'c) list * 'a -> 'c option |
|
203 |
val overwrite: (''a * 'b) list * (''a * 'b) -> (''a * 'b) list |
|
204 |
val gen_overwrite: ('a * 'a -> bool) -> ('a * 'b) list * ('a * 'b) -> ('a * 'b) list |
|
205 |
||
12284 | 206 |
(*lists as tables*) |
207 |
val gen_merge_lists: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list |
|
208 |
val gen_merge_lists': ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list |
|
4621 | 209 |
val merge_lists: ''a list -> ''a list -> ''a list |
12284 | 210 |
val merge_lists': ''a list -> ''a list -> ''a list |
4692 | 211 |
val merge_alists: (''a * 'b) list -> (''a * 'b) list -> (''a * 'b) list |
15263 | 212 |
val merge_alists': (''a * 'b) list -> (''a * 'b) list -> (''a * 'b) list |
4621 | 213 |
|
214 |
(*balanced trees*) |
|
215 |
exception Balance |
|
216 |
val fold_bal: ('a * 'a -> 'a) -> 'a list -> 'a |
|
217 |
val access_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> int -> 'a |
|
218 |
val accesses_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> 'a list |
|
219 |
||
220 |
(*orders*) |
|
221 |
val rev_order: order -> order |
|
222 |
val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order |
|
223 |
val int_ord: int * int -> order |
|
224 |
val string_ord: string * string -> order |
|
225 |
val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order |
|
226 |
val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order |
|
227 |
val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order |
|
228 |
val sort: ('a * 'a -> order) -> 'a list -> 'a list |
|
229 |
val sort_strings: string list -> string list |
|
230 |
val sort_wrt: ('a -> string) -> 'a list -> 'a list |
|
11514 | 231 |
val unique_strings: string list -> string list |
4621 | 232 |
|
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
233 |
(*random numbers*) |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
234 |
exception RANDOM |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
235 |
val random: unit -> real |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
236 |
val random_range: int -> int -> int |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
237 |
val one_of: 'a list -> 'a |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
238 |
val frequency: (int * 'a) list -> 'a |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
239 |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
240 |
(*current directory*) |
4621 | 241 |
val cd: string -> unit |
242 |
val pwd: unit -> string |
|
243 |
||
244 |
(*misc*) |
|
245 |
val make_keylist: ('a -> 'b) -> 'a list -> ('a * 'b) list |
|
246 |
val keyfilter: ('a * ''b) list -> ''b -> 'a list |
|
247 |
val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list |
|
248 |
val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list |
|
249 |
val gensym: string -> string |
|
250 |
val scanwords: (string -> bool) -> string list -> string list |
|
251 |
end; |
|
252 |
||
15570 | 253 |
signature LIBRARY_CLOSED = |
254 |
sig |
|
255 |
include LIBRARY |
|
256 |
||
257 |
val the: 'a option -> 'a |
|
258 |
val if_none: 'a option -> 'a -> 'a |
|
259 |
val is_some: 'a option -> bool |
|
260 |
val apsome: ('a -> 'b) -> 'a option -> 'b option |
|
261 |
||
262 |
val rev_append: 'a list -> 'a list -> 'a list |
|
263 |
val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a |
|
264 |
val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b |
|
265 |
val take: int * 'a list -> 'a list |
|
266 |
val drop: int * 'a list -> 'a list |
|
267 |
val nth_elem: int * 'a list -> 'a |
|
268 |
val last_elem: 'a list -> 'a |
|
269 |
val flat: 'a list list -> 'a list |
|
270 |
val seq: ('a -> unit) -> 'a list -> unit |
|
271 |
val partition: ('a -> bool) -> 'a list -> 'a list * 'a list |
|
272 |
val filter: ('a -> bool) -> 'a list -> 'a list |
|
273 |
val mapfilter: ('a -> 'b option) -> 'a list -> 'b list |
|
274 |
end; |
|
275 |
||
276 |
structure Library: LIBRARY_CLOSED = |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
277 |
struct |
0 | 278 |
|
4995 | 279 |
|
233 | 280 |
(** functions **) |
0 | 281 |
|
233 | 282 |
(*handy combinators*) |
283 |
fun curry f x y = f (x, y); |
|
284 |
fun uncurry f (x, y) = f x y; |
|
285 |
fun I x = x; |
|
286 |
fun K x y = x; |
|
0 | 287 |
|
380 | 288 |
(*reverse apply*) |
410 | 289 |
fun (x |> f) = f x; |
8418 | 290 |
fun ((x, y) |>> f) = (f x, y); |
291 |
fun ((x, y) |>>> f) = let val (x', z) = f x in (x', (y, z)) end; |
|
380 | 292 |
|
233 | 293 |
(*application of (infix) operator to its left or right argument*) |
294 |
fun apl (x, f) y = f (x, y); |
|
295 |
fun apr (f, y) x = f (x, y); |
|
0 | 296 |
|
233 | 297 |
(*function exponentiation: f(...(f x)...) with n applications of f*) |
298 |
fun funpow n f x = |
|
299 |
let fun rep (0, x) = x |
|
300 |
| rep (n, x) = rep (n - 1, f x) |
|
301 |
in rep (n, x) end; |
|
160 | 302 |
|
5893 | 303 |
(*concatenation: 2 and 3 args*) |
304 |
fun (f oo g) x y = f (g x y); |
|
305 |
fun (f ooo g) x y z = f (g x y z); |
|
6510 | 306 |
fun (f oooo g) x y z w = f (g x y z w); |
307 |
||
160 | 308 |
|
309 |
||
2471 | 310 |
(** stamps **) |
311 |
||
312 |
type stamp = unit ref; |
|
313 |
val stamp: unit -> stamp = ref; |
|
314 |
||
315 |
||
316 |
||
233 | 317 |
(** options **) |
0 | 318 |
|
15570 | 319 |
val the = valOf; |
0 | 320 |
|
4212 | 321 |
(*strict!*) |
15531 | 322 |
fun if_none opt a = getOpt(opt,a); |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
323 |
|
15570 | 324 |
val is_some = isSome; |
0 | 325 |
|
15570 | 326 |
fun is_none opt = not (isSome opt); |
0 | 327 |
|
15570 | 328 |
val apsome = Option.map; |
0 | 329 |
|
6959 | 330 |
|
331 |
(* exception handling *) |
|
332 |
||
333 |
exception ERROR; |
|
334 |
||
15531 | 335 |
fun try f x = SOME (f x) |
336 |
handle Interrupt => raise Interrupt | ERROR => raise ERROR | _ => NONE; |
|
6959 | 337 |
|
338 |
fun can f x = is_some (try f x); |
|
4139 | 339 |
|
340 |
||
14868 | 341 |
datatype 'a result = |
342 |
Result of 'a | |
|
343 |
Exn of exn; |
|
344 |
||
345 |
fun capture f x = Result (f x) handle e => Exn e; |
|
346 |
||
347 |
fun release (Result y) = y |
|
348 |
| release (Exn e) = raise e; |
|
349 |
||
15531 | 350 |
fun get_result (Result x) = SOME x |
351 |
| get_result _ = NONE; |
|
14868 | 352 |
|
15531 | 353 |
fun get_exn (Exn exn) = SOME exn |
354 |
| get_exn _ = NONE; |
|
14868 | 355 |
|
356 |
||
4139 | 357 |
|
233 | 358 |
(** pairs **) |
359 |
||
360 |
fun pair x y = (x, y); |
|
361 |
fun rpair x y = (y, x); |
|
362 |
||
363 |
fun fst (x, y) = x; |
|
364 |
fun snd (x, y) = y; |
|
365 |
||
366 |
fun eq_fst ((x1, _), (x2, _)) = x1 = x2; |
|
367 |
fun eq_snd ((_, y1), (_, y2)) = y1 = y2; |
|
368 |
||
369 |
fun swap (x, y) = (y, x); |
|
370 |
||
4212 | 371 |
(*apply function to components*) |
233 | 372 |
fun apfst f (x, y) = (f x, y); |
373 |
fun apsnd f (x, y) = (x, f y); |
|
4212 | 374 |
fun pairself f (x, y) = (f x, f y); |
233 | 375 |
|
376 |
||
377 |
||
378 |
(** booleans **) |
|
379 |
||
380 |
(* equality *) |
|
381 |
||
382 |
fun equal x y = x = y; |
|
383 |
fun not_equal x y = x <> y; |
|
384 |
||
385 |
||
386 |
(* operators for combining predicates *) |
|
387 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
388 |
fun (p orf q) = fn x => p x orelse q x; |
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
389 |
fun (p andf q) = fn x => p x andalso q x; |
233 | 390 |
|
391 |
||
392 |
(* predicates on lists *) |
|
393 |
||
394 |
(*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*) |
|
395 |
fun exists (pred: 'a -> bool) : 'a list -> bool = |
|
396 |
let fun boolf [] = false |
|
397 |
| boolf (x :: xs) = pred x orelse boolf xs |
|
398 |
in boolf end; |
|
399 |
||
400 |
(*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*) |
|
401 |
fun forall (pred: 'a -> bool) : 'a list -> bool = |
|
402 |
let fun boolf [] = true |
|
403 |
| boolf (x :: xs) = pred x andalso boolf xs |
|
404 |
in boolf end; |
|
0 | 405 |
|
233 | 406 |
|
380 | 407 |
(* flags *) |
408 |
||
409 |
fun set flag = (flag := true; true); |
|
410 |
fun reset flag = (flag := false; false); |
|
411 |
fun toggle flag = (flag := not (! flag); ! flag); |
|
412 |
||
9118 | 413 |
fun change r f = r := f (! r); |
414 |
||
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
415 |
(*temporarily set flag, handling exceptions*) |
2978 | 416 |
fun setmp flag value f x = |
2958 | 417 |
let |
418 |
val orig_value = ! flag; |
|
419 |
fun return y = (flag := orig_value; y); |
|
420 |
in |
|
421 |
flag := value; |
|
422 |
return (f x handle exn => (return (); raise exn)) |
|
423 |
end; |
|
424 |
||
380 | 425 |
|
11853 | 426 |
(* conditional execution *) |
427 |
||
428 |
fun conditional b f = if b then f () else (); |
|
429 |
||
430 |
||
233 | 431 |
|
432 |
(** lists **) |
|
433 |
||
15570 | 434 |
exception UnequalLengths; |
233 | 435 |
|
436 |
fun cons x xs = x :: xs; |
|
5285 | 437 |
fun single x = [x]; |
233 | 438 |
|
4629 | 439 |
fun append xs ys = xs @ ys; |
440 |
||
13794 | 441 |
(* tail recursive version *) |
15531 | 442 |
fun rev_append xs ys = List.revAppend(xs,ys); |
13794 | 443 |
|
5904 | 444 |
fun apply [] x = x |
445 |
| apply (f :: fs) x = apply fs (f x); |
|
446 |
||
233 | 447 |
|
448 |
(* fold *) |
|
449 |
||
450 |
(*the following versions of fold are designed to fit nicely with infixes*) |
|
0 | 451 |
|
233 | 452 |
(* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn |
453 |
for operators that associate to the left (TAIL RECURSIVE)*) |
|
454 |
fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a = |
|
455 |
let fun itl (e, []) = e |
|
456 |
| itl (e, a::l) = itl (f(e, a), l) |
|
457 |
in itl end; |
|
458 |
||
459 |
(* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e)) |
|
460 |
for operators that associate to the right (not tail recursive)*) |
|
461 |
fun foldr f (l, e) = |
|
462 |
let fun itr [] = e |
|
463 |
| itr (a::l) = f(a, itr l) |
|
464 |
in itr l end; |
|
465 |
||
466 |
(* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n-1] @ xn)) |
|
467 |
for n > 0, operators that associate to the right (not tail recursive)*) |
|
468 |
fun foldr1 f l = |
|
4181 | 469 |
let fun itr [x] = x |
233 | 470 |
| itr (x::l) = f(x, itr l) |
471 |
in itr l end; |
|
472 |
||
14792 | 473 |
fun fold f xs y = foldl (fn (y', x) => f x y') (y, xs); |
15035
8c57751cd43f
added fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b;
wenzelm
parents:
14981
diff
changeset
|
474 |
fun fold_rev f xs y = foldr (fn (x, y') => f x y') (xs, y); |
14792 | 475 |
|
4956
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
476 |
fun foldl_map _ (x, []) = (x, []) |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
477 |
| foldl_map f (x, y :: ys) = |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
478 |
let |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
479 |
val (x', y') = f (x, y); |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
480 |
val (x'', ys') = foldl_map f (x', ys); |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
481 |
in (x'', y' :: ys') end; |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
482 |
|
11002 | 483 |
fun foldln f xs e = fst (foldl (fn ((e,i), x) => (f (x,i) e, i+1)) ((e,1),xs)); |
233 | 484 |
|
485 |
(* basic list functions *) |
|
486 |
||
487 |
(*take the first n elements from a list*) |
|
488 |
fun take (n, []) = [] |
|
489 |
| take (n, x :: xs) = |
|
490 |
if n > 0 then x :: take (n - 1, xs) else []; |
|
491 |
||
492 |
(*drop the first n elements from a list*) |
|
493 |
fun drop (n, []) = [] |
|
494 |
| drop (n, x :: xs) = |
|
495 |
if n > 0 then drop (n - 1, xs) else x :: xs; |
|
0 | 496 |
|
13629 | 497 |
fun splitAt(n,[]) = ([],[]) |
498 |
| splitAt(n,xs as x::ys) = |
|
499 |
if n>0 then let val (ps,qs) = splitAt(n-1,ys) in (x::ps,qs) end |
|
500 |
else ([],xs) |
|
501 |
||
4713 | 502 |
fun dropwhile P [] = [] |
503 |
| dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys; |
|
504 |
||
233 | 505 |
(*return nth element of a list, where 0 designates the first element; |
506 |
raise EXCEPTION if list too short*) |
|
15570 | 507 |
fun nth_elem (i,xs) = List.nth(xs,i); |
233 | 508 |
|
11773 | 509 |
fun map_nth_elem 0 f (x :: xs) = f x :: xs |
510 |
| map_nth_elem n f (x :: xs) = x :: map_nth_elem (n - 1) f xs |
|
15570 | 511 |
| map_nth_elem _ _ [] = raise Subscript; |
11773 | 512 |
|
233 | 513 |
(*last element of a list*) |
15570 | 514 |
val last_elem = List.last; |
233 | 515 |
|
3762 | 516 |
(*rear decomposition*) |
15570 | 517 |
fun split_last [] = raise Empty |
3762 | 518 |
| split_last [x] = ([], x) |
519 |
| split_last (x :: xs) = apfst (cons x) (split_last xs); |
|
520 |
||
4893 | 521 |
(*update nth element*) |
13629 | 522 |
fun nth_update x n_xs = |
523 |
(case splitAt n_xs of |
|
15570 | 524 |
(_,[]) => raise Subscript |
13629 | 525 |
| (prfx, _ :: sffx') => prfx @ (x :: sffx')) |
4893 | 526 |
|
4212 | 527 |
(*find the position of an element in a list*) |
528 |
fun find_index pred = |
|
529 |
let fun find _ [] = ~1 |
|
530 |
| find n (x :: xs) = if pred x then n else find (n + 1) xs; |
|
531 |
in find 0 end; |
|
3762 | 532 |
|
4224 | 533 |
fun find_index_eq x = find_index (equal x); |
4212 | 534 |
|
535 |
(*find first element satisfying predicate*) |
|
15531 | 536 |
fun find_first _ [] = NONE |
4212 | 537 |
| find_first pred (x :: xs) = |
15531 | 538 |
if pred x then SOME x else find_first pred xs; |
233 | 539 |
|
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
540 |
(*get first element by lookup function*) |
15531 | 541 |
fun get_first _ [] = NONE |
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
542 |
| get_first f (x :: xs) = |
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
543 |
(case f x of |
15531 | 544 |
NONE => get_first f xs |
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
545 |
| some => some); |
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
546 |
|
233 | 547 |
(*flatten a list of lists to a list*) |
15531 | 548 |
val flat = List.concat; |
233 | 549 |
|
12136 | 550 |
fun unflat (xs :: xss) ys = |
13629 | 551 |
let val (ps,qs) = splitAt(length xs,ys) |
552 |
in ps :: unflat xss qs end |
|
12136 | 553 |
| unflat [] [] = [] |
15570 | 554 |
| unflat _ _ = raise UnequalLengths; |
12136 | 555 |
|
233 | 556 |
(*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates |
557 |
(proc x1; ...; proc xn) for side effects*) |
|
15570 | 558 |
val seq = List.app; |
233 | 559 |
|
560 |
(*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*) |
|
561 |
fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs |
|
562 |
| separate _ xs = xs; |
|
563 |
||
564 |
(*make the list [x, x, ..., x] of length n*) |
|
565 |
fun replicate n (x: 'a) : 'a list = |
|
566 |
let fun rep (0, xs) = xs |
|
567 |
| rep (n, xs) = rep (n - 1, x :: xs) |
|
568 |
in |
|
15570 | 569 |
if n < 0 then raise Subscript |
233 | 570 |
else rep (n, []) |
571 |
end; |
|
572 |
||
14926 | 573 |
fun translate_string f = String.translate (f o String.str); |
574 |
||
4248
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
wenzelm
parents:
4224
diff
changeset
|
575 |
(*multiply [a, b, c, ...] * [xs, ys, zs, ...]*) |
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
wenzelm
parents:
4224
diff
changeset
|
576 |
fun multiply ([], _) = [] |
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
wenzelm
parents:
4224
diff
changeset
|
577 |
| multiply (x :: xs, yss) = map (cons x) yss @ multiply (xs, yss); |
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
wenzelm
parents:
4224
diff
changeset
|
578 |
|
14792 | 579 |
(*direct product*) |
580 |
fun product _ [] = [] |
|
581 |
| product [] _ = [] |
|
582 |
| product (x :: xs) ys = map (pair x) ys @ product xs ys; |
|
583 |
||
233 | 584 |
|
585 |
(* filter *) |
|
586 |
||
587 |
(*copy the list preserving elements that satisfy the predicate*) |
|
15531 | 588 |
val filter = List.filter; |
0 | 589 |
|
590 |
fun filter_out f = filter (not o f); |
|
591 |
||
15570 | 592 |
val mapfilter = List.mapPartial; |
233 | 593 |
|
594 |
||
595 |
(* lists of pairs *) |
|
596 |
||
15570 | 597 |
exception UnequalLengths; |
598 |
||
380 | 599 |
fun map2 _ ([], []) = [] |
600 |
| map2 f (x :: xs, y :: ys) = (f (x, y) :: map2 f (xs, ys)) |
|
15570 | 601 |
| map2 _ _ = raise UnequalLengths; |
380 | 602 |
|
603 |
fun exists2 _ ([], []) = false |
|
604 |
| exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys) |
|
15570 | 605 |
| exists2 _ _ = raise UnequalLengths; |
380 | 606 |
|
607 |
fun forall2 _ ([], []) = true |
|
608 |
| forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys) |
|
15570 | 609 |
| forall2 _ _ = raise UnequalLengths; |
380 | 610 |
|
4956
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
611 |
fun seq2 _ ([], []) = () |
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
612 |
| seq2 f (x :: xs, y :: ys) = (f (x, y); seq2 f (xs, ys)) |
15570 | 613 |
| seq2 _ _ = raise UnequalLengths; |
4956
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
614 |
|
233 | 615 |
(*combine two lists forming a list of pairs: |
616 |
[x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*) |
|
617 |
fun [] ~~ [] = [] |
|
618 |
| (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys) |
|
15570 | 619 |
| _ ~~ _ = raise UnequalLengths; |
233 | 620 |
|
621 |
(*inverse of ~~; the old 'split': |
|
622 |
[(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*) |
|
15570 | 623 |
val split_list = ListPair.unzip; |
233 | 624 |
|
7468
6ce03d2f7d91
equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool;
wenzelm
parents:
7090
diff
changeset
|
625 |
fun equal_lists eq (xs, ys) = length xs = length ys andalso forall2 eq (xs, ys); |
6ce03d2f7d91
equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool;
wenzelm
parents:
7090
diff
changeset
|
626 |
|
233 | 627 |
|
628 |
(* prefixes, suffixes *) |
|
629 |
||
630 |
fun [] prefix _ = true |
|
631 |
| (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys) |
|
632 |
| _ prefix _ = false; |
|
633 |
||
634 |
(* [x1, ..., xi, ..., xn] ---> ([x1, ..., x(i-1)], [xi, ..., xn]) |
|
635 |
where xi is the first element that does not satisfy the predicate*) |
|
636 |
fun take_prefix (pred : 'a -> bool) (xs: 'a list) : 'a list * 'a list = |
|
637 |
let fun take (rxs, []) = (rev rxs, []) |
|
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
638 |
| take (rxs, x :: xs) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
639 |
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) |
233 | 640 |
in take([], xs) end; |
641 |
||
642 |
(* [x1, ..., xi, ..., xn] ---> ([x1, ..., xi], [x(i+1), ..., xn]) |
|
643 |
where xi is the last element that does not satisfy the predicate*) |
|
644 |
fun take_suffix _ [] = ([], []) |
|
645 |
| take_suffix pred (x :: xs) = |
|
646 |
(case take_suffix pred xs of |
|
647 |
([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) |
|
648 |
| (prfx, sffx) => (x :: prfx, sffx)); |
|
649 |
||
12249 | 650 |
fun prefixes1 [] = [] |
651 |
| prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs); |
|
652 |
||
653 |
fun suffixes1 xs = map rev (prefixes1 (rev xs)); |
|
654 |
||
233 | 655 |
|
656 |
||
657 |
(** integers **) |
|
658 |
||
10692 | 659 |
fun gcd(x,y) = |
660 |
let fun gxd x y = |
|
661 |
if y = 0 then x else gxd y (x mod y) |
|
662 |
in if x < y then gxd y x else gxd x y end; |
|
663 |
||
664 |
fun lcm(x,y) = (x * y) div gcd(x,y); |
|
665 |
||
2958 | 666 |
fun inc i = (i := ! i + 1; ! i); |
667 |
fun dec i = (i := ! i - 1; ! i); |
|
233 | 668 |
|
669 |
||
670 |
(* lists of integers *) |
|
671 |
||
672 |
(*make the list [from, from + 1, ..., to]*) |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
673 |
fun (from upto to) = |
233 | 674 |
if from > to then [] else from :: ((from + 1) upto to); |
675 |
||
676 |
(*make the list [from, from - 1, ..., to]*) |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
677 |
fun (from downto to) = |
233 | 678 |
if from < to then [] else from :: ((from - 1) downto to); |
679 |
||
680 |
(*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*) |
|
681 |
fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1) |
|
682 |
| downto0 ([], n) = n = ~1; |
|
683 |
||
684 |
||
685 |
(* convert integers to strings *) |
|
686 |
||
687 |
(*expand the number in the given base; |
|
688 |
example: radixpand (2, 8) gives [1, 0, 0, 0]*) |
|
689 |
fun radixpand (base, num) : int list = |
|
690 |
let |
|
691 |
fun radix (n, tail) = |
|
692 |
if n < base then n :: tail |
|
693 |
else radix (n div base, (n mod base) :: tail) |
|
694 |
in radix (num, []) end; |
|
695 |
||
696 |
(*expands a number into a string of characters starting from "zerochar"; |
|
697 |
example: radixstring (2, "0", 8) gives "1000"*) |
|
698 |
fun radixstring (base, zerochar, num) = |
|
699 |
let val offset = ord zerochar; |
|
700 |
fun chrof n = chr (offset + n) |
|
701 |
in implode (map chrof (radixpand (base, num))) end; |
|
702 |
||
703 |
||
3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset
|
704 |
val string_of_int = Int.toString; |
233 | 705 |
|
3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset
|
706 |
fun string_of_indexname (a,0) = a |
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset
|
707 |
| string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; |
233 | 708 |
|
709 |
||
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
710 |
(* read integers *) |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
711 |
|
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
712 |
fun read_radixint (radix: int, cs) : int * string list = |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
713 |
let val zero = ord"0" |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
714 |
val limit = zero+radix |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
715 |
fun scan (num,[]) = (num,[]) |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
716 |
| scan (num, c::cs) = |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
717 |
if zero <= ord c andalso ord c < limit |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
718 |
then scan(radix*num + ord c - zero, cs) |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
719 |
else (num, c::cs) |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
720 |
in scan(0,cs) end; |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
721 |
|
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
722 |
fun read_int cs = read_radixint (10, cs); |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
723 |
|
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
724 |
fun oct_char s = chr (#1 (read_radixint (8, explode s))); |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
725 |
|
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
726 |
|
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
727 |
|
233 | 728 |
(** strings **) |
729 |
||
6312 | 730 |
(*functions tuned for strings, avoiding explode*) |
731 |
||
732 |
fun nth_elem_string (i, str) = |
|
6959 | 733 |
(case try String.substring (str, i, 1) of |
15531 | 734 |
SOME s => s |
15570 | 735 |
| NONE => raise Subscript); |
6312 | 736 |
|
6282 | 737 |
fun foldl_string f (x0, str) = |
738 |
let |
|
739 |
val n = size str; |
|
14968 | 740 |
fun fold (x, i) = |
741 |
if i < n then fold (f (x, String.substring (str, i, 1)), i + 1) else x; |
|
6282 | 742 |
in fold (x0, 0) end; |
743 |
||
14968 | 744 |
fun exists_string pred str = |
745 |
let |
|
746 |
val n = size str; |
|
747 |
fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1)); |
|
748 |
in ex 0 end; |
|
6312 | 749 |
|
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
750 |
(*enclose in brackets*) |
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
751 |
fun enclose lpar rpar str = lpar ^ str ^ rpar; |
6642 | 752 |
fun unenclose str = String.substring (str, 1, size str - 2); |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
753 |
|
233 | 754 |
(*simple quoting (does not escape special chars)*) |
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
755 |
val quote = enclose "\"" "\""; |
233 | 756 |
|
4212 | 757 |
(*space_implode "..." (explode "hello") = "h...e...l...l...o"*) |
233 | 758 |
fun space_implode a bs = implode (separate a bs); |
759 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
760 |
val commas = space_implode ", "; |
380 | 761 |
val commas_quote = commas o map quote; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
762 |
|
233 | 763 |
(*concatenate messages, one per line, into a string*) |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
764 |
val cat_lines = space_implode "\n"; |
233 | 765 |
|
4212 | 766 |
(*space_explode "." "h.e..l.lo" = ["h", "e", "", "l", "lo"]*) |
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
767 |
fun space_explode _ "" = [] |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
768 |
| space_explode sep str = |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
769 |
let |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
770 |
fun expl chs = |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
771 |
(case take_prefix (not_equal sep) chs of |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
772 |
(cs, []) => [implode cs] |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
773 |
| (cs, _ :: cs') => implode cs :: expl cs'); |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
774 |
in expl (explode str) end; |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
775 |
|
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
776 |
val split_lines = space_explode "\n"; |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
777 |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
778 |
fun prefix_lines "" txt = txt |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
779 |
| prefix_lines prfx txt = txt |> split_lines |> map (fn s => prfx ^ s) |> cat_lines; |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
780 |
|
7712 | 781 |
(*untabify*) |
782 |
fun untabify chs = |
|
783 |
let |
|
784 |
val tab_width = 8; |
|
785 |
||
786 |
fun untab (_, "\n") = (0, ["\n"]) |
|
9118 | 787 |
| untab (pos, "\t") = |
788 |
let val d = tab_width - (pos mod tab_width) in (pos + d, replicate d " ") end |
|
7712 | 789 |
| untab (pos, c) = (pos + 1, [c]); |
790 |
in |
|
791 |
if not (exists (equal "\t") chs) then chs |
|
792 |
else flat (#2 (foldl_map untab (0, chs))) |
|
793 |
end; |
|
794 |
||
5285 | 795 |
(*append suffix*) |
796 |
fun suffix sfx s = s ^ sfx; |
|
797 |
||
798 |
(*remove suffix*) |
|
799 |
fun unsuffix sfx s = |
|
800 |
let |
|
801 |
val cs = explode s; |
|
802 |
val prfx_len = size s - size sfx; |
|
803 |
in |
|
804 |
if prfx_len >= 0 andalso implode (drop (prfx_len, cs)) = sfx then |
|
805 |
implode (take (prfx_len, cs)) |
|
15570 | 806 |
else raise Fail "unsuffix" |
5285 | 807 |
end; |
808 |
||
15060 | 809 |
(*remove prefix*) |
810 |
fun unprefix prfx s = |
|
811 |
let val (prfx', sfx) = splitAt (size prfx, explode s) |
|
812 |
in |
|
813 |
if implode prfx' = prfx then implode sfx |
|
15570 | 814 |
else raise Fail "unprefix" |
15060 | 815 |
end; |
816 |
||
10951 | 817 |
fun replicate_string 0 _ = "" |
818 |
| replicate_string 1 a = a |
|
819 |
| replicate_string k a = |
|
820 |
if k mod 2 = 0 then replicate_string (k div 2) (a ^ a) |
|
821 |
else replicate_string (k div 2) (a ^ a) ^ a; |
|
822 |
||
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
823 |
|
233 | 824 |
|
825 |
(** lists as sets **) |
|
826 |
||
827 |
(*membership in a list*) |
|
828 |
fun x mem [] = false |
|
829 |
| x mem (y :: ys) = x = y orelse x mem ys; |
|
0 | 830 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
831 |
(*membership in a list, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
832 |
fun (x:int) mem_int [] = false |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
833 |
| x mem_int (y :: ys) = x = y orelse x mem_int ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
834 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
835 |
(*membership in a list, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
836 |
fun (x:string) mem_string [] = false |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
837 |
| x mem_string (y :: ys) = x = y orelse x mem_string ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
838 |
|
0 | 839 |
(*generalized membership test*) |
233 | 840 |
fun gen_mem eq (x, []) = false |
841 |
| gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); |
|
842 |
||
843 |
||
844 |
(*insertion into list if not already there*) |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
845 |
fun (x ins xs) = if x mem xs then xs else x :: xs; |
0 | 846 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
847 |
(*insertion into list, optimized version for ints*) |
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
848 |
fun (x ins_int xs) = if x mem_int xs then xs else x :: xs; |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
849 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
850 |
(*insertion into list, optimized version for strings*) |
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
851 |
fun (x ins_string xs) = if x mem_string xs then xs else x :: xs; |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
852 |
|
0 | 853 |
(*generalized insertion*) |
233 | 854 |
fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs; |
855 |
||
856 |
||
857 |
(*union of sets represented as lists: no repetitions*) |
|
858 |
fun xs union [] = xs |
|
859 |
| [] union ys = ys |
|
860 |
| (x :: xs) union ys = xs union (x ins ys); |
|
0 | 861 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
862 |
(*union of sets, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
863 |
fun (xs:int list) union_int [] = xs |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
864 |
| [] union_int ys = ys |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
865 |
| (x :: xs) union_int ys = xs union_int (x ins_int ys); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
866 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
867 |
(*union of sets, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
868 |
fun (xs:string list) union_string [] = xs |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
869 |
| [] union_string ys = ys |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
870 |
| (x :: xs) union_string ys = xs union_string (x ins_string ys); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
871 |
|
0 | 872 |
(*generalized union*) |
233 | 873 |
fun gen_union eq (xs, []) = xs |
874 |
| gen_union eq ([], ys) = ys |
|
875 |
| gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys)); |
|
876 |
||
877 |
||
878 |
(*intersection*) |
|
879 |
fun [] inter ys = [] |
|
880 |
| (x :: xs) inter ys = |
|
881 |
if x mem ys then x :: (xs inter ys) else xs inter ys; |
|
882 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
883 |
(*intersection, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
884 |
fun ([]:int list) inter_int ys = [] |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
885 |
| (x :: xs) inter_int ys = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
886 |
if x mem_int ys then x :: (xs inter_int ys) else xs inter_int ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
887 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
888 |
(*intersection, optimized version for strings *) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
889 |
fun ([]:string list) inter_string ys = [] |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
890 |
| (x :: xs) inter_string ys = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
891 |
if x mem_string ys then x :: (xs inter_string ys) else xs inter_string ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
892 |
|
7090 | 893 |
(*generalized intersection*) |
894 |
fun gen_inter eq ([], ys) = [] |
|
12284 | 895 |
| gen_inter eq (x::xs, ys) = |
7090 | 896 |
if gen_mem eq (x,ys) then x :: gen_inter eq (xs, ys) |
12284 | 897 |
else gen_inter eq (xs, ys); |
7090 | 898 |
|
233 | 899 |
|
900 |
(*subset*) |
|
901 |
fun [] subset ys = true |
|
902 |
| (x :: xs) subset ys = x mem ys andalso xs subset ys; |
|
903 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
904 |
(*subset, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
905 |
fun ([]:int list) subset_int ys = true |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
906 |
| (x :: xs) subset_int ys = x mem_int ys andalso xs subset_int ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
907 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
908 |
(*subset, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
909 |
fun ([]:string list) subset_string ys = true |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
910 |
| (x :: xs) subset_string ys = x mem_string ys andalso xs subset_string ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
911 |
|
4363 | 912 |
(*set equality*) |
913 |
fun eq_set (xs, ys) = |
|
914 |
xs = ys orelse (xs subset ys andalso ys subset xs); |
|
915 |
||
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
916 |
(*set equality for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
917 |
fun eq_set_string ((xs:string list), ys) = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
918 |
xs = ys orelse (xs subset_string ys andalso ys subset_string xs); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
919 |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
920 |
fun gen_subset eq (xs, ys) = forall (fn x => gen_mem eq (x, ys)) xs; |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
921 |
|
265 | 922 |
|
233 | 923 |
(*removing an element from a list WITHOUT duplicates*) |
924 |
fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x) |
|
925 |
| [] \ x = []; |
|
926 |
||
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
927 |
fun ys \\ xs = foldl (op \) (ys,xs); |
0 | 928 |
|
233 | 929 |
(*removing an element from a list -- possibly WITH duplicates*) |
930 |
fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs; |
|
12295 | 931 |
fun gen_rems eq (xs, ys) = filter_out (fn x => gen_mem eq (x, ys)) xs; |
233 | 932 |
|
933 |
||
934 |
(*makes a list of the distinct members of the input; preserves order, takes |
|
935 |
first of equal elements*) |
|
936 |
fun gen_distinct eq lst = |
|
937 |
let |
|
938 |
val memb = gen_mem eq; |
|
0 | 939 |
|
233 | 940 |
fun dist (rev_seen, []) = rev rev_seen |
941 |
| dist (rev_seen, x :: xs) = |
|
942 |
if memb (x, rev_seen) then dist (rev_seen, xs) |
|
943 |
else dist (x :: rev_seen, xs); |
|
944 |
in |
|
945 |
dist ([], lst) |
|
946 |
end; |
|
947 |
||
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
948 |
fun distinct l = gen_distinct (op =) l; |
233 | 949 |
|
950 |
||
951 |
(*returns the tail beginning with the first repeated element, or []*) |
|
952 |
fun findrep [] = [] |
|
953 |
| findrep (x :: xs) = if x mem xs then x :: xs else findrep xs; |
|
954 |
||
955 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
956 |
(*returns a list containing all repeated elements exactly once; preserves |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
957 |
order, takes first of equal elements*) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
958 |
fun gen_duplicates eq lst = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
959 |
let |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
960 |
val memb = gen_mem eq; |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
961 |
|
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
962 |
fun dups (rev_dups, []) = rev rev_dups |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
963 |
| dups (rev_dups, x :: xs) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
964 |
if memb (x, rev_dups) orelse not (memb (x, xs)) then |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
965 |
dups (rev_dups, xs) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
966 |
else dups (x :: rev_dups, xs); |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
967 |
in |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
968 |
dups ([], lst) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
969 |
end; |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
970 |
|
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
971 |
fun duplicates l = gen_duplicates (op =) l; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
972 |
|
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
973 |
|
233 | 974 |
|
975 |
(** association lists **) |
|
0 | 976 |
|
233 | 977 |
(*association list lookup*) |
15531 | 978 |
fun assoc ([], key) = NONE |
233 | 979 |
| assoc ((keyi, xi) :: pairs, key) = |
15531 | 980 |
if key = keyi then SOME xi else assoc (pairs, key); |
233 | 981 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
982 |
(*association list lookup, optimized version for ints*) |
15531 | 983 |
fun assoc_int ([], (key:int)) = NONE |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
984 |
| assoc_int ((keyi, xi) :: pairs, key) = |
15531 | 985 |
if key = keyi then SOME xi else assoc_int (pairs, key); |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
986 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
987 |
(*association list lookup, optimized version for strings*) |
15531 | 988 |
fun assoc_string ([], (key:string)) = NONE |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
989 |
| assoc_string ((keyi, xi) :: pairs, key) = |
15531 | 990 |
if key = keyi then SOME xi else assoc_string (pairs, key); |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
991 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
992 |
(*association list lookup, optimized version for string*ints*) |
15531 | 993 |
fun assoc_string_int ([], (key:string*int)) = NONE |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
994 |
| assoc_string_int ((keyi, xi) :: pairs, key) = |
15531 | 995 |
if key = keyi then SOME xi else assoc_string_int (pairs, key); |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
996 |
|
233 | 997 |
fun assocs ps x = |
998 |
(case assoc (ps, x) of |
|
15531 | 999 |
NONE => [] |
1000 |
| SOME ys => ys); |
|
233 | 1001 |
|
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
1002 |
(*two-fold association list lookup*) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
1003 |
fun assoc2 (aal, (key1, key2)) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
1004 |
(case assoc (aal, key1) of |
15531 | 1005 |
SOME al => assoc (al, key2) |
1006 |
| NONE => NONE); |
|
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
1007 |
|
233 | 1008 |
(*generalized association list lookup*) |
15531 | 1009 |
fun gen_assoc eq ([], key) = NONE |
233 | 1010 |
| gen_assoc eq ((keyi, xi) :: pairs, key) = |
15531 | 1011 |
if eq (key, keyi) then SOME xi else gen_assoc eq (pairs, key); |
233 | 1012 |
|
1013 |
(*association list update*) |
|
1014 |
fun overwrite (al, p as (key, _)) = |
|
1015 |
let fun over ((q as (keyi, _)) :: pairs) = |
|
1016 |
if keyi = key then p :: pairs else q :: (over pairs) |
|
1017 |
| over [] = [p] |
|
1018 |
in over al end; |
|
1019 |
||
2522 | 1020 |
fun gen_overwrite eq (al, p as (key, _)) = |
1021 |
let fun over ((q as (keyi, _)) :: pairs) = |
|
1022 |
if eq (keyi, key) then p :: pairs else q :: (over pairs) |
|
1023 |
| over [] = [p] |
|
1024 |
in over al end; |
|
1025 |
||
233 | 1026 |
|
12284 | 1027 |
(* lists as tables *) |
233 | 1028 |
|
12284 | 1029 |
fun gen_merge_lists _ xs [] = xs |
1030 |
| gen_merge_lists _ [] ys = ys |
|
1031 |
| gen_merge_lists eq xs ys = xs @ gen_rems eq (ys, xs); |
|
0 | 1032 |
|
12284 | 1033 |
fun gen_merge_lists' _ xs [] = xs |
1034 |
| gen_merge_lists' _ [] ys = ys |
|
12295 | 1035 |
| gen_merge_lists' eq xs ys = gen_rems eq (ys, xs) @ xs; |
0 | 1036 |
|
12284 | 1037 |
fun merge_lists xs ys = gen_merge_lists (op =) xs ys; |
1038 |
fun merge_lists' xs ys = gen_merge_lists' (op =) xs ys; |
|
1039 |
fun merge_alists al = gen_merge_lists eq_fst al; |
|
15263 | 1040 |
fun merge_alists' al = gen_merge_lists' eq_fst al; |
380 | 1041 |
|
0 | 1042 |
|
1043 |
||
233 | 1044 |
(** balanced trees **) |
1045 |
||
1046 |
exception Balance; (*indicates non-positive argument to balancing fun*) |
|
1047 |
||
1048 |
(*balanced folding; avoids deep nesting*) |
|
1049 |
fun fold_bal f [x] = x |
|
1050 |
| fold_bal f [] = raise Balance |
|
1051 |
| fold_bal f xs = |
|
13629 | 1052 |
let val (ps,qs) = splitAt(length xs div 2, xs) |
1053 |
in f (fold_bal f ps, fold_bal f qs) end; |
|
233 | 1054 |
|
1055 |
(*construct something of the form f(...g(...(x)...)) for balanced access*) |
|
1056 |
fun access_bal (f, g, x) n i = |
|
1057 |
let fun acc n i = (*1<=i<=n*) |
|
1058 |
if n=1 then x else |
|
1059 |
let val n2 = n div 2 |
|
1060 |
in if i<=n2 then f (acc n2 i) |
|
1061 |
else g (acc (n-n2) (i-n2)) |
|
1062 |
end |
|
1063 |
in if 1<=i andalso i<=n then acc n i else raise Balance end; |
|
1064 |
||
1065 |
(*construct ALL such accesses; could try harder to share recursive calls!*) |
|
1066 |
fun accesses_bal (f, g, x) n = |
|
1067 |
let fun acc n = |
|
1068 |
if n=1 then [x] else |
|
1069 |
let val n2 = n div 2 |
|
1070 |
val acc2 = acc n2 |
|
1071 |
in if n-n2=n2 then map f acc2 @ map g acc2 |
|
1072 |
else map f acc2 @ map g (acc (n-n2)) end |
|
1073 |
in if 1<=n then acc n else raise Balance end; |
|
1074 |
||
1075 |
||
1076 |
||
2506 | 1077 |
(** orders **) |
1078 |
||
4445 | 1079 |
fun rev_order LESS = GREATER |
1080 |
| rev_order EQUAL = EQUAL |
|
1081 |
| rev_order GREATER = LESS; |
|
1082 |
||
4479 | 1083 |
(*assume rel is a linear strict order*) |
4445 | 1084 |
fun make_ord rel (x, y) = |
1085 |
if rel (x, y) then LESS |
|
1086 |
else if rel (y, x) then GREATER |
|
1087 |
else EQUAL; |
|
1088 |
||
15051
0dbbab9f77fe
int_ord = Int.compare, string_ord = String.compare;
wenzelm
parents:
15035
diff
changeset
|
1089 |
val int_ord = Int.compare; |
0dbbab9f77fe
int_ord = Int.compare, string_ord = String.compare;
wenzelm
parents:
15035
diff
changeset
|
1090 |
val string_ord = String.compare; |
2506 | 1091 |
|
4343 | 1092 |
(*lexicographic product*) |
1093 |
fun prod_ord a_ord b_ord ((x, y), (x', y')) = |
|
1094 |
(case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord); |
|
1095 |
||
1096 |
(*dictionary order -- in general NOT well-founded!*) |
|
1097 |
fun dict_ord _ ([], []) = EQUAL |
|
1098 |
| dict_ord _ ([], _ :: _) = LESS |
|
1099 |
| dict_ord _ (_ :: _, []) = GREATER |
|
1100 |
| dict_ord elem_ord (x :: xs, y :: ys) = |
|
1101 |
(case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord); |
|
1102 |
||
1103 |
(*lexicographic product of lists*) |
|
1104 |
fun list_ord elem_ord (xs, ys) = |
|
1105 |
prod_ord int_ord (dict_ord elem_ord) ((length xs, xs), (length ys, ys)); |
|
1106 |
||
2506 | 1107 |
|
4621 | 1108 |
(* sorting *) |
1109 |
||
1110 |
(*quicksort (stable, i.e. does not reorder equal elements)*) |
|
1111 |
fun sort ord = |
|
1112 |
let |
|
1113 |
fun qsort xs = |
|
1114 |
let val len = length xs in |
|
1115 |
if len <= 1 then xs |
|
1116 |
else |
|
1117 |
let val (lts, eqs, gts) = part (nth_elem (len div 2, xs)) xs in |
|
1118 |
qsort lts @ eqs @ qsort gts |
|
1119 |
end |
|
1120 |
end |
|
1121 |
and part _ [] = ([], [], []) |
|
1122 |
| part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs) |
|
1123 |
and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts) |
|
1124 |
| add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts) |
|
1125 |
| add GREATER x (lts, eqs, gts) = (lts, eqs, x :: gts); |
|
1126 |
in qsort end; |
|
1127 |
||
1128 |
(*sort strings*) |
|
1129 |
val sort_strings = sort string_ord; |
|
1130 |
fun sort_wrt sel xs = sort (string_ord o pairself sel) xs; |
|
1131 |
||
11514 | 1132 |
fun unique_strings ([]: string list) = [] |
1133 |
| unique_strings [x] = [x] |
|
1134 |
| unique_strings (x :: y :: ys) = |
|
1135 |
if x = y then unique_strings (y :: ys) |
|
1136 |
else x :: unique_strings (y :: ys); |
|
4621 | 1137 |
|
2506 | 1138 |
|
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1139 |
(** random numbers **) |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1140 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1141 |
exception RANDOM; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1142 |
|
14618
068bb99f3ebd
Fixed bug in rmod that caused an overflow exception in SML/NJ.
berghofe
parents:
14472
diff
changeset
|
1143 |
fun rmod x y = x - y * Real.realFloor (x / y); |
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1144 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1145 |
local |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1146 |
val a = 16807.0; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1147 |
val m = 2147483647.0; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1148 |
val random_seed = ref 1.0; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1149 |
in |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1150 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1151 |
fun random () = |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1152 |
let val r = rmod (a * !random_seed) m |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1153 |
in (random_seed := r; r) end; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1154 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1155 |
end; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1156 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1157 |
fun random_range l h = |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1158 |
if h < l orelse l < 0 then raise RANDOM |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1159 |
else l + Real.floor (rmod (random ()) (real (h - l + 1))); |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1160 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1161 |
fun one_of xs = nth_elem (random_range 0 (length xs - 1), xs); |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1162 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1163 |
fun frequency xs = |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1164 |
let |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1165 |
val sum = foldl op + (0, map fst xs); |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1166 |
fun pick n ((k, x) :: xs) = |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1167 |
if n <= k then x else pick (n - k) xs |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1168 |
in pick (random_range 1 sum) xs end; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1169 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1170 |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
1171 |
(** current directory **) |
233 | 1172 |
|
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
1173 |
val cd = OS.FileSys.chDir; |
2317 | 1174 |
val pwd = OS.FileSys.getDir; |
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
1175 |
|
3606 | 1176 |
|
233 | 1177 |
|
10692 | 1178 |
(** rational numbers **) |
1179 |
||
1180 |
datatype rat = Rat of bool * int * int |
|
1181 |
||
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
1182 |
exception RAT of string; |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
1183 |
|
10692 | 1184 |
fun rep_rat(Rat(a,p,q)) = (if a then p else ~p,q) |
1185 |
||
1186 |
fun ratnorm(a,p,q) = if p=0 then Rat(a,0,1) else |
|
1187 |
let val absp = abs p |
|
1188 |
val m = gcd(absp,q) |
|
1189 |
in Rat(a = (0 <= p), absp div m, q div m) end; |
|
1190 |
||
1191 |
fun ratadd(Rat(a,p,q),Rat(b,r,s)) = |
|
1192 |
let val den = lcm(q,s) |
|
1193 |
val p = p*(den div q) and r = r*(den div s) |
|
1194 |
val num = (if a then p else ~p) + (if b then r else ~r) |
|
1195 |
in ratnorm(true,num,den) end; |
|
1196 |
||
1197 |
fun ratmul(Rat(a,p,q),Rat(b,r,s)) = ratnorm(a=b,p*r,q*s) |
|
1198 |
||
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
1199 |
fun ratinv(Rat(a,p,q)) = if p=0 then raise RAT "ratinv" else Rat(a,q,p) |
10692 | 1200 |
|
1201 |
fun int_ratdiv(p,q) = |
|
14826
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
1202 |
if q=0 then raise RAT "int_ratdiv" else ratnorm(0<=q, p, abs q) |
10692 | 1203 |
|
1204 |
fun ratneg(Rat(b,p,q)) = Rat(not b,p,q); |
|
1205 |
||
1206 |
fun rat_of_int i = if i < 0 then Rat(false,abs i,1) else Rat(true,i,1); |
|
1207 |
||
1208 |
||
4621 | 1209 |
(** misc **) |
233 | 1210 |
|
1211 |
(*use the keyfun to make a list of (x, key) pairs*) |
|
0 | 1212 |
fun make_keylist (keyfun: 'a->'b) : 'a list -> ('a * 'b) list = |
233 | 1213 |
let fun keypair x = (x, keyfun x) |
1214 |
in map keypair end; |
|
0 | 1215 |
|
233 | 1216 |
(*given a list of (x, key) pairs and a searchkey |
0 | 1217 |
return the list of xs from each pair whose key equals searchkey*) |
1218 |
fun keyfilter [] searchkey = [] |
|
233 | 1219 |
| keyfilter ((x, key) :: pairs) searchkey = |
1220 |
if key = searchkey then x :: keyfilter pairs searchkey |
|
1221 |
else keyfilter pairs searchkey; |
|
0 | 1222 |
|
1223 |
||
1224 |
(*Partition list into elements that satisfy predicate and those that don't. |
|
233 | 1225 |
Preserves order of elements in both lists.*) |
15570 | 1226 |
val partition = List.partition; |
0 | 1227 |
|
1228 |
||
1229 |
fun partition_eq (eq:'a * 'a -> bool) = |
|
1230 |
let fun part [] = [] |
|
233 | 1231 |
| part (x::ys) = let val (xs, xs') = partition (apl(x, eq)) ys |
1232 |
in (x::xs)::(part xs') end |
|
0 | 1233 |
in part end; |
1234 |
||
1235 |
||
233 | 1236 |
(*Partition a list into buckets [ bi, b(i+1), ..., bj ] |
0 | 1237 |
putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) |
1238 |
fun partition_list p i j = |
|
233 | 1239 |
let fun part k xs = |
1240 |
if k>j then |
|
0 | 1241 |
(case xs of [] => [] |
15570 | 1242 |
| _ => raise Fail "partition_list") |
0 | 1243 |
else |
233 | 1244 |
let val (ns, rest) = partition (p k) xs; |
1245 |
in ns :: part(k+1)rest end |
|
0 | 1246 |
in part i end; |
1247 |
||
1248 |
||
233 | 1249 |
(* generating identifiers *) |
0 | 1250 |
|
4063 | 1251 |
(** Freshly generated identifiers; supplied prefix MUST start with a letter **) |
0 | 1252 |
local |
4063 | 1253 |
(*Maps 0-63 to A-Z, a-z, 0-9 or _ or ' for generating random identifiers*) |
1254 |
fun char i = if i<26 then chr (ord "A" + i) |
|
5904 | 1255 |
else if i<52 then chr (ord "a" + i - 26) |
1256 |
else if i<62 then chr (ord"0" + i - 52) |
|
1257 |
else if i=62 then "_" |
|
1258 |
else (*i=63*) "'"; |
|
4063 | 1259 |
|
1260 |
val charVec = Vector.tabulate (64, char); |
|
1261 |
||
5904 | 1262 |
fun newid n = |
1263 |
let |
|
4284 | 1264 |
in implode (map (fn i => Vector.sub(charVec,i)) (radixpand (64,n))) end; |
2003 | 1265 |
|
4284 | 1266 |
val seedr = ref 0; |
0 | 1267 |
|
4063 | 1268 |
in |
4284 | 1269 |
|
12346 | 1270 |
fun gensym pre = pre ^ (#1(newid (!seedr), inc seedr)); |
2003 | 1271 |
|
4063 | 1272 |
end; |
1273 |
||
1274 |
||
233 | 1275 |
(* lexical scanning *) |
0 | 1276 |
|
233 | 1277 |
(*scan a list of characters into "words" composed of "letters" (recognized by |
1278 |
is_let) and separated by any number of non-"letters"*) |
|
1279 |
fun scanwords is_let cs = |
|
0 | 1280 |
let fun scan1 [] = [] |
233 | 1281 |
| scan1 cs = |
1282 |
let val (lets, rest) = take_prefix is_let cs |
|
1283 |
in implode lets :: scanwords is_let rest end; |
|
1284 |
in scan1 (#2 (take_prefix (not o is_let) cs)) end; |
|
24
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset
|
1285 |
|
4212 | 1286 |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1287 |
end; |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1288 |
|
15570 | 1289 |
structure OpenLibrary: LIBRARY = Library; |
1290 |
open OpenLibrary; |