1 (* library extending Isabelle's library.
2 Author: Walther Neuper 1999
3 (c) copyright due to lincense terms
5 Note: Here many functions reflect changes since Isabelle 98. Changes frequently were resolved
6 quick and dirty by additing Isabelle's old version to the library below.
8 * remove unused functions
9 * review duplicates with other signatures and re-locate respectively
10 * move unparsing ("*_t0_str", "*2str", etc) somewhere else in ThydataC/
11 * apply Isabelle's coding standards and remove warnings
12 * rename "library.sml" to "libraryC.sml" like many other files, where struct <> filename.
19 val and_: bool * bool -> bool
20 val cand_: bool -> bool -> bool
21 val or_: bool * bool -> bool
22 val cor_: bool -> bool -> bool
24 val assoc: (''a * 'b) list * ''a -> 'b option
25 val assoc_string: (string * 'a) list * string -> 'a option
26 val bool2str: bool -> string
27 val commas: string list -> string
28 val dashs: int -> string
29 val de_quote: string -> string
30 val dots: int -> string
31 val drop: int * 'a list -> 'a list
32 val drop_last: 'a list -> 'a list
33 val drop_last_n: int -> 'a list -> 'a list
34 val drop_nth: 'a list -> int * 'a list -> 'a list
35 val dropuntil: ('a -> bool) -> 'a list -> 'a list
36 val dropwhile: ('a -> bool) -> 'a list -> 'a list
37 val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
38 val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
39 val fst3: 'a * 'b * 'c -> 'a
40 val gen_mem: ('a * 'b -> bool) -> 'a * 'b list -> bool
41 val gen_rems: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list
42 val if_none: 'a option -> 'a -> 'a
43 val indent: int -> string
44 val indt: int -> string
45 val idt: string -> int -> string
46 val int2str: int -> string
47 val ints2str': int list -> string
48 val intsto: int -> int list
49 val last_elem: 'a list -> 'a
50 val list2str: string list -> string
51 val list_update: 'a list -> int -> 'a -> 'a list
52 val maxl: int list -> int
53 val member_swap: ('a * 'b -> bool) -> 'a -> 'b list -> bool
54 val nos: string list -> string
55 val nth: int -> 'a list -> 'a
56 val overwrite: (''a * 'b) list * (''a * 'b) -> (''a * 'b) list
57 val overwritel: (''a * 'b) list * (''a * 'b) list -> (''a * 'b) list
59 val pair2str: string * string -> string
60 val pair2str_: string * string -> string
61 val pair2tri: ('a * 'b) * 'c -> 'a * 'b * 'c
62 val snd3: 'a * 'b * 'c -> 'b
63 val spair2str: string * string -> string
65 val split_nlast: int * 'a list -> 'a list * 'a list
66 val string_to_bool: string -> bool
67 val strs2str: string list -> string
68 val strs2str': string list -> string
69 val strs2str_: string list -> string (* duplicates in Rule *)
70 val strslist2strs: string list list -> string
71 type subst = (term * term) list
73 val take: int * 'a list -> 'a list (*conflict with Library.take: int -> 'a list -> 'a list *)
74 val take_fromto: int -> int -> 'a list -> 'a list
75 val takelast: int * 'a list -> 'a list
76 val takerest: int * 'a list -> 'a list
77 val takewhile: 'a list -> ('a -> bool) -> 'a list -> 'a list
78 val termless: term * term -> bool
79 val thd3: 'a * 'b * 'c -> 'c
80 val triple2pair: 'a * 'b * 'c -> 'a * 'b
81 val ~~~ : 'a list * 'b list -> ('a * 'b) list
83 val linefeed: string -> string
84 val dropwhile' : ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b list
86 val enumerate_strings: string list -> string list
87 val quad2pair: 'a * 'b * 'c * 'd -> 'a * 'b
90 (*///------------------------------>>> thy ------------------------------------------------\\\*)
91 val get_thy: string -> string
92 val strip_thy: string -> string
93 (*\\\------------------------------>>> thy ------------------------------------------------///*)
94 (*///------------------------------>>> term -----------------------------------------------\\\*)
95 val subs2str: string list -> string
96 val id_of: term -> string
97 val ids_of: term -> string list
98 (*\\\------------------------------>>> term -----------------------------------------------///*)
102 structure LibraryC(**): LIBRARY_C(**) =
106 val foldl = Library.foldl
107 val foldr = Library.foldr
108 fun take (n, xs) = Library.take n xs;;
109 fun drop (n, xs) = Library.drop n xs;
111 fun last_elem [] = raise ERROR "last_elem"
113 | last_elem (_ :: xs) = last_elem xs;
114 fun member_swap eq x xs = member eq xs x
116 fun gen_mem _ (_, []) = false
117 | gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys);
118 fun gen_rems eq (xs, ys) = filter_out (fn x => gen_mem eq (x, ys)) xs;
120 (*fun nth n xs = Library.nth xs n; exn behaviour different*)
121 fun nth _ [] = raise ERROR "nth _ []" (*Isabelle2002, still saved the hours of update*)
123 | nth n (_ :: xs) = nth (n - 1) xs;
125 fun list_update [] _ _ = []
126 | list_update (x :: xs) i v =
129 | j => x :: list_update xs (j - 1) v
131 fun drop_nth ls (_, []) = ls
132 | drop_nth ls (n, x :: xs) =
135 else drop_nth (ls @ [x]) (n - 1, xs);
137 fun and_ (b1, b2) = b1 andalso b2;
138 fun cand_ b1 b2 = b1 andalso b2;
139 fun or_ (b1, b2) = b1 orelse b2;
140 fun cor_ b1 b2 = b1 orelse b2;
142 fun takerest (i, ls) = (rev o take) (length ls - i, rev ls);
143 fun takelast (i, ls) = (rev o take) (i, rev ls);
144 fun split_nlast (i, ls) = (take (length ls - i, ls), rev (take (i, rev ls)));
145 fun dropwhile _ [] = []
146 | dropwhile P (ys as x :: xs) = if P x then dropwhile P xs else ys;
147 fun takewhile col _ [] = col
148 | takewhile col P (x::xs) =
149 if P x then takewhile (col @ [x]) P xs else col;
150 fun dropuntil _ [] = []
151 | dropuntil P (ys as x :: xs) = if P x then ys else dropuntil P xs;
152 fun drop_last l = ((rev o tl o rev) l);
153 fun drop_last_n n l = rev (takerest (n, rev l));
155 fun pair2tri ((a,b),c) = (a,b,c);
156 fun fst3 (a,_,_) = a;
157 fun snd3 (_,b,_) = b;
158 fun thd3 (_,_,c) = c;
161 let fun scan ss' [] = ss'
162 | scan ss' ("\"" :: ss) = scan ss' ss
163 | scan ss' (s :: ss) = scan (ss' @ [s]) ss;
164 in (implode o (scan []) o Symbol.explode) str end;
165 val commas = Library.space_implode ", ";
167 fun strs2str strl = "[" ^ (commas (map quote strl)) ^ "]";
168 fun strs2str' strl = "[" ^ commas strl ^ "]";
169 fun list2str strl = "[" ^ commas strl ^ "]";
170 val nos = space_implode "#";
171 fun strs2str_ strl = "#" ^ (nos strl) ^ "#";
172 fun strslist2strs strslist = map strs2str strslist |> strs2str';
173 type subst = (term * term) list;
175 fun spair2str (s1, s2) = "(" ^ quote s1 ^ ", " ^ quote s2 ^ ")";
176 fun pair2str_ (s1, s2) = s1 ^ "#" ^ s2;
177 fun pair2str (s1, s2) = "(" ^ s1 ^ ", " ^ s2 ^ ")";
179 val int2str = Library.string_of_int;
180 fun ints2str' ints = (strs2str' o (map string_of_int)) ints;
182 fun overwrite (al, p as (key, _)) =
183 let fun over ((q as (keyi, _)) :: pairs) =
184 if keyi = key then p :: pairs else q :: (over pairs)
187 fun overwritel (al, []) = al
188 | overwritel (al, b::bl) = overwritel (overwrite (al, b), bl);
190 (* TODO replace by Library.upto *)
193 | intsto1 n = (intsto1 (n - 1)) @ [n]
195 fun intsto n = if n < 0 then (raise ERROR "intsto < 0") else intsto1 n
198 fun bool2str true = "true"
199 | bool2str false = "false";
200 fun string_to_bool "true" = true
201 | string_to_bool "false" = false
202 | string_to_bool str = raise ERROR ("string_to_bool: arg = " ^ str)
204 (* take elements from b to e including both *)
205 fun take_fromto from to l =
207 then raise ERROR ("take_fromto from=" ^ string_of_int from ^ " > to=" ^ string_of_int to)
208 else drop (from - 1, take (to, l));
211 | idt str n = idt str (n - 1) ^ str;
212 fun indt n = if n <= 0 then "" else " " ^ indt (n-1);
213 fun indent i = fold (curry op ^) (replicate i ". ") ""
215 fun dashs i = if 0 < i then "-" ^ dashs (i - 1) else "";
216 fun dots i = if 0 < i then "." ^ dots (i - 1) else "";
218 fun assoc ([], _) = NONE(*cp 2002 Pure/library.ML FIXXXME take AList.lookup*)
219 | assoc ((keyi, xi) :: pairs, key) =
220 if key = keyi then SOME xi else assoc (pairs, key);
221 (* optimized version for strings *)
222 fun assoc_string ([], (_ : string)) = NONE
223 | assoc_string ((keyi, xi) :: pairs, key) =
224 if key = keyi then SOME xi else assoc_string (pairs, key);
225 fun if_none NONE y = y (*cp from 2002 Pure/library.ML FIXXXME replace*)
226 | if_none (SOME x) _ = x;
228 fun triple2pair (a, b, _) = (a, b);
230 fun quad2pair (a, b, _, _) = (a, b);
233 (* append a counter to a string list *)
235 fun enumerate_strings strs =
236 let fun enum _ [] = []
237 | enum i (s :: ss) = (s ^ "--" ^ string_of_int i) :: (enum (i + 1) ss)
241 fun maxl [] = raise ERROR "maxl of []"
245 | mx x (y :: ys) = if x < (y: int) then mx y ys else mx x ys
249 let fun aaa xys [] [] = xys
250 | aaa xys [] (_ :: _) = xys
251 | aaa xys (_ :: _) [] = xys
252 | aaa xys (x :: xs) (y :: ys) = aaa (xys @ [(x, y)]) xs ys
255 (*///------------------------------>>> thy ------------------------------------------------\\\*)
258 fun get strl [] = strl
259 | get strl ("." :: _) = strl
260 | get strl ( s :: ss) = get (strl @ [s]) ss
261 in implode (get [] (Symbol.explode str)) end;
265 let fun strip bdVar [] = implode (rev bdVar)
266 | strip bdVar ("." :: _) = implode (rev bdVar)
267 | strip bdVar (c :: cs) = strip (bdVar @ [c]) cs
268 in strip [] (rev (Symbol.explode str)) end;
269 (*\\\------------------------------>>> thy ------------------------------------------------///*)
271 (*///------------------------------>>> term ----------------------------------------------\\\*)
272 fun id_of (Var ((id,ix),_)) = if ix=0 then id else id^(string_of_int ix)
273 | id_of (Free (id ,_)) = id
274 | id_of (Const(id ,_)) = id
275 | id_of _ = ""; (* never such an identifier *)
278 let fun con ss (Const (s,_)) = s::ss
279 | con ss (Free (s,_)) = s::ss
280 | con ss (Abs (s,_,b)) = s::(con ss b)
281 | con ss (t1 $ t2) = (con ss t1) @ (con ss t2)
283 in map strip_thy (((distinct op =) o (con [])) t) end;
285 fun subs2str (subs: string list) = list2str subs;
286 (*> val sss = ["(''bdv'',x)", "(err,#0)"];
288 val it = "[(bdv,x),(err,#0)]" : string*)
289 (*\\\------------------------------>>> term ----------------------------------------------///*)
291 fun termless tu = (Term_Ord.term_ord tu = LESS);
293 val linefeed = curry op ^ "\n"; (* ?\<longrightarrow> libraryC ?*)
295 (*the lists contain eq-al elem-pairs at the beginning;
296 return first list reverted (again) - ie. in order as required subsequently*)
297 fun dropwhile' equal (f1::f2::fs) (i1::i2::is) =
299 if equal f2 i2 then dropwhile' equal (f2 :: fs) (i2 :: is)
300 else (rev (f1 :: f2 :: fs), i1 :: i2 :: is)
301 else raise ERROR "dropwhile': did not start with equal elements"
302 | dropwhile' equal (f::fs) [i] =
305 else raise ERROR "dropwhile': did not start with equal elements"
306 | dropwhile' equal [f] (i::is) =
309 else raise ERROR "dropwhile': did not start with equal elements"
310 | dropwhile' _ _ _ = raise ERROR "dropwhile': uncovered case"
312 end; (*struct*) open LibraryC