doc-src/TutorialI/Misc/case_splits.thy
author nipkow
Tue, 29 Aug 2000 15:13:10 +0200
changeset 9721 7e51c9f3d5a0
parent 9541 d17c0b34d5c8
child 9723 a977245dfc8a
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     1
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     2
theory case_splits = Main:;
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     3
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     4
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     5
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     6
Goals containing \isaindex{if}-expressions are usually proved by case
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     7
distinction on the condition of the \isa{if}. For example the goal
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     8
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     9
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    10
lemma "\\<forall>xs. if xs = [] then rev xs = [] else rev xs \\<noteq> []";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    11
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    12
txt{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    13
can be split into
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    14
\begin{isabellepar}%
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    15
~1.~{\isasymforall}xs.~(xs~=~[]~{\isasymlongrightarrow}~rev~xs~=~[])~{\isasymand}~(xs~{\isasymnoteq}~[]~{\isasymlongrightarrow}~rev~xs~{\isasymnoteq}~[])%
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    16
\end{isabellepar}%
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    17
by a degenerate form of simplification
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    18
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    19
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    20
apply(simp only: split: split_if);
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    21
(*<*)oops;(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    22
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    23
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    24
where no simplification rules are included (\isa{only:} is followed by the
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    25
empty list of theorems) but the rule \isaindexbold{split_if} for
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    26
splitting \isa{if}s is added (via the modifier \isa{split:}). Because
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    27
case-splitting on \isa{if}s is almost always the right proof strategy, the
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    28
simplifier performs it automatically. Try \isacommand{apply}\isa{(simp)}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    29
on the initial goal above.
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    30
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    31
This splitting idea generalizes from \isa{if} to \isaindex{case}:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    32
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    33
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    34
lemma "(case xs of [] \\<Rightarrow> zs | y#ys \\<Rightarrow> y#(ys@zs)) = xs@zs";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    35
txt{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    36
becomes
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    37
\begin{isabellepar}%
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    38
~1.~(xs~=~[]~{\isasymlongrightarrow}~zs~=~xs~@~zs)~{\isasymand}\isanewline
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    39
~~~~({\isasymforall}a~list.~xs~=~a~\#~list~{\isasymlongrightarrow}~a~\#~list~@~zs~=~xs~@~zs)%
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    40
\end{isabellepar}%
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    41
by typing
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    42
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    43
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    44
apply(simp only: split: list.split);
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    45
(*<*)oops;(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    46
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    47
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    48
In contrast to \isa{if}-expressions, the simplifier does not split
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    49
\isa{case}-expressions by default because this can lead to nontermination
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    50
in case of recursive datatypes. Again, if the \isa{only:} modifier is
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    51
dropped, the above goal is solved,
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    52
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    53
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    54
lemma "(case xs of [] \\<Rightarrow> zs | y#ys \\<Rightarrow> y#(ys@zs)) = xs@zs";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    55
(*>*)
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
    56
by(simp split: list.split);
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    57
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    58
text{*\noindent%
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    59
which \isacommand{apply}\isa{(simp)} alone will not do.
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    60
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    61
In general, every datatype $t$ comes with a theorem
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    62
\isa{$t$.split} which can be declared to be a \bfindex{split rule} either
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    63
locally as above, or by giving it the \isa{split} attribute globally:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    64
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    65
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    66
lemmas [split] = list.split;
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    67
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    68
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    69
The \isa{split} attribute can be removed with the \isa{del} modifier,
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    70
either locally
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    71
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    72
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    73
lemma "dummy=dummy";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    74
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    75
apply(simp split del: split_if);
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    76
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    77
oops;
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    78
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    79
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    80
or globally:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    81
*}
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    82
lemmas [split del] = list.split;
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    83
9721
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    84
text{*
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    85
The above split rules intentionally only affect the conclusion of a
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    86
subgoal.  If you want to split an \isa{if} or \isa{case}-expression in
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    87
the assumptions, you have to apply \isa{split\_if\_asm} or $t$\isa{.split_asm}:
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    88
*}
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    89
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    90
lemma "if xs = [] then ys ~= [] else ys = [] ==> xs @ ys ~= []"
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    91
apply(simp only: split: split_if_asm);
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    92
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    93
txt{*\noindent
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    94
In contrast to splitting the conclusion, this actually creates two
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    95
separate subgoals (which are solved by \isa{simp\_all}):
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    96
\begin{isabelle}
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    97
\ \isadigit{1}{\isachardot}\ {\isasymlbrakk}\mbox{xs}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\ \mbox{ys}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ \mbox{ys}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}\isanewline
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    98
\ \isadigit{2}{\isachardot}\ {\isasymlbrakk}\mbox{xs}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\ \mbox{ys}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ \mbox{xs}\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
    99
\end{isabelle}
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
   100
If you need to split both in the assumptions and the conclusion,
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
   101
use $t$\isa{.splits} which subsumes $t$\isa{.split} and $t$\isa{.split_asm}.
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
   102
*}
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
   103
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   104
(*<*)
9721
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9541
diff changeset
   105
by(simp_all)
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   106
end
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   107
(*>*)