src/Pure/General/alist.ML
author haftmann
Mon, 06 Feb 2006 11:00:06 +0100
changeset 18926 4227b1510552
parent 18883 1bbeb076a6de
child 19454 46a7e133f802
permissions -rw-r--r--
clarified semantics of merge
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
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
     5
Association lists -- lists of (key, value) pairs with unique keys.
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
     6
A light-weight representation of finite mappings;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
     7
see also Pure/General/table.ML for a more scalable implementation.
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
     8
*)
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
     9
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    10
signature ALIST =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    11
sig
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    12
  exception DUP
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    13
  val lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    14
  val defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    15
  val update: ('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 default: ('a * 'a -> bool) -> ('a * 'b)
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    18
    -> ('a * 'b) list -> ('a * 'b) list
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    19
  val delete: ('a * 'b -> bool) -> 'a
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    20
    -> ('b * 'c) list -> ('b * 'c) list
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    21
  val map_entry: ('a * 'b -> bool) -> 'a -> ('c -> 'c)
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    22
    -> ('b * 'c) list -> ('b * 'c) list
18883
1bbeb076a6de added map_entry_yield
haftmann
parents: 18453
diff changeset
    23
  val map_entry_yield: ('a * 'b -> bool) -> 'a -> ('c -> 'd * 'c)
1bbeb076a6de added map_entry_yield
haftmann
parents: 18453
diff changeset
    24
    -> ('b * 'c) list -> 'd option * ('b * 'c) list
17487
940713ba9d2b added make function
haftmann
parents: 17191
diff changeset
    25
  val make: ('a -> 'b) -> 'a list -> ('a * 'b) list
17911
fbe857bedcd7 added join
haftmann
parents: 17766
diff changeset
    26
  val join: ('a * 'a -> bool) -> ('a -> 'b * 'b -> 'b option) ->
fbe857bedcd7 added join
haftmann
parents: 17766
diff changeset
    27
    ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list         (*exception DUP*)
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    28
  val merge: ('a * 'a -> bool) -> ('b * 'b -> bool)
17911
fbe857bedcd7 added join
haftmann
parents: 17766
diff changeset
    29
    -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list      (*exception DUP*)
17497
539319bd6162 added make and find
haftmann
parents: 17487
diff changeset
    30
  val find: ('a * 'b -> bool) -> ('c * 'b) list -> 'a -> 'c list
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    31
end;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    32
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    33
structure AList: ALIST =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    34
struct
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    35
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    36
fun find_index eq xs key =
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    37
  let
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    38
    fun find [] _ = ~1
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    39
      | find ((key', value)::xs) i =
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    40
          if eq (key, key')
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    41
          then i
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    42
          else find xs (i+1);
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    43
  in find xs 0 end;
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    44
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    45
fun lookup _ [] _ = NONE
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    46
  | lookup eq ((key, value)::xs) key' =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    47
      if eq (key', key) then SOME value
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    48
      else lookup eq xs key';
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    49
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    50
fun defined _ [] _ = false
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    51
  | defined eq ((key, value)::xs) key' =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    52
      eq (key', key) orelse defined eq xs key';
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    53
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    54
fun update eq (x as (key, value)) xs =
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    55
  let
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    56
    val i = find_index eq xs key;
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    57
    fun upd 0 (_::xs) = x :: xs
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    58
      | upd i (x::xs) = x :: upd (i-1) xs;
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    59
  in if i = ~1 then x::xs else upd i xs end;
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    60
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    61
fun default eq (key, value) xs =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    62
  if defined eq xs key then xs else (key, value)::xs;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    63
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    64
fun delete eq key xs =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    65
  let
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    66
    val i = find_index eq xs key;
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    67
    fun del 0 (_::xs) = xs
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    68
      | del i (x::xs) = x :: del (i-1) xs;
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    69
  in if i = ~1 then xs else del i xs end;
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    70
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    71
fun map_entry eq key f xs =
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    72
  let
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    73
    val i = find_index eq xs key;
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    74
    fun mapp 0 ((x as (key, value))::xs) = (key, f value) :: xs
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
    75
      | mapp i (x::xs) = x :: mapp (i-1) xs;
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    76
  in if i = ~1 then xs else mapp i xs end;
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
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    96
      | SOME value' =>
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    97
          (case f key (value', value) of
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
    98
            SOME value'' => update eq (key, value'') xs
17911
fbe857bedcd7 added join
haftmann
parents: 17766
diff changeset
    99
          | NONE => raise DUP));
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
   100
  in fold_rev add ys xs end;
17911
fbe857bedcd7 added join
haftmann
parents: 17766
diff changeset
   101
18926
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
   102
fun merge eq_key eq_val =
4227b1510552 clarified semantics of merge
haftmann
parents: 18883
diff changeset
   103
  join eq_key (K (fn (yx as (_, x)) => if eq_val yx then SOME x else NONE));
17191
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
   104
17487
940713ba9d2b added make function
haftmann
parents: 17191
diff changeset
   105
fun make keyfun =
940713ba9d2b added make function
haftmann
parents: 17191
diff changeset
   106
  let fun keypair x = (x, keyfun x)
940713ba9d2b added make function
haftmann
parents: 17191
diff changeset
   107
  in map keypair end;
940713ba9d2b added make function
haftmann
parents: 17191
diff changeset
   108
17497
539319bd6162 added make and find
haftmann
parents: 17487
diff changeset
   109
fun find eq [] _ = []
539319bd6162 added make and find
haftmann
parents: 17487
diff changeset
   110
  | find eq ((key, value) :: xs) value' =
539319bd6162 added make and find
haftmann
parents: 17487
diff changeset
   111
      let
17766
10319da54a47 added merge, tuned
haftmann
parents: 17497
diff changeset
   112
        val values = find eq xs value';
17497
539319bd6162 added make and find
haftmann
parents: 17487
diff changeset
   113
      in if eq (value', value) then key :: values else values end;
539319bd6162 added make and find
haftmann
parents: 17487
diff changeset
   114
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
   115
end;