doc-src/IsarImplementation/Thy/tactic.thy
author wenzelm
Thu, 13 Nov 2008 22:05:49 +0100
changeset 28783 dd886b5756a7
parent 28782 a2165c8ca58b
child 28785 64163cddf3e6
permissions -rw-r--r--
more on basic tactics;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     1
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     2
(* $Id$ *)
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     3
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     4
theory tactic imports base begin
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     5
20452
wenzelm
parents: 20451
diff changeset
     6
chapter {* Tactical reasoning *}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     7
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
     8
text {*
20474
wenzelm
parents: 20472
diff changeset
     9
  Tactical reasoning works by refining the initial claim in a
wenzelm
parents: 20472
diff changeset
    10
  backwards fashion, until a solved form is reached.  A @{text "goal"}
wenzelm
parents: 20472
diff changeset
    11
  consists of several subgoals that need to be solved in order to
wenzelm
parents: 20472
diff changeset
    12
  achieve the main statement; zero subgoals means that the proof may
wenzelm
parents: 20472
diff changeset
    13
  be finished.  A @{text "tactic"} is a refinement operation that maps
wenzelm
parents: 20472
diff changeset
    14
  a goal to a lazy sequence of potential successors.  A @{text
wenzelm
parents: 20472
diff changeset
    15
  "tactical"} is a combinator for composing tactics.
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    16
*}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    17
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    18
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    19
section {* Goals \label{sec:tactical-goals} *}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    20
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    21
text {*
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    22
  Isabelle/Pure represents a goal\glossary{Tactical goal}{A theorem of
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    23
  \seeglossary{Horn Clause} form stating that a number of subgoals
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    24
  imply the main conclusion, which is marked as a protected
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    25
  proposition.} as a theorem stating that the subgoals imply the main
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    26
  goal: @{text "A\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^sub>n \<Longrightarrow> C"}.  The outermost goal
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    27
  structure is that of a Horn Clause\glossary{Horn Clause}{An iterated
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    28
  implication @{text "A\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^sub>n \<Longrightarrow> C"}, without any
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    29
  outermost quantifiers.  Strictly speaking, propositions @{text
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    30
  "A\<^sub>i"} need to be atomic in Horn Clauses, but Isabelle admits
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    31
  arbitrary substructure here (nested @{text "\<Longrightarrow>"} and @{text "\<And>"}
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    32
  connectives).}: i.e.\ an iterated implication without any
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    33
  quantifiers\footnote{Recall that outermost @{text "\<And>x. \<phi>[x]"} is
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    34
  always represented via schematic variables in the body: @{text
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    35
  "\<phi>[?x]"}.  These variables may get instantiated during the course of
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    36
  reasoning.}.  For @{text "n = 0"} a goal is called ``solved''.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    37
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    38
  The structure of each subgoal @{text "A\<^sub>i"} is that of a general
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    39
  Hereditary Harrop Formula @{text "\<And>x\<^sub>1 \<dots> \<And>x\<^sub>k. H\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> H\<^sub>m \<Longrightarrow> B"} in
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    40
  normal form.  Here @{text "x\<^sub>1, \<dots>, x\<^sub>k"} are goal parameters, i.e.\
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    41
  arbitrary-but-fixed entities of certain types, and @{text "H\<^sub>1, \<dots>,
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    42
  H\<^sub>m"} are goal hypotheses, i.e.\ facts that may be assumed locally.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    43
  Together, this forms the goal context of the conclusion @{text B} to
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    44
  be established.  The goal hypotheses may be again arbitrary
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    45
  Hereditary Harrop Formulas, although the level of nesting rarely
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
    46
  exceeds 1--2 in practice.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    47
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    48
  The main conclusion @{text C} is internally marked as a protected
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    49
  proposition\glossary{Protected proposition}{An arbitrarily
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    50
  structured proposition @{text "C"} which is forced to appear as
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    51
  atomic by wrapping it into a propositional identity operator;
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    52
  notation @{text "#C"}.  Protecting a proposition prevents basic
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    53
  inferences from entering into that structure for the time being.},
20474
wenzelm
parents: 20472
diff changeset
    54
  which is represented explicitly by the notation @{text "#C"}.  This
wenzelm
parents: 20472
diff changeset
    55
  ensures that the decomposition into subgoals and main conclusion is
wenzelm
parents: 20472
diff changeset
    56
  well-defined for arbitrarily structured claims.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    57
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    58
  \medskip Basic goal management is performed via the following
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20316
diff changeset
    59
  Isabelle/Pure rules:
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    60
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    61
  \[
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    62
  \infer[@{text "(init)"}]{@{text "C \<Longrightarrow> #C"}}{} \qquad
20547
wenzelm
parents: 20474
diff changeset
    63
  \infer[@{text "(finish)"}]{@{text "C"}}{@{text "#C"}}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    64
  \]
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    65
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    66
  \medskip The following low-level variants admit general reasoning
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    67
  with protected propositions:
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    68
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    69
  \[
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    70
  \infer[@{text "(protect)"}]{@{text "#C"}}{@{text "C"}} \qquad
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    71
  \infer[@{text "(conclude)"}]{@{text "A\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^sub>n \<Longrightarrow> C"}}{@{text "A\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^sub>n \<Longrightarrow> #C"}}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    72
  \]
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    73
*}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    74
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    75
text %mlref {*
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    76
  \begin{mldecls}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    77
  @{index_ML Goal.init: "cterm -> thm"} \\
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    78
  @{index_ML Goal.finish: "thm -> thm"} \\
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    79
  @{index_ML Goal.protect: "thm -> thm"} \\
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    80
  @{index_ML Goal.conclude: "thm -> thm"} \\
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    81
  \end{mldecls}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    82
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    83
  \begin{description}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    84
20474
wenzelm
parents: 20472
diff changeset
    85
  \item @{ML "Goal.init"}~@{text C} initializes a tactical goal from
wenzelm
parents: 20472
diff changeset
    86
  the well-formed proposition @{text C}.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    87
20474
wenzelm
parents: 20472
diff changeset
    88
  \item @{ML "Goal.finish"}~@{text "thm"} checks whether theorem
wenzelm
parents: 20472
diff changeset
    89
  @{text "thm"} is a solved goal (no subgoals), and concludes the
wenzelm
parents: 20472
diff changeset
    90
  result by removing the goal protection.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    91
20474
wenzelm
parents: 20472
diff changeset
    92
  \item @{ML "Goal.protect"}~@{text "thm"} protects the full statement
wenzelm
parents: 20472
diff changeset
    93
  of theorem @{text "thm"}.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    94
20474
wenzelm
parents: 20472
diff changeset
    95
  \item @{ML "Goal.conclude"}~@{text "thm"} removes the goal
wenzelm
parents: 20472
diff changeset
    96
  protection, even if there are pending subgoals.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    97
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    98
  \end{description}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    99
*}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   100
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   101
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   102
section {* Tactics *}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   103
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   104
text {* A @{text "tactic"} is a function @{text "goal \<rightarrow> goal\<^sup>*\<^sup>*"} that
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   105
  maps a given goal state (represented as a theorem, cf.\
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   106
  \secref{sec:tactical-goals}) to a lazy sequence of potential
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   107
  successor states.  The underlying sequence implementation is lazy
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   108
  both in head and tail, and is purely functional in \emph{not}
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   109
  supporting memoing.\footnote{The lack of memoing and the strict
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   110
  nature of SML requires some care when working with low-level
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   111
  sequence operations, to avoid duplicate or premature evaluation of
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   112
  results.}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   113
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   114
  An \emph{empty result sequence} means that the tactic has failed: in
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   115
  a compound tactic expressions other tactics might be tried instead,
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   116
  or the whole refinement step might fail outright, producing a
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   117
  toplevel error message.  When implementing tactics from scratch, one
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   118
  should take care to observe the basic protocol of mapping regular
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   119
  error conditions to an empty result; only serious faults should
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   120
  emerge as exceptions.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   121
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   122
  By enumerating \emph{multiple results}, a tactic can easily express
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   123
  the potential outcome of an internal search process.  There are also
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   124
  combinators for building proof tools that involve search
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   125
  systematically, see also \secref{sec:tacticals}.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   126
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   127
  \medskip As explained in \secref{sec:tactical-goals}, a goal state
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   128
  essentially consists of a list of subgoals that imply the main goal
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   129
  (conclusion).  Tactics may operate on all subgoals or on a
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   130
  particularly specified subgoal, but must not change the main
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   131
  conclusion (apart from instantiating schematic goal variables).
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   132
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   133
  Tactics with explicit \emph{subgoal addressing} are of the form
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   134
  @{text "int \<rightarrow> tactic"} and may be applied to a particular subgoal
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   135
  (counting from 1).  If the subgoal number is out of range, the
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   136
  tactic should fail with an empty result sequence, but must not raise
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   137
  an exception!
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   138
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   139
  Operating on a particular subgoal means to replace it by an interval
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   140
  of zero or more subgoals in the same place; other subgoals must not
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   141
  be affected, apart from instantiating schematic variables ranging
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   142
  over the whole goal state.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   143
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   144
  A common pattern of composing tactics with subgoal addressing is to
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   145
  try the first one, and then the second one only if the subgoal has
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   146
  not been solved yet.  Special care is required here to avoid bumping
28782
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   147
  into unrelated subgoals that happen to come after the original
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   148
  subgoal.  Assuming that there is only a single initial subgoal is a
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   149
  very common error when implementing tactics!
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   150
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   151
  Tactics with internal subgoal addressing should expose the subgoal
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   152
  index as @{text "int"} argument in full generality; a hardwired
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   153
  subgoal 1 inappropriate.
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   154
  
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   155
  \medskip The main well-formedness conditions for proper tactics are
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   156
  summarized as follows.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   157
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   158
  \begin{itemize}
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   159
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   160
  \item General tactic failure is indicated by an empty result, only
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   161
  serious faults may produce an exception.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   162
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   163
  \item The main conclusion must not be changed, apart from
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   164
  instantiating schematic variables.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   165
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   166
  \item A tactic operates either uniformly on all subgoals, or
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   167
  specifically on a selected subgoal (without bumping into unrelated
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   168
  subgoals).
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   169
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   170
  \item Range errors in subgoal addressing produce an empty result.
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   171
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   172
  \end{itemize}
28782
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   173
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   174
  Some of these conditions are checked by higher-level goal
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   175
  infrastructure (\secref{sec:results}); others are not checked
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   176
  explicitly, and violating them merely results in ill-behaved tactics
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   177
  experienced by the user (e.g.\ tactics that insist in being
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   178
  applicable only to singleton goals, or disallow composition with
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   179
  basic tacticals).
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   180
*}
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   181
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   182
text %mlref {*
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   183
  \begin{mldecls}
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   184
  @{index_ML_type tactic: "thm -> thm Seq.seq"} \\
28783
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   185
  @{index_ML no_tac: tactic} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   186
  @{index_ML all_tac: tactic} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   187
  @{index_ML print_tac: "string -> tactic"} \\[1ex]
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   188
  @{index_ML PRIMITIVE: "(thm -> thm) -> tactic"} \\[1ex]
28782
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   189
  @{index_ML SUBGOAL: "(term * int -> tactic) -> int -> tactic"} \\
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   190
  @{index_ML CSUBGOAL: "(cterm * int -> tactic) -> int -> tactic"} \\
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   191
  \end{mldecls}
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   192
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   193
  \begin{description}
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   194
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   195
  \item @{ML_type tactic} represents tactics.  The well-formedness
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   196
  conditions described above need to be observed.  See also @{"file"
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   197
  "~~/src/Pure/General/seq.ML"} for the underlying implementation of
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   198
  lazy sequences.
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   199
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   200
  \item @{ML_type "int -> tactic"} represents tactics with explicit
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   201
  subgoal addressing, with well-formedness conditions as described
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   202
  above.
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   203
28783
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   204
  \item @{ML no_tac} is a tactic that always fails, returning the
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   205
  empty sequence.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   206
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   207
  \item @{ML all_tac} is a tactic that always succeeds, returning a
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   208
  singleton sequence with unchanged goal state.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   209
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   210
  \item @{ML print_tac}~@{text "message"} is like @{ML all_tac}, but
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   211
  prints a message together with the goal state on the tracing
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   212
  channel.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   213
28782
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   214
  \item @{ML PRIMITIVE}~@{text rule} turns a primitive inference rule
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   215
  into a tactic with unique result.  Exception @{ML THM} is considered
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   216
  a regular tactic failure and produces an empty result; other
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   217
  exceptions are passed through.
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   218
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   219
  \item @{ML SUBGOAL}~@{text "(fn (subgoal, i) => tactic)"} is the
28783
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   220
  most basic form to produce a tactic with subgoal addressing.  The
28782
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   221
  given abstraction over the subgoal term and subgoal number allows to
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   222
  peek at the relevant information of the full goal state.  The
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   223
  subgoal range is checked as required above.
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   224
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   225
  \item @{ML CSUBGOAL} is similar to @{ML SUBGOAL}, but passes the
28783
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   226
  subgoal as @{ML_type cterm} instead of raw @{ML_type term}.  This
28782
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   227
  avoids expensive re-certification in situations where the subgoal is
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   228
  used directly for primitive inferences.
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   229
a2165c8ca58b basic ML reference for tactics;
wenzelm
parents: 28781
diff changeset
   230
  \end{description}
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   231
*}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   232
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   233
28783
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   234
subsection {* Resolution and assumption tactics *}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   235
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   236
text {* \emph{Resolution} is the most basic mechanism for refining a
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   237
  subgoal using a theorem as object-level rule.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   238
  \emph{Elim-resolution} is particularly suited for elimination rules:
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   239
  it resolves with a rule, proves its first premise by assumption, and
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   240
  finally deletes that assumption from any new subgoals.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   241
  \emph{Destruct-resolution} is like elim-resolution, but the given
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   242
  destruction rules are first turned into canonical elimination
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   243
  format.  \emph{Forward-resolution} is like destruct-resolution, but
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   244
  without deleting the selected assumption.  The @{text r}, @{text e},
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   245
  @{text d}, @{text f} naming convention is maintained for several
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   246
  different kinds of resolution rules and tactics.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   247
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   248
  Assumption tactics close a subgoal by unifying some of its premises
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   249
  against its conclusion.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   250
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   251
  \medskip All the tactics in this section operate on a subgoal
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   252
  designated by a positive integer.  Other subgoals might be affected
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   253
  indirectly, due to instantiation of schematic variables.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   254
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   255
  There are various sources of non-determinism, the tactic result
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   256
  sequence enumerates all possibilities of the following choices (if
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   257
  applicable):
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   258
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   259
  \begin{enumerate}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   260
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   261
  \item selecting one of the rules given as argument to the tactic;
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   262
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   263
  \item selecting a subgoal premise to eliminate, unifying it against
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   264
  the first premise of the rule;
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   265
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   266
  \item unifying the conclusion of the subgoal to the conclusion of
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   267
  the rule.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   268
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   269
  \end{enumerate}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   270
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   271
  Recall that higher-order unification may produce multiple results
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   272
  that are enumerated here.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   273
*}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   274
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   275
text %mlref {*
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   276
  \begin{mldecls}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   277
  @{index_ML resolve_tac: "thm list -> int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   278
  @{index_ML eresolve_tac: "thm list -> int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   279
  @{index_ML dresolve_tac: "thm list -> int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   280
  @{index_ML forward_tac: "thm list -> int -> tactic"} \\[1ex]
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   281
  @{index_ML assume_tac: "int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   282
  @{index_ML eq_assume_tac: "int -> tactic"} \\[1ex]
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   283
  @{index_ML match_tac: "thm list -> int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   284
  @{index_ML ematch_tac: "thm list -> int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   285
  @{index_ML dmatch_tac: "thm list -> int -> tactic"} \\
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   286
  \end{mldecls}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   287
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   288
  \begin{description}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   289
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   290
  \item @{ML resolve_tac}~@{text "thms i"} refines the goal state
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   291
  using the given theorems, which should normally be introduction
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   292
  rules.  The tactic resolves a rule's conclusion with subgoal @{text
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   293
  i}, replacing it by the corresponding versions of the rule's
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   294
  premises.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   295
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   296
  \item @{ML eresolve_tac}~@{text "thms i"} performs elim-resolution
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   297
  with the given theorems, which should normally be elimination rules.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   298
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   299
  \item @{ML dresolve_tac}~@{text "thms i"} performs
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   300
  destruct-resolution with the given theorems, which should normally
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   301
  be destruction rules.  This replaces an assumption by the result of
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   302
  applying one of the rules.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   303
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   304
  \item @{ML forward_tac} is like @{ML dresolve_tac} except that the
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   305
  selected assumption is not deleted.  It applies a rule to an
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   306
  assumption, adding the result as a new assumption.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   307
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   308
  \item @{ML assume_tac}~@{text i} attempts to solve subgoal @{text i}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   309
  by assumption (modulo higher-order unification).
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   310
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   311
  \item @{ML eq_assume_tac} is similar to @{ML assume_tac}, but checks
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   312
  only for immediate @{text "\<alpha>"}-convertibility instead of using
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   313
  unification.  It succeeds (with a unique next state) if one of the
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   314
  assumptions is equal to the subgoal's conclusion.  Since it does not
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   315
  instantiate variables, it cannot make other subgoals unprovable.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   316
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   317
  \item @{ML match_tac}, @{ML ematch_tac}, and @{ML dmatch_tac} are
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   318
  similar to @{ML resolve_tac}, @{ML eresolve_tac}, and @{ML
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   319
  dresolve_tac}, respectively, but do not instantiate schematic
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   320
  variables in the goal state.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   321
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   322
  Flexible subgoals are not updated at will, but are left alone.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   323
  Strictly speaking, matching means to treat the unknowns in the goal
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   324
  state as constants; these tactics merely discard unifiers that would
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   325
  update the goal state.
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   326
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   327
  \end{description}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   328
*}
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   329
dd886b5756a7 more on basic tactics;
wenzelm
parents: 28782
diff changeset
   330
28781
1da96325eedf added section "Tactics";
wenzelm
parents: 20547
diff changeset
   331
section {* Tacticals \label{sec:tacticals} *}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   332
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   333
text {*
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   334
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   335
FIXME
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   336
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   337
\glossary{Tactical}{A functional combinator for building up complex
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   338
tactics from simpler ones.  Typical tactical perform sequential
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   339
composition, disjunction (choice), iteration, or goal addressing.
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   340
Various search strategies may be expressed via tacticals.}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   341
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   342
*}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   343
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   344
end
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   345