author  haftmann 
Fri, 21 Oct 2005 14:49:49 +0200  
changeset 17952  00eccd84608f 
parent 17822  3830b0a41d51 
child 18011  685d95c793ff 
permissions  rwrr 
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 
16188  4 
Author: Markus Wenzel, TU Muenchen 
0  5 

233  6 
Basic library: functions, options, pairs, booleans, lists, integers, 
17952
00eccd84608f
abandoned rational number functions in favor of General/rat.ML
haftmann
parents:
17822
diff
changeset

7 
strings, lists as sets, association lists, generic 
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

8 
tables, balanced trees, orders, current directory, misc. 
0  9 
*) 
10 

17257
0ab67cb765da
introduced binding priority 1 for linear combinators etc.
haftmann
parents:
17153
diff
changeset

11 
infix 1 > > > >> >> >>> #> #>; 
0ab67cb765da
introduced binding priority 1 for linear combinators etc.
haftmann
parents:
17153
diff
changeset

12 
infix 2 ?; 
0ab67cb765da
introduced binding priority 1 for linear combinators etc.
haftmann
parents:
17153
diff
changeset

13 
infix 3 o oo ooo oooo; 
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

14 

17257
0ab67cb765da
introduced binding priority 1 for linear combinators etc.
haftmann
parents:
17153
diff
changeset

15 
infix 4 ~~ upto downto; 
0ab67cb765da
introduced binding priority 1 for linear combinators etc.
haftmann
parents:
17153
diff
changeset

16 
infix orf andf prefix \ \\ ins ins_string ins_int mem mem_int mem_string union union_int 
0ab67cb765da
introduced binding priority 1 for linear combinators etc.
haftmann
parents:
17153
diff
changeset

17 
union_string inter inter_int inter_string subset subset_int subset_string; 
5893  18 

15745  19 
signature BASIC_LIBRARY = 
4621  20 
sig 
21 
(*functions*) 

16842  22 
val I: 'a > 'a 
23 
val K: 'a > 'b > 'a 

4621  24 
val curry: ('a * 'b > 'c) > 'a > 'b > 'c 
25 
val uncurry: ('a > 'b > 'c) > 'a * 'b > 'c 

26 
val > : 'a * ('a > 'b) > 'b 

16691
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

27 
val > : ('c * 'a) * ('c > 'a > 'b) > 'b 
16780
aa284c1b72ad
fold_map > fold_yield, added transformator combinators, added selector combinator
haftmann
parents:
16721
diff
changeset

28 
val > : ('c * 'a) * ('a > 'b) > 'c * 'b 
aa284c1b72ad
fold_map > fold_yield, added transformator combinators, added selector combinator
haftmann
parents:
16721
diff
changeset

29 
val >> : ('c * 'a) * ('a > 'd * 'b) > ('c * 'd) * 'b 
16691
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

30 
val >> : ('a * 'c) * ('a > 'b) > 'b * 'c 
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

31 
val >>> : ('a * 'c) * ('a > 'b * 'd) > 'b * ('c * 'd) 
16780
aa284c1b72ad
fold_map > fold_yield, added transformator combinators, added selector combinator
haftmann
parents:
16721
diff
changeset

32 
val #> : ('a > 'b) * ('b > 'c) > 'a > 'c 
aa284c1b72ad
fold_map > fold_yield, added transformator combinators, added selector combinator
haftmann
parents:
16721
diff
changeset

33 
val #> : ('a > 'c * 'b) * ('c > 'b > 'd) > 'a > 'd 
17141
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset

34 
val ? : ('a > bool) * ('a > 'a) > 'a > 'a 
16825  35 
val ` : ('b > 'a) > 'b > 'a * 'b 
17101  36 
val tap: ('b > 'a) > 'b > 'b 
16721  37 
val oo: ('a > 'b) * ('c > 'd > 'a) > 'c > 'd > 'b 
38 
val ooo: ('a > 'b) * ('c > 'd > 'e > 'a) > 'c > 'd > 'e > 'b 

39 
val oooo: ('a > 'b) * ('c > 'd > 'e > 'f > 'a) > 'c > 'd > 'e > 'f > 'b 

16842  40 
val funpow: int > ('a > 'a) > 'a > 'a 
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

41 

15670
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

42 
(*old options  invalidated*) 
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

43 
datatype invalid = None of invalid 
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

44 
exception OPTION of invalid 
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

45 

15970  46 
(*options*) 
47 
val the: 'a option > 'a 

17153  48 
val these: 'a list option > 'a list 
17313  49 
val the_default: 'a > 'a option > 'a 
50 
val the_list: 'a option > 'a list 

15970  51 
val if_none: 'a option > 'a > 'a 
52 
val is_some: 'a option > bool 

53 
val is_none: 'a option > bool 

54 

17341  55 
exception EXCEPTION of exn * 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

56 
exception ERROR 
6959  57 
val try: ('a > 'b) > 'a > 'b option 
4621  58 
val can: ('a > 'b) > 'a > bool 
14868  59 
datatype 'a result = Result of 'a  Exn of exn 
60 
val capture: ('a > 'b) > 'a > 'b result 

61 
val release: 'a result > 'a 

62 
val get_result: 'a result > 'a option 

63 
val get_exn: 'a result > exn option 

4621  64 

65 
(*pairs*) 

66 
val pair: 'a > 'b > 'a * 'b 

67 
val rpair: 'a > 'b > 'b * 'a 

68 
val fst: 'a * 'b > 'a 

69 
val snd: 'a * 'b > 'b 

17498
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset

70 
val eq_fst: ('a * 'c > bool) > ('a * 'b) * ('c * 'd) > bool 
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset

71 
val eq_snd: ('b * 'd > bool) > ('a * 'b) * ('c * 'd) > bool 
4621  72 
val swap: 'a * 'b > 'b * 'a 
73 
val apfst: ('a > 'b) > 'a * 'c > 'b * 'c 

74 
val apsnd: ('a > 'b) > 'c * 'a > 'c * 'b 

75 
val pairself: ('a > 'b) > 'a * 'a > 'b * 'b 

76 

77 
(*booleans*) 

78 
val equal: ''a > ''a > bool 

79 
val not_equal: ''a > ''a > bool 

80 
val orf: ('a > bool) * ('a > bool) > 'a > bool 

81 
val andf: ('a > bool) * ('a > bool) > 'a > bool 

82 
val exists: ('a > bool) > 'a list > bool 

83 
val forall: ('a > bool) > 'a list > bool 

84 
val set: bool ref > bool 

85 
val reset: bool ref > bool 

86 
val toggle: bool ref > bool 

9118  87 
val change: 'a ref > ('a > 'a) > unit 
4621  88 
val setmp: 'a ref > 'a > ('b > 'c) > 'b > 'c 
11853  89 
val conditional: bool > (unit > unit) > unit 
4621  90 

91 
(*lists*) 

15570  92 
exception UnequalLengths 
4621  93 
val cons: 'a > 'a list > 'a list 
5285  94 
val single: 'a > 'a list 
4629  95 
val append: 'a list > 'a list > 'a list 
5904  96 
val apply: ('a > 'a) list > 'a > 'a 
14792  97 
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

98 
val fold_rev: ('a > 'b > 'b) > 'a list > 'b > 'b 
16869  99 
val fold_map: ('a > 'b > 'c * 'b) > 'a list > 'b > 'c list * 'b 
4956
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

100 
val foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list 
15760  101 
val foldr1: ('a * 'a > 'a) > 'a list > 'a 
11002  102 
val foldln: ('a * int > 'b > 'b) > 'a list > 'b > 'b 
15570  103 
val unflat: 'a list list > 'b list > 'b list list 
13629  104 
val splitAt: int * 'a list > 'a list * 'a list 
4713  105 
val dropwhile: ('a > bool) > 'a list > 'a list 
11773  106 
val map_nth_elem: int > ('a > 'a) > 'a list > 'a list 
4621  107 
val split_last: 'a list > 'a list * 'a 
4893  108 
val nth_update: 'a > int * 'a list > 'a list 
4621  109 
val find_index: ('a > bool) > 'a list > int 
110 
val find_index_eq: ''a > ''a list > int 

111 
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

112 
val get_first: ('a > 'b option) > 'a list > 'b option 
4621  113 
val separate: 'a > 'a list > 'a list 
114 
val replicate: int > 'a > 'a list 

115 
val multiply: 'a list * 'a list list > 'a list list 

14792  116 
val product: 'a list > 'b list > ('a * 'b) list 
16129  117 
val filter: ('a > bool) > 'a list > 'a list 
4621  118 
val filter_out: ('a > bool) > 'a list > 'a list 
119 
val map2: ('a * 'b > 'c) > 'a list * 'b list > 'c list 

120 
val exists2: ('a * 'b > bool) > 'a list * 'b list > bool 

121 
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

122 
val seq2: ('a * 'b > unit) > 'a list * 'b list > unit 
4621  123 
val ~~ : 'a list * 'b list > ('a * 'b) list 
124 
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

125 
val equal_lists: ('a * 'b > bool) > 'a list * 'b list > bool 
4621  126 
val prefix: ''a list * ''a list > bool 
127 
val take_prefix: ('a > bool) > 'a list > 'a list * 'a list 

128 
val take_suffix: ('a > bool) > 'a list > 'a list * 'a list 

12249  129 
val prefixes1: 'a list > 'a list list 
130 
val suffixes1: 'a list > 'a list list 

4621  131 

132 
(*integers*) 

15965
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15760
diff
changeset

133 
val gcd: IntInf.int * IntInf.int > IntInf.int 
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15760
diff
changeset

134 
val lcm: IntInf.int * IntInf.int > IntInf.int 
4621  135 
val inc: int ref > int 
136 
val dec: int ref > int 

137 
val upto: int * int > int list 

138 
val downto: int * int > int list 

139 
val downto0: int list * int > bool 

140 
val radixpand: int * int > int list 

141 
val radixstring: int * string * int > string 

142 
val string_of_int: int > string 

143 
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

144 
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

145 
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

146 
val oct_char: string > string 
4621  147 

148 
(*strings*) 

6312  149 
val nth_elem_string: int * string > string 
16188  150 
val fold_string: (string > 'a > 'a) > string > 'a > 'a 
6312  151 
val exists_string: (string > bool) > string > bool 
16188  152 
val forall_string: (string > bool) > string > bool 
4621  153 
val enclose: string > string > string > string 
6642  154 
val unenclose: string > string 
4621  155 
val quote: string > string 
156 
val space_implode: string > string list > string 

157 
val commas: string list > string 

158 
val commas_quote: string list > string 

159 
val cat_lines: string list > string 

160 
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

161 
val split_lines: string > string list 
5942  162 
val prefix_lines: string > string > string 
7712  163 
val untabify: string list > string list 
5285  164 
val suffix: string > string > string 
165 
val unsuffix: string > string > string 

15060  166 
val unprefix: string > string > string 
10951  167 
val replicate_string: int > string > string 
14926  168 
val translate_string: (string > string) > string > string 
4621  169 

16492  170 
(*lists as sets  see also Pure/General/ord_list.ML*) 
4621  171 
val mem: ''a * ''a list > bool 
172 
val mem_int: int * int list > bool 

173 
val mem_string: string * string list > bool 

174 
val gen_mem: ('a * 'b > bool) > 'a * 'b list > bool 

175 
val ins: ''a * ''a list > ''a list 

176 
val ins_int: int * int list > int list 

177 
val ins_string: string * string list > string list 

178 
val gen_ins: ('a * 'a > bool) > 'a * 'a list > 'a list 

16492  179 
val member: ('b * 'a > bool) > 'a list > 'b > bool 
15760  180 
val insert: ('a * 'a > bool) > 'a > 'a list > 'a list 
16129  181 
val remove: ('b * 'a > bool) > 'b > 'a list > 'a list 
4621  182 
val union: ''a list * ''a list > ''a list 
183 
val union_int: int list * int list > int list 

184 
val union_string: string list * string list > string list 

185 
val gen_union: ('a * 'a > bool) > 'a list * 'a list > 'a list 

7090  186 
val gen_inter: ('a * 'b > bool) > 'a list * 'b list > 'a list 
4621  187 
val inter: ''a list * ''a list > ''a list 
188 
val inter_int: int list * int list > int list 

189 
val inter_string: string list * string list > string list 

190 
val subset: ''a list * ''a list > bool 

191 
val subset_int: int list * int list > bool 

192 
val subset_string: string list * string list > bool 

193 
val eq_set: ''a list * ''a list > bool 

194 
val eq_set_string: string list * string list > bool 

195 
val gen_subset: ('a * 'b > bool) > 'a list * 'b list > bool 

196 
val \ : ''a list * ''a > ''a list 

197 
val \\ : ''a list * ''a list > ''a list 

198 
val gen_rem: ('a * 'b > bool) > 'a list * 'b > 'a list 

199 
val gen_rems: ('a * 'b > bool) > 'a list * 'b list > 'a list 

200 
val gen_distinct: ('a * 'a > bool) > 'a list > 'a list 

201 
val distinct: ''a list > ''a list 

202 
val findrep: ''a list > ''a list 

203 
val gen_duplicates: ('a * 'a > bool) > 'a list > 'a list 

204 
val duplicates: ''a list > ''a list 

16878  205 
val has_duplicates: ('a * 'a > bool) > 'a list > bool 
4621  206 

17540  207 
(*lists as tables  see also Pure/General/alist.ML*) 
12284  208 
val gen_merge_lists: ('a * 'a > bool) > 'a list > 'a list > 'a list 
209 
val gen_merge_lists': ('a * 'a > bool) > 'a list > 'a list > 'a list 

4621  210 
val merge_lists: ''a list > ''a list > ''a list 
12284  211 
val merge_lists': ''a list > ''a list > ''a list 
4692  212 
val merge_alists: (''a * 'b) list > (''a * 'b) list > (''a * 'b) list 
15263  213 
val merge_alists': (''a * 'b) list > (''a * 'b) list > (''a * 'b) list 
4621  214 

215 
(*balanced trees*) 

216 
exception Balance 

217 
val fold_bal: ('a * 'a > 'a) > 'a list > 'a 

218 
val access_bal: ('a > 'a) * ('a > 'a) * 'a > int > int > 'a 

219 
val accesses_bal: ('a > 'a) * ('a > 'a) * 'a > int > 'a list 

220 

221 
(*orders*) 

222 
val rev_order: order > order 

223 
val make_ord: ('a * 'a > bool) > 'a * 'a > order 

224 
val int_ord: int * int > order 

225 
val string_ord: string * string > order 

16676  226 
val fast_string_ord: string * string > order 
16492  227 
val option_ord: ('a * 'b > order) > 'a option * 'b option > order 
4621  228 
val prod_ord: ('a * 'b > order) > ('c * 'd > order) > ('a * 'c) * ('b * 'd) > order 
229 
val dict_ord: ('a * 'b > order) > 'a list * 'b list > order 

230 
val list_ord: ('a * 'b > order) > 'a list * 'b list > order 

231 
val sort: ('a * 'a > order) > 'a list > 'a list 

232 
val sort_strings: string list > string list 

233 
val sort_wrt: ('a > string) > 'a list > 'a list 

11514  234 
val unique_strings: string list > string list 
4621  235 

14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

236 
(*random numbers*) 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

237 
exception RANDOM 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

238 
val random: unit > real 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

239 
val random_range: int > int > int 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

240 
val one_of: 'a list > 'a 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

241 
val frequency: (int * 'a) list > 'a 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

242 

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

243 
(*current directory*) 
4621  244 
val cd: string > unit 
245 
val pwd: unit > string 

246 

247 
(*misc*) 

248 
val partition_eq: ('a * 'a > bool) > 'a list > 'a list list 

249 
val partition_list: (int > 'a > bool) > int > int > 'a list > 'a list list 

250 
val gensym: string > string 

251 
val scanwords: (string > bool) > string list > string list 

16439  252 
type stamp 
253 
val stamp: unit > stamp 

254 
type serial 

255 
val serial: unit > serial 

16535
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

256 
structure Object: sig type T end 
4621  257 
end; 
258 

15745  259 
signature LIBRARY = 
15570  260 
sig 
15745  261 
include BASIC_LIBRARY 
15570  262 
val foldl: ('a * 'b > 'a) > 'a * 'b list > 'a 
263 
val foldr: ('a * 'b > 'b) > 'a list * 'b > 'b 

264 
val take: int * 'a list > 'a list 

265 
val drop: int * 'a list > 'a list 

266 
val nth_elem: int * 'a list > 'a 

267 
val last_elem: 'a list > 'a 

268 
val flat: 'a list list > 'a list 

269 
val seq: ('a > unit) > 'a list > unit 

270 
val partition: ('a > bool) > 'a list > 'a list * 'a list 

271 
val mapfilter: ('a > 'b option) > 'a list > 'b list 

272 
end; 

273 

15745  274 
structure Library: LIBRARY = 
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

275 
struct 
0  276 

4995  277 

233  278 
(** functions **) 
0  279 

16842  280 
fun I x = x; 
281 
fun K x = fn _ => x; 

233  282 
fun curry f x y = f (x, y); 
283 
fun uncurry f (x, y) = f x y; 

0  284 

16705  285 
(*reverse application and structured results*) 
286 
fun x > f = f x; 

287 
fun (x, y) > f = f x y; 

288 
fun (x, y) >> f = (f x, y); 

289 
fun (x, y) > f = (x, f y); 

290 
fun (x, y) >>> f = let val (x', z) = f x in (x', (y, z)) end; 

291 
fun (x, y) >> f = let val (z, y') = f y in ((x, z), y') end; 

16842  292 

293 
(*reverse composition*) 

16780
aa284c1b72ad
fold_map > fold_yield, added transformator combinators, added selector combinator
haftmann
parents:
16721
diff
changeset

294 
fun f #> g = g o f; 
16842  295 
fun f #> g = uncurry g o f; 
296 

17141
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset

297 
(*conditional application*) 
17545  298 
fun b ? f = fn x => if b x then f x else x; 
17141
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset

299 

16842  300 
(*view results*) 
301 
fun `f = fn x => (f x, x); 

17545  302 
fun tap f = fn x => (f x; x); 
380  303 

16721  304 
(*composition with multiple args*) 
305 
fun (f oo g) x y = f (g x y); 

306 
fun (f ooo g) x y z = f (g x y z); 

307 
fun (f oooo g) x y z w = f (g x y z w); 

308 

233  309 
(*function exponentiation: f(...(f x)...) with n applications of f*) 
310 
fun funpow n f x = 

311 
let fun rep (0, x) = x 

312 
 rep (n, x) = rep (n  1, f x) 

313 
in rep (n, x) end; 

160  314 

315 

233  316 
(** options **) 
0  317 

15670
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

318 
(*invalidate former constructors to prevent accidental use as matchall patterns!*) 
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

319 
datatype invalid = None of invalid; 
963cd3f7976c
invalidated former constructors None/OPTION to prevent accidental use as matchall patterns!
wenzelm
parents:
15570
diff
changeset

320 
exception OPTION of invalid; 
0  321 

15970  322 
val the = Option.valOf; 
17341  323 

17153  324 
fun these (SOME x) = x 
17313  325 
 these _ = []; 
17341  326 

17313  327 
fun the_default x (SOME y) = y 
328 
 the_default x _ = x; 

17341  329 

17313  330 
fun the_list (SOME x) = [x] 
331 
 the_list _ = [] 

15970  332 

333 
(*strict!*) 

334 
fun if_none NONE y = y 

335 
 if_none (SOME x) _ = x; 

336 

337 
fun is_some (SOME _) = true 

338 
 is_some NONE = false; 

339 

340 
fun is_none (SOME _) = false 

341 
 is_none NONE = true; 

342 

6959  343 

17341  344 
(* exceptions *) 
345 

346 
exception EXCEPTION of exn * string; 

6959  347 

348 
exception ERROR; 

349 

15531  350 
fun try f x = SOME (f x) 
351 
handle Interrupt => raise Interrupt  ERROR => raise ERROR  _ => NONE; 

6959  352 

353 
fun can f x = is_some (try f x); 

4139  354 

355 

14868  356 
datatype 'a result = 
357 
Result of 'a  

358 
Exn of exn; 

359 

360 
fun capture f x = Result (f x) handle e => Exn e; 

361 

362 
fun release (Result y) = y 

363 
 release (Exn e) = raise e; 

364 

15531  365 
fun get_result (Result x) = SOME x 
366 
 get_result _ = NONE; 

14868  367 

15531  368 
fun get_exn (Exn exn) = SOME exn 
369 
 get_exn _ = NONE; 

14868  370 

371 

4139  372 

233  373 
(** pairs **) 
374 

375 
fun pair x y = (x, y); 

376 
fun rpair x y = (y, x); 

377 

378 
fun fst (x, y) = x; 

379 
fun snd (x, y) = y; 

380 

17498
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset

381 
fun eq_fst eq ((x1, _), (x2, _)) = eq (x1, x2); 
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset

382 
fun eq_snd eq ((_, y1), (_, y2)) = eq (y1, y2); 
233  383 

384 
fun swap (x, y) = (y, x); 

385 

4212  386 
(*apply function to components*) 
233  387 
fun apfst f (x, y) = (f x, y); 
388 
fun apsnd f (x, y) = (x, f y); 

4212  389 
fun pairself f (x, y) = (f x, f y); 
233  390 

391 

392 

393 
(** booleans **) 

394 

395 
(* equality *) 

396 

397 
fun equal x y = x = y; 

398 
fun not_equal x y = x <> y; 

399 

400 

401 
(* operators for combining predicates *) 

402 

16721  403 
fun p orf q = fn x => p x orelse q x; 
404 
fun p andf q = fn x => p x andalso q x; 

233  405 

406 

407 
(* predicates on lists *) 

408 

409 
(*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*) 

410 
fun exists (pred: 'a > bool) : 'a list > bool = 

411 
let fun boolf [] = false 

412 
 boolf (x :: xs) = pred x orelse boolf xs 

413 
in boolf end; 

414 

415 
(*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*) 

416 
fun forall (pred: 'a > bool) : 'a list > bool = 

417 
let fun boolf [] = true 

418 
 boolf (x :: xs) = pred x andalso boolf xs 

419 
in boolf end; 

0  420 

233  421 

380  422 
(* flags *) 
423 

424 
fun set flag = (flag := true; true); 

425 
fun reset flag = (flag := false; false); 

426 
fun toggle flag = (flag := not (! flag); ! flag); 

427 

9118  428 
fun change r f = r := f (! r); 
429 

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

430 
(*temporarily set flag, handling exceptions*) 
2978  431 
fun setmp flag value f x = 
2958  432 
let 
433 
val orig_value = ! flag; 

434 
fun return y = (flag := orig_value; y); 

435 
in 

436 
flag := value; 

437 
return (f x handle exn => (return (); raise exn)) 

438 
end; 

439 

380  440 

11853  441 
(* conditional execution *) 
442 

443 
fun conditional b f = if b then f () else (); 

444 

445 

233  446 

447 
(** lists **) 

448 

15570  449 
exception UnequalLengths; 
233  450 

451 
fun cons x xs = x :: xs; 

5285  452 
fun single x = [x]; 
233  453 

4629  454 
fun append xs ys = xs @ ys; 
455 

5904  456 
fun apply [] x = x 
457 
 apply (f :: fs) x = apply fs (f x); 

458 

233  459 

460 
(* fold *) 

461 

16654  462 
fun fold f = 
463 
let 

464 
fun fold_aux [] y = y 

465 
 fold_aux (x :: xs) y = fold_aux xs (f x y); 

466 
in fold_aux end; 

15760  467 

16654  468 
fun fold_rev f = 
469 
let 

470 
fun fold_aux [] y = y 

471 
 fold_aux (x :: xs) y = f x (fold_aux xs y); 

472 
in fold_aux end; 

473 

16869  474 
fun fold_map f = 
16691
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

475 
let 
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

476 
fun fold_aux [] y = ([], y) 
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

477 
 fold_aux (x :: xs) y = 
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

478 
let 
16705  479 
val (x', y') = f x y; 
480 
val (xs', y'') = fold_aux xs y'; 

16691
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

481 
in (x' :: xs', y'') end; 
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

482 
in fold_aux end; 
539b9cc282fa
added combinatros '>' and '>>' and fold_map fitting nicely to ST combinator '>'
haftmann
parents:
16688
diff
changeset

483 

233  484 
(*the following versions of fold are designed to fit nicely with infixes*) 
0  485 

233  486 
(* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn 
487 
for operators that associate to the left (TAIL RECURSIVE)*) 

488 
fun foldl (f: 'a * 'b > 'a) : 'a * 'b list > 'a = 

489 
let fun itl (e, []) = e 

490 
 itl (e, a::l) = itl (f(e, a), l) 

491 
in itl end; 

492 

493 
(* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e)) 

494 
for operators that associate to the right (not tail recursive)*) 

495 
fun foldr f (l, e) = 

496 
let fun itr [] = e 

497 
 itr (a::l) = f(a, itr l) 

498 
in itr l end; 

499 

500 
(* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n1] @ xn)) 

501 
for n > 0, operators that associate to the right (not tail recursive)*) 

502 
fun foldr1 f l = 

4181  503 
let fun itr [x] = x 
233  504 
 itr (x::l) = f(x, itr l) 
505 
in itr l end; 

506 

15760  507 
fun foldln f xs e = fst (foldl (fn ((e,i), x) => (f (x,i) e, i+1)) ((e,1),xs)); 
14792  508 

16705  509 
fun foldl_map f = 
510 
let 

511 
fun fold_aux (x, []) = (x, []) 

512 
 fold_aux (x, y :: ys) = 

513 
let 

514 
val (x', y') = f (x, y); 

515 
val (x'', ys') = fold_aux (x', ys); 

516 
in (x'', y' :: ys') end; 

517 
in fold_aux end; 

518 

233  519 

520 
(* basic list functions *) 

521 

522 
(*take the first n elements from a list*) 

523 
fun take (n, []) = [] 

524 
 take (n, x :: xs) = 

525 
if n > 0 then x :: take (n  1, xs) else []; 

526 

527 
(*drop the first n elements from a list*) 

528 
fun drop (n, []) = [] 

529 
 drop (n, x :: xs) = 

530 
if n > 0 then drop (n  1, xs) else x :: xs; 

0  531 

13629  532 
fun splitAt(n,[]) = ([],[]) 
533 
 splitAt(n,xs as x::ys) = 

534 
if n>0 then let val (ps,qs) = splitAt(n1,ys) in (x::ps,qs) end 

535 
else ([],xs) 

536 

4713  537 
fun dropwhile P [] = [] 
538 
 dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys; 

539 

233  540 
(*return nth element of a list, where 0 designates the first element; 
541 
raise EXCEPTION if list too short*) 

15570  542 
fun nth_elem (i,xs) = List.nth(xs,i); 
233  543 

11773  544 
fun map_nth_elem 0 f (x :: xs) = f x :: xs 
545 
 map_nth_elem n f (x :: xs) = x :: map_nth_elem (n  1) f xs 

15570  546 
 map_nth_elem _ _ [] = raise Subscript; 
11773  547 

233  548 
(*last element of a list*) 
15570  549 
val last_elem = List.last; 
233  550 

3762  551 
(*rear decomposition*) 
15570  552 
fun split_last [] = raise Empty 
3762  553 
 split_last [x] = ([], x) 
554 
 split_last (x :: xs) = apfst (cons x) (split_last xs); 

555 

4893  556 
(*update nth element*) 
13629  557 
fun nth_update x n_xs = 
558 
(case splitAt n_xs of 

15570  559 
(_,[]) => raise Subscript 
13629  560 
 (prfx, _ :: sffx') => prfx @ (x :: sffx')) 
4893  561 

4212  562 
(*find the position of an element in a list*) 
563 
fun find_index pred = 

564 
let fun find _ [] = ~1 

565 
 find n (x :: xs) = if pred x then n else find (n + 1) xs; 

566 
in find 0 end; 

3762  567 

4224  568 
fun find_index_eq x = find_index (equal x); 
4212  569 

570 
(*find first element satisfying predicate*) 

15531  571 
fun find_first _ [] = NONE 
4212  572 
 find_first pred (x :: xs) = 
15531  573 
if pred x then SOME x else find_first pred xs; 
233  574 

4916
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

575 
(*get first element by lookup function*) 
15531  576 
fun get_first _ [] = NONE 
4916
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

577 
 get_first f (x :: xs) = 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

578 
(case f x of 
15531  579 
NONE => get_first f xs 
4916
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

580 
 some => some); 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

581 

233  582 
(*flatten a list of lists to a list*) 
15531  583 
val flat = List.concat; 
233  584 

12136  585 
fun unflat (xs :: xss) ys = 
13629  586 
let val (ps,qs) = splitAt(length xs,ys) 
587 
in ps :: unflat xss qs end 

12136  588 
 unflat [] [] = [] 
15570  589 
 unflat _ _ = raise UnequalLengths; 
12136  590 

233  591 
(*like Lisp's MAPC  seq proc [x1, ..., xn] evaluates 
592 
(proc x1; ...; proc xn) for side effects*) 

15570  593 
val seq = List.app; 
233  594 

595 
(*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*) 

596 
fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs 

597 
 separate _ xs = xs; 

598 

599 
(*make the list [x, x, ..., x] of length n*) 

600 
fun replicate n (x: 'a) : 'a list = 

601 
let fun rep (0, xs) = xs 

602 
 rep (n, xs) = rep (n  1, x :: xs) 

603 
in 

15570  604 
if n < 0 then raise Subscript 
233  605 
else rep (n, []) 
606 
end; 

607 

14926  608 
fun translate_string f = String.translate (f o String.str); 
609 

4248
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

610 
(*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

611 
fun multiply ([], _) = [] 
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

612 
 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

613 

14792  614 
(*direct product*) 
615 
fun product _ [] = [] 

616 
 product [] _ = [] 

617 
 product (x :: xs) ys = map (pair x) ys @ product xs ys; 

618 

233  619 

620 
(* filter *) 

621 

622 
(*copy the list preserving elements that satisfy the predicate*) 

15531  623 
val filter = List.filter; 
0  624 
fun filter_out f = filter (not o f); 
15570  625 
val mapfilter = List.mapPartial; 
233  626 

627 

628 
(* lists of pairs *) 

629 

15570  630 
exception UnequalLengths; 
631 

380  632 
fun map2 _ ([], []) = [] 
16878  633 
 map2 f (x :: xs, y :: ys) = f (x, y) :: map2 f (xs, ys) 
15570  634 
 map2 _ _ = raise UnequalLengths; 
380  635 

636 
fun exists2 _ ([], []) = false 

637 
 exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys) 

15570  638 
 exists2 _ _ = raise UnequalLengths; 
380  639 

640 
fun forall2 _ ([], []) = true 

641 
 forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys) 

15570  642 
 forall2 _ _ = raise UnequalLengths; 
380  643 

4956
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

644 
fun seq2 _ ([], []) = () 
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

645 
 seq2 f (x :: xs, y :: ys) = (f (x, y); seq2 f (xs, ys)) 
15570  646 
 seq2 _ _ = raise UnequalLengths; 
4956
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

647 

233  648 
(*combine two lists forming a list of pairs: 
649 
[x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*) 

650 
fun [] ~~ [] = [] 

651 
 (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys) 

15570  652 
 _ ~~ _ = raise UnequalLengths; 
233  653 

654 
(*inverse of ~~; the old 'split': 

655 
[(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*) 

15570  656 
val split_list = ListPair.unzip; 
233  657 

7468
6ce03d2f7d91
equal_lists: ('a * 'b > bool) > 'a list * 'b list > bool;
wenzelm
parents:
7090
diff
changeset

658 
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

659 

233  660 

661 
(* prefixes, suffixes *) 

662 

663 
fun [] prefix _ = true 

664 
 (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys) 

665 
 _ prefix _ = false; 

666 

667 
(* [x1, ..., xi, ..., xn] > ([x1, ..., x(i1)], [xi, ..., xn]) 

668 
where xi is the first element that does not satisfy the predicate*) 

669 
fun take_prefix (pred : 'a > bool) (xs: 'a list) : 'a list * 'a list = 

670 
let fun take (rxs, []) = (rev rxs, []) 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

671 
 take (rxs, x :: xs) = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

672 
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) 
233  673 
in take([], xs) end; 
674 

675 
(* [x1, ..., xi, ..., xn] > ([x1, ..., xi], [x(i+1), ..., xn]) 

676 
where xi is the last element that does not satisfy the predicate*) 

677 
fun take_suffix _ [] = ([], []) 

678 
 take_suffix pred (x :: xs) = 

679 
(case take_suffix pred xs of 

680 
([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) 

681 
 (prfx, sffx) => (x :: prfx, sffx)); 

682 

12249  683 
fun prefixes1 [] = [] 
684 
 prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs); 

685 

686 
fun suffixes1 xs = map rev (prefixes1 (rev xs)); 

687 

233  688 

689 

690 
(** integers **) 

691 

16439  692 
fun gcd (x, y) = 
15965
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15760
diff
changeset

693 
let fun gxd x y : IntInf.int = 
10692  694 
if y = 0 then x else gxd y (x mod y) 
695 
in if x < y then gxd y x else gxd x y end; 

696 

16439  697 
fun lcm (x, y) = (x * y) div gcd (x, y); 
10692  698 

2958  699 
fun inc i = (i := ! i + 1; ! i); 
700 
fun dec i = (i := ! i  1; ! i); 

233  701 

702 

703 
(* lists of integers *) 

704 

705 
(*make the list [from, from + 1, ..., to]*) 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

706 
fun (from upto to) = 
233  707 
if from > to then [] else from :: ((from + 1) upto to); 
708 

709 
(*make the list [from, from  1, ..., to]*) 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

710 
fun (from downto to) = 
233  711 
if from < to then [] else from :: ((from  1) downto to); 
712 

713 
(*predicate: downto0 (is, n) <=> is = [n, n  1, ..., 0]*) 

714 
fun downto0 (i :: is, n) = i = n andalso downto0 (is, n  1) 

715 
 downto0 ([], n) = n = ~1; 

716 

717 

718 
(* convert integers to strings *) 

719 

720 
(*expand the number in the given base; 

721 
example: radixpand (2, 8) gives [1, 0, 0, 0]*) 

722 
fun radixpand (base, num) : int list = 

723 
let 

724 
fun radix (n, tail) = 

725 
if n < base then n :: tail 

726 
else radix (n div base, (n mod base) :: tail) 

727 
in radix (num, []) end; 

728 

729 
(*expands a number into a string of characters starting from "zerochar"; 

730 
example: radixstring (2, "0", 8) gives "1000"*) 

731 
fun radixstring (base, zerochar, num) = 

732 
let val offset = ord zerochar; 

733 
fun chrof n = chr (offset + n) 

734 
in implode (map chrof (radixpand (base, num))) end; 

735 

736 

3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset

737 
val string_of_int = Int.toString; 
233  738 

3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset

739 
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

740 
 string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; 
233  741 

742 

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

743 
(* 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

744 

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

745 
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

746 
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

747 
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

748 
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

749 
 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

750 
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

751 
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

752 
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

753 
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

754 

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

755 
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

756 

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

757 
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

758 

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

759 

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

760 

233  761 
(** strings **) 
762 

16188  763 
(* functions tuned for strings, avoiding explode *) 
6312  764 

765 
fun nth_elem_string (i, str) = 

6959  766 
(case try String.substring (str, i, 1) of 
15531  767 
SOME s => s 
15570  768 
 NONE => raise Subscript); 
6312  769 

16188  770 
fun fold_string f str x0 = 
6282  771 
let 
772 
val n = size str; 

16188  773 
fun iter (x, i) = 
774 
if i < n then iter (f (String.substring (str, i, 1)) x, i + 1) else x; 

775 
in iter (x0, 0) end; 

6282  776 

14968  777 
fun exists_string pred str = 
778 
let 

779 
val n = size str; 

780 
fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1)); 

781 
in ex 0 end; 

6312  782 

16188  783 
fun forall_string pred = not o exists_string (not o pred); 
784 

512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

785 
(*enclose in brackets*) 
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

786 
fun enclose lpar rpar str = lpar ^ str ^ rpar; 
6642  787 
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

788 

233  789 
(*simple quoting (does not escape special chars)*) 
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

790 
val quote = enclose "\"" "\""; 
233  791 

4212  792 
(*space_implode "..." (explode "hello") = "h...e...l...l...o"*) 
233  793 
fun space_implode a bs = implode (separate a bs); 
794 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

795 
val commas = space_implode ", "; 
380  796 
val commas_quote = commas o map quote; 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

797 

233  798 
(*concatenate messages, one per line, into a string*) 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

799 
val cat_lines = space_implode "\n"; 
233  800 

4212  801 
(*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

802 
fun space_explode _ "" = [] 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

803 
 space_explode sep str = 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

804 
let 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

805 
fun expl chs = 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

806 
(case take_prefix (not_equal sep) chs of 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

807 
(cs, []) => [implode cs] 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

808 
 (cs, _ :: cs') => implode cs :: expl cs'); 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

809 
in expl (explode str) end; 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

810 

17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

811 
val split_lines = space_explode "\n"; 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

812 

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

813 
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

814 
 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

815 

7712  816 
fun untabify chs = 
817 
let 

818 
val tab_width = 8; 

819 

820 
fun untab (_, "\n") = (0, ["\n"]) 

9118  821 
 untab (pos, "\t") = 
822 
let val d = tab_width  (pos mod tab_width) in (pos + d, replicate d " ") end 

7712  823 
 untab (pos, c) = (pos + 1, [c]); 
824 
in 

825 
if not (exists (equal "\t") chs) then chs 

826 
else flat (#2 (foldl_map untab (0, chs))) 

827 
end; 

828 

16188  829 
fun suffix sffx s = s ^ sffx; 
5285  830 

16188  831 
fun unsuffix sffx s = 
17061  832 
if String.isSuffix sffx s then String.substring (s, 0, size s  size sffx) 
833 
else raise Fail "unsuffix"; 

5285  834 

15060  835 
fun unprefix prfx s = 
17061  836 
if String.isPrefix prfx s then String.substring (s, size prfx, size s  size prfx) 
837 
else raise Fail "unprefix"; 

15060  838 

10951  839 
fun replicate_string 0 _ = "" 
840 
 replicate_string 1 a = a 

841 
 replicate_string k a = 

842 
if k mod 2 = 0 then replicate_string (k div 2) (a ^ a) 

843 
else replicate_string (k div 2) (a ^ a) ^ a; 

844 

3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

845 

233  846 

16492  847 
(** lists as sets  see also Pure/General/ord_list.ML **) 
233  848 

849 
(*membership in a list*) 

850 
fun x mem [] = false 

851 
 x mem (y :: ys) = x = y orelse x mem ys; 

0  852 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

853 
(*membership in a list, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

854 
fun (x:int) mem_int [] = false 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

855 
 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

856 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

857 
(*membership in a list, optimized version for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

858 
fun (x:string) mem_string [] = false 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

859 
 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

860 

0  861 
(*generalized membership test*) 
233  862 
fun gen_mem eq (x, []) = false 
863 
 gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); 

864 

16492  865 
(*member, insert, and remove  with canonical argument order*) 
866 
fun member eq xs x = gen_mem eq (x, xs); 

15760  867 
fun insert eq x xs = if gen_mem eq (x, xs) then xs else x :: xs; 
868 
fun remove eq x xs = if gen_mem eq (x, xs) then filter_out (fn y => eq (x, y)) xs else xs; 

233  869 

870 
(*insertion into list if not already there*) 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

871 
fun (x ins xs) = if x mem xs then xs else x :: xs; 
0  872 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

873 
(*insertion into list, optimized version for ints*) 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

874 
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

875 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

876 
(*insertion into list, optimized version for strings*) 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

877 
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

878 

0  879 
(*generalized insertion*) 
15760  880 
fun gen_ins eq (x, xs) = insert eq x xs; 
233  881 

882 
(*union of sets represented as lists: no repetitions*) 

883 
fun xs union [] = xs 

884 
 [] union ys = ys 

885 
 (x :: xs) union ys = xs union (x ins ys); 

0  886 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

887 
(*union of sets, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

888 
fun (xs:int list) union_int [] = xs 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

889 
 [] union_int ys = ys 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

890 
 (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

891 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

892 
(*union of sets, optimized version for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

893 
fun (xs:string list) union_string [] = xs 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

894 
 [] union_string ys = ys 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

895 
 (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

896 

0  897 
(*generalized union*) 
233  898 
fun gen_union eq (xs, []) = xs 
899 
 gen_union eq ([], ys) = ys 

900 
 gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys)); 

901 

902 

903 
(*intersection*) 

904 
fun [] inter ys = [] 

905 
 (x :: xs) inter ys = 

906 
if x mem ys then x :: (xs inter ys) else xs inter ys; 

907 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

908 
(*intersection, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

909 
fun ([]:int list) inter_int ys = [] 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

910 
 (x :: xs) inter_int ys = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

911 
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

912 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

913 
(*intersection, optimized version for strings *) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

914 
fun ([]:string list) inter_string ys = [] 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

915 
 (x :: xs) inter_string ys = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

916 
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

917 

7090  918 
(*generalized intersection*) 
919 
fun gen_inter eq ([], ys) = [] 

12284  920 
 gen_inter eq (x::xs, ys) = 
7090  921 
if gen_mem eq (x,ys) then x :: gen_inter eq (xs, ys) 
12284  922 
else gen_inter eq (xs, ys); 
7090  923 

233  924 

925 
(*subset*) 

926 
fun [] subset ys = true 

927 
 (x :: xs) subset ys = x mem ys andalso xs subset ys; 

928 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

929 
(*subset, optimized version for ints*) 
16439  930 
fun ([]: int list) subset_int ys = true 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

931 
 (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

932 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

933 
(*subset, optimized version for strings*) 
16439  934 
fun ([]: string list) subset_string ys = true 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

935 
 (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

936 

4363  937 
(*set equality*) 
938 
fun eq_set (xs, ys) = 

939 
xs = ys orelse (xs subset ys andalso ys subset xs); 

940 

2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset

941 
(*set equality for strings*) 
16439  942 
fun eq_set_string ((xs: string list), ys) = 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

943 
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

944 

2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset

945 
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

946 

265  947 

233  948 
(*removing an element from a list WITHOUT duplicates*) 
949 
fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x) 

950 
 [] \ x = []; 

951 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

952 
fun ys \\ xs = foldl (op \) (ys,xs); 
0  953 

233  954 
(*removing an element from a list  possibly WITH duplicates*) 
955 
fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs; 

12295  956 
fun gen_rems eq (xs, ys) = filter_out (fn x => gen_mem eq (x, ys)) xs; 
233  957 

958 
(*makes a list of the distinct members of the input; preserves order, takes 

959 
first of equal elements*) 

960 
fun gen_distinct eq lst = 

961 
let 

962 
val memb = gen_mem eq; 

0  963 

233  964 
fun dist (rev_seen, []) = rev rev_seen 
965 
 dist (rev_seen, x :: xs) = 

966 
if memb (x, rev_seen) then dist (rev_seen, xs) 

967 
else dist (x :: rev_seen, xs); 

968 
in 

969 
dist ([], lst) 

970 
end; 

971 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

972 
fun distinct l = gen_distinct (op =) l; 
233  973 

974 
(*returns the tail beginning with the first repeated element, or []*) 

975 
fun findrep [] = [] 

976 
 findrep (x :: xs) = if x mem xs then x :: xs else findrep xs; 

977 

978 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

979 
(*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

980 
order, takes first of equal elements*) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

981 
fun gen_duplicates eq lst = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

982 
let 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

983 
val memb = gen_mem eq; 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

984 

ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

985 
fun dups (rev_dups, []) = rev rev_dups 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

986 
 dups (rev_dups, x :: xs) = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

987 
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

988 
dups (rev_dups, xs) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

989 
else dups (x :: rev_dups, xs); 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

990 
in 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

991 
dups ([], lst) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

992 
end; 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

993 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

994 
fun duplicates l = gen_duplicates (op =) l; 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

995 

16878  996 
fun has_duplicates eq = 
997 
let 

998 
fun dups [] = false 

999 
 dups (x :: xs) = member eq xs x orelse dups xs; 

1000 
in dups end; 

1001 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

1002 

233  1003 

1004 
(** association lists **) 

0  1005 

12284  1006 
(* lists as tables *) 
233  1007 

12284  1008 
fun gen_merge_lists _ xs [] = xs 
1009 
 gen_merge_lists _ [] ys = ys 

1010 
 gen_merge_lists eq xs ys = xs @ gen_rems eq (ys, xs); 

0  1011 

12284  1012 
fun gen_merge_lists' _ xs [] = xs 
1013 
 gen_merge_lists' _ [] ys = ys 

12295  1014 
 gen_merge_lists' eq xs ys = gen_rems eq (ys, xs) @ xs; 
0  1015 

12284  1016 
fun merge_lists xs ys = gen_merge_lists (op =) xs ys; 
1017 
fun merge_lists' xs ys = gen_merge_lists' (op =) xs ys; 

17498
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset

1018 
fun merge_alists al = gen_merge_lists (eq_fst (op =)) al; 
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset

1019 
fun merge_alists' al = gen_merge_lists' (eq_fst (op =)) al; 
380  1020 

0  1021 

1022 

233  1023 
(** balanced trees **) 
1024 

1025 
exception Balance; (*indicates nonpositive argument to balancing fun*) 

1026 

1027 
(*balanced folding; avoids deep nesting*) 

1028 
fun fold_bal f [x] = x 

1029 
 fold_bal f [] = raise Balance 

1030 
 fold_bal f xs = 

13629  1031 
let val (ps,qs) = splitAt(length xs div 2, xs) 
1032 
in f (fold_bal f ps, fold_bal f qs) end; 

233  1033 

1034 
(*construct something of the form f(...g(...(x)...)) for balanced access*) 

1035 
fun access_bal (f, g, x) n i = 

1036 
let fun acc n i = (*1<=i<=n*) 

1037 
if n=1 then x else 

1038 
let val n2 = n div 2 

1039 
in if i<=n2 then f (acc n2 i) 

1040 
else g (acc (nn2) (in2)) 

1041 
end 

1042 
in if 1<=i andalso i<=n then acc n i else raise Balance end; 

1043 

1044 
(*construct ALL such accesses; could try harder to share recursive calls!*) 

1045 
fun accesses_bal (f, g, x) n = 

1046 
let fun acc n = 

1047 
if n=1 then [x] else 

1048 
let val n2 = n div 2 

1049 
val acc2 = acc n2 

1050 
in if nn2=n2 then map f acc2 @ map g acc2 

1051 
else map f acc2 @ map g (acc (nn2)) end 

1052 
in if 1<=n then acc n else raise Balance end; 

1053 

1054 

1055 

2506  1056 
(** orders **) 
1057 

4445  1058 
fun rev_order LESS = GREATER 
1059 
 rev_order EQUAL = EQUAL 

1060 
 rev_order GREATER = LESS; 

1061 

4479  1062 
(*assume rel is a linear strict order*) 
4445  1063 
fun make_ord rel (x, y) = 
1064 
if rel (x, y) then LESS 

1065 
else if rel (y, x) then GREATER 

1066 
else EQUAL; 

1067 

15051
0dbbab9f77fe
int_ord = Int.compare, string_ord = String.compare;
wenzelm
parents:
15035
diff
changeset

1068 
val int_ord = Int.compare; 
0dbbab9f77fe
int_ord = Int.compare, string_ord = String.compare;
wenzelm
parents:
15035
diff
changeset

1069 
val string_ord = String.compare; 
2506  1070 

16676  1071 
fun fast_string_ord (s1, s2) = 
1072 
(case int_ord (size s1, size s2) of EQUAL => string_ord (s1, s2)  ord => ord); 

1073 

16492  1074 
fun option_ord ord (SOME x, SOME y) = ord (x, y) 
1075 
 option_ord _ (NONE, NONE) = EQUAL 

1076 
 option_ord _ (NONE, SOME _) = LESS 

1077 
 option_ord _ (SOME _, NONE) = GREATER; 

1078 

4343  1079 
(*lexicographic product*) 
1080 
fun prod_ord a_ord b_ord ((x, y), (x', y')) = 

1081 
(case a_ord (x, x') of EQUAL => b_ord (y, y')  ord => ord); 

1082 

1083 
(*dictionary order  in general NOT wellfounded!*) 

16984  1084 
fun dict_ord elem_ord (x :: xs, y :: ys) = 
1085 
(case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys)  ord => ord) 

1086 
 dict_ord _ ([], []) = EQUAL 

4343  1087 
 dict_ord _ ([], _ :: _) = LESS 
16984  1088 
 dict_ord _ (_ :: _, []) = GREATER; 
4343  1089 

1090 
(*lexicographic product of lists*) 

1091 
fun list_ord elem_ord (xs, ys) = 

16676  1092 
(case int_ord (length xs, length ys) of EQUAL => dict_ord elem_ord (xs, ys)  ord => ord); 
4343  1093 

2506  1094 

4621  1095 
(* sorting *) 
1096 

1097 
(*quicksort (stable, i.e. does not reorder equal elements)*) 

1098 
fun sort ord = 

1099 
let 

16878  1100 
fun qsort [] = [] 
1101 
 qsort (xs as [_]) = xs 

1102 
 qsort (xs as [x, y]) = if ord (x, y) = GREATER then [y, x] else xs 

1103 
 qsort xs = 

1104 
let val (lts, eqs, gts) = part (nth_elem (length xs div 2, xs)) xs 

1105 
in qsort lts @ eqs @ qsort gts end 

4621  1106 
and part _ [] = ([], [], []) 
1107 
 part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs) 

1108 
and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts) 

1109 
 add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts) 

1110 
 add GREATER x (lts, eqs, gts) = (lts, eqs, x :: gts); 

1111 
in qsort end; 

1112 

1113 
(*sort strings*) 

1114 
val sort_strings = sort string_ord; 

1115 
fun sort_wrt sel xs = sort (string_ord o pairself sel) xs; 

1116 

11514  1117 
fun unique_strings ([]: string list) = [] 
1118 
 unique_strings [x] = [x] 

1119 
 unique_strings (x :: y :: ys) = 

1120 
if x = y then unique_strings (y :: ys) 

1121 
else x :: unique_strings (y :: ys); 

4621  1122 

2506  1123 

14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1124 
(** random numbers **) 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1125 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1126 
exception RANDOM; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1127 

14618
068bb99f3ebd
Fixed bug in rmod that caused an overflow exception in SML/NJ.
berghofe
parents:
14472
diff
changeset

1128 
fun rmod x y = x  y * Real.realFloor (x / y); 
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1129 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1130 
local 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1131 
val a = 16807.0; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1132 
val m = 2147483647.0; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1133 
val random_seed = ref 1.0; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1134 
in 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1135 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1136 
fun random () = 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1137 
let val r = rmod (a * !random_seed) m 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1138 
in (random_seed := r; r) end; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1139 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1140 
end; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1141 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1142 
fun random_range l h = 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1143 
if h < l orelse l < 0 then raise RANDOM 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1144 
else l + Real.floor (rmod (random ()) (real (h  l + 1))); 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1145 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1146 
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

1147 

bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1148 
fun frequency xs = 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1149 
let 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1150 
val sum = foldl op + (0, map fst xs); 
17756  1151 
fun pick n ((k: int, x) :: xs) = 
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1152 
if n <= k then x else pick (n  k) xs 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1153 
in pick (random_range 1 sum) xs 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 

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

1156 
(** current directory **) 
233  1157 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

1158 
val cd = OS.FileSys.chDir; 
2317  1159 
val pwd = OS.FileSys.getDir; 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

1160 

3606  1161 

4621  1162 
(** misc **) 
233  1163 

0  1164 
(*Partition list into elements that satisfy predicate and those that don't. 
233  1165 
Preserves order of elements in both lists.*) 
15570  1166 
val partition = List.partition; 
0  1167 

1168 
fun partition_eq (eq:'a * 'a > bool) = 

16842  1169 
let 
1170 
fun part [] = [] 

1171 
 part (x :: ys) = 

1172 
let val (xs, xs') = partition (fn y => eq (x, y)) ys 

1173 
in (x::xs)::(part xs') end 

1174 
in part end; 

0  1175 

1176 

233  1177 
(*Partition a list into buckets [ bi, b(i+1), ..., bj ] 
0  1178 
putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) 
1179 
fun partition_list p i j = 

233  1180 
let fun part k xs = 
1181 
if k>j then 

0  1182 
(case xs of [] => [] 
15570  1183 
 _ => raise Fail "partition_list") 
0  1184 
else 
233  1185 
let val (ns, rest) = partition (p k) xs; 
1186 
in ns :: part(k+1)rest end 

0  1187 
in part i end; 
1188 

1189 

233  1190 
(* generating identifiers *) 
0  1191 

4063  1192 
(** Freshly generated identifiers; supplied prefix MUST start with a letter **) 
0  1193 
local 
4063  1194 
(*Maps 063 to AZ, az, 09 or _ or ' for generating random identifiers*) 
1195 
fun char i = if i<26 then chr (ord "A" + i) 

5904  1196 
else if i<52 then chr (ord "a" + i  26) 
1197 
else if i<62 then chr (ord"0" + i  52) 

1198 
else if i=62 then "_" 

1199 
else (*i=63*) "'"; 

4063  1200 

1201 
val charVec = Vector.tabulate (64, char); 

1202 

5904  1203 
fun newid n = 
1204 
let 

4284  1205 
in implode (map (fn i => Vector.sub(charVec,i)) (radixpand (64,n))) end; 
2003  1206 

4284  1207 
val seedr = ref 0; 
0  1208 

4063  1209 
in 
4284  1210 

12346  1211 
fun gensym pre = pre ^ (#1(newid (!seedr), inc seedr)); 
2003  1212 

4063  1213 
end; 
1214 

1215 

233  1216 
(* lexical scanning *) 
0  1217 

233  1218 
(*scan a list of characters into "words" composed of "letters" (recognized by 
1219 
is_let) and separated by any number of non"letters"*) 

1220 
fun scanwords is_let cs = 

0  1221 
let fun scan1 [] = [] 
233  1222 
 scan1 cs = 
1223 
let val (lets, rest) = take_prefix is_let cs 

1224 
in implode lets :: scanwords is_let rest end; 

1225 
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

1226 

4212  1227 

16439  1228 
(* stamps and serial numbers *) 
1229 

1230 
type stamp = unit ref; 

1231 
val stamp: unit > stamp = ref; 

1232 

1233 
type serial = int; 

1234 
local val count = ref 0 

1235 
in fun serial () = inc count end; 

1236 

16535
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1237 

86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1238 
(* generic objects *) 
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1239 

86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1240 
(*note that the builtin exception datatype may be extended by new 
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1241 
constructors at any time*) 
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1242 
structure Object = struct type T = exn end; 
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset

1243 

1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

1244 
end; 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

1245 

15745  1246 
structure BasicLibrary: BASIC_LIBRARY = Library; 
1247 
open BasicLibrary; 