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