src/Pure/General/alist.ML
author haftmann
Wed, 31 Aug 2005 09:01:45 +0200
changeset 17191 ae9901f856aa
parent 17152 a696a3d30b97
child 17487 940713ba9d2b
permissions -rw-r--r--
better map_entry
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
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    12
  val lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    13
  val defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    14
  val update: ('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 default: ('a * 'a -> bool) -> ('a * 'b)
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    17
    -> ('a * 'b) list -> ('a * 'b) list
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    18
  val delete: ('a * 'b -> bool) -> 'a
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    19
    -> ('b * 'c) list -> ('b * 'c) list
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    20
  val map_entry: ('a * 'b -> bool) -> 'a -> ('c -> 'c)
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    21
    -> ('b * 'c) list -> ('b * 'c) list
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    22
end;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    23
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    24
structure AList: ALIST =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    25
struct
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    26
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    27
fun lookup _ [] _ = NONE
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    28
  | lookup eq ((key, value)::xs) key' =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    29
      if eq (key', key) then SOME value
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    30
      else lookup eq xs key';
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    31
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    32
fun defined _ [] _ = false
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    33
  | defined eq ((key, value)::xs) key' =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    34
      eq (key', key) orelse defined eq xs key';
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    35
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    36
fun update eq (key, value) xs =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    37
  let
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    38
    fun upd ((x as (key', _))::xs) =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    39
      if eq (key, key')
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    40
      then (key, value)::xs
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    41
      else x :: upd xs
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    42
  in if defined eq xs key then upd xs else (key, value)::xs end;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    43
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    44
fun default eq (key, value) xs =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    45
  if defined eq xs key then xs else (key, value)::xs;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    46
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    47
fun delete eq key xs =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    48
  let
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    49
    fun del ((x as (key', _))::xs) =
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    50
      if eq (key, key') then xs
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    51
      else x :: del xs;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    52
  in if defined eq xs key then del xs else xs end;
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    53
17191
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    54
fun map_entry eq key' f xs =
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    55
  let
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    56
    fun mapp ((x as (key, value))::xs) =
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    57
      if eq (key', key) then (key, f value)::xs
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    58
      else x :: mapp xs
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    59
  in if defined eq xs key' then mapp xs else xs end;
ae9901f856aa better map_entry
haftmann
parents: 17152
diff changeset
    60
17152
a696a3d30b97 added alist module
haftmann
parents:
diff changeset
    61
end;