src/HOL/Library/AssocList.thy
changeset 19323 ec5cd5b1804c
parent 19234 054332e39e0a
child 19332 bb71a64e1263
equal deleted inserted replaced
19322:bf84bdf05f14 19323:ec5cd5b1804c
    14         function @{term "clearjunk"} distributes over them.*}
    14         function @{term "clearjunk"} distributes over them.*}
    15 consts 
    15 consts 
    16   delete     :: "'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    16   delete     :: "'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    17   update     :: "'key \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    17   update     :: "'key \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    18   updates    :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    18   updates    :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    19   substitute :: "'val \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
       
    20   map_at     :: "('val \<Rightarrow> 'val) \<Rightarrow> 'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val) list"
       
    21   merge      :: "('key * 'val)list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    19   merge      :: "('key * 'val)list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    22   compose    :: "('key * 'a)list \<Rightarrow> ('a * 'b)list \<Rightarrow> ('key * 'b)list"
    20   compose    :: "('key * 'a)list \<Rightarrow> ('a * 'b)list \<Rightarrow> ('key * 'b)list"
    23   restrict   :: "('key set) \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    21   restrict   :: "('key set) \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    24 
    22 
    25   clearjunk  :: "('key * 'val)list \<Rightarrow> ('key * 'val)list"
    23   clearjunk  :: "('key * 'val)list \<Rightarrow> ('key * 'val)list"
       
    24 
       
    25 (* a bit special
       
    26   substitute :: "'val \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
       
    27   map_at     :: "('val \<Rightarrow> 'val) \<Rightarrow> 'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val) list"
       
    28 *)
    26 
    29 
    27 defs
    30 defs
    28 delete_def: "delete k \<equiv> filter (\<lambda>p. fst p \<noteq> k)"
    31 delete_def: "delete k \<equiv> filter (\<lambda>p. fst p \<noteq> k)"
    29 
    32 
    30 primrec
    33 primrec
    33 primrec
    36 primrec
    34 "updates [] vs al = al"
    37 "updates [] vs al = al"
    35 "updates (k#ks) vs al = (case vs of [] \<Rightarrow> al 
    38 "updates (k#ks) vs al = (case vs of [] \<Rightarrow> al 
    36                          | (v#vs') \<Rightarrow> updates ks vs' (update k v al))"
    39                          | (v#vs') \<Rightarrow> updates ks vs' (update k v al))"
    37 primrec
    40 primrec
       
    41 "merge xs [] = xs"
       
    42 "merge xs (p#ps) = update (fst p) (snd p) (merge xs ps)"
       
    43 
       
    44 (*
       
    45 primrec
    38 "substitute v v' [] = []"
    46 "substitute v v' [] = []"
    39 "substitute v v' (p#ps) = (if snd p = v then (fst p,v')#substitute v v' ps
    47 "substitute v v' (p#ps) = (if snd p = v then (fst p,v')#substitute v v' ps
    40                           else p#substitute v v' ps)"
    48                           else p#substitute v v' ps)"
    41 primrec
    49 primrec
    42 "map_at f k [] = []"
    50 "map_at f k [] = []"
    43 "map_at f k (p#ps) = (if fst p = k then (k,f (snd p))#ps else p # map_at f k ps)"
    51 "map_at f k (p#ps) = (if fst p = k then (k,f (snd p))#ps else p # map_at f k ps)"
    44 primrec
    52 *)
    45 "merge xs [] = xs"
       
    46 "merge xs (p#ps) = update (fst p) (snd p) (merge xs ps)"
       
    47 
    53 
    48 lemma length_delete_le: "length (delete k al) \<le> length al"
    54 lemma length_delete_le: "length (delete k al) \<le> length al"
    49 proof (induct al)
    55 proof (induct al)
    50   case Nil thus ?case by (simp add: delete_def)
    56   case Nil thus ?case by (simp add: delete_def)
    51 next
    57 next
   429 
   435 
   430 lemma updates_append2_drop[simp]:
   436 lemma updates_append2_drop[simp]:
   431   "size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al"
   437   "size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al"
   432   by (induct xs fixing: ys al) (auto split: list.splits)
   438   by (induct xs fixing: ys al) (auto split: list.splits)
   433 
   439 
   434 
   440 (*
   435 (* ******************************************************************************** *)
   441 (* ******************************************************************************** *)
   436 subsection {* @{const substitute} *}
   442 subsection {* @{const substitute} *}
   437 (* ******************************************************************************** *)
   443 (* ******************************************************************************** *)
   438 
   444 
   439 lemma substitute_conv: "map_of (substitute v v' al) k = ((map_of al)(v ~> v')) k"
   445 lemma substitute_conv: "map_of (substitute v v' al) k = ((map_of al)(v ~> v')) k"
   454   by (induct ps) auto
   460   by (induct ps) auto
   455 
   461 
   456 lemma clearjunk_substitute:
   462 lemma clearjunk_substitute:
   457  "clearjunk (substitute v v' al) = substitute v v' (clearjunk al)"
   463  "clearjunk (substitute v v' al) = substitute v v' (clearjunk al)"
   458   by (induct al rule: clearjunk.induct) (auto simp add: substitute_filter delete_def)
   464   by (induct al rule: clearjunk.induct) (auto simp add: substitute_filter delete_def)
   459 
   465 *)
       
   466 (*
   460 (* ******************************************************************************** *)
   467 (* ******************************************************************************** *)
   461 subsection {* @{const map_at} *}
   468 subsection {* @{const map_at} *}
   462 (* ******************************************************************************** *)
   469 (* ******************************************************************************** *)
   463   
   470   
   464 lemma map_at_conv: "map_of (map_at f k al) k' = (chg_map f k (map_of al)) k'"
   471 lemma map_at_conv: "map_of (map_at f k al) k' = (chg_map f k (map_of al)) k'"
   489 lemma map_at_update: "map_of al k = Some v \<Longrightarrow> map_at f k al = update k (f v) al"
   496 lemma map_at_update: "map_of al k = Some v \<Longrightarrow> map_at f k al = update k (f v) al"
   490   by (induct al) auto
   497   by (induct al) auto
   491 
   498 
   492 lemma map_at_other [simp]: "a \<noteq> b \<Longrightarrow> map_of (map_at f a al) b = map_of al b"
   499 lemma map_at_other [simp]: "a \<noteq> b \<Longrightarrow> map_of (map_at f a al) b = map_of al b"
   493   by (simp add: map_at_conv')
   500   by (simp add: map_at_conv')
   494 
   501 *)
   495 (* ******************************************************************************** *)
   502 (* ******************************************************************************** *)
   496 subsection {* @{const merge} *}
   503 subsection {* @{const merge} *}
   497 (* ******************************************************************************** *)
   504 (* ******************************************************************************** *)
   498 
   505 
   499 lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
   506 lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"