src/HOL/Unix/Unix.thy
author wenzelm
Sat, 07 Apr 2012 16:41:59 +0200
changeset 47389 e8552cba702d
parent 47215 ca516aa5b6ce
child 49077 154f25a162e3
permissions -rw-r--r--
explicit checks stable_finished_theory/stable_command allow parallel asynchronous command transactions; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Unix/Unix.thy
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
     3
*)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
     4
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
     5
header {* Unix file-systems \label{sec:unix-file-system} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
     6
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
     7
theory Unix
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 39224
diff changeset
     8
imports
44236
b73b7832b384 moved theory Nested_Environment to HOL-Unix (a bit too specific for HOL-Library);
wenzelm
parents: 43433
diff changeset
     9
  Nested_Environment
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 39224
diff changeset
    10
  "~~/src/HOL/Library/List_Prefix"
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
    11
begin
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    12
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    13
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    14
  We give a simple mathematical model of the basic structures
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    15
  underlying the Unix file-system, together with a few fundamental
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    16
  operations that could be imagined to be performed internally by the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    17
  Unix kernel.  This forms the basis for the set of Unix system-calls
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    18
  to be introduced later (see \secref{sec:unix-trans}), which are the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    19
  actual interface offered to processes running in user-space.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    20
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    21
  \medskip Basically, any Unix file is either a \emph{plain file} or a
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    22
  \emph{directory}, consisting of some \emph{content} plus
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    23
  \emph{attributes}.  The content of a plain file is plain text.  The
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    24
  content of a directory is a mapping from names to further
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    25
  files.\footnote{In fact, this is the only way that names get
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    26
  associated with files.  In Unix files do \emph{not} have a name in
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    27
  itself.  Even more, any number of names may be associated with the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    28
  very same file due to \emph{hard links} (although this is excluded
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    29
  from our model).}  Attributes include information to control various
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    30
  ways to access the file (read, write etc.).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    31
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    32
  Our model will be quite liberal in omitting excessive detail that is
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    33
  easily seen to be ``irrelevant'' for the aspects of Unix
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    34
  file-systems to be discussed here.  First of all, we ignore
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    35
  character and block special files, pipes, sockets, hard links,
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    36
  symbolic links, and mount points.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    37
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    38
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    39
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    40
subsection {* Names *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    41
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    42
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    43
  User ids and file name components shall be represented by natural
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    44
  numbers (without loss of generality).  We do not bother about
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    45
  encoding of actual names (e.g.\ strings), nor a mapping between user
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    46
  names and user ids as would be present in a reality.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    47
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    48
41579
4031fb078acc type_synonym;
wenzelm
parents: 41413
diff changeset
    49
type_synonym uid = nat
4031fb078acc type_synonym;
wenzelm
parents: 41413
diff changeset
    50
type_synonym name = nat
4031fb078acc type_synonym;
wenzelm
parents: 41413
diff changeset
    51
type_synonym path = "name list"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    52
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    53
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    54
subsection {* Attributes *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    55
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    56
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    57
  Unix file attributes mainly consist of \emph{owner} information and
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    58
  a number of \emph{permission} bits which control access for
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    59
  ``user'', ``group'', and ``others'' (see the Unix man pages @{text
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    60
  "chmod(2)"} and @{text "stat(2)"} for more details).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    61
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    62
  \medskip Our model of file permissions only considers the ``others''
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    63
  part.  The ``user'' field may be omitted without loss of overall
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    64
  generality, since the owner is usually able to change it anyway by
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    65
  performing @{text chmod}.\footnote{The inclined Unix expert may try
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    66
  to figure out some exotic arrangements of a real-world Unix
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    67
  file-system such that the owner of a file is unable to apply the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    68
  @{text chmod} system call.}  We omit ``group'' permissions as a
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    69
  genuine simplification as we just do not intend to discuss a model
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    70
  of multiple groups and group membership, but pretend that everyone
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    71
  is member of a single global group.\footnote{A general HOL model of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    72
  user group structures and related issues is given in
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    73
  \cite{Naraschewski:2001}.}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    74
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    75
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    76
datatype perm =
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    77
    Readable
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    78
  | Writable
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    79
  | Executable    -- "(ignored)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    80
41579
4031fb078acc type_synonym;
wenzelm
parents: 41413
diff changeset
    81
type_synonym perms = "perm set"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    82
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    83
record att =
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    84
  owner :: uid
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    85
  others :: perms
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    86
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    87
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    88
  For plain files @{term Readable} and @{term Writable} specify read
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    89
  and write access to the actual content, i.e.\ the string of text
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    90
  stored here.  For directories @{term Readable} determines if the set
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    91
  of entry names may be accessed, and @{term Writable} controls the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    92
  ability to create or delete any entries (both plain files or
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    93
  sub-directories).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    94
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    95
  As another simplification, we ignore the @{term Executable}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    96
  permission altogether.  In reality it would indicate executable
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    97
  plain files (also known as ``binaries''), or control actual lookup
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    98
  of directory entries (recall that mere directory browsing is
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
    99
  controlled via @{term Readable}).  Note that the latter means that
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   100
  in order to perform any file-system operation whatsoever, all
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   101
  directories encountered on the path would have to grant @{term
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   102
  Executable}.  We ignore this detail and pretend that all directories
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   103
  give @{term Executable} permission to anybody.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   104
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   105
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   106
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   107
subsection {* Files *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   108
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   109
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   110
  In order to model the general tree structure of a Unix file-system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   111
  we use the arbitrarily branching datatype @{typ "('a, 'b, 'c) env"}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   112
  from the standard library of Isabelle/HOL
13380
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
   113
  \cite{Bauer-et-al:2002:HOL-Library}.  This type provides
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   114
  constructors @{term Val} and @{term Env} as follows:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   115
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   116
  \medskip
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   117
  {\def\isastyleminor{\isastyle}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   118
  \begin{tabular}{l}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   119
  @{term [source] "Val :: 'a \<Rightarrow> ('a, 'b, 'c) env"} \\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   120
  @{term [source] "Env :: 'b \<Rightarrow> ('c \<Rightarrow> ('a, 'b, 'c) env option) \<Rightarrow> ('a, 'b, 'c) env"} \\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   121
  \end{tabular}}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   122
  \medskip
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   123
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   124
  Here the parameter @{typ 'a} refers to plain values occurring at
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   125
  leaf positions, parameter @{typ 'b} to information kept with inner
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   126
  branch nodes, and parameter @{typ 'c} to the branching type of the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   127
  tree structure.  For our purpose we use the type instance with @{typ
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   128
  "att \<times> string"} (representing plain files), @{typ att} (for
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   129
  attributes of directory nodes), and @{typ name} (for the index type
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   130
  of directory nodes).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   131
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   132
41579
4031fb078acc type_synonym;
wenzelm
parents: 41413
diff changeset
   133
type_synonym "file" = "(att \<times> string, att, name) env"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   134
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   135
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   136
  \medskip The HOL library also provides @{term lookup} and @{term
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   137
  update} operations for general tree structures with the subsequent
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   138
  primitive recursive characterizations.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   139
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   140
  \medskip
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   141
  {\def\isastyleminor{\isastyle}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   142
  \begin{tabular}{l}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   143
  @{term [source] "lookup :: ('a, 'b, 'c) env \<Rightarrow> 'c list \<Rightarrow> ('a, 'b, 'c) env option"} \\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   144
  @{term [source]
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   145
  "update :: 'c list \<Rightarrow> ('a, 'b, 'c) env option \<Rightarrow> ('a, 'b, 'c) env \<Rightarrow> ('a, 'b, 'c) env"} \\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   146
  \end{tabular}}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   147
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   148
  @{thm [display, indent = 2, eta_contract = false] lookup_eq [no_vars]}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   149
  @{thm [display, indent = 2, eta_contract = false] update_eq [no_vars]}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   150
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   151
  Several further properties of these operations are proven in
13380
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
   152
  \cite{Bauer-et-al:2002:HOL-Library}.  These will be routinely used
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   153
  later on without further notice.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   154
17146
67e9b86ed211 Put quotation marks around some occurrences of "file", since it is now
berghofe
parents: 16670
diff changeset
   155
  \bigskip Apparently, the elements of type @{typ "file"} contain an
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   156
  @{typ att} component in either case.  We now define a few auxiliary
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   157
  operations to manipulate this field uniformly, following the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   158
  conventions for record types in Isabelle/HOL
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   159
  \cite{Nipkow-et-al:2000:HOL}.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   160
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   161
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   162
definition
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   163
  "attributes file =
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   164
    (case file of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   165
      Val (att, text) \<Rightarrow> att
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   166
    | Env att dir \<Rightarrow> att)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   167
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21372
diff changeset
   168
definition
21001
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   169
  "map_attributes f file =
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   170
    (case file of
21001
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   171
      Val (att, text) \<Rightarrow> Val (f att, text)
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   172
    | Env att dir \<Rightarrow> Env (f att) dir)"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   173
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   174
lemma [simp]: "attributes (Val (att, text)) = att"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   175
  by (simp add: attributes_def)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   176
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   177
lemma [simp]: "attributes (Env att dir) = att"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   178
  by (simp add: attributes_def)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   179
21001
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   180
lemma [simp]: "attributes (map_attributes f file) = f (attributes file)"
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   181
  by (cases "file") (simp_all add: attributes_def map_attributes_def
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   182
    split_tupled_all)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   183
21001
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   184
lemma [simp]: "map_attributes f (Val (att, text)) = Val (f att, text)"
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   185
  by (simp add: map_attributes_def)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   186
21001
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   187
lemma [simp]: "map_attributes f (Env att dir) = Env (f att) dir"
408f3a1cef2e replaced attributes_update by map_attributes;
wenzelm
parents: 20503
diff changeset
   188
  by (simp add: map_attributes_def)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   189
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   190
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   191
subsection {* Initial file-systems *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   192
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   193
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   194
  Given a set of \emph{known users} a file-system shall be initialized
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   195
  by providing an empty home directory for each user, with read-only
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   196
  access for everyone else.  (Note that we may directly use the user
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   197
  id as home directory name, since both types have been identified.)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   198
  Certainly, the very root directory is owned by the super user (who
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   199
  has user id 0).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   200
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   201
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   202
definition
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   203
  "init users =
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   204
    Env \<lparr>owner = 0, others = {Readable}\<rparr>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   205
      (\<lambda>u. if u \<in> users then Some (Env \<lparr>owner = u, others = {Readable}\<rparr> empty)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   206
        else None)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   207
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   208
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   209
subsection {* Accessing file-systems *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   210
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   211
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   212
  The main internal file-system operation is access of a file by a
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   213
  user, requesting a certain set of permissions.  The resulting @{typ
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   214
  "file option"} indicates if a file had been present at the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   215
  corresponding @{typ path} and if access was granted according to the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   216
  permissions recorded within the file-system.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   217
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   218
  Note that by the rules of Unix file-system security (e.g.\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   219
  \cite{Tanenbaum:1992}) both the super-user and owner may always
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   220
  access a file unconditionally (in our simplified model).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   221
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   222
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   223
definition
20321
wenzelm
parents: 19363
diff changeset
   224
  "access root path uid perms =
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   225
    (case lookup root path of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   226
      None \<Rightarrow> None
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   227
    | Some file \<Rightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   228
        if uid = 0
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   229
          \<or> uid = owner (attributes file)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   230
          \<or> perms \<subseteq> others (attributes file)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   231
        then Some file
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   232
        else None)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   233
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   234
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   235
  \medskip Successful access to a certain file is the main
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   236
  prerequisite for system-calls to be applicable (cf.\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   237
  \secref{sec:unix-trans}).  Any modification of the file-system is
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   238
  then performed using the basic @{term update} operation.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   239
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   240
  \medskip We see that @{term access} is just a wrapper for the basic
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   241
  @{term lookup} function, with additional checking of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   242
  attributes. Subsequently we establish a few auxiliary facts that
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   243
  stem from the primitive @{term lookup} used within @{term access}.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   244
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   245
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   246
lemma access_empty_lookup: "access root path uid {} = lookup root path"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   247
  by (simp add: access_def split: option.splits)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   248
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   249
lemma access_some_lookup:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   250
  "access root path uid perms = Some file \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   251
    lookup root path = Some file"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   252
  by (simp add: access_def split: option.splits if_splits)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   253
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   254
lemma access_update_other:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   255
  assumes parallel: "path' \<parallel> path"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   256
  shows "access (update path' opt root) path uid perms = access root path uid perms"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   257
proof -
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   258
  from parallel obtain y z xs ys zs where
11072
wenzelm
parents: 11004
diff changeset
   259
      "y \<noteq> z" and "path' = xs @ y # ys" and "path = xs @ z # zs"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   260
    by (blast dest: parallel_decomp)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   261
  then have "lookup (update path' opt root) path = lookup root path"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   262
    by (blast intro: lookup_update_other)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   263
  then show ?thesis by (simp only: access_def)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   264
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   265
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   266
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   267
section {* File-system transitions \label{sec:unix-trans} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   268
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   269
subsection {* Unix system calls \label{sec:unix-syscall} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   270
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   271
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   272
  According to established operating system design (cf.\
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   273
  \cite{Tanenbaum:1992}) user space processes may only initiate system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   274
  operations by a fixed set of \emph{system-calls}.  This enables the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   275
  kernel to enforce certain security policies in the first
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   276
  place.\footnote{Incidently, this is the very same principle employed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   277
  by any ``LCF-style'' theorem proving system according to Milner's
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   278
  principle of ``correctness by construction'', such as Isabelle/HOL
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   279
  itself.}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   280
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   281
  \medskip In our model of Unix we give a fixed datatype @{text
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   282
  operation} for the syntax of system-calls, together with an
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   283
  inductive definition of file-system state transitions of the form
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   284
  @{text "root \<midarrow>x\<rightarrow> root'"} for the operational semantics.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   285
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   286
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   287
datatype operation =
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   288
    Read uid string path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   289
  | Write uid string path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   290
  | Chmod uid perms path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   291
  | Creat uid perms path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   292
  | Unlink uid path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   293
  | Mkdir uid perms path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   294
  | Rmdir uid path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   295
  | Readdir uid "name set" path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   296
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   297
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   298
  The @{typ uid} field of an operation corresponds to the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   299
  \emph{effective user id} of the underlying process, although our
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   300
  model never mentions processes explicitly.  The other parameters are
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   301
  provided as arguments by the caller; the @{term path} one is common
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   302
  to all kinds of system-calls.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   303
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   304
44601
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   305
primrec uid_of :: "operation \<Rightarrow> uid"
25974
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   306
where
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   307
    "uid_of (Read uid text path) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   308
  | "uid_of (Write uid text path) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   309
  | "uid_of (Chmod uid perms path) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   310
  | "uid_of (Creat uid perms path) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   311
  | "uid_of (Unlink uid path) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   312
  | "uid_of (Mkdir uid path perms) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   313
  | "uid_of (Rmdir uid path) = uid"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   314
  | "uid_of (Readdir uid names path) = uid"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   315
44601
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   316
primrec path_of :: "operation \<Rightarrow> path"
25974
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   317
where
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   318
    "path_of (Read uid text path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   319
  | "path_of (Write uid text path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   320
  | "path_of (Chmod uid perms path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   321
  | "path_of (Creat uid perms path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   322
  | "path_of (Unlink uid path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   323
  | "path_of (Mkdir uid perms path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   324
  | "path_of (Rmdir uid path) = path"
0cb35fa9c6fa modernized primrec;
wenzelm
parents: 25706
diff changeset
   325
  | "path_of (Readdir uid names path) = path"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   326
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   327
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   328
  \medskip Note that we have omitted explicit @{text Open} and @{text
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   329
  Close} operations, pretending that @{term Read} and @{term Write}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   330
  would already take care of this behind the scenes.  Thus we have
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   331
  basically treated actual sequences of real system-calls @{text
13380
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
   332
  "open"}--@{text read}/@{text write}--@{text close} as atomic.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   333
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   334
  In principle, this could make big a difference in a model with
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   335
  explicit concurrent processes.  On the other hand, even on a real
13380
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
   336
  Unix system the exact scheduling of concurrent @{text "open"} and
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   337
  @{text close} operations does \emph{not} directly affect the success
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   338
  of corresponding @{text read} or @{text write}.  Unix allows several
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   339
  processes to have files opened at the same time, even for writing!
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   340
  Certainly, the result from reading the contents later may be hard to
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   341
  predict, but the system-calls involved here will succeed in any
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   342
  case.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   343
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   344
  \bigskip The operational semantics of system calls is now specified
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   345
  via transitions of the file-system configuration.  This is expressed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   346
  as an inductive relation (although there is no actual recursion
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   347
  involved here).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   348
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   349
44601
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   350
inductive transition :: "file \<Rightarrow> operation \<Rightarrow> file \<Rightarrow> bool"
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   351
  ("_ \<midarrow>_\<rightarrow> _" [90, 1000, 90] 100)
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   352
where
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   353
  read:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   354
    "access root path uid {Readable} = Some (Val (att, text)) \<Longrightarrow>
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   355
      root \<midarrow>(Read uid text path)\<rightarrow> root" |
36504
7cc639e20cb2 avoid clash with keyword 'write';
wenzelm
parents: 25974
diff changeset
   356
  "write":
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   357
    "access root path uid {Writable} = Some (Val (att, text')) \<Longrightarrow>
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   358
      root \<midarrow>(Write uid text path)\<rightarrow> update path (Some (Val (att, text))) root" |
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   359
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   360
  chmod:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   361
    "access root path uid {} = Some file \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   362
      uid = 0 \<or> uid = owner (attributes file) \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   363
      root \<midarrow>(Chmod uid perms path)\<rightarrow> update path
25706
45d090186bbe accomodate to replacement of K_record by %x.c
schirmer
parents: 25592
diff changeset
   364
        (Some (map_attributes (others_update (\<lambda>_. perms)) file)) root" |
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   365
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   366
  creat:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   367
    "path = parent_path @ [name] \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   368
      access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   369
      access root path uid {} = None \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   370
      root \<midarrow>(Creat uid perms path)\<rightarrow> update path
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   371
        (Some (Val (\<lparr>owner = uid, others = perms\<rparr>, []))) root" |
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   372
  unlink:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   373
    "path = parent_path @ [name] \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   374
      access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   375
      access root path uid {} = Some (Val plain) \<Longrightarrow>
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   376
      root \<midarrow>(Unlink uid path)\<rightarrow> update path None root" |
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   377
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   378
  mkdir:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   379
    "path = parent_path @ [name] \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   380
      access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   381
      access root path uid {} = None \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   382
      root \<midarrow>(Mkdir uid perms path)\<rightarrow> update path
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   383
        (Some (Env \<lparr>owner = uid, others = perms\<rparr> empty)) root" |
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   384
  rmdir:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   385
    "path = parent_path @ [name] \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   386
      access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   387
      access root path uid {} = Some (Env att' empty) \<Longrightarrow>
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   388
      root \<midarrow>(Rmdir uid path)\<rightarrow> update path None root" |
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   389
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   390
  readdir:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   391
    "access root path uid {Readable} = Some (Env att dir) \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   392
      names = dom dir \<Longrightarrow>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   393
      root \<midarrow>(Readdir uid names path)\<rightarrow> root"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   394
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   395
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   396
  \medskip Certainly, the above specification is central to the whole
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   397
  formal development.  Any of the results to be established later on
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   398
  are only meaningful to the outside world if this transition system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   399
  provides an adequate model of real Unix systems.  This kind of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   400
  ``reality-check'' of a formal model is the well-known problem of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   401
  \emph{validation}.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   402
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   403
  If in doubt, one may consider to compare our definition with the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   404
  informal specifications given the corresponding Unix man pages, or
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   405
  even peek at an actual implementation such as
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   406
  \cite{Torvalds-et-al:Linux}.  Another common way to gain confidence
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   407
  into the formal model is to run simple simulations (see
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   408
  \secref{sec:unix-examples}), and check the results with that of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   409
  experiments performed on a real Unix system.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   410
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   411
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   412
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   413
subsection {* Basic properties of single transitions \label{sec:unix-single-trans} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   414
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   415
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   416
  The transition system @{text "root \<midarrow>x\<rightarrow> root'"} defined above
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   417
  determines a unique result @{term root'} from given @{term root} and
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   418
  @{term x} (this is holds rather trivially, since there is even only
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   419
  one clause for each operation).  This uniqueness statement will
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   420
  simplify our subsequent development to some extent, since we only
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   421
  have to reason about a partial function rather than a general
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   422
  relation.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   423
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   424
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   425
theorem transition_uniq:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   426
  assumes root': "root \<midarrow>x\<rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   427
    and root'': "root \<midarrow>x\<rightarrow> root''"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   428
  shows "root' = root''"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   429
  using root''
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   430
proof cases
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   431
  case read
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   432
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   433
next
36504
7cc639e20cb2 avoid clash with keyword 'write';
wenzelm
parents: 25974
diff changeset
   434
  case "write"
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   435
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   436
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   437
  case chmod
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   438
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   439
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   440
  case creat
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   441
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   442
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   443
  case unlink
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   444
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   445
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   446
  case mkdir
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   447
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   448
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   449
  case rmdir
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   450
  with root' show ?thesis by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   451
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   452
  case readdir
45671
1c769a2a2421 tuned proofs;
wenzelm
parents: 44890
diff changeset
   453
  with root' show ?thesis by cases blast+
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   454
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   455
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   456
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   457
  \medskip Apparently, file-system transitions are \emph{type-safe} in
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   458
  the sense that the result of transforming an actual directory yields
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   459
  again a directory.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   460
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   461
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   462
theorem transition_type_safe:
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   463
  assumes tr: "root \<midarrow>x\<rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   464
    and inv: "\<exists>att dir. root = Env att dir"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   465
  shows "\<exists>att dir. root' = Env att dir"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   466
proof (cases "path_of x")
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   467
  case Nil
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   468
  with tr inv show ?thesis
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   469
    by cases (auto simp add: access_def split: if_splits)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   470
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   471
  case Cons
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   472
  from tr obtain opt where
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   473
      "root' = root \<or> root' = update (path_of x) opt root"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   474
    by cases auto
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   475
  with inv Cons show ?thesis
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   476
    by (auto simp add: update_eq split: list.splits)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   477
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   478
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   479
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   480
  The previous result may be seen as the most basic invariant on the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   481
  file-system state that is enforced by any proper kernel
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   482
  implementation.  So user processes --- being bound to the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   483
  system-call interface --- may never mess up a file-system such that
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   484
  the root becomes a plain file instead of a directory, which would be
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   485
  a strange situation indeed.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   486
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   487
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   488
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   489
subsection {* Iterated transitions *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   490
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   491
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   492
  Iterated system transitions via finite sequences of system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   493
  operations are modeled inductively as follows.  In a sense, this
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   494
  relation describes the cumulative effect of the sequence of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   495
  system-calls issued by a number of running processes over a finite
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   496
  amount of time.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   497
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   498
44601
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   499
inductive transitions :: "file \<Rightarrow> operation list \<Rightarrow> file \<Rightarrow> bool"
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   500
  ("_ =_\<Rightarrow> _" [90, 1000, 90] 100)
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   501
where
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   502
    nil: "root =[]\<Rightarrow> root"
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   503
  | cons: "root \<midarrow>x\<rightarrow> root' \<Longrightarrow> root' =xs\<Rightarrow> root'' \<Longrightarrow> root =(x # xs)\<Rightarrow> root''"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   504
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   505
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   506
  \medskip We establish a few basic facts relating iterated
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   507
  transitions with single ones, according to the recursive structure
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   508
  of lists.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   509
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   510
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   511
lemma transitions_nil_eq: "root =[]\<Rightarrow> root' = (root = root')"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   512
proof
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   513
  assume "root =[]\<Rightarrow> root'"
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   514
  then show "root = root'" by cases simp_all
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   515
next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   516
  assume "root = root'"
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   517
  then show "root =[]\<Rightarrow> root'" by (simp only: transitions.nil)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   518
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   519
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   520
lemma transitions_cons_eq:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   521
  "root =(x # xs)\<Rightarrow> root'' = (\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root'')"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   522
proof
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   523
  assume "root =(x # xs)\<Rightarrow> root''"
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   524
  then show "\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root''"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   525
    by cases auto
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   526
next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   527
  assume "\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root''"
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   528
  then show "root =(x # xs)\<Rightarrow> root''"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   529
    by (blast intro: transitions.cons)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   530
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   531
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   532
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   533
  The next two rules show how to ``destruct'' known transition
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   534
  sequences.  Note that the second one actually relies on the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   535
  uniqueness property of the basic transition system (see
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   536
  \secref{sec:unix-single-trans}).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   537
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   538
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   539
lemma transitions_nilD: "root =[]\<Rightarrow> root' \<Longrightarrow> root' = root"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   540
  by (simp add: transitions_nil_eq)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   541
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   542
lemma transitions_consD:
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   543
  assumes list: "root =(x # xs)\<Rightarrow> root''"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   544
    and hd: "root \<midarrow>x\<rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   545
  shows "root' =xs\<Rightarrow> root''"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   546
proof -
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   547
  from list obtain r' where r': "root \<midarrow>x\<rightarrow> r'" and root'': "r' =xs\<Rightarrow> root''"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   548
    by cases simp_all
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   549
  from r' hd have "r' = root'" by (rule transition_uniq)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   550
  with root'' show "root' =xs\<Rightarrow> root''" by simp
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   551
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   552
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   553
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   554
  \medskip The following fact shows how an invariant @{term Q} of
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   555
  single transitions with property @{term P} may be transferred to
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   556
  iterated transitions.  The proof is rather obvious by rule induction
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   557
  over the definition of @{term "root =xs\<Rightarrow> root'"}.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   558
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   559
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   560
lemma transitions_invariant:
47175
6b906beec36f tuned whitespace;
wenzelm
parents: 47099
diff changeset
   561
  assumes r: "\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r'"
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   562
    and trans: "root =xs\<Rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   563
  shows "Q root \<Longrightarrow> \<forall>x \<in> set xs. P x \<Longrightarrow> Q root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   564
  using trans
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   565
proof induct
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   566
  case nil
23463
9953ff53cc64 tuned proofs -- avoid implicit prems;
wenzelm
parents: 23394
diff changeset
   567
  show ?case by (rule nil.prems)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   568
next
21372
4a0a83378669 converted to 'inductive2';
wenzelm
parents: 21226
diff changeset
   569
  case (cons root x root' xs root'')
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   570
  note P = `\<forall>x \<in> set (x # xs). P x`
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   571
  then have "P x" by simp
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   572
  with `root \<midarrow>x\<rightarrow> root'` and `Q root` have Q': "Q root'" by (rule r)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   573
  from P have "\<forall>x \<in> set xs. P x" by simp
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   574
  with Q' show "Q root''" by (rule cons.hyps)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   575
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   576
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   577
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   578
  As an example of applying the previous result, we transfer the basic
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   579
  type-safety property (see \secref{sec:unix-single-trans}) from
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   580
  single transitions to iterated ones, which is a rather obvious
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   581
  result indeed.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   582
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   583
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   584
theorem transitions_type_safe:
16670
wenzelm
parents: 16417
diff changeset
   585
  assumes "root =xs\<Rightarrow> root'"
wenzelm
parents: 16417
diff changeset
   586
    and "\<exists>att dir. root = Env att dir"
wenzelm
parents: 16417
diff changeset
   587
  shows "\<exists>att dir. root' = Env att dir"
23394
474ff28210c0 tuned proofs;
wenzelm
parents: 21404
diff changeset
   588
  using transition_type_safe and assms
16670
wenzelm
parents: 16417
diff changeset
   589
proof (rule transitions_invariant)
wenzelm
parents: 16417
diff changeset
   590
  show "\<forall>x \<in> set xs. True" by blast
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   591
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   592
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   593
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   594
section {* Executable sequences *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   595
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   596
text {*
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   597
  An inductively defined relation such as the one of @{text "root \<midarrow>x\<rightarrow>
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   598
  root'"} (see \secref{sec:unix-syscall}) has two main aspects.  First
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   599
  of all, the resulting system admits a certain set of transition
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   600
  rules (introductions) as given in the specification.  Furthermore,
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   601
  there is an explicit least-fixed-point construction involved, which
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   602
  results in induction (and case-analysis) rules to eliminate known
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   603
  transitions in an exhaustive manner.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   604
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   605
  \medskip Subsequently, we explore our transition system in an
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   606
  experimental style, mainly using the introduction rules with basic
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   607
  algebraic properties of the underlying structures.  The technique
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   608
  closely resembles that of Prolog combined with functional evaluation
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   609
  in a very simple manner.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   610
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   611
  Just as the ``closed-world assumption'' is left implicit in Prolog,
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   612
  we do not refer to induction over the whole transition system here.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   613
  So this is still purely positive reasoning about possible
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   614
  executions; exhaustive reasoning will be employed only later on (see
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   615
  \secref{sec:unix-bogosity}), when we shall demonstrate that certain
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   616
  behavior is \emph{not} possible.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   617
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   618
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   619
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   620
subsection {* Possible transitions *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   621
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   622
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   623
  Rather obviously, a list of system operations can be executed within
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   624
  a certain state if there is a result state reached by an iterated
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   625
  transition.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   626
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   627
44601
04f64e602fa6 tuned document;
wenzelm
parents: 44236
diff changeset
   628
definition "can_exec root xs = (\<exists>root'. root =xs\<Rightarrow> root')"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   629
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   630
lemma can_exec_nil: "can_exec root []"
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18576
diff changeset
   631
  unfolding can_exec_def by (blast intro: transitions.intros)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   632
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   633
lemma can_exec_cons:
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   634
    "root \<midarrow>x\<rightarrow> root' \<Longrightarrow> can_exec root' xs \<Longrightarrow> can_exec root (x # xs)"
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18576
diff changeset
   635
  unfolding can_exec_def by (blast intro: transitions.intros)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   636
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   637
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   638
  \medskip In case that we already know that a certain sequence can be
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   639
  executed we may destruct it backwardly into individual transitions.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   640
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   641
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   642
lemma can_exec_snocD: "can_exec root (xs @ [y])
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   643
    \<Longrightarrow> \<exists>root' root''. root =xs\<Rightarrow> root' \<and> root' \<midarrow>y\<rightarrow> root''"
20503
503ac4c5ef91 induct method: renamed 'fixing' to 'arbitrary';
wenzelm
parents: 20321
diff changeset
   644
proof (induct xs arbitrary: root)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   645
  case Nil
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   646
  then show ?case
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   647
    by (simp add: can_exec_def transitions_nil_eq transitions_cons_eq)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   648
next
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   649
  case (Cons x xs)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   650
  from `can_exec root ((x # xs) @ [y])` obtain r root'' where
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   651
      x: "root \<midarrow>x\<rightarrow> r" and
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   652
      xs_y: "r =(xs @ [y])\<Rightarrow> root''"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   653
    by (auto simp add: can_exec_def transitions_nil_eq transitions_cons_eq)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   654
  from xs_y Cons.hyps obtain root' r' where xs: "r =xs\<Rightarrow> root'" and y: "root' \<midarrow>y\<rightarrow> r'"
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18576
diff changeset
   655
    unfolding can_exec_def by blast
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   656
  from x xs have "root =(x # xs)\<Rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   657
    by (rule transitions.cons)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   658
  with y show ?case by blast
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   659
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   660
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   661
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   662
subsection {* Example executions \label{sec:unix-examples} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   663
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   664
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   665
  We are now ready to perform a few experiments within our formal
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   666
  model of Unix system-calls.  The common technique is to alternate
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   667
  introduction rules of the transition system (see
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   668
  \secref{sec:unix-trans}), and steps to solve any emerging side
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   669
  conditions using algebraic properties of the underlying file-system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   670
  structures (see \secref{sec:unix-file-system}).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   671
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   672
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   673
lemmas eval = access_def init_def
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   674
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   675
theorem "u \<in> users \<Longrightarrow> can_exec (init users)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   676
    [Mkdir u perms [u, name]]"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   677
  apply (rule can_exec_cons)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   678
    -- {* back-chain @{term can_exec} (of @{term [source] Cons}) *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   679
  apply (rule mkdir)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   680
    -- {* back-chain @{term Mkdir} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   681
  apply (force simp add: eval)+
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   682
    -- {* solve preconditions of @{term Mkdir} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   683
  apply (simp add: eval)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   684
    -- {* peek at resulting dir (optional) *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   685
  txt {* @{subgoals [display]} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   686
  apply (rule can_exec_nil)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   687
    -- {* back-chain @{term can_exec} (of @{term [source] Nil}) *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   688
  done
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   689
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   690
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   691
  By inspecting the result shown just before the final step above, we
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   692
  may gain confidence that our specification of Unix system-calls
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   693
  actually makes sense.  Further common errors are usually exhibited
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   694
  when preconditions of transition rules fail unexpectedly.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   695
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   696
  \medskip Here are a few further experiments, using the same
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   697
  techniques as before.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   698
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   699
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   700
theorem "u \<in> users \<Longrightarrow> can_exec (init users)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   701
   [Creat u perms [u, name],
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   702
    Unlink u [u, name]]"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   703
  apply (rule can_exec_cons)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   704
  apply (rule creat)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   705
  apply (force simp add: eval)+
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   706
  apply (simp add: eval)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   707
  apply (rule can_exec_cons)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   708
  apply (rule unlink)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   709
  apply (force simp add: eval)+
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   710
  apply (simp add: eval)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   711
  txt {* peek at result: @{subgoals [display]} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   712
  apply (rule can_exec_nil)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   713
  done
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   714
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   715
theorem "u \<in> users \<Longrightarrow> Writable \<in> perms\<^isub>1 \<Longrightarrow>
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   716
  Readable \<in> perms\<^isub>2 \<Longrightarrow> name\<^isub>3 \<noteq> name\<^isub>4 \<Longrightarrow>
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   717
  can_exec (init users)
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   718
   [Mkdir u perms\<^isub>1 [u, name\<^isub>1],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   719
    Mkdir u' perms\<^isub>2 [u, name\<^isub>1, name\<^isub>2],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   720
    Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>3],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   721
    Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>4],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   722
    Readdir u {name\<^isub>3, name\<^isub>4} [u, name\<^isub>1, name\<^isub>2]]"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   723
  apply (rule can_exec_cons, rule transition.intros,
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   724
    (force simp add: eval)+, (simp add: eval)?)+
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   725
  txt {* peek at result: @{subgoals [display]} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   726
  apply (rule can_exec_nil)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   727
  done
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   728
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   729
theorem "u \<in> users \<Longrightarrow> Writable \<in> perms\<^isub>1 \<Longrightarrow> Readable \<in> perms\<^isub>3 \<Longrightarrow>
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   730
  can_exec (init users)
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   731
   [Mkdir u perms\<^isub>1 [u, name\<^isub>1],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   732
    Mkdir u' perms\<^isub>2 [u, name\<^isub>1, name\<^isub>2],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   733
    Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>3],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   734
    Write u' ''foo'' [u, name\<^isub>1, name\<^isub>2, name\<^isub>3],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   735
    Read u ''foo'' [u, name\<^isub>1, name\<^isub>2, name\<^isub>3]]"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   736
  apply (rule can_exec_cons, rule transition.intros,
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   737
    (force simp add: eval)+, (simp add: eval)?)+
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   738
  txt {* peek at result: @{subgoals [display]} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   739
  apply (rule can_exec_nil)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   740
  done
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   741
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   742
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   743
section {* Odd effects --- treated formally \label{sec:unix-bogosity} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   744
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   745
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   746
  We are now ready to give a completely formal treatment of the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   747
  slightly odd situation discussed in the introduction (see
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   748
  \secref{sec:unix-intro}): the file-system can easily reach a state
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   749
  where a user is unable to remove his very own directory, because it
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   750
  is still populated by items placed there by another user in an
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   751
  uncouth manner.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   752
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   753
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   754
subsection {* The general procedure \label{sec:unix-inv-procedure} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   755
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   756
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   757
  The following theorem expresses the general procedure we are
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   758
  following to achieve the main result.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   759
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   760
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   761
theorem general_procedure:
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   762
  assumes cannot_y: "\<And>r r'. Q r \<Longrightarrow> r \<midarrow>y\<rightarrow> r' \<Longrightarrow> False"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   763
    and init_inv: "\<And>root. init users =bs\<Rightarrow> root \<Longrightarrow> Q root"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   764
    and preserve_inv: "\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   765
    and init_result: "init users =bs\<Rightarrow> root"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   766
  shows "\<not> (\<exists>xs. (\<forall>x \<in> set xs. P x) \<and> can_exec root (xs @ [y]))"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   767
proof -
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   768
  {
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   769
    fix xs
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   770
    assume Ps: "\<forall>x \<in> set xs. P x"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   771
    assume can_exec: "can_exec root (xs @ [y])"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   772
    then obtain root' root'' where
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   773
        xs: "root =xs\<Rightarrow> root'" and y: "root' \<midarrow>y\<rightarrow> root''"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   774
      by (blast dest: can_exec_snocD)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   775
    from init_result have "Q root" by (rule init_inv)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   776
    from preserve_inv xs this Ps have "Q root'"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   777
      by (rule transitions_invariant)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   778
    from this y have False by (rule cannot_y)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   779
  }
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   780
  then show ?thesis by blast
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   781
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   782
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   783
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   784
  Here @{prop "P x"} refers to the restriction on file-system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   785
  operations that are admitted after having reached the critical
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   786
  configuration; according to the problem specification this will
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   787
  become @{prop "uid_of x = user\<^isub>1"} later on.  Furthermore,
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   788
  @{term y} refers to the operations we claim to be impossible to
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   789
  perform afterwards, we will take @{term Rmdir} later.  Moreover
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   790
  @{term Q} is a suitable (auxiliary) invariant over the file-system;
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   791
  choosing @{term Q} adequately is very important to make the proof
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   792
  work (see \secref{sec:unix-inv-lemmas}).
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   793
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   794
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   795
12079
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   796
subsection {* The particular situation *}
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   797
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   798
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   799
  We introduce a few global declarations and axioms to describe our
12079
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   800
  particular situation considered here.  Thus we avoid excessive use
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   801
  of local parameters in the subsequent development.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   802
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   803
12079
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   804
locale situation =
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   805
  fixes users :: "uid set"
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   806
    and user\<^isub>1 :: uid
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   807
    and user\<^isub>2 :: uid
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   808
    and name\<^isub>1 :: name
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   809
    and name\<^isub>2 :: name
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   810
    and name\<^isub>3 :: name
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   811
    and perms\<^isub>1 :: perms
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   812
    and perms\<^isub>2 :: perms
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   813
  assumes user\<^isub>1_known: "user\<^isub>1 \<in> users"
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   814
    and user\<^isub>1_not_root: "user\<^isub>1 \<noteq> 0"
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   815
    and users_neq: "user\<^isub>1 \<noteq> user\<^isub>2"
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   816
    and perms\<^isub>1_writable: "Writable \<in> perms\<^isub>1"
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   817
    and perms\<^isub>2_not_writable: "Writable \<notin> perms\<^isub>2"
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   818
  notes facts = user\<^isub>1_known user\<^isub>1_not_root users_neq
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   819
    perms\<^isub>1_writable perms\<^isub>2_not_writable
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
   820
begin
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   821
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
   822
definition
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   823
  "bogus =
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   824
     [Mkdir user\<^isub>1 perms\<^isub>1 [user\<^isub>1, name\<^isub>1],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   825
      Mkdir user\<^isub>2 perms\<^isub>2 [user\<^isub>1, name\<^isub>1, name\<^isub>2],
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   826
      Creat user\<^isub>2 perms\<^isub>2 [user\<^isub>1, name\<^isub>1, name\<^isub>2, name\<^isub>3]]"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21372
diff changeset
   827
definition
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
   828
  "bogus_path = [user\<^isub>1, name\<^isub>1, name\<^isub>2]"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   829
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   830
text {*
12079
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   831
  The @{term bogus} operations are the ones that lead into the uncouth
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   832
  situation; @{term bogus_path} is the key position within the
054153c48bde use locales instead of consts/axioms;
wenzelm
parents: 11881
diff changeset
   833
  file-system where things go awry.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   834
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   835
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   836
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   837
subsection {* Invariance lemmas \label{sec:unix-inv-lemmas} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   838
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   839
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   840
  The following invariant over the root file-system describes the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   841
  bogus situation in an abstract manner: located at a certain @{term
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   842
  path} within the file-system is a non-empty directory that is
43433
f67364f35789 tuned text;
wenzelm
parents: 41579
diff changeset
   843
  neither owned nor writable by @{term user\<^isub>1}.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   844
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   845
47099
56adbf5bcc82 resolve invariant constant name clash
kuncar
parents: 46206
diff changeset
   846
56adbf5bcc82 resolve invariant constant name clash
kuncar
parents: 46206
diff changeset
   847
47215
ca516aa5b6ce "invariant" is free in main HOL (cf. 56adbf5bcc82, e64ffc96a49f);
wenzelm
parents: 47175
diff changeset
   848
definition
20321
wenzelm
parents: 19363
diff changeset
   849
  "invariant root path =
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   850
    (\<exists>att dir.
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   851
      access root path user\<^isub>1 {} = Some (Env att dir) \<and> dir \<noteq> empty \<and>
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   852
      user\<^isub>1 \<noteq> owner att \<and>
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   853
      access root path user\<^isub>1 {Writable} = None)"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   854
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   855
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   856
  Following the general procedure (see
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   857
  \secref{sec:unix-inv-procedure}) we will now establish the three key
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   858
  lemmas required to yield the final result.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   859
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   860
  \begin{enumerate}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   861
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   862
  \item The invariant is sufficiently strong to entail the
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   863
  pathological case that @{term user\<^isub>1} is unable to remove the
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   864
  (owned) directory at @{term "[user\<^isub>1, name\<^isub>1]"}.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   865
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   866
  \item The invariant does hold after having executed the @{term
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   867
  bogus} list of operations (starting with an initial file-system
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   868
  configuration).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   869
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   870
  \item The invariant is preserved by any file-system operation
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   871
  performed by @{term user\<^isub>1} alone, without any help by other
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   872
  users.
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   873
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   874
  \end{enumerate}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   875
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   876
  As the invariant appears both as assumptions and conclusions in the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   877
  course of proof, its formulation is rather critical for the whole
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   878
  development to work out properly.  In particular, the third step is
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   879
  very sensitive to the invariant being either too strong or too weak.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   880
  Moreover the invariant has to be sufficiently abstract, lest the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   881
  proof become cluttered by confusing detail.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   882
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   883
  \medskip The first two lemmas are technically straight forward ---
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   884
  we just have to inspect rather special cases.
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   885
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   886
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
   887
lemma cannot_rmdir:
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   888
  assumes inv: "invariant root bogus_path"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   889
    and rmdir: "root \<midarrow>(Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1])\<rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   890
  shows False
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   891
proof -
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   892
  from inv obtain "file" where "access root bogus_path user\<^isub>1 {} = Some file"
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18576
diff changeset
   893
    unfolding invariant_def by blast
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   894
  moreover
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   895
  from rmdir obtain att where
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   896
      "access root [user\<^isub>1, name\<^isub>1] user\<^isub>1 {} = Some (Env att empty)"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   897
    by cases auto
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   898
  then have "access root ([user\<^isub>1, name\<^isub>1] @ [name\<^isub>2]) user\<^isub>1 {} = empty name\<^isub>2"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   899
    by (simp only: access_empty_lookup lookup_append_some) simp
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   900
  ultimately show False by (simp add: bogus_path_def)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   901
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   902
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
   903
lemma
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   904
  assumes init: "init users =bogus\<Rightarrow> root"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   905
  notes eval = facts access_def init_def
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   906
  shows init_invariant: "invariant root bogus_path"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   907
  using init
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   908
  apply (unfold bogus_def bogus_path_def)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   909
  apply (drule transitions_consD, rule transition.intros,
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   910
      (force simp add: eval)+, (simp add: eval)?)+
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   911
    -- "evaluate all operations"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   912
  apply (drule transitions_nilD)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   913
    -- "reach final result"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   914
  apply (simp add: invariant_def eval)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   915
    -- "check the invariant"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   916
  done
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   917
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   918
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   919
  \medskip At last we are left with the main effort to show that the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   920
  ``bogosity'' invariant is preserved by any file-system operation
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   921
  performed by @{term user\<^isub>1} alone.  Note that this holds for
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   922
  any @{term path} given, the particular @{term bogus_path} is not
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   923
  required here.
11004
wenzelm
parents: 10979
diff changeset
   924
*}
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   925
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
   926
lemma preserve_invariant:
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   927
  assumes tr: "root \<midarrow>x\<rightarrow> root'"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   928
    and inv: "invariant root path"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   929
    and uid: "uid_of x = user\<^isub>1"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   930
  shows "invariant root' path"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   931
proof -
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   932
  from inv obtain att dir where
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   933
      inv1: "access root path user\<^isub>1 {} = Some (Env att dir)" and
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   934
      inv2: "dir \<noteq> empty" and
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   935
      inv3: "user\<^isub>1 \<noteq> owner att" and
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   936
      inv4: "access root path user\<^isub>1 {Writable} = None"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   937
    by (auto simp add: invariant_def)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   938
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   939
  from inv1 have lookup: "lookup root path = Some (Env att dir)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   940
    by (simp only: access_empty_lookup)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   941
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   942
  from inv1 inv3 inv4 and user\<^isub>1_not_root
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   943
  have not_writable: "Writable \<notin> others att"
39224
39e0d3b86112 tuned proofs (based on fancy gutter icons in Isabelle/jEdit);
wenzelm
parents: 36862
diff changeset
   944
    by (auto simp add: access_def split: option.splits)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   945
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   946
  show ?thesis
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   947
  proof cases
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   948
    assume "root' = root"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   949
    with inv show "invariant root' path" by (simp only:)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   950
  next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   951
    assume changed: "root' \<noteq> root"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   952
    with tr obtain opt where root': "root' = update (path_of x) opt root"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   953
      by cases auto
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   954
    show ?thesis
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   955
    proof (rule prefix_cases)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   956
      assume "path_of x \<parallel> path"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   957
      with inv root'
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   958
      have "\<And>perms. access root' path user\<^isub>1 perms = access root path user\<^isub>1 perms"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   959
        by (simp only: access_update_other)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   960
      with inv show "invariant root' path"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   961
        by (simp only: invariant_def)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   962
    next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   963
      assume "path_of x \<le> path"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   964
      then obtain ys where path: "path = path_of x @ ys" ..
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   965
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   966
      show ?thesis
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   967
      proof (cases ys)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   968
        assume "ys = []"
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
   969
        with tr uid inv2 inv3 lookup changed path and user\<^isub>1_not_root
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   970
        have False
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   971
          by cases (auto simp add: access_empty_lookup dest: access_some_lookup)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 17455
diff changeset
   972
        then show ?thesis ..
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   973
      next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   974
        fix z zs assume ys: "ys = z # zs"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   975
        have "lookup root' path = lookup root path"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   976
        proof -
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   977
          from inv2 lookup path ys
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   978
          have look: "lookup root (path_of x @ z # zs) = Some (Env att dir)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   979
            by (simp only:)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   980
          then obtain att' dir' file' where
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   981
              look': "lookup root (path_of x) = Some (Env att' dir')" and
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   982
              dir': "dir' z = Some file'" and
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   983
              file': "lookup file' zs = Some (Env att dir)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   984
            by (blast dest: lookup_some_upper)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   985
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   986
          from tr uid changed look' dir' obtain att'' where
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   987
              look'': "lookup root' (path_of x) = Some (Env att'' dir')"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   988
            by cases (auto simp add: access_empty_lookup lookup_update_some
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   989
              dest: access_some_lookup)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   990
          with dir' file' have "lookup root' (path_of x @ z # zs) =
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   991
              Some (Env att dir)"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   992
            by (simp add: lookup_append_some)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   993
          with look path ys show ?thesis
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   994
            by simp
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   995
        qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   996
        with inv show "invariant root' path"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   997
          by (simp only: invariant_def access_def)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   998
      qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
   999
    next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1000
      assume "path < path_of x"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1001
      then obtain y ys where path: "path_of x = path @ y # ys" ..
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1002
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1003
      obtain dir' where
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1004
        lookup': "lookup root' path = Some (Env att dir')" and
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1005
        inv2': "dir' \<noteq> empty"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1006
      proof (cases ys)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1007
        assume "ys = []"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1008
        with path have parent: "path_of x = path @ [y]" by simp
17146
67e9b86ed211 Put quotation marks around some occurrences of "file", since it is now
berghofe
parents: 16670
diff changeset
  1009
        with tr uid inv4 changed obtain "file" where
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1010
            "root' = update (path_of x) (Some file) root"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1011
          by cases auto
10979
wenzelm
parents: 10966
diff changeset
  1012
        with lookup parent have "lookup root' path = Some (Env att (dir(y\<mapsto>file)))"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1013
          by (simp only: update_append_some update_cons_nil_env)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1014
        moreover have "dir(y\<mapsto>file) \<noteq> empty" by simp
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1015
        ultimately show ?thesis ..
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1016
      next
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1017
        fix z zs assume ys: "ys = z # zs"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1018
        with lookup root' path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1019
        have "lookup root' path = Some (update (y # ys) opt (Env att dir))"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1020
          by (simp only: update_append_some)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1021
        also obtain file' where
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1022
          "update (y # ys) opt (Env att dir) = Env att (dir(y\<mapsto>file'))"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1023
        proof -
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1024
          have "dir y \<noteq> None"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1025
          proof -
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1026
            have "dir y = lookup (Env att dir) [y]"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1027
              by (simp split: option.splits)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1028
            also from lookup have "\<dots> = lookup root (path @ [y])"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1029
              by (simp only: lookup_append_some)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1030
            also have "\<dots> \<noteq> None"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1031
            proof -
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1032
              from ys obtain us u where rev_ys: "ys = us @ [u]"
45671
1c769a2a2421 tuned proofs;
wenzelm
parents: 44890
diff changeset
  1033
                by (cases ys rule: rev_cases) simp
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1034
              with tr path
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1035
              have "lookup root ((path @ [y]) @ (us @ [u])) \<noteq> None \<or>
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1036
                  lookup root ((path @ [y]) @ us) \<noteq> None"
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1037
                by cases (auto dest: access_some_lookup)
18447
da548623916a removed or modified some instances of [iff]
paulson
parents: 18372
diff changeset
  1038
              then show ?thesis 
46206
d3d62b528487 tuned proofs;
wenzelm
parents: 45671
diff changeset
  1039
                by (fastforce dest!: lookup_some_append)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1040
            qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1041
            finally show ?thesis .
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1042
          qed
39224
39e0d3b86112 tuned proofs (based on fancy gutter icons in Isabelle/jEdit);
wenzelm
parents: 36862
diff changeset
  1043
          with ys show ?thesis using that by auto
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1044
        qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1045
        also have "dir(y\<mapsto>file') \<noteq> empty" by simp
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1046
        ultimately show ?thesis ..
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1047
      qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1048
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
  1049
      from lookup' have inv1': "access root' path user\<^isub>1 {} = Some (Env att dir')"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1050
        by (simp only: access_empty_lookup)
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1051
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
  1052
      from inv3 lookup' and not_writable user\<^isub>1_not_root
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
  1053
      have "access root' path user\<^isub>1 {Writable} = None"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1054
        by (simp add: access_def)
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18576
diff changeset
  1055
      with inv1' inv2' inv3 show ?thesis unfolding invariant_def by blast
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1056
    qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1057
  qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1058
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1059
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1060
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1061
subsection {* Putting it all together \label{sec:unix-main-result} *}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1062
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1063
text {*
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1064
  The main result is now imminent, just by composing the three
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1065
  invariance lemmas (see \secref{sec:unix-inv-lemmas}) according the the
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1066
  overall procedure (see \secref{sec:unix-inv-procedure}).
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1067
*}
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1068
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
  1069
corollary
13380
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
  1070
  assumes bogus: "init users =bogus\<Rightarrow> root"
17455
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
  1071
  shows "\<not> (\<exists>xs. (\<forall>x \<in> set xs. uid_of x = user\<^isub>1) \<and>
151e76f0e3c7 tuned document;
wenzelm
parents: 17146
diff changeset
  1072
    can_exec root (xs @ [Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1]]))"
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1073
proof -
13380
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
  1074
  from cannot_rmdir init_invariant preserve_invariant
ec17b9cac1fb adapted locales;
wenzelm
parents: 12079
diff changeset
  1075
    and bogus show ?thesis by (rule general_procedure)
10966
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1076
qed
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1077
8f2c27041a8e added HOL-Unix example;
wenzelm
parents:
diff changeset
  1078
end
21029
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
  1079
b98fb9cf903b locale begin/end;
wenzelm
parents: 21001
diff changeset
  1080
end