3 for both, math-engine and isa-98-1-HOL-plus
4 however, functions closely related to original isabelle-98-1 functions
5 are in isa-98-1-HOL-plus/rewrite-parse/library_G
8 (* Isabelle2002 -> Isabelle2009 library changed:
12 val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
13 val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
14 val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
15 val take: int * 'a list -> 'a list
16 val drop: int * 'a list -> 'a list
17 val last_elem: 'a list -> 'a
19 FIXME: overwritten again...*)
20 fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a = (*FIXME.2009*)
21 let fun itl (e, []) = e
22 | itl (e, a::l) = itl (f(e, a), l)
24 fun foldr f (l, e) = (*FIXME.2009*)
26 | itr (a::l) = f(a, itr l)
28 fun take (n, []) = [] (*FIXME.2009*)
30 if n > 0 then x :: take (n - 1, xs) else [];
31 fun drop (n, []) = [] (*FIXME.2009*)
33 if n > 0 then drop (n - 1, xs) else x :: xs;
35 (*exn LIST has disappeared in 2009 ... replaced by error...*)
36 fun last_elem [] = error "last_elem" (*FIXME.2009*)
38 | last_elem (_ :: xs) = last_elem xs;
40 fun gen_mem eq (x, []) = false (*FIXME.2009*)
41 | gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys);
42 fun gen_insert (les : 'a * 'a -> bool) ([], a) = [a]
43 | gen_insert les (x::xs, a) = if les (x, a) then x::(gen_insert les (xs, a))
45 fun gen_sort les xs = foldl (gen_insert les) (xs, []);
46 fun gen_distinct eq lst =
48 val memb = gen_mem eq;
50 fun dist (rev_seen, []) = rev rev_seen
51 | dist (rev_seen, x :: xs) =
52 if memb (x, rev_seen) then dist (rev_seen, xs)
53 else dist (x :: rev_seen, xs);
57 fun gen_rems eq (xs, ys) = filter_out (fn x => gen_mem eq (x, ys)) xs;
58 fun distinct l = gen_distinct (op =) l;
62 (*.see 'fun (y :: ys) \ x' in Pure/library.ML.*)
63 fun gen_dif eq (y :: ys, x) = if eq (y, x) then ys
64 else y :: (gen_dif eq (ys, x))
65 | gen_dif eq ([], x) = [];
66 (* val (eq, (y :: ys, x)) = ();*)
68 (*.see 'fun ys \\ xs' in Pure/library.ML.*)
69 fun gen_diff eq (ys, xs) = foldl (gen_dif eq) (ys,xs);
70 (* val (eq, (ys, xs)) = (eq_thmI, (isacrlsthms, isacthms));
72 (* gen_diff op= ([1,2,3,4,5,6,7],[2,3,5]);
73 val it = [1, 4, 6, 7] : int list*)
76 (*an indulgent version of Isabelle/src/Pure/library.ML ~~*)
79 let fun aaa xys [] [] = xys
80 | aaa xys [] (y :: ys) = xys
81 | aaa xys (x :: xs) [] = xys
82 | aaa xys (x :: xs) (y :: ys) = aaa (xys @ [(x, y)]) xs ys
84 (*[1,2,3] ~~~ ["1","2","3"];
85 val it = [(1, "1"), (2, "2"), (3, "3")] : (int * string) list
86 > [1,2] ~~~ ["1","2","3"];
87 val it = [(1, "1"), (2, "2")] : (int * string) list
88 > [1,2,3] ~~~ ["1","2"];
89 val it = [(1, "1"), (2, "2")] : (int * string) list*)
91 (*from Isabelle2002/src/Pure/library.ML; has changed in 2009 FIXME replace*)
92 fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs;
93 fun gen_insI eq pr (x, xs) =
95 then (tracing ("### occurs twice: "^(pr x)); xs)
97 fun gen_union eq pr (xs, []) = xs
98 | gen_union eq pr ([], ys) = ys
99 | gen_union eq pr (x :: xs, ys) = gen_union eq pr (xs, gen_ins eq (x, ys));
101 fun cons2 (f,g) x = (f x, g x); (*PL softwareparadigmen*)
103 fun nth _ [] = error "nth _ []"
105 | nth n (_::xs) = nth (n-1) xs;
106 (*WN050106 quick for test: doesn't check for exns*)
107 fun drop_nth ls (_, []) = ls
108 | drop_nth ls (n, x :: xs) =
111 else drop_nth (ls @ [x]) (n - 1, xs);
112 (*> drop_nth [] (3,[1,2,3,4,5]);
113 val it = [1, 2, 4, 5] : int list
114 > drop_nth [] (1,[1,2,3,4,5]);
115 val it = [2, 3, 4, 5] : int list
116 > drop_nth [] (5,[1,2,3,4,5]);
117 val it = [1, 2, 3, 4] : int list *)
119 fun and_ (b1,b2) = b1 andalso b2;(* ~/Design.98/ModelSpec.sml/library_G.sml *)
120 fun or_ (b1,b2) = b1 orelse b2;
123 fun takerest (i, ls) = (rev o take) (length ls - i, rev ls);
124 (*> takerest (3, ["normalize","polynomial","univariate","equation"]);
125 val it = ["equation"] : string list
127 fun takelast (i, ls) = (rev o take) (i, rev ls);
128 (* > takelast (2, ["normalize","polynomial","univariate","equation"]);
129 val it = ["univariate", "equation"] : pblID
130 > takelast (2, ["equation"]);
131 val it = ["equation"] : pblID
132 > takelast (3, ["normalize","polynomial","univariate","equation"]);
133 val it = ["polynomial", "univariate", "equation"]*)
134 fun split_nlast (i, ls) =
136 in (rev (takelast (i - 1, rv)), rev (take (i, rv))) end;
138 fun split_nlast (i, ls) = (take (length ls - i, ls), rev (take (i, rev ls)));
139 (* val (a, b) = split_nlast (3, ["a","b","[",".","]"]);
140 val a = ["a", "b"] : string list
141 val b = ["[", ".", "]"] : string list
142 > val (a, b) = split_nlast (3, [".","]"]);
143 val a = [] : string list
144 val b = [".", "]"] : string list *)
146 (*.analoguous to dropwhile in Pure/libarary.ML.*)
147 fun dropwhile P [] = []
148 | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
149 fun takewhile col P [] = col
150 | takewhile col P (ys as x::xs) = if P x then takewhile (col @ [x]) P xs
152 (* > takewhile [] (not o (curry op= 4)) [1,2,3,4,5,6,7];
153 val it = [1, 2, 3] : int list*)
154 fun dropuntil P [] = []
155 | dropuntil P (ys as x::xs) = if P x then ys else dropuntil P xs;
159 fun pair2tri ((a,b),c) = (a,b,c);
160 fun fst3 (a,_,_) = a;
161 fun snd3 (_,b,_) = b;
162 fun thd3 (_,_,c) = c;
164 fun skip_blanks strl =
166 fun skip strl [] = strl
167 | skip strl (" "::ss) = skip strl ss
168 | skip strl ( s ::ss) = skip (strl @ [s]) ss
174 let fun scan ss' [] = ss'
175 | scan ss' ("\""::ss) = scan ss' ss
176 | scan ss' (s::ss) = scan (ss' @ [s]) ss;
177 in (implode o (scan []) o explode) str end;
178 (*> de_quote "\"d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v\"";
179 val it = "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v" : string*)
183 (* conversions to (quoted) strings
184 FIXME: rename *2str --> *2strq (quoted elems)
185 now *2str' (elems NOT quoted) instead of *2str *)
187 val commas = space_implode ",";
189 fun strs2str strl = "[" ^ (commas (map quote strl)) ^ "]";
190 (*> val str = strs2str ["123","asd"]; tracing str;
191 val it = "[\"123\", \"asd\"]" : string
193 fun strs2str' strl = "[" ^ (commas strl) ^ "]";
194 fun list2str strl = "[" ^ (commas strl) ^ "]";
195 (*> val str = list2str ["123","asd"]; tracing str;
196 val str = "[123, asd]" : string
198 fun spair2str (s1,s2) = "(" ^ (quote s1) ^ ", " ^ (quote s2) ^ ")";
199 fun pair2str (s1,s2) = "(" ^ s1 ^ ", " ^ s2 ^ ")";
201 fun subs2str (subs:(string * string) list) =
202 (list2str o (map pair2str)) subs;*)
203 fun subs2str (subs: string list) = list2str subs;
204 (*> val sss = ["(bdv,x)","(err,#0)"];
206 val it = "[(bdv,x),(err,#0)]" : string*)
207 fun subs2str' (subs:(string * string) list) = (*12.12.99???*)
208 (list2str o (map pair2str)) subs;
209 (*> val subs = subs2str [("bdv","x")]; tracing subs;
210 val subs = "[(\"bdv\", \"x\")]" : string
212 fun con2str land_lor = quote " &| ";
213 val int2str = string_of_int;
214 fun ints2str ints = (strs2str o (map string_of_int)) ints;
215 fun ints2str' ints = (strs2str' o (map string_of_int)) ints;
222 (*needed in Isa + ME*)
224 let fun get strl [] = strl
225 | get strl ("."::ss) = strl
226 | get strl ( s ::ss) = get (strl @ [s]) ss
227 in implode( get [] (explode str)) end;
230 let fun strip bdVar [] = implode (rev bdVar)
231 | strip bdVar ("."::_ ) = implode (rev bdVar)
232 | strip bdVar (c ::cs) = strip (bdVar @[c]) cs
233 in strip [] (rev(explode str)) end;
235 fun id_of (Var ((id,ix),_)) = if ix=0 then id else id^(string_of_int ix)
236 | id_of (Free (id ,_)) = id
237 | id_of (Const(id ,_)) = id
238 | id_of _ = ""; (* never such an identifier *)
241 let fun con ss (Const (s,_)) = s::ss
242 | con ss (Free (s,_)) = s::ss
243 | con ss (Abs (s,_,b)) = s::(con ss b)
244 | con ss (t1 $ t2) = (con ss t1) @ (con ss t2)
246 in map strip_thy ((distinct o (con [])) t) end;
248 > val t = (term_of o the o (parse thy))
249 "solve_univar (R, [univar, equation], no_met) (a = b + #1) a";
251 ["solve'_univar","Pair","R","Cons","univar","equation","Nil",...]*)
254 (*FIXME.WN090819 fun overwrite missing in ..2009/../library.ML*)
255 fun overwrite (al, p as (key, _)) = (*FIXME.2009*)
256 let fun over ((q as (keyi, _)) :: pairs) =
257 if keyi = key then p :: pairs else q :: (over pairs)
260 fun overwritel (al, []) = al
261 | overwritel (al, b::bl) = overwritel (overwrite (al, b), bl);
262 (*> val aaa = [(1,11),(2,22),(3,33)];
263 > overwritel (aaa, [(2,2222),(4,4444)]);
264 val it = [(1,11),(2,2222),(3,33),(4,4444)] : (int * int) list*)
269 | intsto1 n = (intsto1 (n-1)) @ [n]
271 fun intsto n = if n < 0 then (error "intsto < 0") else intsto1 n
275 type 'a stack = 'a list;
276 fun top ((x::xs):'a stack) = x
277 | top _ = error "top called for empty list";
278 fun pop ((x::xs):'a stack) = xs:'a stack
279 | pop _ = error "pop called for empty list";
280 fun push x (xs:'a stack) = x::xs:'a stack;
283 fun drop_last l = ((rev o tl o rev) l);
284 fun drop_last_n n l = rev (takerest (n, rev l));
285 (*> drop_last_n 2 [1,2,3,4,5];
286 val it = [1, 2, 3] : int list
287 > drop_last_n 3 [1,2,3,4,5];
288 val it = [1, 2] : int list
289 > drop_last_n 7 [1,2,3,4,5];
290 val it = [] : int list
293 fun bool2str true = "true"
294 | bool2str false = "false";
296 (*.take elements from b to e including both.*)
297 fun take_fromto from to l =
298 if from > to then error ("take_fromto from="^string_of_int from^
299 " > to="^string_of_int to)
300 else drop (from - 1, take (to, l));
301 (*> take_fromto 3 5 [1,2,3,4,5,6,7];
302 val it = [3,4,5] : int list
303 > take_fromto 3 3 [1,2,3,4,5,6,7];
304 val it = [3] : int list*)
308 | idt str n = str ^ idt str (n-1);
310 | indt n = " " ^ indt (n-1);---------does not terminate with negatives*)
311 fun indt n = if n <= 0 then "" else " " ^ indt (n-1);
313 | indent n = ". " ^ indent(n-1);
315 fun dashs i = if 0<i then "-"^ dashs (i-1) else "";
316 fun dots i = if 0<i then "."^ dots (i-1) else "";
318 fun assoc ([], key) = NONE(*cp 2002 Pure/library.ML FIXXXME take AList.lookup*)
319 | assoc ((keyi, xi) :: pairs, key) =
320 if key = keyi then SOME xi else assoc (pairs, key);
321 (*association list lookup, optimized version for strings*)
322 fun assoc_string ([], (key:string)) = NONE
323 | assoc_string ((keyi, xi) :: pairs, key) =
324 if key = keyi then SOME xi else assoc_string (pairs, key);
325 fun if_none NONE y = y (*cp from 2002 Pure/library.ML FIXXXME replace*)
326 | if_none (SOME x) _ = x;
328 (* redirect tracing, following The Isabelle Cookbook *)
329 (* TODO: how redirect tracing to emacs buffer again ? *)
332 fun strip ("\^A" :: _ :: cs) = strip cs
333 | strip (c :: cs) = c :: strip cs
336 implode o strip o explode
339 Output.tracing_fn := (fn s =>
340 (TextIO.output (stream, (strip_specials s));
341 TextIO.output (stream, "\n");
342 TextIO.flushOut stream));
343 fun redirect_tracing path = redir (TextIO.openOut path);