src/Tools/Metis/src/ElementSet.sml
author paulson <lp15@cam.ac.uk>
Mon, 06 May 2024 14:39:33 +0100
changeset 80175 200107cdd3ac
parent 72004 913162a47d9f
permissions -rw-r--r--
Some new simprules – and patches for proofs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     1
(* ========================================================================= *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     2
(* FINITE SETS WITH A FIXED ELEMENT TYPE                                     *)
72004
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
     3
(* Copyright (c) 2004 Joe Leslie-Hurd, distributed under the BSD License     *)
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     4
(* ========================================================================= *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     5
43269
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
     6
functor ElementSet (
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
     7
  KM : KeyMap
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
     8
) :> ElementSet
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
     9
where type element = KM.key
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
    10
and type 'a map = 'a KM.map =
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    11
struct
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    12
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    13
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    14
(* A type of set elements.                                                   *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    15
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    16
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    17
type element = KM.key;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    18
45778
df6e210fb44c updated Metis to 20110926 version
blanchet
parents: 43269
diff changeset
    19
val compareElement = KM.compareKey;
df6e210fb44c updated Metis to 20110926 version
blanchet
parents: 43269
diff changeset
    20
df6e210fb44c updated Metis to 20110926 version
blanchet
parents: 43269
diff changeset
    21
val equalElement = KM.equalKey;
df6e210fb44c updated Metis to 20110926 version
blanchet
parents: 43269
diff changeset
    22
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    23
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    24
(* A type of finite sets.                                                    *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    25
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    26
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    27
type 'a map = 'a KM.map;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    28
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    29
datatype set = Set of unit map;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    30
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    31
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    32
(* Converting to and from maps.                                              *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    33
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    34
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    35
fun dest (Set m) = m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    36
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    37
fun mapPartial f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    38
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    39
      fun mf (elt,()) = f elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    40
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    41
      fn Set m => KM.mapPartial mf m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    42
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    43
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    44
fun map f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    45
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    46
      fun mf (elt,()) = f elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    47
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    48
      fn Set m => KM.map mf m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    49
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    50
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    51
fun domain m = Set (KM.transform (fn _ => ()) m);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    52
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    53
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    54
(* Constructors.                                                             *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    55
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    56
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    57
val empty = Set (KM.new ());
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    58
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    59
fun singleton elt = Set (KM.singleton (elt,()));
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    60
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    61
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    62
(* Set size.                                                                 *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    63
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    64
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    65
fun null (Set m) = KM.null m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    66
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    67
fun size (Set m) = KM.size m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    68
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    69
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    70
(* Querying.                                                                 *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    71
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    72
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    73
fun peek (Set m) elt =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    74
    case KM.peekKey m elt of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    75
      SOME (elt,()) => SOME elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    76
    | NONE => NONE;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    77
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    78
fun member elt (Set m) = KM.inDomain elt m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    79
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    80
fun pick (Set m) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    81
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    82
      val (elt,_) = KM.pick m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    83
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    84
      elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    85
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    86
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    87
fun nth (Set m) n =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    88
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    89
      val (elt,_) = KM.nth m n
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    90
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    91
      elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    92
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    93
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    94
fun random (Set m) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    95
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    96
      val (elt,_) = KM.random m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    97
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    98
      elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    99
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   100
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   101
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   102
(* Adding.                                                                   *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   103
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   104
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   105
fun add (Set m) elt =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   106
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   107
      val m = KM.insert m (elt,())
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   108
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   109
      Set m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   110
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   111
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   112
local
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   113
  fun uncurriedAdd (elt,set) = add set elt;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   114
in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   115
  fun addList set = List.foldl uncurriedAdd set;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   116
end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   117
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   118
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   119
(* Removing.                                                                 *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   120
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   121
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   122
fun delete (Set m) elt =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   123
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   124
      val m = KM.delete m elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   125
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   126
      Set m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   127
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   128
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   129
fun remove (Set m) elt =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   130
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   131
      val m = KM.remove m elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   132
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   133
      Set m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   134
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   135
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   136
fun deletePick (Set m) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   137
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   138
      val ((elt,()),m) = KM.deletePick m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   139
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   140
      (elt, Set m)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   141
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   142
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   143
fun deleteNth (Set m) n =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   144
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   145
      val ((elt,()),m) = KM.deleteNth m n
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   146
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   147
      (elt, Set m)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   148
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   149
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   150
fun deleteRandom (Set m) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   151
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   152
      val ((elt,()),m) = KM.deleteRandom m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   153
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   154
      (elt, Set m)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   155
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   156
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   157
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   158
(* Joining.                                                                  *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   159
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   160
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   161
fun union (Set m1) (Set m2) = Set (KM.unionDomain m1 m2);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   162
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   163
fun unionList sets =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   164
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   165
      val ms = List.map dest sets
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   166
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   167
      Set (KM.unionListDomain ms)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   168
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   169
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   170
fun intersect (Set m1) (Set m2) = Set (KM.intersectDomain m1 m2);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   171
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   172
fun intersectList sets =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   173
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   174
      val ms = List.map dest sets
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   175
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   176
      Set (KM.intersectListDomain ms)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   177
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   178
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   179
fun difference (Set m1) (Set m2) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   180
    Set (KM.differenceDomain m1 m2);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   181
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   182
fun symmetricDifference (Set m1) (Set m2) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   183
    Set (KM.symmetricDifferenceDomain m1 m2);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   184
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   185
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   186
(* Mapping and folding.                                                      *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   187
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   188
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   189
fun filter pred =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   190
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   191
      fun mpred (elt,()) = pred elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   192
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   193
      fn Set m => Set (KM.filter mpred m)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   194
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   195
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   196
fun partition pred =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   197
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   198
      fun mpred (elt,()) = pred elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   199
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   200
      fn Set m =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   201
         let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   202
           val (m1,m2) = KM.partition mpred m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   203
         in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   204
           (Set m1, Set m2)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   205
         end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   206
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   207
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   208
fun app f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   209
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   210
      fun mf (elt,()) = f elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   211
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   212
      fn Set m => KM.app mf m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   213
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   214
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   215
fun foldl f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   216
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   217
      fun mf (elt,(),acc) = f (elt,acc)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   218
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   219
      fn acc => fn Set m => KM.foldl mf acc m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   220
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   221
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   222
fun foldr f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   223
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   224
      fun mf (elt,(),acc) = f (elt,acc)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   225
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   226
      fn acc => fn Set m => KM.foldr mf acc m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   227
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   228
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   229
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   230
(* Searching.                                                                *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   231
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   232
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   233
fun findl p =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   234
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   235
      fun mp (elt,()) = p elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   236
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   237
      fn Set m =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   238
         case KM.findl mp m of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   239
           SOME (elt,()) => SOME elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   240
         | NONE => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   241
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   242
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   243
fun findr p =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   244
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   245
      fun mp (elt,()) = p elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   246
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   247
      fn Set m =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   248
         case KM.findr mp m of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   249
           SOME (elt,()) => SOME elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   250
         | NONE => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   251
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   252
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   253
fun firstl f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   254
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   255
      fun mf (elt,()) = f elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   256
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   257
      fn Set m => KM.firstl mf m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   258
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   259
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   260
fun firstr f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   261
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   262
      fun mf (elt,()) = f elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   263
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   264
      fn Set m => KM.firstr mf m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   265
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   266
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   267
fun exists p =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   268
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   269
      fun mp (elt,()) = p elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   270
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   271
      fn Set m => KM.exists mp m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   272
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   273
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   274
fun all p =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   275
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   276
      fun mp (elt,()) = p elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   277
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   278
      fn Set m => KM.all mp m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   279
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   280
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   281
fun count p =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   282
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   283
      fun mp (elt,()) = p elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   284
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   285
      fn Set m => KM.count mp m
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   286
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   287
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   288
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   289
(* Comparing.                                                                *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   290
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   291
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   292
fun compareValue ((),()) = EQUAL;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   293
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   294
fun equalValue () () = true;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   295
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   296
fun compare (Set m1, Set m2) = KM.compare compareValue (m1,m2);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   297
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   298
fun equal (Set m1) (Set m2) = KM.equal equalValue m1 m2;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   299
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   300
fun subset (Set m1) (Set m2) = KM.subsetDomain m1 m2;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   301
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   302
fun disjoint (Set m1) (Set m2) = KM.disjointDomain m1 m2;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   303
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   304
(* ------------------------------------------------------------------------- *)
72004
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   305
(* Pointwise operations.                                                     *)
43269
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   306
(* ------------------------------------------------------------------------- *)
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   307
72004
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   308
fun lift f =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   309
    let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   310
      fun inc (elt,set) = union set (f elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   311
    in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   312
      foldl inc empty
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   313
    end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   314
43269
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   315
fun closedAdd f =
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   316
    let
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   317
      fun adds acc set = foldl check acc set
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   318
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   319
      and check (elt,acc) =
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   320
          if member elt acc then acc
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   321
          else expand (add acc elt) elt
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   322
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   323
      and expand acc elt = adds acc (f elt)
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   324
    in
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   325
      adds
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   326
    end;
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   327
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   328
fun close f = closedAdd f empty;
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   329
3535f16d9714 new Metis version
blanchet
parents: 39502
diff changeset
   330
(* ------------------------------------------------------------------------- *)
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   331
(* Converting to and from lists.                                             *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   332
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   333
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   334
fun transform f =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   335
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   336
      fun inc (x,l) = f x :: l
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   337
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   338
      foldr inc []
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   339
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   340
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   341
fun toList (Set m) = KM.keys m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   342
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   343
fun fromList elts = addList empty elts;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   344
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   345
(* ------------------------------------------------------------------------- *)
72004
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   346
(* Depth-first search.                                                       *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   347
(* ------------------------------------------------------------------------- *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   348
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   349
datatype ordering =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   350
    Linear of element list
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   351
  | Cycle of element list;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   352
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   353
fun postOrdered children =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   354
    let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   355
      fun check acc elts =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   356
          case elts of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   357
            [] => true
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   358
          | elt :: elts =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   359
            not (member elt acc) andalso
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   360
            let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   361
              val acc = closedAdd children acc (singleton elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   362
            in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   363
              check acc elts
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   364
            end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   365
    in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   366
      check empty
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   367
    end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   368
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   369
fun preOrdered children elts = postOrdered children (List.rev elts);
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   370
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   371
local
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   372
  fun takeStackset elt =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   373
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   374
        fun notElement (e,_,_) = not (equalElement e elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   375
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   376
        Useful.takeWhile notElement
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   377
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   378
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   379
  fun consElement ((e,_,_),el) = e :: el;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   380
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   381
  fun depthFirstSearch children =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   382
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   383
        fun traverse (dealt,dealtset) (stack,stackset) work =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   384
            case work of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   385
              [] =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   386
              (case stack of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   387
                 [] => Linear dealt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   388
               | (elt,work,stackset) :: stack =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   389
                 let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   390
                   val dealt = elt :: dealt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   391
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   392
                   val dealtset = add dealtset elt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   393
                 in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   394
                   traverse (dealt,dealtset) (stack,stackset) work
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   395
                 end)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   396
            | elt :: work =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   397
              if member elt dealtset then
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   398
                traverse (dealt,dealtset) (stack,stackset) work
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   399
              else if member elt stackset then
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   400
                let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   401
                  val cycle = takeStackset elt stack
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   402
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   403
                  val cycle = elt :: List.foldl consElement [elt] cycle
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   404
                in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   405
                  Cycle cycle
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   406
                end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   407
              else
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   408
                let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   409
                  val stack = (elt,work,stackset) :: stack
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   410
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   411
                  val stackset = add stackset elt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   412
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   413
                  val work = toList (children elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   414
                in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   415
                  traverse (dealt,dealtset) (stack,stackset) work
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   416
                end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   417
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   418
        val dealt = []
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   419
        and dealtset = empty
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   420
        and stack = []
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   421
        and stackset = empty
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   422
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   423
        traverse (dealt,dealtset) (stack,stackset)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   424
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   425
in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   426
  fun preOrder children roots =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   427
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   428
        val result = depthFirstSearch children (toList roots)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   429
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   430
(*BasicDebug
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   431
        val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   432
            case result of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   433
              Cycle _ => ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   434
            | Linear l =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   435
              let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   436
                val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   437
                    if subset roots (fromList l) then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   438
                    else raise Useful.Bug "ElementSet.preOrder: missing roots"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   439
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   440
                val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   441
                    if preOrdered children l then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   442
                    else raise Useful.Bug "ElementSet.preOrder: bad ordering"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   443
              in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   444
                ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   445
              end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   446
*)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   447
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   448
        result
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   449
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   450
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   451
  fun postOrder children roots =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   452
      case depthFirstSearch children (toList roots) of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   453
        Linear l =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   454
        let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   455
          val l = List.rev l
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   456
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   457
(*BasicDebug
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   458
          val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   459
              if subset roots (fromList l) then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   460
              else raise Useful.Bug "ElementSet.postOrder: missing roots"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   461
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   462
          val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   463
              if postOrdered children l then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   464
              else raise Useful.Bug "ElementSet.postOrder: bad ordering"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   465
*)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   466
        in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   467
          Linear l
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   468
        end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   469
      | cycle => cycle;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   470
end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   471
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   472
(* ------------------------------------------------------------------------- *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   473
(* Strongly connected components.                                            *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   474
(* ------------------------------------------------------------------------- *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   475
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   476
fun postOrderedSCC children =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   477
    let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   478
      fun check acc eltsl =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   479
          case eltsl of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   480
            [] => true
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   481
          | elts :: eltsl =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   482
            not (null elts) andalso
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   483
            disjoint elts acc andalso
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   484
            let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   485
              fun addElt elt = closedAdd children acc (singleton elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   486
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   487
              val (root,elts) = deletePick elts
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   488
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   489
              fun checkElt elt = member root (addElt elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   490
            in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   491
              all checkElt elts andalso
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   492
              let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   493
                val acc = addElt root
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   494
              in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   495
                subset elts acc andalso
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   496
                check acc eltsl
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   497
              end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   498
            end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   499
    in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   500
      check empty
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   501
    end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   502
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   503
fun preOrderedSCC children eltsl = postOrderedSCC children (List.rev eltsl);
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   504
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   505
(* An implementation of Tarjan's algorithm: *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   506
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   507
(* http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm *)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   508
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   509
local
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   510
  datatype stackSCC = StackSCC of set * (element * set) list;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   511
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   512
  val emptyStack = StackSCC (empty,[]);
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   513
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   514
  fun pushStack (StackSCC (elts,eltl)) elt =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   515
      StackSCC (add elts elt, (elt,elts) :: eltl);
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   516
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   517
  fun inStack elt (StackSCC (elts,_)) = member elt elts;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   518
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   519
  fun popStack root =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   520
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   521
        fun pop scc eltl =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   522
            case eltl of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   523
              [] => raise Useful.Bug "ElementSet.popStack"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   524
            | (elt,elts) :: eltl =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   525
              let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   526
                val scc = add scc elt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   527
              in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   528
                if equalElement elt root then (scc, StackSCC (elts,eltl))
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   529
                else pop scc eltl
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   530
              end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   531
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   532
        fn sccs => fn StackSCC (_,eltl) =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   533
           let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   534
             val (scc,stack) = pop empty eltl
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   535
           in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   536
             (scc :: sccs, stack)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   537
           end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   538
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   539
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   540
  fun getIndex indices e : int =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   541
      case KM.peek indices e of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   542
        SOME i => i
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   543
      | NONE => raise Useful.Bug "ElementSet.getIndex";
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   544
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   545
  fun isRoot indices lows e = getIndex indices e = getIndex lows e;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   546
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   547
  fun reduceIndex indices (e,i) =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   548
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   549
        val j = getIndex indices e
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   550
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   551
        if j <= i then indices else KM.insert indices (e,i)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   552
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   553
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   554
  fun tarjan children =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   555
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   556
        fun dfsVertex sccs callstack index indices lows stack elt =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   557
            let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   558
              val indices = KM.insert indices (elt,index)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   559
              and lows = KM.insert lows (elt,index)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   560
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   561
              val index = index + 1
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   562
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   563
              val stack = pushStack stack elt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   564
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   565
              val chil = toList (children elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   566
            in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   567
              dfsSuccessors sccs callstack index indices lows stack elt chil
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   568
            end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   569
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   570
        and dfsSuccessors sccs callstack index indices lows stack elt chil =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   571
            case chil of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   572
              [] =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   573
              let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   574
                val (sccs,stack) =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   575
                    if isRoot indices lows elt then popStack elt sccs stack
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   576
                    else (sccs,stack)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   577
              in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   578
                case callstack of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   579
                  [] => (sccs,index,indices,lows)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   580
                | (p,elts) :: callstack =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   581
                  let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   582
                    val lows = reduceIndex lows (p, getIndex lows elt)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   583
                  in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   584
                    dfsSuccessors sccs callstack index indices lows stack p elts
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   585
                  end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   586
              end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   587
            | c :: chil =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   588
              case KM.peek indices c of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   589
                NONE =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   590
                let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   591
                  val callstack = (elt,chil) :: callstack
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   592
                in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   593
                  dfsVertex sccs callstack index indices lows stack c
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   594
                end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   595
              | SOME cind =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   596
                let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   597
                  val lows =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   598
                      if inStack c stack then reduceIndex lows (elt,cind)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   599
                      else lows
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   600
                in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   601
                  dfsSuccessors sccs callstack index indices lows stack elt chil
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   602
                end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   603
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   604
        fun dfsRoots sccs index indices lows elts =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   605
            case elts of
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   606
              [] => sccs
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   607
            | elt :: elts =>
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   608
              if KM.inDomain elt indices then
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   609
                dfsRoots sccs index indices lows elts
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   610
              else
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   611
                let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   612
                  val callstack = []
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   613
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   614
                  val (sccs,index,indices,lows) =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   615
                      dfsVertex sccs callstack index indices lows emptyStack elt
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   616
                in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   617
                  dfsRoots sccs index indices lows elts
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   618
                end
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   619
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   620
        val sccs = []
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   621
        and index = 0
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   622
        and indices = KM.new ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   623
        and lows = KM.new ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   624
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   625
        dfsRoots sccs index indices lows
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   626
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   627
in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   628
  fun preOrderSCC children roots =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   629
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   630
        val result = tarjan children (toList roots)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   631
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   632
(*BasicDebug
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   633
        val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   634
            if subset roots (unionList result) then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   635
            else raise Useful.Bug "ElementSet.preOrderSCC: missing roots"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   636
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   637
        val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   638
            if preOrderedSCC children result then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   639
            else raise Useful.Bug "ElementSet.preOrderSCC: bad ordering"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   640
*)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   641
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   642
        result
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   643
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   644
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   645
  fun postOrderSCC children roots =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   646
      let
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   647
        val result = List.rev (tarjan children (toList roots))
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   648
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   649
(*BasicDebug
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   650
        val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   651
            if subset roots (unionList result) then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   652
            else raise Useful.Bug "ElementSet.postOrderSCC: missing roots"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   653
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   654
        val () =
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   655
            if postOrderedSCC children result then ()
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   656
            else raise Useful.Bug "ElementSet.postOrderSCC: bad ordering"
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   657
*)
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   658
      in
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   659
        result
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   660
      end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   661
end;
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   662
913162a47d9f Update Metis to 2.4
desharna
parents: 45778
diff changeset
   663
(* ------------------------------------------------------------------------- *)
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   664
(* Pretty-printing.                                                          *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   665
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   666
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   667
fun toString set =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   668
    "{" ^ (if null set then "" else Int.toString (size set)) ^ "}";
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   669
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   670
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   671
(* Iterators over sets                                                       *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   672
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   673
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   674
type iterator = unit KM.iterator;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   675
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   676
fun mkIterator (Set m) = KM.mkIterator m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   677
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   678
fun mkRevIterator (Set m) = KM.mkRevIterator m;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   679
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   680
fun readIterator iter =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   681
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   682
      val (elt,()) = KM.readIterator iter
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   683
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   684
      elt
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   685
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   686
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   687
fun advanceIterator iter = KM.advanceIterator iter;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   688
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   689
end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   690
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   691
structure IntSet =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   692
ElementSet (IntMap);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   693
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   694
structure IntPairSet =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   695
ElementSet (IntPairMap);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   696
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   697
structure StringSet =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   698
ElementSet (StringMap);