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