doc-src/IsarImplementation/Thy/Logic.thy
author wenzelm
Wed Jan 25 21:14:00 2012 +0100 (2012-01-25)
changeset 46262 912b42e64fde
parent 46256 bc874d2ee55a
child 46497 89ccf66aa73d
permissions -rw-r--r--
tuned ML infixes;
wenzelm@29755
     1
theory Logic
wenzelm@29755
     2
imports Base
wenzelm@29755
     3
begin
wenzelm@18537
     4
wenzelm@20470
     5
chapter {* Primitive logic \label{ch:logic} *}
wenzelm@18537
     6
wenzelm@20480
     7
text {*
wenzelm@20480
     8
  The logical foundations of Isabelle/Isar are that of the Pure logic,
wenzelm@29774
     9
  which has been introduced as a Natural Deduction framework in
wenzelm@20480
    10
  \cite{paulson700}.  This is essentially the same logic as ``@{text
wenzelm@20493
    11
  "\<lambda>HOL"}'' in the more abstract setting of Pure Type Systems (PTS)
wenzelm@20480
    12
  \cite{Barendregt-Geuvers:2001}, although there are some key
wenzelm@20491
    13
  differences in the specific treatment of simple types in
wenzelm@20491
    14
  Isabelle/Pure.
wenzelm@20480
    15
wenzelm@20480
    16
  Following type-theoretic parlance, the Pure logic consists of three
wenzelm@20543
    17
  levels of @{text "\<lambda>"}-calculus with corresponding arrows, @{text
wenzelm@20480
    18
  "\<Rightarrow>"} for syntactic function space (terms depending on terms), @{text
wenzelm@20480
    19
  "\<And>"} for universal quantification (proofs depending on terms), and
wenzelm@20480
    20
  @{text "\<Longrightarrow>"} for implication (proofs depending on proofs).
wenzelm@20480
    21
wenzelm@20537
    22
  Derivations are relative to a logical theory, which declares type
wenzelm@20537
    23
  constructors, constants, and axioms.  Theory declarations support
wenzelm@20537
    24
  schematic polymorphism, which is strictly speaking outside the
wenzelm@20537
    25
  logic.\footnote{This is the deeper logical reason, why the theory
wenzelm@20537
    26
  context @{text "\<Theta>"} is separate from the proof context @{text "\<Gamma>"}
wenzelm@34929
    27
  of the core calculus: type constructors, term constants, and facts
wenzelm@34929
    28
  (proof constants) may involve arbitrary type schemes, but the type
wenzelm@34929
    29
  of a locally fixed term parameter is also fixed!}
wenzelm@20480
    30
*}
wenzelm@20480
    31
wenzelm@20480
    32
wenzelm@20451
    33
section {* Types \label{sec:types} *}
wenzelm@20437
    34
wenzelm@20437
    35
text {*
wenzelm@20480
    36
  The language of types is an uninterpreted order-sorted first-order
wenzelm@20480
    37
  algebra; types are qualified by ordered type classes.
wenzelm@20480
    38
wenzelm@20480
    39
  \medskip A \emph{type class} is an abstract syntactic entity
wenzelm@20480
    40
  declared in the theory context.  The \emph{subclass relation} @{text
wenzelm@20480
    41
  "c\<^isub>1 \<subseteq> c\<^isub>2"} is specified by stating an acyclic
wenzelm@20491
    42
  generating relation; the transitive closure is maintained
wenzelm@20491
    43
  internally.  The resulting relation is an ordering: reflexive,
wenzelm@20491
    44
  transitive, and antisymmetric.
wenzelm@20451
    45
wenzelm@34929
    46
  A \emph{sort} is a list of type classes written as @{text "s = {c\<^isub>1,
wenzelm@34929
    47
  \<dots>, c\<^isub>m}"}, it represents symbolic intersection.  Notationally, the
wenzelm@34929
    48
  curly braces are omitted for singleton intersections, i.e.\ any
wenzelm@34929
    49
  class @{text "c"} may be read as a sort @{text "{c}"}.  The ordering
wenzelm@34929
    50
  on type classes is extended to sorts according to the meaning of
wenzelm@34929
    51
  intersections: @{text "{c\<^isub>1, \<dots> c\<^isub>m} \<subseteq> {d\<^isub>1, \<dots>, d\<^isub>n}"} iff @{text
wenzelm@34929
    52
  "\<forall>j. \<exists>i. c\<^isub>i \<subseteq> d\<^isub>j"}.  The empty intersection @{text "{}"} refers to
wenzelm@34929
    53
  the universal sort, which is the largest element wrt.\ the sort
wenzelm@34929
    54
  order.  Thus @{text "{}"} represents the ``full sort'', not the
wenzelm@34929
    55
  empty one!  The intersection of all (finitely many) classes declared
wenzelm@34929
    56
  in the current theory is the least element wrt.\ the sort ordering.
wenzelm@20480
    57
wenzelm@20491
    58
  \medskip A \emph{fixed type variable} is a pair of a basic name
wenzelm@20537
    59
  (starting with a @{text "'"} character) and a sort constraint, e.g.\
wenzelm@20537
    60
  @{text "('a, s)"} which is usually printed as @{text "\<alpha>\<^isub>s"}.
wenzelm@20537
    61
  A \emph{schematic type variable} is a pair of an indexname and a
wenzelm@20537
    62
  sort constraint, e.g.\ @{text "(('a, 0), s)"} which is usually
wenzelm@20537
    63
  printed as @{text "?\<alpha>\<^isub>s"}.
wenzelm@20451
    64
wenzelm@20480
    65
  Note that \emph{all} syntactic components contribute to the identity
wenzelm@34929
    66
  of type variables: basic name, index, and sort constraint.  The core
wenzelm@34929
    67
  logic handles type variables with the same name but different sorts
wenzelm@34929
    68
  as different, although the type-inference layer (which is outside
wenzelm@34929
    69
  the core) rejects anything like that.
wenzelm@20451
    70
wenzelm@20493
    71
  A \emph{type constructor} @{text "\<kappa>"} is a @{text "k"}-ary operator
wenzelm@20493
    72
  on types declared in the theory.  Type constructor application is
wenzelm@20537
    73
  written postfix as @{text "(\<alpha>\<^isub>1, \<dots>, \<alpha>\<^isub>k)\<kappa>"}.  For
wenzelm@20537
    74
  @{text "k = 0"} the argument tuple is omitted, e.g.\ @{text "prop"}
wenzelm@20537
    75
  instead of @{text "()prop"}.  For @{text "k = 1"} the parentheses
wenzelm@20537
    76
  are omitted, e.g.\ @{text "\<alpha> list"} instead of @{text "(\<alpha>)list"}.
wenzelm@20537
    77
  Further notation is provided for specific constructors, notably the
wenzelm@20537
    78
  right-associative infix @{text "\<alpha> \<Rightarrow> \<beta>"} instead of @{text "(\<alpha>,
wenzelm@20537
    79
  \<beta>)fun"}.
wenzelm@20480
    80
  
wenzelm@34929
    81
  The logical category \emph{type} is defined inductively over type
wenzelm@34929
    82
  variables and type constructors as follows: @{text "\<tau> = \<alpha>\<^isub>s | ?\<alpha>\<^isub>s |
wenzelm@20543
    83
  (\<tau>\<^sub>1, \<dots>, \<tau>\<^sub>k)\<kappa>"}.
wenzelm@20451
    84
wenzelm@20514
    85
  A \emph{type abbreviation} is a syntactic definition @{text
wenzelm@20494
    86
  "(\<^vec>\<alpha>)\<kappa> = \<tau>"} of an arbitrary type expression @{text "\<tau>"} over
wenzelm@20537
    87
  variables @{text "\<^vec>\<alpha>"}.  Type abbreviations appear as type
wenzelm@20537
    88
  constructors in the syntax, but are expanded before entering the
wenzelm@20537
    89
  logical core.
wenzelm@20480
    90
wenzelm@20480
    91
  A \emph{type arity} declares the image behavior of a type
wenzelm@20494
    92
  constructor wrt.\ the algebra of sorts: @{text "\<kappa> :: (s\<^isub>1, \<dots>,
wenzelm@20494
    93
  s\<^isub>k)s"} means that @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>k)\<kappa>"} is
wenzelm@20494
    94
  of sort @{text "s"} if every argument type @{text "\<tau>\<^isub>i"} is
wenzelm@20494
    95
  of sort @{text "s\<^isub>i"}.  Arity declarations are implicitly
wenzelm@20494
    96
  completed, i.e.\ @{text "\<kappa> :: (\<^vec>s)c"} entails @{text "\<kappa> ::
wenzelm@20491
    97
  (\<^vec>s)c'"} for any @{text "c' \<supseteq> c"}.
wenzelm@20491
    98
wenzelm@20491
    99
  \medskip The sort algebra is always maintained as \emph{coregular},
wenzelm@20491
   100
  which means that type arities are consistent with the subclass
wenzelm@20537
   101
  relation: for any type constructor @{text "\<kappa>"}, and classes @{text
wenzelm@20537
   102
  "c\<^isub>1 \<subseteq> c\<^isub>2"}, and arities @{text "\<kappa> ::
wenzelm@20537
   103
  (\<^vec>s\<^isub>1)c\<^isub>1"} and @{text "\<kappa> ::
wenzelm@20537
   104
  (\<^vec>s\<^isub>2)c\<^isub>2"} holds @{text "\<^vec>s\<^isub>1 \<subseteq>
wenzelm@20537
   105
  \<^vec>s\<^isub>2"} component-wise.
wenzelm@20451
   106
wenzelm@20491
   107
  The key property of a coregular order-sorted algebra is that sort
wenzelm@20537
   108
  constraints can be solved in a most general fashion: for each type
wenzelm@20537
   109
  constructor @{text "\<kappa>"} and sort @{text "s"} there is a most general
wenzelm@20537
   110
  vector of argument sorts @{text "(s\<^isub>1, \<dots>, s\<^isub>k)"} such
wenzelm@20537
   111
  that a type scheme @{text "(\<alpha>\<^bsub>s\<^isub>1\<^esub>, \<dots>,
wenzelm@20537
   112
  \<alpha>\<^bsub>s\<^isub>k\<^esub>)\<kappa>"} is of sort @{text "s"}.
wenzelm@20543
   113
  Consequently, type unification has most general solutions (modulo
wenzelm@20543
   114
  equivalence of sorts), so type-inference produces primary types as
wenzelm@20543
   115
  expected \cite{nipkow-prehofer}.
wenzelm@20480
   116
*}
wenzelm@20451
   117
wenzelm@20480
   118
text %mlref {*
wenzelm@20480
   119
  \begin{mldecls}
wenzelm@34921
   120
  @{index_ML_type class: string} \\
wenzelm@34921
   121
  @{index_ML_type sort: "class list"} \\
wenzelm@34921
   122
  @{index_ML_type arity: "string * sort list * sort"} \\
wenzelm@20480
   123
  @{index_ML_type typ} \\
wenzelm@39846
   124
  @{index_ML Term.map_atyps: "(typ -> typ) -> typ -> typ"} \\
wenzelm@39846
   125
  @{index_ML Term.fold_atyps: "(typ -> 'a -> 'a) -> typ -> 'a -> 'a"} \\
wenzelm@20547
   126
  \end{mldecls}
wenzelm@20547
   127
  \begin{mldecls}
wenzelm@20480
   128
  @{index_ML Sign.subsort: "theory -> sort * sort -> bool"} \\
wenzelm@20480
   129
  @{index_ML Sign.of_sort: "theory -> typ * sort -> bool"} \\
wenzelm@42401
   130
  @{index_ML Sign.add_types: "Proof.context ->
wenzelm@42401
   131
  (binding * int * mixfix) list -> theory -> theory"} \\
wenzelm@42401
   132
  @{index_ML Sign.add_type_abbrev: "Proof.context ->
wenzelm@42401
   133
  binding * string list * typ -> theory -> theory"} \\
wenzelm@30355
   134
  @{index_ML Sign.primitive_class: "binding * class list -> theory -> theory"} \\
wenzelm@20480
   135
  @{index_ML Sign.primitive_classrel: "class * class -> theory -> theory"} \\
wenzelm@20480
   136
  @{index_ML Sign.primitive_arity: "arity -> theory -> theory"} \\
wenzelm@20480
   137
  \end{mldecls}
wenzelm@20480
   138
wenzelm@20480
   139
  \begin{description}
wenzelm@20480
   140
wenzelm@39864
   141
  \item Type @{ML_type class} represents type classes.
wenzelm@20480
   142
wenzelm@39864
   143
  \item Type @{ML_type sort} represents sorts, i.e.\ finite
wenzelm@39864
   144
  intersections of classes.  The empty list @{ML "[]: sort"} refers to
wenzelm@39864
   145
  the empty class intersection, i.e.\ the ``full sort''.
wenzelm@20451
   146
wenzelm@39864
   147
  \item Type @{ML_type arity} represents type arities.  A triple
wenzelm@39864
   148
  @{text "(\<kappa>, \<^vec>s, s) : arity"} represents @{text "\<kappa> ::
wenzelm@39864
   149
  (\<^vec>s)s"} as described above.
wenzelm@20480
   150
wenzelm@39864
   151
  \item Type @{ML_type typ} represents types; this is a datatype with
wenzelm@20480
   152
  constructors @{ML TFree}, @{ML TVar}, @{ML Type}.
wenzelm@20480
   153
wenzelm@39846
   154
  \item @{ML Term.map_atyps}~@{text "f \<tau>"} applies the mapping @{text
wenzelm@39846
   155
  "f"} to all atomic types (@{ML TFree}, @{ML TVar}) occurring in
wenzelm@39846
   156
  @{text "\<tau>"}.
wenzelm@20519
   157
wenzelm@39846
   158
  \item @{ML Term.fold_atyps}~@{text "f \<tau>"} iterates the operation
wenzelm@39846
   159
  @{text "f"} over all occurrences of atomic types (@{ML TFree}, @{ML
wenzelm@39846
   160
  TVar}) in @{text "\<tau>"}; the type structure is traversed from left to
wenzelm@39846
   161
  right.
wenzelm@20494
   162
wenzelm@20480
   163
  \item @{ML Sign.subsort}~@{text "thy (s\<^isub>1, s\<^isub>2)"}
wenzelm@20480
   164
  tests the subsort relation @{text "s\<^isub>1 \<subseteq> s\<^isub>2"}.
wenzelm@20480
   165
wenzelm@20537
   166
  \item @{ML Sign.of_sort}~@{text "thy (\<tau>, s)"} tests whether type
wenzelm@20537
   167
  @{text "\<tau>"} is of sort @{text "s"}.
wenzelm@20480
   168
wenzelm@42401
   169
  \item @{ML Sign.add_types}~@{text "ctxt [(\<kappa>, k, mx), \<dots>]"} declares a
wenzelm@42401
   170
  new type constructors @{text "\<kappa>"} with @{text "k"} arguments and
wenzelm@20480
   171
  optional mixfix syntax.
wenzelm@20451
   172
wenzelm@42401
   173
  \item @{ML Sign.add_type_abbrev}~@{text "ctxt (\<kappa>, \<^vec>\<alpha>, \<tau>)"}
wenzelm@42401
   174
  defines a new type abbreviation @{text "(\<^vec>\<alpha>)\<kappa> = \<tau>"}.
wenzelm@20480
   175
wenzelm@20480
   176
  \item @{ML Sign.primitive_class}~@{text "(c, [c\<^isub>1, \<dots>,
wenzelm@20537
   177
  c\<^isub>n])"} declares a new class @{text "c"}, together with class
wenzelm@20494
   178
  relations @{text "c \<subseteq> c\<^isub>i"}, for @{text "i = 1, \<dots>, n"}.
wenzelm@20480
   179
wenzelm@20480
   180
  \item @{ML Sign.primitive_classrel}~@{text "(c\<^isub>1,
wenzelm@20543
   181
  c\<^isub>2)"} declares the class relation @{text "c\<^isub>1 \<subseteq>
wenzelm@20480
   182
  c\<^isub>2"}.
wenzelm@20480
   183
wenzelm@20494
   184
  \item @{ML Sign.primitive_arity}~@{text "(\<kappa>, \<^vec>s, s)"} declares
wenzelm@20537
   185
  the arity @{text "\<kappa> :: (\<^vec>s)s"}.
wenzelm@20480
   186
wenzelm@20480
   187
  \end{description}
wenzelm@20437
   188
*}
wenzelm@20437
   189
wenzelm@39832
   190
text %mlantiq {*
wenzelm@39832
   191
  \begin{matharray}{rcl}
wenzelm@39832
   192
  @{ML_antiquotation_def "class"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   193
  @{ML_antiquotation_def "sort"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   194
  @{ML_antiquotation_def "type_name"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   195
  @{ML_antiquotation_def "type_abbrev"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   196
  @{ML_antiquotation_def "nonterminal"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   197
  @{ML_antiquotation_def "typ"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   198
  \end{matharray}
wenzelm@39832
   199
wenzelm@42510
   200
  @{rail "
wenzelm@42510
   201
  @@{ML_antiquotation class} nameref
wenzelm@39832
   202
  ;
wenzelm@42510
   203
  @@{ML_antiquotation sort} sort
wenzelm@39832
   204
  ;
wenzelm@42510
   205
  (@@{ML_antiquotation type_name} |
wenzelm@42510
   206
   @@{ML_antiquotation type_abbrev} |
wenzelm@42510
   207
   @@{ML_antiquotation nonterminal}) nameref
wenzelm@39832
   208
  ;
wenzelm@42510
   209
  @@{ML_antiquotation typ} type
wenzelm@42510
   210
  "}
wenzelm@39832
   211
wenzelm@39832
   212
  \begin{description}
wenzelm@39832
   213
wenzelm@39832
   214
  \item @{text "@{class c}"} inlines the internalized class @{text
wenzelm@39832
   215
  "c"} --- as @{ML_type string} literal.
wenzelm@39832
   216
wenzelm@39832
   217
  \item @{text "@{sort s}"} inlines the internalized sort @{text "s"}
wenzelm@39832
   218
  --- as @{ML_type "string list"} literal.
wenzelm@39832
   219
wenzelm@39832
   220
  \item @{text "@{type_name c}"} inlines the internalized type
wenzelm@39832
   221
  constructor @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   222
wenzelm@39832
   223
  \item @{text "@{type_abbrev c}"} inlines the internalized type
wenzelm@39832
   224
  abbreviation @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   225
wenzelm@39832
   226
  \item @{text "@{nonterminal c}"} inlines the internalized syntactic
wenzelm@39832
   227
  type~/ grammar nonterminal @{text "c"} --- as @{ML_type string}
wenzelm@39832
   228
  literal.
wenzelm@39832
   229
wenzelm@39832
   230
  \item @{text "@{typ \<tau>}"} inlines the internalized type @{text "\<tau>"}
wenzelm@39832
   231
  --- as constructor term for datatype @{ML_type typ}.
wenzelm@39832
   232
wenzelm@39832
   233
  \end{description}
wenzelm@39832
   234
*}
wenzelm@39832
   235
wenzelm@20437
   236
wenzelm@20451
   237
section {* Terms \label{sec:terms} *}
wenzelm@18537
   238
wenzelm@18537
   239
text {*
wenzelm@20491
   240
  The language of terms is that of simply-typed @{text "\<lambda>"}-calculus
wenzelm@20520
   241
  with de-Bruijn indices for bound variables (cf.\ \cite{debruijn72}
wenzelm@29761
   242
  or \cite{paulson-ml2}), with the types being determined by the
wenzelm@29761
   243
  corresponding binders.  In contrast, free variables and constants
wenzelm@34929
   244
  have an explicit name and type in each occurrence.
wenzelm@20514
   245
wenzelm@20514
   246
  \medskip A \emph{bound variable} is a natural number @{text "b"},
wenzelm@20537
   247
  which accounts for the number of intermediate binders between the
wenzelm@20537
   248
  variable occurrence in the body and its binding position.  For
wenzelm@34929
   249
  example, the de-Bruijn term @{text "\<lambda>\<^bsub>bool\<^esub>. \<lambda>\<^bsub>bool\<^esub>. 1 \<and> 0"} would
wenzelm@34929
   250
  correspond to @{text "\<lambda>x\<^bsub>bool\<^esub>. \<lambda>y\<^bsub>bool\<^esub>. x \<and> y"} in a named
wenzelm@20543
   251
  representation.  Note that a bound variable may be represented by
wenzelm@20543
   252
  different de-Bruijn indices at different occurrences, depending on
wenzelm@20543
   253
  the nesting of abstractions.
wenzelm@20537
   254
wenzelm@20543
   255
  A \emph{loose variable} is a bound variable that is outside the
wenzelm@20537
   256
  scope of local binders.  The types (and names) for loose variables
wenzelm@20543
   257
  can be managed as a separate context, that is maintained as a stack
wenzelm@20543
   258
  of hypothetical binders.  The core logic operates on closed terms,
wenzelm@20543
   259
  without any loose variables.
wenzelm@20514
   260
wenzelm@20537
   261
  A \emph{fixed variable} is a pair of a basic name and a type, e.g.\
wenzelm@34929
   262
  @{text "(x, \<tau>)"} which is usually printed @{text "x\<^isub>\<tau>"} here.  A
wenzelm@20537
   263
  \emph{schematic variable} is a pair of an indexname and a type,
wenzelm@34929
   264
  e.g.\ @{text "((x, 0), \<tau>)"} which is likewise printed as @{text
wenzelm@20537
   265
  "?x\<^isub>\<tau>"}.
wenzelm@20491
   266
wenzelm@20537
   267
  \medskip A \emph{constant} is a pair of a basic name and a type,
wenzelm@34929
   268
  e.g.\ @{text "(c, \<tau>)"} which is usually printed as @{text "c\<^isub>\<tau>"}
wenzelm@34929
   269
  here.  Constants are declared in the context as polymorphic families
wenzelm@34929
   270
  @{text "c :: \<sigma>"}, meaning that all substitution instances @{text
wenzelm@34929
   271
  "c\<^isub>\<tau>"} for @{text "\<tau> = \<sigma>\<vartheta>"} are valid.
wenzelm@20514
   272
wenzelm@34929
   273
  The vector of \emph{type arguments} of constant @{text "c\<^isub>\<tau>"} wrt.\
wenzelm@34929
   274
  the declaration @{text "c :: \<sigma>"} is defined as the codomain of the
wenzelm@34929
   275
  matcher @{text "\<vartheta> = {?\<alpha>\<^isub>1 \<mapsto> \<tau>\<^isub>1, \<dots>, ?\<alpha>\<^isub>n \<mapsto> \<tau>\<^isub>n}"} presented in
wenzelm@34929
   276
  canonical order @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n)"}, corresponding to the
wenzelm@34929
   277
  left-to-right occurrences of the @{text "\<alpha>\<^isub>i"} in @{text "\<sigma>"}.
wenzelm@34929
   278
  Within a given theory context, there is a one-to-one correspondence
wenzelm@34929
   279
  between any constant @{text "c\<^isub>\<tau>"} and the application @{text "c(\<tau>\<^isub>1,
wenzelm@34929
   280
  \<dots>, \<tau>\<^isub>n)"} of its type arguments.  For example, with @{text "plus :: \<alpha>
wenzelm@34929
   281
  \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"}, the instance @{text "plus\<^bsub>nat \<Rightarrow> nat \<Rightarrow> nat\<^esub>"} corresponds to
wenzelm@34929
   282
  @{text "plus(nat)"}.
wenzelm@20480
   283
wenzelm@20514
   284
  Constant declarations @{text "c :: \<sigma>"} may contain sort constraints
wenzelm@20514
   285
  for type variables in @{text "\<sigma>"}.  These are observed by
wenzelm@20514
   286
  type-inference as expected, but \emph{ignored} by the core logic.
wenzelm@20514
   287
  This means the primitive logic is able to reason with instances of
wenzelm@20537
   288
  polymorphic constants that the user-level type-checker would reject
wenzelm@20537
   289
  due to violation of type class restrictions.
wenzelm@20480
   290
wenzelm@39861
   291
  \medskip An \emph{atomic term} is either a variable or constant.
wenzelm@34929
   292
  The logical category \emph{term} is defined inductively over atomic
wenzelm@34929
   293
  terms, with abstraction and application as follows: @{text "t = b |
wenzelm@34929
   294
  x\<^isub>\<tau> | ?x\<^isub>\<tau> | c\<^isub>\<tau> | \<lambda>\<^isub>\<tau>. t | t\<^isub>1 t\<^isub>2"}.  Parsing and printing takes care of
wenzelm@34929
   295
  converting between an external representation with named bound
wenzelm@34929
   296
  variables.  Subsequently, we shall use the latter notation instead
wenzelm@34929
   297
  of internal de-Bruijn representation.
wenzelm@20498
   298
wenzelm@20537
   299
  The inductive relation @{text "t :: \<tau>"} assigns a (unique) type to a
wenzelm@20537
   300
  term according to the structure of atomic terms, abstractions, and
wenzelm@20537
   301
  applicatins:
wenzelm@20498
   302
  \[
wenzelm@20501
   303
  \infer{@{text "a\<^isub>\<tau> :: \<tau>"}}{}
wenzelm@20498
   304
  \qquad
wenzelm@20501
   305
  \infer{@{text "(\<lambda>x\<^sub>\<tau>. t) :: \<tau> \<Rightarrow> \<sigma>"}}{@{text "t :: \<sigma>"}}
wenzelm@20501
   306
  \qquad
wenzelm@20501
   307
  \infer{@{text "t u :: \<sigma>"}}{@{text "t :: \<tau> \<Rightarrow> \<sigma>"} & @{text "u :: \<tau>"}}
wenzelm@20498
   308
  \]
wenzelm@20514
   309
  A \emph{well-typed term} is a term that can be typed according to these rules.
wenzelm@20498
   310
wenzelm@20514
   311
  Typing information can be omitted: type-inference is able to
wenzelm@20514
   312
  reconstruct the most general type of a raw term, while assigning
wenzelm@20514
   313
  most general types to all of its variables and constants.
wenzelm@20514
   314
  Type-inference depends on a context of type constraints for fixed
wenzelm@20514
   315
  variables, and declarations for polymorphic constants.
wenzelm@20514
   316
wenzelm@20537
   317
  The identity of atomic terms consists both of the name and the type
wenzelm@20537
   318
  component.  This means that different variables @{text
wenzelm@34929
   319
  "x\<^bsub>\<tau>\<^isub>1\<^esub>"} and @{text "x\<^bsub>\<tau>\<^isub>2\<^esub>"} may become the same after
wenzelm@34929
   320
  type instantiation.  Type-inference rejects variables of the same
wenzelm@34929
   321
  name, but different types.  In contrast, mixed instances of
wenzelm@34929
   322
  polymorphic constants occur routinely.
wenzelm@20514
   323
wenzelm@20514
   324
  \medskip The \emph{hidden polymorphism} of a term @{text "t :: \<sigma>"}
wenzelm@20514
   325
  is the set of type variables occurring in @{text "t"}, but not in
wenzelm@34929
   326
  its type @{text "\<sigma>"}.  This means that the term implicitly depends
wenzelm@34929
   327
  on type arguments that are not accounted in the result type, i.e.\
wenzelm@34929
   328
  there are different type instances @{text "t\<vartheta> :: \<sigma>"} and
wenzelm@34929
   329
  @{text "t\<vartheta>' :: \<sigma>"} with the same type.  This slightly
wenzelm@20543
   330
  pathological situation notoriously demands additional care.
wenzelm@20514
   331
wenzelm@20514
   332
  \medskip A \emph{term abbreviation} is a syntactic definition @{text
wenzelm@20537
   333
  "c\<^isub>\<sigma> \<equiv> t"} of a closed term @{text "t"} of type @{text "\<sigma>"},
wenzelm@20537
   334
  without any hidden polymorphism.  A term abbreviation looks like a
wenzelm@20543
   335
  constant in the syntax, but is expanded before entering the logical
wenzelm@20543
   336
  core.  Abbreviations are usually reverted when printing terms, using
wenzelm@20543
   337
  @{text "t \<rightarrow> c\<^isub>\<sigma>"} as rules for higher-order rewriting.
wenzelm@20519
   338
wenzelm@20519
   339
  \medskip Canonical operations on @{text "\<lambda>"}-terms include @{text
wenzelm@20537
   340
  "\<alpha>\<beta>\<eta>"}-conversion: @{text "\<alpha>"}-conversion refers to capture-free
wenzelm@20519
   341
  renaming of bound variables; @{text "\<beta>"}-conversion contracts an
wenzelm@20537
   342
  abstraction applied to an argument term, substituting the argument
wenzelm@20519
   343
  in the body: @{text "(\<lambda>x. b)a"} becomes @{text "b[a/x]"}; @{text
wenzelm@20519
   344
  "\<eta>"}-conversion contracts vacuous application-abstraction: @{text
wenzelm@20519
   345
  "\<lambda>x. f x"} becomes @{text "f"}, provided that the bound variable
wenzelm@20537
   346
  does not occur in @{text "f"}.
wenzelm@20519
   347
wenzelm@20537
   348
  Terms are normally treated modulo @{text "\<alpha>"}-conversion, which is
wenzelm@20537
   349
  implicit in the de-Bruijn representation.  Names for bound variables
wenzelm@20537
   350
  in abstractions are maintained separately as (meaningless) comments,
wenzelm@20537
   351
  mostly for parsing and printing.  Full @{text "\<alpha>\<beta>\<eta>"}-conversion is
wenzelm@28784
   352
  commonplace in various standard operations (\secref{sec:obj-rules})
wenzelm@28784
   353
  that are based on higher-order unification and matching.
wenzelm@18537
   354
*}
wenzelm@18537
   355
wenzelm@20514
   356
text %mlref {*
wenzelm@20514
   357
  \begin{mldecls}
wenzelm@20514
   358
  @{index_ML_type term} \\
wenzelm@46262
   359
  @{index_ML_op "aconv": "term * term -> bool"} \\
wenzelm@39846
   360
  @{index_ML Term.map_types: "(typ -> typ) -> term -> term"} \\
wenzelm@39846
   361
  @{index_ML Term.fold_types: "(typ -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@39846
   362
  @{index_ML Term.map_aterms: "(term -> term) -> term -> term"} \\
wenzelm@39846
   363
  @{index_ML Term.fold_aterms: "(term -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@20547
   364
  \end{mldecls}
wenzelm@20547
   365
  \begin{mldecls}
wenzelm@20514
   366
  @{index_ML fastype_of: "term -> typ"} \\
wenzelm@20519
   367
  @{index_ML lambda: "term -> term -> term"} \\
wenzelm@20519
   368
  @{index_ML betapply: "term * term -> term"} \\
wenzelm@42934
   369
  @{index_ML incr_boundvars: "int -> term -> term"} \\
wenzelm@42401
   370
  @{index_ML Sign.declare_const: "Proof.context ->
wenzelm@42401
   371
  (binding * typ) * mixfix -> theory -> term * theory"} \\
haftmann@33174
   372
  @{index_ML Sign.add_abbrev: "string -> binding * term ->
wenzelm@24972
   373
  theory -> (term * term) * theory"} \\
wenzelm@20519
   374
  @{index_ML Sign.const_typargs: "theory -> string * typ -> typ list"} \\
wenzelm@20519
   375
  @{index_ML Sign.const_instance: "theory -> string * typ list -> typ"} \\
wenzelm@20514
   376
  \end{mldecls}
wenzelm@18537
   377
wenzelm@20514
   378
  \begin{description}
wenzelm@18537
   379
wenzelm@39864
   380
  \item Type @{ML_type term} represents de-Bruijn terms, with comments
wenzelm@39864
   381
  in abstractions, and explicitly named free variables and constants;
wenzelm@20537
   382
  this is a datatype with constructors @{ML Bound}, @{ML Free}, @{ML
wenzelm@46262
   383
  Var}, @{ML Const}, @{ML Abs}, @{ML_op "$"}.
wenzelm@20519
   384
wenzelm@36166
   385
  \item @{text "t"}~@{ML_text aconv}~@{text "u"} checks @{text
wenzelm@20519
   386
  "\<alpha>"}-equivalence of two terms.  This is the basic equality relation
wenzelm@20519
   387
  on type @{ML_type term}; raw datatype equality should only be used
wenzelm@20519
   388
  for operations related to parsing or printing!
wenzelm@20519
   389
wenzelm@39846
   390
  \item @{ML Term.map_types}~@{text "f t"} applies the mapping @{text
wenzelm@20537
   391
  "f"} to all types occurring in @{text "t"}.
wenzelm@20537
   392
wenzelm@39846
   393
  \item @{ML Term.fold_types}~@{text "f t"} iterates the operation
wenzelm@39846
   394
  @{text "f"} over all occurrences of types in @{text "t"}; the term
wenzelm@20537
   395
  structure is traversed from left to right.
wenzelm@20519
   396
wenzelm@39846
   397
  \item @{ML Term.map_aterms}~@{text "f t"} applies the mapping @{text
wenzelm@39846
   398
  "f"} to all atomic terms (@{ML Bound}, @{ML Free}, @{ML Var}, @{ML
wenzelm@20537
   399
  Const}) occurring in @{text "t"}.
wenzelm@20537
   400
wenzelm@39846
   401
  \item @{ML Term.fold_aterms}~@{text "f t"} iterates the operation
wenzelm@39846
   402
  @{text "f"} over all occurrences of atomic terms (@{ML Bound}, @{ML
wenzelm@39846
   403
  Free}, @{ML Var}, @{ML Const}) in @{text "t"}; the term structure is
wenzelm@20519
   404
  traversed from left to right.
wenzelm@20519
   405
wenzelm@20537
   406
  \item @{ML fastype_of}~@{text "t"} determines the type of a
wenzelm@20537
   407
  well-typed term.  This operation is relatively slow, despite the
wenzelm@20537
   408
  omission of any sanity checks.
wenzelm@20519
   409
wenzelm@20519
   410
  \item @{ML lambda}~@{text "a b"} produces an abstraction @{text
wenzelm@20537
   411
  "\<lambda>a. b"}, where occurrences of the atomic term @{text "a"} in the
wenzelm@20537
   412
  body @{text "b"} are replaced by bound variables.
wenzelm@20519
   413
wenzelm@20537
   414
  \item @{ML betapply}~@{text "(t, u)"} produces an application @{text
wenzelm@20537
   415
  "t u"}, with topmost @{text "\<beta>"}-conversion if @{text "t"} is an
wenzelm@20537
   416
  abstraction.
wenzelm@20519
   417
wenzelm@42934
   418
  \item @{ML incr_boundvars}~@{text "j"} increments a term's dangling
wenzelm@42934
   419
  bound variables by the offset @{text "j"}.  This is required when
wenzelm@42934
   420
  moving a subterm into a context where it is enclosed by a different
wenzelm@42934
   421
  number of abstractions.  Bound variables with a matching abstraction
wenzelm@42934
   422
  are unaffected.
wenzelm@42934
   423
wenzelm@42401
   424
  \item @{ML Sign.declare_const}~@{text "ctxt ((c, \<sigma>), mx)"} declares
wenzelm@42401
   425
  a new constant @{text "c :: \<sigma>"} with optional mixfix syntax.
wenzelm@20519
   426
haftmann@33174
   427
  \item @{ML Sign.add_abbrev}~@{text "print_mode (c, t)"}
wenzelm@21827
   428
  introduces a new term abbreviation @{text "c \<equiv> t"}.
wenzelm@20519
   429
wenzelm@20520
   430
  \item @{ML Sign.const_typargs}~@{text "thy (c, \<tau>)"} and @{ML
wenzelm@20520
   431
  Sign.const_instance}~@{text "thy (c, [\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n])"}
wenzelm@20543
   432
  convert between two representations of polymorphic constants: full
wenzelm@20543
   433
  type instance vs.\ compact type arguments form.
wenzelm@18537
   434
wenzelm@20514
   435
  \end{description}
wenzelm@18537
   436
*}
wenzelm@18537
   437
wenzelm@39832
   438
text %mlantiq {*
wenzelm@39832
   439
  \begin{matharray}{rcl}
wenzelm@39832
   440
  @{ML_antiquotation_def "const_name"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   441
  @{ML_antiquotation_def "const_abbrev"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   442
  @{ML_antiquotation_def "const"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   443
  @{ML_antiquotation_def "term"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   444
  @{ML_antiquotation_def "prop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   445
  \end{matharray}
wenzelm@39832
   446
wenzelm@42510
   447
  @{rail "
wenzelm@42510
   448
  (@@{ML_antiquotation const_name} |
wenzelm@42510
   449
   @@{ML_antiquotation const_abbrev}) nameref
wenzelm@39832
   450
  ;
wenzelm@42510
   451
  @@{ML_antiquotation const} ('(' (type + ',') ')')?
wenzelm@39832
   452
  ;
wenzelm@42510
   453
  @@{ML_antiquotation term} term
wenzelm@39832
   454
  ;
wenzelm@42510
   455
  @@{ML_antiquotation prop} prop
wenzelm@42510
   456
  "}
wenzelm@39832
   457
wenzelm@39832
   458
  \begin{description}
wenzelm@39832
   459
wenzelm@39832
   460
  \item @{text "@{const_name c}"} inlines the internalized logical
wenzelm@39832
   461
  constant name @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   462
wenzelm@39832
   463
  \item @{text "@{const_abbrev c}"} inlines the internalized
wenzelm@39832
   464
  abbreviated constant name @{text "c"} --- as @{ML_type string}
wenzelm@39832
   465
  literal.
wenzelm@39832
   466
wenzelm@39832
   467
  \item @{text "@{const c(\<^vec>\<tau>)}"} inlines the internalized
wenzelm@39832
   468
  constant @{text "c"} with precise type instantiation in the sense of
wenzelm@39832
   469
  @{ML Sign.const_instance} --- as @{ML Const} constructor term for
wenzelm@39832
   470
  datatype @{ML_type term}.
wenzelm@39832
   471
wenzelm@39832
   472
  \item @{text "@{term t}"} inlines the internalized term @{text "t"}
wenzelm@39832
   473
  --- as constructor term for datatype @{ML_type term}.
wenzelm@39832
   474
wenzelm@39832
   475
  \item @{text "@{prop \<phi>}"} inlines the internalized proposition
wenzelm@39832
   476
  @{text "\<phi>"} --- as constructor term for datatype @{ML_type term}.
wenzelm@39832
   477
wenzelm@39832
   478
  \end{description}
wenzelm@39832
   479
*}
wenzelm@39832
   480
wenzelm@18537
   481
wenzelm@20451
   482
section {* Theorems \label{sec:thms} *}
wenzelm@18537
   483
wenzelm@18537
   484
text {*
wenzelm@20543
   485
  A \emph{proposition} is a well-typed term of type @{text "prop"}, a
wenzelm@20521
   486
  \emph{theorem} is a proven proposition (depending on a context of
wenzelm@20521
   487
  hypotheses and the background theory).  Primitive inferences include
wenzelm@29774
   488
  plain Natural Deduction rules for the primary connectives @{text
wenzelm@20537
   489
  "\<And>"} and @{text "\<Longrightarrow>"} of the framework.  There is also a builtin
wenzelm@20537
   490
  notion of equality/equivalence @{text "\<equiv>"}.
wenzelm@20521
   491
*}
wenzelm@20521
   492
wenzelm@29758
   493
wenzelm@26872
   494
subsection {* Primitive connectives and rules \label{sec:prim-rules} *}
wenzelm@18537
   495
wenzelm@20521
   496
text {*
wenzelm@20543
   497
  The theory @{text "Pure"} contains constant declarations for the
wenzelm@20543
   498
  primitive connectives @{text "\<And>"}, @{text "\<Longrightarrow>"}, and @{text "\<equiv>"} of
wenzelm@20543
   499
  the logical framework, see \figref{fig:pure-connectives}.  The
wenzelm@20543
   500
  derivability judgment @{text "A\<^isub>1, \<dots>, A\<^isub>n \<turnstile> B"} is
wenzelm@20543
   501
  defined inductively by the primitive inferences given in
wenzelm@20543
   502
  \figref{fig:prim-rules}, with the global restriction that the
wenzelm@20543
   503
  hypotheses must \emph{not} contain any schematic variables.  The
wenzelm@20543
   504
  builtin equality is conceptually axiomatized as shown in
wenzelm@20521
   505
  \figref{fig:pure-equality}, although the implementation works
wenzelm@20543
   506
  directly with derived inferences.
wenzelm@20521
   507
wenzelm@20521
   508
  \begin{figure}[htb]
wenzelm@20521
   509
  \begin{center}
wenzelm@20501
   510
  \begin{tabular}{ll}
wenzelm@20501
   511
  @{text "all :: (\<alpha> \<Rightarrow> prop) \<Rightarrow> prop"} & universal quantification (binder @{text "\<And>"}) \\
wenzelm@20501
   512
  @{text "\<Longrightarrow> :: prop \<Rightarrow> prop \<Rightarrow> prop"} & implication (right associative infix) \\
wenzelm@20521
   513
  @{text "\<equiv> :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> prop"} & equality relation (infix) \\
wenzelm@20501
   514
  \end{tabular}
wenzelm@20537
   515
  \caption{Primitive connectives of Pure}\label{fig:pure-connectives}
wenzelm@20521
   516
  \end{center}
wenzelm@20521
   517
  \end{figure}
wenzelm@18537
   518
wenzelm@20501
   519
  \begin{figure}[htb]
wenzelm@20501
   520
  \begin{center}
wenzelm@20498
   521
  \[
wenzelm@20498
   522
  \infer[@{text "(axiom)"}]{@{text "\<turnstile> A"}}{@{text "A \<in> \<Theta>"}}
wenzelm@20498
   523
  \qquad
wenzelm@20498
   524
  \infer[@{text "(assume)"}]{@{text "A \<turnstile> A"}}{}
wenzelm@20498
   525
  \]
wenzelm@20498
   526
  \[
wenzelm@42666
   527
  \infer[@{text "(\<And>\<hyphen>intro)"}]{@{text "\<Gamma> \<turnstile> \<And>x. b[x]"}}{@{text "\<Gamma> \<turnstile> b[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   528
  \qquad
wenzelm@42666
   529
  \infer[@{text "(\<And>\<hyphen>elim)"}]{@{text "\<Gamma> \<turnstile> b[a]"}}{@{text "\<Gamma> \<turnstile> \<And>x. b[x]"}}
wenzelm@20498
   530
  \]
wenzelm@20498
   531
  \[
wenzelm@42666
   532
  \infer[@{text "(\<Longrightarrow>\<hyphen>intro)"}]{@{text "\<Gamma> - A \<turnstile> A \<Longrightarrow> B"}}{@{text "\<Gamma> \<turnstile> B"}}
wenzelm@20498
   533
  \qquad
wenzelm@42666
   534
  \infer[@{text "(\<Longrightarrow>\<hyphen>elim)"}]{@{text "\<Gamma>\<^sub>1 \<union> \<Gamma>\<^sub>2 \<turnstile> B"}}{@{text "\<Gamma>\<^sub>1 \<turnstile> A \<Longrightarrow> B"} & @{text "\<Gamma>\<^sub>2 \<turnstile> A"}}
wenzelm@20498
   535
  \]
wenzelm@20521
   536
  \caption{Primitive inferences of Pure}\label{fig:prim-rules}
wenzelm@20521
   537
  \end{center}
wenzelm@20521
   538
  \end{figure}
wenzelm@20521
   539
wenzelm@20521
   540
  \begin{figure}[htb]
wenzelm@20521
   541
  \begin{center}
wenzelm@20521
   542
  \begin{tabular}{ll}
wenzelm@20537
   543
  @{text "\<turnstile> (\<lambda>x. b[x]) a \<equiv> b[a]"} & @{text "\<beta>"}-conversion \\
wenzelm@20521
   544
  @{text "\<turnstile> x \<equiv> x"} & reflexivity \\
wenzelm@20521
   545
  @{text "\<turnstile> x \<equiv> y \<Longrightarrow> P x \<Longrightarrow> P y"} & substitution \\
wenzelm@20521
   546
  @{text "\<turnstile> (\<And>x. f x \<equiv> g x) \<Longrightarrow> f \<equiv> g"} & extensionality \\
wenzelm@20537
   547
  @{text "\<turnstile> (A \<Longrightarrow> B) \<Longrightarrow> (B \<Longrightarrow> A) \<Longrightarrow> A \<equiv> B"} & logical equivalence \\
wenzelm@20521
   548
  \end{tabular}
wenzelm@20542
   549
  \caption{Conceptual axiomatization of Pure equality}\label{fig:pure-equality}
wenzelm@20501
   550
  \end{center}
wenzelm@20501
   551
  \end{figure}
wenzelm@18537
   552
wenzelm@20501
   553
  The introduction and elimination rules for @{text "\<And>"} and @{text
wenzelm@20537
   554
  "\<Longrightarrow>"} are analogous to formation of dependently typed @{text
wenzelm@20501
   555
  "\<lambda>"}-terms representing the underlying proof objects.  Proof terms
wenzelm@20543
   556
  are irrelevant in the Pure logic, though; they cannot occur within
wenzelm@20543
   557
  propositions.  The system provides a runtime option to record
wenzelm@20537
   558
  explicit proof terms for primitive inferences.  Thus all three
wenzelm@20537
   559
  levels of @{text "\<lambda>"}-calculus become explicit: @{text "\<Rightarrow>"} for
wenzelm@20537
   560
  terms, and @{text "\<And>/\<Longrightarrow>"} for proofs (cf.\
wenzelm@20537
   561
  \cite{Berghofer-Nipkow:2000:TPHOL}).
wenzelm@20491
   562
wenzelm@34929
   563
  Observe that locally fixed parameters (as in @{text
wenzelm@42666
   564
  "\<And>\<hyphen>intro"}) need not be recorded in the hypotheses, because
wenzelm@34929
   565
  the simple syntactic types of Pure are always inhabitable.
wenzelm@34929
   566
  ``Assumptions'' @{text "x :: \<tau>"} for type-membership are only
wenzelm@34929
   567
  present as long as some @{text "x\<^isub>\<tau>"} occurs in the statement
wenzelm@34929
   568
  body.\footnote{This is the key difference to ``@{text "\<lambda>HOL"}'' in
wenzelm@34929
   569
  the PTS framework \cite{Barendregt-Geuvers:2001}, where hypotheses
wenzelm@34929
   570
  @{text "x : A"} are treated uniformly for propositions and types.}
wenzelm@20501
   571
wenzelm@20501
   572
  \medskip The axiomatization of a theory is implicitly closed by
wenzelm@20537
   573
  forming all instances of type and term variables: @{text "\<turnstile>
wenzelm@20537
   574
  A\<vartheta>"} holds for any substitution instance of an axiom
wenzelm@20543
   575
  @{text "\<turnstile> A"}.  By pushing substitutions through derivations
wenzelm@20543
   576
  inductively, we also get admissible @{text "generalize"} and @{text
wenzelm@34929
   577
  "instantiate"} rules as shown in \figref{fig:subst-rules}.
wenzelm@20501
   578
wenzelm@20501
   579
  \begin{figure}[htb]
wenzelm@20501
   580
  \begin{center}
wenzelm@20498
   581
  \[
wenzelm@20501
   582
  \infer{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}{@{text "\<Gamma> \<turnstile> B[\<alpha>]"} & @{text "\<alpha> \<notin> \<Gamma>"}}
wenzelm@20501
   583
  \quad
wenzelm@20501
   584
  \infer[\quad@{text "(generalize)"}]{@{text "\<Gamma> \<turnstile> B[?x]"}}{@{text "\<Gamma> \<turnstile> B[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   585
  \]
wenzelm@20498
   586
  \[
wenzelm@20501
   587
  \infer{@{text "\<Gamma> \<turnstile> B[\<tau>]"}}{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}
wenzelm@20501
   588
  \quad
wenzelm@20501
   589
  \infer[\quad@{text "(instantiate)"}]{@{text "\<Gamma> \<turnstile> B[t]"}}{@{text "\<Gamma> \<turnstile> B[?x]"}}
wenzelm@20498
   590
  \]
wenzelm@20501
   591
  \caption{Admissible substitution rules}\label{fig:subst-rules}
wenzelm@20501
   592
  \end{center}
wenzelm@20501
   593
  \end{figure}
wenzelm@18537
   594
wenzelm@20537
   595
  Note that @{text "instantiate"} does not require an explicit
wenzelm@20537
   596
  side-condition, because @{text "\<Gamma>"} may never contain schematic
wenzelm@20537
   597
  variables.
wenzelm@20537
   598
wenzelm@20537
   599
  In principle, variables could be substituted in hypotheses as well,
wenzelm@20543
   600
  but this would disrupt the monotonicity of reasoning: deriving
wenzelm@20543
   601
  @{text "\<Gamma>\<vartheta> \<turnstile> B\<vartheta>"} from @{text "\<Gamma> \<turnstile> B"} is
wenzelm@20543
   602
  correct, but @{text "\<Gamma>\<vartheta> \<supseteq> \<Gamma>"} does not necessarily hold:
wenzelm@20543
   603
  the result belongs to a different proof context.
wenzelm@20542
   604
wenzelm@20543
   605
  \medskip An \emph{oracle} is a function that produces axioms on the
wenzelm@20543
   606
  fly.  Logically, this is an instance of the @{text "axiom"} rule
wenzelm@20543
   607
  (\figref{fig:prim-rules}), but there is an operational difference.
wenzelm@20543
   608
  The system always records oracle invocations within derivations of
wenzelm@29768
   609
  theorems by a unique tag.
wenzelm@20542
   610
wenzelm@20542
   611
  Axiomatizations should be limited to the bare minimum, typically as
wenzelm@20542
   612
  part of the initial logical basis of an object-logic formalization.
wenzelm@20543
   613
  Later on, theories are usually developed in a strictly definitional
wenzelm@20543
   614
  fashion, by stating only certain equalities over new constants.
wenzelm@20542
   615
wenzelm@20542
   616
  A \emph{simple definition} consists of a constant declaration @{text
wenzelm@20543
   617
  "c :: \<sigma>"} together with an axiom @{text "\<turnstile> c \<equiv> t"}, where @{text "t
wenzelm@20543
   618
  :: \<sigma>"} is a closed term without any hidden polymorphism.  The RHS
wenzelm@20543
   619
  may depend on further defined constants, but not @{text "c"} itself.
wenzelm@20543
   620
  Definitions of functions may be presented as @{text "c \<^vec>x \<equiv>
wenzelm@20543
   621
  t"} instead of the puristic @{text "c \<equiv> \<lambda>\<^vec>x. t"}.
wenzelm@20542
   622
wenzelm@20543
   623
  An \emph{overloaded definition} consists of a collection of axioms
wenzelm@20543
   624
  for the same constant, with zero or one equations @{text
wenzelm@20543
   625
  "c((\<^vec>\<alpha>)\<kappa>) \<equiv> t"} for each type constructor @{text "\<kappa>"} (for
wenzelm@20543
   626
  distinct variables @{text "\<^vec>\<alpha>"}).  The RHS may mention
wenzelm@20543
   627
  previously defined constants as above, or arbitrary constants @{text
wenzelm@20543
   628
  "d(\<alpha>\<^isub>i)"} for some @{text "\<alpha>\<^isub>i"} projected from @{text
wenzelm@20543
   629
  "\<^vec>\<alpha>"}.  Thus overloaded definitions essentially work by
wenzelm@20543
   630
  primitive recursion over the syntactic structure of a single type
wenzelm@39840
   631
  argument.  See also \cite[\S4.3]{Haftmann-Wenzel:2006:classes}.
wenzelm@20521
   632
*}
wenzelm@20498
   633
wenzelm@20521
   634
text %mlref {*
wenzelm@20521
   635
  \begin{mldecls}
wenzelm@46253
   636
  @{index_ML Logic.all: "term -> term -> term"} \\
wenzelm@46253
   637
  @{index_ML Logic.mk_implies: "term * term -> term"} \\
wenzelm@46253
   638
  \end{mldecls}
wenzelm@46253
   639
  \begin{mldecls}
wenzelm@20521
   640
  @{index_ML_type ctyp} \\
wenzelm@20521
   641
  @{index_ML_type cterm} \\
wenzelm@20547
   642
  @{index_ML Thm.ctyp_of: "theory -> typ -> ctyp"} \\
wenzelm@20547
   643
  @{index_ML Thm.cterm_of: "theory -> term -> cterm"} \\
wenzelm@46253
   644
  @{index_ML Thm.capply: "cterm -> cterm -> cterm"} \\
wenzelm@46253
   645
  @{index_ML Thm.cabs: "cterm -> cterm -> cterm"} \\
wenzelm@46253
   646
  @{index_ML Thm.all: "cterm -> cterm -> cterm"} \\
wenzelm@46253
   647
  @{index_ML Drule.mk_implies: "cterm * cterm -> cterm"} \\
wenzelm@20547
   648
  \end{mldecls}
wenzelm@20547
   649
  \begin{mldecls}
wenzelm@20521
   650
  @{index_ML_type thm} \\
wenzelm@32833
   651
  @{index_ML proofs: "int Unsynchronized.ref"} \\
wenzelm@42933
   652
  @{index_ML Thm.transfer: "theory -> thm -> thm"} \\
wenzelm@20542
   653
  @{index_ML Thm.assume: "cterm -> thm"} \\
wenzelm@20542
   654
  @{index_ML Thm.forall_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   655
  @{index_ML Thm.forall_elim: "cterm -> thm -> thm"} \\
wenzelm@20542
   656
  @{index_ML Thm.implies_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   657
  @{index_ML Thm.implies_elim: "thm -> thm -> thm"} \\
wenzelm@20542
   658
  @{index_ML Thm.generalize: "string list * string list -> int -> thm -> thm"} \\
wenzelm@20542
   659
  @{index_ML Thm.instantiate: "(ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm"} \\
wenzelm@42401
   660
  @{index_ML Thm.add_axiom: "Proof.context ->
wenzelm@42401
   661
  binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@39821
   662
  @{index_ML Thm.add_oracle: "binding * ('a -> cterm) -> theory ->
wenzelm@39821
   663
  (string * ('a -> thm)) * theory"} \\
wenzelm@42401
   664
  @{index_ML Thm.add_def: "Proof.context -> bool -> bool ->
wenzelm@42401
   665
  binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@20547
   666
  \end{mldecls}
wenzelm@20547
   667
  \begin{mldecls}
wenzelm@42401
   668
  @{index_ML Theory.add_deps: "Proof.context -> string ->
wenzelm@42401
   669
  string * typ -> (string * typ) list -> theory -> theory"} \\
wenzelm@20521
   670
  \end{mldecls}
wenzelm@20521
   671
wenzelm@20521
   672
  \begin{description}
wenzelm@20521
   673
wenzelm@46253
   674
  \item @{ML Logic.all}~@{text "a B"} produces a Pure quantification
wenzelm@46253
   675
  @{text "\<And>a. B"}, where occurrences of the atomic term @{text "a"} in
wenzelm@46253
   676
  the body proposition @{text "B"} are replaced by bound variables.
wenzelm@46253
   677
  (See also @{ML lambda} on terms.)
wenzelm@46253
   678
wenzelm@46253
   679
  \item @{ML Logic.mk_implies}~@{text "(A, B)"} produces a Pure
wenzelm@46253
   680
  implication @{text "A \<Longrightarrow> B"}.
wenzelm@46253
   681
wenzelm@39864
   682
  \item Types @{ML_type ctyp} and @{ML_type cterm} represent certified
wenzelm@39864
   683
  types and terms, respectively.  These are abstract datatypes that
wenzelm@20542
   684
  guarantee that its values have passed the full well-formedness (and
wenzelm@20542
   685
  well-typedness) checks, relative to the declarations of type
wenzelm@46253
   686
  constructors, constants etc.\ in the background theory.  The
wenzelm@46253
   687
  abstract types @{ML_type ctyp} and @{ML_type cterm} are part of the
wenzelm@46253
   688
  same inference kernel that is mainly responsible for @{ML_type thm}.
wenzelm@46253
   689
  Thus syntactic operations on @{ML_type ctyp} and @{ML_type cterm}
wenzelm@46253
   690
  are located in the @{ML_struct Thm} module, even though theorems are
wenzelm@46253
   691
  not yet involved at that stage.
wenzelm@20542
   692
wenzelm@29768
   693
  \item @{ML Thm.ctyp_of}~@{text "thy \<tau>"} and @{ML
wenzelm@29768
   694
  Thm.cterm_of}~@{text "thy t"} explicitly checks types and terms,
wenzelm@29768
   695
  respectively.  This also involves some basic normalizations, such
wenzelm@29768
   696
  expansion of type and term abbreviations from the theory context.
wenzelm@46253
   697
  Full re-certification is relatively slow and should be avoided in
wenzelm@46253
   698
  tight reasoning loops.
wenzelm@20547
   699
wenzelm@46253
   700
  \item @{ML Thm.capply}, @{ML Thm.cabs}, @{ML Thm.all}, @{ML
wenzelm@46253
   701
  Drule.mk_implies} etc.\ compose certified terms (or propositions)
wenzelm@46253
   702
  incrementally.  This is equivalent to @{ML Thm.cterm_of} after
wenzelm@46262
   703
  unchecked @{ML_op "$"}, @{ML lambda}, @{ML Logic.all}, @{ML
wenzelm@46253
   704
  Logic.mk_implies} etc., but there can be a big difference in
wenzelm@46253
   705
  performance when large existing entities are composed by a few extra
wenzelm@46253
   706
  constructions on top.  There are separate operations to decompose
wenzelm@46253
   707
  certified terms and theorems to produce certified terms again.
wenzelm@20542
   708
wenzelm@39864
   709
  \item Type @{ML_type thm} represents proven propositions.  This is
wenzelm@39864
   710
  an abstract datatype that guarantees that its values have been
wenzelm@20542
   711
  constructed by basic principles of the @{ML_struct Thm} module.
wenzelm@39281
   712
  Every @{ML_type thm} value contains a sliding back-reference to the
wenzelm@20543
   713
  enclosing theory, cf.\ \secref{sec:context-theory}.
wenzelm@20542
   714
wenzelm@34929
   715
  \item @{ML proofs} specifies the detail of proof recording within
wenzelm@29768
   716
  @{ML_type thm} values: @{ML 0} records only the names of oracles,
wenzelm@29768
   717
  @{ML 1} records oracle names and propositions, @{ML 2} additionally
wenzelm@29768
   718
  records full proof terms.  Officially named theorems that contribute
wenzelm@34929
   719
  to a result are recorded in any case.
wenzelm@20542
   720
wenzelm@42933
   721
  \item @{ML Thm.transfer}~@{text "thy thm"} transfers the given
wenzelm@42933
   722
  theorem to a \emph{larger} theory, see also \secref{sec:context}.
wenzelm@42933
   723
  This formal adjustment of the background context has no logical
wenzelm@42933
   724
  significance, but is occasionally required for formal reasons, e.g.\
wenzelm@42933
   725
  when theorems that are imported from more basic theories are used in
wenzelm@42933
   726
  the current situation.
wenzelm@42933
   727
wenzelm@20542
   728
  \item @{ML Thm.assume}, @{ML Thm.forall_intr}, @{ML
wenzelm@20542
   729
  Thm.forall_elim}, @{ML Thm.implies_intr}, and @{ML Thm.implies_elim}
wenzelm@20542
   730
  correspond to the primitive inferences of \figref{fig:prim-rules}.
wenzelm@20542
   731
wenzelm@20542
   732
  \item @{ML Thm.generalize}~@{text "(\<^vec>\<alpha>, \<^vec>x)"}
wenzelm@20542
   733
  corresponds to the @{text "generalize"} rules of
wenzelm@20543
   734
  \figref{fig:subst-rules}.  Here collections of type and term
wenzelm@20543
   735
  variables are generalized simultaneously, specified by the given
wenzelm@20543
   736
  basic names.
wenzelm@20521
   737
wenzelm@20542
   738
  \item @{ML Thm.instantiate}~@{text "(\<^vec>\<alpha>\<^isub>s,
wenzelm@20542
   739
  \<^vec>x\<^isub>\<tau>)"} corresponds to the @{text "instantiate"} rules
wenzelm@20542
   740
  of \figref{fig:subst-rules}.  Type variables are substituted before
wenzelm@20542
   741
  term variables.  Note that the types in @{text "\<^vec>x\<^isub>\<tau>"}
wenzelm@20542
   742
  refer to the instantiated versions.
wenzelm@20542
   743
wenzelm@42401
   744
  \item @{ML Thm.add_axiom}~@{text "ctxt (name, A)"} declares an
wenzelm@35927
   745
  arbitrary proposition as axiom, and retrieves it as a theorem from
wenzelm@35927
   746
  the resulting theory, cf.\ @{text "axiom"} in
wenzelm@35927
   747
  \figref{fig:prim-rules}.  Note that the low-level representation in
wenzelm@35927
   748
  the axiom table may differ slightly from the returned theorem.
wenzelm@20542
   749
wenzelm@30288
   750
  \item @{ML Thm.add_oracle}~@{text "(binding, oracle)"} produces a named
wenzelm@28290
   751
  oracle rule, essentially generating arbitrary axioms on the fly,
wenzelm@28290
   752
  cf.\ @{text "axiom"} in \figref{fig:prim-rules}.
wenzelm@20521
   753
wenzelm@42401
   754
  \item @{ML Thm.add_def}~@{text "ctxt unchecked overloaded (name, c
wenzelm@35927
   755
  \<^vec>x \<equiv> t)"} states a definitional axiom for an existing constant
wenzelm@35927
   756
  @{text "c"}.  Dependencies are recorded via @{ML Theory.add_deps},
wenzelm@35927
   757
  unless the @{text "unchecked"} option is set.  Note that the
wenzelm@35927
   758
  low-level representation in the axiom table may differ slightly from
wenzelm@35927
   759
  the returned theorem.
wenzelm@20542
   760
wenzelm@42401
   761
  \item @{ML Theory.add_deps}~@{text "ctxt name c\<^isub>\<tau> \<^vec>d\<^isub>\<sigma>"}
wenzelm@42401
   762
  declares dependencies of a named specification for constant @{text
wenzelm@42401
   763
  "c\<^isub>\<tau>"}, relative to existing specifications for constants @{text
wenzelm@42401
   764
  "\<^vec>d\<^isub>\<sigma>"}.
wenzelm@20542
   765
wenzelm@20521
   766
  \end{description}
wenzelm@20521
   767
*}
wenzelm@20521
   768
wenzelm@20521
   769
wenzelm@39832
   770
text %mlantiq {*
wenzelm@39832
   771
  \begin{matharray}{rcl}
wenzelm@39832
   772
  @{ML_antiquotation_def "ctyp"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   773
  @{ML_antiquotation_def "cterm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   774
  @{ML_antiquotation_def "cprop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   775
  @{ML_antiquotation_def "thm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   776
  @{ML_antiquotation_def "thms"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   777
  @{ML_antiquotation_def "lemma"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   778
  \end{matharray}
wenzelm@39832
   779
wenzelm@42510
   780
  @{rail "
wenzelm@42510
   781
  @@{ML_antiquotation ctyp} typ
wenzelm@39832
   782
  ;
wenzelm@42510
   783
  @@{ML_antiquotation cterm} term
wenzelm@39832
   784
  ;
wenzelm@42510
   785
  @@{ML_antiquotation cprop} prop
wenzelm@39832
   786
  ;
wenzelm@42510
   787
  @@{ML_antiquotation thm} thmref
wenzelm@42510
   788
  ;
wenzelm@42510
   789
  @@{ML_antiquotation thms} thmrefs
wenzelm@39832
   790
  ;
wenzelm@42517
   791
  @@{ML_antiquotation lemma} ('(' @'open' ')')? ((prop +) + @'and') \\
wenzelm@42517
   792
    @'by' method method?
wenzelm@42510
   793
  "}
wenzelm@39832
   794
wenzelm@39832
   795
  \begin{description}
wenzelm@39832
   796
wenzelm@39832
   797
  \item @{text "@{ctyp \<tau>}"} produces a certified type wrt.\ the
wenzelm@39832
   798
  current background theory --- as abstract value of type @{ML_type
wenzelm@39832
   799
  ctyp}.
wenzelm@39832
   800
wenzelm@39832
   801
  \item @{text "@{cterm t}"} and @{text "@{cprop \<phi>}"} produce a
wenzelm@39832
   802
  certified term wrt.\ the current background theory --- as abstract
wenzelm@39832
   803
  value of type @{ML_type cterm}.
wenzelm@39832
   804
wenzelm@39832
   805
  \item @{text "@{thm a}"} produces a singleton fact --- as abstract
wenzelm@39832
   806
  value of type @{ML_type thm}.
wenzelm@39832
   807
wenzelm@39832
   808
  \item @{text "@{thms a}"} produces a general fact --- as abstract
wenzelm@39832
   809
  value of type @{ML_type "thm list"}.
wenzelm@39832
   810
wenzelm@39832
   811
  \item @{text "@{lemma \<phi> by meth}"} produces a fact that is proven on
wenzelm@39832
   812
  the spot according to the minimal proof, which imitates a terminal
wenzelm@39832
   813
  Isar proof.  The result is an abstract value of type @{ML_type thm}
wenzelm@39832
   814
  or @{ML_type "thm list"}, depending on the number of propositions
wenzelm@39832
   815
  given here.
wenzelm@39832
   816
wenzelm@39832
   817
  The internal derivation object lacks a proper theorem name, but it
wenzelm@39832
   818
  is formally closed, unless the @{text "(open)"} option is specified
wenzelm@39832
   819
  (this may impact performance of applications with proof terms).
wenzelm@39832
   820
wenzelm@39832
   821
  Since ML antiquotations are always evaluated at compile-time, there
wenzelm@39832
   822
  is no run-time overhead even for non-trivial proofs.  Nonetheless,
wenzelm@39832
   823
  the justification is syntactically limited to a single @{command
wenzelm@39832
   824
  "by"} step.  More complex Isar proofs should be done in regular
wenzelm@39832
   825
  theory source, before compiling the corresponding ML text that uses
wenzelm@39832
   826
  the result.
wenzelm@39832
   827
wenzelm@39832
   828
  \end{description}
wenzelm@39832
   829
wenzelm@39832
   830
*}
wenzelm@39832
   831
wenzelm@39832
   832
wenzelm@46254
   833
subsection {* Auxiliary connectives \label{sec:logic-aux} *}
wenzelm@20521
   834
wenzelm@46254
   835
text {* Theory @{text "Pure"} provides a few auxiliary connectives
wenzelm@46254
   836
  that are defined on top of the primitive ones, see
wenzelm@46254
   837
  \figref{fig:pure-aux}.  These special constants are useful in
wenzelm@46254
   838
  certain internal encodings, and are normally not directly exposed to
wenzelm@46254
   839
  the user.
wenzelm@20501
   840
wenzelm@20501
   841
  \begin{figure}[htb]
wenzelm@20501
   842
  \begin{center}
wenzelm@20498
   843
  \begin{tabular}{ll}
wenzelm@34929
   844
  @{text "conjunction :: prop \<Rightarrow> prop \<Rightarrow> prop"} & (infix @{text "&&&"}) \\
wenzelm@34929
   845
  @{text "\<turnstile> A &&& B \<equiv> (\<And>C. (A \<Longrightarrow> B \<Longrightarrow> C) \<Longrightarrow> C)"} \\[1ex]
wenzelm@20543
   846
  @{text "prop :: prop \<Rightarrow> prop"} & (prefix @{text "#"}, suppressed) \\
wenzelm@20521
   847
  @{text "#A \<equiv> A"} \\[1ex]
wenzelm@20521
   848
  @{text "term :: \<alpha> \<Rightarrow> prop"} & (prefix @{text "TERM"}) \\
wenzelm@20521
   849
  @{text "term x \<equiv> (\<And>A. A \<Longrightarrow> A)"} \\[1ex]
wenzelm@20521
   850
  @{text "TYPE :: \<alpha> itself"} & (prefix @{text "TYPE"}) \\
wenzelm@20521
   851
  @{text "(unspecified)"} \\
wenzelm@20498
   852
  \end{tabular}
wenzelm@20521
   853
  \caption{Definitions of auxiliary connectives}\label{fig:pure-aux}
wenzelm@20501
   854
  \end{center}
wenzelm@20501
   855
  \end{figure}
wenzelm@20501
   856
wenzelm@34929
   857
  The introduction @{text "A \<Longrightarrow> B \<Longrightarrow> A &&& B"}, and eliminations
wenzelm@34929
   858
  (projections) @{text "A &&& B \<Longrightarrow> A"} and @{text "A &&& B \<Longrightarrow> B"} are
wenzelm@34929
   859
  available as derived rules.  Conjunction allows to treat
wenzelm@34929
   860
  simultaneous assumptions and conclusions uniformly, e.g.\ consider
wenzelm@34929
   861
  @{text "A \<Longrightarrow> B \<Longrightarrow> C &&& D"}.  In particular, the goal mechanism
wenzelm@34929
   862
  represents multiple claims as explicit conjunction internally, but
wenzelm@34929
   863
  this is refined (via backwards introduction) into separate sub-goals
wenzelm@34929
   864
  before the user commences the proof; the final result is projected
wenzelm@34929
   865
  into a list of theorems using eliminations (cf.\
wenzelm@20537
   866
  \secref{sec:tactical-goals}).
wenzelm@20498
   867
wenzelm@20537
   868
  The @{text "prop"} marker (@{text "#"}) makes arbitrarily complex
wenzelm@20537
   869
  propositions appear as atomic, without changing the meaning: @{text
wenzelm@20537
   870
  "\<Gamma> \<turnstile> A"} and @{text "\<Gamma> \<turnstile> #A"} are interchangeable.  See
wenzelm@20537
   871
  \secref{sec:tactical-goals} for specific operations.
wenzelm@20521
   872
wenzelm@20543
   873
  The @{text "term"} marker turns any well-typed term into a derivable
wenzelm@20543
   874
  proposition: @{text "\<turnstile> TERM t"} holds unconditionally.  Although
wenzelm@20543
   875
  this is logically vacuous, it allows to treat terms and proofs
wenzelm@20543
   876
  uniformly, similar to a type-theoretic framework.
wenzelm@20498
   877
wenzelm@20537
   878
  The @{text "TYPE"} constructor is the canonical representative of
wenzelm@20537
   879
  the unspecified type @{text "\<alpha> itself"}; it essentially injects the
wenzelm@20537
   880
  language of types into that of terms.  There is specific notation
wenzelm@20537
   881
  @{text "TYPE(\<tau>)"} for @{text "TYPE\<^bsub>\<tau>
wenzelm@20521
   882
 itself\<^esub>"}.
wenzelm@34929
   883
  Although being devoid of any particular meaning, the term @{text
wenzelm@20537
   884
  "TYPE(\<tau>)"} accounts for the type @{text "\<tau>"} within the term
wenzelm@20537
   885
  language.  In particular, @{text "TYPE(\<alpha>)"} may be used as formal
wenzelm@20537
   886
  argument in primitive definitions, in order to circumvent hidden
wenzelm@20537
   887
  polymorphism (cf.\ \secref{sec:terms}).  For example, @{text "c
wenzelm@20537
   888
  TYPE(\<alpha>) \<equiv> A[\<alpha>]"} defines @{text "c :: \<alpha> itself \<Rightarrow> prop"} in terms of
wenzelm@20537
   889
  a proposition @{text "A"} that depends on an additional type
wenzelm@20537
   890
  argument, which is essentially a predicate on types.
wenzelm@20521
   891
*}
wenzelm@20501
   892
wenzelm@20521
   893
text %mlref {*
wenzelm@20521
   894
  \begin{mldecls}
wenzelm@20521
   895
  @{index_ML Conjunction.intr: "thm -> thm -> thm"} \\
wenzelm@20521
   896
  @{index_ML Conjunction.elim: "thm -> thm * thm"} \\
wenzelm@20521
   897
  @{index_ML Drule.mk_term: "cterm -> thm"} \\
wenzelm@20521
   898
  @{index_ML Drule.dest_term: "thm -> cterm"} \\
wenzelm@20521
   899
  @{index_ML Logic.mk_type: "typ -> term"} \\
wenzelm@20521
   900
  @{index_ML Logic.dest_type: "term -> typ"} \\
wenzelm@20521
   901
  \end{mldecls}
wenzelm@20521
   902
wenzelm@20521
   903
  \begin{description}
wenzelm@20521
   904
wenzelm@34929
   905
  \item @{ML Conjunction.intr} derives @{text "A &&& B"} from @{text
wenzelm@20542
   906
  "A"} and @{text "B"}.
wenzelm@20542
   907
wenzelm@20543
   908
  \item @{ML Conjunction.elim} derives @{text "A"} and @{text "B"}
wenzelm@34929
   909
  from @{text "A &&& B"}.
wenzelm@20542
   910
wenzelm@20543
   911
  \item @{ML Drule.mk_term} derives @{text "TERM t"}.
wenzelm@20542
   912
wenzelm@20543
   913
  \item @{ML Drule.dest_term} recovers term @{text "t"} from @{text
wenzelm@20543
   914
  "TERM t"}.
wenzelm@20542
   915
wenzelm@20542
   916
  \item @{ML Logic.mk_type}~@{text "\<tau>"} produces the term @{text
wenzelm@20542
   917
  "TYPE(\<tau>)"}.
wenzelm@20542
   918
wenzelm@20542
   919
  \item @{ML Logic.dest_type}~@{text "TYPE(\<tau>)"} recovers the type
wenzelm@20542
   920
  @{text "\<tau>"}.
wenzelm@20521
   921
wenzelm@20521
   922
  \end{description}
wenzelm@20491
   923
*}
wenzelm@18537
   924
wenzelm@20480
   925
wenzelm@28784
   926
section {* Object-level rules \label{sec:obj-rules} *}
wenzelm@18537
   927
wenzelm@29768
   928
text {*
wenzelm@29768
   929
  The primitive inferences covered so far mostly serve foundational
wenzelm@29768
   930
  purposes.  User-level reasoning usually works via object-level rules
wenzelm@29768
   931
  that are represented as theorems of Pure.  Composition of rules
wenzelm@29771
   932
  involves \emph{backchaining}, \emph{higher-order unification} modulo
wenzelm@29771
   933
  @{text "\<alpha>\<beta>\<eta>"}-conversion of @{text "\<lambda>"}-terms, and so-called
wenzelm@29771
   934
  \emph{lifting} of rules into a context of @{text "\<And>"} and @{text
wenzelm@29774
   935
  "\<Longrightarrow>"} connectives.  Thus the full power of higher-order Natural
wenzelm@29774
   936
  Deduction in Isabelle/Pure becomes readily available.
wenzelm@29769
   937
*}
wenzelm@20491
   938
wenzelm@29769
   939
wenzelm@29769
   940
subsection {* Hereditary Harrop Formulae *}
wenzelm@29769
   941
wenzelm@29769
   942
text {*
wenzelm@29768
   943
  The idea of object-level rules is to model Natural Deduction
wenzelm@29768
   944
  inferences in the style of Gentzen \cite{Gentzen:1935}, but we allow
wenzelm@29768
   945
  arbitrary nesting similar to \cite{extensions91}.  The most basic
wenzelm@29768
   946
  rule format is that of a \emph{Horn Clause}:
wenzelm@29768
   947
  \[
wenzelm@29768
   948
  \infer{@{text "A"}}{@{text "A\<^sub>1"} & @{text "\<dots>"} & @{text "A\<^sub>n"}}
wenzelm@29768
   949
  \]
wenzelm@29768
   950
  where @{text "A, A\<^sub>1, \<dots>, A\<^sub>n"} are atomic propositions
wenzelm@29768
   951
  of the framework, usually of the form @{text "Trueprop B"}, where
wenzelm@29768
   952
  @{text "B"} is a (compound) object-level statement.  This
wenzelm@29768
   953
  object-level inference corresponds to an iterated implication in
wenzelm@29768
   954
  Pure like this:
wenzelm@29768
   955
  \[
wenzelm@29768
   956
  @{text "A\<^sub>1 \<Longrightarrow> \<dots> A\<^sub>n \<Longrightarrow> A"}
wenzelm@29768
   957
  \]
wenzelm@29769
   958
  As an example consider conjunction introduction: @{text "A \<Longrightarrow> B \<Longrightarrow> A \<and>
wenzelm@29769
   959
  B"}.  Any parameters occurring in such rule statements are
wenzelm@29769
   960
  conceptionally treated as arbitrary:
wenzelm@29768
   961
  \[
wenzelm@29769
   962
  @{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
   963
  \]
wenzelm@20491
   964
wenzelm@29769
   965
  Nesting of rules means that the positions of @{text "A\<^sub>i"} may
wenzelm@29770
   966
  again hold compound rules, not just atomic propositions.
wenzelm@29769
   967
  Propositions of this format are called \emph{Hereditary Harrop
wenzelm@29769
   968
  Formulae} in the literature \cite{Miller:1991}.  Here we give an
wenzelm@29769
   969
  inductive characterization as follows:
wenzelm@29768
   970
wenzelm@29768
   971
  \medskip
wenzelm@29768
   972
  \begin{tabular}{ll}
wenzelm@29768
   973
  @{text "\<^bold>x"} & set of variables \\
wenzelm@29768
   974
  @{text "\<^bold>A"} & set of atomic propositions \\
wenzelm@29768
   975
  @{text "\<^bold>H  =  \<And>\<^bold>x\<^sup>*. \<^bold>H\<^sup>* \<Longrightarrow> \<^bold>A"} & set of Hereditary Harrop Formulas \\
wenzelm@29768
   976
  \end{tabular}
wenzelm@29768
   977
  \medskip
wenzelm@29768
   978
wenzelm@39861
   979
  Thus we essentially impose nesting levels on propositions formed
wenzelm@39861
   980
  from @{text "\<And>"} and @{text "\<Longrightarrow>"}.  At each level there is a prefix
wenzelm@39861
   981
  of parameters and compound premises, concluding an atomic
wenzelm@29770
   982
  proposition.  Typical examples are @{text "\<longrightarrow>"}-introduction @{text
wenzelm@29770
   983
  "(A \<Longrightarrow> B) \<Longrightarrow> A \<longrightarrow> B"} or mathematical induction @{text "P 0 \<Longrightarrow> (\<And>n. P n
wenzelm@29770
   984
  \<Longrightarrow> P (Suc n)) \<Longrightarrow> P n"}.  Even deeper nesting occurs in well-founded
wenzelm@29770
   985
  induction @{text "(\<And>x. (\<And>y. y \<prec> x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P x"}, but this
wenzelm@34929
   986
  already marks the limit of rule complexity that is usually seen in
wenzelm@34929
   987
  practice.
wenzelm@29769
   988
wenzelm@29770
   989
  \medskip Regular user-level inferences in Isabelle/Pure always
wenzelm@29770
   990
  maintain the following canonical form of results:
wenzelm@29769
   991
wenzelm@29769
   992
  \begin{itemize}
wenzelm@29768
   993
wenzelm@29774
   994
  \item Normalization by @{text "(A \<Longrightarrow> (\<And>x. B x)) \<equiv> (\<And>x. A \<Longrightarrow> B x)"},
wenzelm@29774
   995
  which is a theorem of Pure, means that quantifiers are pushed in
wenzelm@29774
   996
  front of implication at each level of nesting.  The normal form is a
wenzelm@29774
   997
  Hereditary Harrop Formula.
wenzelm@29769
   998
wenzelm@29769
   999
  \item The outermost prefix of parameters is represented via
wenzelm@29770
  1000
  schematic variables: instead of @{text "\<And>\<^vec>x. \<^vec>H \<^vec>x
wenzelm@29774
  1001
  \<Longrightarrow> A \<^vec>x"} we have @{text "\<^vec>H ?\<^vec>x \<Longrightarrow> A ?\<^vec>x"}.
wenzelm@29774
  1002
  Note that this representation looses information about the order of
wenzelm@29774
  1003
  parameters, and vacuous quantifiers vanish automatically.
wenzelm@29769
  1004
wenzelm@29769
  1005
  \end{itemize}
wenzelm@29769
  1006
*}
wenzelm@29769
  1007
wenzelm@29771
  1008
text %mlref {*
wenzelm@29771
  1009
  \begin{mldecls}
wenzelm@30552
  1010
  @{index_ML Simplifier.norm_hhf: "thm -> thm"} \\
wenzelm@29771
  1011
  \end{mldecls}
wenzelm@29771
  1012
wenzelm@29771
  1013
  \begin{description}
wenzelm@29771
  1014
wenzelm@30552
  1015
  \item @{ML Simplifier.norm_hhf}~@{text thm} normalizes the given
wenzelm@29771
  1016
  theorem according to the canonical form specified above.  This is
wenzelm@29771
  1017
  occasionally helpful to repair some low-level tools that do not
wenzelm@29771
  1018
  handle Hereditary Harrop Formulae properly.
wenzelm@29771
  1019
wenzelm@29771
  1020
  \end{description}
wenzelm@29771
  1021
*}
wenzelm@29771
  1022
wenzelm@29769
  1023
wenzelm@29769
  1024
subsection {* Rule composition *}
wenzelm@29769
  1025
wenzelm@29769
  1026
text {*
wenzelm@29771
  1027
  The rule calculus of Isabelle/Pure provides two main inferences:
wenzelm@29771
  1028
  @{inference resolution} (i.e.\ back-chaining of rules) and
wenzelm@29771
  1029
  @{inference assumption} (i.e.\ closing a branch), both modulo
wenzelm@29771
  1030
  higher-order unification.  There are also combined variants, notably
wenzelm@29771
  1031
  @{inference elim_resolution} and @{inference dest_resolution}.
wenzelm@20491
  1032
wenzelm@29771
  1033
  To understand the all-important @{inference resolution} principle,
wenzelm@29771
  1034
  we first consider raw @{inference_def composition} (modulo
wenzelm@29771
  1035
  higher-order unification with substitution @{text "\<vartheta>"}):
wenzelm@20498
  1036
  \[
wenzelm@29771
  1037
  \infer[(@{inference_def composition})]{@{text "\<^vec>A\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
  1038
  {@{text "\<^vec>A \<Longrightarrow> B"} & @{text "B' \<Longrightarrow> C"} & @{text "B\<vartheta> = B'\<vartheta>"}}
wenzelm@20498
  1039
  \]
wenzelm@29771
  1040
  Here the conclusion of the first rule is unified with the premise of
wenzelm@29771
  1041
  the second; the resulting rule instance inherits the premises of the
wenzelm@29771
  1042
  first and conclusion of the second.  Note that @{text "C"} can again
wenzelm@29771
  1043
  consist of iterated implications.  We can also permute the premises
wenzelm@29771
  1044
  of the second rule back-and-forth in order to compose with @{text
wenzelm@29771
  1045
  "B'"} in any position (subsequently we shall always refer to
wenzelm@29771
  1046
  position 1 w.l.o.g.).
wenzelm@20498
  1047
wenzelm@29774
  1048
  In @{inference composition} the internal structure of the common
wenzelm@29774
  1049
  part @{text "B"} and @{text "B'"} is not taken into account.  For
wenzelm@29774
  1050
  proper @{inference resolution} we require @{text "B"} to be atomic,
wenzelm@29774
  1051
  and explicitly observe the structure @{text "\<And>\<^vec>x. \<^vec>H
wenzelm@29774
  1052
  \<^vec>x \<Longrightarrow> B' \<^vec>x"} of the premise of the second rule.  The
wenzelm@29774
  1053
  idea is to adapt the first rule by ``lifting'' it into this context,
wenzelm@29774
  1054
  by means of iterated application of the following inferences:
wenzelm@20498
  1055
  \[
wenzelm@29771
  1056
  \infer[(@{inference_def imp_lift})]{@{text "(\<^vec>H \<Longrightarrow> \<^vec>A) \<Longrightarrow> (\<^vec>H \<Longrightarrow> B)"}}{@{text "\<^vec>A \<Longrightarrow> B"}}
wenzelm@20498
  1057
  \]
wenzelm@20498
  1058
  \[
wenzelm@29771
  1059
  \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
  1060
  \]
wenzelm@29771
  1061
  By combining raw composition with lifting, we get full @{inference
wenzelm@29771
  1062
  resolution} as follows:
wenzelm@20498
  1063
  \[
wenzelm@29771
  1064
  \infer[(@{inference_def resolution})]
wenzelm@20498
  1065
  {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>A (?\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
  1066
  {\begin{tabular}{l}
wenzelm@20498
  1067
    @{text "\<^vec>A ?\<^vec>a \<Longrightarrow> B ?\<^vec>a"} \\
wenzelm@20498
  1068
    @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
wenzelm@20498
  1069
    @{text "(\<lambda>\<^vec>x. B (?\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
wenzelm@20498
  1070
   \end{tabular}}
wenzelm@20498
  1071
  \]
wenzelm@20498
  1072
wenzelm@29774
  1073
  Continued resolution of rules allows to back-chain a problem towards
wenzelm@29774
  1074
  more and sub-problems.  Branches are closed either by resolving with
wenzelm@29774
  1075
  a rule of 0 premises, or by producing a ``short-circuit'' within a
wenzelm@29774
  1076
  solved situation (again modulo unification):
wenzelm@29771
  1077
  \[
wenzelm@29771
  1078
  \infer[(@{inference_def assumption})]{@{text "C\<vartheta>"}}
wenzelm@29771
  1079
  {@{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
  1080
  \]
wenzelm@20498
  1081
wenzelm@29771
  1082
  FIXME @{inference_def elim_resolution}, @{inference_def dest_resolution}
wenzelm@18537
  1083
*}
wenzelm@18537
  1084
wenzelm@29768
  1085
text %mlref {*
wenzelm@29768
  1086
  \begin{mldecls}
wenzelm@46262
  1087
  @{index_ML_op "RSN": "thm * (int * thm) -> thm"} \\
wenzelm@46262
  1088
  @{index_ML_op "RS": "thm * thm -> thm"} \\
wenzelm@46256
  1089
wenzelm@46262
  1090
  @{index_ML_op "RLN": "thm list * (int * thm list) -> thm list"} \\
wenzelm@46262
  1091
  @{index_ML_op "RL": "thm list * thm list -> thm list"} \\
wenzelm@46256
  1092
wenzelm@46262
  1093
  @{index_ML_op "MRS": "thm list * thm -> thm"} \\
wenzelm@46262
  1094
  @{index_ML_op "OF": "thm * thm list -> thm"} \\
wenzelm@29768
  1095
  \end{mldecls}
wenzelm@29768
  1096
wenzelm@29768
  1097
  \begin{description}
wenzelm@29768
  1098
wenzelm@46256
  1099
  \item @{text "rule\<^sub>1 RSN (i, rule\<^sub>2)"} resolves the conclusion of
wenzelm@46256
  1100
  @{text "rule\<^sub>1"} with the @{text i}-th premise of @{text "rule\<^sub>2"},
wenzelm@46256
  1101
  according to the @{inference resolution} principle explained above.
wenzelm@46256
  1102
  Unless there is precisely one resolvent it raises exception @{ML
wenzelm@46256
  1103
  THM}.
wenzelm@46256
  1104
wenzelm@46256
  1105
  This corresponds to the rule attribute @{attribute THEN} in Isar
wenzelm@46256
  1106
  source language.
wenzelm@46256
  1107
wenzelm@46256
  1108
  \item @{text "rule\<^sub>1 RS rule\<^sub>2"} abbreviates @{text "rule\<^sub>1 RS (1,
wenzelm@46256
  1109
  rule\<^sub>2)"}.
wenzelm@29768
  1110
wenzelm@46256
  1111
  \item @{text "rules\<^sub>1 RLN (i, rules\<^sub>2)"} joins lists of rules.  For
wenzelm@46256
  1112
  every @{text "rule\<^sub>1"} in @{text "rules\<^sub>1"} and @{text "rule\<^sub>2"} in
wenzelm@46256
  1113
  @{text "rules\<^sub>2"}, it resolves the conclusion of @{text "rule\<^sub>1"} with
wenzelm@46256
  1114
  the @{text "i"}-th premise of @{text "rule\<^sub>2"}, accumulating multiple
wenzelm@46256
  1115
  results in one big list.  Note that such strict enumerations of
wenzelm@46256
  1116
  higher-order unifications can be inefficient compared to the lazy
wenzelm@46256
  1117
  variant seen in elementary tactics like @{ML resolve_tac}.
wenzelm@46256
  1118
wenzelm@46256
  1119
  \item @{text "rules\<^sub>1 RL rules\<^sub>2"} abbreviates @{text "rules\<^sub>1 RLN (1,
wenzelm@46256
  1120
  rules\<^sub>2)"}.
wenzelm@46256
  1121
wenzelm@46256
  1122
  \item @{text "[rule\<^sub>1, \<dots>, rule\<^sub>n] MRS rule"} resolves @{text "rule\<^isub>i"}
wenzelm@46256
  1123
  against premise @{text "i"} of @{text "rule"}, for @{text "i = n, \<dots>,
wenzelm@46256
  1124
  1"}.  By working from right to left, newly emerging premises are
wenzelm@46256
  1125
  concatenated in the result, without interfering.
wenzelm@46256
  1126
wenzelm@46256
  1127
  \item @{text "rule OF rules"} abbreviates @{text "rules MRS rule"}.
wenzelm@46256
  1128
wenzelm@46256
  1129
  This corresponds to the rule attribute @{attribute OF} in Isar
wenzelm@46256
  1130
  source language.
wenzelm@29768
  1131
wenzelm@29768
  1132
  \end{description}
wenzelm@29768
  1133
*}
wenzelm@30272
  1134
wenzelm@18537
  1135
end