author | paulson |
Fri, 18 Mar 2005 14:31:50 +0100 | |
changeset 15614 | b098158a3f39 |
parent 15481 | fc075ae929e4 |
child 16353 | 94e565ded526 |
permissions | -rw-r--r-- |
10560 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Pairs}% |
|
11866 | 4 |
\isamarkupfalse% |
10560 | 5 |
% |
11428 | 6 |
\isamarkupsection{Pairs and Tuples% |
10560 | 7 |
} |
11866 | 8 |
\isamarkuptrue% |
10560 | 9 |
% |
10 |
\begin{isamarkuptext}% |
|
11 |
\label{sec:products} |
|
11428 | 12 |
Ordered pairs were already introduced in \S\ref{sec:pairs}, but only with a minimal |
10560 | 13 |
repertoire of operations: pairing and the two projections \isa{fst} and |
11149 | 14 |
\isa{snd}. In any non-trivial application of pairs you will find that this |
11494 | 15 |
quickly leads to unreadable nests of projections. This |
16 |
section introduces syntactic sugar to overcome this |
|
10560 | 17 |
problem: pattern matching with tuples.% |
18 |
\end{isamarkuptext}% |
|
11866 | 19 |
\isamarkuptrue% |
10560 | 20 |
% |
10878 | 21 |
\isamarkupsubsection{Pattern Matching with Tuples% |
10560 | 22 |
} |
11866 | 23 |
\isamarkuptrue% |
10560 | 24 |
% |
25 |
\begin{isamarkuptext}% |
|
10878 | 26 |
Tuples may be used as patterns in $\lambda$-abstractions, |
10560 | 27 |
for example \isa{{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharcomma}z{\isacharparenright}{\isachardot}x{\isacharplus}y{\isacharplus}z} and \isa{{\isasymlambda}{\isacharparenleft}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isacharcomma}z{\isacharparenright}{\isachardot}x{\isacharplus}y{\isacharplus}z}. In fact, |
10878 | 28 |
tuple patterns can be used in most variable binding constructs, |
29 |
and they can be nested. Here are |
|
10560 | 30 |
some typical examples: |
31 |
\begin{quote} |
|
32 |
\isa{let\ {\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isacharequal}\ f\ z\ in\ {\isacharparenleft}y{\isacharcomma}\ x{\isacharparenright}}\\ |
|
12699 | 33 |
\isa{case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isadigit{0}}\ {\isacharbar}\ {\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isacharhash}\ zs\ {\isasymRightarrow}\ x\ {\isacharplus}\ y}\\ |
10560 | 34 |
\isa{{\isasymforall}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isasymin}A{\isachardot}\ x{\isacharequal}y}\\ |
10878 | 35 |
\isa{{\isacharbraceleft}{\isacharparenleft}x{\isacharcomma}y{\isacharcomma}z{\isacharparenright}{\isachardot}\ x{\isacharequal}z{\isacharbraceright}}\\ |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13791
diff
changeset
|
36 |
\isa{{\isasymUnion}\isactrlbsub {\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}{\isasymin}A\isactrlesub \ {\isacharbraceleft}x\ {\isacharplus}\ y{\isacharbraceright}} |
11149 | 37 |
\end{quote} |
10878 | 38 |
The intuitive meanings of these expressions should be obvious. |
10560 | 39 |
Unfortunately, we need to know in more detail what the notation really stands |
10878 | 40 |
for once we have to reason about it. Abstraction |
10560 | 41 |
over pairs and tuples is merely a convenient shorthand for a more complex |
42 |
internal representation. Thus the internal and external form of a term may |
|
43 |
differ, which can affect proofs. If you want to avoid this complication, |
|
44 |
stick to \isa{fst} and \isa{snd} and write \isa{{\isasymlambda}p{\isachardot}\ fst\ p\ {\isacharplus}\ snd\ p} |
|
11494 | 45 |
instead of \isa{{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharplus}y}. These terms are distinct even though they |
46 |
denote the same function. |
|
10560 | 47 |
|
48 |
Internally, \isa{{\isasymlambda}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}{\isachardot}\ t} becomes \isa{split\ {\isacharparenleft}{\isasymlambda}x\ y{\isachardot}\ t{\isacharparenright}}, where |
|
11494 | 49 |
\cdx{split} is the uncurrying function of type \isa{{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}c{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}c} defined as |
10560 | 50 |
\begin{center} |
51 |
\isa{split\ {\isasymequiv}\ {\isasymlambda}c\ p{\isachardot}\ c\ {\isacharparenleft}fst\ p{\isacharparenright}\ {\isacharparenleft}snd\ p{\isacharparenright}} |
|
52 |
\hfill(\isa{split{\isacharunderscore}def}) |
|
53 |
\end{center} |
|
54 |
Pattern matching in |
|
55 |
other variable binding constructs is translated similarly. Thus we need to |
|
56 |
understand how to reason about such constructs.% |
|
57 |
\end{isamarkuptext}% |
|
11866 | 58 |
\isamarkuptrue% |
10560 | 59 |
% |
10878 | 60 |
\isamarkupsubsection{Theorem Proving% |
10560 | 61 |
} |
11866 | 62 |
\isamarkuptrue% |
10560 | 63 |
% |
64 |
\begin{isamarkuptext}% |
|
65 |
The most obvious approach is the brute force expansion of \isa{split}:% |
|
66 |
\end{isamarkuptext}% |
|
11866 | 67 |
\isamarkuptrue% |
10560 | 68 |
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}x{\isacharparenright}\ p\ {\isacharequal}\ fst\ p{\isachardoublequote}\isanewline |
11866 | 69 |
\isamarkupfalse% |
15481 | 70 |
\isamarkupfalse% |
11866 | 71 |
% |
10560 | 72 |
\begin{isamarkuptext}% |
73 |
This works well if rewriting with \isa{split{\isacharunderscore}def} finishes the |
|
11149 | 74 |
proof, as it does above. But if it does not, you end up with exactly what |
10560 | 75 |
we are trying to avoid: nests of \isa{fst} and \isa{snd}. Thus this |
76 |
approach is neither elegant nor very practical in large examples, although it |
|
77 |
can be effective in small ones. |
|
78 |
||
11494 | 79 |
If we consider why this lemma presents a problem, |
80 |
we quickly realize that we need to replace the variable~\isa{p} by some pair \isa{{\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}}. Then both sides of the |
|
81 |
equation would simplify to \isa{a} by the simplification rules |
|
82 |
\isa{split\ c\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\ {\isacharequal}\ c\ a\ b} and \isa{fst\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\ {\isacharequal}\ a}. |
|
83 |
To reason about tuple patterns requires some way of |
|
84 |
converting a variable of product type into a pair. |
|
10560 | 85 |
|
86 |
In case of a subterm of the form \isa{split\ f\ p} this is easy: the split |
|
87 |
rule \isa{split{\isacharunderscore}split} replaces \isa{p} by a pair:% |
|
11494 | 88 |
\index{*split (method)}% |
10560 | 89 |
\end{isamarkuptext}% |
11866 | 90 |
\isamarkuptrue% |
10560 | 91 |
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}y{\isacharparenright}\ p\ {\isacharequal}\ snd\ p{\isachardoublequote}\isanewline |
11866 | 92 |
\isamarkupfalse% |
15481 | 93 |
\isamarkupfalse% |
11866 | 94 |
\isamarkuptrue% |
15614 | 95 |
\isanewline |
11866 | 96 |
\isamarkupfalse% |
97 |
\isamarkupfalse% |
|
15481 | 98 |
\isamarkupfalse% |
11866 | 99 |
% |
10824 | 100 |
\begin{isamarkuptext}% |
10560 | 101 |
Let us look at a second example:% |
10824 | 102 |
\end{isamarkuptext}% |
11866 | 103 |
\isamarkuptrue% |
10560 | 104 |
\isacommand{lemma}\ {\isachardoublequote}let\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isacharequal}\ p\ in\ fst\ p\ {\isacharequal}\ x{\isachardoublequote}\isanewline |
11866 | 105 |
\isamarkupfalse% |
15481 | 106 |
\isamarkupfalse% |
11866 | 107 |
\isamarkuptrue% |
108 |
\isamarkupfalse% |
|
10560 | 109 |
\isacommand{lemma}\ {\isachardoublequote}p\ {\isasymin}\ {\isacharbraceleft}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharequal}y{\isacharbraceright}\ {\isasymlongrightarrow}\ fst\ p\ {\isacharequal}\ snd\ p{\isachardoublequote}\isanewline |
11866 | 110 |
\isamarkupfalse% |
15481 | 111 |
\isamarkupfalse% |
11866 | 112 |
\isamarkuptrue% |
113 |
\isamarkupfalse% |
|
114 |
\isacommand{lemma}\ {\isachardoublequote}p\ {\isasymin}\ {\isacharbraceleft}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharequal}y{\isacharbraceright}\ {\isasymLongrightarrow}\ fst\ p\ {\isacharequal}\ snd\ p{\isachardoublequote}\isamarkupfalse% |
|
115 |
\isamarkuptrue% |
|
116 |
\isamarkupfalse% |
|
10560 | 117 |
\isacommand{consts}\ swap\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}b\ {\isasymtimes}\ {\isacharprime}a{\isachardoublequote}\isanewline |
11866 | 118 |
\isamarkupfalse% |
10560 | 119 |
\isacommand{primrec}\isanewline |
11866 | 120 |
\ \ {\isachardoublequote}swap\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}x{\isacharparenright}{\isachardoublequote}\isamarkupfalse% |
121 |
% |
|
10560 | 122 |
\begin{isamarkuptext}% |
123 |
\noindent |
|
124 |
Note that the above \isacommand{primrec} definition is admissible |
|
125 |
because \isa{{\isasymtimes}} is a datatype. When we now try to prove% |
|
126 |
\end{isamarkuptext}% |
|
11866 | 127 |
\isamarkuptrue% |
128 |
\isacommand{lemma}\ {\isachardoublequote}swap{\isacharparenleft}swap\ p{\isacharparenright}\ {\isacharequal}\ p{\isachardoublequote}\isamarkupfalse% |
|
129 |
\isamarkuptrue% |
|
15481 | 130 |
\isamarkupfalse% |
11866 | 131 |
\isamarkuptrue% |
132 |
\isamarkupfalse% |
|
10560 | 133 |
\isacommand{lemma}\ {\isachardoublequote}{\isasymAnd}p\ q{\isachardot}\ swap{\isacharparenleft}swap\ p{\isacharparenright}\ {\isacharequal}\ q\ {\isasymlongrightarrow}\ p\ {\isacharequal}\ q{\isachardoublequote}\isanewline |
11866 | 134 |
\isamarkupfalse% |
15481 | 135 |
\isamarkupfalse% |
11866 | 136 |
\isamarkuptrue% |
137 |
\isamarkupfalse% |
|
15481 | 138 |
\isamarkupfalse% |
11866 | 139 |
% |
10560 | 140 |
\begin{isamarkuptext}% |
141 |
\noindent |
|
142 |
Note that we have intentionally included only \isa{split{\isacharunderscore}paired{\isacharunderscore}all} |
|
11494 | 143 |
in the first simplification step, and then we simplify again. |
144 |
This time the reason was not merely |
|
10560 | 145 |
pedagogical: |
11494 | 146 |
\isa{split{\isacharunderscore}paired{\isacharunderscore}all} may interfere with other functions |
147 |
of the simplifier. |
|
148 |
The following command could fail (here it does not) |
|
149 |
where two separate \isa{simp} applications succeed.% |
|
10560 | 150 |
\end{isamarkuptext}% |
11866 | 151 |
\isamarkuptrue% |
152 |
\isamarkupfalse% |
|
15481 | 153 |
\isamarkupfalse% |
11866 | 154 |
\isamarkupfalse% |
155 |
% |
|
10560 | 156 |
\begin{isamarkuptext}% |
157 |
\noindent |
|
11494 | 158 |
Finally, the simplifier automatically splits all \isa{{\isasymforall}} and |
159 |
\isa{{\isasymexists}}-quantified variables:% |
|
10560 | 160 |
\end{isamarkuptext}% |
11866 | 161 |
\isamarkuptrue% |
10560 | 162 |
\isacommand{lemma}\ {\isachardoublequote}{\isasymforall}p{\isachardot}\ {\isasymexists}q{\isachardot}\ swap\ p\ {\isacharequal}\ swap\ q{\isachardoublequote}\isanewline |
11866 | 163 |
\isamarkupfalse% |
15481 | 164 |
\isamarkupfalse% |
11866 | 165 |
% |
10560 | 166 |
\begin{isamarkuptext}% |
167 |
\noindent |
|
11494 | 168 |
To turn off this automatic splitting, just disable the |
10560 | 169 |
responsible simplification rules: |
170 |
\begin{center} |
|
10654 | 171 |
\isa{{\isacharparenleft}{\isasymforall}x{\isachardot}\ P\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isasymforall}a\ b{\isachardot}\ P\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}{\isacharparenright}} |
10560 | 172 |
\hfill |
173 |
(\isa{split{\isacharunderscore}paired{\isacharunderscore}All})\\ |
|
10654 | 174 |
\isa{{\isacharparenleft}{\isasymexists}x{\isachardot}\ P\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isasymexists}a\ b{\isachardot}\ P\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}{\isacharparenright}} |
10560 | 175 |
\hfill |
176 |
(\isa{split{\isacharunderscore}paired{\isacharunderscore}Ex}) |
|
177 |
\end{center}% |
|
178 |
\end{isamarkuptext}% |
|
11866 | 179 |
\isamarkuptrue% |
180 |
\isamarkupfalse% |
|
10560 | 181 |
\end{isabellebody}% |
182 |
%%% Local Variables: |
|
183 |
%%% mode: latex |
|
184 |
%%% TeX-master: "root" |
|
185 |
%%% End: |