src/sml/library.sml
author wneuper
Fri, 03 Nov 2006 10:51:51 +0100
branchstart_Take
changeset 680 0bf7c1333a35
parent 632 e33b5003539a
permissions -rw-r--r--
"sym_thm ... [.]" fixed with "fun string_of_thmI"
agriesma@308
     1
(* use"library.sml";
agriesma@308
     2
   WN.22.10.99
agriesma@308
     3
   for both, math-engine and isa-98-1-HOL-plus
agriesma@308
     4
   however, functions closely related to original isabelle-98-1 functions
agriesma@308
     5
            are in isa-98-1-HOL-plus/rewrite-parse/library_G
agriesma@308
     6
*)
agriesma@308
     7
wneuper@583
     8
(*------------ from Pure/library.ML for tests ------------------------*
wneuper@579
     9
fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a =
wneuper@579
    10
  let fun itl (e, [])  = e
wneuper@579
    11
        | itl (e, a::l) = itl (f(e, a), l)
wneuper@579
    12
  in  itl end;
wneuper@579
    13
(* val () = ();
wneuper@579
    14
   *)
wneuper@583
    15
----------------------------------------------------------------------*)
wneuper@579
    16
wneuper@583
    17
(*WN0607 sort in library.ML seems to be broken*)
wneuper@583
    18
fun insert ([], a : string) = [a]
wneuper@583
    19
  | insert (x::xs, a) = if x < a then x::(insert (xs, a)) else a::x::xs;
wneuper@583
    20
fun inssort xs = foldl insert (xs, []);
wneuper@579
    21
wneuper@583
    22
fun gen_insert (les : 'a * 'a -> bool) ([], a) = [a]
wneuper@583
    23
  | gen_insert les (x::xs, a) = if les (x, a) then x::(gen_insert les (xs, a)) 
wneuper@583
    24
			    else a::x::xs;
wneuper@583
    25
fun gen_sort les xs = foldl (gen_insert les) (xs, []);
wneuper@579
    26
wneuper@579
    27
wneuper@579
    28
(*.see 'fun (y :: ys) \ x' in Pure/library.ML.*)
wneuper@579
    29
fun gen_dif eq (y :: ys, x) = if eq (y, x) then ys 
wneuper@579
    30
			      else y :: (gen_dif eq (ys, x))
wneuper@579
    31
  | gen_dif eq ([], x) = [];
wneuper@579
    32
(* val (eq, (y :: ys, x)) = ();
wneuper@579
    33
wneuper@579
    34
   *)
wneuper@579
    35
wneuper@579
    36
wneuper@579
    37
(*.see 'fun ys \\ xs' in Pure/library.ML.*)
wneuper@579
    38
fun gen_diff eq (ys, xs) = foldl (gen_dif eq) (ys,xs);
wneuper@579
    39
(* val (eq, (ys, xs)) = (eq_thmI, (isacrlsthms, isacthms));
wneuper@579
    40
wneuper@579
    41
 *)
wneuper@579
    42
(* gen_diff op= ([1,2,3,4,5,6,7],[2,3,5]);
wneuper@579
    43
val it = [1, 4, 6, 7] : int list*)
wneuper@579
    44
wneuper@579
    45
wneuper@343
    46
(*an indulgent version of Isabelle/src/Pure/library.ML ~~*)
wneuper@343
    47
infix ~~~;
wneuper@343
    48
fun xs ~~~ ys =
wneuper@343
    49
    let fun aaa xys []        []        = xys
wneuper@343
    50
	  | aaa xys []        (y :: ys) = xys
wneuper@343
    51
	  | aaa xys (x :: xs) []        = xys
wneuper@343
    52
	  | aaa xys (x :: xs) (y :: ys) = aaa (xys @ [(x, y)]) xs ys
wneuper@343
    53
    in aaa [] xs ys end;
wneuper@343
    54
(*[1,2,3] ~~~ ["1","2","3"];
wneuper@343
    55
val it = [(1, "1"), (2, "2"), (3, "3")] : (int * string) list
wneuper@343
    56
> [1,2] ~~~ ["1","2","3"];
wneuper@343
    57
val it = [(1, "1"), (2, "2")] : (int * string) list
wneuper@343
    58
> [1,2,3] ~~~ ["1","2"];
wneuper@343
    59
val it = [(1, "1"), (2, "2")] : (int * string) list*)
agriesma@308
    60
agriesma@308
    61
fun gen_insI eq pr (x, xs) = 
agriesma@308
    62
    if gen_mem eq (x, xs) 
agriesma@308
    63
    then (writeln ("### occurs twice: "^(pr x)); xs) 
agriesma@308
    64
    else x :: xs;
agriesma@308
    65
fun gen_union eq pr (xs, []) = xs
agriesma@308
    66
  | gen_union eq pr ([], ys) = ys
agriesma@308
    67
  | gen_union eq pr (x :: xs, ys) = gen_union eq pr (xs, gen_ins eq (x, ys));
agriesma@308
    68
agriesma@308
    69
fun cons2 (f,g) x = (f x, g x); (*PL softwareparadigmen*)
agriesma@308
    70
agriesma@308
    71
fun nth _ []      = raise error "nth _ []"
wneuper@343
    72
  | nth 1 (x::_) = x
wneuper@343
    73
  | nth n (_::xs) = nth (n-1) xs;
wneuper@343
    74
(*WN050106 quick for test: doesn't check for exns*)
wneuper@343
    75
fun drop_nth ls (_, []) = ls
wneuper@343
    76
  | drop_nth ls (n, x :: xs) = 
wneuper@343
    77
      if n = 1 
wneuper@343
    78
      then ls @ xs
wneuper@343
    79
      else drop_nth (ls @ [x]) (n - 1, xs);
wneuper@343
    80
(*> drop_nth [] (3,[1,2,3,4,5]); 
wneuper@343
    81
val it = [1, 2, 4, 5] : int list
wneuper@343
    82
 > drop_nth [] (1,[1,2,3,4,5]); 
wneuper@343
    83
val it = [2, 3, 4, 5] : int list
wneuper@343
    84
 > drop_nth [] (5,[1,2,3,4,5]); 
wneuper@343
    85
val it = [1, 2, 3, 4] : int list *)
agriesma@308
    86
agriesma@308
    87
fun and_ (b1,b2) = b1 andalso b2;(* ~/Design.98/ModelSpec.sml/library_G.sml *) 
agriesma@308
    88
fun or_ (b1,b2) = b1 orelse b2;
wneuper@680
    89
agriesma@308
    90
fun takerest (i, ls) = (rev o take) (length ls - i, rev ls);
wneuper@562
    91
(*> takerest (3, ["normalize","polynomial","univariate","equation"]);
wneuper@562
    92
val it = ["equation"] : string list
wneuper@562
    93
*)
wneuper@459
    94
fun takelast (i, ls) = (rev o take) (i, rev ls);
wneuper@459
    95
(* > takelast (2, ["normalize","polynomial","univariate","equation"]);
wneuper@459
    96
val it = ["univariate", "equation"] : pblID
wneuper@562
    97
> takelast (2, ["equation"]);
wneuper@562
    98
val it = ["equation"] : pblID
wneuper@562
    99
> takelast (3, ["normalize","polynomial","univariate","equation"]);
wneuper@563
   100
val it = ["polynomial", "univariate", "equation"]*)
wneuper@680
   101
fun split_nlast (i, ls) =
wneuper@680
   102
    let val rv = rev ls
wneuper@680
   103
    in (rev (takelast (i - 1, rv)), rev (take (i, rv))) end;
wneuper@680
   104
wneuper@680
   105
fun split_nlast (i, ls) = (take (length ls - i, ls), rev (take (i, rev ls)));
wneuper@680
   106
(* val (a, b) = split_nlast (3, ["a","b","[",".","]"]);
wneuper@680
   107
val a = ["a", "b"] : string list
wneuper@680
   108
val b = ["[", ".", "]"] : string list
wneuper@680
   109
>  val (a, b) = split_nlast (3, [".","]"]);
wneuper@680
   110
val a = [] : string list
wneuper@680
   111
val b = [".", "]"] : string list   *)
wneuper@557
   112
wneuper@557
   113
(*.analoguous to dropwhile in Pure/libarary.ML.*)
wneuper@557
   114
fun dropwhile P [] = []
wneuper@557
   115
  | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
wneuper@557
   116
fun takewhile col P [] = col
wneuper@557
   117
  | takewhile col P (ys as x::xs) = if P x then takewhile (col @ [x]) P xs
wneuper@557
   118
				     else col;
wneuper@568
   119
(* > takewhile [] (not o (curry op= 4)) [1,2,3,4,5,6,7];
wneuper@568
   120
   val it = [1, 2, 3] : int list*)
wneuper@567
   121
fun dropuntil P [] = []
wneuper@567
   122
  | dropuntil P (ys as x::xs) = if P x then ys else dropuntil P xs;
wneuper@567
   123
wneuper@567
   124
wneuper@567
   125
agriesma@308
   126
fun pair2tri ((a,b),c) = (a,b,c);
agriesma@308
   127
fun fst3 (a,_,_) = a;
agriesma@308
   128
fun snd3 (_,b,_) = b;
agriesma@308
   129
fun thd3 (_,_,c) = c;
agriesma@308
   130
agriesma@308
   131
fun skip_blanks strl = 
agriesma@308
   132
  let 
agriesma@308
   133
    fun skip strl []        = strl
agriesma@308
   134
      | skip strl (" "::ss) = skip strl ss
agriesma@308
   135
      | skip strl ( s ::ss) = skip (strl @ [s]) ss
agriesma@308
   136
  in skip [] strl end;
agriesma@308
   137
agriesma@308
   138
agriesma@308
   139
agriesma@308
   140
fun de_quote str =
agriesma@308
   141
  let fun scan ss' [] = ss'
agriesma@308
   142
	| scan ss' ("\""::ss) = scan ss' ss
agriesma@308
   143
	| scan ss' (s::ss) = scan (ss' @ [s]) ss;
agriesma@308
   144
  in (implode o (scan []) o explode) str end;
wneuper@632
   145
(*> de_quote "\"d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v\"";
agriesma@308
   146
val it = "d_d ?bdv (?u + ?v) = d_d ?bdv ?u + d_d ?bdv ?v" : string*)
agriesma@308
   147
agriesma@308
   148
agriesma@308
   149
agriesma@308
   150
(* conversions to (quoted) strings 
agriesma@308
   151
   FIXME: rename *2str --> *2strq (quoted elems) 
agriesma@308
   152
             now *2str' (elems NOT quoted) instead of *2str *)
agriesma@308
   153
agriesma@308
   154
val commas = space_implode ",";
agriesma@308
   155
agriesma@308
   156
fun strs2str strl = "[" ^ (commas (map quote strl)) ^ "]";
agriesma@308
   157
(*> val str = strs2str ["123","asd"]; writeln str;
agriesma@308
   158
val it = "[\"123\", \"asd\"]" : string
agriesma@308
   159
"123", "asd"] *)
agriesma@308
   160
fun strs2str' strl = "[" ^ (commas strl) ^ "]";
agriesma@308
   161
fun list2str strl = "[" ^ (commas strl) ^ "]";
agriesma@308
   162
(*> val str = list2str ["123","asd"]; writeln str;
agriesma@308
   163
val str = "[123, asd]" : string
agriesma@308
   164
[123, asd] *)
agriesma@308
   165
fun spair2str (s1,s2) =   "(" ^ (quote s1) ^ ", " ^ (quote s2) ^ ")";
agriesma@308
   166
fun pair2str (s1,s2) =   "(" ^ s1 ^ ", " ^ s2 ^ ")";
agriesma@308
   167
(*16.11.00
agriesma@308
   168
fun subs2str (subs:(string * string) list) = 
agriesma@308
   169
  (list2str o (map pair2str)) subs;*)
agriesma@308
   170
fun subs2str (subs: string list) = list2str  subs;
agriesma@308
   171
(*> val sss = ["(bdv,x)","(err,#0)"];
agriesma@308
   172
> subs2str sss;
agriesma@308
   173
val it = "[(bdv,x),(err,#0)]" : string*)
agriesma@308
   174
fun subs2str' (subs:(string * string) list) = (*12.12.99???*)
agriesma@308
   175
  (list2str o (map pair2str)) subs;
agriesma@308
   176
(*> val subs = subs2str [("bdv","x")]; writeln subs;
agriesma@308
   177
val subs = "[(\"bdv\", \"x\")]" : string
agriesma@308
   178
[("bdv", "x")] *)
agriesma@308
   179
fun con2str land_lor = quote " &| ";
wneuper@472
   180
val int2str = string_of_int;
agriesma@308
   181
fun ints2str ints = (strs2str o (map string_of_int)) ints;
agriesma@308
   182
fun ints2str' ints = (strs2str' o (map string_of_int)) ints;
agriesma@308
   183
agriesma@308
   184
agriesma@308
   185
(* use"library.sml";
agriesma@308
   186
   *)
agriesma@308
   187
agriesma@308
   188
agriesma@308
   189
(*needed in Isa + ME*)
agriesma@308
   190
fun get_thy str = 
agriesma@308
   191
  let fun get strl []        = strl
agriesma@308
   192
	| get strl ("."::ss) = strl
agriesma@308
   193
	| get strl ( s ::ss) = get (strl @ [s]) ss
agriesma@308
   194
  in implode( get [] (explode str)) end;
agriesma@308
   195
wneuper@553
   196
fun strip_thy str =
agriesma@308
   197
  let fun strip bdVar []        = implode (rev bdVar)
agriesma@308
   198
	| strip bdVar ("."::_ ) = implode (rev bdVar)
agriesma@308
   199
	| strip bdVar (c  ::cs) = strip (bdVar @[c]) cs
agriesma@308
   200
  in strip [] (rev(explode str)) end;
agriesma@308
   201
    
wneuper@343
   202
fun id_of (Var ((id,ix),_)) = if ix=0 then id else id^(string_of_int ix)
wneuper@343
   203
  | id_of (Free (id    ,_)) = id
wneuper@343
   204
  | id_of (Const(id    ,_)) = id
wneuper@343
   205
  | id_of _                 = ""; (* never such an identifier *)
wneuper@343
   206
agriesma@308
   207
fun ids_of t =
agriesma@308
   208
  let fun con ss (Const (s,_)) = s::ss
agriesma@308
   209
	| con ss (Free (s,_)) = s::ss
agriesma@308
   210
	| con ss (Abs (s,_,b)) = s::(con ss b)
agriesma@308
   211
	| con ss (t1 $ t2) = (con ss t1) @ (con ss t2)
agriesma@308
   212
	| con ss _ = ss
agriesma@308
   213
  in map strip_thy ((distinct o (con [])) t) end;
agriesma@308
   214
(*
agriesma@308
   215
> val t = (term_of o the o (parse thy))
agriesma@308
   216
  "solve_univar (R, [univar, equation], no_met) (a = b + #1) a";
agriesma@308
   217
> ids_of t;
agriesma@308
   218
["solve'_univar","Pair","R","Cons","univar","equation","Nil",...]*)
agriesma@308
   219
agriesma@308
   220
agriesma@308
   221
fun overwritel (al, []) = al
agriesma@308
   222
  | overwritel (al, b::bl) = overwritel (overwrite (al, b), bl);
agriesma@308
   223
(*> val aaa = [(1,11),(2,22),(3,33)];
agriesma@308
   224
> overwritel (aaa, [(2,2222),(4,4444)]);
agriesma@308
   225
val it = [(1,11),(2,2222),(3,33),(4,4444)] : (int * int) list*)
agriesma@308
   226
wneuper@501
   227
agriesma@308
   228
local
agriesma@308
   229
fun intsto1 0 = []
agriesma@308
   230
  | intsto1 n = (intsto1 (n-1)) @ [n]
agriesma@308
   231
in
agriesma@308
   232
fun intsto n  = if n < 0 then (raise error "intsto < 0") else intsto1 n
agriesma@308
   233
end;
agriesma@308
   234
agriesma@308
   235
agriesma@308
   236
type 'a stack = 'a list;
agriesma@308
   237
fun top ((x::xs):'a stack) = x
agriesma@308
   238
  | top _ = raise error "top called for empty list";
agriesma@308
   239
fun pop ((x::xs):'a stack) = xs:'a stack
agriesma@308
   240
  | pop _ = raise error "pop called for empty list";
agriesma@308
   241
fun push x (xs:'a stack) = x::xs:'a stack;
agriesma@308
   242
agriesma@308
   243
agriesma@308
   244
fun drop_last l = ((rev o tl o rev) l);
wneuper@492
   245
fun drop_last_n n l = rev (takerest (n, rev l));
wneuper@492
   246
(*> drop_last_n 2 [1,2,3,4,5];
wneuper@492
   247
val it = [1, 2, 3] : int list
wneuper@492
   248
> drop_last_n 3 [1,2,3,4,5];
wneuper@492
   249
val it = [1, 2] : int list
wneuper@492
   250
> drop_last_n 7 [1,2,3,4,5];
wneuper@492
   251
val it = [] : int list
wneuper@492
   252
*)
agriesma@308
   253
agriesma@308
   254
fun bool2str true = "true"
agriesma@308
   255
  | bool2str false = "false";
agriesma@308
   256
wneuper@343
   257
(*.take elements from b to e including both.*)
wneuper@343
   258
fun take_fromto from to l = 
wneuper@343
   259
    if from > to then raise LIST ("take_fromto from="^string_of_int from^
wneuper@343
   260
				  " > to="^string_of_int to)
wneuper@343
   261
    else drop (from - 1, take (to, l));
wneuper@343
   262
(*> take_fromto 3 5 [1,2,3,4,5,6,7];
wneuper@343
   263
val it = [3,4,5] : int list 
wneuper@343
   264
> take_fromto 3 3  [1,2,3,4,5,6,7];
wneuper@343
   265
val it = [3] : int list*)
wneuper@343
   266
wneuper@365
   267
wneuper@366
   268
fun idt str 0 = " "
wneuper@366
   269
  | idt str n = str ^ idt str (n-1);
wneuper@366
   270
(*fun indt 0 = ""
wneuper@366
   271
  | indt n = " " ^ indt (n-1);---------does not terminate with negatives*)
wneuper@366
   272
fun indt n = if n <= 0 then "" else " " ^ indt (n-1);
wneuper@366
   273
fun indent 0 = ""
wneuper@366
   274
  | indent n = ". " ^ indent(n-1);
wneuper@366
   275
wneuper@365
   276
fun dashs i = if 0<i then "-"^ dashs (i-1) else "";
wneuper@366
   277
fun dots i = if 0<i then "."^ dots (i-1) else "";
wneuper@591
   278