src/Doc/Implementation/Proof.thy
author wenzelm
Thu, 22 Oct 2015 23:01:49 +0200
changeset 61506 436b7fe89cdc
parent 61493 0debd22f0c0e
child 61580 c49a8ebd30cc
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29755
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 28541
diff changeset
     1
theory Proof
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 28541
diff changeset
     2
imports Base
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 28541
diff changeset
     3
begin
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     4
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     5
chapter \<open>Structured proofs\<close>
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     6
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     7
section \<open>Variables \label{sec:variables}\<close>
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
     8
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     9
text \<open>
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    10
  Any variable that is not explicitly bound by \<open>\<lambda>\<close>-abstraction
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    11
  is considered as ``free''.  Logically, free variables act like
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    12
  outermost universal quantification at the sequent level: \<open>A\<^sub>1(x), \<dots>, A\<^sub>n(x) \<turnstile> B(x)\<close> means that the result
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    13
  holds \<^emph>\<open>for all\<close> values of \<open>x\<close>.  Free variables for
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    14
  terms (not types) can be fully internalized into the logic: \<open>\<turnstile> B(x)\<close> and \<open>\<turnstile> \<And>x. B(x)\<close> are interchangeable, provided
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    15
  that \<open>x\<close> does not occur elsewhere in the context.
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    16
  Inspecting \<open>\<turnstile> \<And>x. B(x)\<close> more closely, we see that inside the
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    17
  quantifier, \<open>x\<close> is essentially ``arbitrary, but fixed'',
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    18
  while from outside it appears as a place-holder for instantiation
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    19
  (thanks to \<open>\<And>\<close> elimination).
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    20
20474
wenzelm
parents: 20472
diff changeset
    21
  The Pure logic represents the idea of variables being either inside
wenzelm
parents: 20472
diff changeset
    22
  or outside the current scope by providing separate syntactic
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    23
  categories for \<^emph>\<open>fixed variables\<close> (e.g.\ \<open>x\<close>) vs.\
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    24
  \<^emph>\<open>schematic variables\<close> (e.g.\ \<open>?x\<close>).  Incidently, a
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    25
  universal result \<open>\<turnstile> \<And>x. B(x)\<close> has the HHF normal form \<open>\<turnstile> B(?x)\<close>, which represents its generality without requiring an
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
    26
  explicit quantifier.  The same principle works for type variables:
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    27
  \<open>\<turnstile> B(?\<alpha>)\<close> represents the idea of ``\<open>\<turnstile> \<forall>\<alpha>. B(\<alpha>)\<close>''
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
    28
  without demanding a truly polymorphic framework.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    29
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
    30
  \<^medskip>
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
    31
  Additional care is required to treat type variables in a
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    32
  way that facilitates type-inference.  In principle, term variables
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    33
  depend on type variables, which means that type variables would have
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    34
  to be declared first.  For example, a raw type-theoretic framework
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    35
  would demand the context to be constructed in stages as follows:
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    36
  \<open>\<Gamma> = \<alpha>: type, x: \<alpha>, a: A(x\<^sub>\<alpha>)\<close>.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    37
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    38
  We allow a slightly less formalistic mode of operation: term
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    39
  variables \<open>x\<close> are fixed without specifying a type yet
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    40
  (essentially \<^emph>\<open>all\<close> potential occurrences of some instance
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    41
  \<open>x\<^sub>\<tau>\<close> are fixed); the first occurrence of \<open>x\<close>
20474
wenzelm
parents: 20472
diff changeset
    42
  within a specific term assigns its most general type, which is then
wenzelm
parents: 20472
diff changeset
    43
  maintained consistently in the context.  The above example becomes
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    44
  \<open>\<Gamma> = x: term, \<alpha>: type, A(x\<^sub>\<alpha>)\<close>, where type \<open>\<alpha>\<close> is fixed \<^emph>\<open>after\<close> term \<open>x\<close>, and the constraint
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    45
  \<open>x :: \<alpha>\<close> is an implicit consequence of the occurrence of
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    46
  \<open>x\<^sub>\<alpha>\<close> in the subsequent proposition.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    47
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    48
  This twist of dependencies is also accommodated by the reverse
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    49
  operation of exporting results from a context: a type variable
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    50
  \<open>\<alpha>\<close> is considered fixed as long as it occurs in some fixed
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    51
  term variable of the context.  For example, exporting \<open>x:
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    52
  term, \<alpha>: type \<turnstile> x\<^sub>\<alpha> \<equiv> x\<^sub>\<alpha>\<close> produces in the first step \<open>x: term
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    53
  \<turnstile> x\<^sub>\<alpha> \<equiv> x\<^sub>\<alpha>\<close> for fixed \<open>\<alpha>\<close>, and only in the second step
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    54
  \<open>\<turnstile> ?x\<^sub>?\<^sub>\<alpha> \<equiv> ?x\<^sub>?\<^sub>\<alpha>\<close> for schematic \<open>?x\<close> and \<open>?\<alpha>\<close>.
39841
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    55
  The following Isar source text illustrates this scenario.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    56
\<close>
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    57
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
    58
notepad
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
    59
begin
39841
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    60
  {
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    61
    fix x  -- \<open>all potential occurrences of some \<open>x::\<tau>\<close> are fixed\<close>
39841
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    62
    {
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    63
      have "x::'a \<equiv> x"  -- \<open>implicit type assignment by concrete occurrence\<close>
39841
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    64
        by (rule reflexive)
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    65
    }
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    66
    thm this  -- \<open>result still with fixed type \<open>'a\<close>\<close>
39841
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    67
  }
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    68
  thm this  -- \<open>fully general result for arbitrary \<open>?x::?'a\<close>\<close>
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
    69
end
39841
c7f3efe59e4e more examples;
wenzelm
parents: 39821
diff changeset
    70
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    71
text \<open>The Isabelle/Isar proof context manages the details of term
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39853
diff changeset
    72
  vs.\ type variables, with high-level principles for moving the
20474
wenzelm
parents: 20472
diff changeset
    73
  frontier between fixed and schematic variables.
wenzelm
parents: 20472
diff changeset
    74
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    75
  The \<open>add_fixes\<close> operation explicitly declares fixed
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    76
  variables; the \<open>declare_term\<close> operation absorbs a term into
20474
wenzelm
parents: 20472
diff changeset
    77
  a context by fixing new type variables and adding syntactic
wenzelm
parents: 20472
diff changeset
    78
  constraints.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    79
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    80
  The \<open>export\<close> operation is able to perform the main work of
20474
wenzelm
parents: 20472
diff changeset
    81
  generalizing term and type variables as sketched above, assuming
wenzelm
parents: 20472
diff changeset
    82
  that fixing variables and terms have been declared properly.
wenzelm
parents: 20472
diff changeset
    83
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    84
  There \<open>import\<close> operation makes a generalized fact a genuine
20474
wenzelm
parents: 20472
diff changeset
    85
  part of the context, by inventing fixed variables for the schematic
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    86
  ones.  The effect can be reversed by using \<open>export\<close> later,
20474
wenzelm
parents: 20472
diff changeset
    87
  potentially with an extended context; the result is equivalent to
wenzelm
parents: 20472
diff changeset
    88
  the original modulo renaming of schematic variables.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
    89
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    90
  The \<open>focus\<close> operation provides a variant of \<open>import\<close>
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    91
  for nested propositions (with explicit quantification): \<open>\<And>x\<^sub>1 \<dots> x\<^sub>n. B(x\<^sub>1, \<dots>, x\<^sub>n)\<close> is
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    92
  decomposed by inventing fixed variables \<open>x\<^sub>1, \<dots>,
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    93
  x\<^sub>n\<close> for the body.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    94
\<close>
20064
wenzelm
parents: 20041
diff changeset
    95
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    96
text %mlref \<open>
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
    97
  \begin{mldecls}
20474
wenzelm
parents: 20472
diff changeset
    98
  @{index_ML Variable.add_fixes: "
wenzelm
parents: 20472
diff changeset
    99
  string list -> Proof.context -> string list * Proof.context"} \\
20797
c1f0bc7e7d80 renamed Variable.invent_fixes to Variable.variant_fixes;
wenzelm
parents: 20547
diff changeset
   100
  @{index_ML Variable.variant_fixes: "
20474
wenzelm
parents: 20472
diff changeset
   101
  string list -> Proof.context -> string list * Proof.context"} \\
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
   102
  @{index_ML Variable.declare_term: "term -> Proof.context -> Proof.context"} \\
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   103
  @{index_ML Variable.declare_constraints: "term -> Proof.context -> Proof.context"} \\
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   104
  @{index_ML Variable.export: "Proof.context -> Proof.context -> thm list -> thm list"} \\
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   105
  @{index_ML Variable.polymorphic: "Proof.context -> term list -> term list"} \\
31794
71af1fd6a5e4 renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
wenzelm
parents: 30272
diff changeset
   106
  @{index_ML Variable.import: "bool -> thm list -> Proof.context ->
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 59902
diff changeset
   107
  ((((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list) * thm list) * Proof.context"} \\
60695
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   108
  @{index_ML Variable.focus: "binding list option -> term -> Proof.context ->
42509
9d107a52b634 updated Variable.focus;
wenzelm
parents: 42361
diff changeset
   109
  ((string * (string * typ)) list * term) * Proof.context"} \\
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
   110
  \end{mldecls}
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
   111
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   112
  \<^descr> @{ML Variable.add_fixes}~\<open>xs ctxt\<close> fixes term
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   113
  variables \<open>xs\<close>, returning the resulting internal names.  By
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   114
  default, the internal representation coincides with the external
20474
wenzelm
parents: 20472
diff changeset
   115
  one, which also means that the given variables must not be fixed
wenzelm
parents: 20472
diff changeset
   116
  already.  There is a different policy within a local proof body: the
wenzelm
parents: 20472
diff changeset
   117
  given names are just hints for newly invented Skolem variables.
20064
wenzelm
parents: 20041
diff changeset
   118
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 61416
diff changeset
   119
  \<^descr> @{ML Variable.variant_fixes} is similar to @{ML
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   120
  Variable.add_fixes}, but always produces fresh variants of the given
20474
wenzelm
parents: 20472
diff changeset
   121
  names.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   122
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   123
  \<^descr> @{ML Variable.declare_term}~\<open>t ctxt\<close> declares term
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   124
  \<open>t\<close> to belong to the context.  This automatically fixes new
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   125
  type variables, but not term variables.  Syntactic constraints for
20474
wenzelm
parents: 20472
diff changeset
   126
  type and term variables are declared uniformly, though.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   127
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   128
  \<^descr> @{ML Variable.declare_constraints}~\<open>t ctxt\<close> declares
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   129
  syntactic constraints from term \<open>t\<close>, without making it part
20474
wenzelm
parents: 20472
diff changeset
   130
  of the context yet.
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
   131
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   132
  \<^descr> @{ML Variable.export}~\<open>inner outer thms\<close> generalizes
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   133
  fixed type and term variables in \<open>thms\<close> according to the
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   134
  difference of the \<open>inner\<close> and \<open>outer\<close> context,
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   135
  following the principles sketched above.
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   136
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   137
  \<^descr> @{ML Variable.polymorphic}~\<open>ctxt ts\<close> generalizes type
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   138
  variables in \<open>ts\<close> as far as possible, even those occurring
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   139
  in fixed term variables.  The default policy of type-inference is to
20474
wenzelm
parents: 20472
diff changeset
   140
  fix newly introduced type variables, which is essentially reversed
wenzelm
parents: 20472
diff changeset
   141
  with @{ML Variable.polymorphic}: here the given terms are detached
wenzelm
parents: 20472
diff changeset
   142
  from the context as far as possible.
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20460
diff changeset
   143
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   144
  \<^descr> @{ML Variable.import}~\<open>open thms ctxt\<close> invents fixed
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   145
  type and term variables for the schematic ones occurring in \<open>thms\<close>.  The \<open>open\<close> flag indicates whether the fixed names
20474
wenzelm
parents: 20472
diff changeset
   146
  should be accessible to the user, otherwise newly introduced names
wenzelm
parents: 20472
diff changeset
   147
  are marked as ``internal'' (\secref{sec:names}).
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
   148
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   149
  \<^descr> @{ML Variable.focus}~\<open>bindings B\<close> decomposes the outermost \<open>\<And>\<close> prefix of proposition \<open>B\<close>, using the given name bindings.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   150
\<close>
20026
3469df62fe21 Local variables;
wenzelm
parents: 18537
diff changeset
   151
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   152
text %mlex \<open>The following example shows how to work with fixed term
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   153
  and type parameters and with type-inference.\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   154
59902
6afbe5a99139 misc tuning -- keep name space more clean;
wenzelm
parents: 59567
diff changeset
   155
ML_val \<open>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   156
  (*static compile-time context -- for testing only*)
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   157
  val ctxt0 = @{context};
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   158
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   159
  (*locally fixed parameters -- no type assignment yet*)
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   160
  val ([x, y], ctxt1) = ctxt0 |> Variable.add_fixes ["x", "y"];
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   161
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   162
  (*t1: most general fixed type; t1': most general arbitrary type*)
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   163
  val t1 = Syntax.read_term ctxt1 "x";
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   164
  val t1' = singleton (Variable.polymorphic ctxt1) t1;
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   165
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   166
  (*term u enforces specific type assignment*)
39846
cb6634eb8926 examples in Isabelle/HOL;
wenzelm
parents: 39841
diff changeset
   167
  val u = Syntax.read_term ctxt1 "(x::nat) \<equiv> y";
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   168
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   169
  (*official declaration of u -- propagates constraints etc.*)
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   170
  val ctxt2 = ctxt1 |> Variable.declare_term u;
39846
cb6634eb8926 examples in Isabelle/HOL;
wenzelm
parents: 39841
diff changeset
   171
  val t2 = Syntax.read_term ctxt2 "x";  (*x::nat is enforced*)
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   172
\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   173
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   174
text \<open>In the above example, the starting context is derived from the
40126
916cb4a28ffd misc tuning;
wenzelm
parents: 39864
diff changeset
   175
  toplevel theory, which means that fixed variables are internalized
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   176
  literally: \<open>x\<close> is mapped again to \<open>x\<close>, and
40126
916cb4a28ffd misc tuning;
wenzelm
parents: 39864
diff changeset
   177
  attempting to fix it again in the subsequent context is an error.
916cb4a28ffd misc tuning;
wenzelm
parents: 39864
diff changeset
   178
  Alternatively, fixed parameters can be renamed explicitly as
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   179
  follows:\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   180
59902
6afbe5a99139 misc tuning -- keep name space more clean;
wenzelm
parents: 59567
diff changeset
   181
ML_val \<open>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   182
  val ctxt0 = @{context};
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   183
  val ([x1, x2, x3], ctxt1) =
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   184
    ctxt0 |> Variable.variant_fixes ["x", "x", "x"];
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   185
\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   186
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   187
text \<open>The following ML code can now work with the invented names of
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   188
  \<open>x1\<close>, \<open>x2\<close>, \<open>x3\<close>, without depending on
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39853
diff changeset
   189
  the details on the system policy for introducing these variants.
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39853
diff changeset
   190
  Recall that within a proof body the system always invents fresh
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   191
  ``Skolem constants'', e.g.\ as follows:\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   192
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   193
notepad
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   194
begin
58728
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   195
  ML_prf %"ML"
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   196
   \<open>val ctxt0 = @{context};
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   197
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   198
    val ([x1], ctxt1) = ctxt0 |> Variable.add_fixes ["x"];
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   199
    val ([x2], ctxt2) = ctxt1 |> Variable.add_fixes ["x"];
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   200
    val ([x3], ctxt3) = ctxt2 |> Variable.add_fixes ["x"];
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   201
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   202
    val ([y1, y2], ctxt4) =
58728
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   203
      ctxt3 |> Variable.variant_fixes ["y", "y"];\<close>
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   204
end
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   205
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   206
text \<open>In this situation @{ML Variable.add_fixes} and @{ML
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   207
  Variable.variant_fixes} are very similar, but identical name
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   208
  proposals given in a row are only accepted by the second version.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   209
\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   210
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   211
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   212
section \<open>Assumptions \label{sec:assumptions}\<close>
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20218
diff changeset
   213
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   214
text \<open>
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   215
  An \<^emph>\<open>assumption\<close> is a proposition that it is postulated in the
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   216
  current context.  Local conclusions may use assumptions as
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   217
  additional facts, but this imposes implicit hypotheses that weaken
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   218
  the overall statement.
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   219
20474
wenzelm
parents: 20472
diff changeset
   220
  Assumptions are restricted to fixed non-schematic statements, i.e.\
wenzelm
parents: 20472
diff changeset
   221
  all generality needs to be expressed by explicit quantifiers.
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   222
  Nevertheless, the result will be in HHF normal form with outermost
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   223
  quantifiers stripped.  For example, by assuming \<open>\<And>x :: \<alpha>. P
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   224
  x\<close> we get \<open>\<And>x :: \<alpha>. P x \<turnstile> P ?x\<close> for schematic \<open>?x\<close>
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   225
  of fixed type \<open>\<alpha>\<close>.  Local derivations accumulate more and
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   226
  more explicit references to hypotheses: \<open>A\<^sub>1, \<dots>,
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   227
  A\<^sub>n \<turnstile> B\<close> where \<open>A\<^sub>1, \<dots>, A\<^sub>n\<close> needs to
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   228
  be covered by the assumptions of the current context.
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   229
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   230
  \<^medskip>
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   231
  The \<open>add_assms\<close> operation augments the context by
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   232
  local assumptions, which are parameterized by an arbitrary \<open>export\<close> rule (see below).
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   233
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   234
  The \<open>export\<close> operation moves facts from a (larger) inner
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   235
  context into a (smaller) outer context, by discharging the
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   236
  difference of the assumptions as specified by the associated export
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   237
  rules.  Note that the discharged portion is determined by the
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   238
  difference of contexts, not the facts being exported!  There is a
20459
wenzelm
parents: 20458
diff changeset
   239
  separate flag to indicate a goal context, where the result is meant
29760
9c6c1b3f3eb6 tuned refs;
wenzelm
parents: 29755
diff changeset
   240
  to refine an enclosing sub-goal of a structured proof state.
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   241
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   242
  \<^medskip>
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   243
  The most basic export rule discharges assumptions directly
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   244
  by means of the \<open>\<Longrightarrow>\<close> introduction rule:
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   245
  \[
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   246
  \infer[(\<open>\<Longrightarrow>\<hyphen>intro\<close>)]{\<open>\<Gamma> - A \<turnstile> A \<Longrightarrow> B\<close>}{\<open>\<Gamma> \<turnstile> B\<close>}
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   247
  \]
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   248
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   249
  The variant for goal refinements marks the newly introduced
20474
wenzelm
parents: 20472
diff changeset
   250
  premises, which causes the canonical Isar goal refinement scheme to
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   251
  enforce unification with local premises within the goal:
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   252
  \[
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   253
  \infer[(\<open>#\<Longrightarrow>\<hyphen>intro\<close>)]{\<open>\<Gamma> - A \<turnstile> #A \<Longrightarrow> B\<close>}{\<open>\<Gamma> \<turnstile> B\<close>}
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   254
  \]
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   255
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   256
  \<^medskip>
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   257
  Alternative versions of assumptions may perform arbitrary
20474
wenzelm
parents: 20472
diff changeset
   258
  transformations on export, as long as the corresponding portion of
20459
wenzelm
parents: 20458
diff changeset
   259
  hypotheses is removed from the given facts.  For example, a local
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   260
  definition works by fixing \<open>x\<close> and assuming \<open>x \<equiv> t\<close>,
20459
wenzelm
parents: 20458
diff changeset
   261
  with the following export rule to reverse the effect:
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   262
  \[
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   263
  \infer[(\<open>\<equiv>\<hyphen>expand\<close>)]{\<open>\<Gamma> - (x \<equiv> t) \<turnstile> B t\<close>}{\<open>\<Gamma> \<turnstile> B x\<close>}
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   264
  \]
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   265
  This works, because the assumption \<open>x \<equiv> t\<close> was introduced in
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   266
  a context with \<open>x\<close> being fresh, so \<open>x\<close> does not
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   267
  occur in \<open>\<Gamma>\<close> here.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   268
\<close>
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   269
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   270
text %mlref \<open>
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   271
  \begin{mldecls}
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   272
  @{index_ML_type Assumption.export} \\
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 53015
diff changeset
   273
  @{index_ML Assumption.assume: "Proof.context -> cterm -> thm"} \\
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   274
  @{index_ML Assumption.add_assms:
20459
wenzelm
parents: 20458
diff changeset
   275
    "Assumption.export ->
wenzelm
parents: 20458
diff changeset
   276
  cterm list -> Proof.context -> thm list * Proof.context"} \\
wenzelm
parents: 20458
diff changeset
   277
  @{index_ML Assumption.add_assumes: "
wenzelm
parents: 20458
diff changeset
   278
  cterm list -> Proof.context -> thm list * Proof.context"} \\
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   279
  @{index_ML Assumption.export: "bool -> Proof.context -> Proof.context -> thm -> thm"} \\
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   280
  \end{mldecls}
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   281
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 61416
diff changeset
   282
  \<^descr> Type @{ML_type Assumption.export} represents arbitrary export
39864
wenzelm
parents: 39861
diff changeset
   283
  rules, which is any function of type @{ML_type "bool -> cterm list
wenzelm
parents: 39861
diff changeset
   284
  -> thm -> thm"}, where the @{ML_type "bool"} indicates goal mode,
wenzelm
parents: 39861
diff changeset
   285
  and the @{ML_type "cterm list"} the collection of assumptions to be
wenzelm
parents: 39861
diff changeset
   286
  discharged simultaneously.
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   287
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   288
  \<^descr> @{ML Assumption.assume}~\<open>ctxt A\<close> turns proposition \<open>A\<close> into a primitive assumption \<open>A \<turnstile> A'\<close>, where the
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   289
  conclusion \<open>A'\<close> is in HHF normal form.
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   290
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   291
  \<^descr> @{ML Assumption.add_assms}~\<open>r As\<close> augments the context
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   292
  by assumptions \<open>As\<close> with export rule \<open>r\<close>.  The
20474
wenzelm
parents: 20472
diff changeset
   293
  resulting facts are hypothetical theorems as produced by the raw
wenzelm
parents: 20472
diff changeset
   294
  @{ML Assumption.assume}.
20459
wenzelm
parents: 20458
diff changeset
   295
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   296
  \<^descr> @{ML Assumption.add_assumes}~\<open>As\<close> is a special case of
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   297
  @{ML Assumption.add_assms} where the export rule performs \<open>\<Longrightarrow>\<hyphen>intro\<close> or \<open>#\<Longrightarrow>\<hyphen>intro\<close>, depending on goal
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   298
  mode.
20458
ab1d60e1ee31 explain assumptions;
wenzelm
parents: 20452
diff changeset
   299
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   300
  \<^descr> @{ML Assumption.export}~\<open>is_goal inner outer thm\<close>
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   301
  exports result \<open>thm\<close> from the the \<open>inner\<close> context
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   302
  back into the \<open>outer\<close> one; \<open>is_goal = true\<close> means
20459
wenzelm
parents: 20458
diff changeset
   303
  this is a goal context.  The result is in HHF normal form.  Note
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 40964
diff changeset
   304
  that @{ML "Proof_Context.export"} combines @{ML "Variable.export"}
20459
wenzelm
parents: 20458
diff changeset
   305
  and @{ML "Assumption.export"} in the canonical way.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   306
\<close>
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20218
diff changeset
   307
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   308
text %mlex \<open>The following example demonstrates how rules can be
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   309
  derived by building up a context of assumptions first, and exporting
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   310
  some local fact afterwards.  We refer to @{theory Pure} equality
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   311
  here for testing purposes.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   312
\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   313
59902
6afbe5a99139 misc tuning -- keep name space more clean;
wenzelm
parents: 59567
diff changeset
   314
ML_val \<open>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   315
  (*static compile-time context -- for testing only*)
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   316
  val ctxt0 = @{context};
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   317
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   318
  val ([eq], ctxt1) =
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   319
    ctxt0 |> Assumption.add_assumes [@{cprop "x \<equiv> y"}];
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   320
  val eq' = Thm.symmetric eq;
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   321
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   322
  (*back to original context -- discharges assumption*)
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   323
  val r = Assumption.export false ctxt1 ctxt0 eq';
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   324
\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   325
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   326
text \<open>Note that the variables of the resulting rule are not
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39853
diff changeset
   327
  generalized.  This would have required to fix them properly in the
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39853
diff changeset
   328
  context beforehand, and export wrt.\ variables afterwards (cf.\ @{ML
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   329
  Variable.export} or the combined @{ML "Proof_Context.export"}).\<close>
34932
28e231e4144b some examples for basic context operations;
wenzelm
parents: 34930
diff changeset
   330
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20218
diff changeset
   331
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   332
section \<open>Structured goals and results \label{sec:struct-goals}\<close>
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20218
diff changeset
   333
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   334
text \<open>
20472
wenzelm
parents: 20470
diff changeset
   335
  Local results are established by monotonic reasoning from facts
20474
wenzelm
parents: 20472
diff changeset
   336
  within a context.  This allows common combinations of theorems,
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   337
  e.g.\ via \<open>\<And>/\<Longrightarrow>\<close> elimination, resolution rules, or equational
20474
wenzelm
parents: 20472
diff changeset
   338
  reasoning, see \secref{sec:thms}.  Unaccounted context manipulations
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   339
  should be avoided, notably raw \<open>\<And>/\<Longrightarrow>\<close> introduction or ad-hoc
20472
wenzelm
parents: 20470
diff changeset
   340
  references to free variables or assumptions not present in the proof
wenzelm
parents: 20470
diff changeset
   341
  context.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   342
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   343
  \<^medskip>
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   344
  The \<open>SUBPROOF\<close> combinator allows to structure a
20491
wenzelm
parents: 20474
diff changeset
   345
  tactical proof recursively by decomposing a selected sub-goal:
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   346
  \<open>(\<And>x. A(x) \<Longrightarrow> B(x)) \<Longrightarrow> \<dots>\<close> is turned into \<open>B(x) \<Longrightarrow> \<dots>\<close>
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   347
  after fixing \<open>x\<close> and assuming \<open>A(x)\<close>.  This means
20491
wenzelm
parents: 20474
diff changeset
   348
  the tactic needs to solve the conclusion, but may use the premise as
wenzelm
parents: 20474
diff changeset
   349
  a local fact, for locally fixed variables.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   350
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   351
  The family of \<open>FOCUS\<close> combinators is similar to \<open>SUBPROOF\<close>, but allows to retain schematic variables and pending
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   352
  subgoals in the resulting goal state.
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   353
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   354
  The \<open>prove\<close> operation provides an interface for structured
20491
wenzelm
parents: 20474
diff changeset
   355
  backwards reasoning under program control, with some explicit sanity
wenzelm
parents: 20474
diff changeset
   356
  checks of the result.  The goal context can be augmented by
wenzelm
parents: 20474
diff changeset
   357
  additional fixed variables (cf.\ \secref{sec:variables}) and
wenzelm
parents: 20474
diff changeset
   358
  assumptions (cf.\ \secref{sec:assumptions}), which will be available
wenzelm
parents: 20474
diff changeset
   359
  as local facts during the proof and discharged into implications in
wenzelm
parents: 20474
diff changeset
   360
  the result.  Type and term variables are generalized as usual,
wenzelm
parents: 20474
diff changeset
   361
  according to the context.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   362
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   363
  The \<open>obtain\<close> operation produces results by eliminating
20491
wenzelm
parents: 20474
diff changeset
   364
  existing facts by means of a given tactic.  This acts like a dual
wenzelm
parents: 20474
diff changeset
   365
  conclusion: the proof demonstrates that the context may be augmented
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   366
  by parameters and assumptions, without affecting any conclusions
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   367
  that do not mention these parameters.  See also
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 56579
diff changeset
   368
  @{cite "isabelle-isar-ref"} for the user-level @{command obtain} and
39851
7219a771ab63 more examples;
wenzelm
parents: 39846
diff changeset
   369
  @{command guess} elements.  Final results, which may not refer to
20491
wenzelm
parents: 20474
diff changeset
   370
  the parameters in the conclusion, need to exported explicitly into
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   371
  the original context.\<close>
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   372
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   373
text %mlref \<open>
20472
wenzelm
parents: 20470
diff changeset
   374
  \begin{mldecls}
39821
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   375
  @{index_ML SUBPROOF: "(Subgoal.focus -> tactic) ->
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   376
  Proof.context -> int -> tactic"} \\
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   377
  @{index_ML Subgoal.FOCUS: "(Subgoal.focus -> tactic) ->
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   378
  Proof.context -> int -> tactic"} \\
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   379
  @{index_ML Subgoal.FOCUS_PREMS: "(Subgoal.focus -> tactic) ->
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   380
  Proof.context -> int -> tactic"} \\
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   381
  @{index_ML Subgoal.FOCUS_PARAMS: "(Subgoal.focus -> tactic) ->
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   382
  Proof.context -> int -> tactic"} \\
60695
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   383
  @{index_ML Subgoal.focus: "Proof.context -> int -> binding list option ->
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   384
  thm -> Subgoal.focus * thm"} \\
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   385
  @{index_ML Subgoal.focus_prems: "Proof.context -> int -> binding list option ->
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   386
  thm -> Subgoal.focus * thm"} \\
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   387
  @{index_ML Subgoal.focus_params: "Proof.context -> int -> binding list option ->
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   388
  thm -> Subgoal.focus * thm"} \\
20547
wenzelm
parents: 20542
diff changeset
   389
  \end{mldecls}
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   390
20547
wenzelm
parents: 20542
diff changeset
   391
  \begin{mldecls}
20472
wenzelm
parents: 20470
diff changeset
   392
  @{index_ML Goal.prove: "Proof.context -> string list -> term list -> term ->
wenzelm
parents: 20470
diff changeset
   393
  ({prems: thm list, context: Proof.context} -> tactic) -> thm"} \\
59564
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   394
  @{index_ML Goal.prove_common: "Proof.context -> int option ->
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   395
  string list -> term list -> term list ->
20472
wenzelm
parents: 20470
diff changeset
   396
  ({prems: thm list, context: Proof.context} -> tactic) -> thm list"} \\
20547
wenzelm
parents: 20542
diff changeset
   397
  \end{mldecls}
wenzelm
parents: 20542
diff changeset
   398
  \begin{mldecls}
39821
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   399
  @{index_ML Obtain.result: "(Proof.context -> tactic) -> thm list ->
bf164c153d10 minor tuning and updating;
wenzelm
parents: 34932
diff changeset
   400
  Proof.context -> ((string * cterm) list * thm list) * Proof.context"} \\
20472
wenzelm
parents: 20470
diff changeset
   401
  \end{mldecls}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   402
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   403
  \<^descr> @{ML SUBPROOF}~\<open>tac ctxt i\<close> decomposes the structure
29761
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29760
diff changeset
   404
  of the specified sub-goal, producing an extended context and a
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29760
diff changeset
   405
  reduced goal, which needs to be solved by the given tactic.  All
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29760
diff changeset
   406
  schematic parameters of the goal are imported into the context as
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29760
diff changeset
   407
  fixed ones, which may not be instantiated in the sub-proof.
20491
wenzelm
parents: 20474
diff changeset
   408
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 61416
diff changeset
   409
  \<^descr> @{ML Subgoal.FOCUS}, @{ML Subgoal.FOCUS_PREMS}, and @{ML
34930
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   410
  Subgoal.FOCUS_PARAMS} are similar to @{ML SUBPROOF}, but are
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   411
  slightly more flexible: only the specified parts of the subgoal are
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   412
  imported into the context, and the body tactic may introduce new
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   413
  subgoals and schematic variables.
f3bce1cc513c added Subgoal.FOCUS;
wenzelm
parents: 32302
diff changeset
   414
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 61416
diff changeset
   415
  \<^descr> @{ML Subgoal.focus}, @{ML Subgoal.focus_prems}, @{ML
39853
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   416
  Subgoal.focus_params} extract the focus information from a goal
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   417
  state in the same way as the corresponding tacticals above.  This is
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   418
  occasionally useful to experiment without writing actual tactics
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   419
  yet.
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   420
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   421
  \<^descr> @{ML Goal.prove}~\<open>ctxt xs As C tac\<close> states goal \<open>C\<close> in the context augmented by fixed variables \<open>xs\<close> and
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   422
  assumptions \<open>As\<close>, and applies tactic \<open>tac\<close> to solve
20474
wenzelm
parents: 20472
diff changeset
   423
  it.  The latter may depend on the local assumptions being presented
wenzelm
parents: 20472
diff changeset
   424
  as facts.  The result is in HHF normal form.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   425
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   426
  \<^descr> @{ML Goal.prove_common}~\<open>ctxt fork_pri\<close> is the common form
59564
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   427
  to state and prove a simultaneous goal statement, where @{ML Goal.prove}
59567
wenzelm
parents: 59564
diff changeset
   428
  is a convenient shorthand that is most frequently used in applications.
59564
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   429
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   430
  The given list of simultaneous conclusions is encoded in the goal state by
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   431
  means of Pure conjunction: @{ML Goal.conjunction_tac} will turn this into
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   432
  a collection of individual subgoals, but note that the original multi-goal
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   433
  state is usually required for advanced induction.
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   434
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   435
  It is possible to provide an optional priority for a forked proof,
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   436
  typically @{ML "SOME ~1"}, while @{ML NONE} means the proof is immediate
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   437
  (sequential) as for @{ML Goal.prove}. Note that a forked proof does not
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   438
  exhibit any failures in the usual way via exceptions in ML, but
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   439
  accumulates error situations under the execution id of the running
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   440
  transaction. Thus the system is able to expose error messages ultimately
fdc03c8daacc Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm
parents: 58801
diff changeset
   441
  to the end-user, even though the subsequent ML code misses them.
20472
wenzelm
parents: 20470
diff changeset
   442
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   443
  \<^descr> @{ML Obtain.result}~\<open>tac thms ctxt\<close> eliminates the
20491
wenzelm
parents: 20474
diff changeset
   444
  given facts using a tactic, which results in additional fixed
wenzelm
parents: 20474
diff changeset
   445
  variables and assumptions in the context.  Final results need to be
wenzelm
parents: 20474
diff changeset
   446
  exported explicitly.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   447
\<close>
30272
2d612824e642 regenerated document;
wenzelm
parents: 30270
diff changeset
   448
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   449
text %mlex \<open>The following minimal example illustrates how to access
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   450
  the focus information of a structured goal state.\<close>
39853
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   451
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   452
notepad
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   453
begin
39853
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   454
  fix A B C :: "'a \<Rightarrow> bool"
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   455
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   456
  have "\<And>x. A x \<Longrightarrow> B x \<Longrightarrow> C x"
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   457
    ML_val
58728
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   458
     \<open>val {goal, context = goal_ctxt, ...} = @{Isar.goal};
39853
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   459
      val (focus as {params, asms, concl, ...}, goal') =
60695
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   460
        Subgoal.focus goal_ctxt 1 (SOME [@{binding x}]) goal;
39853
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   461
      val [A, B] = #prems focus;
58728
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   462
      val [(_, x)] = #params focus;\<close>
58801
f420225a22d6 'notepad' requires proper nesting of begin/end;
wenzelm
parents: 58728
diff changeset
   463
    sorry
f420225a22d6 'notepad' requires proper nesting of begin/end;
wenzelm
parents: 58728
diff changeset
   464
end
39853
a5a731dec31c more examples;
wenzelm
parents: 39851
diff changeset
   465
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   466
text \<open>
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   467
  \<^medskip>
b9a3324e4e62 more symbols;
wenzelm
parents: 60754
diff changeset
   468
  The next example demonstrates forward-elimination in
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   469
  a local context, using @{ML Obtain.result}.\<close>
39851
7219a771ab63 more examples;
wenzelm
parents: 39846
diff changeset
   470
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   471
notepad
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   472
begin
39851
7219a771ab63 more examples;
wenzelm
parents: 39846
diff changeset
   473
  assume ex: "\<exists>x. B x"
7219a771ab63 more examples;
wenzelm
parents: 39846
diff changeset
   474
58728
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   475
  ML_prf %"ML"
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   476
   \<open>val ctxt0 = @{context};
39851
7219a771ab63 more examples;
wenzelm
parents: 39846
diff changeset
   477
    val (([(_, x)], [B]), ctxt1) = ctxt0
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60695
diff changeset
   478
      |> Obtain.result (fn _ => eresolve_tac ctxt0 @{thms exE} 1) [@{thm ex}];\<close>
58728
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   479
  ML_prf %"ML"
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   480
   \<open>singleton (Proof_Context.export ctxt1 ctxt0) @{thm refl};\<close>
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   481
  ML_prf %"ML"
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   482
   \<open>Proof_Context.export ctxt1 ctxt0 [Thm.reflexive x]
42398b610f86 tuned spacing;
wenzelm
parents: 58618
diff changeset
   483
      handle ERROR msg => (warning msg; []);\<close>
40964
482a8334ee9e prefer 'notepad' over 'example_proof';
wenzelm
parents: 40153
diff changeset
   484
end
39851
7219a771ab63 more examples;
wenzelm
parents: 39846
diff changeset
   485
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   486
end