CookBook/FirstSteps.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 30 Sep 2008 03:30:40 -0400
changeset 11 733614e236a3
parent 10 df09e49b19bf
child 12 2f1736cb8f26
permissions -rw-r--r--
tuned and updated antquote_setup.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory FirstSteps
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
imports Main
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
uses "antiquote_setup.ML"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
     ("comp_simproc")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
begin
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
(*<*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
local structure O = ThyOutput
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
in
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  fun check_exists f = 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    if File.exists (Path.explode ("~~/src/" ^ f)) then ()
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    else error ("Source file " ^ quote f ^ " does not exist.")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  val _ = O.add_commands
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
   [("ML_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
         (check_exists name; Pretty.str name))))];
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
end
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
(*>*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
chapter {* First Steps *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    27
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
text {* 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    29
  Isabelle programming is done in Standard ML.
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    30
  Just like lemmas and proofs, code in Isabelle is part of a 
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    31
  theory. If you want to follow the code written in this chapter, we 
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    32
  assume you are working inside the theory defined by
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    34
  \begin{center}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    35
  \begin{tabular}{@ {}l}
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    36
  \isacommand{theory} CookBook\\
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    37
  \isacommand{imports} Main\\
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    38
  \isacommand{begin}\\
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    39
  \ldots
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    40
  \end{tabular}
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    41
  \end{center}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    42
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    43
  The easiest and quickest way to include code in a theory is
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    44
  by using the \isacommand{ML} command. For example
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  3 + 4
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
text {*
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    52
  The expression inside \isacommand{ML} commands is immediately evaluated,
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    53
  like ``normal'' Isabelle proof scripts, by using the advance and undo buttons of 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    54
  your Isabelle environment. The code inside the \isacommand{ML} command 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    55
  can also contain value- and function bindings. However on such ML-commands the 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    56
  undo operation behaves slightly counter-intuitive, because if you define
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    57
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    58
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    59
ML {*
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    60
  val foo = true
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    63
text {*
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    64
  then Isabelle's undo operation has no effect on the definition of 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    65
  @{ML "foo"}. 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    66
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
    67
  During developments you might find it necessary to quickly inspect some data
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    68
  in your code. This can be done in a ``quick-and-dirty'' fashion using 
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
    69
  the function @{ML "warning"}. For example
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    70
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    71
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    72
ML {*
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    73
  val _ = warning "any string"
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    74
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    75
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    76
text {*
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    77
  will print out the string inside the response buffer of Isabelle.
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    78
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    79
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    80
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    81
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
section {* Antiquotations *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
text {*
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    85
  The main advantage of embedding all code 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    86
  in a theory is that the code can contain references to entities defined 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    87
  on the logical level of Isabelle. This is done using antiquotations.
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    88
  For example, one can print out the name of 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    89
  the current theory by typing
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
ML {* Context.theory_name @{theory} *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    94
text {* 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    95
  where @{text "@{theory}"} is an antiquotation that is substituted with the
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    96
  current theory (remember that we assumed we are inside the theory CookBook). 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    97
  The name of this theory can be extrated using a the function @{ML "Context.theory_name"}. 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    98
  So the code above returns the string @{ML "\"CookBook\""}.
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    99
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   100
  Note, however, that antiquotations are statically scoped, that is the value is
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   101
  determined at ``compile-time'' not ``run-time''. For example the function
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   102
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
ML {* 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  fun current_thyname () = Context.theory_name @{theory}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
text {*
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   110
  does \emph{not} return the name of the current theory, if it is run in a 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   111
  different theory. Instead, the code above defines the constant function 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   112
  that always returns the string @{ML "\"CookBook\""}, no matter where the
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   113
  function is called. Operationally speaking,  @{text "@{theory}"} is 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   114
  \emph{not} replaced with code that will look up the current theory in 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   115
  some data structure and return it. Instead, it is literally
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   116
  replaced with the value representing the theory name.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   118
  In a similar way you can use antiquotations to refer to types and theorems:
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   119
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   120
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   121
ML {* @{typ "(int * nat) list"} *}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   122
ML {* @{thm allI} *}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   123
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   124
text {*
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   125
  In the course of this introduction, we will learn more about 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   126
  these antoquotations: they greatly simplify Isabelle programming since one
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  can directly access all kinds of logical elements from ML.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
section {* Terms *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
text {*
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   133
  One way to construct terms of Isabelle on the ML-level is by using the antiquotation 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   134
  @{text "@{term \<dots>}"}:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
ML {* @{term "(a::nat) + b = c"} *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
text {*
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   140
  This will show the term @{term "(a::nat) + b = c"}, but printed out using the internal
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   141
  representation of this term. This internal represenation corresponds to the 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   142
  datatype defined in @{ML_file "Pure/term.ML"}.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   144
  The internal representation of terms uses the usual de-Bruijn indices mechanism where bound 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   145
  variables are represented by the constructor @{ML Bound}. The index in @{ML Bound} refers to
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   146
  the number of Abstractions (@{ML Abs}) we have to skip until we hit the @{ML Abs} that
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   147
  binds the corresponding variable. However, the names of bound variables are 
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   148
  kept at abstractions for printing purposes, and so should be treated only as comments. 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   149
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  \begin{readmore}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  Terms are described in detail in \ichcite{ch:logic}. Their
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  definition and many useful operations can be found in @{ML_file "Pure/term.ML"}.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  \end{readmore}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  Sometimes the internal representation can be surprisingly different
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  from what you see at the user level, because the layer of
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   157
  parsing/type checking/pretty printing can be quite elaborate. 
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   159
  \begin{exercise}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
  Look at the internal term representation of the following terms, and
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  find out why they are represented like this.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  \begin{itemize}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  \item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  \item @{term "\<lambda>(x,y). P y x"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  \item @{term "{ [x::int] | x. x \<le> -2 }"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  \end{itemize}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  Hint: The third term is already quite big, and the pretty printer
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  may omit parts of it by default. If you want to see all of it, you
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  can use @{ML "print_depth 50"} to set the limit to a value high enough.
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   172
  \end{exercise}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  @{const_name plus}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  @{term "{ [x::int] | x. x \<le> -2 }"}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  The internal names of constants like @{term "zero"} or @{text "+"} are
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  often more complex than one first expects. Here, the extra prefixes
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  @{text zero_class} and @{text plus_class} are present because the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  constants are defined within a type class. Guessing such internal
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  names can be extremely hard, which is why the system provides
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  another antiquotation: @{ML "@{const_name plus}"} gives just this
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  name.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   192
  FIXME: maybe explain @{text "@{prop \<dots>}"} as a special kind of terms 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   193
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   194
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   195
ML {* @{prop "True"} *}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   196
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   197
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   198
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   199
section {* Possible Section on Construting Explicitly Terms *} 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   200
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   201
text {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   202
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   203
  There is a disadvantage of using the @{text "@{term \<dots>}"} antiquotation
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   204
  directly in order to construct terms. 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   205
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   206
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   207
ML {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   208
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   209
  val nat = HOLogic.natT
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   210
  val x = Free ("x", nat)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   211
  val t = Free ("t", nat)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   212
  val P = Free ("P", nat --> HOLogic.boolT)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   213
  val Q = Free ("Q", nat --> HOLogic.boolT)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   214
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   215
  val A1 = Logic.all x 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   216
           (Logic.mk_implies (HOLogic.mk_Trueprop (P $ x),
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   217
                              HOLogic.mk_Trueprop (Q $ x)))
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   218
           
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   219
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   220
  val A2 = HOLogic.mk_Trueprop (P $ t)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   221
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   222
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   223
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   224
text {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   225
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   226
  \begin{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   227
  Write a function @{ML_text "rev_sum : term -> term"} that takes a
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   228
  term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "i"} might be zero)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   229
  and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   230
  the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   231
  associates to the left. Try your function on some examples, and see if 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   232
  the result typechecks. (FIXME: clash with the type-checking section later)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   233
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   234
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   235
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   236
ML {* 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   237
  fun rev_sum t =
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   238
  let
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   239
   fun dest_sum (Const (@{const_name plus}, _) $ u $ u') = 
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   240
                                                      u' :: dest_sum u
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   241
     | dest_sum u = [u]
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   242
   in
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   243
     foldl1 (HOLogic.mk_binop @{const_name plus}) (dest_sum t)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   244
   end;
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   245
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   246
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   247
text {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   248
  \begin{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   249
  Write a function which takes two terms representing natural numbers
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   250
  in unary (like @{term "Suc (Suc (Suc 0))"}), and produce the unary
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   251
  number representing their sum.
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   252
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   253
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   254
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   255
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   256
ML {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   257
  fun make_sum t1 t2 =
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   258
      HOLogic.mk_nat (HOLogic.dest_nat t1 + HOLogic.dest_nat t2)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   259
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   260
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   261
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   262
text {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   263
  \begin{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   264
  Look at the functions defined in @{ML_file "Pure/logic.ML"} and
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   265
  @{ML_file "HOL/hologic.ML"} and see if they can make your life
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   266
  easier.
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   267
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   268
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   269
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   270
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   271
section {* Type checking *}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   272
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   273
text {* 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   274
  We can freely construct and manipulate terms, since they are just
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   275
  arbitrary unchecked trees. However, we eventually want to see if a
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   276
  term is wellformed, or type checks, relative to a theory.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   278
  Type checking is done via the function @{ML cterm_of}, which turns 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   279
  a @{ML_type term} into a  @{ML_type cterm}, a \emph{certified} term. 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   280
  Unlike @{ML_type term}s, which are just trees, @{ML_type
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   281
  "cterm"}s are abstract objects that are guaranteed to be
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   282
  type-correct, and can only be constructed via the official
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   283
  interfaces.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   285
  (FIXME: Alex what do you mean concretely by ``official interfaces'')
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   286
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   287
  Type checking is always relative to a theory context. For now we can use
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   288
  the @{ML "@{theory}"} antiquotation to get hold of the current theory.
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   289
  For example we can write:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   292
ML {* cterm_of @{theory} @{term "(a::nat) + b = c"} *}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   293
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
section {* Theorems *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  Just like @{ML_type cterm}s, theorems (of type @{ML_type thm}) are
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
  abstract objects that can only be built by going through the kernel
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  interfaces, which means that all your proofs will be checked. The
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
  basic rules of the Isabelle/Pure logical framework are defined in
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
  @{ML_file "Pure/thm.ML"}. 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
  Using these rules, which are just ML functions, you can do simple
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
  natural deduction proofs on the ML level. For example, the statement
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   305
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   306
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   307
  lemma 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   308
   assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   309
   and     assm\<^isub>2: "P t"
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   310
   shows "Q t"
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   311
   (*<*)oops(*>*) 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   312
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   313
text {*
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   314
  can be proved in ML like 
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  this\footnote{Note that @{text "|>"} is just reverse
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
  application. This combinator, and several variants are defined in
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
  @{ML_file "Pure/General/basics.ML"}}:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   318
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   323
ML {*
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   324
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   325
let
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   326
  val thy = @{theory}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   327
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   328
  val assm1 = cterm_of thy @{prop "\<And>(x::nat). P x \<Longrightarrow> Q x"}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   329
  val assm2 = cterm_of thy @{prop "((P::nat\<Rightarrow>bool) t)"}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   330
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   331
  val Pt_implies_Qt = 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   332
        assume assm1
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   333
        |> forall_elim (cterm_of thy @{term "t::nat"});
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   334
  
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   335
  val Qt = implies_elim Pt_implies_Qt (assume assm2);
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   336
in
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   337
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   338
  Qt 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   339
  |> implies_intr assm2
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   340
  |> implies_intr assm1
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   341
end
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   342
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   343
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   344
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   345
text {*
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   346
  FIXME Explain this program more carefully (@{ML_text assume},  @{ML_text "forall_elim"} \ldots)
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   347
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   348
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   349
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
section {* Tactical reasoning *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  The goal-oriented tactical style is similar to the @{text apply}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
  style at the user level. Reasoning is centered around a \emph{goal},
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
  which is modified in a sequence of proof steps until it is solved.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
  A goal (or goal state) is a special @{ML_type thm}, which by
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
  convention is an implication:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
  @{text[display] "A\<^isub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^isub>n \<Longrightarrow> #(C)"}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  Since the final result @{term C} could again be an implication, there is the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  @{text "#"} around the final result, which protects its premises from being
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
  misinterpreted as open subgoals. The protection @{text "# :: prop \<Rightarrow>
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
  prop"} is just the identity and used as a syntactic marker.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  Now tactics are just functions that map a goal state to a (lazy)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  sequence of successor states, hence the type of a tactic is
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
  @{ML_type[display] "thm -> thm Seq.seq"}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
  See @{ML_file "Pure/General/seq.ML"} for the implementation of lazy
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  sequences.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
  Of course, tactics are expected to behave nicely and leave the final
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
  conclusion @{term C} intact. In order to start a tactical proof for
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  @{term A}, we
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
  just set up the trivial goal @{text "A \<Longrightarrow> #(A)"} and run the tactic
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
  on it. When the subgoal is solved, we have just @{text "#(A)"} and
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
  can remove the protection.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
  The operations in @{ML_file "Pure/goal.ML"} do just that and we can use
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
  them.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
  Let us transcribe a simple apply style proof from the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
  tutorial\cite{isa-tutorial} into ML:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
lemma disj_swap: "P \<or> Q \<Longrightarrow> Q \<or> P"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
apply (erule disjE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
 apply (rule disjI2)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
apply (rule disjI1)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
done
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
let
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
  val ctxt = @{context}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
  val goal = @{prop "P \<or> Q \<Longrightarrow> Q \<or> P"}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
in
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
  Goal.prove ctxt ["P", "Q"] [] goal (fn _ => 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
    eresolve_tac [disjE] 1
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
    THEN resolve_tac [disjI2] 1
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
    THEN assume_tac 1
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
    THEN resolve_tac [disjI1] 1
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
    THEN assume_tac 1)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
end
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
  Tactics that affect only a certain subgoal, take a subgoal number as
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
  an integer parameter. Here we always work on the first subgoal,
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
  following exactly the @{text "apply"} script.
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   411
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   412
  (Fixme: would it make sense to explain THEN' here)
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   413
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
section {* Case Study: Relation Composition *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  \emph{Note: This is completely unfinished. I hoped to have a section
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
  with a nontrivial example, but I ran into several problems.}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
  Recall that HOL has special syntax for set comprehensions:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
  @{term "{ f x y |x y. P x y}"} abbreviates 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
  @{term[source] "{u. \<exists>x y. u = f x y \<and> P x y}"}. 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
  We will automatically prove statements of the following form:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
  @{lemma[display] "{(l\<^isub>1 x, r\<^isub>1 x) |x. P\<^isub>1 x} O {(l\<^isub>2 x, r\<^isub>2 x) |x. P\<^isub>2 x}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
  = {(l\<^isub>2 x, r\<^isub>1 y) |x y. r\<^isub>2 x = l\<^isub>1 y \<and>
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
  P\<^isub>2 x \<and> P\<^isub>1 y}" by auto}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  In Isabelle, relation composition is defined to be consistent with
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
  function composition, that is, the relation applied ``first'' is
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
  written on the right hand side. This different from what many
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  textbooks do.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
  The above statement about composition is not proved automatically by
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
  @{method simp}, and it cannot be solved by a fixed set of rewrite
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
  rules, since the number of (implicit) quantifiers may vary. Here, we
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
  only have one bound variable in each comprehension, but in general
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
  there can be more. On the other hand, @{method auto} proves the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
  above statement quickly, by breaking the equality into two parts and
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
  proving them separately. However, if e.g.\ @{term "P\<^isub>1"} is a
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
  complicated expression, the automated tools may get confused.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
  Our goal is now to develop a small procedure that can compute (with proof) the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
  composition of two relation comprehensions, which can be used to
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
  extend the simplifier.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
section {*A tactic *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
text {* Let's start with a step-by-step proof of the above statement *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
lemma "{(l\<^isub>1 x, r\<^isub>1 x) |x. P\<^isub>1 x} O {(l\<^isub>2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
  x, r\<^isub>2 x) |x. P\<^isub>2 x}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
  = {(l\<^isub>2 x, r\<^isub>1 y) |x y. r\<^isub>2 x = l\<^isub>1 y \<and> P\<^isub>2 x \<and> P\<^isub>1 y}"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
apply (rule set_ext)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
apply (rule iffI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
 apply (erule rel_compE)  -- {* @{text "\<subseteq>"} *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
 apply (erule CollectE)     -- {* eliminate @{text "Collect"}, @{text "\<exists>"}, @{text "\<and>"}, and pairs *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
 apply (erule CollectE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
 apply (erule exE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
 apply (erule exE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
 apply (erule conjE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
 apply (erule conjE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
 apply (erule Pair_inject)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
 apply (erule Pair_inject)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
 apply (simp only:)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
 apply (rule CollectI)    -- {* introduce them again *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
 apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
 apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
  apply (rule refl)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
  apply (rule sym)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
  apply (assumption)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
  apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
apply (erule CollectE)   -- {* @{text "\<subseteq>"} *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
apply (erule exE)+
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
apply (erule conjE)+
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
apply (simp only:)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
apply (rule rel_compI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
 apply (rule CollectI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
 apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
  apply (rule refl)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
apply (rule CollectI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
apply (subst Pair_eq)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
apply (rule refl)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
done
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
  The reader will probably need to step through the proof and verify
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
  that there is nothing spectacular going on here.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
  The @{text apply} script just applies the usual elimination and introduction rules in the right order.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
  This script is of course totally unreadable. But we are not trying
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
  to produce pretty Isar proofs here. We just want to find out which
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
  rules are needed and how they must be applied to complete the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
  proof. And a detailed apply-style proof can often be turned into a
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
  tactic quite easily. Of course we must resist the temptation to use
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
  @{method auto}, @{method blast} and friends, since their behaviour
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
  is not predictable enough. But the simple @{method rule} and
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
  @{method erule} methods are fine.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
  Notice that this proof depends only in one detail on the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
  concrete equation that we want to prove: The number of bound
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
  variables in the comprehension corresponds to the number of
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
  existential quantifiers that we have to eliminate and introduce
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
  again. In fact this is the only reason why the equations that we
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
  want to prove are not just instances of a single rule.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
  Here is the ML equivalent of the tactic script above:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
val compr_compose_tac =
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
  rtac @{thm set_ext}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
  THEN' rtac @{thm iffI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
  THEN' etac @{thm rel_compE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
  THEN' etac @{thm CollectE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
  THEN' etac @{thm CollectE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
  THEN' (fn i => REPEAT (etac @{thm exE} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
  THEN' etac @{thm Pair_inject}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
  THEN' etac @{thm Pair_inject}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
  THEN' asm_full_simp_tac HOL_basic_ss
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
  THEN' rtac @{thm CollectI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
  THEN' (fn i => REPEAT (rtac @{thm exI} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  THEN' rtac @{thm refl}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
  THEN' rtac @{thm sym}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  THEN' etac @{thm CollectE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
  THEN' (fn i => REPEAT (etac @{thm exE} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
  THEN' asm_full_simp_tac HOL_basic_ss
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
  THEN' rtac @{thm rel_compI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
  THEN' rtac @{thm CollectI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
  THEN' (fn i => REPEAT (rtac @{thm exI} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
  THEN' rtac @{thm refl}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  THEN' rtac @{thm CollectI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
  THEN' (fn i => REPEAT (rtac @{thm exI} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
  THEN' simp_tac (HOL_basic_ss addsimps [@{thm Pair_eq}])
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
  THEN' rtac @{thm refl}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
lemma test1: "{(l\<^isub>1 x, r\<^isub>1 x) |x. P\<^isub>1 x} O {(l\<^isub>2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
  x, r\<^isub>2 x) |x. P\<^isub>2 x}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
  = {(l\<^isub>2 x, r\<^isub>1 y) |x y. r\<^isub>2 x = l\<^isub>1 y \<and> P\<^isub>2 x \<and> P\<^isub>1 y}"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
by (tactic "compr_compose_tac 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
lemma test3: "{(l\<^isub>1 x, r\<^isub>1 x) |x. P\<^isub>1 x} O {(l\<^isub>2 x z, r\<^isub>2 x z) |x z. P\<^isub>2 x z}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
  = {(l\<^isub>2 x z, r\<^isub>1 y) |x y z. r\<^isub>2 x z = l\<^isub>1 y \<and> P\<^isub>2 x z \<and> P\<^isub>1 y}"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
by (tactic "compr_compose_tac 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
  So we have a tactic that works on at least two examples.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
  Getting it really right requires some more effort. Consider the goal
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
lemma "{(n, Suc n) |n. n > 0} O {(n, Suc n) |n. P n}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
  = {(n, Suc m)|n m. Suc n = m \<and> P n \<and> m > 0}"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
(*lemma "{(l\<^isub>1 x, r\<^isub>1 x) |x. P\<^isub>1 x} O {(l\<^isub>2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
  x, r\<^isub>2 x) |x. P\<^isub>2 x}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
  = {(l\<^isub>2 x, r\<^isub>1 y) |x y. r\<^isub>2 x = l\<^isub>1 y \<and> P\<^isub>2 x \<and> P\<^isub>1 y}"*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
txt {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
  This is exactly an instance of @{fact test1}, but our tactic fails
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
  on it with the usual uninformative
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
  \emph{empty result requence}.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
  We are now in the frequent situation that we need to debug. One simple instrument for this is @{ML "print_tac"},
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
  which is the same as @{ML all_tac} (the identity for @{ML_text "THEN"}),
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
  i.e.\ it does nothing, but it prints the current goal state as a
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
  side effect.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
  Another debugging option is of course to step through the interactive apply script.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
  Finding the problem could be taken as an exercise for the patient
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
  reader, and we will go ahead with the solution.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
  The problem is that in this instance the simplifier does more than it did in the general version
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
  of lemma @{fact test1}. Since @{text "l\<^isub>1"} and @{text "l\<^isub>2"} are just the identity function,
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
  the equation corresponding to @{text "l\<^isub>1 y = r\<^isub>2 x "}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
  becomes @{text "m = Suc n"}. Then the simplifier eagerly replaces
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
  all occurences of @{term "m"} by @{term "Suc n"} which destroys the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
  structure of the proof.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
  This is perhaps the most important lesson to learn, when writing tactics:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
  \textbf{Avoid automation at all cost!!!}.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
  Let us look at the proof state at the point where the simplifier is
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
  invoked:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
(*<*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
apply (rule set_ext)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
apply (rule iffI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
 apply (erule rel_compE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
 apply (erule CollectE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
 apply (erule CollectE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
 apply (erule exE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
 apply (erule exE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
 apply (erule conjE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
 apply (erule conjE)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
 apply (erule Pair_inject)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
 apply (erule Pair_inject)(*>*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
txt {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
  @{subgoals[display]}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
  Like in the apply proof, we now want to eliminate the equations that
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
  ``define'' @{term x}, @{term xa} and @{term z}. The other equations
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
  are just there by coincidence, and we must not touch them.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
  For such purposes, there is the internal tactic @{text "hyp_subst_single"}.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
  Its job is to take exactly one premise of the form @{term "v = t"},
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
  where @{term v} is a variable, and replace @{term "v"} in the whole
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
  subgoal. The hypothesis to eliminate is given by its position.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
  We can use this tactic to eliminate @{term x}:
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
apply (tactic "single_hyp_subst_tac 0 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
txt {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
  @{subgoals[display]}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
apply (tactic "single_hyp_subst_tac 2 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
apply (tactic "single_hyp_subst_tac 2 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
apply (tactic "single_hyp_subst_tac 3 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
 apply (rule CollectI)    -- {* introduce them again *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
 apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
 apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
  apply (rule refl)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
  apply (assumption)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
  apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
apply (erule CollectE)   -- {* @{text "\<subseteq>"} *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
apply (erule exE)+
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
apply (erule conjE)+
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
apply (tactic "single_hyp_subst_tac 0 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   672
apply (rule rel_compI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
 apply (rule CollectI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
 apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
 apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
  apply (rule refl)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
apply (rule CollectI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
apply (rule exI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
apply (subst Pair_eq)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
apply (rule conjI)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
 apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
apply (rule refl)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
apply assumption
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
done
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
ML {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
val compr_compose_tac =
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
  rtac @{thm set_ext}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
  THEN' rtac @{thm iffI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
  THEN' etac @{thm rel_compE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
  THEN' etac @{thm CollectE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   695
  THEN' etac @{thm CollectE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
  THEN' (fn i => REPEAT (etac @{thm exE} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
  THEN' etac @{thm Pair_inject}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
  THEN' etac @{thm Pair_inject}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
  THEN' single_hyp_subst_tac 0
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
  THEN' single_hyp_subst_tac 2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
  THEN' single_hyp_subst_tac 2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
  THEN' single_hyp_subst_tac 3
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
  THEN' rtac @{thm CollectI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
  THEN' (fn i => REPEAT (rtac @{thm exI} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
  THEN' rtac @{thm refl}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
  THEN' etac @{thm CollectE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
  THEN' (fn i => REPEAT (etac @{thm exE} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
  THEN' etac @{thm conjE}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
  THEN' single_hyp_subst_tac 0
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
  THEN' rtac @{thm rel_compI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
  THEN' rtac @{thm CollectI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
  THEN' (fn i => REPEAT (rtac @{thm exI} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
  THEN' rtac @{thm refl}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
  THEN' rtac @{thm CollectI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
  THEN' (fn i => REPEAT (rtac @{thm exI} i))
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
  THEN' stac @{thm Pair_eq}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
  THEN' rtac @{thm conjI}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
  THEN' rtac @{thm refl}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
  THEN' assume_tac
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
lemma "{(n, Suc n) |n. n > 0 \<and> A} O {(n, Suc n) |n m. P m n}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
  = {(n, Suc m)|n m' m. Suc n = m \<and> P m' n \<and> (m > 0 \<and> A)}"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
apply (tactic "compr_compose_tac 1")
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
done
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
text {*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
  The next step is now to turn this tactic into a simplification
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
  procedure. This just means that we need some code that builds the
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
  term of the composed relation.
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
use "comp_simproc"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
(*<*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
(*simproc_setup mysp ("x O y") = {* compose_simproc *}*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
lemma "{(n, Suc n) |n. n > 0 \<and> A} O {(n, Suc n) |n m. P m n} = x"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
(*apply (simp del:ex_simps)*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   756
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
lemma "({(g m, k) | m k. Q m k} 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
O {(h j, f j) | j. R j}) = x"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
(*apply (simp del:ex_simps) *)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
lemma "{uu. \<exists>j m k. uu = (h j, k) \<and> f j = g m \<and> R j \<and> Q m k}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
O {(h j, f j) | j. R j} = x"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   765
(*apply (simp del:ex_simps)*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
lemma "
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
  { (l x, r x) | x. P x \<and> Q x \<and> Q' x }
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   770
O { (l1 x, r1 x) | x. P1 x \<and> Q1 x \<and> Q1' x }
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
= A"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
(*apply (simp del:ex_simps)*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   773
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   774
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
lemma "
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
  { (l x, r x) | x. P x }
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
O { (l1 x, r1 x) | x. P1 x }
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   778
O { (l2 x, r2 x) | x. P2 x }
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
= A"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   780
(*
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
apply (simp del:ex_simps)*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
lemma "{(f n, m) |n m. P n m} O ({(g m, k) | m k. Q m k} 
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
O {(h j, f j) | j. R j}) = x"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
(*apply (simp del:ex_simps)*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
lemma "{u. \<exists>n. u=(f n, g n)} O {u. \<exists>n. u=(h n, j n)} = A"
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
oops
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   793
(*>*)
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
end