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