src/Pure/General/table.ML
changeset 25391 783e5de2a6c7
parent 23655 d2d1138e0ddc
child 27508 abdab08677d8
equal deleted inserted replaced
25390:8bfa6566ac6b 25391:783e5de2a6c7
    47   val delete_safe: key -> 'a table -> 'a table
    47   val delete_safe: key -> 'a table -> 'a table
    48   val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
    48   val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
    49   val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
    49   val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
    50   val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
    50   val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
    51   val lookup_list: 'a list table -> key -> 'a list
    51   val lookup_list: 'a list table -> key -> 'a list
    52   val update_list: (key * 'a) -> 'a list table -> 'a list table
    52   val cons_list: (key * 'a) -> 'a list table -> 'a list table
    53   val insert_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
    53   val insert_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
    54   val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
    54   val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
       
    55   val update_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
    55   val make_list: (key * 'a) list -> 'a list table
    56   val make_list: (key * 'a) list -> 'a list table
    56   val dest_list: 'a list table -> (key * 'a) list
    57   val dest_list: 'a list table -> (key * 'a) list
    57   val merge_list: ('a * 'a -> bool) ->
    58   val merge_list: ('a * 'a -> bool) ->
    58     'a list table * 'a list table -> 'a list table                     (*exception DUP*)
    59     'a list table * 'a list table -> 'a list table                     (*exception DUP*)
    59 end;
    60 end;
   360 
   361 
   361 
   362 
   362 (* list tables *)
   363 (* list tables *)
   363 
   364 
   364 fun lookup_list tab key = these (lookup tab key);
   365 fun lookup_list tab key = these (lookup tab key);
   365 fun update_list (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab;
   366 
       
   367 fun cons_list (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab;
   366 
   368 
   367 fun insert_list eq (key, x) =
   369 fun insert_list eq (key, x) =
   368   modify key (fn NONE => [x] | SOME xs => if Library.member eq xs x then raise SAME else x :: xs);
   370   modify key (fn NONE => [x] | SOME xs => if Library.member eq xs x then raise SAME else x :: xs);
   369   
   371 
   370 fun remove_list eq (key, x) tab =
   372 fun remove_list eq (key, x) tab =
   371   map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab
   373   map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab
   372   handle UNDEF _ => delete key tab;
   374   handle UNDEF _ => delete key tab;
   373 
   375 
   374 fun make_list args = fold_rev update_list args empty;
   376 fun update_list eq (key, x) =
       
   377   modify key (fn NONE => [x] | SOME [] => [x] | SOME (xs as y :: _) =>
       
   378     if eq (x, y) then raise SAME else Library.update eq x xs);
       
   379 
       
   380 fun make_list args = fold_rev cons_list args empty;
   375 fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab);
   381 fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab);
   376 fun merge_list eq = join (fn _ => Library.merge eq);
   382 fun merge_list eq = join (fn _ => Library.merge eq);
   377 
   383 
   378 
   384 
   379 (*final declarations of this structure!*)
   385 (*final declarations of this structure!*)