src/Doc/IsarImplementation/Logic.thy
author wenzelm
Mon Jun 17 20:15:34 2013 +0200 (2013-06-17)
changeset 52411 f192c4ea5b17
parent 52410 fb1fb867c146
child 52412 4cfa094da3cb
permissions -rw-r--r--
more on reconstructing and checking proof terms;
wenzelm@29755
     1
theory Logic
wenzelm@29755
     2
imports Base
wenzelm@29755
     3
begin
wenzelm@18537
     4
wenzelm@20470
     5
chapter {* Primitive logic \label{ch:logic} *}
wenzelm@18537
     6
wenzelm@20480
     7
text {*
wenzelm@20480
     8
  The logical foundations of Isabelle/Isar are that of the Pure logic,
wenzelm@29774
     9
  which has been introduced as a Natural Deduction framework in
wenzelm@20480
    10
  \cite{paulson700}.  This is essentially the same logic as ``@{text
wenzelm@20493
    11
  "\<lambda>HOL"}'' in the more abstract setting of Pure Type Systems (PTS)
wenzelm@20480
    12
  \cite{Barendregt-Geuvers:2001}, although there are some key
wenzelm@20491
    13
  differences in the specific treatment of simple types in
wenzelm@20491
    14
  Isabelle/Pure.
wenzelm@20480
    15
wenzelm@20480
    16
  Following type-theoretic parlance, the Pure logic consists of three
wenzelm@20543
    17
  levels of @{text "\<lambda>"}-calculus with corresponding arrows, @{text
wenzelm@20480
    18
  "\<Rightarrow>"} for syntactic function space (terms depending on terms), @{text
wenzelm@20480
    19
  "\<And>"} for universal quantification (proofs depending on terms), and
wenzelm@20480
    20
  @{text "\<Longrightarrow>"} for implication (proofs depending on proofs).
wenzelm@20480
    21
wenzelm@20537
    22
  Derivations are relative to a logical theory, which declares type
wenzelm@20537
    23
  constructors, constants, and axioms.  Theory declarations support
wenzelm@20537
    24
  schematic polymorphism, which is strictly speaking outside the
wenzelm@20537
    25
  logic.\footnote{This is the deeper logical reason, why the theory
wenzelm@20537
    26
  context @{text "\<Theta>"} is separate from the proof context @{text "\<Gamma>"}
wenzelm@34929
    27
  of the core calculus: type constructors, term constants, and facts
wenzelm@34929
    28
  (proof constants) may involve arbitrary type schemes, but the type
wenzelm@34929
    29
  of a locally fixed term parameter is also fixed!}
wenzelm@20480
    30
*}
wenzelm@20480
    31
wenzelm@20480
    32
wenzelm@20451
    33
section {* Types \label{sec:types} *}
wenzelm@20437
    34
wenzelm@20437
    35
text {*
wenzelm@20480
    36
  The language of types is an uninterpreted order-sorted first-order
wenzelm@20480
    37
  algebra; types are qualified by ordered type classes.
wenzelm@20480
    38
wenzelm@20480
    39
  \medskip A \emph{type class} is an abstract syntactic entity
wenzelm@20480
    40
  declared in the theory context.  The \emph{subclass relation} @{text
wenzelm@20480
    41
  "c\<^isub>1 \<subseteq> c\<^isub>2"} is specified by stating an acyclic
wenzelm@20491
    42
  generating relation; the transitive closure is maintained
wenzelm@20491
    43
  internally.  The resulting relation is an ordering: reflexive,
wenzelm@20491
    44
  transitive, and antisymmetric.
wenzelm@20451
    45
wenzelm@34929
    46
  A \emph{sort} is a list of type classes written as @{text "s = {c\<^isub>1,
wenzelm@34929
    47
  \<dots>, c\<^isub>m}"}, it represents symbolic intersection.  Notationally, the
wenzelm@34929
    48
  curly braces are omitted for singleton intersections, i.e.\ any
wenzelm@34929
    49
  class @{text "c"} may be read as a sort @{text "{c}"}.  The ordering
wenzelm@34929
    50
  on type classes is extended to sorts according to the meaning of
wenzelm@34929
    51
  intersections: @{text "{c\<^isub>1, \<dots> c\<^isub>m} \<subseteq> {d\<^isub>1, \<dots>, d\<^isub>n}"} iff @{text
wenzelm@34929
    52
  "\<forall>j. \<exists>i. c\<^isub>i \<subseteq> d\<^isub>j"}.  The empty intersection @{text "{}"} refers to
wenzelm@34929
    53
  the universal sort, which is the largest element wrt.\ the sort
wenzelm@34929
    54
  order.  Thus @{text "{}"} represents the ``full sort'', not the
wenzelm@34929
    55
  empty one!  The intersection of all (finitely many) classes declared
wenzelm@34929
    56
  in the current theory is the least element wrt.\ the sort ordering.
wenzelm@20480
    57
wenzelm@20491
    58
  \medskip A \emph{fixed type variable} is a pair of a basic name
wenzelm@20537
    59
  (starting with a @{text "'"} character) and a sort constraint, e.g.\
wenzelm@20537
    60
  @{text "('a, s)"} which is usually printed as @{text "\<alpha>\<^isub>s"}.
wenzelm@20537
    61
  A \emph{schematic type variable} is a pair of an indexname and a
wenzelm@20537
    62
  sort constraint, e.g.\ @{text "(('a, 0), s)"} which is usually
wenzelm@20537
    63
  printed as @{text "?\<alpha>\<^isub>s"}.
wenzelm@20451
    64
wenzelm@20480
    65
  Note that \emph{all} syntactic components contribute to the identity
wenzelm@34929
    66
  of type variables: basic name, index, and sort constraint.  The core
wenzelm@34929
    67
  logic handles type variables with the same name but different sorts
wenzelm@34929
    68
  as different, although the type-inference layer (which is outside
wenzelm@34929
    69
  the core) rejects anything like that.
wenzelm@20451
    70
wenzelm@20493
    71
  A \emph{type constructor} @{text "\<kappa>"} is a @{text "k"}-ary operator
wenzelm@20493
    72
  on types declared in the theory.  Type constructor application is
wenzelm@20537
    73
  written postfix as @{text "(\<alpha>\<^isub>1, \<dots>, \<alpha>\<^isub>k)\<kappa>"}.  For
wenzelm@20537
    74
  @{text "k = 0"} the argument tuple is omitted, e.g.\ @{text "prop"}
wenzelm@20537
    75
  instead of @{text "()prop"}.  For @{text "k = 1"} the parentheses
wenzelm@20537
    76
  are omitted, e.g.\ @{text "\<alpha> list"} instead of @{text "(\<alpha>)list"}.
wenzelm@20537
    77
  Further notation is provided for specific constructors, notably the
wenzelm@20537
    78
  right-associative infix @{text "\<alpha> \<Rightarrow> \<beta>"} instead of @{text "(\<alpha>,
wenzelm@20537
    79
  \<beta>)fun"}.
wenzelm@20480
    80
  
wenzelm@34929
    81
  The logical category \emph{type} is defined inductively over type
wenzelm@34929
    82
  variables and type constructors as follows: @{text "\<tau> = \<alpha>\<^isub>s | ?\<alpha>\<^isub>s |
wenzelm@20543
    83
  (\<tau>\<^sub>1, \<dots>, \<tau>\<^sub>k)\<kappa>"}.
wenzelm@20451
    84
wenzelm@20514
    85
  A \emph{type abbreviation} is a syntactic definition @{text
wenzelm@20494
    86
  "(\<^vec>\<alpha>)\<kappa> = \<tau>"} of an arbitrary type expression @{text "\<tau>"} over
wenzelm@20537
    87
  variables @{text "\<^vec>\<alpha>"}.  Type abbreviations appear as type
wenzelm@20537
    88
  constructors in the syntax, but are expanded before entering the
wenzelm@20537
    89
  logical core.
wenzelm@20480
    90
wenzelm@20480
    91
  A \emph{type arity} declares the image behavior of a type
wenzelm@20494
    92
  constructor wrt.\ the algebra of sorts: @{text "\<kappa> :: (s\<^isub>1, \<dots>,
wenzelm@20494
    93
  s\<^isub>k)s"} means that @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>k)\<kappa>"} is
wenzelm@20494
    94
  of sort @{text "s"} if every argument type @{text "\<tau>\<^isub>i"} is
wenzelm@20494
    95
  of sort @{text "s\<^isub>i"}.  Arity declarations are implicitly
wenzelm@20494
    96
  completed, i.e.\ @{text "\<kappa> :: (\<^vec>s)c"} entails @{text "\<kappa> ::
wenzelm@20491
    97
  (\<^vec>s)c'"} for any @{text "c' \<supseteq> c"}.
wenzelm@20491
    98
wenzelm@20491
    99
  \medskip The sort algebra is always maintained as \emph{coregular},
wenzelm@20491
   100
  which means that type arities are consistent with the subclass
wenzelm@20537
   101
  relation: for any type constructor @{text "\<kappa>"}, and classes @{text
wenzelm@20537
   102
  "c\<^isub>1 \<subseteq> c\<^isub>2"}, and arities @{text "\<kappa> ::
wenzelm@20537
   103
  (\<^vec>s\<^isub>1)c\<^isub>1"} and @{text "\<kappa> ::
wenzelm@20537
   104
  (\<^vec>s\<^isub>2)c\<^isub>2"} holds @{text "\<^vec>s\<^isub>1 \<subseteq>
wenzelm@20537
   105
  \<^vec>s\<^isub>2"} component-wise.
wenzelm@20451
   106
wenzelm@20491
   107
  The key property of a coregular order-sorted algebra is that sort
wenzelm@20537
   108
  constraints can be solved in a most general fashion: for each type
wenzelm@20537
   109
  constructor @{text "\<kappa>"} and sort @{text "s"} there is a most general
wenzelm@20537
   110
  vector of argument sorts @{text "(s\<^isub>1, \<dots>, s\<^isub>k)"} such
wenzelm@20537
   111
  that a type scheme @{text "(\<alpha>\<^bsub>s\<^isub>1\<^esub>, \<dots>,
wenzelm@20537
   112
  \<alpha>\<^bsub>s\<^isub>k\<^esub>)\<kappa>"} is of sort @{text "s"}.
wenzelm@20543
   113
  Consequently, type unification has most general solutions (modulo
wenzelm@20543
   114
  equivalence of sorts), so type-inference produces primary types as
wenzelm@20543
   115
  expected \cite{nipkow-prehofer}.
wenzelm@20480
   116
*}
wenzelm@20451
   117
wenzelm@20480
   118
text %mlref {*
wenzelm@20480
   119
  \begin{mldecls}
wenzelm@34921
   120
  @{index_ML_type class: string} \\
wenzelm@34921
   121
  @{index_ML_type sort: "class list"} \\
wenzelm@34921
   122
  @{index_ML_type arity: "string * sort list * sort"} \\
wenzelm@20480
   123
  @{index_ML_type typ} \\
wenzelm@39846
   124
  @{index_ML Term.map_atyps: "(typ -> typ) -> typ -> typ"} \\
wenzelm@39846
   125
  @{index_ML Term.fold_atyps: "(typ -> 'a -> 'a) -> typ -> 'a -> 'a"} \\
wenzelm@20547
   126
  \end{mldecls}
wenzelm@20547
   127
  \begin{mldecls}
wenzelm@20480
   128
  @{index_ML Sign.subsort: "theory -> sort * sort -> bool"} \\
wenzelm@20480
   129
  @{index_ML Sign.of_sort: "theory -> typ * sort -> bool"} \\
wenzelm@47174
   130
  @{index_ML Sign.add_type: "Proof.context -> binding * int * mixfix -> theory -> theory"} \\
wenzelm@42401
   131
  @{index_ML Sign.add_type_abbrev: "Proof.context ->
wenzelm@42401
   132
  binding * string list * typ -> theory -> theory"} \\
wenzelm@30355
   133
  @{index_ML Sign.primitive_class: "binding * class list -> theory -> theory"} \\
wenzelm@20480
   134
  @{index_ML Sign.primitive_classrel: "class * class -> theory -> theory"} \\
wenzelm@20480
   135
  @{index_ML Sign.primitive_arity: "arity -> theory -> theory"} \\
wenzelm@20480
   136
  \end{mldecls}
wenzelm@20480
   137
wenzelm@20480
   138
  \begin{description}
wenzelm@20480
   139
wenzelm@39864
   140
  \item Type @{ML_type class} represents type classes.
wenzelm@20480
   141
wenzelm@39864
   142
  \item Type @{ML_type sort} represents sorts, i.e.\ finite
wenzelm@39864
   143
  intersections of classes.  The empty list @{ML "[]: sort"} refers to
wenzelm@39864
   144
  the empty class intersection, i.e.\ the ``full sort''.
wenzelm@20451
   145
wenzelm@39864
   146
  \item Type @{ML_type arity} represents type arities.  A triple
wenzelm@39864
   147
  @{text "(\<kappa>, \<^vec>s, s) : arity"} represents @{text "\<kappa> ::
wenzelm@39864
   148
  (\<^vec>s)s"} as described above.
wenzelm@20480
   149
wenzelm@39864
   150
  \item Type @{ML_type typ} represents types; this is a datatype with
wenzelm@20480
   151
  constructors @{ML TFree}, @{ML TVar}, @{ML Type}.
wenzelm@20480
   152
wenzelm@39846
   153
  \item @{ML Term.map_atyps}~@{text "f \<tau>"} applies the mapping @{text
wenzelm@39846
   154
  "f"} to all atomic types (@{ML TFree}, @{ML TVar}) occurring in
wenzelm@39846
   155
  @{text "\<tau>"}.
wenzelm@20519
   156
wenzelm@39846
   157
  \item @{ML Term.fold_atyps}~@{text "f \<tau>"} iterates the operation
wenzelm@39846
   158
  @{text "f"} over all occurrences of atomic types (@{ML TFree}, @{ML
wenzelm@39846
   159
  TVar}) in @{text "\<tau>"}; the type structure is traversed from left to
wenzelm@39846
   160
  right.
wenzelm@20494
   161
wenzelm@20480
   162
  \item @{ML Sign.subsort}~@{text "thy (s\<^isub>1, s\<^isub>2)"}
wenzelm@20480
   163
  tests the subsort relation @{text "s\<^isub>1 \<subseteq> s\<^isub>2"}.
wenzelm@20480
   164
wenzelm@20537
   165
  \item @{ML Sign.of_sort}~@{text "thy (\<tau>, s)"} tests whether type
wenzelm@20537
   166
  @{text "\<tau>"} is of sort @{text "s"}.
wenzelm@20480
   167
wenzelm@47174
   168
  \item @{ML Sign.add_type}~@{text "ctxt (\<kappa>, k, mx)"} declares a
wenzelm@42401
   169
  new type constructors @{text "\<kappa>"} with @{text "k"} arguments and
wenzelm@20480
   170
  optional mixfix syntax.
wenzelm@20451
   171
wenzelm@42401
   172
  \item @{ML Sign.add_type_abbrev}~@{text "ctxt (\<kappa>, \<^vec>\<alpha>, \<tau>)"}
wenzelm@42401
   173
  defines a new type abbreviation @{text "(\<^vec>\<alpha>)\<kappa> = \<tau>"}.
wenzelm@20480
   174
wenzelm@20480
   175
  \item @{ML Sign.primitive_class}~@{text "(c, [c\<^isub>1, \<dots>,
wenzelm@20537
   176
  c\<^isub>n])"} declares a new class @{text "c"}, together with class
wenzelm@20494
   177
  relations @{text "c \<subseteq> c\<^isub>i"}, for @{text "i = 1, \<dots>, n"}.
wenzelm@20480
   178
wenzelm@20480
   179
  \item @{ML Sign.primitive_classrel}~@{text "(c\<^isub>1,
wenzelm@20543
   180
  c\<^isub>2)"} declares the class relation @{text "c\<^isub>1 \<subseteq>
wenzelm@20480
   181
  c\<^isub>2"}.
wenzelm@20480
   182
wenzelm@20494
   183
  \item @{ML Sign.primitive_arity}~@{text "(\<kappa>, \<^vec>s, s)"} declares
wenzelm@20537
   184
  the arity @{text "\<kappa> :: (\<^vec>s)s"}.
wenzelm@20480
   185
wenzelm@20480
   186
  \end{description}
wenzelm@20437
   187
*}
wenzelm@20437
   188
wenzelm@39832
   189
text %mlantiq {*
wenzelm@39832
   190
  \begin{matharray}{rcl}
wenzelm@39832
   191
  @{ML_antiquotation_def "class"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   192
  @{ML_antiquotation_def "sort"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   193
  @{ML_antiquotation_def "type_name"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   194
  @{ML_antiquotation_def "type_abbrev"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   195
  @{ML_antiquotation_def "nonterminal"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   196
  @{ML_antiquotation_def "typ"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   197
  \end{matharray}
wenzelm@39832
   198
wenzelm@42510
   199
  @{rail "
wenzelm@42510
   200
  @@{ML_antiquotation class} nameref
wenzelm@39832
   201
  ;
wenzelm@42510
   202
  @@{ML_antiquotation sort} sort
wenzelm@39832
   203
  ;
wenzelm@42510
   204
  (@@{ML_antiquotation type_name} |
wenzelm@42510
   205
   @@{ML_antiquotation type_abbrev} |
wenzelm@42510
   206
   @@{ML_antiquotation nonterminal}) nameref
wenzelm@39832
   207
  ;
wenzelm@42510
   208
  @@{ML_antiquotation typ} type
wenzelm@42510
   209
  "}
wenzelm@39832
   210
wenzelm@39832
   211
  \begin{description}
wenzelm@39832
   212
wenzelm@39832
   213
  \item @{text "@{class c}"} inlines the internalized class @{text
wenzelm@39832
   214
  "c"} --- as @{ML_type string} literal.
wenzelm@39832
   215
wenzelm@39832
   216
  \item @{text "@{sort s}"} inlines the internalized sort @{text "s"}
wenzelm@39832
   217
  --- as @{ML_type "string list"} literal.
wenzelm@39832
   218
wenzelm@39832
   219
  \item @{text "@{type_name c}"} inlines the internalized type
wenzelm@39832
   220
  constructor @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   221
wenzelm@39832
   222
  \item @{text "@{type_abbrev c}"} inlines the internalized type
wenzelm@39832
   223
  abbreviation @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   224
wenzelm@39832
   225
  \item @{text "@{nonterminal c}"} inlines the internalized syntactic
wenzelm@39832
   226
  type~/ grammar nonterminal @{text "c"} --- as @{ML_type string}
wenzelm@39832
   227
  literal.
wenzelm@39832
   228
wenzelm@39832
   229
  \item @{text "@{typ \<tau>}"} inlines the internalized type @{text "\<tau>"}
wenzelm@39832
   230
  --- as constructor term for datatype @{ML_type typ}.
wenzelm@39832
   231
wenzelm@39832
   232
  \end{description}
wenzelm@39832
   233
*}
wenzelm@39832
   234
wenzelm@20437
   235
wenzelm@20451
   236
section {* Terms \label{sec:terms} *}
wenzelm@18537
   237
wenzelm@18537
   238
text {*
wenzelm@20491
   239
  The language of terms is that of simply-typed @{text "\<lambda>"}-calculus
wenzelm@20520
   240
  with de-Bruijn indices for bound variables (cf.\ \cite{debruijn72}
wenzelm@29761
   241
  or \cite{paulson-ml2}), with the types being determined by the
wenzelm@29761
   242
  corresponding binders.  In contrast, free variables and constants
wenzelm@34929
   243
  have an explicit name and type in each occurrence.
wenzelm@20514
   244
wenzelm@20514
   245
  \medskip A \emph{bound variable} is a natural number @{text "b"},
wenzelm@20537
   246
  which accounts for the number of intermediate binders between the
wenzelm@20537
   247
  variable occurrence in the body and its binding position.  For
wenzelm@34929
   248
  example, the de-Bruijn term @{text "\<lambda>\<^bsub>bool\<^esub>. \<lambda>\<^bsub>bool\<^esub>. 1 \<and> 0"} would
wenzelm@34929
   249
  correspond to @{text "\<lambda>x\<^bsub>bool\<^esub>. \<lambda>y\<^bsub>bool\<^esub>. x \<and> y"} in a named
wenzelm@20543
   250
  representation.  Note that a bound variable may be represented by
wenzelm@20543
   251
  different de-Bruijn indices at different occurrences, depending on
wenzelm@20543
   252
  the nesting of abstractions.
wenzelm@20537
   253
wenzelm@20543
   254
  A \emph{loose variable} is a bound variable that is outside the
wenzelm@20537
   255
  scope of local binders.  The types (and names) for loose variables
wenzelm@20543
   256
  can be managed as a separate context, that is maintained as a stack
wenzelm@20543
   257
  of hypothetical binders.  The core logic operates on closed terms,
wenzelm@20543
   258
  without any loose variables.
wenzelm@20514
   259
wenzelm@20537
   260
  A \emph{fixed variable} is a pair of a basic name and a type, e.g.\
wenzelm@34929
   261
  @{text "(x, \<tau>)"} which is usually printed @{text "x\<^isub>\<tau>"} here.  A
wenzelm@20537
   262
  \emph{schematic variable} is a pair of an indexname and a type,
wenzelm@34929
   263
  e.g.\ @{text "((x, 0), \<tau>)"} which is likewise printed as @{text
wenzelm@20537
   264
  "?x\<^isub>\<tau>"}.
wenzelm@20491
   265
wenzelm@20537
   266
  \medskip A \emph{constant} is a pair of a basic name and a type,
wenzelm@34929
   267
  e.g.\ @{text "(c, \<tau>)"} which is usually printed as @{text "c\<^isub>\<tau>"}
wenzelm@34929
   268
  here.  Constants are declared in the context as polymorphic families
wenzelm@34929
   269
  @{text "c :: \<sigma>"}, meaning that all substitution instances @{text
wenzelm@34929
   270
  "c\<^isub>\<tau>"} for @{text "\<tau> = \<sigma>\<vartheta>"} are valid.
wenzelm@20514
   271
wenzelm@34929
   272
  The vector of \emph{type arguments} of constant @{text "c\<^isub>\<tau>"} wrt.\
wenzelm@34929
   273
  the declaration @{text "c :: \<sigma>"} is defined as the codomain of the
wenzelm@34929
   274
  matcher @{text "\<vartheta> = {?\<alpha>\<^isub>1 \<mapsto> \<tau>\<^isub>1, \<dots>, ?\<alpha>\<^isub>n \<mapsto> \<tau>\<^isub>n}"} presented in
wenzelm@34929
   275
  canonical order @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n)"}, corresponding to the
wenzelm@34929
   276
  left-to-right occurrences of the @{text "\<alpha>\<^isub>i"} in @{text "\<sigma>"}.
wenzelm@34929
   277
  Within a given theory context, there is a one-to-one correspondence
wenzelm@34929
   278
  between any constant @{text "c\<^isub>\<tau>"} and the application @{text "c(\<tau>\<^isub>1,
wenzelm@34929
   279
  \<dots>, \<tau>\<^isub>n)"} of its type arguments.  For example, with @{text "plus :: \<alpha>
wenzelm@34929
   280
  \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"}, the instance @{text "plus\<^bsub>nat \<Rightarrow> nat \<Rightarrow> nat\<^esub>"} corresponds to
wenzelm@34929
   281
  @{text "plus(nat)"}.
wenzelm@20480
   282
wenzelm@20514
   283
  Constant declarations @{text "c :: \<sigma>"} may contain sort constraints
wenzelm@20514
   284
  for type variables in @{text "\<sigma>"}.  These are observed by
wenzelm@20514
   285
  type-inference as expected, but \emph{ignored} by the core logic.
wenzelm@20514
   286
  This means the primitive logic is able to reason with instances of
wenzelm@20537
   287
  polymorphic constants that the user-level type-checker would reject
wenzelm@20537
   288
  due to violation of type class restrictions.
wenzelm@20480
   289
wenzelm@39861
   290
  \medskip An \emph{atomic term} is either a variable or constant.
wenzelm@34929
   291
  The logical category \emph{term} is defined inductively over atomic
wenzelm@34929
   292
  terms, with abstraction and application as follows: @{text "t = b |
wenzelm@34929
   293
  x\<^isub>\<tau> | ?x\<^isub>\<tau> | c\<^isub>\<tau> | \<lambda>\<^isub>\<tau>. t | t\<^isub>1 t\<^isub>2"}.  Parsing and printing takes care of
wenzelm@34929
   294
  converting between an external representation with named bound
wenzelm@34929
   295
  variables.  Subsequently, we shall use the latter notation instead
wenzelm@34929
   296
  of internal de-Bruijn representation.
wenzelm@20498
   297
wenzelm@20537
   298
  The inductive relation @{text "t :: \<tau>"} assigns a (unique) type to a
wenzelm@20537
   299
  term according to the structure of atomic terms, abstractions, and
wenzelm@20537
   300
  applicatins:
wenzelm@20498
   301
  \[
wenzelm@20501
   302
  \infer{@{text "a\<^isub>\<tau> :: \<tau>"}}{}
wenzelm@20498
   303
  \qquad
wenzelm@20501
   304
  \infer{@{text "(\<lambda>x\<^sub>\<tau>. t) :: \<tau> \<Rightarrow> \<sigma>"}}{@{text "t :: \<sigma>"}}
wenzelm@20501
   305
  \qquad
wenzelm@20501
   306
  \infer{@{text "t u :: \<sigma>"}}{@{text "t :: \<tau> \<Rightarrow> \<sigma>"} & @{text "u :: \<tau>"}}
wenzelm@20498
   307
  \]
wenzelm@20514
   308
  A \emph{well-typed term} is a term that can be typed according to these rules.
wenzelm@20498
   309
wenzelm@20514
   310
  Typing information can be omitted: type-inference is able to
wenzelm@20514
   311
  reconstruct the most general type of a raw term, while assigning
wenzelm@20514
   312
  most general types to all of its variables and constants.
wenzelm@20514
   313
  Type-inference depends on a context of type constraints for fixed
wenzelm@20514
   314
  variables, and declarations for polymorphic constants.
wenzelm@20514
   315
wenzelm@20537
   316
  The identity of atomic terms consists both of the name and the type
wenzelm@20537
   317
  component.  This means that different variables @{text
wenzelm@34929
   318
  "x\<^bsub>\<tau>\<^isub>1\<^esub>"} and @{text "x\<^bsub>\<tau>\<^isub>2\<^esub>"} may become the same after
wenzelm@34929
   319
  type instantiation.  Type-inference rejects variables of the same
wenzelm@34929
   320
  name, but different types.  In contrast, mixed instances of
wenzelm@34929
   321
  polymorphic constants occur routinely.
wenzelm@20514
   322
wenzelm@20514
   323
  \medskip The \emph{hidden polymorphism} of a term @{text "t :: \<sigma>"}
wenzelm@20514
   324
  is the set of type variables occurring in @{text "t"}, but not in
wenzelm@34929
   325
  its type @{text "\<sigma>"}.  This means that the term implicitly depends
wenzelm@34929
   326
  on type arguments that are not accounted in the result type, i.e.\
wenzelm@34929
   327
  there are different type instances @{text "t\<vartheta> :: \<sigma>"} and
wenzelm@34929
   328
  @{text "t\<vartheta>' :: \<sigma>"} with the same type.  This slightly
wenzelm@20543
   329
  pathological situation notoriously demands additional care.
wenzelm@20514
   330
wenzelm@20514
   331
  \medskip A \emph{term abbreviation} is a syntactic definition @{text
wenzelm@20537
   332
  "c\<^isub>\<sigma> \<equiv> t"} of a closed term @{text "t"} of type @{text "\<sigma>"},
wenzelm@20537
   333
  without any hidden polymorphism.  A term abbreviation looks like a
wenzelm@20543
   334
  constant in the syntax, but is expanded before entering the logical
wenzelm@20543
   335
  core.  Abbreviations are usually reverted when printing terms, using
wenzelm@20543
   336
  @{text "t \<rightarrow> c\<^isub>\<sigma>"} as rules for higher-order rewriting.
wenzelm@20519
   337
wenzelm@20519
   338
  \medskip Canonical operations on @{text "\<lambda>"}-terms include @{text
wenzelm@20537
   339
  "\<alpha>\<beta>\<eta>"}-conversion: @{text "\<alpha>"}-conversion refers to capture-free
wenzelm@20519
   340
  renaming of bound variables; @{text "\<beta>"}-conversion contracts an
wenzelm@20537
   341
  abstraction applied to an argument term, substituting the argument
wenzelm@20519
   342
  in the body: @{text "(\<lambda>x. b)a"} becomes @{text "b[a/x]"}; @{text
wenzelm@20519
   343
  "\<eta>"}-conversion contracts vacuous application-abstraction: @{text
wenzelm@20519
   344
  "\<lambda>x. f x"} becomes @{text "f"}, provided that the bound variable
wenzelm@20537
   345
  does not occur in @{text "f"}.
wenzelm@20519
   346
wenzelm@20537
   347
  Terms are normally treated modulo @{text "\<alpha>"}-conversion, which is
wenzelm@20537
   348
  implicit in the de-Bruijn representation.  Names for bound variables
wenzelm@20537
   349
  in abstractions are maintained separately as (meaningless) comments,
wenzelm@20537
   350
  mostly for parsing and printing.  Full @{text "\<alpha>\<beta>\<eta>"}-conversion is
wenzelm@28784
   351
  commonplace in various standard operations (\secref{sec:obj-rules})
wenzelm@28784
   352
  that are based on higher-order unification and matching.
wenzelm@18537
   353
*}
wenzelm@18537
   354
wenzelm@20514
   355
text %mlref {*
wenzelm@20514
   356
  \begin{mldecls}
wenzelm@20514
   357
  @{index_ML_type term} \\
wenzelm@46262
   358
  @{index_ML_op "aconv": "term * term -> bool"} \\
wenzelm@39846
   359
  @{index_ML Term.map_types: "(typ -> typ) -> term -> term"} \\
wenzelm@39846
   360
  @{index_ML Term.fold_types: "(typ -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@39846
   361
  @{index_ML Term.map_aterms: "(term -> term) -> term -> term"} \\
wenzelm@39846
   362
  @{index_ML Term.fold_aterms: "(term -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@20547
   363
  \end{mldecls}
wenzelm@20547
   364
  \begin{mldecls}
wenzelm@20514
   365
  @{index_ML fastype_of: "term -> typ"} \\
wenzelm@20519
   366
  @{index_ML lambda: "term -> term -> term"} \\
wenzelm@20519
   367
  @{index_ML betapply: "term * term -> term"} \\
wenzelm@42934
   368
  @{index_ML incr_boundvars: "int -> term -> term"} \\
wenzelm@42401
   369
  @{index_ML Sign.declare_const: "Proof.context ->
wenzelm@42401
   370
  (binding * typ) * mixfix -> theory -> term * theory"} \\
haftmann@33174
   371
  @{index_ML Sign.add_abbrev: "string -> binding * term ->
wenzelm@24972
   372
  theory -> (term * term) * theory"} \\
wenzelm@20519
   373
  @{index_ML Sign.const_typargs: "theory -> string * typ -> typ list"} \\
wenzelm@20519
   374
  @{index_ML Sign.const_instance: "theory -> string * typ list -> typ"} \\
wenzelm@20514
   375
  \end{mldecls}
wenzelm@18537
   376
wenzelm@20514
   377
  \begin{description}
wenzelm@18537
   378
wenzelm@39864
   379
  \item Type @{ML_type term} represents de-Bruijn terms, with comments
wenzelm@39864
   380
  in abstractions, and explicitly named free variables and constants;
wenzelm@52408
   381
  this is a datatype with constructors @{index_ML Bound}, @{index_ML
wenzelm@52408
   382
  Free}, @{index_ML Var}, @{index_ML Const}, @{index_ML Abs},
wenzelm@52408
   383
  @{index_ML_op "$"}.
wenzelm@20519
   384
wenzelm@36166
   385
  \item @{text "t"}~@{ML_text aconv}~@{text "u"} checks @{text
wenzelm@20519
   386
  "\<alpha>"}-equivalence of two terms.  This is the basic equality relation
wenzelm@20519
   387
  on type @{ML_type term}; raw datatype equality should only be used
wenzelm@20519
   388
  for operations related to parsing or printing!
wenzelm@20519
   389
wenzelm@39846
   390
  \item @{ML Term.map_types}~@{text "f t"} applies the mapping @{text
wenzelm@20537
   391
  "f"} to all types occurring in @{text "t"}.
wenzelm@20537
   392
wenzelm@39846
   393
  \item @{ML Term.fold_types}~@{text "f t"} iterates the operation
wenzelm@39846
   394
  @{text "f"} over all occurrences of types in @{text "t"}; the term
wenzelm@20537
   395
  structure is traversed from left to right.
wenzelm@20519
   396
wenzelm@39846
   397
  \item @{ML Term.map_aterms}~@{text "f t"} applies the mapping @{text
wenzelm@39846
   398
  "f"} to all atomic terms (@{ML Bound}, @{ML Free}, @{ML Var}, @{ML
wenzelm@20537
   399
  Const}) occurring in @{text "t"}.
wenzelm@20537
   400
wenzelm@39846
   401
  \item @{ML Term.fold_aterms}~@{text "f t"} iterates the operation
wenzelm@39846
   402
  @{text "f"} over all occurrences of atomic terms (@{ML Bound}, @{ML
wenzelm@39846
   403
  Free}, @{ML Var}, @{ML Const}) in @{text "t"}; the term structure is
wenzelm@20519
   404
  traversed from left to right.
wenzelm@20519
   405
wenzelm@20537
   406
  \item @{ML fastype_of}~@{text "t"} determines the type of a
wenzelm@20537
   407
  well-typed term.  This operation is relatively slow, despite the
wenzelm@20537
   408
  omission of any sanity checks.
wenzelm@20519
   409
wenzelm@20519
   410
  \item @{ML lambda}~@{text "a b"} produces an abstraction @{text
wenzelm@20537
   411
  "\<lambda>a. b"}, where occurrences of the atomic term @{text "a"} in the
wenzelm@20537
   412
  body @{text "b"} are replaced by bound variables.
wenzelm@20519
   413
wenzelm@20537
   414
  \item @{ML betapply}~@{text "(t, u)"} produces an application @{text
wenzelm@20537
   415
  "t u"}, with topmost @{text "\<beta>"}-conversion if @{text "t"} is an
wenzelm@20537
   416
  abstraction.
wenzelm@20519
   417
wenzelm@42934
   418
  \item @{ML incr_boundvars}~@{text "j"} increments a term's dangling
wenzelm@42934
   419
  bound variables by the offset @{text "j"}.  This is required when
wenzelm@42934
   420
  moving a subterm into a context where it is enclosed by a different
wenzelm@42934
   421
  number of abstractions.  Bound variables with a matching abstraction
wenzelm@42934
   422
  are unaffected.
wenzelm@42934
   423
wenzelm@42401
   424
  \item @{ML Sign.declare_const}~@{text "ctxt ((c, \<sigma>), mx)"} declares
wenzelm@42401
   425
  a new constant @{text "c :: \<sigma>"} with optional mixfix syntax.
wenzelm@20519
   426
haftmann@33174
   427
  \item @{ML Sign.add_abbrev}~@{text "print_mode (c, t)"}
wenzelm@21827
   428
  introduces a new term abbreviation @{text "c \<equiv> t"}.
wenzelm@20519
   429
wenzelm@20520
   430
  \item @{ML Sign.const_typargs}~@{text "thy (c, \<tau>)"} and @{ML
wenzelm@20520
   431
  Sign.const_instance}~@{text "thy (c, [\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n])"}
wenzelm@20543
   432
  convert between two representations of polymorphic constants: full
wenzelm@20543
   433
  type instance vs.\ compact type arguments form.
wenzelm@18537
   434
wenzelm@20514
   435
  \end{description}
wenzelm@18537
   436
*}
wenzelm@18537
   437
wenzelm@39832
   438
text %mlantiq {*
wenzelm@39832
   439
  \begin{matharray}{rcl}
wenzelm@39832
   440
  @{ML_antiquotation_def "const_name"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   441
  @{ML_antiquotation_def "const_abbrev"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   442
  @{ML_antiquotation_def "const"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   443
  @{ML_antiquotation_def "term"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   444
  @{ML_antiquotation_def "prop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   445
  \end{matharray}
wenzelm@39832
   446
wenzelm@42510
   447
  @{rail "
wenzelm@42510
   448
  (@@{ML_antiquotation const_name} |
wenzelm@42510
   449
   @@{ML_antiquotation const_abbrev}) nameref
wenzelm@39832
   450
  ;
wenzelm@42510
   451
  @@{ML_antiquotation const} ('(' (type + ',') ')')?
wenzelm@39832
   452
  ;
wenzelm@42510
   453
  @@{ML_antiquotation term} term
wenzelm@39832
   454
  ;
wenzelm@42510
   455
  @@{ML_antiquotation prop} prop
wenzelm@42510
   456
  "}
wenzelm@39832
   457
wenzelm@39832
   458
  \begin{description}
wenzelm@39832
   459
wenzelm@39832
   460
  \item @{text "@{const_name c}"} inlines the internalized logical
wenzelm@39832
   461
  constant name @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   462
wenzelm@39832
   463
  \item @{text "@{const_abbrev c}"} inlines the internalized
wenzelm@39832
   464
  abbreviated constant name @{text "c"} --- as @{ML_type string}
wenzelm@39832
   465
  literal.
wenzelm@39832
   466
wenzelm@39832
   467
  \item @{text "@{const c(\<^vec>\<tau>)}"} inlines the internalized
wenzelm@39832
   468
  constant @{text "c"} with precise type instantiation in the sense of
wenzelm@39832
   469
  @{ML Sign.const_instance} --- as @{ML Const} constructor term for
wenzelm@39832
   470
  datatype @{ML_type term}.
wenzelm@39832
   471
wenzelm@39832
   472
  \item @{text "@{term t}"} inlines the internalized term @{text "t"}
wenzelm@39832
   473
  --- as constructor term for datatype @{ML_type term}.
wenzelm@39832
   474
wenzelm@39832
   475
  \item @{text "@{prop \<phi>}"} inlines the internalized proposition
wenzelm@39832
   476
  @{text "\<phi>"} --- as constructor term for datatype @{ML_type term}.
wenzelm@39832
   477
wenzelm@39832
   478
  \end{description}
wenzelm@39832
   479
*}
wenzelm@39832
   480
wenzelm@18537
   481
wenzelm@20451
   482
section {* Theorems \label{sec:thms} *}
wenzelm@18537
   483
wenzelm@18537
   484
text {*
wenzelm@20543
   485
  A \emph{proposition} is a well-typed term of type @{text "prop"}, a
wenzelm@20521
   486
  \emph{theorem} is a proven proposition (depending on a context of
wenzelm@20521
   487
  hypotheses and the background theory).  Primitive inferences include
wenzelm@29774
   488
  plain Natural Deduction rules for the primary connectives @{text
wenzelm@20537
   489
  "\<And>"} and @{text "\<Longrightarrow>"} of the framework.  There is also a builtin
wenzelm@20537
   490
  notion of equality/equivalence @{text "\<equiv>"}.
wenzelm@20521
   491
*}
wenzelm@20521
   492
wenzelm@29758
   493
wenzelm@26872
   494
subsection {* Primitive connectives and rules \label{sec:prim-rules} *}
wenzelm@18537
   495
wenzelm@20521
   496
text {*
wenzelm@20543
   497
  The theory @{text "Pure"} contains constant declarations for the
wenzelm@20543
   498
  primitive connectives @{text "\<And>"}, @{text "\<Longrightarrow>"}, and @{text "\<equiv>"} of
wenzelm@20543
   499
  the logical framework, see \figref{fig:pure-connectives}.  The
wenzelm@20543
   500
  derivability judgment @{text "A\<^isub>1, \<dots>, A\<^isub>n \<turnstile> B"} is
wenzelm@20543
   501
  defined inductively by the primitive inferences given in
wenzelm@20543
   502
  \figref{fig:prim-rules}, with the global restriction that the
wenzelm@20543
   503
  hypotheses must \emph{not} contain any schematic variables.  The
wenzelm@20543
   504
  builtin equality is conceptually axiomatized as shown in
wenzelm@20521
   505
  \figref{fig:pure-equality}, although the implementation works
wenzelm@20543
   506
  directly with derived inferences.
wenzelm@20521
   507
wenzelm@20521
   508
  \begin{figure}[htb]
wenzelm@20521
   509
  \begin{center}
wenzelm@20501
   510
  \begin{tabular}{ll}
wenzelm@20501
   511
  @{text "all :: (\<alpha> \<Rightarrow> prop) \<Rightarrow> prop"} & universal quantification (binder @{text "\<And>"}) \\
wenzelm@20501
   512
  @{text "\<Longrightarrow> :: prop \<Rightarrow> prop \<Rightarrow> prop"} & implication (right associative infix) \\
wenzelm@20521
   513
  @{text "\<equiv> :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> prop"} & equality relation (infix) \\
wenzelm@20501
   514
  \end{tabular}
wenzelm@20537
   515
  \caption{Primitive connectives of Pure}\label{fig:pure-connectives}
wenzelm@20521
   516
  \end{center}
wenzelm@20521
   517
  \end{figure}
wenzelm@18537
   518
wenzelm@20501
   519
  \begin{figure}[htb]
wenzelm@20501
   520
  \begin{center}
wenzelm@20498
   521
  \[
wenzelm@20498
   522
  \infer[@{text "(axiom)"}]{@{text "\<turnstile> A"}}{@{text "A \<in> \<Theta>"}}
wenzelm@20498
   523
  \qquad
wenzelm@20498
   524
  \infer[@{text "(assume)"}]{@{text "A \<turnstile> A"}}{}
wenzelm@20498
   525
  \]
wenzelm@20498
   526
  \[
wenzelm@52407
   527
  \infer[@{text "(\<And>\<hyphen>intro)"}]{@{text "\<Gamma> \<turnstile> \<And>x. B[x]"}}{@{text "\<Gamma> \<turnstile> B[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   528
  \qquad
wenzelm@52407
   529
  \infer[@{text "(\<And>\<hyphen>elim)"}]{@{text "\<Gamma> \<turnstile> B[a]"}}{@{text "\<Gamma> \<turnstile> \<And>x. B[x]"}}
wenzelm@20498
   530
  \]
wenzelm@20498
   531
  \[
wenzelm@42666
   532
  \infer[@{text "(\<Longrightarrow>\<hyphen>intro)"}]{@{text "\<Gamma> - A \<turnstile> A \<Longrightarrow> B"}}{@{text "\<Gamma> \<turnstile> B"}}
wenzelm@20498
   533
  \qquad
wenzelm@42666
   534
  \infer[@{text "(\<Longrightarrow>\<hyphen>elim)"}]{@{text "\<Gamma>\<^sub>1 \<union> \<Gamma>\<^sub>2 \<turnstile> B"}}{@{text "\<Gamma>\<^sub>1 \<turnstile> A \<Longrightarrow> B"} & @{text "\<Gamma>\<^sub>2 \<turnstile> A"}}
wenzelm@20498
   535
  \]
wenzelm@20521
   536
  \caption{Primitive inferences of Pure}\label{fig:prim-rules}
wenzelm@20521
   537
  \end{center}
wenzelm@20521
   538
  \end{figure}
wenzelm@20521
   539
wenzelm@20521
   540
  \begin{figure}[htb]
wenzelm@20521
   541
  \begin{center}
wenzelm@20521
   542
  \begin{tabular}{ll}
wenzelm@20537
   543
  @{text "\<turnstile> (\<lambda>x. b[x]) a \<equiv> b[a]"} & @{text "\<beta>"}-conversion \\
wenzelm@20521
   544
  @{text "\<turnstile> x \<equiv> x"} & reflexivity \\
wenzelm@20521
   545
  @{text "\<turnstile> x \<equiv> y \<Longrightarrow> P x \<Longrightarrow> P y"} & substitution \\
wenzelm@20521
   546
  @{text "\<turnstile> (\<And>x. f x \<equiv> g x) \<Longrightarrow> f \<equiv> g"} & extensionality \\
wenzelm@20537
   547
  @{text "\<turnstile> (A \<Longrightarrow> B) \<Longrightarrow> (B \<Longrightarrow> A) \<Longrightarrow> A \<equiv> B"} & logical equivalence \\
wenzelm@20521
   548
  \end{tabular}
wenzelm@20542
   549
  \caption{Conceptual axiomatization of Pure equality}\label{fig:pure-equality}
wenzelm@20501
   550
  \end{center}
wenzelm@20501
   551
  \end{figure}
wenzelm@18537
   552
wenzelm@20501
   553
  The introduction and elimination rules for @{text "\<And>"} and @{text
wenzelm@20537
   554
  "\<Longrightarrow>"} are analogous to formation of dependently typed @{text
wenzelm@20501
   555
  "\<lambda>"}-terms representing the underlying proof objects.  Proof terms
wenzelm@20543
   556
  are irrelevant in the Pure logic, though; they cannot occur within
wenzelm@20543
   557
  propositions.  The system provides a runtime option to record
wenzelm@52408
   558
  explicit proof terms for primitive inferences, see also
wenzelm@52408
   559
  \secref{sec:proof-terms}.  Thus all three levels of @{text
wenzelm@52408
   560
  "\<lambda>"}-calculus become explicit: @{text "\<Rightarrow>"} for terms, and @{text
wenzelm@52408
   561
  "\<And>/\<Longrightarrow>"} for proofs (cf.\ \cite{Berghofer-Nipkow:2000:TPHOL}).
wenzelm@20491
   562
wenzelm@34929
   563
  Observe that locally fixed parameters (as in @{text
wenzelm@42666
   564
  "\<And>\<hyphen>intro"}) need not be recorded in the hypotheses, because
wenzelm@34929
   565
  the simple syntactic types of Pure are always inhabitable.
wenzelm@34929
   566
  ``Assumptions'' @{text "x :: \<tau>"} for type-membership are only
wenzelm@34929
   567
  present as long as some @{text "x\<^isub>\<tau>"} occurs in the statement
wenzelm@34929
   568
  body.\footnote{This is the key difference to ``@{text "\<lambda>HOL"}'' in
wenzelm@34929
   569
  the PTS framework \cite{Barendregt-Geuvers:2001}, where hypotheses
wenzelm@34929
   570
  @{text "x : A"} are treated uniformly for propositions and types.}
wenzelm@20501
   571
wenzelm@20501
   572
  \medskip The axiomatization of a theory is implicitly closed by
wenzelm@20537
   573
  forming all instances of type and term variables: @{text "\<turnstile>
wenzelm@20537
   574
  A\<vartheta>"} holds for any substitution instance of an axiom
wenzelm@20543
   575
  @{text "\<turnstile> A"}.  By pushing substitutions through derivations
wenzelm@20543
   576
  inductively, we also get admissible @{text "generalize"} and @{text
wenzelm@34929
   577
  "instantiate"} rules as shown in \figref{fig:subst-rules}.
wenzelm@20501
   578
wenzelm@20501
   579
  \begin{figure}[htb]
wenzelm@20501
   580
  \begin{center}
wenzelm@20498
   581
  \[
wenzelm@20501
   582
  \infer{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}{@{text "\<Gamma> \<turnstile> B[\<alpha>]"} & @{text "\<alpha> \<notin> \<Gamma>"}}
wenzelm@20501
   583
  \quad
wenzelm@20501
   584
  \infer[\quad@{text "(generalize)"}]{@{text "\<Gamma> \<turnstile> B[?x]"}}{@{text "\<Gamma> \<turnstile> B[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   585
  \]
wenzelm@20498
   586
  \[
wenzelm@20501
   587
  \infer{@{text "\<Gamma> \<turnstile> B[\<tau>]"}}{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}
wenzelm@20501
   588
  \quad
wenzelm@20501
   589
  \infer[\quad@{text "(instantiate)"}]{@{text "\<Gamma> \<turnstile> B[t]"}}{@{text "\<Gamma> \<turnstile> B[?x]"}}
wenzelm@20498
   590
  \]
wenzelm@20501
   591
  \caption{Admissible substitution rules}\label{fig:subst-rules}
wenzelm@20501
   592
  \end{center}
wenzelm@20501
   593
  \end{figure}
wenzelm@18537
   594
wenzelm@20537
   595
  Note that @{text "instantiate"} does not require an explicit
wenzelm@20537
   596
  side-condition, because @{text "\<Gamma>"} may never contain schematic
wenzelm@20537
   597
  variables.
wenzelm@20537
   598
wenzelm@20537
   599
  In principle, variables could be substituted in hypotheses as well,
wenzelm@20543
   600
  but this would disrupt the monotonicity of reasoning: deriving
wenzelm@20543
   601
  @{text "\<Gamma>\<vartheta> \<turnstile> B\<vartheta>"} from @{text "\<Gamma> \<turnstile> B"} is
wenzelm@20543
   602
  correct, but @{text "\<Gamma>\<vartheta> \<supseteq> \<Gamma>"} does not necessarily hold:
wenzelm@20543
   603
  the result belongs to a different proof context.
wenzelm@20542
   604
wenzelm@20543
   605
  \medskip An \emph{oracle} is a function that produces axioms on the
wenzelm@20543
   606
  fly.  Logically, this is an instance of the @{text "axiom"} rule
wenzelm@20543
   607
  (\figref{fig:prim-rules}), but there is an operational difference.
wenzelm@20543
   608
  The system always records oracle invocations within derivations of
wenzelm@29768
   609
  theorems by a unique tag.
wenzelm@20542
   610
wenzelm@20542
   611
  Axiomatizations should be limited to the bare minimum, typically as
wenzelm@20542
   612
  part of the initial logical basis of an object-logic formalization.
wenzelm@20543
   613
  Later on, theories are usually developed in a strictly definitional
wenzelm@20543
   614
  fashion, by stating only certain equalities over new constants.
wenzelm@20542
   615
wenzelm@20542
   616
  A \emph{simple definition} consists of a constant declaration @{text
wenzelm@20543
   617
  "c :: \<sigma>"} together with an axiom @{text "\<turnstile> c \<equiv> t"}, where @{text "t
wenzelm@20543
   618
  :: \<sigma>"} is a closed term without any hidden polymorphism.  The RHS
wenzelm@20543
   619
  may depend on further defined constants, but not @{text "c"} itself.
wenzelm@20543
   620
  Definitions of functions may be presented as @{text "c \<^vec>x \<equiv>
wenzelm@20543
   621
  t"} instead of the puristic @{text "c \<equiv> \<lambda>\<^vec>x. t"}.
wenzelm@20542
   622
wenzelm@20543
   623
  An \emph{overloaded definition} consists of a collection of axioms
wenzelm@20543
   624
  for the same constant, with zero or one equations @{text
wenzelm@20543
   625
  "c((\<^vec>\<alpha>)\<kappa>) \<equiv> t"} for each type constructor @{text "\<kappa>"} (for
wenzelm@20543
   626
  distinct variables @{text "\<^vec>\<alpha>"}).  The RHS may mention
wenzelm@20543
   627
  previously defined constants as above, or arbitrary constants @{text
wenzelm@20543
   628
  "d(\<alpha>\<^isub>i)"} for some @{text "\<alpha>\<^isub>i"} projected from @{text
wenzelm@20543
   629
  "\<^vec>\<alpha>"}.  Thus overloaded definitions essentially work by
wenzelm@20543
   630
  primitive recursion over the syntactic structure of a single type
wenzelm@39840
   631
  argument.  See also \cite[\S4.3]{Haftmann-Wenzel:2006:classes}.
wenzelm@20521
   632
*}
wenzelm@20498
   633
wenzelm@20521
   634
text %mlref {*
wenzelm@20521
   635
  \begin{mldecls}
wenzelm@46253
   636
  @{index_ML Logic.all: "term -> term -> term"} \\
wenzelm@46253
   637
  @{index_ML Logic.mk_implies: "term * term -> term"} \\
wenzelm@46253
   638
  \end{mldecls}
wenzelm@46253
   639
  \begin{mldecls}
wenzelm@20521
   640
  @{index_ML_type ctyp} \\
wenzelm@20521
   641
  @{index_ML_type cterm} \\
wenzelm@20547
   642
  @{index_ML Thm.ctyp_of: "theory -> typ -> ctyp"} \\
wenzelm@20547
   643
  @{index_ML Thm.cterm_of: "theory -> term -> cterm"} \\
wenzelm@46497
   644
  @{index_ML Thm.apply: "cterm -> cterm -> cterm"} \\
wenzelm@46497
   645
  @{index_ML Thm.lambda: "cterm -> cterm -> cterm"} \\
wenzelm@46253
   646
  @{index_ML Thm.all: "cterm -> cterm -> cterm"} \\
wenzelm@46253
   647
  @{index_ML Drule.mk_implies: "cterm * cterm -> cterm"} \\
wenzelm@20547
   648
  \end{mldecls}
wenzelm@20547
   649
  \begin{mldecls}
wenzelm@20521
   650
  @{index_ML_type thm} \\
wenzelm@50126
   651
  @{index_ML Thm.peek_status: "thm -> {oracle: bool, unfinished: bool, failed: bool}"} \\
wenzelm@42933
   652
  @{index_ML Thm.transfer: "theory -> thm -> thm"} \\
wenzelm@20542
   653
  @{index_ML Thm.assume: "cterm -> thm"} \\
wenzelm@20542
   654
  @{index_ML Thm.forall_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   655
  @{index_ML Thm.forall_elim: "cterm -> thm -> thm"} \\
wenzelm@20542
   656
  @{index_ML Thm.implies_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   657
  @{index_ML Thm.implies_elim: "thm -> thm -> thm"} \\
wenzelm@20542
   658
  @{index_ML Thm.generalize: "string list * string list -> int -> thm -> thm"} \\
wenzelm@20542
   659
  @{index_ML Thm.instantiate: "(ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm"} \\
wenzelm@42401
   660
  @{index_ML Thm.add_axiom: "Proof.context ->
wenzelm@42401
   661
  binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@39821
   662
  @{index_ML Thm.add_oracle: "binding * ('a -> cterm) -> theory ->
wenzelm@39821
   663
  (string * ('a -> thm)) * theory"} \\
wenzelm@42401
   664
  @{index_ML Thm.add_def: "Proof.context -> bool -> bool ->
wenzelm@42401
   665
  binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@20547
   666
  \end{mldecls}
wenzelm@20547
   667
  \begin{mldecls}
wenzelm@42401
   668
  @{index_ML Theory.add_deps: "Proof.context -> string ->
wenzelm@42401
   669
  string * typ -> (string * typ) list -> theory -> theory"} \\
wenzelm@20521
   670
  \end{mldecls}
wenzelm@20521
   671
wenzelm@20521
   672
  \begin{description}
wenzelm@20521
   673
wenzelm@50126
   674
  \item @{ML Thm.peek_status}~@{text "thm"} informs about the current
wenzelm@50126
   675
  status of the derivation object behind the given theorem.  This is a
wenzelm@50126
   676
  snapshot of a potentially ongoing (parallel) evaluation of proofs.
wenzelm@50126
   677
  The three Boolean values indicate the following: @{verbatim oracle}
wenzelm@50126
   678
  if the finished part contains some oracle invocation; @{verbatim
wenzelm@50126
   679
  unfinished} if some future proofs are still pending; @{verbatim
wenzelm@50126
   680
  failed} if some future proof has failed, rendering the theorem
wenzelm@50126
   681
  invalid!
wenzelm@50126
   682
wenzelm@46253
   683
  \item @{ML Logic.all}~@{text "a B"} produces a Pure quantification
wenzelm@46253
   684
  @{text "\<And>a. B"}, where occurrences of the atomic term @{text "a"} in
wenzelm@46253
   685
  the body proposition @{text "B"} are replaced by bound variables.
wenzelm@46253
   686
  (See also @{ML lambda} on terms.)
wenzelm@46253
   687
wenzelm@46253
   688
  \item @{ML Logic.mk_implies}~@{text "(A, B)"} produces a Pure
wenzelm@46253
   689
  implication @{text "A \<Longrightarrow> B"}.
wenzelm@46253
   690
wenzelm@39864
   691
  \item Types @{ML_type ctyp} and @{ML_type cterm} represent certified
wenzelm@39864
   692
  types and terms, respectively.  These are abstract datatypes that
wenzelm@20542
   693
  guarantee that its values have passed the full well-formedness (and
wenzelm@20542
   694
  well-typedness) checks, relative to the declarations of type
wenzelm@46253
   695
  constructors, constants etc.\ in the background theory.  The
wenzelm@46253
   696
  abstract types @{ML_type ctyp} and @{ML_type cterm} are part of the
wenzelm@46253
   697
  same inference kernel that is mainly responsible for @{ML_type thm}.
wenzelm@46253
   698
  Thus syntactic operations on @{ML_type ctyp} and @{ML_type cterm}
wenzelm@46253
   699
  are located in the @{ML_struct Thm} module, even though theorems are
wenzelm@46253
   700
  not yet involved at that stage.
wenzelm@20542
   701
wenzelm@29768
   702
  \item @{ML Thm.ctyp_of}~@{text "thy \<tau>"} and @{ML
wenzelm@29768
   703
  Thm.cterm_of}~@{text "thy t"} explicitly checks types and terms,
wenzelm@29768
   704
  respectively.  This also involves some basic normalizations, such
wenzelm@29768
   705
  expansion of type and term abbreviations from the theory context.
wenzelm@46253
   706
  Full re-certification is relatively slow and should be avoided in
wenzelm@46253
   707
  tight reasoning loops.
wenzelm@20547
   708
wenzelm@46497
   709
  \item @{ML Thm.apply}, @{ML Thm.lambda}, @{ML Thm.all}, @{ML
wenzelm@46253
   710
  Drule.mk_implies} etc.\ compose certified terms (or propositions)
wenzelm@46253
   711
  incrementally.  This is equivalent to @{ML Thm.cterm_of} after
wenzelm@46262
   712
  unchecked @{ML_op "$"}, @{ML lambda}, @{ML Logic.all}, @{ML
wenzelm@46253
   713
  Logic.mk_implies} etc., but there can be a big difference in
wenzelm@46253
   714
  performance when large existing entities are composed by a few extra
wenzelm@46253
   715
  constructions on top.  There are separate operations to decompose
wenzelm@46253
   716
  certified terms and theorems to produce certified terms again.
wenzelm@20542
   717
wenzelm@39864
   718
  \item Type @{ML_type thm} represents proven propositions.  This is
wenzelm@39864
   719
  an abstract datatype that guarantees that its values have been
wenzelm@20542
   720
  constructed by basic principles of the @{ML_struct Thm} module.
wenzelm@39281
   721
  Every @{ML_type thm} value contains a sliding back-reference to the
wenzelm@20543
   722
  enclosing theory, cf.\ \secref{sec:context-theory}.
wenzelm@20542
   723
wenzelm@42933
   724
  \item @{ML Thm.transfer}~@{text "thy thm"} transfers the given
wenzelm@42933
   725
  theorem to a \emph{larger} theory, see also \secref{sec:context}.
wenzelm@42933
   726
  This formal adjustment of the background context has no logical
wenzelm@42933
   727
  significance, but is occasionally required for formal reasons, e.g.\
wenzelm@42933
   728
  when theorems that are imported from more basic theories are used in
wenzelm@42933
   729
  the current situation.
wenzelm@42933
   730
wenzelm@20542
   731
  \item @{ML Thm.assume}, @{ML Thm.forall_intr}, @{ML
wenzelm@20542
   732
  Thm.forall_elim}, @{ML Thm.implies_intr}, and @{ML Thm.implies_elim}
wenzelm@20542
   733
  correspond to the primitive inferences of \figref{fig:prim-rules}.
wenzelm@20542
   734
wenzelm@20542
   735
  \item @{ML Thm.generalize}~@{text "(\<^vec>\<alpha>, \<^vec>x)"}
wenzelm@20542
   736
  corresponds to the @{text "generalize"} rules of
wenzelm@20543
   737
  \figref{fig:subst-rules}.  Here collections of type and term
wenzelm@20543
   738
  variables are generalized simultaneously, specified by the given
wenzelm@20543
   739
  basic names.
wenzelm@20521
   740
wenzelm@20542
   741
  \item @{ML Thm.instantiate}~@{text "(\<^vec>\<alpha>\<^isub>s,
wenzelm@20542
   742
  \<^vec>x\<^isub>\<tau>)"} corresponds to the @{text "instantiate"} rules
wenzelm@20542
   743
  of \figref{fig:subst-rules}.  Type variables are substituted before
wenzelm@20542
   744
  term variables.  Note that the types in @{text "\<^vec>x\<^isub>\<tau>"}
wenzelm@20542
   745
  refer to the instantiated versions.
wenzelm@20542
   746
wenzelm@42401
   747
  \item @{ML Thm.add_axiom}~@{text "ctxt (name, A)"} declares an
wenzelm@35927
   748
  arbitrary proposition as axiom, and retrieves it as a theorem from
wenzelm@35927
   749
  the resulting theory, cf.\ @{text "axiom"} in
wenzelm@35927
   750
  \figref{fig:prim-rules}.  Note that the low-level representation in
wenzelm@35927
   751
  the axiom table may differ slightly from the returned theorem.
wenzelm@20542
   752
wenzelm@30288
   753
  \item @{ML Thm.add_oracle}~@{text "(binding, oracle)"} produces a named
wenzelm@28290
   754
  oracle rule, essentially generating arbitrary axioms on the fly,
wenzelm@28290
   755
  cf.\ @{text "axiom"} in \figref{fig:prim-rules}.
wenzelm@20521
   756
wenzelm@42401
   757
  \item @{ML Thm.add_def}~@{text "ctxt unchecked overloaded (name, c
wenzelm@35927
   758
  \<^vec>x \<equiv> t)"} states a definitional axiom for an existing constant
wenzelm@35927
   759
  @{text "c"}.  Dependencies are recorded via @{ML Theory.add_deps},
wenzelm@35927
   760
  unless the @{text "unchecked"} option is set.  Note that the
wenzelm@35927
   761
  low-level representation in the axiom table may differ slightly from
wenzelm@35927
   762
  the returned theorem.
wenzelm@20542
   763
wenzelm@42401
   764
  \item @{ML Theory.add_deps}~@{text "ctxt name c\<^isub>\<tau> \<^vec>d\<^isub>\<sigma>"}
wenzelm@42401
   765
  declares dependencies of a named specification for constant @{text
wenzelm@42401
   766
  "c\<^isub>\<tau>"}, relative to existing specifications for constants @{text
wenzelm@42401
   767
  "\<^vec>d\<^isub>\<sigma>"}.
wenzelm@20542
   768
wenzelm@20521
   769
  \end{description}
wenzelm@20521
   770
*}
wenzelm@20521
   771
wenzelm@20521
   772
wenzelm@39832
   773
text %mlantiq {*
wenzelm@39832
   774
  \begin{matharray}{rcl}
wenzelm@39832
   775
  @{ML_antiquotation_def "ctyp"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   776
  @{ML_antiquotation_def "cterm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   777
  @{ML_antiquotation_def "cprop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   778
  @{ML_antiquotation_def "thm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   779
  @{ML_antiquotation_def "thms"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   780
  @{ML_antiquotation_def "lemma"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   781
  \end{matharray}
wenzelm@39832
   782
wenzelm@42510
   783
  @{rail "
wenzelm@42510
   784
  @@{ML_antiquotation ctyp} typ
wenzelm@39832
   785
  ;
wenzelm@42510
   786
  @@{ML_antiquotation cterm} term
wenzelm@39832
   787
  ;
wenzelm@42510
   788
  @@{ML_antiquotation cprop} prop
wenzelm@39832
   789
  ;
wenzelm@42510
   790
  @@{ML_antiquotation thm} thmref
wenzelm@42510
   791
  ;
wenzelm@42510
   792
  @@{ML_antiquotation thms} thmrefs
wenzelm@39832
   793
  ;
wenzelm@42517
   794
  @@{ML_antiquotation lemma} ('(' @'open' ')')? ((prop +) + @'and') \\
wenzelm@42517
   795
    @'by' method method?
wenzelm@42510
   796
  "}
wenzelm@39832
   797
wenzelm@39832
   798
  \begin{description}
wenzelm@39832
   799
wenzelm@39832
   800
  \item @{text "@{ctyp \<tau>}"} produces a certified type wrt.\ the
wenzelm@39832
   801
  current background theory --- as abstract value of type @{ML_type
wenzelm@39832
   802
  ctyp}.
wenzelm@39832
   803
wenzelm@39832
   804
  \item @{text "@{cterm t}"} and @{text "@{cprop \<phi>}"} produce a
wenzelm@39832
   805
  certified term wrt.\ the current background theory --- as abstract
wenzelm@39832
   806
  value of type @{ML_type cterm}.
wenzelm@39832
   807
wenzelm@39832
   808
  \item @{text "@{thm a}"} produces a singleton fact --- as abstract
wenzelm@39832
   809
  value of type @{ML_type thm}.
wenzelm@39832
   810
wenzelm@39832
   811
  \item @{text "@{thms a}"} produces a general fact --- as abstract
wenzelm@39832
   812
  value of type @{ML_type "thm list"}.
wenzelm@39832
   813
wenzelm@39832
   814
  \item @{text "@{lemma \<phi> by meth}"} produces a fact that is proven on
wenzelm@39832
   815
  the spot according to the minimal proof, which imitates a terminal
wenzelm@39832
   816
  Isar proof.  The result is an abstract value of type @{ML_type thm}
wenzelm@39832
   817
  or @{ML_type "thm list"}, depending on the number of propositions
wenzelm@39832
   818
  given here.
wenzelm@39832
   819
wenzelm@39832
   820
  The internal derivation object lacks a proper theorem name, but it
wenzelm@39832
   821
  is formally closed, unless the @{text "(open)"} option is specified
wenzelm@39832
   822
  (this may impact performance of applications with proof terms).
wenzelm@39832
   823
wenzelm@39832
   824
  Since ML antiquotations are always evaluated at compile-time, there
wenzelm@39832
   825
  is no run-time overhead even for non-trivial proofs.  Nonetheless,
wenzelm@39832
   826
  the justification is syntactically limited to a single @{command
wenzelm@39832
   827
  "by"} step.  More complex Isar proofs should be done in regular
wenzelm@39832
   828
  theory source, before compiling the corresponding ML text that uses
wenzelm@39832
   829
  the result.
wenzelm@39832
   830
wenzelm@39832
   831
  \end{description}
wenzelm@39832
   832
wenzelm@39832
   833
*}
wenzelm@39832
   834
wenzelm@39832
   835
wenzelm@46254
   836
subsection {* Auxiliary connectives \label{sec:logic-aux} *}
wenzelm@20521
   837
wenzelm@46254
   838
text {* Theory @{text "Pure"} provides a few auxiliary connectives
wenzelm@46254
   839
  that are defined on top of the primitive ones, see
wenzelm@46254
   840
  \figref{fig:pure-aux}.  These special constants are useful in
wenzelm@46254
   841
  certain internal encodings, and are normally not directly exposed to
wenzelm@46254
   842
  the user.
wenzelm@20501
   843
wenzelm@20501
   844
  \begin{figure}[htb]
wenzelm@20501
   845
  \begin{center}
wenzelm@20498
   846
  \begin{tabular}{ll}
wenzelm@34929
   847
  @{text "conjunction :: prop \<Rightarrow> prop \<Rightarrow> prop"} & (infix @{text "&&&"}) \\
wenzelm@34929
   848
  @{text "\<turnstile> A &&& B \<equiv> (\<And>C. (A \<Longrightarrow> B \<Longrightarrow> C) \<Longrightarrow> C)"} \\[1ex]
wenzelm@20543
   849
  @{text "prop :: prop \<Rightarrow> prop"} & (prefix @{text "#"}, suppressed) \\
wenzelm@20521
   850
  @{text "#A \<equiv> A"} \\[1ex]
wenzelm@20521
   851
  @{text "term :: \<alpha> \<Rightarrow> prop"} & (prefix @{text "TERM"}) \\
wenzelm@20521
   852
  @{text "term x \<equiv> (\<And>A. A \<Longrightarrow> A)"} \\[1ex]
wenzelm@20521
   853
  @{text "TYPE :: \<alpha> itself"} & (prefix @{text "TYPE"}) \\
wenzelm@20521
   854
  @{text "(unspecified)"} \\
wenzelm@20498
   855
  \end{tabular}
wenzelm@20521
   856
  \caption{Definitions of auxiliary connectives}\label{fig:pure-aux}
wenzelm@20501
   857
  \end{center}
wenzelm@20501
   858
  \end{figure}
wenzelm@20501
   859
wenzelm@34929
   860
  The introduction @{text "A \<Longrightarrow> B \<Longrightarrow> A &&& B"}, and eliminations
wenzelm@34929
   861
  (projections) @{text "A &&& B \<Longrightarrow> A"} and @{text "A &&& B \<Longrightarrow> B"} are
wenzelm@34929
   862
  available as derived rules.  Conjunction allows to treat
wenzelm@34929
   863
  simultaneous assumptions and conclusions uniformly, e.g.\ consider
wenzelm@34929
   864
  @{text "A \<Longrightarrow> B \<Longrightarrow> C &&& D"}.  In particular, the goal mechanism
wenzelm@34929
   865
  represents multiple claims as explicit conjunction internally, but
wenzelm@34929
   866
  this is refined (via backwards introduction) into separate sub-goals
wenzelm@34929
   867
  before the user commences the proof; the final result is projected
wenzelm@34929
   868
  into a list of theorems using eliminations (cf.\
wenzelm@20537
   869
  \secref{sec:tactical-goals}).
wenzelm@20498
   870
wenzelm@20537
   871
  The @{text "prop"} marker (@{text "#"}) makes arbitrarily complex
wenzelm@20537
   872
  propositions appear as atomic, without changing the meaning: @{text
wenzelm@20537
   873
  "\<Gamma> \<turnstile> A"} and @{text "\<Gamma> \<turnstile> #A"} are interchangeable.  See
wenzelm@20537
   874
  \secref{sec:tactical-goals} for specific operations.
wenzelm@20521
   875
wenzelm@20543
   876
  The @{text "term"} marker turns any well-typed term into a derivable
wenzelm@20543
   877
  proposition: @{text "\<turnstile> TERM t"} holds unconditionally.  Although
wenzelm@20543
   878
  this is logically vacuous, it allows to treat terms and proofs
wenzelm@20543
   879
  uniformly, similar to a type-theoretic framework.
wenzelm@20498
   880
wenzelm@20537
   881
  The @{text "TYPE"} constructor is the canonical representative of
wenzelm@20537
   882
  the unspecified type @{text "\<alpha> itself"}; it essentially injects the
wenzelm@20537
   883
  language of types into that of terms.  There is specific notation
wenzelm@20537
   884
  @{text "TYPE(\<tau>)"} for @{text "TYPE\<^bsub>\<tau>
wenzelm@20521
   885
 itself\<^esub>"}.
wenzelm@34929
   886
  Although being devoid of any particular meaning, the term @{text
wenzelm@20537
   887
  "TYPE(\<tau>)"} accounts for the type @{text "\<tau>"} within the term
wenzelm@20537
   888
  language.  In particular, @{text "TYPE(\<alpha>)"} may be used as formal
wenzelm@20537
   889
  argument in primitive definitions, in order to circumvent hidden
wenzelm@20537
   890
  polymorphism (cf.\ \secref{sec:terms}).  For example, @{text "c
wenzelm@20537
   891
  TYPE(\<alpha>) \<equiv> A[\<alpha>]"} defines @{text "c :: \<alpha> itself \<Rightarrow> prop"} in terms of
wenzelm@20537
   892
  a proposition @{text "A"} that depends on an additional type
wenzelm@20537
   893
  argument, which is essentially a predicate on types.
wenzelm@20521
   894
*}
wenzelm@20501
   895
wenzelm@20521
   896
text %mlref {*
wenzelm@20521
   897
  \begin{mldecls}
wenzelm@20521
   898
  @{index_ML Conjunction.intr: "thm -> thm -> thm"} \\
wenzelm@20521
   899
  @{index_ML Conjunction.elim: "thm -> thm * thm"} \\
wenzelm@20521
   900
  @{index_ML Drule.mk_term: "cterm -> thm"} \\
wenzelm@20521
   901
  @{index_ML Drule.dest_term: "thm -> cterm"} \\
wenzelm@20521
   902
  @{index_ML Logic.mk_type: "typ -> term"} \\
wenzelm@20521
   903
  @{index_ML Logic.dest_type: "term -> typ"} \\
wenzelm@20521
   904
  \end{mldecls}
wenzelm@20521
   905
wenzelm@20521
   906
  \begin{description}
wenzelm@20521
   907
wenzelm@34929
   908
  \item @{ML Conjunction.intr} derives @{text "A &&& B"} from @{text
wenzelm@20542
   909
  "A"} and @{text "B"}.
wenzelm@20542
   910
wenzelm@20543
   911
  \item @{ML Conjunction.elim} derives @{text "A"} and @{text "B"}
wenzelm@34929
   912
  from @{text "A &&& B"}.
wenzelm@20542
   913
wenzelm@20543
   914
  \item @{ML Drule.mk_term} derives @{text "TERM t"}.
wenzelm@20542
   915
wenzelm@20543
   916
  \item @{ML Drule.dest_term} recovers term @{text "t"} from @{text
wenzelm@20543
   917
  "TERM t"}.
wenzelm@20542
   918
wenzelm@20542
   919
  \item @{ML Logic.mk_type}~@{text "\<tau>"} produces the term @{text
wenzelm@20542
   920
  "TYPE(\<tau>)"}.
wenzelm@20542
   921
wenzelm@20542
   922
  \item @{ML Logic.dest_type}~@{text "TYPE(\<tau>)"} recovers the type
wenzelm@20542
   923
  @{text "\<tau>"}.
wenzelm@20521
   924
wenzelm@20521
   925
  \end{description}
wenzelm@20491
   926
*}
wenzelm@18537
   927
wenzelm@20480
   928
wenzelm@52406
   929
subsection {* Sort hypotheses *}
wenzelm@52406
   930
wenzelm@52406
   931
text {* Type variables are decorated with sorts, as explained in
wenzelm@52406
   932
  \secref{sec:types}.  This constrains type instantiation to certain
wenzelm@52406
   933
  ranges of types: variable @{text "\<alpha>\<^sub>s"} may only be assigned to types
wenzelm@52406
   934
  @{text "\<tau>"} that belong to sort @{text "s"}.  Within the logic, sort
wenzelm@52406
   935
  constraints act like implicit preconditions on the result @{text
wenzelm@52406
   936
  "\<lparr>\<alpha>\<^sub>1 : s\<^sub>1\<rparr>, \<dots>, \<lparr>\<alpha>\<^sub>n : s\<^sub>n\<rparr>, \<Gamma> \<turnstile> \<phi>"} where the type variables @{text
wenzelm@52406
   937
  "\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n"} cover the propositions @{text "\<Gamma>"}, @{text "\<phi>"}, as
wenzelm@52406
   938
  well as the proof of @{text "\<Gamma> \<turnstile> \<phi>"}.
wenzelm@52406
   939
wenzelm@52406
   940
  These \emph{sort hypothesis} of a theorem are passed monotonically
wenzelm@52406
   941
  through further derivations.  They are redundant, as long as the
wenzelm@52406
   942
  statement of a theorem still contains the type variables that are
wenzelm@52406
   943
  accounted here.  The logical significance of sort hypotheses is
wenzelm@52406
   944
  limited to the boundary case where type variables disappear from the
wenzelm@52406
   945
  proposition, e.g.\ @{text "\<lparr>\<alpha>\<^sub>s : s\<rparr> \<turnstile> \<phi>"}.  Since such dangling type
wenzelm@52406
   946
  variables can be renamed arbitrarily without changing the
wenzelm@52406
   947
  proposition @{text "\<phi>"}, the inference kernel maintains sort
wenzelm@52406
   948
  hypotheses in anonymous form @{text "s \<turnstile> \<phi>"}.
wenzelm@52406
   949
wenzelm@52406
   950
  In most practical situations, such extra sort hypotheses may be
wenzelm@52406
   951
  stripped in a final bookkeeping step, e.g.\ at the end of a proof:
wenzelm@52406
   952
  they are typically left over from intermediate reasoning with type
wenzelm@52406
   953
  classes that can be satisfied by some concrete type @{text "\<tau>"} of
wenzelm@52406
   954
  sort @{text "s"} to replace the hypothetical type variable @{text
wenzelm@52406
   955
  "\<alpha>\<^sub>s"}.  *}
wenzelm@52406
   956
wenzelm@52406
   957
text %mlref {*
wenzelm@52406
   958
  \begin{mldecls}
wenzelm@52406
   959
  @{index_ML Thm.extra_shyps: "thm -> sort list"} \\
wenzelm@52406
   960
  @{index_ML Thm.strip_shyps: "thm -> thm"} \\
wenzelm@52406
   961
  \end{mldecls}
wenzelm@52406
   962
wenzelm@52406
   963
  \begin{description}
wenzelm@52406
   964
wenzelm@52406
   965
  \item @{ML Thm.extra_shyps}~@{text "thm"} determines the extraneous
wenzelm@52406
   966
  sort hypotheses of the given theorem, i.e.\ the sorts that are not
wenzelm@52406
   967
  present within type variables of the statement.
wenzelm@52406
   968
wenzelm@52406
   969
  \item @{ML Thm.strip_shyps}~@{text "thm"} removes any extraneous
wenzelm@52406
   970
  sort hypotheses that can be witnessed from the type signature.
wenzelm@52406
   971
wenzelm@52406
   972
  \end{description}
wenzelm@52406
   973
*}
wenzelm@52406
   974
wenzelm@52406
   975
text %mlex {* The following artificial example demonstrates the
wenzelm@52406
   976
  derivation of @{prop False} with a pending sort hypothesis involving
wenzelm@52406
   977
  a logically empty sort.  *}
wenzelm@52406
   978
wenzelm@52406
   979
class empty =
wenzelm@52406
   980
  assumes bad: "\<And>(x::'a) y. x \<noteq> y"
wenzelm@52406
   981
wenzelm@52406
   982
theorem (in empty) false: False
wenzelm@52406
   983
  using bad by blast
wenzelm@52406
   984
wenzelm@52406
   985
ML {*
wenzelm@52406
   986
  @{assert} (Thm.extra_shyps @{thm false} = [@{sort empty}])
wenzelm@52406
   987
*}
wenzelm@52406
   988
wenzelm@52406
   989
text {* Thanks to the inference kernel managing sort hypothesis
wenzelm@52406
   990
  according to their logical significance, this example is merely an
wenzelm@52406
   991
  instance of \emph{ex falso quodlibet consequitur} --- not a collapse
wenzelm@52406
   992
  of the logical framework! *}
wenzelm@52406
   993
wenzelm@52406
   994
wenzelm@28784
   995
section {* Object-level rules \label{sec:obj-rules} *}
wenzelm@18537
   996
wenzelm@29768
   997
text {*
wenzelm@29768
   998
  The primitive inferences covered so far mostly serve foundational
wenzelm@29768
   999
  purposes.  User-level reasoning usually works via object-level rules
wenzelm@29768
  1000
  that are represented as theorems of Pure.  Composition of rules
wenzelm@29771
  1001
  involves \emph{backchaining}, \emph{higher-order unification} modulo
wenzelm@29771
  1002
  @{text "\<alpha>\<beta>\<eta>"}-conversion of @{text "\<lambda>"}-terms, and so-called
wenzelm@29771
  1003
  \emph{lifting} of rules into a context of @{text "\<And>"} and @{text
wenzelm@29774
  1004
  "\<Longrightarrow>"} connectives.  Thus the full power of higher-order Natural
wenzelm@29774
  1005
  Deduction in Isabelle/Pure becomes readily available.
wenzelm@29769
  1006
*}
wenzelm@20491
  1007
wenzelm@29769
  1008
wenzelm@29769
  1009
subsection {* Hereditary Harrop Formulae *}
wenzelm@29769
  1010
wenzelm@29769
  1011
text {*
wenzelm@29768
  1012
  The idea of object-level rules is to model Natural Deduction
wenzelm@29768
  1013
  inferences in the style of Gentzen \cite{Gentzen:1935}, but we allow
wenzelm@29768
  1014
  arbitrary nesting similar to \cite{extensions91}.  The most basic
wenzelm@29768
  1015
  rule format is that of a \emph{Horn Clause}:
wenzelm@29768
  1016
  \[
wenzelm@29768
  1017
  \infer{@{text "A"}}{@{text "A\<^sub>1"} & @{text "\<dots>"} & @{text "A\<^sub>n"}}
wenzelm@29768
  1018
  \]
wenzelm@29768
  1019
  where @{text "A, A\<^sub>1, \<dots>, A\<^sub>n"} are atomic propositions
wenzelm@29768
  1020
  of the framework, usually of the form @{text "Trueprop B"}, where
wenzelm@29768
  1021
  @{text "B"} is a (compound) object-level statement.  This
wenzelm@29768
  1022
  object-level inference corresponds to an iterated implication in
wenzelm@29768
  1023
  Pure like this:
wenzelm@29768
  1024
  \[
wenzelm@29768
  1025
  @{text "A\<^sub>1 \<Longrightarrow> \<dots> A\<^sub>n \<Longrightarrow> A"}
wenzelm@29768
  1026
  \]
wenzelm@29769
  1027
  As an example consider conjunction introduction: @{text "A \<Longrightarrow> B \<Longrightarrow> A \<and>
wenzelm@29769
  1028
  B"}.  Any parameters occurring in such rule statements are
wenzelm@29769
  1029
  conceptionally treated as arbitrary:
wenzelm@29768
  1030
  \[
wenzelm@29769
  1031
  @{text "\<And>x\<^sub>1 \<dots> x\<^sub>m. A\<^sub>1 x\<^sub>1 \<dots> x\<^sub>m \<Longrightarrow> \<dots> A\<^sub>n x\<^sub>1 \<dots> x\<^sub>m \<Longrightarrow> A x\<^sub>1 \<dots> x\<^sub>m"}
wenzelm@29768
  1032
  \]
wenzelm@20491
  1033
wenzelm@29769
  1034
  Nesting of rules means that the positions of @{text "A\<^sub>i"} may
wenzelm@29770
  1035
  again hold compound rules, not just atomic propositions.
wenzelm@29769
  1036
  Propositions of this format are called \emph{Hereditary Harrop
wenzelm@29769
  1037
  Formulae} in the literature \cite{Miller:1991}.  Here we give an
wenzelm@29769
  1038
  inductive characterization as follows:
wenzelm@29768
  1039
wenzelm@29768
  1040
  \medskip
wenzelm@29768
  1041
  \begin{tabular}{ll}
wenzelm@29768
  1042
  @{text "\<^bold>x"} & set of variables \\
wenzelm@29768
  1043
  @{text "\<^bold>A"} & set of atomic propositions \\
wenzelm@29768
  1044
  @{text "\<^bold>H  =  \<And>\<^bold>x\<^sup>*. \<^bold>H\<^sup>* \<Longrightarrow> \<^bold>A"} & set of Hereditary Harrop Formulas \\
wenzelm@29768
  1045
  \end{tabular}
wenzelm@29768
  1046
  \medskip
wenzelm@29768
  1047
wenzelm@39861
  1048
  Thus we essentially impose nesting levels on propositions formed
wenzelm@39861
  1049
  from @{text "\<And>"} and @{text "\<Longrightarrow>"}.  At each level there is a prefix
wenzelm@39861
  1050
  of parameters and compound premises, concluding an atomic
wenzelm@29770
  1051
  proposition.  Typical examples are @{text "\<longrightarrow>"}-introduction @{text
wenzelm@29770
  1052
  "(A \<Longrightarrow> B) \<Longrightarrow> A \<longrightarrow> B"} or mathematical induction @{text "P 0 \<Longrightarrow> (\<And>n. P n
wenzelm@29770
  1053
  \<Longrightarrow> P (Suc n)) \<Longrightarrow> P n"}.  Even deeper nesting occurs in well-founded
wenzelm@29770
  1054
  induction @{text "(\<And>x. (\<And>y. y \<prec> x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P x"}, but this
wenzelm@34929
  1055
  already marks the limit of rule complexity that is usually seen in
wenzelm@34929
  1056
  practice.
wenzelm@29769
  1057
wenzelm@29770
  1058
  \medskip Regular user-level inferences in Isabelle/Pure always
wenzelm@29770
  1059
  maintain the following canonical form of results:
wenzelm@29769
  1060
wenzelm@29769
  1061
  \begin{itemize}
wenzelm@29768
  1062
wenzelm@29774
  1063
  \item Normalization by @{text "(A \<Longrightarrow> (\<And>x. B x)) \<equiv> (\<And>x. A \<Longrightarrow> B x)"},
wenzelm@29774
  1064
  which is a theorem of Pure, means that quantifiers are pushed in
wenzelm@29774
  1065
  front of implication at each level of nesting.  The normal form is a
wenzelm@29774
  1066
  Hereditary Harrop Formula.
wenzelm@29769
  1067
wenzelm@29769
  1068
  \item The outermost prefix of parameters is represented via
wenzelm@29770
  1069
  schematic variables: instead of @{text "\<And>\<^vec>x. \<^vec>H \<^vec>x
wenzelm@29774
  1070
  \<Longrightarrow> A \<^vec>x"} we have @{text "\<^vec>H ?\<^vec>x \<Longrightarrow> A ?\<^vec>x"}.
wenzelm@29774
  1071
  Note that this representation looses information about the order of
wenzelm@29774
  1072
  parameters, and vacuous quantifiers vanish automatically.
wenzelm@29769
  1073
wenzelm@29769
  1074
  \end{itemize}
wenzelm@29769
  1075
*}
wenzelm@29769
  1076
wenzelm@29771
  1077
text %mlref {*
wenzelm@29771
  1078
  \begin{mldecls}
wenzelm@30552
  1079
  @{index_ML Simplifier.norm_hhf: "thm -> thm"} \\
wenzelm@29771
  1080
  \end{mldecls}
wenzelm@29771
  1081
wenzelm@29771
  1082
  \begin{description}
wenzelm@29771
  1083
wenzelm@30552
  1084
  \item @{ML Simplifier.norm_hhf}~@{text thm} normalizes the given
wenzelm@29771
  1085
  theorem according to the canonical form specified above.  This is
wenzelm@29771
  1086
  occasionally helpful to repair some low-level tools that do not
wenzelm@29771
  1087
  handle Hereditary Harrop Formulae properly.
wenzelm@29771
  1088
wenzelm@29771
  1089
  \end{description}
wenzelm@29771
  1090
*}
wenzelm@29771
  1091
wenzelm@29769
  1092
wenzelm@29769
  1093
subsection {* Rule composition *}
wenzelm@29769
  1094
wenzelm@29769
  1095
text {*
wenzelm@29771
  1096
  The rule calculus of Isabelle/Pure provides two main inferences:
wenzelm@29771
  1097
  @{inference resolution} (i.e.\ back-chaining of rules) and
wenzelm@29771
  1098
  @{inference assumption} (i.e.\ closing a branch), both modulo
wenzelm@29771
  1099
  higher-order unification.  There are also combined variants, notably
wenzelm@29771
  1100
  @{inference elim_resolution} and @{inference dest_resolution}.
wenzelm@20491
  1101
wenzelm@29771
  1102
  To understand the all-important @{inference resolution} principle,
wenzelm@29771
  1103
  we first consider raw @{inference_def composition} (modulo
wenzelm@29771
  1104
  higher-order unification with substitution @{text "\<vartheta>"}):
wenzelm@20498
  1105
  \[
wenzelm@29771
  1106
  \infer[(@{inference_def composition})]{@{text "\<^vec>A\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
  1107
  {@{text "\<^vec>A \<Longrightarrow> B"} & @{text "B' \<Longrightarrow> C"} & @{text "B\<vartheta> = B'\<vartheta>"}}
wenzelm@20498
  1108
  \]
wenzelm@29771
  1109
  Here the conclusion of the first rule is unified with the premise of
wenzelm@29771
  1110
  the second; the resulting rule instance inherits the premises of the
wenzelm@29771
  1111
  first and conclusion of the second.  Note that @{text "C"} can again
wenzelm@29771
  1112
  consist of iterated implications.  We can also permute the premises
wenzelm@29771
  1113
  of the second rule back-and-forth in order to compose with @{text
wenzelm@29771
  1114
  "B'"} in any position (subsequently we shall always refer to
wenzelm@29771
  1115
  position 1 w.l.o.g.).
wenzelm@20498
  1116
wenzelm@29774
  1117
  In @{inference composition} the internal structure of the common
wenzelm@29774
  1118
  part @{text "B"} and @{text "B'"} is not taken into account.  For
wenzelm@29774
  1119
  proper @{inference resolution} we require @{text "B"} to be atomic,
wenzelm@29774
  1120
  and explicitly observe the structure @{text "\<And>\<^vec>x. \<^vec>H
wenzelm@29774
  1121
  \<^vec>x \<Longrightarrow> B' \<^vec>x"} of the premise of the second rule.  The
wenzelm@29774
  1122
  idea is to adapt the first rule by ``lifting'' it into this context,
wenzelm@29774
  1123
  by means of iterated application of the following inferences:
wenzelm@20498
  1124
  \[
wenzelm@29771
  1125
  \infer[(@{inference_def imp_lift})]{@{text "(\<^vec>H \<Longrightarrow> \<^vec>A) \<Longrightarrow> (\<^vec>H \<Longrightarrow> B)"}}{@{text "\<^vec>A \<Longrightarrow> B"}}
wenzelm@20498
  1126
  \]
wenzelm@20498
  1127
  \[
wenzelm@29771
  1128
  \infer[(@{inference_def all_lift})]{@{text "(\<And>\<^vec>x. \<^vec>A (?\<^vec>a \<^vec>x)) \<Longrightarrow> (\<And>\<^vec>x. B (?\<^vec>a \<^vec>x))"}}{@{text "\<^vec>A ?\<^vec>a \<Longrightarrow> B ?\<^vec>a"}}
wenzelm@20498
  1129
  \]
wenzelm@29771
  1130
  By combining raw composition with lifting, we get full @{inference
wenzelm@29771
  1131
  resolution} as follows:
wenzelm@20498
  1132
  \[
wenzelm@29771
  1133
  \infer[(@{inference_def resolution})]
wenzelm@20498
  1134
  {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>A (?\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
  1135
  {\begin{tabular}{l}
wenzelm@20498
  1136
    @{text "\<^vec>A ?\<^vec>a \<Longrightarrow> B ?\<^vec>a"} \\
wenzelm@20498
  1137
    @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
wenzelm@20498
  1138
    @{text "(\<lambda>\<^vec>x. B (?\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
wenzelm@20498
  1139
   \end{tabular}}
wenzelm@20498
  1140
  \]
wenzelm@20498
  1141
wenzelm@29774
  1142
  Continued resolution of rules allows to back-chain a problem towards
wenzelm@29774
  1143
  more and sub-problems.  Branches are closed either by resolving with
wenzelm@29774
  1144
  a rule of 0 premises, or by producing a ``short-circuit'' within a
wenzelm@29774
  1145
  solved situation (again modulo unification):
wenzelm@29771
  1146
  \[
wenzelm@29771
  1147
  \infer[(@{inference_def assumption})]{@{text "C\<vartheta>"}}
wenzelm@29771
  1148
  {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> A \<^vec>x) \<Longrightarrow> C"} & @{text "A\<vartheta> = H\<^sub>i\<vartheta>"}~~\text{(for some~@{text i})}}
wenzelm@29771
  1149
  \]
wenzelm@20498
  1150
wenzelm@29771
  1151
  FIXME @{inference_def elim_resolution}, @{inference_def dest_resolution}
wenzelm@18537
  1152
*}
wenzelm@18537
  1153
wenzelm@29768
  1154
text %mlref {*
wenzelm@29768
  1155
  \begin{mldecls}
wenzelm@46262
  1156
  @{index_ML_op "RSN": "thm * (int * thm) -> thm"} \\
wenzelm@46262
  1157
  @{index_ML_op "RS": "thm * thm -> thm"} \\
wenzelm@46256
  1158
wenzelm@46262
  1159
  @{index_ML_op "RLN": "thm list * (int * thm list) -> thm list"} \\
wenzelm@46262
  1160
  @{index_ML_op "RL": "thm list * thm list -> thm list"} \\
wenzelm@46256
  1161
wenzelm@46262
  1162
  @{index_ML_op "MRS": "thm list * thm -> thm"} \\
wenzelm@46262
  1163
  @{index_ML_op "OF": "thm * thm list -> thm"} \\
wenzelm@29768
  1164
  \end{mldecls}
wenzelm@29768
  1165
wenzelm@29768
  1166
  \begin{description}
wenzelm@29768
  1167
wenzelm@46256
  1168
  \item @{text "rule\<^sub>1 RSN (i, rule\<^sub>2)"} resolves the conclusion of
wenzelm@46256
  1169
  @{text "rule\<^sub>1"} with the @{text i}-th premise of @{text "rule\<^sub>2"},
wenzelm@46256
  1170
  according to the @{inference resolution} principle explained above.
wenzelm@46256
  1171
  Unless there is precisely one resolvent it raises exception @{ML
wenzelm@46256
  1172
  THM}.
wenzelm@46256
  1173
wenzelm@46256
  1174
  This corresponds to the rule attribute @{attribute THEN} in Isar
wenzelm@46256
  1175
  source language.
wenzelm@46256
  1176
wenzelm@46256
  1177
  \item @{text "rule\<^sub>1 RS rule\<^sub>2"} abbreviates @{text "rule\<^sub>1 RS (1,
wenzelm@46256
  1178
  rule\<^sub>2)"}.
wenzelm@29768
  1179
wenzelm@46256
  1180
  \item @{text "rules\<^sub>1 RLN (i, rules\<^sub>2)"} joins lists of rules.  For
wenzelm@46256
  1181
  every @{text "rule\<^sub>1"} in @{text "rules\<^sub>1"} and @{text "rule\<^sub>2"} in
wenzelm@46256
  1182
  @{text "rules\<^sub>2"}, it resolves the conclusion of @{text "rule\<^sub>1"} with
wenzelm@46256
  1183
  the @{text "i"}-th premise of @{text "rule\<^sub>2"}, accumulating multiple
wenzelm@46256
  1184
  results in one big list.  Note that such strict enumerations of
wenzelm@46256
  1185
  higher-order unifications can be inefficient compared to the lazy
wenzelm@46256
  1186
  variant seen in elementary tactics like @{ML resolve_tac}.
wenzelm@46256
  1187
wenzelm@46256
  1188
  \item @{text "rules\<^sub>1 RL rules\<^sub>2"} abbreviates @{text "rules\<^sub>1 RLN (1,
wenzelm@46256
  1189
  rules\<^sub>2)"}.
wenzelm@46256
  1190
wenzelm@46256
  1191
  \item @{text "[rule\<^sub>1, \<dots>, rule\<^sub>n] MRS rule"} resolves @{text "rule\<^isub>i"}
wenzelm@46256
  1192
  against premise @{text "i"} of @{text "rule"}, for @{text "i = n, \<dots>,
wenzelm@46256
  1193
  1"}.  By working from right to left, newly emerging premises are
wenzelm@46256
  1194
  concatenated in the result, without interfering.
wenzelm@46256
  1195
wenzelm@47498
  1196
  \item @{text "rule OF rules"} is an alternative notation for @{text
wenzelm@47498
  1197
  "rules MRS rule"}, which makes rule composition look more like
wenzelm@47498
  1198
  function application.  Note that the argument @{text "rules"} need
wenzelm@47498
  1199
  not be atomic.
wenzelm@46256
  1200
wenzelm@46256
  1201
  This corresponds to the rule attribute @{attribute OF} in Isar
wenzelm@46256
  1202
  source language.
wenzelm@29768
  1203
wenzelm@29768
  1204
  \end{description}
wenzelm@29768
  1205
*}
wenzelm@30272
  1206
wenzelm@52407
  1207
wenzelm@52407
  1208
section {* Proof terms \label{sec:proof-terms} *}
wenzelm@52407
  1209
wenzelm@52407
  1210
text {* The Isabelle/Pure inference kernel can record the proof of
wenzelm@52407
  1211
  each theorem as a proof term that contains all logical inferences in
wenzelm@52407
  1212
  detail.  Rule composition by resolution (\secref{sec:obj-rules}) and
wenzelm@52407
  1213
  type-class reasoning is broken down to primitive rules of the
wenzelm@52407
  1214
  logical framework.  The proof term can be inspected by a separate
wenzelm@52407
  1215
  proof-checker, for example.
wenzelm@52407
  1216
wenzelm@52407
  1217
  According to the well-known \emph{Curry-Howard isomorphism}, a proof
wenzelm@52407
  1218
  can be viewed as a @{text "\<lambda>"}-term. Following this idea, proofs in
wenzelm@52407
  1219
  Isabelle are internally represented by a datatype similar to the one
wenzelm@52407
  1220
  for terms described in \secref{sec:terms}.  On top of these
wenzelm@52407
  1221
  syntactic terms, two more layers of @{text "\<lambda>"}-calculus are added,
wenzelm@52407
  1222
  which correspond to @{text "\<And>x :: \<alpha>. B x"} and @{text "A \<Longrightarrow> B"}
wenzelm@52407
  1223
  according to the propositions-as-types principle.  The resulting
wenzelm@52407
  1224
  3-level @{text "\<lambda>"}-calculus resembles ``@{text "\<lambda>HOL"}'' in the
wenzelm@52407
  1225
  more abstract setting of Pure Type Systems (PTS)
wenzelm@52407
  1226
  \cite{Barendregt-Geuvers:2001}, if some fine points like schematic
wenzelm@52407
  1227
  polymorphism and type classes are ignored.
wenzelm@52407
  1228
wenzelm@52407
  1229
  \medskip\emph{Proof abstractions} of the form @{text "\<^bold>\<lambda>x :: \<alpha>. prf"}
wenzelm@52407
  1230
  or @{text "\<^bold>\<lambda>p : A. prf"} correspond to introduction of @{text
wenzelm@52407
  1231
  "\<And>"}/@{text "\<Longrightarrow>"}, and \emph{proof applications} of the form @{text
wenzelm@52407
  1232
  "p \<cdot> t"} or @{text "p \<bullet> q"} correspond to elimination of @{text
wenzelm@52407
  1233
  "\<And>"}/@{text "\<Longrightarrow>"}.  Actual types @{text "\<alpha>"}, propositions @{text
wenzelm@52407
  1234
  "A"}, and terms @{text "t"} might be suppressed and reconstructed
wenzelm@52407
  1235
  from the overall proof term.
wenzelm@52407
  1236
wenzelm@52407
  1237
  \medskip Various atomic proofs indicate special situations within
wenzelm@52407
  1238
  the proof construction as follows.
wenzelm@52407
  1239
wenzelm@52407
  1240
  A \emph{bound proof variable} is a natural number @{text "b"} that
wenzelm@52407
  1241
  acts as de-Bruijn index for proof term abstractions.
wenzelm@52407
  1242
wenzelm@52407
  1243
  A \emph{minimal proof} ``@{text "?"}'' is a dummy proof term.  This
wenzelm@52407
  1244
  indicates some unrecorded part of the proof.
wenzelm@52407
  1245
wenzelm@52407
  1246
  @{text "Hyp A"} refers to some pending hypothesis by giving its
wenzelm@52407
  1247
  proposition.  This indicates an open context of implicit hypotheses,
wenzelm@52407
  1248
  similar to loose bound variables or free variables within a term
wenzelm@52407
  1249
  (\secref{sec:terms}).
wenzelm@52407
  1250
wenzelm@52407
  1251
  An \emph{axiom} or \emph{oracle} @{text "a : A[\<^vec>\<tau>]"} refers
wenzelm@52407
  1252
  some postulated @{text "proof constant"}, which is subject to
wenzelm@52407
  1253
  schematic polymorphism of theory content, and the particular type
wenzelm@52407
  1254
  instantiation may be given explicitly.  The vector of types @{text
wenzelm@52407
  1255
  "\<^vec>\<tau>"} refers to the schematic type variables in the generic
wenzelm@52407
  1256
  proposition @{text "A"} in canonical order.
wenzelm@52407
  1257
wenzelm@52407
  1258
  A \emph{proof promise} @{text "a : A[\<^vec>\<tau>]"} is a placeholder
wenzelm@52407
  1259
  for some proof of polymorphic proposition @{text "A"}, with explicit
wenzelm@52407
  1260
  type instantiation as given by the vector @{text "\<^vec>\<tau>"}, as
wenzelm@52407
  1261
  above.  Unlike axioms or oracles, proof promises may be
wenzelm@52407
  1262
  \emph{fulfilled} eventually, by substituting @{text "a"} by some
wenzelm@52407
  1263
  particular proof @{text "q"} at the corresponding type instance.
wenzelm@52407
  1264
  This acts like Hindley-Milner @{text "let"}-polymorphism: a generic
wenzelm@52407
  1265
  local proof definition may get used at different type instances, and
wenzelm@52407
  1266
  is replaced by the concrete instance eventually.
wenzelm@52407
  1267
wenzelm@52407
  1268
  A \emph{named theorem} wraps up some concrete proof as a closed
wenzelm@52407
  1269
  formal entity, in the manner of constant definitions for proof
wenzelm@52407
  1270
  terms.  The \emph{proof body} of such boxed theorems involves some
wenzelm@52407
  1271
  digest about oracles and promises occurring in the original proof.
wenzelm@52407
  1272
  This allows the inference kernel to manage this critical information
wenzelm@52407
  1273
  without the full overhead of explicit proof terms.
wenzelm@52407
  1274
*}
wenzelm@52407
  1275
wenzelm@52411
  1276
wenzelm@52411
  1277
subsection {* Reconstructing and checking proof terms *}
wenzelm@52411
  1278
wenzelm@52411
  1279
text {* Fully explicit proof terms can be large, but most of this
wenzelm@52411
  1280
  information is redundant and can be reconstructed from the context.
wenzelm@52411
  1281
  Therefore, the Isabelle/Pure inference kernel records only
wenzelm@52411
  1282
  \emph{implicit} proof terms, by omitting all typing information in
wenzelm@52411
  1283
  terms, all term and type labels of proof abstractions, and some
wenzelm@52411
  1284
  argument terms of applications @{text "p \<cdot> t"} (if possible).
wenzelm@52411
  1285
wenzelm@52411
  1286
  There are separate operations to reconstruct the full proof term
wenzelm@52411
  1287
  later on, using \emph{higher-order pattern unification}
wenzelm@52411
  1288
  \cite{nipkow-patterns,Berghofer-Nipkow:2000:TPHOL}.
wenzelm@52411
  1289
wenzelm@52411
  1290
  The \emph{proof checker} expects a fully reconstructed proof term,
wenzelm@52411
  1291
  and can turn it into a theorem by replaying its primitive inferences
wenzelm@52411
  1292
  within the kernel.  *}
wenzelm@52411
  1293
wenzelm@52408
  1294
text %mlref {*
wenzelm@52408
  1295
  \begin{mldecls}
wenzelm@52408
  1296
  @{index_ML_type proof} \\
wenzelm@52408
  1297
  @{index_ML_type proof_body} \\
wenzelm@52408
  1298
  @{index_ML proofs: "int Unsynchronized.ref"} \\
wenzelm@52411
  1299
  @{index_ML Reconstruct.reconstruct_proof:
wenzelm@52411
  1300
  "theory -> term -> proof -> proof"} \\
wenzelm@52411
  1301
  @{index_ML Reconstruct.expand_proof: "theory ->
wenzelm@52411
  1302
  (string * term option) list -> proof -> proof"} \\
wenzelm@52411
  1303
  @{index_ML Proof_Checker.thm_of_proof:
wenzelm@52411
  1304
  "theory -> proof -> thm"} \\
wenzelm@52408
  1305
  \end{mldecls}
wenzelm@52408
  1306
wenzelm@52408
  1307
  \begin{description}
wenzelm@52408
  1308
wenzelm@52408
  1309
  \item Type @{ML_type proof} represents proof terms; this is a
wenzelm@52408
  1310
  datatype with constructors @{index_ML Abst}, @{index_ML AbsP},
wenzelm@52408
  1311
  @{index_ML_op "%"}, @{index_ML_op "%%"}, @{index_ML PBound},
wenzelm@52408
  1312
  @{index_ML MinProof}, @{index_ML Hyp}, @{index_ML PAxm}, @{index_ML
wenzelm@52408
  1313
  Oracle}, @{index_ML Promise}, @{index_ML PThm} as explained above.
wenzelm@52408
  1314
wenzelm@52408
  1315
  \item Type @{ML_type proof_body} represents the nested proof
wenzelm@52408
  1316
  information of a named theorem, consisting of a digest of oracles
wenzelm@52408
  1317
  and named theorem over some proof term.  The digest only covers the
wenzelm@52408
  1318
  directly visible part of the proof: in order to get the full
wenzelm@52408
  1319
  information, the implicit graph of nested theorems needs to be
wenzelm@52408
  1320
  traversed (e.g.\ using @{ML Proofterm.fold_body_thms}).
wenzelm@52408
  1321
wenzelm@52408
  1322
  \item @{ML Thm.proof_of}~@{text "thm"} and @{ML
wenzelm@52408
  1323
  Thm.proof_body_of}~@{text "thm"} produce the proof term or proof
wenzelm@52408
  1324
  body (with digest of oracles and theorems) from a given theorem.
wenzelm@52408
  1325
  Note that this involves a full join of internal futures that fulfill
wenzelm@52408
  1326
  pending proof promises, and thus disrupts the natural bottom-up
wenzelm@52408
  1327
  construction of proofs by introducing dynamic ad-hoc dependencies.
wenzelm@52408
  1328
  Parallel performance may suffer by inspecting proof terms at
wenzelm@52408
  1329
  run-time.
wenzelm@52408
  1330
wenzelm@52408
  1331
  \item @{ML proofs} specifies the detail of proof recording within
wenzelm@52408
  1332
  @{ML_type thm} values produced by the inference kernel: @{ML 0}
wenzelm@52408
  1333
  records only the names of oracles, @{ML 1} records oracle names and
wenzelm@52408
  1334
  propositions, @{ML 2} additionally records full proof terms.
wenzelm@52408
  1335
  Officially named theorems that contribute to a result are recorded
wenzelm@52408
  1336
  in any case.
wenzelm@52408
  1337
wenzelm@52411
  1338
  \item @{ML Reconstruct.reconstruct_proof}~@{text "thy prop prf"}
wenzelm@52411
  1339
  turns the implicit proof term @{text "prf"} into a full proof of the
wenzelm@52411
  1340
  given proposition.
wenzelm@52411
  1341
wenzelm@52411
  1342
  Reconstruction may fail if @{text "prf"} is not a proof of @{text
wenzelm@52411
  1343
  "prop"}, or if it does not contain sufficient information for
wenzelm@52411
  1344
  reconstruction.  Failure may only happen for proofs that are
wenzelm@52411
  1345
  constructed manually, but not for those produced automatically by
wenzelm@52411
  1346
  the inference kernel.
wenzelm@52411
  1347
wenzelm@52411
  1348
  \item @{ML Reconstruct.expand_proof}~@{text "thy [thm\<^sub>1, \<dots>, thm\<^sub>n]
wenzelm@52411
  1349
  prf"} expands and reconstructs the proofs of all specified theorems,
wenzelm@52411
  1350
  with the given (full) proof.  Theorems that are not unique specified
wenzelm@52411
  1351
  via their name may be disambiguated by giving their proposition.
wenzelm@52411
  1352
wenzelm@52411
  1353
  \item @{ML Proof_Checker.thm_of_proof}~@{text "thy prf"} turns the
wenzelm@52411
  1354
  given (full) proof into a theorem, by replaying it using only
wenzelm@52411
  1355
  primitive rules of the inference kernel.
wenzelm@52411
  1356
wenzelm@52408
  1357
  \end{description}
wenzelm@52408
  1358
*}
wenzelm@52408
  1359
wenzelm@52410
  1360
text %mlex {* Detailed proof information of a theorem may be retrieved
wenzelm@52410
  1361
  as follows: *}
wenzelm@52410
  1362
wenzelm@52410
  1363
lemma ex: "A \<and> B \<longrightarrow> B \<and> A"
wenzelm@52410
  1364
proof
wenzelm@52410
  1365
  assume "A \<and> B"
wenzelm@52410
  1366
  then obtain B and A ..
wenzelm@52410
  1367
  then show "B \<and> A" ..
wenzelm@52410
  1368
qed
wenzelm@52410
  1369
wenzelm@52410
  1370
ML_val {*
wenzelm@52410
  1371
  (*proof body with digest*)
wenzelm@52410
  1372
  val body = Proofterm.strip_thm (Thm.proof_body_of @{thm ex});
wenzelm@52410
  1373
wenzelm@52410
  1374
  (*proof term only*)
wenzelm@52410
  1375
  val prf = Proofterm.proof_of body;
wenzelm@52410
  1376
  Pretty.writeln (Proof_Syntax.pretty_proof @{context} prf);
wenzelm@52410
  1377
wenzelm@52410
  1378
  (*all theorems used in the graph of nested proofs*)
wenzelm@52410
  1379
  val all_thms =
wenzelm@52410
  1380
    Proofterm.fold_body_thms
wenzelm@52410
  1381
      (fn (name, _, _) => insert (op =) name) [body] [];
wenzelm@52410
  1382
*}
wenzelm@52410
  1383
wenzelm@52410
  1384
text {* The result refers to various basic facts of Isabelle/HOL:
wenzelm@52410
  1385
  @{thm [source] HOL.impI}, @{thm [source] HOL.conjE}, @{thm [source]
wenzelm@52410
  1386
  HOL.conjI} etc.  The combinator @{ML Proofterm.fold_body_thms}
wenzelm@52410
  1387
  recursively explores the graph of the proofs of all theorems being
wenzelm@52410
  1388
  used here.
wenzelm@52410
  1389
wenzelm@52410
  1390
  \medskip Alternatively, we may produce a proof term manually, and
wenzelm@52410
  1391
  turn it into a theorem as follows: *}
wenzelm@52410
  1392
wenzelm@52410
  1393
ML_val {*
wenzelm@52410
  1394
  val thy = @{theory};
wenzelm@52410
  1395
  val prf =
wenzelm@52410
  1396
    Proof_Syntax.read_proof thy true false
wenzelm@52410
  1397
      "impI \<cdot> _ \<cdot> _ \<bullet> \
wenzelm@52410
  1398
      \   (Lam H: _. \
wenzelm@52410
  1399
      \     conjE \<cdot> _ \<cdot> _ \<cdot> _ \<bullet> H \<bullet> \
wenzelm@52410
  1400
      \       (Lam (H: _) Ha: _. conjI \<cdot> _ \<cdot> _ \<bullet> Ha \<bullet> H))";
wenzelm@52410
  1401
  val thm =
wenzelm@52410
  1402
    prf
wenzelm@52410
  1403
    |> Reconstruct.reconstruct_proof thy @{prop "A \<and> B \<longrightarrow> B \<and> A"}
wenzelm@52410
  1404
    |> Proof_Checker.thm_of_proof thy
wenzelm@52410
  1405
    |> Drule.export_without_context;
wenzelm@52410
  1406
*}
wenzelm@52410
  1407
wenzelm@52410
  1408
text {* \medskip The subsequent example illustrates the use of various
wenzelm@52410
  1409
  key operations on proof terms: the proof term of an existing theorem
wenzelm@52410
  1410
  is reconstructed and turned again into a theorem using the proof
wenzelm@52410
  1411
  checker; some informative output is printed as well.  *}
wenzelm@52410
  1412
wenzelm@52410
  1413
ML {*
wenzelm@52410
  1414
  fun recheck ctxt0 thm0 =
wenzelm@52410
  1415
    let
wenzelm@52410
  1416
      (*formal transfer and import -- avoid schematic variables*)
wenzelm@52410
  1417
      val thy = Proof_Context.theory_of ctxt0;
wenzelm@52410
  1418
      val ((_, [thm]), ctxt) =
wenzelm@52410
  1419
        Variable.import true [Thm.transfer thy thm0] ctxt0;
wenzelm@52410
  1420
wenzelm@52410
  1421
      (*main proof information*)
wenzelm@52410
  1422
      val prop = Thm.prop_of thm;
wenzelm@52410
  1423
      val prf =
wenzelm@52410
  1424
        Proofterm.proof_of
wenzelm@52410
  1425
          (Proofterm.strip_thm (Thm.proof_body_of thm));
wenzelm@52410
  1426
      val full_prf = Reconstruct.reconstruct_proof thy prop prf;
wenzelm@52410
  1427
wenzelm@52410
  1428
      (*informative output*)
wenzelm@52410
  1429
      fun pretty_item name prt =
wenzelm@52410
  1430
        Pretty.block [Pretty.str name, Pretty.brk 1, prt];
wenzelm@52410
  1431
      val _ =
wenzelm@52410
  1432
        [pretty_item "proposition:" (Syntax.pretty_term ctxt prop),
wenzelm@52410
  1433
         pretty_item "proof:" (Proof_Syntax.pretty_proof ctxt prf),
wenzelm@52410
  1434
         pretty_item "full proof:"
wenzelm@52410
  1435
          (Proof_Syntax.pretty_proof ctxt full_prf)]
wenzelm@52410
  1436
        |> Pretty.chunks |> Pretty.writeln;
wenzelm@52410
  1437
wenzelm@52410
  1438
      (*rechecked theorem*)
wenzelm@52410
  1439
      val thm' =
wenzelm@52410
  1440
        Proof_Checker.thm_of_proof thy full_prf
wenzelm@52410
  1441
        |> singleton (Proof_Context.export ctxt ctxt0);
wenzelm@52410
  1442
    in thm' end;
wenzelm@52410
  1443
*}
wenzelm@52410
  1444
wenzelm@52410
  1445
text {* As anticipated, the rechecked theorem establishes the same
wenzelm@52410
  1446
  proposition: *}
wenzelm@52410
  1447
wenzelm@52410
  1448
ML_val {*
wenzelm@52410
  1449
  val thm = @{thm ex};
wenzelm@52410
  1450
  val thm' = recheck @{context} thm;
wenzelm@52410
  1451
  @{assert} (Thm.eq_thm_prop (thm, thm'));
wenzelm@52410
  1452
*}
wenzelm@52410
  1453
wenzelm@52410
  1454
text {* More precise theorem equality is achieved by adjusting a few
wenzelm@52410
  1455
  accidental details of the theorems involved here: *}
wenzelm@52410
  1456
wenzelm@52410
  1457
ML_val {*
wenzelm@52410
  1458
  val thm = Thm.map_tags (K []) @{thm ex};
wenzelm@52410
  1459
  val thm' = Thm.strip_shyps (recheck @{context} thm);
wenzelm@52410
  1460
  @{assert} (Thm.eq_thm (thm, thm'));
wenzelm@52410
  1461
*}
wenzelm@52410
  1462
wenzelm@18537
  1463
end