src/Pure/library.ML
changeset 18441 7488d8ea61bc
parent 18427 b7ee916ae3ec
child 18450 e57731ba01dd
equal deleted inserted replaced
18440:72ee07f4b56b 18441:7488d8ea61bc
    11 infix 1 |> |-> ||> ||>> |>> |>>> #> #->;
    11 infix 1 |> |-> ||> ||>> |>> |>>> #> #->;
    12 infix 2 ?;
    12 infix 2 ?;
    13 infix 3 o oo ooo oooo;
    13 infix 3 o oo ooo oooo;
    14 
    14 
    15 infix 4 ~~ upto downto;
    15 infix 4 ~~ upto downto;
    16 infix orf andf prefix \ \\ ins ins_string ins_int mem mem_int mem_string union union_int
    16 infix orf andf \ \\ ins ins_string ins_int mem mem_int mem_string union union_int
    17   union_string inter inter_int inter_string subset subset_int subset_string;
    17   union_string inter inter_int inter_string subset subset_int subset_string;
    18 
    18 
    19 signature BASIC_LIBRARY =
    19 signature BASIC_LIBRARY =
    20 sig
    20 sig
    21   (*functions*)
    21   (*functions*)
    99   val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
    99   val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
   100   val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
   100   val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
   101   val fold_index: (int * 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b
   101   val fold_index: (int * 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b
   102   val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
   102   val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
   103   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
   103   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
   104   val dig: ('a list -> 'a list) -> 'a list list -> 'a list list
   104   val burrow: ('a list -> 'b list) -> 'a list list -> 'b list list
   105   val unflat: 'a list list -> 'b list -> 'b list list
   105   val unflat: 'a list list -> 'b list -> 'b list list
   106   val splitAt: int * 'a list -> 'a list * 'a list
   106   val splitAt: int * 'a list -> 'a list * 'a list
   107   val dropwhile: ('a -> bool) -> 'a list -> 'a list
   107   val dropwhile: ('a -> bool) -> 'a list -> 'a list
   108   val nth: 'a list -> int -> 'a
   108   val nth: 'a list -> int -> 'a
   109   val nth_update: int * 'a -> 'a list -> 'a list
   109   val nth_update: int * 'a -> 'a list -> 'a list
   123   val multiply: 'a list -> 'a list list -> 'a list list
   123   val multiply: 'a list -> 'a list list -> 'a list list
   124   val product: 'a list -> 'b list -> ('a * 'b) list
   124   val product: 'a list -> 'b list -> ('a * 'b) list
   125   val filter: ('a -> bool) -> 'a list -> 'a list
   125   val filter: ('a -> bool) -> 'a list -> 'a list
   126   val filter_out: ('a -> bool) -> 'a list -> 'a list
   126   val filter_out: ('a -> bool) -> 'a list -> 'a list
   127   val equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   127   val equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   128   val prefix: ''a list * ''a list -> bool
   128   val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool
   129   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
   129   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
   130   val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
   130   val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
   131   val prefixes1: 'a list -> 'a list list
   131   val prefixes1: 'a list -> 'a list list
   132   val suffixes1: 'a list -> 'a list list
   132   val suffixes1: 'a list -> 'a list list
   133 
   133 
   594       let val (ps,qs) = splitAt(length xs,ys)
   594       let val (ps,qs) = splitAt(length xs,ys)
   595       in ps :: unflat xss qs end
   595       in ps :: unflat xss qs end
   596   | unflat [] [] = []
   596   | unflat [] [] = []
   597   | unflat _ _ = raise UnequalLengths;
   597   | unflat _ _ = raise UnequalLengths;
   598 
   598 
   599 fun dig f xss =
   599 fun burrow f xss =
   600   unflat xss ((f o flat) xss);
   600   unflat xss ((f o flat) xss);
   601 
   601 
   602 
   602 
   603 (*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates
   603 (*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates
   604   (proc x1; ...; proc xn) for side effects*)
   604   (proc x1; ...; proc xn) for side effects*)
   670   in length xs = length ys andalso eq' xs ys end;
   670   in length xs = length ys andalso eq' xs ys end;
   671 
   671 
   672 
   672 
   673 (* prefixes, suffixes *)
   673 (* prefixes, suffixes *)
   674 
   674 
   675 fun [] prefix _ = true
   675 fun is_prefix _ [] _ = true
   676   | (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys)
   676   | is_prefix eq (x :: xs) (y :: ys) = eq (x, y) andalso is_prefix eq xs ys
   677   | _ prefix _ = false;
   677   | is_prefix eq _ _ = false;
   678 
   678 
   679 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., x(i-1)], [xi, ..., xn])
   679 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., x(i-1)], [xi, ..., xn])
   680    where xi is the first element that does not satisfy the predicate*)
   680    where xi is the first element that does not satisfy the predicate*)
   681 fun take_prefix (pred : 'a -> bool)  (xs: 'a list) : 'a list * 'a list =
   681 fun take_prefix (pred : 'a -> bool)  (xs: 'a list) : 'a list * 'a list =
   682   let fun take (rxs, []) = (rev rxs, [])
   682   let fun take (rxs, []) = (rev rxs, [])