src/HOL/Library/AList_Mapping.thy
author Christian Sternagel
Wed Aug 29 12:23:14 2012 +0900 (2012-08-29)
changeset 49083 01081bca31b6
parent 46238 9ace9e5b79be
child 49929 70300f1b6835
permissions -rw-r--r--
dropped ord and bot instance for list prefixes (use locale interpretation instead, which allows users to decide what order to use on lists)
bulwahn@44897
     1
(* Title: HOL/Library/AList_Mapping.thy
bulwahn@44897
     2
   Author: Florian Haftmann, TU Muenchen
bulwahn@44897
     3
*)
bulwahn@44897
     4
bulwahn@44897
     5
header {* Implementation of mappings with Association Lists *}
bulwahn@44897
     6
bulwahn@44897
     7
theory AList_Mapping
bulwahn@46238
     8
imports AList Mapping
bulwahn@44897
     9
begin
bulwahn@44897
    10
bulwahn@44897
    11
definition Mapping :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) mapping" where
bulwahn@44897
    12
  "Mapping xs = Mapping.Mapping (map_of xs)"
bulwahn@44897
    13
bulwahn@44897
    14
code_datatype Mapping
bulwahn@44897
    15
bulwahn@44897
    16
lemma lookup_Mapping [simp, code]:
bulwahn@44897
    17
  "Mapping.lookup (Mapping xs) = map_of xs"
bulwahn@44897
    18
  by (simp add: Mapping_def)
bulwahn@44897
    19
bulwahn@44897
    20
lemma keys_Mapping [simp, code]:
bulwahn@44897
    21
  "Mapping.keys (Mapping xs) = set (map fst xs)"
bulwahn@44897
    22
  by (simp add: keys_def dom_map_of_conv_image_fst)
bulwahn@44897
    23
bulwahn@44897
    24
lemma empty_Mapping [code]:
bulwahn@44897
    25
  "Mapping.empty = Mapping []"
bulwahn@44897
    26
  by (rule mapping_eqI) simp
bulwahn@44897
    27
bulwahn@44897
    28
lemma is_empty_Mapping [code]:
bulwahn@44897
    29
  "Mapping.is_empty (Mapping xs) \<longleftrightarrow> List.null xs"
bulwahn@44897
    30
  by (cases xs) (simp_all add: is_empty_def null_def)
bulwahn@44897
    31
bulwahn@44897
    32
lemma update_Mapping [code]:
bulwahn@46238
    33
  "Mapping.update k v (Mapping xs) = Mapping (AList.update k v xs)"
bulwahn@44897
    34
  by (rule mapping_eqI) (simp add: update_conv')
bulwahn@44897
    35
bulwahn@44897
    36
lemma delete_Mapping [code]:
bulwahn@46238
    37
  "Mapping.delete k (Mapping xs) = Mapping (AList.delete k xs)"
bulwahn@44897
    38
  by (rule mapping_eqI) (simp add: delete_conv')
bulwahn@44897
    39
bulwahn@44897
    40
lemma ordered_keys_Mapping [code]:
bulwahn@44897
    41
  "Mapping.ordered_keys (Mapping xs) = sort (remdups (map fst xs))"
bulwahn@44897
    42
  by (simp only: ordered_keys_def keys_Mapping sorted_list_of_set_sort_remdups) simp
bulwahn@44897
    43
bulwahn@44897
    44
lemma size_Mapping [code]:
bulwahn@44897
    45
  "Mapping.size (Mapping xs) = length (remdups (map fst xs))"
bulwahn@44897
    46
  by (simp add: size_def length_remdups_card_conv dom_map_of_conv_image_fst)
bulwahn@44897
    47
bulwahn@44897
    48
lemma tabulate_Mapping [code]:
bulwahn@44897
    49
  "Mapping.tabulate ks f = Mapping (map (\<lambda>k. (k, f k)) ks)"
bulwahn@44897
    50
  by (rule mapping_eqI) (simp add: map_of_map_restrict)
bulwahn@44897
    51
bulwahn@44897
    52
lemma bulkload_Mapping [code]:
bulwahn@44897
    53
  "Mapping.bulkload vs = Mapping (map (\<lambda>n. (n, vs ! n)) [0..<length vs])"
bulwahn@44897
    54
  by (rule mapping_eqI) (simp add: map_of_map_restrict fun_eq_iff)
bulwahn@44897
    55
bulwahn@44897
    56
lemma equal_Mapping [code]:
bulwahn@44897
    57
  "HOL.equal (Mapping xs) (Mapping ys) \<longleftrightarrow>
bulwahn@44897
    58
    (let ks = map fst xs; ls = map fst ys
bulwahn@44897
    59
    in (\<forall>l\<in>set ls. l \<in> set ks) \<and> (\<forall>k\<in>set ks. k \<in> set ls \<and> map_of xs k = map_of ys k))"
bulwahn@44897
    60
proof -
bulwahn@44897
    61
  have aux: "\<And>a b xs. (a, b) \<in> set xs \<Longrightarrow> a \<in> fst ` set xs"
bulwahn@44897
    62
    by (auto simp add: image_def intro!: bexI)
bulwahn@44897
    63
  show ?thesis
bulwahn@44897
    64
    by (auto intro!: map_of_eqI simp add: Let_def equal Mapping_def)
bulwahn@44897
    65
      (auto dest!: map_of_eq_dom intro: aux)
bulwahn@44897
    66
qed
bulwahn@44897
    67
bulwahn@44897
    68
lemma [code nbe]:
bulwahn@44897
    69
  "HOL.equal (x :: ('a, 'b) mapping) x \<longleftrightarrow> True"
bulwahn@44897
    70
  by (fact equal_refl)
bulwahn@44897
    71
  
bulwahn@44913
    72
end