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;
34 (*exn LIST has disappeared in 2009 ... replaced by error...*)
35 fun last_elem [] = raise error "last_elem" (*FIXME.2009*)
37 | last_elem (_ :: xs) = last_elem xs;
39 fun gen_mem eq (x, []) = false (*FIXME.2009*)
40 | gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys);
41 fun gen_insert (les : 'a * 'a -> bool) ([], a) = [a]
42 | gen_insert les (x::xs, a) = if les (x, a) then x::(gen_insert les (xs, a))
44 fun gen_sort les xs = foldl (gen_insert les) (xs, []);
45 fun gen_distinct eq lst =
47 val memb = gen_mem eq;
49 fun dist (rev_seen, []) = rev rev_seen
50 | dist (rev_seen, x :: xs) =
51 if memb (x, rev_seen) then dist (rev_seen, xs)
52 else dist (x :: rev_seen, xs);
56 fun gen_rems eq (xs, ys) = filter_out (fn x => gen_mem eq (x, ys)) xs;
57 fun distinct l = gen_distinct (op =) l;
61 (*.see 'fun (y :: ys) \ x' in Pure/library.ML.*)
62 fun gen_dif eq (y :: ys, x) = if eq (y, x) then ys
63 else y :: (gen_dif eq (ys, x))
64 | gen_dif eq ([], x) = [];
65 (* val (eq, (y :: ys, x)) = ();*)
67 (*.see 'fun ys \\ xs' in Pure/library.ML.*)
68 fun gen_diff eq (ys, xs) = foldl (gen_dif eq) (ys,xs);
69 (* val (eq, (ys, xs)) = (eq_thmI, (isacrlsthms, isacthms));
71 (* gen_diff op= ([1,2,3,4,5,6,7],[2,3,5]);
72 val it = [1, 4, 6, 7] : int list*)
75 (*an indulgent version of Isabelle/src/Pure/library.ML ~~*)
78 let fun aaa xys [] [] = xys
79 | aaa xys [] (y :: ys) = xys
80 | aaa xys (x :: xs) [] = xys
81 | aaa xys (x :: xs) (y :: ys) = aaa (xys @ [(x, y)]) xs ys
83 (*[1,2,3] ~~~ ["1","2","3"];
84 val it = [(1, "1"), (2, "2"), (3, "3")] : (int * string) list
85 > [1,2] ~~~ ["1","2","3"];
86 val it = [(1, "1"), (2, "2")] : (int * string) list
87 > [1,2,3] ~~~ ["1","2"];
88 val it = [(1, "1"), (2, "2")] : (int * string) list*)
90 (*from Isabelle2002/src/Pure/library.ML; has changed in 2009 FIXME replace*)
91 fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs;
92 fun gen_insI eq pr (x, xs) =
94 then (writeln ("### occurs twice: "^(pr x)); xs)
96 fun gen_union eq pr (xs, []) = xs
97 | gen_union eq pr ([], ys) = ys
98 | gen_union eq pr (x :: xs, ys) = gen_union eq pr (xs, gen_ins eq (x, ys));
100 fun cons2 (f,g) x = (f x, g x); (*PL softwareparadigmen*)
102 fun nth _ [] = raise error "nth _ []"
104 | nth n (_::xs) = nth (n-1) xs;
105 (*WN050106 quick for test: doesn't check for exns*)
106 fun drop_nth ls (_, []) = ls
107 | drop_nth ls (n, x :: xs) =
110 else drop_nth (ls @ [x]) (n - 1, xs);
111 (*> drop_nth [] (3,[1,2,3,4,5]);
112 val it = [1, 2, 4, 5] : int list
113 > drop_nth [] (1,[1,2,3,4,5]);
114 val it = [2, 3, 4, 5] : int list
115 > drop_nth [] (5,[1,2,3,4,5]);
116 val it = [1, 2, 3, 4] : int list *)
118 fun and_ (b1,b2) = b1 andalso b2;(* ~/Design.98/ModelSpec.sml/library_G.sml *)
119 fun or_ (b1,b2) = b1 orelse b2;
122 fun takerest (i, ls) = (rev o take) (length ls - i, rev ls);
123 (*> takerest (3, ["normalize","polynomial","univariate","equation"]);
124 val it = ["equation"] : string list
126 fun takelast (i, ls) = (rev o take) (i, rev ls);
127 (* > takelast (2, ["normalize","polynomial","univariate","equation"]);
128 val it = ["univariate", "equation"] : pblID
129 > takelast (2, ["equation"]);
130 val it = ["equation"] : pblID
131 > takelast (3, ["normalize","polynomial","univariate","equation"]);
132 val it = ["polynomial", "univariate", "equation"]*)
133 fun split_nlast (i, ls) =
135 in (rev (takelast (i - 1, rv)), rev (take (i, rv))) end;
137 fun split_nlast (i, ls) = (take (length ls - i, ls), rev (take (i, rev ls)));
138 (* val (a, b) = split_nlast (3, ["a","b","[",".","]"]);
139 val a = ["a", "b"] : string list
140 val b = ["[", ".", "]"] : string list
141 > val (a, b) = split_nlast (3, [".","]"]);
142 val a = [] : string list
143 val b = [".", "]"] : string list *)
145 (*.analoguous to dropwhile in Pure/libarary.ML.*)
146 fun dropwhile P [] = []
147 | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
148 fun takewhile col P [] = col
149 | takewhile col P (ys as x::xs) = if P x then takewhile (col @ [x]) P xs
151 (* > takewhile [] (not o (curry op= 4)) [1,2,3,4,5,6,7];
152 val it = [1, 2, 3] : int list*)
153 fun dropuntil P [] = []
154 | dropuntil P (ys as x::xs) = if P x then ys else dropuntil P xs;
158 fun pair2tri ((a,b),c) = (a,b,c);
159 fun fst3 (a,_,_) = a;
160 fun snd3 (_,b,_) = b;
161 fun thd3 (_,_,c) = c;
163 fun skip_blanks strl =
165 fun skip strl [] = strl
166 | skip strl (" "::ss) = skip strl ss
167 | skip strl ( s ::ss) = skip (strl @ [s]) ss
173 let fun scan ss' [] = ss'
174 | scan ss' ("\""::ss) = scan ss' ss
175 | scan ss' (s::ss) = scan (ss' @ [s]) ss;
176 in (implode o (scan []) o explode) str end;
177 (*> de_quote "\"d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v\"";
178 val it = "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v" : string*)
182 (* conversions to (quoted) strings
183 FIXME: rename *2str --> *2strq (quoted elems)
184 now *2str' (elems NOT quoted) instead of *2str *)
186 val commas = space_implode ",";
188 fun strs2str strl = "[" ^ (commas (map quote strl)) ^ "]";
189 (*> val str = strs2str ["123","asd"]; writeln str;
190 val it = "[\"123\", \"asd\"]" : string
192 fun strs2str' strl = "[" ^ (commas strl) ^ "]";
193 fun list2str strl = "[" ^ (commas strl) ^ "]";
194 (*> val str = list2str ["123","asd"]; writeln str;
195 val str = "[123, asd]" : string
197 fun spair2str (s1,s2) = "(" ^ (quote s1) ^ ", " ^ (quote s2) ^ ")";
198 fun pair2str (s1,s2) = "(" ^ s1 ^ ", " ^ s2 ^ ")";
200 fun subs2str (subs:(string * string) list) =
201 (list2str o (map pair2str)) subs;*)
202 fun subs2str (subs: string list) = list2str subs;
203 (*> val sss = ["(bdv,x)","(err,#0)"];
205 val it = "[(bdv,x),(err,#0)]" : string*)
206 fun subs2str' (subs:(string * string) list) = (*12.12.99???*)
207 (list2str o (map pair2str)) subs;
208 (*> val subs = subs2str [("bdv","x")]; writeln subs;
209 val subs = "[(\"bdv\", \"x\")]" : string
211 fun con2str land_lor = quote " &| ";
212 val int2str = string_of_int;
213 fun ints2str ints = (strs2str o (map string_of_int)) ints;
214 fun ints2str' ints = (strs2str' o (map string_of_int)) ints;
221 (*needed in Isa + ME*)
223 let fun get strl [] = strl
224 | get strl ("."::ss) = strl
225 | get strl ( s ::ss) = get (strl @ [s]) ss
226 in implode( get [] (explode str)) end;
229 let fun strip bdVar [] = implode (rev bdVar)
230 | strip bdVar ("."::_ ) = implode (rev bdVar)
231 | strip bdVar (c ::cs) = strip (bdVar @[c]) cs
232 in strip [] (rev(explode str)) end;
234 fun id_of (Var ((id,ix),_)) = if ix=0 then id else id^(string_of_int ix)
235 | id_of (Free (id ,_)) = id
236 | id_of (Const(id ,_)) = id
237 | id_of _ = ""; (* never such an identifier *)
240 let fun con ss (Const (s,_)) = s::ss
241 | con ss (Free (s,_)) = s::ss
242 | con ss (Abs (s,_,b)) = s::(con ss b)
243 | con ss (t1 $ t2) = (con ss t1) @ (con ss t2)
245 in map strip_thy ((distinct o (con [])) t) end;
247 > val t = (term_of o the o (parse thy))
248 "solve_univar (R, [univar, equation], no_met) (a = b + #1) a";
250 ["solve'_univar","Pair","R","Cons","univar","equation","Nil",...]*)
253 (*FIXME.WN090819 fun overwrite missing in ..2009/../library.ML*)
254 fun overwrite (al, p as (key, _)) = (*FIXME.2009*)
255 let fun over ((q as (keyi, _)) :: pairs) =
256 if keyi = key then p :: pairs else q :: (over pairs)
259 fun overwritel (al, []) = al
260 | overwritel (al, b::bl) = overwritel (overwrite (al, b), bl);
261 (*> val aaa = [(1,11),(2,22),(3,33)];
262 > overwritel (aaa, [(2,2222),(4,4444)]);
263 val it = [(1,11),(2,2222),(3,33),(4,4444)] : (int * int) list*)
268 | intsto1 n = (intsto1 (n-1)) @ [n]
270 fun intsto n = if n < 0 then (raise error "intsto < 0") else intsto1 n
274 type 'a stack = 'a list;
275 fun top ((x::xs):'a stack) = x
276 | top _ = raise error "top called for empty list";
277 fun pop ((x::xs):'a stack) = xs:'a stack
278 | pop _ = raise error "pop called for empty list";
279 fun push x (xs:'a stack) = x::xs:'a stack;
282 fun drop_last l = ((rev o tl o rev) l);
283 fun drop_last_n n l = rev (takerest (n, rev l));
284 (*> drop_last_n 2 [1,2,3,4,5];
285 val it = [1, 2, 3] : int list
286 > drop_last_n 3 [1,2,3,4,5];
287 val it = [1, 2] : int list
288 > drop_last_n 7 [1,2,3,4,5];
289 val it = [] : int list
292 fun bool2str true = "true"
293 | bool2str false = "false";
295 (*.take elements from b to e including both.*)
296 fun take_fromto from to l =
297 if from > to then raise error ("take_fromto from="^string_of_int from^
298 " > to="^string_of_int to)
299 else drop (from - 1, take (to, l));
300 (*> take_fromto 3 5 [1,2,3,4,5,6,7];
301 val it = [3,4,5] : int list
302 > take_fromto 3 3 [1,2,3,4,5,6,7];
303 val it = [3] : int list*)
307 | idt str n = str ^ idt str (n-1);
309 | indt n = " " ^ indt (n-1);---------does not terminate with negatives*)
310 fun indt n = if n <= 0 then "" else " " ^ indt (n-1);
312 | indent n = ". " ^ indent(n-1);
314 fun dashs i = if 0<i then "-"^ dashs (i-1) else "";
315 fun dots i = if 0<i then "."^ dots (i-1) else "";
317 fun assoc ([], key) = NONE(*cp 2002 Pure/library.ML FIXXXME take AList.lookup*)
318 | assoc ((keyi, xi) :: pairs, key) =
319 if key = keyi then SOME xi else assoc (pairs, key);
320 (*association list lookup, optimized version for strings*)
321 fun assoc_string ([], (key:string)) = NONE
322 | assoc_string ((keyi, xi) :: pairs, key) =
323 if key = keyi then SOME xi else assoc_string (pairs, key);
324 fun if_none NONE y = y (*cp from 2002 Pure/library.ML FIXXXME replace*)
325 | if_none (SOME x) _ = x;