src/Pure/library.ML
changeset 23424 d0580634f128
parent 23251 471b576aad25
child 23718 8ff68cb5860c
equal deleted inserted replaced
23423:b2d64f86d21b 23424:d0580634f128
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Author:     Markus Wenzel, TU Muenchen
     4     Author:     Markus Wenzel, TU Muenchen
     5 
     5 
     6 Basic library: functions, options, pairs, booleans, lists, integers,
     6 Basic library: functions, options, pairs, booleans, lists, integers,
     7 strings, lists as sets, balanced trees, orders, current directory, misc.
     7 strings, lists as sets, orders, current directory, misc.
     8 
     8 
     9 See also General/basics.ML for the most fundamental concepts.
     9 See also General/basics.ML for the most fundamental concepts.
    10 *)
    10 *)
    11 
    11 
    12 infix 1 |>>>
    12 infix 1 |>>>
   192 
   192 
   193   (*lists as multisets*)
   193   (*lists as multisets*)
   194   val remove1: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
   194   val remove1: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
   195   val submultiset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   195   val submultiset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   196 
   196 
   197   (*balanced trees*)
       
   198   exception Balance
       
   199   val fold_bal: ('a * 'a -> 'a) -> 'a list -> 'a
       
   200   val access_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> int -> 'a
       
   201   val accesses_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> 'a list
       
   202 
       
   203   (*orders*)
   197   (*orders*)
   204   val is_equal: order -> bool
   198   val is_equal: order -> bool
   205   val rev_order: order -> order
   199   val rev_order: order -> order
   206   val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
   200   val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
   207   val int_ord: int * int -> order
   201   val int_ord: int * int -> order
   915 fun remove1 _ _ [] = raise Empty
   909 fun remove1 _ _ [] = raise Empty
   916   | remove1 eq y (x::xs) = if eq (y, x) then xs else x :: remove1 eq y xs;
   910   | remove1 eq y (x::xs) = if eq (y, x) then xs else x :: remove1 eq y xs;
   917 
   911 
   918 fun submultiset _ ([], _)  = true
   912 fun submultiset _ ([], _)  = true
   919   | submultiset eq (x :: xs, ys) = member eq ys x andalso submultiset eq (xs, remove1 eq x ys);
   913   | submultiset eq (x :: xs, ys) = member eq ys x andalso submultiset eq (xs, remove1 eq x ys);
   920 
       
   921 
       
   922 
       
   923 (** balanced trees **)
       
   924 
       
   925 exception Balance;      (*indicates non-positive argument to balancing fun*)
       
   926 
       
   927 (*balanced folding; avoids deep nesting*)
       
   928 fun fold_bal f [x] = x
       
   929   | fold_bal f [] = raise Balance
       
   930   | fold_bal f xs =
       
   931       let val (ps, qs) = chop (length xs div 2) xs
       
   932       in  f (fold_bal f ps, fold_bal f qs)  end;
       
   933 
       
   934 (*construct something of the form f(...g(...(x)...)) for balanced access*)
       
   935 fun access_bal (f, g, x) n i =
       
   936   let fun acc n i =     (*1<=i<=n*)
       
   937           if n=1 then x else
       
   938           let val n2 = n div 2
       
   939           in  if i<=n2 then f (acc n2 i)
       
   940                        else g (acc (n-n2) (i-n2))
       
   941           end
       
   942   in  if 1<=i andalso i<=n then acc n i else raise Balance  end;
       
   943 
       
   944 (*construct ALL such accesses; could try harder to share recursive calls!*)
       
   945 fun accesses_bal (f, g, x) n =
       
   946   let fun acc n =
       
   947           if n=1 then [x] else
       
   948           let val n2 = n div 2
       
   949               val acc2 = acc n2
       
   950           in  if n-n2=n2 then map f acc2 @ map g acc2
       
   951                          else map f acc2 @ map g (acc (n-n2)) end
       
   952   in  if 1<=n then acc n else raise Balance  end;
       
   953 
   914 
   954 
   915 
   955 
   916 
   956 (** orders **)
   917 (** orders **)
   957 
   918