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