author | wenzelm |
Wed, 26 Nov 2014 20:05:34 +0100 | |
changeset 59058 | a78612c67ec0 |
parent 58635 | 010b610eb55d |
child 59172 | d1c500e0a722 |
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 |
233 | 2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
16188 | 3 |
Author: Markus Wenzel, TU Muenchen |
0 | 4 |
|
28732 | 5 |
Basic library: functions, pairs, booleans, lists, integers, |
23424
d0580634f128
moved balanced tree operations to General/balanced_tree.ML;
wenzelm
parents:
23251
diff
changeset
|
6 |
strings, lists as sets, orders, current directory, misc. |
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
7 |
|
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
8 |
See also General/basics.ML for the most fundamental concepts. |
0 | 9 |
*) |
10 |
||
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
11 |
infix 2 ? |
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
12 |
infix 3 o oo ooo oooo |
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
13 |
infix 4 ~~ upto downto |
36692
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents:
35976
diff
changeset
|
14 |
infix orf andf |
5893 | 15 |
|
15745 | 16 |
signature BASIC_LIBRARY = |
4621 | 17 |
sig |
18 |
(*functions*) |
|
23860 | 19 |
val undefined: 'a -> 'b |
16842 | 20 |
val I: 'a -> 'a |
21 |
val K: 'a -> 'b -> 'a |
|
4621 | 22 |
val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c |
23 |
val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c |
|
21565 | 24 |
val ? : bool * ('a -> 'a) -> 'a -> 'a |
16721 | 25 |
val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b |
26 |
val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b |
|
27 |
val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b |
|
16842 | 28 |
val funpow: int -> ('a -> 'a) -> 'a -> 'a |
31250 | 29 |
val funpow_yield: int -> ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
30 |
|
43652 | 31 |
(*user errors*) |
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
32 |
exception ERROR of string |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
33 |
val error: string -> 'a |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
34 |
val cat_error: string -> string -> 'a |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
35 |
|
4621 | 36 |
(*pairs*) |
37 |
val pair: 'a -> 'b -> 'a * 'b |
|
38 |
val rpair: 'a -> 'b -> 'b * 'a |
|
39 |
val fst: 'a * 'b -> 'a |
|
40 |
val snd: 'a * 'b -> 'b |
|
17498
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset
|
41 |
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
|
42 |
val eq_snd: ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool |
19454
46a7e133f802
moved coalesce to AList, added equality predicates to library
haftmann
parents:
19424
diff
changeset
|
43 |
val eq_pair: ('a * 'c -> bool) -> ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool |
4621 | 44 |
val swap: 'a * 'b -> 'b * 'a |
45 |
val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c |
|
46 |
val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58635
diff
changeset
|
47 |
val apply2: ('a -> 'b) -> 'a * 'a -> 'b * 'b |
4621 | 48 |
|
49 |
(*booleans*) |
|
50 |
val equal: ''a -> ''a -> bool |
|
51 |
val not_equal: ''a -> ''a -> bool |
|
52 |
val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool |
|
53 |
val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool |
|
54 |
val exists: ('a -> bool) -> 'a list -> bool |
|
55 |
val forall: ('a -> bool) -> 'a list -> bool |
|
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32737
diff
changeset
|
56 |
val setmp_CRITICAL: 'a Unsynchronized.ref -> 'a -> ('b -> 'c) -> 'b -> 'c |
25797 | 57 |
val setmp_thread_data: 'a Universal.tag -> 'a -> 'a -> ('b -> 'c) -> 'b -> 'c |
4621 | 58 |
|
59 |
(*lists*) |
|
5285 | 60 |
val single: 'a -> 'a list |
20882 | 61 |
val the_single: 'a list -> 'a |
19273 | 62 |
val singleton: ('a list -> 'b list) -> 'a -> 'b |
25061 | 63 |
val yield_singleton: ('a list -> 'c -> 'b list * 'c) -> 'a -> 'c -> 'b * 'c |
25058 | 64 |
val perhaps_apply: ('a -> 'a option) list -> 'a -> 'a option |
65 |
val perhaps_loop: ('a -> 'a option) -> 'a -> 'a option |
|
25681 | 66 |
val foldl1: ('a * 'a -> 'a) -> 'a list -> 'a |
15760 | 67 |
val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a |
46891 | 68 |
val eq_list: ('a * 'a -> bool) -> 'a list * 'a list -> bool |
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
69 |
val maps: ('a -> 'b list) -> 'a list -> 'b list |
25549 | 70 |
val filter: ('a -> bool) -> 'a list -> 'a list |
71 |
val filter_out: ('a -> bool) -> 'a list -> 'a list |
|
72 |
val map_filter: ('a -> 'b option) -> 'a list -> 'b list |
|
33955 | 73 |
val take: int -> 'a list -> 'a list |
74 |
val drop: int -> 'a list -> 'a list |
|
19011 | 75 |
val chop: int -> 'a list -> 'a list * 'a list |
46891 | 76 |
val chop_groups: int -> 'a list -> 'a list list |
18278 | 77 |
val nth: 'a list -> int -> 'a |
46891 | 78 |
val nth_list: 'a list list -> int -> 'a list |
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17952
diff
changeset
|
79 |
val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list |
24846 | 80 |
val nth_drop: int -> 'a list -> 'a list |
18514 | 81 |
val map_index: (int * 'a -> 'b) -> 'a list -> 'b list |
82 |
val fold_index: (int * 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b |
|
33063 | 83 |
val map_range: (int -> 'a) -> int -> 'a list |
52271
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
84 |
val fold_range: (int -> 'a -> 'a) -> int -> 'a -> 'a |
4621 | 85 |
val split_last: 'a list -> 'a list * 'a |
46891 | 86 |
val find_first: ('a -> bool) -> 'a list -> 'a option |
4621 | 87 |
val find_index: ('a -> bool) -> 'a list -> int |
46891 | 88 |
val get_first: ('a -> 'b option) -> 'a list -> 'b option |
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19119
diff
changeset
|
89 |
val get_index: ('a -> 'b option) -> 'a list -> (int * 'b) option |
46891 | 90 |
val flat: 'a list list -> 'a list |
91 |
val unflat: 'a list list -> 'b list -> 'b list list |
|
92 |
val grouped: int -> (('a list -> 'b list) -> 'c list list -> 'd list list) -> |
|
93 |
('a -> 'b) -> 'c list -> 'd list |
|
94 |
val burrow: ('a list -> 'b list) -> 'a list list -> 'b list list |
|
95 |
val burrow_options: ('a list -> 'b list) -> 'a option list -> 'b option list |
|
96 |
val fold_burrow: ('a list -> 'c -> 'b list * 'd) -> 'a list list -> 'c -> 'b list list * 'd |
|
97 |
val separate: 'a -> 'a list -> 'a list |
|
98 |
val surround: 'a -> 'a list -> 'a list |
|
99 |
val replicate: int -> 'a -> 'a list |
|
100 |
val map_product: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list |
|
101 |
val fold_product: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c |
|
18330 | 102 |
val map2: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list |
103 |
val fold2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c |
|
46891 | 104 |
val map_split: ('a -> 'b * 'c) -> 'a list -> 'b list * 'c list |
19799 | 105 |
val zip_options: 'a list -> 'b option list -> ('a * 'b) list |
18330 | 106 |
val ~~ : 'a list * 'b list -> ('a * 'b) list |
107 |
val split_list: ('a * 'b) list -> 'a list * 'b list |
|
46891 | 108 |
val burrow_fst: ('a list -> 'b list) -> ('a * 'c) list -> ('b * 'c) list |
18441 | 109 |
val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool |
4621 | 110 |
val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list |
20108 | 111 |
val chop_prefix: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list * ('a list * 'b list) |
4621 | 112 |
val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list |
12249 | 113 |
val prefixes1: 'a list -> 'a list list |
19011 | 114 |
val prefixes: 'a list -> 'a list list |
12249 | 115 |
val suffixes1: 'a list -> 'a list list |
19011 | 116 |
val suffixes: 'a list -> 'a list list |
4621 | 117 |
|
118 |
(*integers*) |
|
119 |
val upto: int * int -> int list |
|
120 |
val downto: int * int -> int list |
|
121 |
val radixpand: int * int -> int list |
|
122 |
val radixstring: int * string * int -> string |
|
123 |
val string_of_int: int -> string |
|
21942
d6218d0f9ec3
added signed_string_of_int (pruduces proper - instead of SML's ~);
wenzelm
parents:
21920
diff
changeset
|
124 |
val signed_string_of_int: int -> string |
4621 | 125 |
val string_of_indexname: string * int -> string |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24593
diff
changeset
|
126 |
val read_radix_int: int -> string list -> int * 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
|
127 |
val read_int: string list -> int * string list |
48cfe0fe53e2
output channels and diagnostics moved to General/output.ML; added read_int etc. from term.ML; removed obsolete mtree; type rat uses exception RAT;
wenzelm
parents:
14797
diff
changeset
|
128 |
val oct_char: string -> string |
4621 | 129 |
|
130 |
(*strings*) |
|
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17952
diff
changeset
|
131 |
val nth_string: string -> int -> string |
16188 | 132 |
val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a |
6312 | 133 |
val exists_string: (string -> bool) -> string -> bool |
16188 | 134 |
val forall_string: (string -> bool) -> string -> bool |
28025 | 135 |
val first_field: string -> string -> (string * string) option |
4621 | 136 |
val enclose: string -> string -> string -> string |
6642 | 137 |
val unenclose: string -> string |
4621 | 138 |
val quote: string -> string |
55033 | 139 |
val cartouche: string -> string |
4621 | 140 |
val space_implode: string -> string list -> string |
141 |
val commas: string list -> string |
|
142 |
val commas_quote: string list -> string |
|
143 |
val cat_lines: string list -> string |
|
144 |
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
|
145 |
val split_lines: string -> string list |
56038
0e2dec666152
tuned messages -- in accordance to Isabelle/Scala;
wenzelm
parents:
55033
diff
changeset
|
146 |
val plain_words: string -> string |
5942 | 147 |
val prefix_lines: string -> string -> string |
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
148 |
val prefix: string -> string -> string |
5285 | 149 |
val suffix: string -> string -> string |
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
150 |
val unprefix: string -> string -> string |
5285 | 151 |
val unsuffix: string -> string -> string |
47499
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents:
47060
diff
changeset
|
152 |
val trim_line: string -> string |
10951 | 153 |
val replicate_string: int -> string -> string |
14926 | 154 |
val translate_string: (string -> string) -> string -> string |
29882
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
155 |
val match_string: string -> string -> bool |
4621 | 156 |
|
41516
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
157 |
(*reals*) |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
158 |
val string_of_real: real -> string |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
159 |
val signed_string_of_real: real -> string |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
160 |
|
16492 | 161 |
(*lists as sets -- see also Pure/General/ord_list.ML*) |
18923 | 162 |
val member: ('b * 'a -> bool) -> 'a list -> 'b -> bool |
163 |
val insert: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list |
|
164 |
val remove: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list |
|
24049 | 165 |
val update: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list |
33042 | 166 |
val union: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list |
19301 | 167 |
val subtract: ('b * 'a -> bool) -> 'b list -> 'a list -> 'a list |
33049
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
haftmann
parents:
33040
diff
changeset
|
168 |
val inter: ('a * 'b -> bool) -> 'b list -> 'a list -> 'a list |
18923 | 169 |
val merge: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list |
33038 | 170 |
val subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool |
42403
38b29c9fc742
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
wenzelm
parents:
41516
diff
changeset
|
171 |
val eq_set: ('a * 'a -> bool) -> 'a list * 'a list -> bool |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19011
diff
changeset
|
172 |
val distinct: ('a * 'a -> bool) -> 'a list -> 'a list |
18966 | 173 |
val duplicates: ('a * 'a -> bool) -> 'a list -> 'a list |
16878 | 174 |
val has_duplicates: ('a * 'a -> bool) -> 'a list -> bool |
46891 | 175 |
val map_transpose: ('a list -> 'b) -> 'a list list -> 'b list |
4621 | 176 |
|
23220 | 177 |
(*lists as multisets*) |
22142 | 178 |
val remove1: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list |
33078
3aea60ca3900
multiset operations with canonical argument order
haftmann
parents:
33063
diff
changeset
|
179 |
val combine: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list |
33079 | 180 |
val submultiset: ('a * 'b -> bool) -> 'a list * 'b list -> bool |
4621 | 181 |
|
182 |
(*orders*) |
|
18966 | 183 |
val is_equal: order -> bool |
4621 | 184 |
val rev_order: order -> order |
185 |
val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order |
|
25224 | 186 |
val bool_ord: bool * bool -> order |
4621 | 187 |
val int_ord: int * int -> order |
188 |
val string_ord: string * string -> order |
|
16676 | 189 |
val fast_string_ord: string * string -> order |
16492 | 190 |
val option_ord: ('a * 'b -> order) -> 'a option * 'b option -> order |
4621 | 191 |
val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order |
192 |
val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order |
|
193 |
val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order |
|
194 |
val sort: ('a * 'a -> order) -> 'a list -> 'a list |
|
18427 | 195 |
val sort_distinct: ('a * 'a -> order) -> 'a list -> 'a list |
4621 | 196 |
val sort_strings: string list -> string list |
197 |
val sort_wrt: ('a -> string) -> 'a list -> 'a list |
|
30558
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
198 |
val tag_list: int -> 'a list -> (int * 'a) list |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
199 |
val untag_list: (int * 'a) list -> 'a list |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
200 |
val order_list: (int * 'a) list -> 'a list |
4621 | 201 |
|
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
202 |
(*random numbers*) |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
203 |
exception RANDOM |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
204 |
val random: unit -> real |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
205 |
val random_range: int -> int -> int |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
206 |
|
4621 | 207 |
(*misc*) |
19644
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
208 |
val divide_and_conquer: ('a -> 'a list * ('b list -> 'b)) -> 'a -> 'b |
32978 | 209 |
val divide_and_conquer': ('a -> 'b -> ('a list * ('c list * 'b -> 'c * 'b)) * 'b) -> |
210 |
'a -> 'b -> 'c * 'b |
|
4621 | 211 |
val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list |
212 |
val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list |
|
40509 | 213 |
type serial = int |
16439 | 214 |
val serial: unit -> serial |
19512 | 215 |
val serial_string: unit -> string |
45626
b4f374a45668
more abstract datatype stamp, which avoids pointless allocation of mutable cells;
wenzelm
parents:
44617
diff
changeset
|
216 |
eqtype stamp |
b4f374a45668
more abstract datatype stamp, which avoids pointless allocation of mutable cells;
wenzelm
parents:
44617
diff
changeset
|
217 |
val stamp: unit -> stamp |
51368
2ea5c7c2d825
tuned signature -- prefer terminology of Scala and Axiom;
wenzelm
parents:
50913
diff
changeset
|
218 |
structure Any: sig type T = exn end |
43603 | 219 |
val cd: string -> unit |
220 |
val pwd: unit -> string |
|
221 |
val getenv: string -> string |
|
222 |
val getenv_strict: string -> string |
|
4621 | 223 |
end; |
224 |
||
15745 | 225 |
signature LIBRARY = |
15570 | 226 |
sig |
15745 | 227 |
include BASIC_LIBRARY |
15570 | 228 |
val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a |
229 |
val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b |
|
230 |
end; |
|
231 |
||
15745 | 232 |
structure Library: LIBRARY = |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
233 |
struct |
0 | 234 |
|
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
235 |
(* functions *) |
0 | 236 |
|
23860 | 237 |
fun undefined _ = raise Match; |
238 |
||
16842 | 239 |
fun I x = x; |
240 |
fun K x = fn _ => x; |
|
233 | 241 |
fun curry f x y = f (x, y); |
242 |
fun uncurry f (x, y) = f x y; |
|
0 | 243 |
|
17141
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset
|
244 |
(*conditional application*) |
21565 | 245 |
fun b ? f = fn x => if b then f x else x; |
17141
4b0dc89de43b
added ? combinator for conditional transformations
haftmann
parents:
17101
diff
changeset
|
246 |
|
16721 | 247 |
(*composition with multiple args*) |
248 |
fun (f oo g) x y = f (g x y); |
|
249 |
fun (f ooo g) x y z = f (g x y z); |
|
250 |
fun (f oooo g) x y z w = f (g x y z w); |
|
251 |
||
31250 | 252 |
(*function exponentiation: f (... (f x) ...) with n applications of f*) |
253 |
fun funpow (0 : int) _ = I |
|
254 |
| funpow n f = f #> funpow (n - 1) f; |
|
160 | 255 |
|
31250 | 256 |
fun funpow_yield (0 : int) _ x = ([], x) |
257 |
| funpow_yield n f x = x |> f ||>> funpow_yield (n - 1) f |>> op ::; |
|
160 | 258 |
|
40318
035b2afbeb2e
discontinued obsolete function sys_error and exception SYS_ERROR;
wenzelm
parents:
40291
diff
changeset
|
259 |
|
43652 | 260 |
(* user errors *) |
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
261 |
|
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
262 |
exception ERROR of string; |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
263 |
fun error msg = raise ERROR msg; |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
264 |
|
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
265 |
fun cat_error "" msg = error msg |
57831
885888a880fb
more general notion of "user error" including empty message -- NB: Output.error_message needs non-empty string to emit anything;
wenzelm
parents:
56038
diff
changeset
|
266 |
| cat_error msg "" = error msg |
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
267 |
| cat_error msg1 msg2 = error (msg1 ^ "\n" ^ msg2); |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
268 |
|
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
269 |
|
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
270 |
(* pairs *) |
233 | 271 |
|
272 |
fun pair x y = (x, y); |
|
273 |
fun rpair x y = (y, x); |
|
274 |
||
275 |
fun fst (x, y) = x; |
|
276 |
fun snd (x, y) = y; |
|
277 |
||
17498
d83af87bbdc5
improved eq_fst and eq_snd, removed some deprecated stuff
haftmann
parents:
17486
diff
changeset
|
278 |
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
|
279 |
fun eq_snd eq ((_, y1), (_, y2)) = eq (y1, y2); |
19454
46a7e133f802
moved coalesce to AList, added equality predicates to library
haftmann
parents:
19424
diff
changeset
|
280 |
fun eq_pair eqx eqy ((x1, y1), (x2, y2)) = eqx (x1, x2) andalso eqy (y1, y2); |
233 | 281 |
|
282 |
fun swap (x, y) = (y, x); |
|
283 |
||
284 |
fun apfst f (x, y) = (f x, y); |
|
285 |
fun apsnd f (x, y) = (x, f y); |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58635
diff
changeset
|
286 |
fun apply2 f (x, y) = (f x, f y); |
233 | 287 |
|
288 |
||
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
289 |
(* booleans *) |
233 | 290 |
|
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
291 |
(*polymorphic equality*) |
233 | 292 |
fun equal x y = x = y; |
293 |
fun not_equal x y = x <> y; |
|
294 |
||
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
295 |
(*combining predicates*) |
16721 | 296 |
fun p orf q = fn x => p x orelse q x; |
297 |
fun p andf q = fn x => p x andalso q x; |
|
233 | 298 |
|
25752 | 299 |
val exists = List.exists; |
300 |
val forall = List.all; |
|
0 | 301 |
|
19644
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
302 |
|
380 | 303 |
(* flags *) |
304 |
||
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32737
diff
changeset
|
305 |
fun setmp_CRITICAL flag value f x = |
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
37851
diff
changeset
|
306 |
NAMED_CRITICAL "setmp" (fn () => Unsynchronized.setmp flag value f x); |
23932 | 307 |
|
25797 | 308 |
fun setmp_thread_data tag orig_data data f x = |
26079 | 309 |
uninterruptible (fn restore_attributes => fn () => |
310 |
let |
|
28122 | 311 |
val _ = Thread.setLocal (tag, data); |
26079 | 312 |
val result = Exn.capture (restore_attributes f) x; |
28122 | 313 |
val _ = Thread.setLocal (tag, orig_data); |
26079 | 314 |
in Exn.release result end) (); |
25797 | 315 |
|
19644
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
316 |
|
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
317 |
|
233 | 318 |
(** lists **) |
319 |
||
5285 | 320 |
fun single x = [x]; |
233 | 321 |
|
20882 | 322 |
fun the_single [x] = x |
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
47023
diff
changeset
|
323 |
| the_single _ = raise List.Empty; |
20882 | 324 |
|
325 |
fun singleton f x = the_single (f [x]); |
|
19273 | 326 |
|
25061 | 327 |
fun yield_singleton f x = f [x] #>> the_single; |
328 |
||
25058 | 329 |
fun perhaps_apply funs arg = |
330 |
let |
|
331 |
fun app [] res = res |
|
332 |
| app (f :: fs) (changed, x) = |
|
333 |
(case f x of |
|
334 |
NONE => app fs (changed, x) |
|
335 |
| SOME x' => app fs (true, x')); |
|
336 |
in (case app funs (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end; |
|
337 |
||
338 |
fun perhaps_loop f arg = |
|
339 |
let |
|
340 |
fun loop (changed, x) = |
|
341 |
(case f x of |
|
342 |
NONE => (changed, x) |
|
343 |
| SOME x' => loop (true, x')); |
|
344 |
in (case loop (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end; |
|
345 |
||
233 | 346 |
|
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
21335
diff
changeset
|
347 |
(* fold -- old versions *) |
16691
539b9cc282fa
added combinatros '||>' and '||>>' and fold_map fitting nicely to ST combinator '|->'
haftmann
parents:
16688
diff
changeset
|
348 |
|
233 | 349 |
(*the following versions of fold are designed to fit nicely with infixes*) |
0 | 350 |
|
233 | 351 |
(* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn |
352 |
for operators that associate to the left (TAIL RECURSIVE)*) |
|
353 |
fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a = |
|
354 |
let fun itl (e, []) = e |
|
355 |
| itl (e, a::l) = itl (f(e, a), l) |
|
356 |
in itl end; |
|
357 |
||
358 |
(* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e)) |
|
359 |
for operators that associate to the right (not tail recursive)*) |
|
360 |
fun foldr f (l, e) = |
|
361 |
let fun itr [] = e |
|
362 |
| itr (a::l) = f(a, itr l) |
|
363 |
in itr l end; |
|
364 |
||
25681 | 365 |
(* (op @) [x1, ..., xn] ===> ((x1 @ x2) @ x3) ... @ xn |
366 |
for operators that associate to the left (TAIL RECURSIVE)*) |
|
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
47023
diff
changeset
|
367 |
fun foldl1 f [] = raise List.Empty |
25681 | 368 |
| foldl1 f (x :: xs) = foldl f (x, xs); |
369 |
||
233 | 370 |
(* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n-1] @ xn)) |
371 |
for n > 0, operators that associate to the right (not tail recursive)*) |
|
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
47023
diff
changeset
|
372 |
fun foldr1 f [] = raise List.Empty |
20510 | 373 |
| foldr1 f l = |
20443 | 374 |
let fun itr [x] = x |
20510 | 375 |
| itr (x::l) = f(x, itr l) |
20443 | 376 |
in itr l end; |
233 | 377 |
|
378 |
||
379 |
(* basic list functions *) |
|
380 |
||
20510 | 381 |
fun eq_list eq (list1, list2) = |
42403
38b29c9fc742
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
wenzelm
parents:
41516
diff
changeset
|
382 |
pointer_eq (list1, list2) orelse |
38b29c9fc742
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
wenzelm
parents:
41516
diff
changeset
|
383 |
let |
38b29c9fc742
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
wenzelm
parents:
41516
diff
changeset
|
384 |
fun eq_lst (x :: xs, y :: ys) = eq (x, y) andalso eq_lst (xs, ys) |
38b29c9fc742
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
wenzelm
parents:
41516
diff
changeset
|
385 |
| eq_lst _ = true; |
38b29c9fc742
slightly more special eq_list/eq_set, with shortcut involving pointer_eq;
wenzelm
parents:
41516
diff
changeset
|
386 |
in length list1 = length list2 andalso eq_lst (list1, list2) end; |
20348 | 387 |
|
19483
55ee839198bd
renamed mapfilter to map_filter, made pervasive (again);
wenzelm
parents:
19474
diff
changeset
|
388 |
fun maps f [] = [] |
55ee839198bd
renamed mapfilter to map_filter, made pervasive (again);
wenzelm
parents:
19474
diff
changeset
|
389 |
| maps f (x :: xs) = f x @ maps f xs; |
55ee839198bd
renamed mapfilter to map_filter, made pervasive (again);
wenzelm
parents:
19474
diff
changeset
|
390 |
|
25538 | 391 |
val filter = List.filter; |
392 |
fun filter_out f = filter (not o f); |
|
393 |
val map_filter = List.mapPartial; |
|
394 |
||
33955 | 395 |
fun take (0: int) xs = [] |
396 |
| take _ [] = [] |
|
34059 | 397 |
| take n (x :: xs) = x :: take (n - 1) xs; |
33955 | 398 |
|
399 |
fun drop (0: int) xs = xs |
|
400 |
| drop _ [] = [] |
|
34059 | 401 |
| drop n (x :: xs) = drop (n - 1) xs; |
33955 | 402 |
|
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
403 |
fun chop (0: int) xs = ([], xs) |
23220 | 404 |
| chop _ [] = ([], []) |
405 |
| chop n (x :: xs) = chop (n - 1) xs |>> cons x; |
|
19011 | 406 |
|
46891 | 407 |
fun chop_groups n list = |
408 |
(case chop (Int.max (n, 1)) list of |
|
409 |
([], _) => [] |
|
410 |
| (g, rest) => g :: chop_groups n rest); |
|
411 |
||
412 |
||
233 | 413 |
(*return nth element of a list, where 0 designates the first element; |
18461 | 414 |
raise Subscript if list too short*) |
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17952
diff
changeset
|
415 |
fun nth xs i = List.nth (xs, i); |
233 | 416 |
|
43278 | 417 |
fun nth_list xss i = nth xss i handle General.Subscript => []; |
18461 | 418 |
|
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17952
diff
changeset
|
419 |
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
|
420 |
| nth_map n f (x :: xs) = x :: nth_map (n - 1) f xs |
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
421 |
| nth_map (_: int) _ [] = raise Subscript; |
11773 | 422 |
|
24846 | 423 |
fun nth_drop n xs = |
424 |
List.take (xs, n) @ List.drop (xs, n + 1); |
|
425 |
||
18514 | 426 |
fun map_index f = |
427 |
let |
|
52271
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
428 |
fun map_aux (_: int) [] = [] |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
429 |
| map_aux i (x :: xs) = f (i, x) :: map_aux (i + 1) xs |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
430 |
in map_aux 0 end; |
18514 | 431 |
|
21118 | 432 |
fun fold_index f = |
433 |
let |
|
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
434 |
fun fold_aux (_: int) [] y = y |
52271
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
435 |
| fold_aux i (x :: xs) y = fold_aux (i + 1) xs (f (i, x) y) |
21118 | 436 |
in fold_aux 0 end; |
437 |
||
33063 | 438 |
fun map_range f i = |
439 |
let |
|
52271
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
440 |
fun map_aux (k: int) = |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
441 |
if k < i then f k :: map_aux (k + 1) else [] |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
442 |
in map_aux 0 end; |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
443 |
|
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
444 |
fun fold_range f i = |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
445 |
let |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
446 |
fun fold_aux (k: int) y = |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
447 |
if k < i then fold_aux (k + 1) (f k y) else y |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
448 |
in fold_aux 0 end; |
6f3771c00280
combinator fold_range, corresponding to map_range
haftmann
parents:
51990
diff
changeset
|
449 |
|
33063 | 450 |
|
3762 | 451 |
(*rear decomposition*) |
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
47023
diff
changeset
|
452 |
fun split_last [] = raise List.Empty |
3762 | 453 |
| split_last [x] = ([], x) |
454 |
| split_last (x :: xs) = apfst (cons x) (split_last xs); |
|
455 |
||
46891 | 456 |
(*find first element satisfying predicate*) |
457 |
val find_first = List.find; |
|
458 |
||
29209 | 459 |
(*find position of first element satisfying a predicate*) |
4212 | 460 |
fun find_index pred = |
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
461 |
let fun find (_: int) [] = ~1 |
4212 | 462 |
| find n (x :: xs) = if pred x then n else find (n + 1) xs; |
463 |
in find 0 end; |
|
3762 | 464 |
|
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
465 |
(*get first element by lookup function*) |
15531 | 466 |
fun get_first _ [] = NONE |
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
467 |
| get_first f (x :: xs) = |
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
468 |
(case f x of |
15531 | 469 |
NONE => get_first f xs |
4916
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
470 |
| some => some); |
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
wenzelm
parents:
4893
diff
changeset
|
471 |
|
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19119
diff
changeset
|
472 |
fun get_index f = |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19119
diff
changeset
|
473 |
let |
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
474 |
fun get (_: int) [] = NONE |
19461 | 475 |
| get i (x :: xs) = |
46838 | 476 |
(case f x of |
477 |
NONE => get (i + 1) xs |
|
478 |
| SOME y => SOME (i, y)) |
|
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19119
diff
changeset
|
479 |
in get 0 end; |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19119
diff
changeset
|
480 |
|
15531 | 481 |
val flat = List.concat; |
233 | 482 |
|
12136 | 483 |
fun unflat (xs :: xss) ys = |
19424 | 484 |
let val (ps, qs) = chop (length xs) ys |
13629 | 485 |
in ps :: unflat xss qs end |
12136 | 486 |
| unflat [] [] = [] |
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
487 |
| unflat _ _ = raise ListPair.UnequalLengths; |
12136 | 488 |
|
46891 | 489 |
fun grouped n comb f = chop_groups n #> comb (map f) #> flat; |
490 |
||
21479
63e7eb863ae6
moved string_of_pair/list/option to structure ML_Syntax;
wenzelm
parents:
21395
diff
changeset
|
491 |
fun burrow f xss = unflat xss (f (flat xss)); |
18359 | 492 |
|
24864 | 493 |
fun burrow_options f os = map (try hd) (burrow f (map the_list os)); |
494 |
||
18549
5308a6ea3b96
rearranged burrow_split to fold_burrow to allow composition with fold_map
haftmann
parents:
18514
diff
changeset
|
495 |
fun fold_burrow f xss s = |
5308a6ea3b96
rearranged burrow_split to fold_burrow to allow composition with fold_map
haftmann
parents:
18514
diff
changeset
|
496 |
apfst (unflat xss) (f (flat xss) s); |
18359 | 497 |
|
233 | 498 |
(*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*) |
499 |
fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs |
|
500 |
| separate _ xs = xs; |
|
501 |
||
25980 | 502 |
fun surround s (x :: xs) = s :: x :: surround s xs |
503 |
| surround s [] = [s]; |
|
504 |
||
233 | 505 |
(*make the list [x, x, ..., x] of length n*) |
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
506 |
fun replicate (n: int) x = |
233 | 507 |
let fun rep (0, xs) = xs |
508 |
| rep (n, xs) = rep (n - 1, x :: xs) |
|
509 |
in |
|
15570 | 510 |
if n < 0 then raise Subscript |
233 | 511 |
else rep (n, []) |
512 |
end; |
|
513 |
||
4248
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
wenzelm
parents:
4224
diff
changeset
|
514 |
|
25549 | 515 |
(* direct product *) |
516 |
||
25538 | 517 |
fun map_product f _ [] = [] |
518 |
| map_product f [] _ = [] |
|
519 |
| map_product f (x :: xs) ys = map (f x) ys @ map_product f xs ys; |
|
233 | 520 |
|
25538 | 521 |
fun fold_product f _ [] z = z |
522 |
| fold_product f [] _ z = z |
|
523 |
| fold_product f (x :: xs) ys z = z |> fold (f x) ys |> fold_product f xs ys; |
|
233 | 524 |
|
25549 | 525 |
|
526 |
(* lists of pairs *) |
|
233 | 527 |
|
18330 | 528 |
fun map2 _ [] [] = [] |
529 |
| map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys |
|
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
530 |
| map2 _ _ _ = raise ListPair.UnequalLengths; |
380 | 531 |
|
58633 | 532 |
fun fold2 _ [] [] z = z |
23220 | 533 |
| fold2 f (x :: xs) (y :: ys) z = fold2 f xs ys (f x y z) |
58633 | 534 |
| fold2 _ _ _ _ = raise ListPair.UnequalLengths; |
380 | 535 |
|
58633 | 536 |
fun map_split _ [] = ([], []) |
25943 | 537 |
| map_split f (x :: xs) = |
538 |
let |
|
539 |
val (y, w) = f x; |
|
540 |
val (ys, ws) = map_split f xs; |
|
541 |
in (y :: ys, w :: ws) end; |
|
542 |
||
19799 | 543 |
fun zip_options (x :: xs) (SOME y :: ys) = (x, y) :: zip_options xs ys |
544 |
| zip_options (_ :: xs) (NONE :: ys) = zip_options xs ys |
|
545 |
| zip_options _ [] = [] |
|
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
546 |
| zip_options [] _ = raise ListPair.UnequalLengths; |
4956
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
wenzelm
parents:
4945
diff
changeset
|
547 |
|
233 | 548 |
(*combine two lists forming a list of pairs: |
549 |
[x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*) |
|
550 |
fun [] ~~ [] = [] |
|
551 |
| (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys) |
|
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
552 |
| _ ~~ _ = raise ListPair.UnequalLengths; |
233 | 553 |
|
554 |
(*inverse of ~~; the old 'split': |
|
555 |
[(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*) |
|
15570 | 556 |
val split_list = ListPair.unzip; |
233 | 557 |
|
28347 | 558 |
fun burrow_fst f xs = split_list xs |>> f |> op ~~; |
559 |
||
233 | 560 |
|
561 |
(* prefixes, suffixes *) |
|
562 |
||
18441 | 563 |
fun is_prefix _ [] _ = true |
564 |
| is_prefix eq (x :: xs) (y :: ys) = eq (x, y) andalso is_prefix eq xs ys |
|
565 |
| is_prefix eq _ _ = false; |
|
233 | 566 |
|
567 |
(* [x1, ..., xi, ..., xn] ---> ([x1, ..., x(i-1)], [xi, ..., xn]) |
|
568 |
where xi is the first element that does not satisfy the predicate*) |
|
569 |
fun take_prefix (pred : 'a -> bool) (xs: 'a list) : 'a list * 'a list = |
|
570 |
let fun take (rxs, []) = (rev rxs, []) |
|
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
571 |
| take (rxs, x :: xs) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
572 |
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) |
233 | 573 |
in take([], xs) end; |
574 |
||
20108 | 575 |
fun chop_prefix eq ([], ys) = ([], ([], ys)) |
576 |
| chop_prefix eq (xs, []) = ([], (xs, [])) |
|
23220 | 577 |
| chop_prefix eq (xs as x :: xs', ys as y :: ys') = |
20108 | 578 |
if eq (x, y) then |
579 |
let val (ps', xys'') = chop_prefix eq (xs', ys') |
|
23220 | 580 |
in (x :: ps', xys'') end |
20108 | 581 |
else ([], (xs, ys)); |
582 |
||
233 | 583 |
(* [x1, ..., xi, ..., xn] ---> ([x1, ..., xi], [x(i+1), ..., xn]) |
584 |
where xi is the last element that does not satisfy the predicate*) |
|
585 |
fun take_suffix _ [] = ([], []) |
|
586 |
| take_suffix pred (x :: xs) = |
|
587 |
(case take_suffix pred xs of |
|
588 |
([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) |
|
589 |
| (prfx, sffx) => (x :: prfx, sffx)); |
|
590 |
||
12249 | 591 |
fun prefixes1 [] = [] |
592 |
| prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs); |
|
593 |
||
19011 | 594 |
fun prefixes xs = [] :: prefixes1 xs; |
595 |
||
12249 | 596 |
fun suffixes1 xs = map rev (prefixes1 (rev xs)); |
19011 | 597 |
fun suffixes xs = [] :: suffixes1 xs; |
233 | 598 |
|
23220 | 599 |
|
600 |
||
233 | 601 |
(** integers **) |
602 |
||
603 |
(* lists of integers *) |
|
604 |
||
605 |
(*make the list [from, from + 1, ..., to]*) |
|
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
606 |
fun ((i: int) upto j) = |
21859 | 607 |
if i > j then [] else i :: (i + 1 upto j); |
233 | 608 |
|
609 |
(*make the list [from, from - 1, ..., to]*) |
|
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
610 |
fun ((i: int) downto j) = |
21859 | 611 |
if i < j then [] else i :: (i - 1 downto j); |
233 | 612 |
|
613 |
||
614 |
(* convert integers to strings *) |
|
615 |
||
616 |
(*expand the number in the given base; |
|
617 |
example: radixpand (2, 8) gives [1, 0, 0, 0]*) |
|
618 |
fun radixpand (base, num) : int list = |
|
619 |
let |
|
620 |
fun radix (n, tail) = |
|
621 |
if n < base then n :: tail |
|
622 |
else radix (n div base, (n mod base) :: tail) |
|
623 |
in radix (num, []) end; |
|
624 |
||
625 |
(*expands a number into a string of characters starting from "zerochar"; |
|
626 |
example: radixstring (2, "0", 8) gives "1000"*) |
|
627 |
fun radixstring (base, zerochar, num) = |
|
628 |
let val offset = ord zerochar; |
|
629 |
fun chrof n = chr (offset + n) |
|
630 |
in implode (map chrof (radixpand (base, num))) end; |
|
631 |
||
632 |
||
41492
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
633 |
local |
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
634 |
val zero = ord "0"; |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57884
diff
changeset
|
635 |
val small_int = 10000: int; |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57884
diff
changeset
|
636 |
val small_int_table = Vector.tabulate (small_int, Int.toString); |
41492
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
637 |
in |
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
638 |
|
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
639 |
fun string_of_int i = |
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
640 |
if i < 0 then Int.toString i |
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
641 |
else if i < 10 then chr (zero + i) |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57884
diff
changeset
|
642 |
else if i < small_int then Vector.sub (small_int_table, i) |
41492
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
643 |
else Int.toString i; |
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
644 |
|
7a4138cb46db
tuned string_of_int to avoid allocation for small integers;
wenzelm
parents:
41489
diff
changeset
|
645 |
end; |
233 | 646 |
|
21942
d6218d0f9ec3
added signed_string_of_int (pruduces proper - instead of SML's ~);
wenzelm
parents:
21920
diff
changeset
|
647 |
fun signed_string_of_int i = |
d6218d0f9ec3
added signed_string_of_int (pruduces proper - instead of SML's ~);
wenzelm
parents:
21920
diff
changeset
|
648 |
if i < 0 then "-" ^ string_of_int (~ i) else string_of_int i; |
d6218d0f9ec3
added signed_string_of_int (pruduces proper - instead of SML's ~);
wenzelm
parents:
21920
diff
changeset
|
649 |
|
23220 | 650 |
fun string_of_indexname (a, 0) = a |
651 |
| string_of_indexname (a, i) = a ^ "_" ^ string_of_int i; |
|
233 | 652 |
|
653 |
||
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
|
654 |
(* 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
|
655 |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24593
diff
changeset
|
656 |
fun read_radix_int radix cs = |
20095 | 657 |
let |
658 |
val zero = ord "0"; |
|
659 |
val limit = zero + radix; |
|
660 |
fun scan (num, []) = (num, []) |
|
661 |
| scan (num, c :: cs) = |
|
50637 | 662 |
if zero <= ord c andalso ord c < limit then |
663 |
scan (radix * num + (ord c - zero), cs) |
|
664 |
else (num, c :: cs); |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24593
diff
changeset
|
665 |
in scan (0, cs) end; |
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
|
666 |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24593
diff
changeset
|
667 |
val read_int = read_radix_int 10; |
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
|
668 |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40564
diff
changeset
|
669 |
fun oct_char s = chr (#1 (read_radix_int 8 (raw_explode s))); |
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
|
670 |
|
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
|
671 |
|
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
|
672 |
|
233 | 673 |
(** strings **) |
674 |
||
16188 | 675 |
(* functions tuned for strings, avoiding explode *) |
6312 | 676 |
|
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17952
diff
changeset
|
677 |
fun nth_string str i = |
6959 | 678 |
(case try String.substring (str, i, 1) of |
15531 | 679 |
SOME s => s |
15570 | 680 |
| NONE => raise Subscript); |
6312 | 681 |
|
16188 | 682 |
fun fold_string f str x0 = |
6282 | 683 |
let |
684 |
val n = size str; |
|
16188 | 685 |
fun iter (x, i) = |
686 |
if i < n then iter (f (String.substring (str, i, 1)) x, i + 1) else x; |
|
687 |
in iter (x0, 0) end; |
|
6282 | 688 |
|
14968 | 689 |
fun exists_string pred str = |
690 |
let |
|
691 |
val n = size str; |
|
692 |
fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1)); |
|
693 |
in ex 0 end; |
|
6312 | 694 |
|
16188 | 695 |
fun forall_string pred = not o exists_string (not o pred); |
696 |
||
28025 | 697 |
fun first_field sep str = |
28022 | 698 |
let |
28025 | 699 |
val n = size sep; |
28022 | 700 |
val len = size str; |
701 |
fun find i = |
|
702 |
if i + n > len then NONE |
|
28025 | 703 |
else if String.substring (str, i, n) = sep then SOME i |
28022 | 704 |
else find (i + 1); |
28025 | 705 |
in |
706 |
(case find 0 of |
|
707 |
NONE => NONE |
|
708 |
| SOME i => SOME (String.substring (str, 0, i), String.extract (str, i + n, NONE))) |
|
709 |
end; |
|
28022 | 710 |
|
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
711 |
(*enclose in brackets*) |
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
712 |
fun enclose lpar rpar str = lpar ^ str ^ rpar; |
6642 | 713 |
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
|
714 |
|
233 | 715 |
(*simple quoting (does not escape special chars)*) |
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
716 |
val quote = enclose "\"" "\""; |
233 | 717 |
|
55033 | 718 |
val cartouche = enclose "\\<open>" "\\<close>"; |
719 |
||
233 | 720 |
fun space_implode a bs = implode (separate a bs); |
721 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
722 |
val commas = space_implode ", "; |
380 | 723 |
val commas_quote = commas o map quote; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
724 |
|
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
725 |
val cat_lines = space_implode "\n"; |
233 | 726 |
|
4212 | 727 |
(*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
|
728 |
fun space_explode _ "" = [] |
21899 | 729 |
| space_explode sep s = String.fields (fn c => str c = sep) s; |
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
730 |
|
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
731 |
val split_lines = space_explode "\n"; |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
732 |
|
56038
0e2dec666152
tuned messages -- in accordance to Isabelle/Scala;
wenzelm
parents:
55033
diff
changeset
|
733 |
fun plain_words s = space_explode "_" s |> space_implode " "; |
0e2dec666152
tuned messages -- in accordance to Isabelle/Scala;
wenzelm
parents:
55033
diff
changeset
|
734 |
|
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
|
735 |
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
|
736 |
| 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
|
737 |
|
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
738 |
fun prefix prfx s = prfx ^ s; |
16188 | 739 |
fun suffix sffx s = s ^ sffx; |
5285 | 740 |
|
18681
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
741 |
fun unprefix prfx s = |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
742 |
if String.isPrefix prfx s then String.substring (s, size prfx, size s - size prfx) |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
743 |
else raise Fail "unprefix"; |
3020496cff28
added exception ERROR, error, cat_error, sys_error, assert, deny, assert_all;
wenzelm
parents:
18549
diff
changeset
|
744 |
|
16188 | 745 |
fun unsuffix sffx s = |
17061 | 746 |
if String.isSuffix sffx s then String.substring (s, 0, size s - size sffx) |
747 |
else raise Fail "unsuffix"; |
|
5285 | 748 |
|
47499
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents:
47060
diff
changeset
|
749 |
val trim_line = perhaps (try (unsuffix "\n")); |
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents:
47060
diff
changeset
|
750 |
|
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24148
diff
changeset
|
751 |
fun replicate_string (0: int) _ = "" |
10951 | 752 |
| replicate_string 1 a = a |
753 |
| replicate_string k a = |
|
754 |
if k mod 2 = 0 then replicate_string (k div 2) (a ^ a) |
|
755 |
else replicate_string (k div 2) (a ^ a) ^ a; |
|
756 |
||
31250 | 757 |
fun translate_string f = String.translate (f o String.str); |
758 |
||
29882
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
759 |
(*crude matching of str against simple glob pat*) |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
760 |
fun match_string pat str = |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
761 |
let |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
762 |
fun match [] _ = true |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
763 |
| match (p :: ps) s = |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
764 |
size p <= size s andalso |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
765 |
(case try (unprefix p) s of |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
766 |
SOME s' => match ps s' |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
767 |
| NONE => match (p :: ps) (String.substring (s, 1, size s - 1))); |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29209
diff
changeset
|
768 |
in match (space_explode "*" pat) str end; |
23220 | 769 |
|
35976 | 770 |
|
41516
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
771 |
(** reals **) |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
772 |
|
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
773 |
val string_of_real = Real.fmt (StringCvt.GEN NONE); |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
774 |
|
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
775 |
fun signed_string_of_real x = |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
776 |
if x < 0.0 then "-" ^ string_of_real (~ x) else string_of_real x; |
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
777 |
|
3a70387b5e01
smart_string_of_real: print integer values without fractional part;
wenzelm
parents:
41494
diff
changeset
|
778 |
|
35976 | 779 |
|
16492 | 780 |
(** lists as sets -- see also Pure/General/ord_list.ML **) |
233 | 781 |
|
26439 | 782 |
(* canonical operations *) |
783 |
||
18923 | 784 |
fun member eq list x = |
785 |
let |
|
786 |
fun memb [] = false |
|
787 |
| memb (y :: ys) = eq (x, y) orelse memb ys; |
|
788 |
in memb list end; |
|
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
789 |
|
18923 | 790 |
fun insert eq x xs = if member eq xs x then xs else x :: xs; |
791 |
fun remove eq x xs = if member eq xs x then filter_out (fn y => eq (x, y)) xs else xs; |
|
24049 | 792 |
fun update eq x xs = cons x (remove eq x xs); |
233 | 793 |
|
33049
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
haftmann
parents:
33040
diff
changeset
|
794 |
fun inter eq xs = filter (member eq xs); |
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
haftmann
parents:
33040
diff
changeset
|
795 |
|
33042 | 796 |
fun union eq = fold (insert eq); |
19301 | 797 |
fun subtract eq = fold (remove eq); |
798 |
||
30572
8fbc355100f2
Library.merge/OrdList.union: optimize the important special case where the tables coincide -- NOTE: this changes both the operational behaviour and the result for non-standard eq/ord notion;
wenzelm
parents:
30570
diff
changeset
|
799 |
fun merge eq (xs, ys) = |
8fbc355100f2
Library.merge/OrdList.union: optimize the important special case where the tables coincide -- NOTE: this changes both the operational behaviour and the result for non-standard eq/ord notion;
wenzelm
parents:
30570
diff
changeset
|
800 |
if pointer_eq (xs, ys) then xs |
8fbc355100f2
Library.merge/OrdList.union: optimize the important special case where the tables coincide -- NOTE: this changes both the operational behaviour and the result for non-standard eq/ord notion;
wenzelm
parents:
30570
diff
changeset
|
801 |
else if null xs then ys |
8fbc355100f2
Library.merge/OrdList.union: optimize the important special case where the tables coincide -- NOTE: this changes both the operational behaviour and the result for non-standard eq/ord notion;
wenzelm
parents:
30570
diff
changeset
|
802 |
else fold_rev (insert eq) ys xs; |
0 | 803 |
|
26439 | 804 |
|
33050 | 805 |
(* subset and set equality *) |
806 |
||
33038 | 807 |
fun subset eq (xs, ys) = forall (member eq ys) xs; |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
808 |
|
33038 | 809 |
fun eq_set eq (xs, ys) = |
20348 | 810 |
eq_list eq (xs, ys) orelse |
33038 | 811 |
(subset eq (xs, ys) andalso subset (eq o swap) (ys, xs)); |
19301 | 812 |
|
265 | 813 |
|
233 | 814 |
(*makes a list of the distinct members of the input; preserves order, takes |
815 |
first of equal elements*) |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19011
diff
changeset
|
816 |
fun distinct eq lst = |
233 | 817 |
let |
818 |
fun dist (rev_seen, []) = rev rev_seen |
|
819 |
| dist (rev_seen, x :: xs) = |
|
18923 | 820 |
if member eq rev_seen x then dist (rev_seen, xs) |
233 | 821 |
else dist (x :: rev_seen, xs); |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19011
diff
changeset
|
822 |
in dist ([], lst) end; |
233 | 823 |
|
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
824 |
(*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
|
825 |
order, takes first of equal elements*) |
18966 | 826 |
fun duplicates eq lst = |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
827 |
let |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
828 |
fun dups (rev_dups, []) = rev rev_dups |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
829 |
| dups (rev_dups, x :: xs) = |
18923 | 830 |
if member eq rev_dups x orelse not (member eq xs x) then |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
831 |
dups (rev_dups, xs) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
832 |
else dups (x :: rev_dups, xs); |
18966 | 833 |
in dups ([], lst) end; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
834 |
|
16878 | 835 |
fun has_duplicates eq = |
836 |
let |
|
837 |
fun dups [] = false |
|
838 |
| dups (x :: xs) = member eq xs x orelse dups xs; |
|
839 |
in dups end; |
|
840 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
841 |
|
32352 | 842 |
(* matrices *) |
843 |
||
844 |
fun map_transpose f xss = |
|
845 |
let |
|
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
846 |
val n = |
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
847 |
(case distinct (op =) (map length xss) of |
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
848 |
[] => 0 |
32352 | 849 |
| [n] => n |
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
850 |
| _ => raise ListPair.UnequalLengths); |
33206 | 851 |
in map_range (fn m => f (map (fn xs => nth xs m) xss)) n end; |
32352 | 852 |
|
853 |
||
23220 | 854 |
|
22142 | 855 |
(** lists as multisets **) |
856 |
||
33078
3aea60ca3900
multiset operations with canonical argument order
haftmann
parents:
33063
diff
changeset
|
857 |
fun remove1 eq x [] = [] |
3aea60ca3900
multiset operations with canonical argument order
haftmann
parents:
33063
diff
changeset
|
858 |
| remove1 eq x (y :: ys) = if eq (x, y) then ys else y :: remove1 eq x ys; |
22142 | 859 |
|
33078
3aea60ca3900
multiset operations with canonical argument order
haftmann
parents:
33063
diff
changeset
|
860 |
fun combine eq xs ys = fold (remove1 eq) ys xs @ ys; |
233 | 861 |
|
33079 | 862 |
fun submultiset _ ([], _) = true |
863 |
| submultiset eq (x :: xs, ys) = member eq ys x andalso submultiset eq (xs, remove1 eq x ys); |
|
864 |
||
0 | 865 |
|
866 |
||
2506 | 867 |
(** orders **) |
868 |
||
18966 | 869 |
fun is_equal EQUAL = true |
870 |
| is_equal _ = false; |
|
871 |
||
4445 | 872 |
fun rev_order LESS = GREATER |
873 |
| rev_order EQUAL = EQUAL |
|
874 |
| rev_order GREATER = LESS; |
|
875 |
||
4479 | 876 |
(*assume rel is a linear strict order*) |
4445 | 877 |
fun make_ord rel (x, y) = |
878 |
if rel (x, y) then LESS |
|
879 |
else if rel (y, x) then GREATER |
|
880 |
else EQUAL; |
|
881 |
||
25224 | 882 |
fun bool_ord (false, true) = LESS |
883 |
| bool_ord (true, false) = GREATER |
|
884 |
| bool_ord _ = EQUAL; |
|
885 |
||
15051
0dbbab9f77fe
int_ord = Int.compare, string_ord = String.compare;
wenzelm
parents:
15035
diff
changeset
|
886 |
val int_ord = Int.compare; |
0dbbab9f77fe
int_ord = Int.compare, string_ord = String.compare;
wenzelm
parents:
15035
diff
changeset
|
887 |
val string_ord = String.compare; |
2506 | 888 |
|
16676 | 889 |
fun fast_string_ord (s1, s2) = |
43793 | 890 |
if pointer_eq (s1, s2) then EQUAL |
891 |
else (case int_ord (size s1, size s2) of EQUAL => string_ord (s1, s2) | ord => ord); |
|
16676 | 892 |
|
16492 | 893 |
fun option_ord ord (SOME x, SOME y) = ord (x, y) |
894 |
| option_ord _ (NONE, NONE) = EQUAL |
|
895 |
| option_ord _ (NONE, SOME _) = LESS |
|
896 |
| option_ord _ (SOME _, NONE) = GREATER; |
|
897 |
||
4343 | 898 |
(*lexicographic product*) |
899 |
fun prod_ord a_ord b_ord ((x, y), (x', y')) = |
|
900 |
(case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord); |
|
901 |
||
902 |
(*dictionary order -- in general NOT well-founded!*) |
|
16984 | 903 |
fun dict_ord elem_ord (x :: xs, y :: ys) = |
904 |
(case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord) |
|
905 |
| dict_ord _ ([], []) = EQUAL |
|
4343 | 906 |
| dict_ord _ ([], _ :: _) = LESS |
16984 | 907 |
| dict_ord _ (_ :: _, []) = GREATER; |
4343 | 908 |
|
909 |
(*lexicographic product of lists*) |
|
910 |
fun list_ord elem_ord (xs, ys) = |
|
16676 | 911 |
(case int_ord (length xs, length ys) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord); |
4343 | 912 |
|
2506 | 913 |
|
4621 | 914 |
(* sorting *) |
915 |
||
48271
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
916 |
(*stable mergesort -- preserves order of equal elements*) |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
917 |
fun mergesort unique ord = |
4621 | 918 |
let |
48271
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
919 |
fun merge (xs as x :: xs') (ys as y :: ys') = |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
920 |
(case ord (x, y) of |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
921 |
LESS => x :: merge xs' ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
922 |
| EQUAL => |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
923 |
if unique then merge xs ys' |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
924 |
else x :: merge xs' ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
925 |
| GREATER => y :: merge xs ys') |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
926 |
| merge [] ys = ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
927 |
| merge xs [] = xs; |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
928 |
|
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
929 |
fun merge_all [xs] = xs |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
930 |
| merge_all xss = merge_all (merge_pairs xss) |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
931 |
and merge_pairs (xs :: ys :: xss) = merge xs ys :: merge_pairs xss |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
932 |
| merge_pairs xss = xss; |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
933 |
|
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
934 |
fun runs (x :: y :: xs) = |
18427 | 935 |
(case ord (x, y) of |
48271
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
936 |
LESS => ascending y [x] xs |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
937 |
| EQUAL => |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
938 |
if unique then runs (x :: xs) |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
939 |
else ascending y [x] xs |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
940 |
| GREATER => descending y [x] xs) |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
941 |
| runs xs = [xs] |
4621 | 942 |
|
48271
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
943 |
and ascending x xs (zs as y :: ys) = |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
944 |
(case ord (x, y) of |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
945 |
LESS => ascending y (x :: xs) ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
946 |
| EQUAL => |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
947 |
if unique then ascending x xs ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
948 |
else ascending y (x :: xs) ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
949 |
| GREATER => rev (x :: xs) :: runs zs) |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
950 |
| ascending x xs [] = [rev (x :: xs)] |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
951 |
|
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
952 |
and descending x xs (zs as y :: ys) = |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
953 |
(case ord (x, y) of |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
954 |
GREATER => descending y (x :: xs) ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
955 |
| EQUAL => |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
956 |
if unique then descending x xs ys |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
957 |
else (x :: xs) :: runs zs |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
958 |
| LESS => (x :: xs) :: runs zs) |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
959 |
| descending x xs [] = [x :: xs]; |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
960 |
|
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
961 |
in merge_all o runs end; |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
962 |
|
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
963 |
fun sort ord = mergesort false ord; |
b28defd0b5a5
replaced quicksort by mergesort, which might be a bit more efficient for key operations like Ord_List.make, Sorts.minimize_sort;
wenzelm
parents:
47499
diff
changeset
|
964 |
fun sort_distinct ord = mergesort true ord; |
18427 | 965 |
|
4621 | 966 |
val sort_strings = sort string_ord; |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58635
diff
changeset
|
967 |
fun sort_wrt key xs = sort (string_ord o apply2 key) xs; |
4621 | 968 |
|
969 |
||
30558
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
970 |
(* items tagged by integer index *) |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
971 |
|
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
972 |
(*insert tags*) |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
973 |
fun tag_list k [] = [] |
30570 | 974 |
| tag_list k (x :: xs) = (k:int, x) :: tag_list (k + 1) xs; |
30558
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
975 |
|
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
976 |
(*remove tags and suppress duplicates -- list is assumed sorted!*) |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
977 |
fun untag_list [] = [] |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
978 |
| untag_list [(k: int, x)] = [x] |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
979 |
| untag_list ((k, x) :: (rest as (k', x') :: _)) = |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
980 |
if k = k' then untag_list rest |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
981 |
else x :: untag_list rest; |
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
982 |
|
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
983 |
(*return list elements in original order*) |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58635
diff
changeset
|
984 |
fun order_list list = untag_list (sort (int_ord o apply2 fst) list); |
30558
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
985 |
|
2ef9892114fd
renamed Tactic.taglist/untaglist/orderlist to tag_list/untag_list/order_list (in library.ML);
wenzelm
parents:
30190
diff
changeset
|
986 |
|
2506 | 987 |
|
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
988 |
(** random numbers **) |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
989 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
990 |
exception RANDOM; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
991 |
|
14618
068bb99f3ebd
Fixed bug in rmod that caused an overflow exception in SML/NJ.
berghofe
parents:
14472
diff
changeset
|
992 |
fun rmod x y = x - y * Real.realFloor (x / y); |
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
993 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
994 |
local |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
995 |
val a = 16807.0; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
996 |
val m = 2147483647.0; |
32737
76fa673eee8b
Raw ML references as unsynchronized state variables.
wenzelm
parents:
32352
diff
changeset
|
997 |
val random_seed = Unsynchronized.ref 1.0; |
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
998 |
in |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
999 |
|
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23860
diff
changeset
|
1000 |
fun random () = CRITICAL (fn () => |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23860
diff
changeset
|
1001 |
let val r = rmod (a * ! random_seed) m |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23860
diff
changeset
|
1002 |
in (random_seed := r; r) end); |
14106
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1003 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1004 |
end; |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1005 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1006 |
fun random_range l h = |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1007 |
if h < l orelse l < 0 then raise RANDOM |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1008 |
else l + Real.floor (rmod (random ()) (real (h - l + 1))); |
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1009 |
|
bbf708a7e27f
Added several functions for producing random numbers.
berghofe
parents:
13794
diff
changeset
|
1010 |
|
23220 | 1011 |
|
4621 | 1012 |
(** misc **) |
233 | 1013 |
|
19644
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
1014 |
fun divide_and_conquer decomp x = |
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
1015 |
let val (ys, recomb) = decomp x |
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
1016 |
in recomb (map (divide_and_conquer decomp) ys) end; |
0b01436e1843
added divide_and_conquer combinator (by Amine Chaieb);
wenzelm
parents:
19596
diff
changeset
|
1017 |
|
32978 | 1018 |
fun divide_and_conquer' decomp x s = |
1019 |
let val ((ys, recomb), s') = decomp x s |
|
1020 |
in recomb (fold_map (divide_and_conquer' decomp) ys s') end; |
|
1021 |
||
0 | 1022 |
|
233 | 1023 |
(*Partition a list into buckets [ bi, b(i+1), ..., bj ] |
0 | 1024 |
putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) |
1025 |
fun partition_list p i j = |
|
37851 | 1026 |
let |
1027 |
fun part (k: int) xs = |
|
1028 |
if k > j then |
|
1029 |
(case xs of |
|
1030 |
[] => [] |
|
1031 |
| _ => raise Fail "partition_list") |
|
1032 |
else |
|
1033 |
let val (ns, rest) = List.partition (p k) xs |
|
1034 |
in ns :: part (k + 1) rest end; |
|
1035 |
in part (i: int) end; |
|
0 | 1036 |
|
37851 | 1037 |
fun partition_eq (eq: 'a * 'a -> bool) = |
19691 | 1038 |
let |
1039 |
fun part [] = [] |
|
1040 |
| part (x :: ys) = |
|
1041 |
let val (xs, xs') = List.partition (fn y => eq (x, y)) ys |
|
37851 | 1042 |
in (x :: xs) :: part xs' end; |
19691 | 1043 |
in part end; |
1044 |
||
1045 |
||
45626
b4f374a45668
more abstract datatype stamp, which avoids pointless allocation of mutable cells;
wenzelm
parents:
44617
diff
changeset
|
1046 |
(* serial numbers and abstract stamps *) |
16439 | 1047 |
|
1048 |
type serial = int; |
|
25702
a61554b1e7a9
serial: now based on specific version in structure Multithreading;
wenzelm
parents:
25681
diff
changeset
|
1049 |
val serial = Multithreading.serial; |
19512 | 1050 |
val serial_string = string_of_int o serial; |
1051 |
||
45626
b4f374a45668
more abstract datatype stamp, which avoids pointless allocation of mutable cells;
wenzelm
parents:
44617
diff
changeset
|
1052 |
datatype stamp = Stamp of serial; |
b4f374a45668
more abstract datatype stamp, which avoids pointless allocation of mutable cells;
wenzelm
parents:
44617
diff
changeset
|
1053 |
fun stamp () = Stamp (serial ()); |
b4f374a45668
more abstract datatype stamp, which avoids pointless allocation of mutable cells;
wenzelm
parents:
44617
diff
changeset
|
1054 |
|
16535
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset
|
1055 |
|
51368
2ea5c7c2d825
tuned signature -- prefer terminology of Scala and Axiom;
wenzelm
parents:
50913
diff
changeset
|
1056 |
(* values of any type *) |
16535
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset
|
1057 |
|
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset
|
1058 |
(*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
|
1059 |
constructors at any time*) |
51368
2ea5c7c2d825
tuned signature -- prefer terminology of Scala and Axiom;
wenzelm
parents:
50913
diff
changeset
|
1060 |
structure Any = struct type T = exn end; |
16535
86c9eada525b
added structure Object (from Pure/General/object.ML);
wenzelm
parents:
16492
diff
changeset
|
1061 |
|
43603 | 1062 |
|
1063 |
(* current directory *) |
|
1064 |
||
1065 |
val cd = OS.FileSys.chDir; |
|
1066 |
val pwd = OS.FileSys.getDir; |
|
1067 |
||
1068 |
||
1069 |
(* getenv *) |
|
1070 |
||
1071 |
fun getenv x = |
|
1072 |
(case OS.Process.getEnv x of |
|
1073 |
NONE => "" |
|
1074 |
| SOME y => y); |
|
1075 |
||
1076 |
fun getenv_strict x = |
|
1077 |
(case getenv x of |
|
1078 |
"" => error ("Undefined Isabelle environment variable: " ^ quote x) |
|
1079 |
| y => y); |
|
1080 |
||
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1081 |
end; |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1082 |
|
32188 | 1083 |
structure Basic_Library: BASIC_LIBRARY = Library; |
1084 |
open Basic_Library; |
|
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40721
diff
changeset
|
1085 |