src/HOL/Data_Structures/Set_Specs.thy
author wenzelm
Sun, 27 May 2018 22:37:08 +0200
changeset 68300 cd8ab1a7a286
parent 67965 aaa31cd0caef
child 68439 c8101022e52a
permissions -rw-r--r--
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
     2
67965
aaa31cd0caef more name tuning
nipkow
parents: 67964
diff changeset
     3
section \<open>Specifications of Set ADT\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
     4
67965
aaa31cd0caef more name tuning
nipkow
parents: 67964
diff changeset
     5
theory Set_Specs
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
     6
imports List_Ins_Del
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
     7
begin
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
     8
67965
aaa31cd0caef more name tuning
nipkow
parents: 67964
diff changeset
     9
text \<open>The basic set interface with traditional \<open>set\<close>-based specification:\<close>
67964
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    10
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    11
locale Set =
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    12
fixes empty :: "'s"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    13
fixes insert :: "'a \<Rightarrow> 's \<Rightarrow> 's"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    14
fixes delete :: "'a \<Rightarrow> 's \<Rightarrow> 's"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    15
fixes isin :: "'s \<Rightarrow> 'a \<Rightarrow> bool"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    16
fixes set :: "'s \<Rightarrow> 'a set"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    17
fixes invar :: "'s \<Rightarrow> bool"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    18
assumes set_empty:    "set empty = {}"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    19
assumes set_isin:     "invar s \<Longrightarrow> isin s x = (x \<in> set s)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    20
assumes set_insert:   "invar s \<Longrightarrow> set(insert x s) = Set.insert x (set s)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    21
assumes set_delete:   "invar s \<Longrightarrow> set(delete x s) = set s - {x}"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    22
assumes invar_empty:  "invar empty"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    23
assumes invar_insert: "invar s \<Longrightarrow> invar(insert x s)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    24
assumes invar_delete: "invar s \<Longrightarrow> invar(delete x s)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    25
67964
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    26
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    27
text \<open>The basic set interface with \<open>inorder\<close>-based specification:\<close>
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    28
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    29
locale Set_by_Ordered =
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    30
fixes empty :: "'t"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    31
fixes insert :: "'a::linorder \<Rightarrow> 't \<Rightarrow> 't"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    32
fixes delete :: "'a \<Rightarrow> 't \<Rightarrow> 't"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    33
fixes isin :: "'t \<Rightarrow> 'a \<Rightarrow> bool"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    34
fixes inorder :: "'t \<Rightarrow> 'a list"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    35
fixes inv :: "'t \<Rightarrow> bool"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    36
assumes empty: "inorder empty = []"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    37
assumes isin: "inv t \<and> sorted(inorder t) \<Longrightarrow>
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    38
  isin t x = (x \<in> set (inorder t))"
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    39
assumes insert: "inv t \<and> sorted(inorder t) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    40
  inorder(insert x t) = ins_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    41
assumes delete: "inv t \<and> sorted(inorder t) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    42
  inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    43
assumes inv_empty:  "inv empty"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    44
assumes inv_insert: "inv t \<and> sorted(inorder t) \<Longrightarrow> inv(insert x t)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    45
assumes inv_delete: "inv t \<and> sorted(inorder t) \<Longrightarrow> inv(delete x t)"
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    46
begin
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    47
67964
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    48
text \<open>It implements the traditional specification:\<close>
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    49
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    50
sublocale Set
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    51
  empty insert delete isin "set o inorder" "\<lambda>t. inv t \<and> sorted(inorder t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    52
proof(standard, goal_cases)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    53
  case 1 show ?case by (auto simp: empty)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    54
next
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    55
  case 2 thus ?case by(simp add: isin)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    56
next
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    57
  case 3 thus ?case by(simp add: insert set_ins_list)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    58
next
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    59
  case (4 s x) thus ?case
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    60
    using delete[OF 4, of x] by (auto simp: distinct_if_sorted set_del_list_eq)
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    61
next
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    62
  case 5 thus ?case by(simp add: empty inv_empty)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    63
next
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    64
  case 6 thus ?case by(simp add: insert inv_insert sorted_ins_list)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    65
next
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    66
  case 7 thus ?case by (auto simp: delete inv_delete sorted_del_list)
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    67
qed
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    68
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    69
end
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    70
67964
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    71
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    72
text \<open>Set2 = Set with binary operations:\<close>
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    73
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    74
locale Set2 = Set
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    75
  where insert = insert for insert :: "'a \<Rightarrow> 's \<Rightarrow> 's" (*for typing purposes only*) +
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    76
fixes union :: "'s \<Rightarrow> 's \<Rightarrow> 's"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    77
fixes inter :: "'s \<Rightarrow> 's \<Rightarrow> 's"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    78
fixes diff  :: "'s \<Rightarrow> 's \<Rightarrow> 's"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    79
assumes set_union:   "\<lbrakk> invar s1; invar s2 \<rbrakk> \<Longrightarrow> set(union s1 s2) = set s1 \<union> set s2"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    80
assumes set_inter:   "\<lbrakk> invar s1; invar s2 \<rbrakk> \<Longrightarrow> set(inter s1 s2) = set s1 \<inter> set s2"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    81
assumes set_diff:   "\<lbrakk> invar s1; invar s2 \<rbrakk> \<Longrightarrow> set(diff s1 s2) = set s1 - set s2"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    82
assumes invar_union:   "\<lbrakk> invar s1; invar s2 \<rbrakk> \<Longrightarrow> invar(union s1 s2)"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    83
assumes invar_inter:   "\<lbrakk> invar s1; invar s2 \<rbrakk> \<Longrightarrow> invar(inter s1 s2)"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    84
assumes invar_diff:   "\<lbrakk> invar s1; invar s2 \<rbrakk> \<Longrightarrow> invar(diff s1 s2)"
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67929
diff changeset
    85
61640
44c9198f210c no CRLF
nipkow
parents: 61589
diff changeset
    86
end