author  haftmann 
Thu, 08 Sep 2005 16:08:50 +0200  
changeset 17313  7d97f60293ae 
parent 17257  0ab67cb765da 
child 17341  ca0e5105c4b1 
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, 
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 

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 
4621  41 
val apl: 'a * ('a * 'b > 'c) > 'b > 'c 
42 
val apr: ('a * 'b > 'c) * 'b > 'a > 'c 

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

43 

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

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

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

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

47 

15970  48 
(*options*) 
49 
val the: 'a option > 'a 

17153  50 
val these: 'a list option > 'a list 
17313  51 
val the_default: 'a > 'a option > 'a 
52 
val the_list: 'a option > 'a list 

15970  53 
val if_none: 'a option > 'a > 'a 
54 
val is_some: 'a option > bool 

55 
val is_none: 'a option > bool 

56 

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

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

62 
val release: 'a result > 'a 

63 
val get_result: 'a result > 'a option 

64 
val get_exn: 'a result > exn option 

4621  65 

66 
(*pairs*) 

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

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

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

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

71 
val eq_fst: (''a * 'b) * (''a * 'c) > bool 

72 
val eq_snd: ('a * ''b) * ('c * ''b) > bool 

73 
val swap: 'a * 'b > 'b * 'a 

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

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

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

77 

78 
(*booleans*) 

79 
val equal: ''a > ''a > bool 

80 
val not_equal: ''a > ''a > bool 

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

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

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

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

85 
val set: bool ref > bool 

86 
val reset: bool ref > bool 

87 
val toggle: bool ref > bool 

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

92 
(*lists*) 

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

99 
val fold_rev: ('a > 'b > 'b) > 'a list > 'b > 'b 
16869  100 
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

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

112 
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

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

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

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

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

122 
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

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

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

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

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

4621  132 

133 
(*integers*) 

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

134 
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

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

138 
val upto: int * int > int list 

139 
val downto: int * int > int list 

140 
val downto0: int list * int > bool 

141 
val radixpand: int * int > int list 

142 
val radixstring: int * string * int > string 

143 
val string_of_int: int > string 

144 
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

145 
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

146 
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

147 
val oct_char: string > string 
4621  148 

10692  149 
(*rational numbers*) 
150 
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

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

152 
val rep_rat: rat > IntInf.int * IntInf.int 
17028  153 
val ratge0: rat > bool 
154 
val ratgt0: rat > bool 

155 
val ratle: rat * rat > bool 

156 
val ratlt: rat * rat > bool 

10692  157 
val ratadd: rat * rat > rat 
158 
val ratmul: rat * rat > rat 

159 
val ratinv: rat > rat 

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

160 
val int_ratdiv: IntInf.int * IntInf.int > rat 
10692  161 
val ratneg: rat > rat 
162 
val rat_of_int: int > rat 

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

163 
val rat_of_intinf: IntInf.int > rat 
17028  164 
val rat0: rat 
10692  165 

4621  166 
(*strings*) 
6312  167 
val nth_elem_string: int * string > string 
16188  168 
val fold_string: (string > 'a > 'a) > string > 'a > 'a 
6312  169 
val exists_string: (string > bool) > string > bool 
16188  170 
val forall_string: (string > bool) > string > bool 
4621  171 
val enclose: string > string > string > string 
6642  172 
val unenclose: string > string 
4621  173 
val quote: string > string 
174 
val space_implode: string > string list > string 

175 
val commas: string list > string 

176 
val commas_quote: string list > string 

177 
val cat_lines: string list > string 

178 
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

179 
val split_lines: string > string list 
5942  180 
val prefix_lines: string > string > string 
7712  181 
val untabify: string list > string list 
5285  182 
val suffix: string > string > string 
183 
val unsuffix: string > string > string 

15060  184 
val unprefix: string > string > string 
10951  185 
val replicate_string: int > string > string 
14926  186 
val translate_string: (string > string) > string > string 
4621  187 

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

191 
val mem_string: string * string list > bool 

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

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

194 
val ins_int: int * int list > int list 

195 
val ins_string: string * string list > string list 

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

16492  197 
val member: ('b * 'a > bool) > 'a list > 'b > bool 
15760  198 
val insert: ('a * 'a > bool) > 'a > 'a list > 'a list 
16129  199 
val remove: ('b * 'a > bool) > 'b > 'a list > 'a list 
4621  200 
val union: ''a list * ''a list > ''a list 
201 
val union_int: int list * int list > int list 

202 
val union_string: string list * string list > string list 

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

7090  204 
val gen_inter: ('a * 'b > bool) > 'a list * 'b list > 'a list 
4621  205 
val inter: ''a list * ''a list > ''a list 
206 
val inter_int: int list * int list > int list 

207 
val inter_string: string list * string list > string list 

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

209 
val subset_int: int list * int list > bool 

210 
val subset_string: string list * string list > bool 

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

212 
val eq_set_string: string list * string list > bool 

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

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

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

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

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

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

219 
val distinct: ''a list > ''a list 

220 
val findrep: ''a list > ''a list 

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

222 
val duplicates: ''a list > ''a list 

16878  223 
val has_duplicates: ('a * 'a > bool) > 'a list > bool 
4621  224 

17153  225 
(*association lists  see also Pure/General/alist.ML*) 
4621  226 
val assoc: (''a * 'b) list * ''a > 'b option 
227 
val assoc_int: (int * 'a) list * int > 'a option 

228 
val assoc_string: (string * 'a) list * string > 'a option 

229 
val assoc_string_int: ((string * int) * 'a) list * (string * int) > 'a option 

230 
val assocs: (''a * 'b list) list > ''a > 'b list 

231 
val gen_assoc: ('a * 'b > bool) > ('b * 'c) list * 'a > 'c option 

232 
val overwrite: (''a * 'b) list * (''a * 'b) > (''a * 'b) list 

233 
val gen_overwrite: ('a * 'a > bool) > ('a * 'b) list * ('a * 'b) > ('a * 'b) list 

234 

12284  235 
(*lists as tables*) 
236 
val gen_merge_lists: ('a * 'a > bool) > 'a list > 'a list > 'a list 

237 
val gen_merge_lists': ('a * 'a > bool) > 'a list > 'a list > 'a list 

4621  238 
val merge_lists: ''a list > ''a list > ''a list 
12284  239 
val merge_lists': ''a list > ''a list > ''a list 
4692  240 
val merge_alists: (''a * 'b) list > (''a * 'b) list > (''a * 'b) list 
15263  241 
val merge_alists': (''a * 'b) list > (''a * 'b) list > (''a * 'b) list 
4621  242 

243 
(*balanced trees*) 

244 
exception Balance 

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

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

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

248 

249 
(*orders*) 

250 
val rev_order: order > order 

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

252 
val int_ord: int * int > order 

253 
val string_ord: string * string > order 

16676  254 
val fast_string_ord: string * string > order 
16492  255 
val option_ord: ('a * 'b > order) > 'a option * 'b option > order 
4621  256 
val prod_ord: ('a * 'b > order) > ('c * 'd > order) > ('a * 'c) * ('b * 'd) > order 
257 
val dict_ord: ('a * 'b > order) > 'a list * 'b list > order 

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

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

260 
val sort_strings: string list > string list 

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

11514  262 
val unique_strings: string list > string list 
4621  263 

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

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

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

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

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

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

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

270 

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

271 
(*current directory*) 
4621  272 
val cd: string > unit 
273 
val pwd: unit > string 

274 

275 
(*misc*) 

276 
val make_keylist: ('a > 'b) > 'a list > ('a * 'b) list 

277 
val keyfilter: ('a * ''b) list > ''b > 'a list 

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

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

280 
val gensym: string > string 

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

16439  282 
type stamp 
283 
val stamp: unit > stamp 

284 
type serial 

285 
val serial: unit > serial 

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

286 
structure Object: sig type T end 
4621  287 
end; 
288 

15745  289 
signature LIBRARY = 
15570  290 
sig 
15745  291 
include BASIC_LIBRARY 
15570  292 
val foldl: ('a * 'b > 'a) > 'a * 'b list > 'a 
293 
val foldr: ('a * 'b > 'b) > 'a list * 'b > 'b 

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

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

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

297 
val last_elem: 'a list > 'a 

298 
val flat: 'a list list > 'a list 

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

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

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

302 
end; 

303 

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

305 
struct 
0  306 

4995  307 

233  308 
(** functions **) 
0  309 

16842  310 
fun I x = x; 
311 
fun K x = fn _ => x; 

233  312 
fun curry f x y = f (x, y); 
313 
fun uncurry f (x, y) = f x y; 

0  314 

16705  315 
(*reverse application and structured results*) 
316 
fun x > f = f x; 

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

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

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

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

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

16842  322 

323 
(*reverse composition*) 

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

324 
fun f #> g = g o f; 
16842  325 
fun f #> g = uncurry g o f; 
326 

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

327 
(*conditional application*) 
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset

328 
fun b ? f = fn x => if b x then f x else x 
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset

329 

16842  330 
(*view results*) 
331 
fun `f = fn x => (f x, x); 

17101  332 
fun tap f x = (f x; x); 
380  333 

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

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

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

338 

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

341 
let fun rep (0, x) = x 

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

343 
in rep (n, x) end; 

160  344 

16842  345 
(*application of (infix) operator to its left or right argument*) 
346 
fun apl (x, f) y = f (x, y); 

347 
fun apr (f, y) x = f (x, y); 

160  348 

349 

233  350 
(** options **) 
0  351 

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

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

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

354 
exception OPTION of invalid; 
0  355 

15970  356 
val the = Option.valOf; 
17153  357 
fun these (SOME x) = x 
17313  358 
 these _ = []; 
359 
fun the_default x (SOME y) = y 

360 
 the_default x _ = x; 

361 
fun the_list (SOME x) = [x] 

362 
 the_list _ = [] 

15970  363 

364 
(*strict!*) 

365 
fun if_none NONE y = y 

366 
 if_none (SOME x) _ = x; 

367 

368 
fun is_some (SOME _) = true 

369 
 is_some NONE = false; 

370 

371 
fun is_none (SOME _) = false 

372 
 is_none NONE = true; 

373 

6959  374 

375 
(* exception handling *) 

376 

377 
exception ERROR; 

378 

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

6959  381 

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

4139  383 

384 

14868  385 
datatype 'a result = 
386 
Result of 'a  

387 
Exn of exn; 

388 

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

390 

391 
fun release (Result y) = y 

392 
 release (Exn e) = raise e; 

393 

15531  394 
fun get_result (Result x) = SOME x 
395 
 get_result _ = NONE; 

14868  396 

15531  397 
fun get_exn (Exn exn) = SOME exn 
398 
 get_exn _ = NONE; 

14868  399 

400 

4139  401 

233  402 
(** pairs **) 
403 

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

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

406 

407 
fun fst (x, y) = x; 

408 
fun snd (x, y) = y; 

409 

410 
fun eq_fst ((x1, _), (x2, _)) = x1 = x2; 

411 
fun eq_snd ((_, y1), (_, y2)) = y1 = y2; 

412 

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

414 

4212  415 
(*apply function to components*) 
233  416 
fun apfst f (x, y) = (f x, y); 
417 
fun apsnd f (x, y) = (x, f y); 

4212  418 
fun pairself f (x, y) = (f x, f y); 
233  419 

420 

421 

422 
(** booleans **) 

423 

424 
(* equality *) 

425 

426 
fun equal x y = x = y; 

427 
fun not_equal x y = x <> y; 

428 

429 

430 
(* operators for combining predicates *) 

431 

16721  432 
fun p orf q = fn x => p x orelse q x; 
433 
fun p andf q = fn x => p x andalso q x; 

233  434 

435 

436 
(* predicates on lists *) 

437 

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

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

440 
let fun boolf [] = false 

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

442 
in boolf end; 

443 

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

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

446 
let fun boolf [] = true 

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

448 
in boolf end; 

0  449 

233  450 

380  451 
(* flags *) 
452 

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

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

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

456 

9118  457 
fun change r f = r := f (! r); 
458 

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

459 
(*temporarily set flag, handling exceptions*) 
2978  460 
fun setmp flag value f x = 
2958  461 
let 
462 
val orig_value = ! flag; 

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

464 
in 

465 
flag := value; 

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

467 
end; 

468 

380  469 

11853  470 
(* conditional execution *) 
471 

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

473 

474 

233  475 

476 
(** lists **) 

477 

15570  478 
exception UnequalLengths; 
233  479 

480 
fun cons x xs = x :: xs; 

5285  481 
fun single x = [x]; 
233  482 

4629  483 
fun append xs ys = xs @ ys; 
484 

5904  485 
fun apply [] x = x 
486 
 apply (f :: fs) x = apply fs (f x); 

487 

233  488 

489 
(* fold *) 

490 

16654  491 
fun fold f = 
492 
let 

493 
fun fold_aux [] y = y 

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

495 
in fold_aux end; 

15760  496 

16654  497 
fun fold_rev f = 
498 
let 

499 
fun fold_aux [] y = y 

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

501 
in fold_aux end; 

502 

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

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

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

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

507 
let 
16705  508 
val (x', y') = f x y; 
509 
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

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

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

512 

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

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

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

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

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

520 
in itl end; 

521 

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

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

524 
fun foldr f (l, e) = 

525 
let fun itr [] = e 

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

527 
in itr l end; 

528 

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

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

531 
fun foldr1 f l = 

4181  532 
let fun itr [x] = x 
233  533 
 itr (x::l) = f(x, itr l) 
534 
in itr l end; 

535 

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

16705  538 
fun foldl_map f = 
539 
let 

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

541 
 fold_aux (x, y :: ys) = 

542 
let 

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

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

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

546 
in fold_aux end; 

547 

233  548 

549 
(* basic list functions *) 

550 

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

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

553 
 take (n, x :: xs) = 

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

555 

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

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

558 
 drop (n, x :: xs) = 

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

0  560 

13629  561 
fun splitAt(n,[]) = ([],[]) 
562 
 splitAt(n,xs as x::ys) = 

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

564 
else ([],xs) 

565 

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

568 

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

15570  571 
fun nth_elem (i,xs) = List.nth(xs,i); 
233  572 

11773  573 
fun map_nth_elem 0 f (x :: xs) = f x :: xs 
574 
 map_nth_elem n f (x :: xs) = x :: map_nth_elem (n  1) f xs 

15570  575 
 map_nth_elem _ _ [] = raise Subscript; 
11773  576 

233  577 
(*last element of a list*) 
15570  578 
val last_elem = List.last; 
233  579 

3762  580 
(*rear decomposition*) 
15570  581 
fun split_last [] = raise Empty 
3762  582 
 split_last [x] = ([], x) 
583 
 split_last (x :: xs) = apfst (cons x) (split_last xs); 

584 

4893  585 
(*update nth element*) 
13629  586 
fun nth_update x n_xs = 
587 
(case splitAt n_xs of 

15570  588 
(_,[]) => raise Subscript 
13629  589 
 (prfx, _ :: sffx') => prfx @ (x :: sffx')) 
4893  590 

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

593 
let fun find _ [] = ~1 

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

595 
in find 0 end; 

3762  596 

4224  597 
fun find_index_eq x = find_index (equal x); 
4212  598 

599 
(*find first element satisfying predicate*) 

15531  600 
fun find_first _ [] = NONE 
4212  601 
 find_first pred (x :: xs) = 
15531  602 
if pred x then SOME x else find_first pred xs; 
233  603 

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

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

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

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

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

610 

233  611 
(*flatten a list of lists to a list*) 
15531  612 
val flat = List.concat; 
233  613 

12136  614 
fun unflat (xs :: xss) ys = 
13629  615 
let val (ps,qs) = splitAt(length xs,ys) 
616 
in ps :: unflat xss qs end 

12136  617 
 unflat [] [] = [] 
15570  618 
 unflat _ _ = raise UnequalLengths; 
12136  619 

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

15570  622 
val seq = List.app; 
233  623 

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

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

626 
 separate _ xs = xs; 

627 

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

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

630 
let fun rep (0, xs) = xs 

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

632 
in 

15570  633 
if n < 0 then raise Subscript 
233  634 
else rep (n, []) 
635 
end; 

636 

14926  637 
fun translate_string f = String.translate (f o String.str); 
638 

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

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

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

641 
 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

642 

14792  643 
(*direct product*) 
644 
fun product _ [] = [] 

645 
 product [] _ = [] 

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

647 

233  648 

649 
(* filter *) 

650 

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

15531  652 
val filter = List.filter; 
0  653 
fun filter_out f = filter (not o f); 
15570  654 
val mapfilter = List.mapPartial; 
233  655 

656 

657 
(* lists of pairs *) 

658 

15570  659 
exception UnequalLengths; 
660 

380  661 
fun map2 _ ([], []) = [] 
16878  662 
 map2 f (x :: xs, y :: ys) = f (x, y) :: map2 f (xs, ys) 
15570  663 
 map2 _ _ = raise UnequalLengths; 
380  664 

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

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

15570  667 
 exists2 _ _ = raise UnequalLengths; 
380  668 

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

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

15570  671 
 forall2 _ _ = raise UnequalLengths; 
380  672 

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

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

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

676 

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

679 
fun [] ~~ [] = [] 

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

15570  681 
 _ ~~ _ = raise UnequalLengths; 
233  682 

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

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

15570  685 
val split_list = ListPair.unzip; 
233  686 

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

687 
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

688 

233  689 

690 
(* prefixes, suffixes *) 

691 

692 
fun [] prefix _ = true 

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

694 
 _ prefix _ = false; 

695 

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

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

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

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

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

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

701 
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) 
233  702 
in take([], xs) end; 
703 

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

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

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

707 
 take_suffix pred (x :: xs) = 

708 
(case take_suffix pred xs of 

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

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

711 

12249  712 
fun prefixes1 [] = [] 
713 
 prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs); 

714 

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

716 

233  717 

718 

719 
(** integers **) 

720 

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

722 
let fun gxd x y : IntInf.int = 
10692  723 
if y = 0 then x else gxd y (x mod y) 
724 
in if x < y then gxd y x else gxd x y end; 

725 

16439  726 
fun lcm (x, y) = (x * y) div gcd (x, y); 
10692  727 

2958  728 
fun inc i = (i := ! i + 1; ! i); 
729 
fun dec i = (i := ! i  1; ! i); 

233  730 

731 

732 
(* lists of integers *) 

733 

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

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

735 
fun (from upto to) = 
233  736 
if from > to then [] else from :: ((from + 1) upto to); 
737 

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

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

739 
fun (from downto to) = 
233  740 
if from < to then [] else from :: ((from  1) downto to); 
741 

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

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

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

745 

746 

747 
(* convert integers to strings *) 

748 

749 
(*expand the number in the given base; 

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

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

752 
let 

753 
fun radix (n, tail) = 

754 
if n < base then n :: tail 

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

756 
in radix (num, []) end; 

757 

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

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

760 
fun radixstring (base, zerochar, num) = 

761 
let val offset = ord zerochar; 

762 
fun chrof n = chr (offset + n) 

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

764 

765 

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

766 
val string_of_int = Int.toString; 
233  767 

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

768 
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

769 
 string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; 
233  770 

771 

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

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

773 

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

774 
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

775 
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

776 
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

777 
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

778 
 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

779 
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

780 
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

781 
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

782 
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

783 

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

784 
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

785 

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

786 
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

787 

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

788 

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

789 

233  790 
(** strings **) 
791 

16188  792 
(* functions tuned for strings, avoiding explode *) 
6312  793 

794 
fun nth_elem_string (i, str) = 

6959  795 
(case try String.substring (str, i, 1) of 
15531  796 
SOME s => s 
15570  797 
 NONE => raise Subscript); 
6312  798 

16188  799 
fun fold_string f str x0 = 
6282  800 
let 
801 
val n = size str; 

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

804 
in iter (x0, 0) end; 

6282  805 

14968  806 
fun exists_string pred str = 
807 
let 

808 
val n = size str; 

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

810 
in ex 0 end; 

6312  811 

16188  812 
fun forall_string pred = not o exists_string (not o pred); 
813 

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

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

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

817 

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

819 
val quote = enclose "\"" "\""; 
233  820 

4212  821 
(*space_implode "..." (explode "hello") = "h...e...l...l...o"*) 
233  822 
fun space_implode a bs = implode (separate a bs); 
823 

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

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

826 

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

828 
val cat_lines = space_implode "\n"; 
233  829 

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

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

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

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

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

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

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

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

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

839 

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

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

841 

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

842 
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

843 
 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

844 

7712  845 
fun untabify chs = 
846 
let 

847 
val tab_width = 8; 

848 

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

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

7712  852 
 untab (pos, c) = (pos + 1, [c]); 
853 
in 

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

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

856 
end; 

857 

16188  858 
fun suffix sffx s = s ^ sffx; 
5285  859 

16188  860 
fun unsuffix sffx s = 
17061  861 
if String.isSuffix sffx s then String.substring (s, 0, size s  size sffx) 
862 
else raise Fail "unsuffix"; 

5285  863 

15060  864 
fun unprefix prfx s = 
17061  865 
if String.isPrefix prfx s then String.substring (s, size prfx, size s  size prfx) 
866 
else raise Fail "unprefix"; 

15060  867 

10951  868 
fun replicate_string 0 _ = "" 
869 
 replicate_string 1 a = a 

870 
 replicate_string k a = 

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

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

873 

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

874 

233  875 

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

878 
(*membership in a list*) 

879 
fun x mem [] = false 

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

0  881 

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

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

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

884 
 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

885 

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

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

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

888 
 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

889 

0  890 
(*generalized membership test*) 
233  891 
fun gen_mem eq (x, []) = false 
892 
 gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); 

893 

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

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

233  898 

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

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

900 
fun (x ins xs) = if x mem xs then xs else x :: xs; 
0  901 

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

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

903 
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

904 

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

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

906 
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

907 

0  908 
(*generalized insertion*) 
15760  909 
fun gen_ins eq (x, xs) = insert eq x xs; 
233  910 

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

912 
fun xs union [] = xs 

913 
 [] union ys = ys 

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

0  915 

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

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

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

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

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

920 

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

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

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

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

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

925 

0  926 
(*generalized union*) 
233  927 
fun gen_union eq (xs, []) = xs 
928 
 gen_union eq ([], ys) = ys 

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

930 

931 

932 
(*intersection*) 

933 
fun [] inter ys = [] 

934 
 (x :: xs) inter ys = 

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

936 

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

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

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

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

940 
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

941 

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

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

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

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

945 
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

946 

7090  947 
(*generalized intersection*) 
948 
fun gen_inter eq ([], ys) = [] 

12284  949 
 gen_inter eq (x::xs, ys) = 
7090  950 
if gen_mem eq (x,ys) then x :: gen_inter eq (xs, ys) 
12284  951 
else gen_inter eq (xs, ys); 
7090  952 

233  953 

954 
(*subset*) 

955 
fun [] subset ys = true 

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

957 

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

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

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

961 

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

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

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

965 

4363  966 
(*set equality*) 
967 
fun eq_set (xs, ys) = 

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

969 

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

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

972 
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

973 

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

974 
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

975 

265  976 

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

979 
 [] \ x = []; 

980 

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

981 
fun ys \\ xs = foldl (op \) (ys,xs); 
0  982 

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

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

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

988 
first of equal elements*) 

989 
fun gen_distinct eq lst = 

990 
let 

991 
val memb = gen_mem eq; 

0  992 

233  993 
fun dist (rev_seen, []) = rev rev_seen 
994 
 dist (rev_seen, x :: xs) = 

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

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

997 
in 

998 
dist ([], lst) 

999 
end; 

1000 

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

1001 
fun distinct l = gen_distinct (op =) l; 
233  1002 

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

1004 
fun findrep [] = [] 

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

1006 

1007 

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

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

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

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

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

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

1013 

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

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

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

1016 
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

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

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

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

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

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

1022 

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

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

1024 

16878  1025 
fun has_duplicates eq = 
1026 
let 

1027 
fun dups [] = false 

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

1029 
in dups end; 

1030 

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

1031 

233  1032 

1033 
(** association lists **) 

0  1034 

233  1035 
(*association list lookup*) 
15531  1036 
fun assoc ([], key) = NONE 
233  1037 
 assoc ((keyi, xi) :: pairs, key) = 
15531  1038 
if key = keyi then SOME xi else assoc (pairs, key); 
233  1039 

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

1040 
(*association list lookup, optimized version for ints*) 
15531  1041 
fun assoc_int ([], (key:int)) = NONE 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

1042 
 assoc_int ((keyi, xi) :: pairs, key) = 
15531  1043 
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

1044 

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

1045 
(*association list lookup, optimized version for strings*) 
15531  1046 
fun assoc_string ([], (key:string)) = NONE 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

1047 
 assoc_string ((keyi, xi) :: pairs, key) = 
15531  1048 
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

1049 

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

1050 
(*association list lookup, optimized version for string*ints*) 
15531  1051 
fun assoc_string_int ([], (key:string*int)) = NONE 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

1052 
 assoc_string_int ((keyi, xi) :: pairs, key) = 
15531  1053 
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

1054 

17153  1055 
fun assocs z = curry (these o assoc) z 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

1056 

233  1057 
(*generalized association list lookup*) 
15531  1058 
fun gen_assoc eq ([], key) = NONE 
233  1059 
 gen_assoc eq ((keyi, xi) :: pairs, key) = 
15531  1060 
if eq (key, keyi) then SOME xi else gen_assoc eq (pairs, key); 
233  1061 

1062 
(*association list update*) 

1063 
fun overwrite (al, p as (key, _)) = 

1064 
let fun over ((q as (keyi, _)) :: pairs) = 

1065 
if keyi = key then p :: pairs else q :: (over pairs) 

1066 
 over [] = [p] 

1067 
in over al end; 

1068 

2522  1069 
fun gen_overwrite eq (al, p as (key, _)) = 
1070 
let fun over ((q as (keyi, _)) :: pairs) = 

1071 
if eq (keyi, key) then p :: pairs else q :: (over pairs) 

1072 
 over [] = [p] 

1073 
in over al end; 

1074 

233  1075 

12284  1076 
(* lists as tables *) 
233  1077 

12284  1078 
fun gen_merge_lists _ xs [] = xs 
1079 
 gen_merge_lists _ [] ys = ys 

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

0  1081 

12284  1082 
fun gen_merge_lists' _ xs [] = xs 
1083 
 gen_merge_lists' _ [] ys = ys 

12295  1084 
 gen_merge_lists' eq xs ys = gen_rems eq (ys, xs) @ xs; 
0  1085 

12284  1086 
fun merge_lists xs ys = gen_merge_lists (op =) xs ys; 
1087 
fun merge_lists' xs ys = gen_merge_lists' (op =) xs ys; 

1088 
fun merge_alists al = gen_merge_lists eq_fst al; 

15263  1089 
fun merge_alists' al = gen_merge_lists' eq_fst al; 
380  1090 

0  1091 

1092 

233  1093 
(** balanced trees **) 
1094 

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

1096 

1097 
(*balanced folding; avoids deep nesting*) 

1098 
fun fold_bal f [x] = x 

1099 
 fold_bal f [] = raise Balance 

1100 
 fold_bal f xs = 

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

233  1103 

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

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

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

1107 
if n=1 then x else 

1108 
let val n2 = n div 2 

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

1110 
else g (acc (nn2) (in2)) 

1111 
end 

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

1113 

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

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

1116 
let fun acc n = 

1117 
if n=1 then [x] else 

1118 
let val n2 = n div 2 

1119 
val acc2 = acc n2 

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

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

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

1123 

1124 

1125 

2506  1126 
(** orders **) 
1127 

4445  1128 
fun rev_order LESS = GREATER 
1129 
 rev_order EQUAL = EQUAL 

1130 
 rev_order GREATER = LESS; 

1131 

4479  1132 
(*assume rel is a linear strict order*) 
4445  1133 
fun make_ord rel (x, y) = 
1134 
if rel (x, y) then LESS 

1135 
else if rel (y, x) then GREATER 

1136 
else EQUAL; 

1137 

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

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

1139 
val string_ord = String.compare; 
2506  1140 

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

1143 

16492  1144 
fun option_ord ord (SOME x, SOME y) = ord (x, y) 
1145 
 option_ord _ (NONE, NONE) = EQUAL 

1146 
 option_ord _ (NONE, SOME _) = LESS 

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

1148 

4343  1149 
(*lexicographic product*) 
1150 
fun prod_ord a_ord b_ord ((x, y), (x', y')) = 

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

1152 

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

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

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

4343  1157 
 dict_ord _ ([], _ :: _) = LESS 
16984  1158 
 dict_ord _ (_ :: _, []) = GREATER; 
4343  1159 

1160 
(*lexicographic product of lists*) 

1161 
fun list_ord elem_ord (xs, ys) = 

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

2506  1164 

4621  1165 
(* sorting *) 
1166 

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

1168 
fun sort ord = 

1169 
let 

16878  1170 
fun qsort [] = [] 
1171 
 qsort (xs as [_]) = xs 

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

1173 
 qsort xs = 

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

1175 
in qsort lts @ eqs @ qsort gts end 

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

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

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

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

1181 
in qsort end; 

1182 

1183 
(*sort strings*) 

1184 
val sort_strings = sort string_ord; 

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

1186 

11514  1187 
fun unique_strings ([]: string list) = [] 
1188 
 unique_strings [x] = [x] 

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

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

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

4621  1192 

2506  1193 

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

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

1195 

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

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

1197 

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

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

1199 

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

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

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

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

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

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

1205 

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

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

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

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

1209 

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

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

1211 

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

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

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

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

1215 

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

1216 
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

1217 

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

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

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

1220 
val sum = foldl op + (0, map fst xs); 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1221 
fun pick n ((k, x) :: xs) = 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

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

1223 
in pick (random_range 1 sum) xs end; 
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset

1224 

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

1225 

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

1226 
(** current directory **) 
233  1227 

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

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

1230 

3606  1231 

233  1232 

10692  1233 
(** rational numbers **) 
17028  1234 
(* Keep them normalized! *) 
10692  1235 

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

1236 
datatype rat = Rat of bool * IntInf.int * IntInf.int 
10692  1237 

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

1238 
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

1239 

10692  1240 
fun rep_rat(Rat(a,p,q)) = (if a then p else ~p,q) 
1241 

1242 
fun ratnorm(a,p,q) = if p=0 then Rat(a,0,1) else 

1243 
let val absp = abs p 

1244 
val m = gcd(absp,q) 

1245 
in Rat(a = (0 <= p), absp div m, q div m) end; 

1246 

17028  1247 
fun ratcommon(p,q,r,s) = 
10692  1248 
let val den = lcm(q,s) 
17028  1249 
in (p*(den div q), r*(den div s), den) end 
1250 

1251 
fun ratge0(Rat(a,p,q)) = a; 

1252 
fun ratgt0(Rat(a,p,q)) = a andalso p > 0; 

1253 

1254 
fun ratle(Rat(a,p,q),Rat(b,r,s)) = 

1255 
not a andalso b orelse 

1256 
a = b andalso 

1257 
let val (p,r,_) = ratcommon(p,q,r,s) 

1258 
in if a then p <= r else r <= p end 

1259 

1260 
fun ratlt(Rat(a,p,q),Rat(b,r,s)) = 

1261 
not a andalso b orelse 

1262 
a = b andalso 

1263 
let val (p,r,_) = ratcommon(p,q,r,s) 

1264 
in if a then p < r else r < p end 

1265 

1266 
fun ratadd(Rat(a,p,q),Rat(b,r,s)) = 

17032  1267 
let val (p,r,den) = ratcommon(p,q,r,s) 
10692  1268 
val num = (if a then p else ~p) + (if b then r else ~r) 
1269 
in ratnorm(true,num,den) end; 

1270 

1271 
fun ratmul(Rat(a,p,q),Rat(b,r,s)) = ratnorm(a=b,p*r,q*s) 

1272 

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

1273 
fun ratinv(Rat(a,p,q)) = if p=0 then raise RAT "ratinv" else Rat(a,q,p) 
10692  1274 

1275 
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

1276 
if q=0 then raise RAT "int_ratdiv" else ratnorm(0<=q, p, abs q) 
10692  1277 

1278 
fun ratneg(Rat(b,p,q)) = Rat(not b,p,q); 

1279 

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

1280 
fun rat_of_intinf i = if i < 0 then Rat(false,abs i,1) else Rat(true,i,1); 
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15760
diff
changeset

1281 

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

1282 
fun rat_of_int i = rat_of_intinf (IntInf.fromInt i); 
10692  1283 

17028  1284 
val rat0 = rat_of_int 0; 
10692  1285 

4621  1286 
(** misc **) 
233  1287 

1288 
(*use the keyfun to make a list of (x, key) pairs*) 

0  1289 
fun make_keylist (keyfun: 'a>'b) : 'a list > ('a * 'b) list = 
233  1290 
let fun keypair x = (x, keyfun x) 
1291 
in map keypair end; 

0  1292 

233  1293 
(*given a list of (x, key) pairs and a searchkey 
0  1294 
return the list of xs from each pair whose key equals searchkey*) 
1295 
fun keyfilter [] searchkey = [] 

233  1296 
 keyfilter ((x, key) :: pairs) searchkey = 
1297 
if key = searchkey then x :: keyfilter pairs searchkey 

1298 
else keyfilter pairs searchkey; 

0  1299 

1300 

1301 
(*Partition list into elements that satisfy predicate and those that don't. 

233  1302 
Preserves order of elements in both lists.*) 
15570  1303 
val partition = List.partition; 
0  1304 

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

16842  1306 
let 
1307 
fun part [] = [] 

1308 
 part (x :: ys) = 

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

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

1311 
in part end; 

0  1312 

1313 

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

233  1317 
let fun part k xs = 
1318 
if k>j then 

0  1319 
(case xs of [] => [] 
15570  1320 
 _ => raise Fail "partition_list") 
0  1321 
else 
233  1322 
let val (ns, rest) = partition (p k) xs; 
1323 
in ns :: part(k+1)rest end 

0  1324 
in part i end; 
1325 

1326 

233  1327 
(* generating identifiers *) 
0  1328 

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

5904 