| 16464 |      1 | (*  Title:      Pure/General/ord_list.ML
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Makarius
 | 
|  |      4 | 
 | 
|  |      5 | Ordered lists without duplicates -- a light-weight representation of
 | 
| 16497 |      6 | finite sets, all operations take linear time and economize heap usage.
 | 
| 16464 |      7 | *)
 | 
|  |      8 | 
 | 
|  |      9 | signature ORD_LIST =
 | 
|  |     10 | sig
 | 
| 28354 |     11 |   type 'a T = 'a list
 | 
| 28626 |     12 |   val make: ('a * 'a -> order) -> 'a list -> 'a T
 | 
| 28354 |     13 |   val member: ('b * 'a -> order) -> 'a T -> 'b -> bool
 | 
|  |     14 |   val insert: ('a * 'a -> order) -> 'a -> 'a T -> 'a T
 | 
|  |     15 |   val remove: ('b * 'a -> order) -> 'b -> 'a T -> 'a T
 | 
|  |     16 |   val subset: ('b * 'a -> order) -> 'b T * 'a T -> bool
 | 
|  |     17 |   val union: ('a * 'a -> order) -> 'a T -> 'a T -> 'a T
 | 
|  |     18 |   val inter: ('b * 'a -> order) -> 'b T -> 'a T -> 'a T
 | 
|  |     19 |   val subtract: ('b * 'a -> order) -> 'b T -> 'a T -> 'a T
 | 
| 16464 |     20 | end;
 | 
|  |     21 | 
 | 
|  |     22 | structure OrdList: ORD_LIST =
 | 
|  |     23 | struct
 | 
|  |     24 | 
 | 
| 28354 |     25 | type 'a T = 'a list;
 | 
| 28626 |     26 | fun make ord = sort_distinct ord;
 | 
| 28354 |     27 | 
 | 
| 16511 |     28 | 
 | 
|  |     29 | (* single elements *)
 | 
|  |     30 | 
 | 
|  |     31 | fun find_index ord list x =
 | 
|  |     32 |   let
 | 
| 16811 |     33 |     fun find i [] = ~ i
 | 
| 16511 |     34 |       | find i (y :: ys) =
 | 
|  |     35 |           (case ord (x, y) of
 | 
| 16811 |     36 |             LESS => ~ i
 | 
| 16511 |     37 |           | EQUAL => i
 | 
|  |     38 |           | GREATER => find (i + 1) ys);
 | 
| 16811 |     39 |   in find 1 list end;
 | 
| 16497 |     40 | 
 | 
| 16811 |     41 | fun member ord list x = find_index ord list x > 0;
 | 
| 16464 |     42 | 
 | 
|  |     43 | fun insert ord x list =
 | 
|  |     44 |   let
 | 
| 16811 |     45 |     fun insrt 1 ys = x :: ys
 | 
| 16511 |     46 |       | insrt i (y :: ys) = y :: insrt (i - 1) ys;
 | 
| 16811 |     47 |     val idx = find_index ord list x;
 | 
|  |     48 |   in if idx > 0 then list else insrt (~ idx) list end;
 | 
| 16464 |     49 | 
 | 
|  |     50 | fun remove ord x list =
 | 
|  |     51 |   let
 | 
| 16811 |     52 |     fun rmove 1 (_ :: ys) = ys
 | 
| 16511 |     53 |       | rmove i (y :: ys) = y :: rmove (i - 1) ys;
 | 
| 16811 |     54 |     val idx = find_index ord list x;
 | 
|  |     55 |   in if idx > 0 then rmove idx list else list end;
 | 
| 16511 |     56 | 
 | 
|  |     57 | 
 | 
|  |     58 | (* lists as sets *)
 | 
|  |     59 | 
 | 
|  |     60 | nonfix subset;
 | 
|  |     61 | fun subset ord (list1, list2) =
 | 
|  |     62 |   let
 | 
|  |     63 |     fun sub [] _ = true
 | 
|  |     64 |       | sub _ [] = false
 | 
|  |     65 |       | sub (lst1 as x :: xs) (y :: ys) =
 | 
| 16464 |     66 |           (case ord (x, y) of
 | 
| 16511 |     67 |             LESS => false
 | 
|  |     68 |           | EQUAL => sub xs ys
 | 
|  |     69 |           | GREATER => sub lst1 ys);
 | 
|  |     70 |   in sub list1 list2 end;
 | 
|  |     71 | 
 | 
|  |     72 | 
 | 
|  |     73 | (* algebraic operations *)
 | 
|  |     74 | 
 | 
|  |     75 | exception SAME;
 | 
|  |     76 | fun handle_same f x = f x handle SAME => x;
 | 
| 16464 |     77 | 
 | 
| 16497 |     78 | (*union: insert elements of first list into second list*)
 | 
| 16464 |     79 | nonfix union;
 | 
| 16497 |     80 | fun union ord list1 list2 =
 | 
|  |     81 |   let
 | 
|  |     82 |     fun unio [] _ = raise SAME
 | 
|  |     83 |       | unio xs [] = xs
 | 
|  |     84 |       | unio (lst1 as x :: xs) (lst2 as y :: ys) =
 | 
|  |     85 |           (case ord (x, y) of
 | 
|  |     86 |             LESS => x :: handle_same (unio xs) lst2
 | 
|  |     87 |           | EQUAL => y :: unio xs ys
 | 
|  |     88 |           | GREATER => y :: unio lst1 ys);
 | 
| 16886 |     89 |   in handle_same (unio list1) list2 end;
 | 
| 16464 |     90 | 
 | 
| 16497 |     91 | (*intersection: filter second list for elements present in first list*)
 | 
| 16464 |     92 | nonfix inter;
 | 
| 16497 |     93 | fun inter ord list1 list2 =
 | 
|  |     94 |   let
 | 
|  |     95 |     fun intr _ [] = raise SAME
 | 
|  |     96 |       | intr [] _ = []
 | 
|  |     97 |       | intr (lst1 as x :: xs) (lst2 as y :: ys) =
 | 
|  |     98 |           (case ord (x, y) of
 | 
|  |     99 |             LESS => intr xs lst2
 | 
|  |    100 |           | EQUAL => y :: intr xs ys
 | 
|  |    101 |           | GREATER => handle_same (intr lst1) ys);
 | 
|  |    102 |   in handle_same (intr list1) list2 end;
 | 
|  |    103 | 
 | 
|  |    104 | (*subtraction: filter second list for elements NOT present in first list*)
 | 
|  |    105 | fun subtract ord list1 list2 =
 | 
|  |    106 |   let
 | 
|  |    107 |     fun subtr [] _ = raise SAME
 | 
|  |    108 |       | subtr _ [] = raise SAME
 | 
|  |    109 |       | subtr (lst1 as x :: xs) (lst2 as y :: ys) =
 | 
|  |    110 |           (case ord (x, y) of
 | 
|  |    111 |             LESS => subtr xs lst2
 | 
|  |    112 |           | EQUAL => handle_same (subtr xs) ys
 | 
|  |    113 |           | GREATER => y :: subtr lst1 ys);
 | 
|  |    114 |   in handle_same (subtr list1) list2 end;
 | 
| 16464 |    115 | 
 | 
|  |    116 | end;
 |