src/HOL/Data_Structures/Map_by_Ordered.thy
author wenzelm
Mon, 02 Nov 2015 18:09:14 +0100
changeset 61545 57000ac6291f
parent 61428 5e1938107371
child 61640 44c9198f210c
permissions -rw-r--r--
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     2
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     3
section {* Implementing Ordered Maps *}
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     4
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     5
theory Map_by_Ordered
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     6
imports AList_Upd_Del
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     7
begin
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     8
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     9
locale Map =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    10
fixes empty :: "'m"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    11
fixes update :: "'a \<Rightarrow> 'b \<Rightarrow> 'm \<Rightarrow> 'm"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    12
fixes delete :: "'a \<Rightarrow> 'm \<Rightarrow> 'm"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    13
fixes map_of :: "'m \<Rightarrow> 'a \<Rightarrow> 'b option"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    14
fixes invar :: "'m \<Rightarrow> bool"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    15
assumes "map_of empty = (\<lambda>_. None)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    16
assumes "invar m \<Longrightarrow> map_of(update a b m) = (map_of m)(a := Some b)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    17
assumes "invar m \<Longrightarrow> map_of(delete a m) = (map_of m)(a := None)"
61428
5e1938107371 added invar empty
nipkow
parents: 61203
diff changeset
    18
assumes "invar empty"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    19
assumes "invar m \<Longrightarrow> invar(update a b m)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    20
assumes "invar m \<Longrightarrow> invar(delete a m)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    21
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    22
locale Map_by_Ordered =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    23
fixes empty :: "'t"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    24
fixes update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> 't \<Rightarrow> 't"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    25
fixes delete :: "'a \<Rightarrow> 't \<Rightarrow> 't"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    26
fixes lookup :: "'t \<Rightarrow> 'a \<Rightarrow> 'b option"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    27
fixes inorder :: "'t \<Rightarrow> ('a * 'b) list"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    28
fixes wf :: "'t \<Rightarrow> bool"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    29
assumes empty: "inorder empty = []"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    30
assumes lookup: "wf t \<and> sorted1 (inorder t) \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    31
  lookup t a = map_of (inorder t) a"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    32
assumes update: "wf t \<and> sorted1 (inorder t) \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    33
  inorder(update a b t) = upd_list a b (inorder t)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    34
assumes delete: "wf t \<and> sorted1 (inorder t) \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    35
  inorder(delete a t) = del_list a (inorder t)"
61428
5e1938107371 added invar empty
nipkow
parents: 61203
diff changeset
    36
assumes wf_empty:  "wf empty"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    37
assumes wf_insert: "wf t \<and> sorted1 (inorder t) \<Longrightarrow> wf(update a b t)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    38
assumes wf_delete: "wf t \<and> sorted1 (inorder t) \<Longrightarrow> wf(delete a t)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    39
begin
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    40
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    41
sublocale Map
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    42
  empty update delete "map_of o inorder" "\<lambda>t. wf t \<and> sorted1 (inorder t)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    43
proof(standard, goal_cases)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    44
  case 1 show ?case by (auto simp: empty)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    45
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    46
  case 2 thus ?case by(simp add: update map_of_ins_list)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    47
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    48
  case 3 thus ?case by(simp add: delete map_of_del_list)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    49
next
61428
5e1938107371 added invar empty
nipkow
parents: 61203
diff changeset
    50
  case 4 thus ?case by(simp add: empty wf_empty)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    51
next
61428
5e1938107371 added invar empty
nipkow
parents: 61203
diff changeset
    52
  case 5 thus ?case by(simp add: update wf_insert sorted_upd_list)
5e1938107371 added invar empty
nipkow
parents: 61203
diff changeset
    53
next
5e1938107371 added invar empty
nipkow
parents: 61203
diff changeset
    54
  case 6 thus ?case by (auto simp: delete wf_delete sorted_del_list)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    55
qed
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    56
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    57
end
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    58
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    59
end