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