nipkow@61640: (* Author: Tobias Nipkow *) nipkow@61640: nipkow@63411: section \Unbalanced Tree Implementation of Map\ nipkow@61640: nipkow@61640: theory Tree_Map nipkow@61640: imports nipkow@61640: Tree_Set nipkow@67965: Map_Specs nipkow@61640: begin nipkow@61640: nipkow@63411: fun lookup :: "('a::linorder*'b) tree \ 'a \ 'b option" where nipkow@61640: "lookup Leaf x = None" | nipkow@61640: "lookup (Node l (a,b) r) x = nipkow@61640: (case cmp x a of LT \ lookup l x | GT \ lookup r x | EQ \ Some b)" nipkow@61640: nipkow@63411: fun update :: "'a::linorder \ 'b \ ('a*'b) tree \ ('a*'b) tree" where nipkow@61640: "update x y Leaf = Node Leaf (x,y) Leaf" | nipkow@61640: "update x y (Node l (a,b) r) = (case cmp x a of nipkow@61640: LT \ Node (update x y l) (a,b) r | nipkow@61640: EQ \ Node l (x,y) r | nipkow@61640: GT \ Node l (a,b) (update x y r))" nipkow@61640: nipkow@63411: fun delete :: "'a::linorder \ ('a*'b) tree \ ('a*'b) tree" where nipkow@61640: "delete x Leaf = Leaf" | nipkow@61640: "delete x (Node l (a,b) r) = (case cmp x a of nipkow@61640: LT \ Node (delete x l) (a,b) r | nipkow@61640: GT \ Node l (a,b) (delete x r) | nipkow@68020: EQ \ if r = Leaf then l else let (ab',r') = split_min r in Node l ab' r')" nipkow@61640: nipkow@61640: nipkow@61640: subsection "Functional Correctness Proofs" nipkow@61640: nipkow@61790: lemma lookup_map_of: nipkow@61640: "sorted1(inorder t) \ lookup t x = map_of (inorder t) x" nipkow@61640: by (induction t) (auto simp: map_of_simps split: option.split) nipkow@61640: nipkow@68440: lemma inorder_update: nipkow@61640: "sorted1(inorder t) \ inorder(update a b t) = upd_list a b (inorder t)" nipkow@61640: by(induction t) (auto simp: upd_list_simps) nipkow@61640: nipkow@68440: lemma inorder_delete: nipkow@61640: "sorted1(inorder t) \ inorder(delete x t) = del_list x (inorder t)" nipkow@68020: by(induction t) (auto simp: del_list_simps split_minD split: prod.splits) nipkow@61640: nipkow@68440: interpretation M: Map_by_Ordered nipkow@68431: where empty = empty and lookup = lookup and update = update and delete = delete nipkow@61686: and inorder = inorder and inv = "\_. True" nipkow@61640: proof (standard, goal_cases) nipkow@68431: case 1 show ?case by (simp add: empty_def) nipkow@61640: next nipkow@61790: case 2 thus ?case by(simp add: lookup_map_of) nipkow@61640: next nipkow@68440: case 3 thus ?case by(simp add: inorder_update) nipkow@61640: next nipkow@68440: case 4 thus ?case by(simp add: inorder_delete) nipkow@61686: qed auto nipkow@61640: nipkow@61640: end