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