src/Tools/isac/library.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 09:06:56 +0200
branchisac-update-Isa09-2
changeset 38031 460c24a6a6ba
parent 38022 e6d356fc4d38
child 38051 efdeff9df986
permissions -rw-r--r--
tuned error and writeln

# raise error --> error
# writeln in atomtyp, atomty, atomt and xmlsrc
     1 (* use"library.sml";
     2    WN.22.10.99
     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
     6 *)
     7 
     8 (* Isabelle2002 -> Isabelle2009 library changed:
     9 signature LIBRARY =
    10 sig
    11   include BASIC_LIBRARY
    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
    18 end;
    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)
    23   in  itl end;
    24 fun foldr f (l, e) = (*FIXME.2009*)
    25   let fun itr [] = e
    26         | itr (a::l) = f(a, itr l)
    27   in  itr l  end;
    28 fun take (n, []) = [] (*FIXME.2009*)
    29   | take (n, x :: xs) =
    30       if n > 0 then x :: take (n - 1, xs) else [];
    31 fun drop (n, []) = [] (*FIXME.2009*)
    32   | drop (n, x :: xs) =
    33       if n > 0 then drop (n - 1, xs) else x :: xs;
    34 
    35 (*exn LIST has disappeared in 2009 ... replaced by error...*)
    36 fun last_elem [] = error "last_elem" (*FIXME.2009*)
    37   | last_elem [x] = x
    38   | last_elem (_ :: xs) = last_elem xs;
    39 
    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)) 
    44 			    else a::x::xs;
    45 fun gen_sort les xs = foldl (gen_insert les) (xs, []);
    46 fun gen_distinct eq lst =
    47   let
    48     val memb = gen_mem eq;
    49 
    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);
    54   in
    55     dist ([], lst)
    56   end;
    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;
    59 
    60 
    61 
    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)) = ();*)
    67 
    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));
    71  *)
    72 (* gen_diff op= ([1,2,3,4,5,6,7],[2,3,5]);
    73 val it = [1, 4, 6, 7] : int list*)
    74 
    75 
    76 (*an indulgent version of Isabelle/src/Pure/library.ML ~~*)
    77 infix ~~~;
    78 fun xs ~~~ ys =
    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
    83     in aaa [] xs ys end;
    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*)
    90 
    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) = 
    94     if gen_mem eq (x, xs) 
    95     then (tracing ("### occurs twice: "^(pr x)); xs) 
    96     else 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));
   100 
   101 fun cons2 (f,g) x = (f x, g x); (*PL softwareparadigmen*)
   102 
   103 fun nth _ []      = error "nth _ []"
   104   | nth 1 (x::_) = x
   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) = 
   109       if n = 1 
   110       then ls @ 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 *)
   118 
   119 fun and_ (b1,b2) = b1 andalso b2;(* ~/Design.98/ModelSpec.sml/library_G.sml *) 
   120 fun or_ (b1,b2) = b1 orelse b2;
   121 
   122 
   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
   126 *)
   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) =
   135     let val rv = rev ls
   136     in (rev (takelast (i - 1, rv)), rev (take (i, rv))) end;
   137 
   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   *)
   145 
   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
   151 				     else col;
   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;
   156 
   157 
   158 
   159 fun pair2tri ((a,b),c) = (a,b,c);
   160 fun fst3 (a,_,_) = a;
   161 fun snd3 (_,b,_) = b;
   162 fun thd3 (_,_,c) = c;
   163 
   164 fun skip_blanks strl = 
   165   let 
   166     fun skip strl []        = strl
   167       | skip strl (" "::ss) = skip strl ss
   168       | skip strl ( s ::ss) = skip (strl @ [s]) ss
   169   in skip [] strl end;
   170 
   171 
   172 
   173 fun de_quote str =
   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*)
   180 
   181 
   182 
   183 (* conversions to (quoted) strings 
   184    FIXME: rename *2str --> *2strq (quoted elems) 
   185              now *2str' (elems NOT quoted) instead of *2str *)
   186 
   187 val commas = space_implode ",";
   188 
   189 fun strs2str strl = "[" ^ (commas (map quote strl)) ^ "]";
   190 (*> val str = strs2str ["123","asd"]; tracing str;
   191 val it = "[\"123\", \"asd\"]" : string
   192 "123", "asd"] *)
   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
   197 [123, asd] *)
   198 fun spair2str (s1,s2) =   "(" ^ (quote s1) ^ ", " ^ (quote s2) ^ ")";
   199 fun pair2str (s1,s2) =   "(" ^ s1 ^ ", " ^ s2 ^ ")";
   200 (*16.11.00
   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)"];
   205 > subs2str sss;
   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
   211 [("bdv", "x")] *)
   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;
   216 
   217 
   218 (* use"library.sml";
   219    *)
   220 
   221 
   222 (*needed in Isa + ME*)
   223 fun get_thy str = 
   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;
   228 
   229 fun strip_thy str =
   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;
   234     
   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 *)
   239 
   240 fun ids_of t =
   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)
   245 	| con ss _ = ss
   246   in map strip_thy ((distinct o (con [])) t) end;
   247 (*
   248 > val t = (term_of o the o (parse thy))
   249   "solve_univar (R, [univar, equation], no_met) (a = b + #1) a";
   250 > ids_of t;
   251 ["solve'_univar","Pair","R","Cons","univar","equation","Nil",...]*)
   252 
   253 
   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)
   258         | over [] = [p]
   259   in over al end;
   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*)
   265 
   266 
   267 local
   268 fun intsto1 0 = []
   269   | intsto1 n = (intsto1 (n-1)) @ [n]
   270 in
   271 fun intsto n  = if n < 0 then (error "intsto < 0") else intsto1 n
   272 end;
   273 
   274 
   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;
   281 
   282 
   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
   291 *)
   292 
   293 fun bool2str true = "true"
   294   | bool2str false = "false";
   295 
   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*)
   305 
   306 
   307 fun idt str 0 = " "
   308   | idt str n = str ^ idt str (n-1);
   309 (*fun indt 0 = ""
   310   | indt n = " " ^ indt (n-1);---------does not terminate with negatives*)
   311 fun indt n = if n <= 0 then "" else " " ^ indt (n-1);
   312 fun indent 0 = ""
   313   | indent n = ". " ^ indent(n-1);
   314 
   315 fun dashs i = if 0<i then "-"^ dashs (i-1) else "";
   316 fun dots i = if 0<i then "."^ dots (i-1) else "";
   317 
   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;
   327 
   328 (* redirect tracing, following The Isabelle Cookbook *)
   329 (* TODO: how redirect tracing to emacs buffer again ? *)
   330 val strip_specials =
   331     let
   332         fun strip ("\^A" :: _ :: cs) = strip cs
   333           | strip (c :: cs) = c :: strip cs
   334           | strip [] = [];
   335     in
   336         implode o strip o explode
   337     end
   338 fun redir stream =
   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);