src/Pure/General/alist.ML
author wenzelm
Thu Jul 19 23:18:48 2007 +0200 (2007-07-19)
changeset 23863 8f3099589cfa
parent 22319 6f162dd72f60
child 29606 fedb8be05f24
permissions -rw-r--r--
tuned signature;
haftmann@17152
     1
(*  Title:      Pure/General/alist.ML
haftmann@17152
     2
    ID:         $Id$
haftmann@17152
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@17152
     4
haftmann@19454
     5
Association lists -- lists of (key, value) pairs.
haftmann@17152
     6
*)
haftmann@17152
     7
haftmann@17152
     8
signature ALIST =
haftmann@17152
     9
sig
haftmann@17766
    10
  exception DUP
haftmann@17152
    11
  val lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option
haftmann@17152
    12
  val defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool
haftmann@17152
    13
  val update: ('a * 'a -> bool) -> ('a * 'b)
haftmann@17152
    14
    -> ('a * 'b) list -> ('a * 'b) list
haftmann@17152
    15
  val default: ('a * 'a -> bool) -> ('a * 'b)
haftmann@17152
    16
    -> ('a * 'b) list -> ('a * 'b) list
haftmann@17152
    17
  val delete: ('a * 'b -> bool) -> 'a
haftmann@17152
    18
    -> ('b * 'c) list -> ('b * 'c) list
haftmann@17152
    19
  val map_entry: ('a * 'b -> bool) -> 'a -> ('c -> 'c)
haftmann@17152
    20
    -> ('b * 'c) list -> ('b * 'c) list
haftmann@18883
    21
  val map_entry_yield: ('a * 'b -> bool) -> 'a -> ('c -> 'd * 'c)
haftmann@18883
    22
    -> ('b * 'c) list -> 'd option * ('b * 'c) list
haftmann@20166
    23
  val map_default: ('a * 'a -> bool) -> 'a * 'b -> ('b -> 'b)
haftmann@20166
    24
    -> ('a * 'b) list -> ('a * 'b) list
haftmann@20142
    25
  val join: ('a * 'a -> bool) -> ('a -> 'b * 'b -> 'b) (*exception DUP*)
haftmann@22319
    26
    -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list    (*exception DUP*)
haftmann@17766
    27
  val merge: ('a * 'a -> bool) -> ('b * 'b -> bool)
haftmann@22319
    28
    -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list    (*exception DUP*)
haftmann@19454
    29
  val make: ('a -> 'b) -> 'a list -> ('a * 'b) list
haftmann@17497
    30
  val find: ('a * 'b -> bool) -> ('c * 'b) list -> 'a -> 'c list
haftmann@19454
    31
  val coalesce: ('a * 'a -> bool) -> ('a * 'b) list -> ('a * 'b list) list
haftmann@19454
    32
    (*coalesce ranges of equal neighbour keys*)
haftmann@19454
    33
  val group: ('a * 'a -> bool) -> ('a * 'b) list -> ('a * 'b list) list
haftmann@17152
    34
end;
haftmann@17152
    35
haftmann@17152
    36
structure AList: ALIST =
haftmann@17152
    37
struct
haftmann@17152
    38
haftmann@17766
    39
fun find_index eq xs key =
haftmann@17766
    40
  let
haftmann@18926
    41
    fun find [] _ = ~1
haftmann@17766
    42
      | find ((key', value)::xs) i =
haftmann@17766
    43
          if eq (key, key')
haftmann@17766
    44
          then i
haftmann@17766
    45
          else find xs (i+1);
haftmann@18926
    46
  in find xs 0 end;
haftmann@17766
    47
haftmann@20166
    48
fun map_index eq key f_none f_some xs =
haftmann@20166
    49
  let
haftmann@20166
    50
    val i = find_index eq xs key;
haftmann@20166
    51
    fun mapp 0 (x::xs) = f_some x xs
haftmann@20166
    52
      | mapp i (x::xs) = x :: mapp (i-1) xs;
haftmann@20166
    53
  in (if i = ~1 then f_none else mapp i) xs end;
haftmann@20166
    54
haftmann@17152
    55
fun lookup _ [] _ = NONE
haftmann@17152
    56
  | lookup eq ((key, value)::xs) key' =
haftmann@17152
    57
      if eq (key', key) then SOME value
haftmann@17152
    58
      else lookup eq xs key';
haftmann@17152
    59
haftmann@17152
    60
fun defined _ [] _ = false
haftmann@17152
    61
  | defined eq ((key, value)::xs) key' =
haftmann@17152
    62
      eq (key', key) orelse defined eq xs key';
haftmann@17152
    63
haftmann@20166
    64
fun update eq (x as (key, value)) =
haftmann@20166
    65
  map_index eq key (cons x) (fn _ => cons x);
haftmann@17152
    66
haftmann@17152
    67
fun default eq (key, value) xs =
haftmann@20166
    68
  if defined eq xs key then xs else (key, value) :: xs;
haftmann@20166
    69
haftmann@20166
    70
fun delete eq key =
haftmann@20166
    71
  map_index eq key I (K I);
haftmann@17152
    72
haftmann@20166
    73
fun map_entry eq key f =
haftmann@20166
    74
  map_index eq key I (fn (key, value) => cons (key, f value));
haftmann@17766
    75
haftmann@20166
    76
fun map_default eq (key, value) f =
haftmann@20166
    77
  map_index eq key (cons (key, f value)) (fn (key, value) => cons (key, f value));
haftmann@17152
    78
haftmann@18883
    79
fun map_entry_yield eq key f xs =
haftmann@18883
    80
  let
haftmann@18883
    81
    val i = find_index eq xs key;
haftmann@18926
    82
    fun mapp 0 ((x as (key, value))::xs) =
haftmann@18883
    83
          let val (r, value') = f value
haftmann@18883
    84
          in (SOME r, (key, value') :: xs) end
haftmann@18883
    85
      | mapp i (x::xs) =
haftmann@18883
    86
          let val (r, xs') = mapp (i-1) xs
haftmann@18883
    87
          in (r, x::xs') end;
haftmann@18926
    88
  in if i = ~1 then (NONE, xs) else mapp i xs end;
haftmann@18883
    89
haftmann@17766
    90
exception DUP;
haftmann@17766
    91
haftmann@17911
    92
fun join eq f (xs, ys) =
haftmann@17911
    93
  let
haftmann@18926
    94
    fun add (y as (key, value)) xs =
haftmann@17911
    95
      (case lookup eq xs key of
haftmann@18926
    96
        NONE => cons y xs
haftmann@20142
    97
      | SOME value' => update eq (key, f key (value', value)) xs);
haftmann@18926
    98
  in fold_rev add ys xs end;
haftmann@17911
    99
haftmann@18926
   100
fun merge eq_key eq_val =
haftmann@20142
   101
  join eq_key (K (fn (yx as (_, x)) => if eq_val yx then x else raise DUP));
haftmann@17191
   102
haftmann@17487
   103
fun make keyfun =
haftmann@17487
   104
  let fun keypair x = (x, keyfun x)
haftmann@17487
   105
  in map keypair end;
haftmann@17487
   106
haftmann@17497
   107
fun find eq [] _ = []
haftmann@17497
   108
  | find eq ((key, value) :: xs) value' =
haftmann@17497
   109
      let
haftmann@17766
   110
        val values = find eq xs value';
haftmann@17497
   111
      in if eq (value', value) then key :: values else values end;
haftmann@17497
   112
haftmann@19454
   113
fun coalesce eq =
haftmann@19454
   114
  let
haftmann@19454
   115
    fun vals _ [] = ([], [])
haftmann@19454
   116
      | vals x (lst as (y, b) :: ps) =
haftmann@19454
   117
          if eq (x, y) then vals x ps |>> cons b
haftmann@19454
   118
          else ([], lst);
haftmann@19454
   119
    fun coal [] = []
haftmann@19454
   120
      | coal ((x, a) :: ps) =
haftmann@19454
   121
          let val (bs, qs) = vals x ps
haftmann@19454
   122
          in (x, a :: bs) :: coal qs end;
haftmann@19454
   123
  in coal end;
haftmann@19454
   124
haftmann@19454
   125
fun group eq xs =
haftmann@19454
   126
  fold_rev (fn (k, v) => default eq (k, []) #> map_entry eq k (cons v)) xs [];
haftmann@19454
   127
haftmann@17152
   128
end;