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