src/Tools/isac/BaseDefinitions/libraryC.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 11 Jan 2023 11:38:01 +0100
changeset 60650 06ec8abfd3bc
parent 60388 51ef69967865
child 60681 dcc82831573a
permissions -rw-r--r--
eliminate use of Thy_Info 12: TermC partially
     1 (* library extending Isabelle's library.
     2    Author: Walther Neuper 1999
     3    (c) copyright due to lincense terms
     4 
     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.
     7 TODO:
     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.
    13 *)
    14 
    15 infix 1 ~~~
    16 
    17 signature LIBRARY_C =
    18 sig
    19   val and_: bool * bool -> bool
    20   val cand_: bool -> bool -> bool
    21   val or_: bool * bool -> bool
    22   val cor_: bool -> bool -> bool
    23 
    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
    58 
    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
    64 
    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
    72 
    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
    82 
    83   val linefeed: string -> string
    84   val dropwhile' : ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b list
    85 \<^isac_test>\<open>
    86   val enumerate_strings: string list -> string list
    87   val quad2pair: 'a * 'b * 'c * 'd -> 'a * 'b
    88 \<close>
    89 
    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 -----------------------------------------------///*)
    99 end;                                                      
   100 
   101 (**)
   102 structure LibraryC(**): LIBRARY_C(**) =
   103 struct
   104 (**)
   105 
   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;
   110 
   111 fun last_elem [] = raise ERROR "last_elem"
   112   | last_elem [x] = x
   113   | last_elem (_ :: xs) = last_elem xs;
   114 fun member_swap eq x xs = member eq xs x
   115 
   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;
   119 
   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*)
   122   | nth 1 (x :: _) = x
   123   | nth n (_ :: xs) = nth (n - 1) xs;
   124 
   125 fun list_update [] _ _ = []
   126   | list_update (x :: xs) i v = 
   127       case i of 
   128         0 => v :: xs
   129       | j => x :: list_update xs (j - 1) v
   130 
   131 fun drop_nth ls (_, []) = ls
   132   | drop_nth ls (n, x :: xs) = 
   133     if n = 1 
   134     then ls @ xs
   135     else drop_nth (ls @ [x]) (n - 1, xs);
   136 
   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;
   141 
   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));
   154 
   155 fun pair2tri ((a,b),c) = (a,b,c);
   156 fun fst3 (a,_,_) = a;
   157 fun snd3 (_,b,_) = b;
   158 fun thd3 (_,_,c) = c;
   159 
   160 fun de_quote str =
   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 ", ";
   166 
   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;
   174 
   175 fun spair2str (s1, s2) =   "(" ^ quote s1  ^ ", " ^ quote s2 ^ ")";
   176 fun pair2str_ (s1, s2) =   s1 ^ "#" ^ s2;
   177 fun pair2str (s1, s2) =   "(" ^ s1 ^ ", " ^ s2 ^ ")";
   178 
   179 val int2str = Library.string_of_int;
   180 fun ints2str' ints = (strs2str' o (map string_of_int)) ints;
   181 
   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)
   185         | over [] = [p]
   186   in over al end;
   187 fun overwritel (al, []) = al
   188   | overwritel (al, b::bl) = overwritel (overwrite (al, b), bl);
   189 
   190 (* TODO replace by Library.upto *)
   191 local
   192 fun intsto1 0 = []
   193   | intsto1 n = (intsto1 (n - 1)) @ [n]
   194 in
   195 fun intsto n  = if n < 0 then (raise ERROR "intsto < 0") else intsto1 n
   196 end;
   197 
   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)
   203 
   204 (* take elements from b to e including both *)
   205 fun take_fromto from to l = 
   206   if from > to
   207   then raise ERROR ("take_fromto from=" ^ string_of_int from ^ " > to=" ^ string_of_int to)
   208   else drop (from - 1, take (to, l));
   209 
   210 fun idt _ 0 = "@"
   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 ". ") ""
   214 
   215 fun dashs i = if 0 < i then "-" ^ dashs (i - 1) else "";
   216 fun dots i = if 0 < i then "." ^ dots (i - 1) else "";
   217 
   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;
   227 
   228 fun triple2pair (a, b, _) = (a, b);
   229 \<^isac_test>\<open>
   230 fun quad2pair (a, b, _, _) = (a, b);
   231 \<close>
   232 
   233 (* append a counter to a string list *)
   234 \<^isac_test>\<open>
   235 fun enumerate_strings strs =
   236   let fun enum _ [] = []
   237         | enum i (s :: ss) = (s ^ "--" ^ string_of_int i) :: (enum (i + 1) ss)
   238   in enum 1 strs end
   239 \<close>
   240 
   241 fun maxl [] = raise ERROR "maxl of []"
   242   | maxl (y :: ys) =
   243     let
   244       fun mx x [] = x
   245         | mx x (y :: ys) = if x < (y: int) then mx y ys else mx x ys
   246   in mx y ys end
   247 
   248 fun xs ~~~ 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
   253     in aaa [] xs ys end;
   254 
   255 (*///------------------------------>>> thy ------------------------------------------------\\\*)
   256 fun get_thy str = 
   257   let
   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;
   262 
   263 
   264 fun strip_thy str =
   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 ------------------------------------------------///*)
   270     
   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 *)
   276 
   277 fun ids_of t =
   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)
   282 	| con ss _ = ss
   283   in map strip_thy (((distinct op =) o (con [])) t) end;
   284 
   285 fun subs2str (subs: string list) = list2str  subs;
   286 (*> val sss = ["(''bdv'',x)", "(err,#0)"];
   287 > subs2str sss;
   288 val it = "[(bdv,x),(err,#0)]" : string*)
   289 (*\\\------------------------------>>>  term ----------------------------------------------///*)
   290 
   291 fun termless tu = (Term_Ord.term_ord tu = LESS);
   292 
   293 val linefeed = curry op ^ "\n"; (* ?\<longrightarrow> libraryC ?*)
   294 
   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) =
   298     if equal f1 i1 then
   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] =
   303     if equal f i then
   304       (rev (f::fs), [i])
   305     else raise ERROR "dropwhile': did not start with equal elements"
   306   | dropwhile' equal [f] (i::is) =
   307     if equal f i then
   308       ([f], i::is)
   309     else raise ERROR "dropwhile': did not start with equal elements"
   310   | dropwhile' _ _ _ = raise ERROR "dropwhile': uncovered case"
   311 
   312 end; (*struct*) open LibraryC