src/HOL/Data_Structures/Lookup2.thy
author nipkow
Wed, 23 Sep 2015 09:47:04 +0200
changeset 61232 c46faf9762f7
child 61693 f6b9f528c89c
permissions -rw-r--r--
added AVL and lookup function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     3
section \<open>Function \textit{lookup} for Tree2\<close>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     4
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     5
theory Lookup2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     6
imports
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     7
  Tree2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     8
  Map_by_Ordered
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     9
begin
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    10
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    11
fun lookup :: "('a::linorder * 'b, 'c) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    12
"lookup Leaf x = None" |
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    13
"lookup (Node _ l (a,b) r) x =
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    14
  (if x < a then lookup l x else
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    15
   if x > a then lookup r x else Some b)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    16
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    17
lemma lookup_eq:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    18
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    19
by(induction t) (auto simp: map_of_simps split: option.split)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    20
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    21
end