doc-src/IsarImplementation/Thy/Logic.thy
author wenzelm
Tue Apr 19 10:50:54 2011 +0200 (2011-04-19)
changeset 42401 9bfaf6819291
parent 40255 9ffbc25e1606
child 42510 b9c106763325
permissions -rw-r--r--
updated some theory primitives, which now depend on auxiliary context;
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@39832
   200
  \begin{rail}
wenzelm@39832
   201
  'class' nameref
wenzelm@39832
   202
  ;
wenzelm@39832
   203
  'sort' sort
wenzelm@39832
   204
  ;
wenzelm@40255
   205
  ('type_name' | 'type_abbrev' | 'nonterminal') nameref
wenzelm@39832
   206
  ;
wenzelm@39832
   207
  'typ' type
wenzelm@39832
   208
  ;
wenzelm@39832
   209
  \end{rail}
wenzelm@39832
   210
wenzelm@39832
   211
  \begin{description}
wenzelm@39832
   212
wenzelm@39832
   213
  \item @{text "@{class c}"} inlines the internalized class @{text
wenzelm@39832
   214
  "c"} --- as @{ML_type string} literal.
wenzelm@39832
   215
wenzelm@39832
   216
  \item @{text "@{sort s}"} inlines the internalized sort @{text "s"}
wenzelm@39832
   217
  --- as @{ML_type "string list"} literal.
wenzelm@39832
   218
wenzelm@39832
   219
  \item @{text "@{type_name c}"} inlines the internalized type
wenzelm@39832
   220
  constructor @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   221
wenzelm@39832
   222
  \item @{text "@{type_abbrev c}"} inlines the internalized type
wenzelm@39832
   223
  abbreviation @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   224
wenzelm@39832
   225
  \item @{text "@{nonterminal c}"} inlines the internalized syntactic
wenzelm@39832
   226
  type~/ grammar nonterminal @{text "c"} --- as @{ML_type string}
wenzelm@39832
   227
  literal.
wenzelm@39832
   228
wenzelm@39832
   229
  \item @{text "@{typ \<tau>}"} inlines the internalized type @{text "\<tau>"}
wenzelm@39832
   230
  --- as constructor term for datatype @{ML_type typ}.
wenzelm@39832
   231
wenzelm@39832
   232
  \end{description}
wenzelm@39832
   233
*}
wenzelm@39832
   234
wenzelm@20437
   235
wenzelm@20451
   236
section {* Terms \label{sec:terms} *}
wenzelm@18537
   237
wenzelm@18537
   238
text {*
wenzelm@20491
   239
  The language of terms is that of simply-typed @{text "\<lambda>"}-calculus
wenzelm@20520
   240
  with de-Bruijn indices for bound variables (cf.\ \cite{debruijn72}
wenzelm@29761
   241
  or \cite{paulson-ml2}), with the types being determined by the
wenzelm@29761
   242
  corresponding binders.  In contrast, free variables and constants
wenzelm@34929
   243
  have an explicit name and type in each occurrence.
wenzelm@20514
   244
wenzelm@20514
   245
  \medskip A \emph{bound variable} is a natural number @{text "b"},
wenzelm@20537
   246
  which accounts for the number of intermediate binders between the
wenzelm@20537
   247
  variable occurrence in the body and its binding position.  For
wenzelm@34929
   248
  example, the de-Bruijn term @{text "\<lambda>\<^bsub>bool\<^esub>. \<lambda>\<^bsub>bool\<^esub>. 1 \<and> 0"} would
wenzelm@34929
   249
  correspond to @{text "\<lambda>x\<^bsub>bool\<^esub>. \<lambda>y\<^bsub>bool\<^esub>. x \<and> y"} in a named
wenzelm@20543
   250
  representation.  Note that a bound variable may be represented by
wenzelm@20543
   251
  different de-Bruijn indices at different occurrences, depending on
wenzelm@20543
   252
  the nesting of abstractions.
wenzelm@20537
   253
wenzelm@20543
   254
  A \emph{loose variable} is a bound variable that is outside the
wenzelm@20537
   255
  scope of local binders.  The types (and names) for loose variables
wenzelm@20543
   256
  can be managed as a separate context, that is maintained as a stack
wenzelm@20543
   257
  of hypothetical binders.  The core logic operates on closed terms,
wenzelm@20543
   258
  without any loose variables.
wenzelm@20514
   259
wenzelm@20537
   260
  A \emph{fixed variable} is a pair of a basic name and a type, e.g.\
wenzelm@34929
   261
  @{text "(x, \<tau>)"} which is usually printed @{text "x\<^isub>\<tau>"} here.  A
wenzelm@20537
   262
  \emph{schematic variable} is a pair of an indexname and a type,
wenzelm@34929
   263
  e.g.\ @{text "((x, 0), \<tau>)"} which is likewise printed as @{text
wenzelm@20537
   264
  "?x\<^isub>\<tau>"}.
wenzelm@20491
   265
wenzelm@20537
   266
  \medskip A \emph{constant} is a pair of a basic name and a type,
wenzelm@34929
   267
  e.g.\ @{text "(c, \<tau>)"} which is usually printed as @{text "c\<^isub>\<tau>"}
wenzelm@34929
   268
  here.  Constants are declared in the context as polymorphic families
wenzelm@34929
   269
  @{text "c :: \<sigma>"}, meaning that all substitution instances @{text
wenzelm@34929
   270
  "c\<^isub>\<tau>"} for @{text "\<tau> = \<sigma>\<vartheta>"} are valid.
wenzelm@20514
   271
wenzelm@34929
   272
  The vector of \emph{type arguments} of constant @{text "c\<^isub>\<tau>"} wrt.\
wenzelm@34929
   273
  the declaration @{text "c :: \<sigma>"} is defined as the codomain of the
wenzelm@34929
   274
  matcher @{text "\<vartheta> = {?\<alpha>\<^isub>1 \<mapsto> \<tau>\<^isub>1, \<dots>, ?\<alpha>\<^isub>n \<mapsto> \<tau>\<^isub>n}"} presented in
wenzelm@34929
   275
  canonical order @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n)"}, corresponding to the
wenzelm@34929
   276
  left-to-right occurrences of the @{text "\<alpha>\<^isub>i"} in @{text "\<sigma>"}.
wenzelm@34929
   277
  Within a given theory context, there is a one-to-one correspondence
wenzelm@34929
   278
  between any constant @{text "c\<^isub>\<tau>"} and the application @{text "c(\<tau>\<^isub>1,
wenzelm@34929
   279
  \<dots>, \<tau>\<^isub>n)"} of its type arguments.  For example, with @{text "plus :: \<alpha>
wenzelm@34929
   280
  \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"}, the instance @{text "plus\<^bsub>nat \<Rightarrow> nat \<Rightarrow> nat\<^esub>"} corresponds to
wenzelm@34929
   281
  @{text "plus(nat)"}.
wenzelm@20480
   282
wenzelm@20514
   283
  Constant declarations @{text "c :: \<sigma>"} may contain sort constraints
wenzelm@20514
   284
  for type variables in @{text "\<sigma>"}.  These are observed by
wenzelm@20514
   285
  type-inference as expected, but \emph{ignored} by the core logic.
wenzelm@20514
   286
  This means the primitive logic is able to reason with instances of
wenzelm@20537
   287
  polymorphic constants that the user-level type-checker would reject
wenzelm@20537
   288
  due to violation of type class restrictions.
wenzelm@20480
   289
wenzelm@39861
   290
  \medskip An \emph{atomic term} is either a variable or constant.
wenzelm@34929
   291
  The logical category \emph{term} is defined inductively over atomic
wenzelm@34929
   292
  terms, with abstraction and application as follows: @{text "t = b |
wenzelm@34929
   293
  x\<^isub>\<tau> | ?x\<^isub>\<tau> | c\<^isub>\<tau> | \<lambda>\<^isub>\<tau>. t | t\<^isub>1 t\<^isub>2"}.  Parsing and printing takes care of
wenzelm@34929
   294
  converting between an external representation with named bound
wenzelm@34929
   295
  variables.  Subsequently, we shall use the latter notation instead
wenzelm@34929
   296
  of internal de-Bruijn representation.
wenzelm@20498
   297
wenzelm@20537
   298
  The inductive relation @{text "t :: \<tau>"} assigns a (unique) type to a
wenzelm@20537
   299
  term according to the structure of atomic terms, abstractions, and
wenzelm@20537
   300
  applicatins:
wenzelm@20498
   301
  \[
wenzelm@20501
   302
  \infer{@{text "a\<^isub>\<tau> :: \<tau>"}}{}
wenzelm@20498
   303
  \qquad
wenzelm@20501
   304
  \infer{@{text "(\<lambda>x\<^sub>\<tau>. t) :: \<tau> \<Rightarrow> \<sigma>"}}{@{text "t :: \<sigma>"}}
wenzelm@20501
   305
  \qquad
wenzelm@20501
   306
  \infer{@{text "t u :: \<sigma>"}}{@{text "t :: \<tau> \<Rightarrow> \<sigma>"} & @{text "u :: \<tau>"}}
wenzelm@20498
   307
  \]
wenzelm@20514
   308
  A \emph{well-typed term} is a term that can be typed according to these rules.
wenzelm@20498
   309
wenzelm@20514
   310
  Typing information can be omitted: type-inference is able to
wenzelm@20514
   311
  reconstruct the most general type of a raw term, while assigning
wenzelm@20514
   312
  most general types to all of its variables and constants.
wenzelm@20514
   313
  Type-inference depends on a context of type constraints for fixed
wenzelm@20514
   314
  variables, and declarations for polymorphic constants.
wenzelm@20514
   315
wenzelm@20537
   316
  The identity of atomic terms consists both of the name and the type
wenzelm@20537
   317
  component.  This means that different variables @{text
wenzelm@34929
   318
  "x\<^bsub>\<tau>\<^isub>1\<^esub>"} and @{text "x\<^bsub>\<tau>\<^isub>2\<^esub>"} may become the same after
wenzelm@34929
   319
  type instantiation.  Type-inference rejects variables of the same
wenzelm@34929
   320
  name, but different types.  In contrast, mixed instances of
wenzelm@34929
   321
  polymorphic constants occur routinely.
wenzelm@20514
   322
wenzelm@20514
   323
  \medskip The \emph{hidden polymorphism} of a term @{text "t :: \<sigma>"}
wenzelm@20514
   324
  is the set of type variables occurring in @{text "t"}, but not in
wenzelm@34929
   325
  its type @{text "\<sigma>"}.  This means that the term implicitly depends
wenzelm@34929
   326
  on type arguments that are not accounted in the result type, i.e.\
wenzelm@34929
   327
  there are different type instances @{text "t\<vartheta> :: \<sigma>"} and
wenzelm@34929
   328
  @{text "t\<vartheta>' :: \<sigma>"} with the same type.  This slightly
wenzelm@20543
   329
  pathological situation notoriously demands additional care.
wenzelm@20514
   330
wenzelm@20514
   331
  \medskip A \emph{term abbreviation} is a syntactic definition @{text
wenzelm@20537
   332
  "c\<^isub>\<sigma> \<equiv> t"} of a closed term @{text "t"} of type @{text "\<sigma>"},
wenzelm@20537
   333
  without any hidden polymorphism.  A term abbreviation looks like a
wenzelm@20543
   334
  constant in the syntax, but is expanded before entering the logical
wenzelm@20543
   335
  core.  Abbreviations are usually reverted when printing terms, using
wenzelm@20543
   336
  @{text "t \<rightarrow> c\<^isub>\<sigma>"} as rules for higher-order rewriting.
wenzelm@20519
   337
wenzelm@20519
   338
  \medskip Canonical operations on @{text "\<lambda>"}-terms include @{text
wenzelm@20537
   339
  "\<alpha>\<beta>\<eta>"}-conversion: @{text "\<alpha>"}-conversion refers to capture-free
wenzelm@20519
   340
  renaming of bound variables; @{text "\<beta>"}-conversion contracts an
wenzelm@20537
   341
  abstraction applied to an argument term, substituting the argument
wenzelm@20519
   342
  in the body: @{text "(\<lambda>x. b)a"} becomes @{text "b[a/x]"}; @{text
wenzelm@20519
   343
  "\<eta>"}-conversion contracts vacuous application-abstraction: @{text
wenzelm@20519
   344
  "\<lambda>x. f x"} becomes @{text "f"}, provided that the bound variable
wenzelm@20537
   345
  does not occur in @{text "f"}.
wenzelm@20519
   346
wenzelm@20537
   347
  Terms are normally treated modulo @{text "\<alpha>"}-conversion, which is
wenzelm@20537
   348
  implicit in the de-Bruijn representation.  Names for bound variables
wenzelm@20537
   349
  in abstractions are maintained separately as (meaningless) comments,
wenzelm@20537
   350
  mostly for parsing and printing.  Full @{text "\<alpha>\<beta>\<eta>"}-conversion is
wenzelm@28784
   351
  commonplace in various standard operations (\secref{sec:obj-rules})
wenzelm@28784
   352
  that are based on higher-order unification and matching.
wenzelm@18537
   353
*}
wenzelm@18537
   354
wenzelm@20514
   355
text %mlref {*
wenzelm@20514
   356
  \begin{mldecls}
wenzelm@20514
   357
  @{index_ML_type term} \\
wenzelm@20519
   358
  @{index_ML "op aconv": "term * term -> bool"} \\
wenzelm@39846
   359
  @{index_ML Term.map_types: "(typ -> typ) -> term -> term"} \\
wenzelm@39846
   360
  @{index_ML Term.fold_types: "(typ -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@39846
   361
  @{index_ML Term.map_aterms: "(term -> term) -> term -> term"} \\
wenzelm@39846
   362
  @{index_ML Term.fold_aterms: "(term -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@20547
   363
  \end{mldecls}
wenzelm@20547
   364
  \begin{mldecls}
wenzelm@20514
   365
  @{index_ML fastype_of: "term -> typ"} \\
wenzelm@20519
   366
  @{index_ML lambda: "term -> term -> term"} \\
wenzelm@20519
   367
  @{index_ML betapply: "term * term -> term"} \\
wenzelm@42401
   368
  @{index_ML Sign.declare_const: "Proof.context ->
wenzelm@42401
   369
  (binding * typ) * mixfix -> theory -> term * theory"} \\
haftmann@33174
   370
  @{index_ML Sign.add_abbrev: "string -> binding * term ->
wenzelm@24972
   371
  theory -> (term * term) * theory"} \\
wenzelm@20519
   372
  @{index_ML Sign.const_typargs: "theory -> string * typ -> typ list"} \\
wenzelm@20519
   373
  @{index_ML Sign.const_instance: "theory -> string * typ list -> typ"} \\
wenzelm@20514
   374
  \end{mldecls}
wenzelm@18537
   375
wenzelm@20514
   376
  \begin{description}
wenzelm@18537
   377
wenzelm@39864
   378
  \item Type @{ML_type term} represents de-Bruijn terms, with comments
wenzelm@39864
   379
  in abstractions, and explicitly named free variables and constants;
wenzelm@20537
   380
  this is a datatype with constructors @{ML Bound}, @{ML Free}, @{ML
wenzelm@20537
   381
  Var}, @{ML Const}, @{ML Abs}, @{ML "op $"}.
wenzelm@20519
   382
wenzelm@36166
   383
  \item @{text "t"}~@{ML_text aconv}~@{text "u"} checks @{text
wenzelm@20519
   384
  "\<alpha>"}-equivalence of two terms.  This is the basic equality relation
wenzelm@20519
   385
  on type @{ML_type term}; raw datatype equality should only be used
wenzelm@20519
   386
  for operations related to parsing or printing!
wenzelm@20519
   387
wenzelm@39846
   388
  \item @{ML Term.map_types}~@{text "f t"} applies the mapping @{text
wenzelm@20537
   389
  "f"} to all types occurring in @{text "t"}.
wenzelm@20537
   390
wenzelm@39846
   391
  \item @{ML Term.fold_types}~@{text "f t"} iterates the operation
wenzelm@39846
   392
  @{text "f"} over all occurrences of types in @{text "t"}; the term
wenzelm@20537
   393
  structure is traversed from left to right.
wenzelm@20519
   394
wenzelm@39846
   395
  \item @{ML Term.map_aterms}~@{text "f t"} applies the mapping @{text
wenzelm@39846
   396
  "f"} to all atomic terms (@{ML Bound}, @{ML Free}, @{ML Var}, @{ML
wenzelm@20537
   397
  Const}) occurring in @{text "t"}.
wenzelm@20537
   398
wenzelm@39846
   399
  \item @{ML Term.fold_aterms}~@{text "f t"} iterates the operation
wenzelm@39846
   400
  @{text "f"} over all occurrences of atomic terms (@{ML Bound}, @{ML
wenzelm@39846
   401
  Free}, @{ML Var}, @{ML Const}) in @{text "t"}; the term structure is
wenzelm@20519
   402
  traversed from left to right.
wenzelm@20519
   403
wenzelm@20537
   404
  \item @{ML fastype_of}~@{text "t"} determines the type of a
wenzelm@20537
   405
  well-typed term.  This operation is relatively slow, despite the
wenzelm@20537
   406
  omission of any sanity checks.
wenzelm@20519
   407
wenzelm@20519
   408
  \item @{ML lambda}~@{text "a b"} produces an abstraction @{text
wenzelm@20537
   409
  "\<lambda>a. b"}, where occurrences of the atomic term @{text "a"} in the
wenzelm@20537
   410
  body @{text "b"} are replaced by bound variables.
wenzelm@20519
   411
wenzelm@20537
   412
  \item @{ML betapply}~@{text "(t, u)"} produces an application @{text
wenzelm@20537
   413
  "t u"}, with topmost @{text "\<beta>"}-conversion if @{text "t"} is an
wenzelm@20537
   414
  abstraction.
wenzelm@20519
   415
wenzelm@42401
   416
  \item @{ML Sign.declare_const}~@{text "ctxt ((c, \<sigma>), mx)"} declares
wenzelm@42401
   417
  a new constant @{text "c :: \<sigma>"} with optional mixfix syntax.
wenzelm@20519
   418
haftmann@33174
   419
  \item @{ML Sign.add_abbrev}~@{text "print_mode (c, t)"}
wenzelm@21827
   420
  introduces a new term abbreviation @{text "c \<equiv> t"}.
wenzelm@20519
   421
wenzelm@20520
   422
  \item @{ML Sign.const_typargs}~@{text "thy (c, \<tau>)"} and @{ML
wenzelm@20520
   423
  Sign.const_instance}~@{text "thy (c, [\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n])"}
wenzelm@20543
   424
  convert between two representations of polymorphic constants: full
wenzelm@20543
   425
  type instance vs.\ compact type arguments form.
wenzelm@18537
   426
wenzelm@20514
   427
  \end{description}
wenzelm@18537
   428
*}
wenzelm@18537
   429
wenzelm@39832
   430
text %mlantiq {*
wenzelm@39832
   431
  \begin{matharray}{rcl}
wenzelm@39832
   432
  @{ML_antiquotation_def "const_name"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   433
  @{ML_antiquotation_def "const_abbrev"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   434
  @{ML_antiquotation_def "const"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   435
  @{ML_antiquotation_def "term"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   436
  @{ML_antiquotation_def "prop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   437
  \end{matharray}
wenzelm@39832
   438
wenzelm@39832
   439
  \begin{rail}
wenzelm@40255
   440
  ('const_name' | 'const_abbrev') nameref
wenzelm@39832
   441
  ;
wenzelm@39832
   442
  'const' ('(' (type + ',') ')')?
wenzelm@39832
   443
  ;
wenzelm@39832
   444
  'term' term
wenzelm@39832
   445
  ;
wenzelm@39832
   446
  'prop' prop
wenzelm@39832
   447
  ;
wenzelm@39832
   448
  \end{rail}
wenzelm@39832
   449
wenzelm@39832
   450
  \begin{description}
wenzelm@39832
   451
wenzelm@39832
   452
  \item @{text "@{const_name c}"} inlines the internalized logical
wenzelm@39832
   453
  constant name @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   454
wenzelm@39832
   455
  \item @{text "@{const_abbrev c}"} inlines the internalized
wenzelm@39832
   456
  abbreviated constant name @{text "c"} --- as @{ML_type string}
wenzelm@39832
   457
  literal.
wenzelm@39832
   458
wenzelm@39832
   459
  \item @{text "@{const c(\<^vec>\<tau>)}"} inlines the internalized
wenzelm@39832
   460
  constant @{text "c"} with precise type instantiation in the sense of
wenzelm@39832
   461
  @{ML Sign.const_instance} --- as @{ML Const} constructor term for
wenzelm@39832
   462
  datatype @{ML_type term}.
wenzelm@39832
   463
wenzelm@39832
   464
  \item @{text "@{term t}"} inlines the internalized term @{text "t"}
wenzelm@39832
   465
  --- as constructor term for datatype @{ML_type term}.
wenzelm@39832
   466
wenzelm@39832
   467
  \item @{text "@{prop \<phi>}"} inlines the internalized proposition
wenzelm@39832
   468
  @{text "\<phi>"} --- as constructor term for datatype @{ML_type term}.
wenzelm@39832
   469
wenzelm@39832
   470
  \end{description}
wenzelm@39832
   471
*}
wenzelm@39832
   472
wenzelm@18537
   473
wenzelm@20451
   474
section {* Theorems \label{sec:thms} *}
wenzelm@18537
   475
wenzelm@18537
   476
text {*
wenzelm@20543
   477
  A \emph{proposition} is a well-typed term of type @{text "prop"}, a
wenzelm@20521
   478
  \emph{theorem} is a proven proposition (depending on a context of
wenzelm@20521
   479
  hypotheses and the background theory).  Primitive inferences include
wenzelm@29774
   480
  plain Natural Deduction rules for the primary connectives @{text
wenzelm@20537
   481
  "\<And>"} and @{text "\<Longrightarrow>"} of the framework.  There is also a builtin
wenzelm@20537
   482
  notion of equality/equivalence @{text "\<equiv>"}.
wenzelm@20521
   483
*}
wenzelm@20521
   484
wenzelm@29758
   485
wenzelm@26872
   486
subsection {* Primitive connectives and rules \label{sec:prim-rules} *}
wenzelm@18537
   487
wenzelm@20521
   488
text {*
wenzelm@20543
   489
  The theory @{text "Pure"} contains constant declarations for the
wenzelm@20543
   490
  primitive connectives @{text "\<And>"}, @{text "\<Longrightarrow>"}, and @{text "\<equiv>"} of
wenzelm@20543
   491
  the logical framework, see \figref{fig:pure-connectives}.  The
wenzelm@20543
   492
  derivability judgment @{text "A\<^isub>1, \<dots>, A\<^isub>n \<turnstile> B"} is
wenzelm@20543
   493
  defined inductively by the primitive inferences given in
wenzelm@20543
   494
  \figref{fig:prim-rules}, with the global restriction that the
wenzelm@20543
   495
  hypotheses must \emph{not} contain any schematic variables.  The
wenzelm@20543
   496
  builtin equality is conceptually axiomatized as shown in
wenzelm@20521
   497
  \figref{fig:pure-equality}, although the implementation works
wenzelm@20543
   498
  directly with derived inferences.
wenzelm@20521
   499
wenzelm@20521
   500
  \begin{figure}[htb]
wenzelm@20521
   501
  \begin{center}
wenzelm@20501
   502
  \begin{tabular}{ll}
wenzelm@20501
   503
  @{text "all :: (\<alpha> \<Rightarrow> prop) \<Rightarrow> prop"} & universal quantification (binder @{text "\<And>"}) \\
wenzelm@20501
   504
  @{text "\<Longrightarrow> :: prop \<Rightarrow> prop \<Rightarrow> prop"} & implication (right associative infix) \\
wenzelm@20521
   505
  @{text "\<equiv> :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> prop"} & equality relation (infix) \\
wenzelm@20501
   506
  \end{tabular}
wenzelm@20537
   507
  \caption{Primitive connectives of Pure}\label{fig:pure-connectives}
wenzelm@20521
   508
  \end{center}
wenzelm@20521
   509
  \end{figure}
wenzelm@18537
   510
wenzelm@20501
   511
  \begin{figure}[htb]
wenzelm@20501
   512
  \begin{center}
wenzelm@20498
   513
  \[
wenzelm@20498
   514
  \infer[@{text "(axiom)"}]{@{text "\<turnstile> A"}}{@{text "A \<in> \<Theta>"}}
wenzelm@20498
   515
  \qquad
wenzelm@20498
   516
  \infer[@{text "(assume)"}]{@{text "A \<turnstile> A"}}{}
wenzelm@20498
   517
  \]
wenzelm@20498
   518
  \[
wenzelm@34929
   519
  \infer[@{text "(\<And>\<dash>intro)"}]{@{text "\<Gamma> \<turnstile> \<And>x. b[x]"}}{@{text "\<Gamma> \<turnstile> b[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   520
  \qquad
wenzelm@34929
   521
  \infer[@{text "(\<And>\<dash>elim)"}]{@{text "\<Gamma> \<turnstile> b[a]"}}{@{text "\<Gamma> \<turnstile> \<And>x. b[x]"}}
wenzelm@20498
   522
  \]
wenzelm@20498
   523
  \[
wenzelm@34929
   524
  \infer[@{text "(\<Longrightarrow>\<dash>intro)"}]{@{text "\<Gamma> - A \<turnstile> A \<Longrightarrow> B"}}{@{text "\<Gamma> \<turnstile> B"}}
wenzelm@20498
   525
  \qquad
wenzelm@34929
   526
  \infer[@{text "(\<Longrightarrow>\<dash>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
   527
  \]
wenzelm@20521
   528
  \caption{Primitive inferences of Pure}\label{fig:prim-rules}
wenzelm@20521
   529
  \end{center}
wenzelm@20521
   530
  \end{figure}
wenzelm@20521
   531
wenzelm@20521
   532
  \begin{figure}[htb]
wenzelm@20521
   533
  \begin{center}
wenzelm@20521
   534
  \begin{tabular}{ll}
wenzelm@20537
   535
  @{text "\<turnstile> (\<lambda>x. b[x]) a \<equiv> b[a]"} & @{text "\<beta>"}-conversion \\
wenzelm@20521
   536
  @{text "\<turnstile> x \<equiv> x"} & reflexivity \\
wenzelm@20521
   537
  @{text "\<turnstile> x \<equiv> y \<Longrightarrow> P x \<Longrightarrow> P y"} & substitution \\
wenzelm@20521
   538
  @{text "\<turnstile> (\<And>x. f x \<equiv> g x) \<Longrightarrow> f \<equiv> g"} & extensionality \\
wenzelm@20537
   539
  @{text "\<turnstile> (A \<Longrightarrow> B) \<Longrightarrow> (B \<Longrightarrow> A) \<Longrightarrow> A \<equiv> B"} & logical equivalence \\
wenzelm@20521
   540
  \end{tabular}
wenzelm@20542
   541
  \caption{Conceptual axiomatization of Pure equality}\label{fig:pure-equality}
wenzelm@20501
   542
  \end{center}
wenzelm@20501
   543
  \end{figure}
wenzelm@18537
   544
wenzelm@20501
   545
  The introduction and elimination rules for @{text "\<And>"} and @{text
wenzelm@20537
   546
  "\<Longrightarrow>"} are analogous to formation of dependently typed @{text
wenzelm@20501
   547
  "\<lambda>"}-terms representing the underlying proof objects.  Proof terms
wenzelm@20543
   548
  are irrelevant in the Pure logic, though; they cannot occur within
wenzelm@20543
   549
  propositions.  The system provides a runtime option to record
wenzelm@20537
   550
  explicit proof terms for primitive inferences.  Thus all three
wenzelm@20537
   551
  levels of @{text "\<lambda>"}-calculus become explicit: @{text "\<Rightarrow>"} for
wenzelm@20537
   552
  terms, and @{text "\<And>/\<Longrightarrow>"} for proofs (cf.\
wenzelm@20537
   553
  \cite{Berghofer-Nipkow:2000:TPHOL}).
wenzelm@20491
   554
wenzelm@34929
   555
  Observe that locally fixed parameters (as in @{text
wenzelm@34929
   556
  "\<And>\<dash>intro"}) need not be recorded in the hypotheses, because
wenzelm@34929
   557
  the simple syntactic types of Pure are always inhabitable.
wenzelm@34929
   558
  ``Assumptions'' @{text "x :: \<tau>"} for type-membership are only
wenzelm@34929
   559
  present as long as some @{text "x\<^isub>\<tau>"} occurs in the statement
wenzelm@34929
   560
  body.\footnote{This is the key difference to ``@{text "\<lambda>HOL"}'' in
wenzelm@34929
   561
  the PTS framework \cite{Barendregt-Geuvers:2001}, where hypotheses
wenzelm@34929
   562
  @{text "x : A"} are treated uniformly for propositions and types.}
wenzelm@20501
   563
wenzelm@20501
   564
  \medskip The axiomatization of a theory is implicitly closed by
wenzelm@20537
   565
  forming all instances of type and term variables: @{text "\<turnstile>
wenzelm@20537
   566
  A\<vartheta>"} holds for any substitution instance of an axiom
wenzelm@20543
   567
  @{text "\<turnstile> A"}.  By pushing substitutions through derivations
wenzelm@20543
   568
  inductively, we also get admissible @{text "generalize"} and @{text
wenzelm@34929
   569
  "instantiate"} rules as shown in \figref{fig:subst-rules}.
wenzelm@20501
   570
wenzelm@20501
   571
  \begin{figure}[htb]
wenzelm@20501
   572
  \begin{center}
wenzelm@20498
   573
  \[
wenzelm@20501
   574
  \infer{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}{@{text "\<Gamma> \<turnstile> B[\<alpha>]"} & @{text "\<alpha> \<notin> \<Gamma>"}}
wenzelm@20501
   575
  \quad
wenzelm@20501
   576
  \infer[\quad@{text "(generalize)"}]{@{text "\<Gamma> \<turnstile> B[?x]"}}{@{text "\<Gamma> \<turnstile> B[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   577
  \]
wenzelm@20498
   578
  \[
wenzelm@20501
   579
  \infer{@{text "\<Gamma> \<turnstile> B[\<tau>]"}}{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}
wenzelm@20501
   580
  \quad
wenzelm@20501
   581
  \infer[\quad@{text "(instantiate)"}]{@{text "\<Gamma> \<turnstile> B[t]"}}{@{text "\<Gamma> \<turnstile> B[?x]"}}
wenzelm@20498
   582
  \]
wenzelm@20501
   583
  \caption{Admissible substitution rules}\label{fig:subst-rules}
wenzelm@20501
   584
  \end{center}
wenzelm@20501
   585
  \end{figure}
wenzelm@18537
   586
wenzelm@20537
   587
  Note that @{text "instantiate"} does not require an explicit
wenzelm@20537
   588
  side-condition, because @{text "\<Gamma>"} may never contain schematic
wenzelm@20537
   589
  variables.
wenzelm@20537
   590
wenzelm@20537
   591
  In principle, variables could be substituted in hypotheses as well,
wenzelm@20543
   592
  but this would disrupt the monotonicity of reasoning: deriving
wenzelm@20543
   593
  @{text "\<Gamma>\<vartheta> \<turnstile> B\<vartheta>"} from @{text "\<Gamma> \<turnstile> B"} is
wenzelm@20543
   594
  correct, but @{text "\<Gamma>\<vartheta> \<supseteq> \<Gamma>"} does not necessarily hold:
wenzelm@20543
   595
  the result belongs to a different proof context.
wenzelm@20542
   596
wenzelm@20543
   597
  \medskip An \emph{oracle} is a function that produces axioms on the
wenzelm@20543
   598
  fly.  Logically, this is an instance of the @{text "axiom"} rule
wenzelm@20543
   599
  (\figref{fig:prim-rules}), but there is an operational difference.
wenzelm@20543
   600
  The system always records oracle invocations within derivations of
wenzelm@29768
   601
  theorems by a unique tag.
wenzelm@20542
   602
wenzelm@20542
   603
  Axiomatizations should be limited to the bare minimum, typically as
wenzelm@20542
   604
  part of the initial logical basis of an object-logic formalization.
wenzelm@20543
   605
  Later on, theories are usually developed in a strictly definitional
wenzelm@20543
   606
  fashion, by stating only certain equalities over new constants.
wenzelm@20542
   607
wenzelm@20542
   608
  A \emph{simple definition} consists of a constant declaration @{text
wenzelm@20543
   609
  "c :: \<sigma>"} together with an axiom @{text "\<turnstile> c \<equiv> t"}, where @{text "t
wenzelm@20543
   610
  :: \<sigma>"} is a closed term without any hidden polymorphism.  The RHS
wenzelm@20543
   611
  may depend on further defined constants, but not @{text "c"} itself.
wenzelm@20543
   612
  Definitions of functions may be presented as @{text "c \<^vec>x \<equiv>
wenzelm@20543
   613
  t"} instead of the puristic @{text "c \<equiv> \<lambda>\<^vec>x. t"}.
wenzelm@20542
   614
wenzelm@20543
   615
  An \emph{overloaded definition} consists of a collection of axioms
wenzelm@20543
   616
  for the same constant, with zero or one equations @{text
wenzelm@20543
   617
  "c((\<^vec>\<alpha>)\<kappa>) \<equiv> t"} for each type constructor @{text "\<kappa>"} (for
wenzelm@20543
   618
  distinct variables @{text "\<^vec>\<alpha>"}).  The RHS may mention
wenzelm@20543
   619
  previously defined constants as above, or arbitrary constants @{text
wenzelm@20543
   620
  "d(\<alpha>\<^isub>i)"} for some @{text "\<alpha>\<^isub>i"} projected from @{text
wenzelm@20543
   621
  "\<^vec>\<alpha>"}.  Thus overloaded definitions essentially work by
wenzelm@20543
   622
  primitive recursion over the syntactic structure of a single type
wenzelm@39840
   623
  argument.  See also \cite[\S4.3]{Haftmann-Wenzel:2006:classes}.
wenzelm@20521
   624
*}
wenzelm@20498
   625
wenzelm@20521
   626
text %mlref {*
wenzelm@20521
   627
  \begin{mldecls}
wenzelm@20521
   628
  @{index_ML_type ctyp} \\
wenzelm@20521
   629
  @{index_ML_type cterm} \\
wenzelm@20547
   630
  @{index_ML Thm.ctyp_of: "theory -> typ -> ctyp"} \\
wenzelm@20547
   631
  @{index_ML Thm.cterm_of: "theory -> term -> cterm"} \\
wenzelm@20547
   632
  \end{mldecls}
wenzelm@20547
   633
  \begin{mldecls}
wenzelm@20521
   634
  @{index_ML_type thm} \\
wenzelm@32833
   635
  @{index_ML proofs: "int Unsynchronized.ref"} \\
wenzelm@20542
   636
  @{index_ML Thm.assume: "cterm -> thm"} \\
wenzelm@20542
   637
  @{index_ML Thm.forall_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   638
  @{index_ML Thm.forall_elim: "cterm -> thm -> thm"} \\
wenzelm@20542
   639
  @{index_ML Thm.implies_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   640
  @{index_ML Thm.implies_elim: "thm -> thm -> thm"} \\
wenzelm@20542
   641
  @{index_ML Thm.generalize: "string list * string list -> int -> thm -> thm"} \\
wenzelm@20542
   642
  @{index_ML Thm.instantiate: "(ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm"} \\
wenzelm@42401
   643
  @{index_ML Thm.add_axiom: "Proof.context ->
wenzelm@42401
   644
  binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@39821
   645
  @{index_ML Thm.add_oracle: "binding * ('a -> cterm) -> theory ->
wenzelm@39821
   646
  (string * ('a -> thm)) * theory"} \\
wenzelm@42401
   647
  @{index_ML Thm.add_def: "Proof.context -> bool -> bool ->
wenzelm@42401
   648
  binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@20547
   649
  \end{mldecls}
wenzelm@20547
   650
  \begin{mldecls}
wenzelm@42401
   651
  @{index_ML Theory.add_deps: "Proof.context -> string ->
wenzelm@42401
   652
  string * typ -> (string * typ) list -> theory -> theory"} \\
wenzelm@20521
   653
  \end{mldecls}
wenzelm@20521
   654
wenzelm@20521
   655
  \begin{description}
wenzelm@20521
   656
wenzelm@39864
   657
  \item Types @{ML_type ctyp} and @{ML_type cterm} represent certified
wenzelm@39864
   658
  types and terms, respectively.  These are abstract datatypes that
wenzelm@20542
   659
  guarantee that its values have passed the full well-formedness (and
wenzelm@20542
   660
  well-typedness) checks, relative to the declarations of type
wenzelm@20542
   661
  constructors, constants etc. in the theory.
wenzelm@20542
   662
wenzelm@29768
   663
  \item @{ML Thm.ctyp_of}~@{text "thy \<tau>"} and @{ML
wenzelm@29768
   664
  Thm.cterm_of}~@{text "thy t"} explicitly checks types and terms,
wenzelm@29768
   665
  respectively.  This also involves some basic normalizations, such
wenzelm@29768
   666
  expansion of type and term abbreviations from the theory context.
wenzelm@20547
   667
wenzelm@20547
   668
  Re-certification is relatively slow and should be avoided in tight
wenzelm@20547
   669
  reasoning loops.  There are separate operations to decompose
wenzelm@20547
   670
  certified entities (including actual theorems).
wenzelm@20542
   671
wenzelm@39864
   672
  \item Type @{ML_type thm} represents proven propositions.  This is
wenzelm@39864
   673
  an abstract datatype that guarantees that its values have been
wenzelm@20542
   674
  constructed by basic principles of the @{ML_struct Thm} module.
wenzelm@39281
   675
  Every @{ML_type thm} value contains a sliding back-reference to the
wenzelm@20543
   676
  enclosing theory, cf.\ \secref{sec:context-theory}.
wenzelm@20542
   677
wenzelm@34929
   678
  \item @{ML proofs} specifies the detail of proof recording within
wenzelm@29768
   679
  @{ML_type thm} values: @{ML 0} records only the names of oracles,
wenzelm@29768
   680
  @{ML 1} records oracle names and propositions, @{ML 2} additionally
wenzelm@29768
   681
  records full proof terms.  Officially named theorems that contribute
wenzelm@34929
   682
  to a result are recorded in any case.
wenzelm@20542
   683
wenzelm@20542
   684
  \item @{ML Thm.assume}, @{ML Thm.forall_intr}, @{ML
wenzelm@20542
   685
  Thm.forall_elim}, @{ML Thm.implies_intr}, and @{ML Thm.implies_elim}
wenzelm@20542
   686
  correspond to the primitive inferences of \figref{fig:prim-rules}.
wenzelm@20542
   687
wenzelm@20542
   688
  \item @{ML Thm.generalize}~@{text "(\<^vec>\<alpha>, \<^vec>x)"}
wenzelm@20542
   689
  corresponds to the @{text "generalize"} rules of
wenzelm@20543
   690
  \figref{fig:subst-rules}.  Here collections of type and term
wenzelm@20543
   691
  variables are generalized simultaneously, specified by the given
wenzelm@20543
   692
  basic names.
wenzelm@20521
   693
wenzelm@20542
   694
  \item @{ML Thm.instantiate}~@{text "(\<^vec>\<alpha>\<^isub>s,
wenzelm@20542
   695
  \<^vec>x\<^isub>\<tau>)"} corresponds to the @{text "instantiate"} rules
wenzelm@20542
   696
  of \figref{fig:subst-rules}.  Type variables are substituted before
wenzelm@20542
   697
  term variables.  Note that the types in @{text "\<^vec>x\<^isub>\<tau>"}
wenzelm@20542
   698
  refer to the instantiated versions.
wenzelm@20542
   699
wenzelm@42401
   700
  \item @{ML Thm.add_axiom}~@{text "ctxt (name, A)"} declares an
wenzelm@35927
   701
  arbitrary proposition as axiom, and retrieves it as a theorem from
wenzelm@35927
   702
  the resulting theory, cf.\ @{text "axiom"} in
wenzelm@35927
   703
  \figref{fig:prim-rules}.  Note that the low-level representation in
wenzelm@35927
   704
  the axiom table may differ slightly from the returned theorem.
wenzelm@20542
   705
wenzelm@30288
   706
  \item @{ML Thm.add_oracle}~@{text "(binding, oracle)"} produces a named
wenzelm@28290
   707
  oracle rule, essentially generating arbitrary axioms on the fly,
wenzelm@28290
   708
  cf.\ @{text "axiom"} in \figref{fig:prim-rules}.
wenzelm@20521
   709
wenzelm@42401
   710
  \item @{ML Thm.add_def}~@{text "ctxt unchecked overloaded (name, c
wenzelm@35927
   711
  \<^vec>x \<equiv> t)"} states a definitional axiom for an existing constant
wenzelm@35927
   712
  @{text "c"}.  Dependencies are recorded via @{ML Theory.add_deps},
wenzelm@35927
   713
  unless the @{text "unchecked"} option is set.  Note that the
wenzelm@35927
   714
  low-level representation in the axiom table may differ slightly from
wenzelm@35927
   715
  the returned theorem.
wenzelm@20542
   716
wenzelm@42401
   717
  \item @{ML Theory.add_deps}~@{text "ctxt name c\<^isub>\<tau> \<^vec>d\<^isub>\<sigma>"}
wenzelm@42401
   718
  declares dependencies of a named specification for constant @{text
wenzelm@42401
   719
  "c\<^isub>\<tau>"}, relative to existing specifications for constants @{text
wenzelm@42401
   720
  "\<^vec>d\<^isub>\<sigma>"}.
wenzelm@20542
   721
wenzelm@20521
   722
  \end{description}
wenzelm@20521
   723
*}
wenzelm@20521
   724
wenzelm@20521
   725
wenzelm@39832
   726
text %mlantiq {*
wenzelm@39832
   727
  \begin{matharray}{rcl}
wenzelm@39832
   728
  @{ML_antiquotation_def "ctyp"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   729
  @{ML_antiquotation_def "cterm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   730
  @{ML_antiquotation_def "cprop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   731
  @{ML_antiquotation_def "thm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   732
  @{ML_antiquotation_def "thms"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   733
  @{ML_antiquotation_def "lemma"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   734
  \end{matharray}
wenzelm@39832
   735
wenzelm@39832
   736
  \begin{rail}
wenzelm@39832
   737
  'ctyp' typ
wenzelm@39832
   738
  ;
wenzelm@39832
   739
  'cterm' term
wenzelm@39832
   740
  ;
wenzelm@39832
   741
  'cprop' prop
wenzelm@39832
   742
  ;
wenzelm@39832
   743
  'thm' thmref
wenzelm@39832
   744
  ;
wenzelm@39832
   745
  'thms' thmrefs
wenzelm@39832
   746
  ;
wenzelm@39832
   747
  'lemma' ('(open)')? ((prop +) + 'and') \\ 'by' method method?
wenzelm@39832
   748
  \end{rail}
wenzelm@39832
   749
wenzelm@39832
   750
  \begin{description}
wenzelm@39832
   751
wenzelm@39832
   752
  \item @{text "@{ctyp \<tau>}"} produces a certified type wrt.\ the
wenzelm@39832
   753
  current background theory --- as abstract value of type @{ML_type
wenzelm@39832
   754
  ctyp}.
wenzelm@39832
   755
wenzelm@39832
   756
  \item @{text "@{cterm t}"} and @{text "@{cprop \<phi>}"} produce a
wenzelm@39832
   757
  certified term wrt.\ the current background theory --- as abstract
wenzelm@39832
   758
  value of type @{ML_type cterm}.
wenzelm@39832
   759
wenzelm@39832
   760
  \item @{text "@{thm a}"} produces a singleton fact --- as abstract
wenzelm@39832
   761
  value of type @{ML_type thm}.
wenzelm@39832
   762
wenzelm@39832
   763
  \item @{text "@{thms a}"} produces a general fact --- as abstract
wenzelm@39832
   764
  value of type @{ML_type "thm list"}.
wenzelm@39832
   765
wenzelm@39832
   766
  \item @{text "@{lemma \<phi> by meth}"} produces a fact that is proven on
wenzelm@39832
   767
  the spot according to the minimal proof, which imitates a terminal
wenzelm@39832
   768
  Isar proof.  The result is an abstract value of type @{ML_type thm}
wenzelm@39832
   769
  or @{ML_type "thm list"}, depending on the number of propositions
wenzelm@39832
   770
  given here.
wenzelm@39832
   771
wenzelm@39832
   772
  The internal derivation object lacks a proper theorem name, but it
wenzelm@39832
   773
  is formally closed, unless the @{text "(open)"} option is specified
wenzelm@39832
   774
  (this may impact performance of applications with proof terms).
wenzelm@39832
   775
wenzelm@39832
   776
  Since ML antiquotations are always evaluated at compile-time, there
wenzelm@39832
   777
  is no run-time overhead even for non-trivial proofs.  Nonetheless,
wenzelm@39832
   778
  the justification is syntactically limited to a single @{command
wenzelm@39832
   779
  "by"} step.  More complex Isar proofs should be done in regular
wenzelm@39832
   780
  theory source, before compiling the corresponding ML text that uses
wenzelm@39832
   781
  the result.
wenzelm@39832
   782
wenzelm@39832
   783
  \end{description}
wenzelm@39832
   784
wenzelm@39832
   785
*}
wenzelm@39832
   786
wenzelm@39832
   787
wenzelm@40126
   788
subsection {* Auxiliary definitions \label{sec:logic-aux} *}
wenzelm@20521
   789
wenzelm@20521
   790
text {*
wenzelm@20543
   791
  Theory @{text "Pure"} provides a few auxiliary definitions, see
wenzelm@20543
   792
  \figref{fig:pure-aux}.  These special constants are normally not
wenzelm@20543
   793
  exposed to the user, but appear in internal encodings.
wenzelm@20501
   794
wenzelm@20501
   795
  \begin{figure}[htb]
wenzelm@20501
   796
  \begin{center}
wenzelm@20498
   797
  \begin{tabular}{ll}
wenzelm@34929
   798
  @{text "conjunction :: prop \<Rightarrow> prop \<Rightarrow> prop"} & (infix @{text "&&&"}) \\
wenzelm@34929
   799
  @{text "\<turnstile> A &&& B \<equiv> (\<And>C. (A \<Longrightarrow> B \<Longrightarrow> C) \<Longrightarrow> C)"} \\[1ex]
wenzelm@20543
   800
  @{text "prop :: prop \<Rightarrow> prop"} & (prefix @{text "#"}, suppressed) \\
wenzelm@20521
   801
  @{text "#A \<equiv> A"} \\[1ex]
wenzelm@20521
   802
  @{text "term :: \<alpha> \<Rightarrow> prop"} & (prefix @{text "TERM"}) \\
wenzelm@20521
   803
  @{text "term x \<equiv> (\<And>A. A \<Longrightarrow> A)"} \\[1ex]
wenzelm@20521
   804
  @{text "TYPE :: \<alpha> itself"} & (prefix @{text "TYPE"}) \\
wenzelm@20521
   805
  @{text "(unspecified)"} \\
wenzelm@20498
   806
  \end{tabular}
wenzelm@20521
   807
  \caption{Definitions of auxiliary connectives}\label{fig:pure-aux}
wenzelm@20501
   808
  \end{center}
wenzelm@20501
   809
  \end{figure}
wenzelm@20501
   810
wenzelm@34929
   811
  The introduction @{text "A \<Longrightarrow> B \<Longrightarrow> A &&& B"}, and eliminations
wenzelm@34929
   812
  (projections) @{text "A &&& B \<Longrightarrow> A"} and @{text "A &&& B \<Longrightarrow> B"} are
wenzelm@34929
   813
  available as derived rules.  Conjunction allows to treat
wenzelm@34929
   814
  simultaneous assumptions and conclusions uniformly, e.g.\ consider
wenzelm@34929
   815
  @{text "A \<Longrightarrow> B \<Longrightarrow> C &&& D"}.  In particular, the goal mechanism
wenzelm@34929
   816
  represents multiple claims as explicit conjunction internally, but
wenzelm@34929
   817
  this is refined (via backwards introduction) into separate sub-goals
wenzelm@34929
   818
  before the user commences the proof; the final result is projected
wenzelm@34929
   819
  into a list of theorems using eliminations (cf.\
wenzelm@20537
   820
  \secref{sec:tactical-goals}).
wenzelm@20498
   821
wenzelm@20537
   822
  The @{text "prop"} marker (@{text "#"}) makes arbitrarily complex
wenzelm@20537
   823
  propositions appear as atomic, without changing the meaning: @{text
wenzelm@20537
   824
  "\<Gamma> \<turnstile> A"} and @{text "\<Gamma> \<turnstile> #A"} are interchangeable.  See
wenzelm@20537
   825
  \secref{sec:tactical-goals} for specific operations.
wenzelm@20521
   826
wenzelm@20543
   827
  The @{text "term"} marker turns any well-typed term into a derivable
wenzelm@20543
   828
  proposition: @{text "\<turnstile> TERM t"} holds unconditionally.  Although
wenzelm@20543
   829
  this is logically vacuous, it allows to treat terms and proofs
wenzelm@20543
   830
  uniformly, similar to a type-theoretic framework.
wenzelm@20498
   831
wenzelm@20537
   832
  The @{text "TYPE"} constructor is the canonical representative of
wenzelm@20537
   833
  the unspecified type @{text "\<alpha> itself"}; it essentially injects the
wenzelm@20537
   834
  language of types into that of terms.  There is specific notation
wenzelm@20537
   835
  @{text "TYPE(\<tau>)"} for @{text "TYPE\<^bsub>\<tau>
wenzelm@20521
   836
 itself\<^esub>"}.
wenzelm@34929
   837
  Although being devoid of any particular meaning, the term @{text
wenzelm@20537
   838
  "TYPE(\<tau>)"} accounts for the type @{text "\<tau>"} within the term
wenzelm@20537
   839
  language.  In particular, @{text "TYPE(\<alpha>)"} may be used as formal
wenzelm@20537
   840
  argument in primitive definitions, in order to circumvent hidden
wenzelm@20537
   841
  polymorphism (cf.\ \secref{sec:terms}).  For example, @{text "c
wenzelm@20537
   842
  TYPE(\<alpha>) \<equiv> A[\<alpha>]"} defines @{text "c :: \<alpha> itself \<Rightarrow> prop"} in terms of
wenzelm@20537
   843
  a proposition @{text "A"} that depends on an additional type
wenzelm@20537
   844
  argument, which is essentially a predicate on types.
wenzelm@20521
   845
*}
wenzelm@20501
   846
wenzelm@20521
   847
text %mlref {*
wenzelm@20521
   848
  \begin{mldecls}
wenzelm@20521
   849
  @{index_ML Conjunction.intr: "thm -> thm -> thm"} \\
wenzelm@20521
   850
  @{index_ML Conjunction.elim: "thm -> thm * thm"} \\
wenzelm@20521
   851
  @{index_ML Drule.mk_term: "cterm -> thm"} \\
wenzelm@20521
   852
  @{index_ML Drule.dest_term: "thm -> cterm"} \\
wenzelm@20521
   853
  @{index_ML Logic.mk_type: "typ -> term"} \\
wenzelm@20521
   854
  @{index_ML Logic.dest_type: "term -> typ"} \\
wenzelm@20521
   855
  \end{mldecls}
wenzelm@20521
   856
wenzelm@20521
   857
  \begin{description}
wenzelm@20521
   858
wenzelm@34929
   859
  \item @{ML Conjunction.intr} derives @{text "A &&& B"} from @{text
wenzelm@20542
   860
  "A"} and @{text "B"}.
wenzelm@20542
   861
wenzelm@20543
   862
  \item @{ML Conjunction.elim} derives @{text "A"} and @{text "B"}
wenzelm@34929
   863
  from @{text "A &&& B"}.
wenzelm@20542
   864
wenzelm@20543
   865
  \item @{ML Drule.mk_term} derives @{text "TERM t"}.
wenzelm@20542
   866
wenzelm@20543
   867
  \item @{ML Drule.dest_term} recovers term @{text "t"} from @{text
wenzelm@20543
   868
  "TERM t"}.
wenzelm@20542
   869
wenzelm@20542
   870
  \item @{ML Logic.mk_type}~@{text "\<tau>"} produces the term @{text
wenzelm@20542
   871
  "TYPE(\<tau>)"}.
wenzelm@20542
   872
wenzelm@20542
   873
  \item @{ML Logic.dest_type}~@{text "TYPE(\<tau>)"} recovers the type
wenzelm@20542
   874
  @{text "\<tau>"}.
wenzelm@20521
   875
wenzelm@20521
   876
  \end{description}
wenzelm@20491
   877
*}
wenzelm@18537
   878
wenzelm@20480
   879
wenzelm@28784
   880
section {* Object-level rules \label{sec:obj-rules} *}
wenzelm@18537
   881
wenzelm@29768
   882
text {*
wenzelm@29768
   883
  The primitive inferences covered so far mostly serve foundational
wenzelm@29768
   884
  purposes.  User-level reasoning usually works via object-level rules
wenzelm@29768
   885
  that are represented as theorems of Pure.  Composition of rules
wenzelm@29771
   886
  involves \emph{backchaining}, \emph{higher-order unification} modulo
wenzelm@29771
   887
  @{text "\<alpha>\<beta>\<eta>"}-conversion of @{text "\<lambda>"}-terms, and so-called
wenzelm@29771
   888
  \emph{lifting} of rules into a context of @{text "\<And>"} and @{text
wenzelm@29774
   889
  "\<Longrightarrow>"} connectives.  Thus the full power of higher-order Natural
wenzelm@29774
   890
  Deduction in Isabelle/Pure becomes readily available.
wenzelm@29769
   891
*}
wenzelm@20491
   892
wenzelm@29769
   893
wenzelm@29769
   894
subsection {* Hereditary Harrop Formulae *}
wenzelm@29769
   895
wenzelm@29769
   896
text {*
wenzelm@29768
   897
  The idea of object-level rules is to model Natural Deduction
wenzelm@29768
   898
  inferences in the style of Gentzen \cite{Gentzen:1935}, but we allow
wenzelm@29768
   899
  arbitrary nesting similar to \cite{extensions91}.  The most basic
wenzelm@29768
   900
  rule format is that of a \emph{Horn Clause}:
wenzelm@29768
   901
  \[
wenzelm@29768
   902
  \infer{@{text "A"}}{@{text "A\<^sub>1"} & @{text "\<dots>"} & @{text "A\<^sub>n"}}
wenzelm@29768
   903
  \]
wenzelm@29768
   904
  where @{text "A, A\<^sub>1, \<dots>, A\<^sub>n"} are atomic propositions
wenzelm@29768
   905
  of the framework, usually of the form @{text "Trueprop B"}, where
wenzelm@29768
   906
  @{text "B"} is a (compound) object-level statement.  This
wenzelm@29768
   907
  object-level inference corresponds to an iterated implication in
wenzelm@29768
   908
  Pure like this:
wenzelm@29768
   909
  \[
wenzelm@29768
   910
  @{text "A\<^sub>1 \<Longrightarrow> \<dots> A\<^sub>n \<Longrightarrow> A"}
wenzelm@29768
   911
  \]
wenzelm@29769
   912
  As an example consider conjunction introduction: @{text "A \<Longrightarrow> B \<Longrightarrow> A \<and>
wenzelm@29769
   913
  B"}.  Any parameters occurring in such rule statements are
wenzelm@29769
   914
  conceptionally treated as arbitrary:
wenzelm@29768
   915
  \[
wenzelm@29769
   916
  @{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
   917
  \]
wenzelm@20491
   918
wenzelm@29769
   919
  Nesting of rules means that the positions of @{text "A\<^sub>i"} may
wenzelm@29770
   920
  again hold compound rules, not just atomic propositions.
wenzelm@29769
   921
  Propositions of this format are called \emph{Hereditary Harrop
wenzelm@29769
   922
  Formulae} in the literature \cite{Miller:1991}.  Here we give an
wenzelm@29769
   923
  inductive characterization as follows:
wenzelm@29768
   924
wenzelm@29768
   925
  \medskip
wenzelm@29768
   926
  \begin{tabular}{ll}
wenzelm@29768
   927
  @{text "\<^bold>x"} & set of variables \\
wenzelm@29768
   928
  @{text "\<^bold>A"} & set of atomic propositions \\
wenzelm@29768
   929
  @{text "\<^bold>H  =  \<And>\<^bold>x\<^sup>*. \<^bold>H\<^sup>* \<Longrightarrow> \<^bold>A"} & set of Hereditary Harrop Formulas \\
wenzelm@29768
   930
  \end{tabular}
wenzelm@29768
   931
  \medskip
wenzelm@29768
   932
wenzelm@39861
   933
  Thus we essentially impose nesting levels on propositions formed
wenzelm@39861
   934
  from @{text "\<And>"} and @{text "\<Longrightarrow>"}.  At each level there is a prefix
wenzelm@39861
   935
  of parameters and compound premises, concluding an atomic
wenzelm@29770
   936
  proposition.  Typical examples are @{text "\<longrightarrow>"}-introduction @{text
wenzelm@29770
   937
  "(A \<Longrightarrow> B) \<Longrightarrow> A \<longrightarrow> B"} or mathematical induction @{text "P 0 \<Longrightarrow> (\<And>n. P n
wenzelm@29770
   938
  \<Longrightarrow> P (Suc n)) \<Longrightarrow> P n"}.  Even deeper nesting occurs in well-founded
wenzelm@29770
   939
  induction @{text "(\<And>x. (\<And>y. y \<prec> x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P x"}, but this
wenzelm@34929
   940
  already marks the limit of rule complexity that is usually seen in
wenzelm@34929
   941
  practice.
wenzelm@29769
   942
wenzelm@29770
   943
  \medskip Regular user-level inferences in Isabelle/Pure always
wenzelm@29770
   944
  maintain the following canonical form of results:
wenzelm@29769
   945
wenzelm@29769
   946
  \begin{itemize}
wenzelm@29768
   947
wenzelm@29774
   948
  \item Normalization by @{text "(A \<Longrightarrow> (\<And>x. B x)) \<equiv> (\<And>x. A \<Longrightarrow> B x)"},
wenzelm@29774
   949
  which is a theorem of Pure, means that quantifiers are pushed in
wenzelm@29774
   950
  front of implication at each level of nesting.  The normal form is a
wenzelm@29774
   951
  Hereditary Harrop Formula.
wenzelm@29769
   952
wenzelm@29769
   953
  \item The outermost prefix of parameters is represented via
wenzelm@29770
   954
  schematic variables: instead of @{text "\<And>\<^vec>x. \<^vec>H \<^vec>x
wenzelm@29774
   955
  \<Longrightarrow> A \<^vec>x"} we have @{text "\<^vec>H ?\<^vec>x \<Longrightarrow> A ?\<^vec>x"}.
wenzelm@29774
   956
  Note that this representation looses information about the order of
wenzelm@29774
   957
  parameters, and vacuous quantifiers vanish automatically.
wenzelm@29769
   958
wenzelm@29769
   959
  \end{itemize}
wenzelm@29769
   960
*}
wenzelm@29769
   961
wenzelm@29771
   962
text %mlref {*
wenzelm@29771
   963
  \begin{mldecls}
wenzelm@30552
   964
  @{index_ML Simplifier.norm_hhf: "thm -> thm"} \\
wenzelm@29771
   965
  \end{mldecls}
wenzelm@29771
   966
wenzelm@29771
   967
  \begin{description}
wenzelm@29771
   968
wenzelm@30552
   969
  \item @{ML Simplifier.norm_hhf}~@{text thm} normalizes the given
wenzelm@29771
   970
  theorem according to the canonical form specified above.  This is
wenzelm@29771
   971
  occasionally helpful to repair some low-level tools that do not
wenzelm@29771
   972
  handle Hereditary Harrop Formulae properly.
wenzelm@29771
   973
wenzelm@29771
   974
  \end{description}
wenzelm@29771
   975
*}
wenzelm@29771
   976
wenzelm@29769
   977
wenzelm@29769
   978
subsection {* Rule composition *}
wenzelm@29769
   979
wenzelm@29769
   980
text {*
wenzelm@29771
   981
  The rule calculus of Isabelle/Pure provides two main inferences:
wenzelm@29771
   982
  @{inference resolution} (i.e.\ back-chaining of rules) and
wenzelm@29771
   983
  @{inference assumption} (i.e.\ closing a branch), both modulo
wenzelm@29771
   984
  higher-order unification.  There are also combined variants, notably
wenzelm@29771
   985
  @{inference elim_resolution} and @{inference dest_resolution}.
wenzelm@20491
   986
wenzelm@29771
   987
  To understand the all-important @{inference resolution} principle,
wenzelm@29771
   988
  we first consider raw @{inference_def composition} (modulo
wenzelm@29771
   989
  higher-order unification with substitution @{text "\<vartheta>"}):
wenzelm@20498
   990
  \[
wenzelm@29771
   991
  \infer[(@{inference_def composition})]{@{text "\<^vec>A\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
   992
  {@{text "\<^vec>A \<Longrightarrow> B"} & @{text "B' \<Longrightarrow> C"} & @{text "B\<vartheta> = B'\<vartheta>"}}
wenzelm@20498
   993
  \]
wenzelm@29771
   994
  Here the conclusion of the first rule is unified with the premise of
wenzelm@29771
   995
  the second; the resulting rule instance inherits the premises of the
wenzelm@29771
   996
  first and conclusion of the second.  Note that @{text "C"} can again
wenzelm@29771
   997
  consist of iterated implications.  We can also permute the premises
wenzelm@29771
   998
  of the second rule back-and-forth in order to compose with @{text
wenzelm@29771
   999
  "B'"} in any position (subsequently we shall always refer to
wenzelm@29771
  1000
  position 1 w.l.o.g.).
wenzelm@20498
  1001
wenzelm@29774
  1002
  In @{inference composition} the internal structure of the common
wenzelm@29774
  1003
  part @{text "B"} and @{text "B'"} is not taken into account.  For
wenzelm@29774
  1004
  proper @{inference resolution} we require @{text "B"} to be atomic,
wenzelm@29774
  1005
  and explicitly observe the structure @{text "\<And>\<^vec>x. \<^vec>H
wenzelm@29774
  1006
  \<^vec>x \<Longrightarrow> B' \<^vec>x"} of the premise of the second rule.  The
wenzelm@29774
  1007
  idea is to adapt the first rule by ``lifting'' it into this context,
wenzelm@29774
  1008
  by means of iterated application of the following inferences:
wenzelm@20498
  1009
  \[
wenzelm@29771
  1010
  \infer[(@{inference_def imp_lift})]{@{text "(\<^vec>H \<Longrightarrow> \<^vec>A) \<Longrightarrow> (\<^vec>H \<Longrightarrow> B)"}}{@{text "\<^vec>A \<Longrightarrow> B"}}
wenzelm@20498
  1011
  \]
wenzelm@20498
  1012
  \[
wenzelm@29771
  1013
  \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
  1014
  \]
wenzelm@29771
  1015
  By combining raw composition with lifting, we get full @{inference
wenzelm@29771
  1016
  resolution} as follows:
wenzelm@20498
  1017
  \[
wenzelm@29771
  1018
  \infer[(@{inference_def resolution})]
wenzelm@20498
  1019
  {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>A (?\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
  1020
  {\begin{tabular}{l}
wenzelm@20498
  1021
    @{text "\<^vec>A ?\<^vec>a \<Longrightarrow> B ?\<^vec>a"} \\
wenzelm@20498
  1022
    @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
wenzelm@20498
  1023
    @{text "(\<lambda>\<^vec>x. B (?\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
wenzelm@20498
  1024
   \end{tabular}}
wenzelm@20498
  1025
  \]
wenzelm@20498
  1026
wenzelm@29774
  1027
  Continued resolution of rules allows to back-chain a problem towards
wenzelm@29774
  1028
  more and sub-problems.  Branches are closed either by resolving with
wenzelm@29774
  1029
  a rule of 0 premises, or by producing a ``short-circuit'' within a
wenzelm@29774
  1030
  solved situation (again modulo unification):
wenzelm@29771
  1031
  \[
wenzelm@29771
  1032
  \infer[(@{inference_def assumption})]{@{text "C\<vartheta>"}}
wenzelm@29771
  1033
  {@{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
  1034
  \]
wenzelm@20498
  1035
wenzelm@29771
  1036
  FIXME @{inference_def elim_resolution}, @{inference_def dest_resolution}
wenzelm@18537
  1037
*}
wenzelm@18537
  1038
wenzelm@29768
  1039
text %mlref {*
wenzelm@29768
  1040
  \begin{mldecls}
wenzelm@29768
  1041
  @{index_ML "op RS": "thm * thm -> thm"} \\
wenzelm@29768
  1042
  @{index_ML "op OF": "thm * thm list -> thm"} \\
wenzelm@29768
  1043
  \end{mldecls}
wenzelm@29768
  1044
wenzelm@29768
  1045
  \begin{description}
wenzelm@29768
  1046
wenzelm@34929
  1047
  \item @{text "rule\<^sub>1 RS rule\<^sub>2"} resolves @{text "rule\<^sub>1"} with @{text
wenzelm@34929
  1048
  "rule\<^sub>2"} according to the @{inference resolution} principle
wenzelm@34929
  1049
  explained above.  Note that the corresponding rule attribute in the
wenzelm@34929
  1050
  Isar language is called @{attribute THEN}.
wenzelm@29768
  1051
wenzelm@29771
  1052
  \item @{text "rule OF rules"} resolves a list of rules with the
wenzelm@29774
  1053
  first rule, addressing its premises @{text "1, \<dots>, length rules"}
wenzelm@29774
  1054
  (operating from last to first).  This means the newly emerging
wenzelm@29774
  1055
  premises are all concatenated, without interfering.  Also note that
wenzelm@29774
  1056
  compared to @{text "RS"}, the rule argument order is swapped: @{text
wenzelm@29774
  1057
  "rule\<^sub>1 RS rule\<^sub>2 = rule\<^sub>2 OF [rule\<^sub>1]"}.
wenzelm@29768
  1058
wenzelm@29768
  1059
  \end{description}
wenzelm@29768
  1060
*}
wenzelm@30272
  1061
wenzelm@18537
  1062
end