doc-src/Exercises/2001/a5/Aufgabe5.thy
author obua
Sun, 09 May 2004 23:04:36 +0200
changeset 14722 8e739a6eaf11
parent 13739 f5d0a66c8124
permissions -rw-r--r--
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13739
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     1
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     2
theory Aufgabe5 = Main:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     3
(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     4
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     5
subsection {* Interval lists *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
text {* Sets of natural numbers can be implemented as lists of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
intervals, where an interval is simply a pair of numbers. For example
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
the set @{term "{2, 3, 5, 7, 8, 9::nat}"} can be represented by the
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
list @{term "[(2, 3), (5, 5), (7::nat,9::nat)]"}. A typical application
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
is the list of free blocks of dynamically allocated memory. *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
subsubsection {* Definitions *} 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
text {* We introduce the type *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
types intervals = "(nat*nat) list" 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
text {* This type contains all possible lists of pairs of natural
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
numbers, even those that we may not recognize as meaningful
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
representations of sets. Thus you should introduce an \emph{invariant} *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
consts inv :: "intervals => bool"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
text {* that characterizes exactly those interval lists representing
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    27
sets.  (The reason why we call this an invariant will become clear
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    28
below.)  For efficiency reasons intervals should be sorted in
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    29
ascending order, the lower bound of each interval should be less or
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    30
equal the upper bound, and the intervals should be chosen as large as
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    31
possible, i.e.\ no two adjecent intervals should overlap or even touch
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    32
each other.  It turns out to be convenient to define @{term inv} in
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    33
terms of a more general function *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    34
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    35
consts inv2 :: "nat => intervals => bool"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    36
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    37
text {* such that the additional argument is a lower bound for the
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    38
intervals in the list.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    39
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    40
To relate intervals back to sets define an \emph{abstraktion funktion}  *} 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    41
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    42
consts set_of :: "intervals => nat set"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    43
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    44
text {* that yields the set corresponding to an interval list (that
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    45
satisfies the invariant).
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    46
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    47
Finally, define two primitive recursive functions
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    48
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    49
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    50
consts add :: "(nat*nat) => intervals => intervals"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    51
       rem :: "(nat*nat) => intervals => intervals"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    52
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    53
text {* for inserting and deleting an interval from an interval
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    54
list. The result should again satisfies the invariant. Hence the name:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    55
@{text inv} is invariant under the application of the operations
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    56
@{term add} and @{term rem} --- if @{text inv} holds for the input, it
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    57
must also hold for the output.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    58
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    59
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    60
subsubsection {* Proving the invariant *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    61
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    62
declare Let_def [simp]
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    63
declare split_split [split]
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    64
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    65
text {* Start off by proving the monotonicity of @{term inv2}: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    66
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    67
lemma inv2_monotone: "inv2 m ins \<Longrightarrow> n\<le>m \<Longrightarrow> inv2 n ins"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    68
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    69
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    70
text {*
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    71
Now show that @{term add} and @{term rem} preserve the invariant:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    72
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    73
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    74
theorem inv_add: "\<lbrakk> i\<le>j; inv ins \<rbrakk> \<Longrightarrow> inv (add (i,j) ins)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    75
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    76
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    77
theorem inv_rem: "\<lbrakk> i\<le>j; inv ins \<rbrakk> \<Longrightarrow> inv (rem (i,j) ins)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    78
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    79
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    80
text {* Hint: you should first prove a more general statement
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    81
(involving @{term inv2}). This will probably involve some advanced
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    82
forms of induction discussed in section~9.3.1 of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    83
\cite{isabelle-tutorial}.  *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    84
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    85
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    86
subsubsection {* Proving correctness of the implementation *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    87
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    88
text {* Show the correctness of @{term add} and @{term rem} wrt.\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    89
their counterparts @{text"\<union>"} and @{text"-"} on sets: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    90
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    91
theorem set_of_add: 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    92
  "\<lbrakk> i\<le>j; inv ins \<rbrakk> \<Longrightarrow> set_of (add (i,j) ins) = set_of [(i,j)] \<union> set_of ins"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    93
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    94
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    95
theorem set_of_rem:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    96
  "\<lbrakk> i \<le> j; inv ins \<rbrakk> \<Longrightarrow> set_of (rem (i,j) ins) = set_of ins - set_of [(i,j)]"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    97
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    98
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    99
text {* Hints: in addition to the hints above, you may also find it
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   100
useful to prove some relationship between @{term inv2} and @{term
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   101
set_of} as a lemma.  *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   102
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   103
subsubsection{* General hints *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   104
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   105
text {* 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   106
\begin{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   107
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   108
\item You should be familiar both with simplification and predicate
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   109
calculus reasoning. Automatic tactics like @{text blast} and @{text
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   110
force} can simplify the proof.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   111
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   112
\item
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   113
Equality of two sets can often be proved via the rule @{text set_ext}:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   114
@{thm[display] set_ext[of A B,no_vars]}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   115
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   116
\item 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   117
Potentially useful theorems for the simplification of sets include \\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   118
@{text "Un_Diff:"}~@{thm Un_Diff [no_vars]} \\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   119
@{text "Diff_triv:"}~@{thm Diff_triv [no_vars]}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   120
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   121
\item 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   122
Theorems can be instantiated and simplified via @{text of} and
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   123
@{text "[simplified]"} (see \cite{isabelle-tutorial}).
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   124
\end{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   125
*}(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   126
end
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   127
(*>*)