doc-src/IsarImplementation/Thy/Logic.thy
author wenzelm
Sun Oct 17 20:25:36 2010 +0100 (2010-10-17)
changeset 39861 b8d89db3e238
parent 39846 cb6634eb8926
child 39864 f3b4fde34cd1
permissions -rw-r--r--
use continental paragraph style, which works better with mixture of (in)formal text;
tuned skips and indents;
tuned;
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@30355
   130
  @{index_ML Sign.add_types: "(binding * int * mixfix) list -> theory -> theory"} \\
wenzelm@36345
   131
  @{index_ML Sign.add_type_abbrev: "binding * string list * typ -> theory -> theory"} \\
wenzelm@30355
   132
  @{index_ML Sign.primitive_class: "binding * class list -> theory -> theory"} \\
wenzelm@20480
   133
  @{index_ML Sign.primitive_classrel: "class * class -> theory -> theory"} \\
wenzelm@20480
   134
  @{index_ML Sign.primitive_arity: "arity -> theory -> theory"} \\
wenzelm@20480
   135
  \end{mldecls}
wenzelm@20480
   136
wenzelm@20480
   137
  \begin{description}
wenzelm@20480
   138
wenzelm@34921
   139
  \item @{ML_type class} represents type classes.
wenzelm@20480
   140
wenzelm@34921
   141
  \item @{ML_type sort} represents sorts, i.e.\ finite intersections
wenzelm@34921
   142
  of classes.  The empty list @{ML "[]: sort"} refers to the empty
wenzelm@34921
   143
  class intersection, i.e.\ the ``full sort''.
wenzelm@20451
   144
wenzelm@34921
   145
  \item @{ML_type arity} represents type arities.  A triple @{text
wenzelm@34921
   146
  "(\<kappa>, \<^vec>s, s) : arity"} represents @{text "\<kappa> :: (\<^vec>s)s"} as
wenzelm@34921
   147
  described above.
wenzelm@20480
   148
wenzelm@20480
   149
  \item @{ML_type typ} represents types; this is a datatype with
wenzelm@20480
   150
  constructors @{ML TFree}, @{ML TVar}, @{ML Type}.
wenzelm@20480
   151
wenzelm@39846
   152
  \item @{ML Term.map_atyps}~@{text "f \<tau>"} applies the mapping @{text
wenzelm@39846
   153
  "f"} to all atomic types (@{ML TFree}, @{ML TVar}) occurring in
wenzelm@39846
   154
  @{text "\<tau>"}.
wenzelm@20519
   155
wenzelm@39846
   156
  \item @{ML Term.fold_atyps}~@{text "f \<tau>"} iterates the operation
wenzelm@39846
   157
  @{text "f"} over all occurrences of atomic types (@{ML TFree}, @{ML
wenzelm@39846
   158
  TVar}) in @{text "\<tau>"}; the type structure is traversed from left to
wenzelm@39846
   159
  right.
wenzelm@20494
   160
wenzelm@20480
   161
  \item @{ML Sign.subsort}~@{text "thy (s\<^isub>1, s\<^isub>2)"}
wenzelm@20480
   162
  tests the subsort relation @{text "s\<^isub>1 \<subseteq> s\<^isub>2"}.
wenzelm@20480
   163
wenzelm@20537
   164
  \item @{ML Sign.of_sort}~@{text "thy (\<tau>, s)"} tests whether type
wenzelm@20537
   165
  @{text "\<tau>"} is of sort @{text "s"}.
wenzelm@20480
   166
wenzelm@20537
   167
  \item @{ML Sign.add_types}~@{text "[(\<kappa>, k, mx), \<dots>]"} declares a new
wenzelm@20494
   168
  type constructors @{text "\<kappa>"} with @{text "k"} arguments and
wenzelm@20480
   169
  optional mixfix syntax.
wenzelm@20451
   170
wenzelm@36345
   171
  \item @{ML Sign.add_type_abbrev}~@{text "(\<kappa>, \<^vec>\<alpha>,
wenzelm@36345
   172
  \<tau>)"} defines a new type abbreviation @{text
wenzelm@36345
   173
  "(\<^vec>\<alpha>)\<kappa> = \<tau>"}.
wenzelm@20480
   174
wenzelm@20480
   175
  \item @{ML Sign.primitive_class}~@{text "(c, [c\<^isub>1, \<dots>,
wenzelm@20537
   176
  c\<^isub>n])"} declares a new class @{text "c"}, together with class
wenzelm@20494
   177
  relations @{text "c \<subseteq> c\<^isub>i"}, for @{text "i = 1, \<dots>, n"}.
wenzelm@20480
   178
wenzelm@20480
   179
  \item @{ML Sign.primitive_classrel}~@{text "(c\<^isub>1,
wenzelm@20543
   180
  c\<^isub>2)"} declares the class relation @{text "c\<^isub>1 \<subseteq>
wenzelm@20480
   181
  c\<^isub>2"}.
wenzelm@20480
   182
wenzelm@20494
   183
  \item @{ML Sign.primitive_arity}~@{text "(\<kappa>, \<^vec>s, s)"} declares
wenzelm@20537
   184
  the arity @{text "\<kappa> :: (\<^vec>s)s"}.
wenzelm@20480
   185
wenzelm@20480
   186
  \end{description}
wenzelm@20437
   187
*}
wenzelm@20437
   188
wenzelm@39832
   189
text %mlantiq {*
wenzelm@39832
   190
  \begin{matharray}{rcl}
wenzelm@39832
   191
  @{ML_antiquotation_def "class"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   192
  @{ML_antiquotation_def "sort"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   193
  @{ML_antiquotation_def "type_name"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   194
  @{ML_antiquotation_def "type_abbrev"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   195
  @{ML_antiquotation_def "nonterminal"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   196
  @{ML_antiquotation_def "typ"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   197
  \end{matharray}
wenzelm@39832
   198
wenzelm@39832
   199
  \begin{rail}
wenzelm@39832
   200
  'class' nameref
wenzelm@39832
   201
  ;
wenzelm@39832
   202
  'sort' sort
wenzelm@39832
   203
  ;
wenzelm@39832
   204
  ('type\_name' | 'type\_abbrev' | 'nonterminal') nameref
wenzelm@39832
   205
  ;
wenzelm@39832
   206
  'typ' type
wenzelm@39832
   207
  ;
wenzelm@39832
   208
  \end{rail}
wenzelm@39832
   209
wenzelm@39832
   210
  \begin{description}
wenzelm@39832
   211
wenzelm@39832
   212
  \item @{text "@{class c}"} inlines the internalized class @{text
wenzelm@39832
   213
  "c"} --- as @{ML_type string} literal.
wenzelm@39832
   214
wenzelm@39832
   215
  \item @{text "@{sort s}"} inlines the internalized sort @{text "s"}
wenzelm@39832
   216
  --- as @{ML_type "string list"} literal.
wenzelm@39832
   217
wenzelm@39832
   218
  \item @{text "@{type_name c}"} inlines the internalized type
wenzelm@39832
   219
  constructor @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   220
wenzelm@39832
   221
  \item @{text "@{type_abbrev c}"} inlines the internalized type
wenzelm@39832
   222
  abbreviation @{text "c"} --- as @{ML_type string} literal.
wenzelm@39832
   223
wenzelm@39832
   224
  \item @{text "@{nonterminal c}"} inlines the internalized syntactic
wenzelm@39832
   225
  type~/ grammar nonterminal @{text "c"} --- as @{ML_type string}
wenzelm@39832
   226
  literal.
wenzelm@39832
   227
wenzelm@39832
   228
  \item @{text "@{typ \<tau>}"} inlines the internalized type @{text "\<tau>"}
wenzelm@39832
   229
  --- as constructor term for datatype @{ML_type typ}.
wenzelm@39832
   230
wenzelm@39832
   231
  \end{description}
wenzelm@39832
   232
*}
wenzelm@39832
   233
wenzelm@20437
   234
wenzelm@20451
   235
section {* Terms \label{sec:terms} *}
wenzelm@18537
   236
wenzelm@18537
   237
text {*
wenzelm@20491
   238
  The language of terms is that of simply-typed @{text "\<lambda>"}-calculus
wenzelm@20520
   239
  with de-Bruijn indices for bound variables (cf.\ \cite{debruijn72}
wenzelm@29761
   240
  or \cite{paulson-ml2}), with the types being determined by the
wenzelm@29761
   241
  corresponding binders.  In contrast, free variables and constants
wenzelm@34929
   242
  have an explicit name and type in each occurrence.
wenzelm@20514
   243
wenzelm@20514
   244
  \medskip A \emph{bound variable} is a natural number @{text "b"},
wenzelm@20537
   245
  which accounts for the number of intermediate binders between the
wenzelm@20537
   246
  variable occurrence in the body and its binding position.  For
wenzelm@34929
   247
  example, the de-Bruijn term @{text "\<lambda>\<^bsub>bool\<^esub>. \<lambda>\<^bsub>bool\<^esub>. 1 \<and> 0"} would
wenzelm@34929
   248
  correspond to @{text "\<lambda>x\<^bsub>bool\<^esub>. \<lambda>y\<^bsub>bool\<^esub>. x \<and> y"} in a named
wenzelm@20543
   249
  representation.  Note that a bound variable may be represented by
wenzelm@20543
   250
  different de-Bruijn indices at different occurrences, depending on
wenzelm@20543
   251
  the nesting of abstractions.
wenzelm@20537
   252
wenzelm@20543
   253
  A \emph{loose variable} is a bound variable that is outside the
wenzelm@20537
   254
  scope of local binders.  The types (and names) for loose variables
wenzelm@20543
   255
  can be managed as a separate context, that is maintained as a stack
wenzelm@20543
   256
  of hypothetical binders.  The core logic operates on closed terms,
wenzelm@20543
   257
  without any loose variables.
wenzelm@20514
   258
wenzelm@20537
   259
  A \emph{fixed variable} is a pair of a basic name and a type, e.g.\
wenzelm@34929
   260
  @{text "(x, \<tau>)"} which is usually printed @{text "x\<^isub>\<tau>"} here.  A
wenzelm@20537
   261
  \emph{schematic variable} is a pair of an indexname and a type,
wenzelm@34929
   262
  e.g.\ @{text "((x, 0), \<tau>)"} which is likewise printed as @{text
wenzelm@20537
   263
  "?x\<^isub>\<tau>"}.
wenzelm@20491
   264
wenzelm@20537
   265
  \medskip A \emph{constant} is a pair of a basic name and a type,
wenzelm@34929
   266
  e.g.\ @{text "(c, \<tau>)"} which is usually printed as @{text "c\<^isub>\<tau>"}
wenzelm@34929
   267
  here.  Constants are declared in the context as polymorphic families
wenzelm@34929
   268
  @{text "c :: \<sigma>"}, meaning that all substitution instances @{text
wenzelm@34929
   269
  "c\<^isub>\<tau>"} for @{text "\<tau> = \<sigma>\<vartheta>"} are valid.
wenzelm@20514
   270
wenzelm@34929
   271
  The vector of \emph{type arguments} of constant @{text "c\<^isub>\<tau>"} wrt.\
wenzelm@34929
   272
  the declaration @{text "c :: \<sigma>"} is defined as the codomain of the
wenzelm@34929
   273
  matcher @{text "\<vartheta> = {?\<alpha>\<^isub>1 \<mapsto> \<tau>\<^isub>1, \<dots>, ?\<alpha>\<^isub>n \<mapsto> \<tau>\<^isub>n}"} presented in
wenzelm@34929
   274
  canonical order @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n)"}, corresponding to the
wenzelm@34929
   275
  left-to-right occurrences of the @{text "\<alpha>\<^isub>i"} in @{text "\<sigma>"}.
wenzelm@34929
   276
  Within a given theory context, there is a one-to-one correspondence
wenzelm@34929
   277
  between any constant @{text "c\<^isub>\<tau>"} and the application @{text "c(\<tau>\<^isub>1,
wenzelm@34929
   278
  \<dots>, \<tau>\<^isub>n)"} of its type arguments.  For example, with @{text "plus :: \<alpha>
wenzelm@34929
   279
  \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"}, the instance @{text "plus\<^bsub>nat \<Rightarrow> nat \<Rightarrow> nat\<^esub>"} corresponds to
wenzelm@34929
   280
  @{text "plus(nat)"}.
wenzelm@20480
   281
wenzelm@20514
   282
  Constant declarations @{text "c :: \<sigma>"} may contain sort constraints
wenzelm@20514
   283
  for type variables in @{text "\<sigma>"}.  These are observed by
wenzelm@20514
   284
  type-inference as expected, but \emph{ignored} by the core logic.
wenzelm@20514
   285
  This means the primitive logic is able to reason with instances of
wenzelm@20537
   286
  polymorphic constants that the user-level type-checker would reject
wenzelm@20537
   287
  due to violation of type class restrictions.
wenzelm@20480
   288
wenzelm@39861
   289
  \medskip An \emph{atomic term} is either a variable or constant.
wenzelm@34929
   290
  The logical category \emph{term} is defined inductively over atomic
wenzelm@34929
   291
  terms, with abstraction and application as follows: @{text "t = b |
wenzelm@34929
   292
  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
   293
  converting between an external representation with named bound
wenzelm@34929
   294
  variables.  Subsequently, we shall use the latter notation instead
wenzelm@34929
   295
  of internal de-Bruijn representation.
wenzelm@20498
   296
wenzelm@20537
   297
  The inductive relation @{text "t :: \<tau>"} assigns a (unique) type to a
wenzelm@20537
   298
  term according to the structure of atomic terms, abstractions, and
wenzelm@20537
   299
  applicatins:
wenzelm@20498
   300
  \[
wenzelm@20501
   301
  \infer{@{text "a\<^isub>\<tau> :: \<tau>"}}{}
wenzelm@20498
   302
  \qquad
wenzelm@20501
   303
  \infer{@{text "(\<lambda>x\<^sub>\<tau>. t) :: \<tau> \<Rightarrow> \<sigma>"}}{@{text "t :: \<sigma>"}}
wenzelm@20501
   304
  \qquad
wenzelm@20501
   305
  \infer{@{text "t u :: \<sigma>"}}{@{text "t :: \<tau> \<Rightarrow> \<sigma>"} & @{text "u :: \<tau>"}}
wenzelm@20498
   306
  \]
wenzelm@20514
   307
  A \emph{well-typed term} is a term that can be typed according to these rules.
wenzelm@20498
   308
wenzelm@20514
   309
  Typing information can be omitted: type-inference is able to
wenzelm@20514
   310
  reconstruct the most general type of a raw term, while assigning
wenzelm@20514
   311
  most general types to all of its variables and constants.
wenzelm@20514
   312
  Type-inference depends on a context of type constraints for fixed
wenzelm@20514
   313
  variables, and declarations for polymorphic constants.
wenzelm@20514
   314
wenzelm@20537
   315
  The identity of atomic terms consists both of the name and the type
wenzelm@20537
   316
  component.  This means that different variables @{text
wenzelm@34929
   317
  "x\<^bsub>\<tau>\<^isub>1\<^esub>"} and @{text "x\<^bsub>\<tau>\<^isub>2\<^esub>"} may become the same after
wenzelm@34929
   318
  type instantiation.  Type-inference rejects variables of the same
wenzelm@34929
   319
  name, but different types.  In contrast, mixed instances of
wenzelm@34929
   320
  polymorphic constants occur routinely.
wenzelm@20514
   321
wenzelm@20514
   322
  \medskip The \emph{hidden polymorphism} of a term @{text "t :: \<sigma>"}
wenzelm@20514
   323
  is the set of type variables occurring in @{text "t"}, but not in
wenzelm@34929
   324
  its type @{text "\<sigma>"}.  This means that the term implicitly depends
wenzelm@34929
   325
  on type arguments that are not accounted in the result type, i.e.\
wenzelm@34929
   326
  there are different type instances @{text "t\<vartheta> :: \<sigma>"} and
wenzelm@34929
   327
  @{text "t\<vartheta>' :: \<sigma>"} with the same type.  This slightly
wenzelm@20543
   328
  pathological situation notoriously demands additional care.
wenzelm@20514
   329
wenzelm@20514
   330
  \medskip A \emph{term abbreviation} is a syntactic definition @{text
wenzelm@20537
   331
  "c\<^isub>\<sigma> \<equiv> t"} of a closed term @{text "t"} of type @{text "\<sigma>"},
wenzelm@20537
   332
  without any hidden polymorphism.  A term abbreviation looks like a
wenzelm@20543
   333
  constant in the syntax, but is expanded before entering the logical
wenzelm@20543
   334
  core.  Abbreviations are usually reverted when printing terms, using
wenzelm@20543
   335
  @{text "t \<rightarrow> c\<^isub>\<sigma>"} as rules for higher-order rewriting.
wenzelm@20519
   336
wenzelm@20519
   337
  \medskip Canonical operations on @{text "\<lambda>"}-terms include @{text
wenzelm@20537
   338
  "\<alpha>\<beta>\<eta>"}-conversion: @{text "\<alpha>"}-conversion refers to capture-free
wenzelm@20519
   339
  renaming of bound variables; @{text "\<beta>"}-conversion contracts an
wenzelm@20537
   340
  abstraction applied to an argument term, substituting the argument
wenzelm@20519
   341
  in the body: @{text "(\<lambda>x. b)a"} becomes @{text "b[a/x]"}; @{text
wenzelm@20519
   342
  "\<eta>"}-conversion contracts vacuous application-abstraction: @{text
wenzelm@20519
   343
  "\<lambda>x. f x"} becomes @{text "f"}, provided that the bound variable
wenzelm@20537
   344
  does not occur in @{text "f"}.
wenzelm@20519
   345
wenzelm@20537
   346
  Terms are normally treated modulo @{text "\<alpha>"}-conversion, which is
wenzelm@20537
   347
  implicit in the de-Bruijn representation.  Names for bound variables
wenzelm@20537
   348
  in abstractions are maintained separately as (meaningless) comments,
wenzelm@20537
   349
  mostly for parsing and printing.  Full @{text "\<alpha>\<beta>\<eta>"}-conversion is
wenzelm@28784
   350
  commonplace in various standard operations (\secref{sec:obj-rules})
wenzelm@28784
   351
  that are based on higher-order unification and matching.
wenzelm@18537
   352
*}
wenzelm@18537
   353
wenzelm@20514
   354
text %mlref {*
wenzelm@20514
   355
  \begin{mldecls}
wenzelm@20514
   356
  @{index_ML_type term} \\
wenzelm@20519
   357
  @{index_ML "op aconv": "term * term -> bool"} \\
wenzelm@39846
   358
  @{index_ML Term.map_types: "(typ -> typ) -> term -> term"} \\
wenzelm@39846
   359
  @{index_ML Term.fold_types: "(typ -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@39846
   360
  @{index_ML Term.map_aterms: "(term -> term) -> term -> term"} \\
wenzelm@39846
   361
  @{index_ML Term.fold_aterms: "(term -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@20547
   362
  \end{mldecls}
wenzelm@20547
   363
  \begin{mldecls}
wenzelm@20514
   364
  @{index_ML fastype_of: "term -> typ"} \\
wenzelm@20519
   365
  @{index_ML lambda: "term -> term -> term"} \\
wenzelm@20519
   366
  @{index_ML betapply: "term * term -> term"} \\
haftmann@33174
   367
  @{index_ML Sign.declare_const: "(binding * typ) * mixfix ->
wenzelm@24972
   368
  theory -> term * theory"} \\
haftmann@33174
   369
  @{index_ML Sign.add_abbrev: "string -> binding * term ->
wenzelm@24972
   370
  theory -> (term * term) * theory"} \\
wenzelm@20519
   371
  @{index_ML Sign.const_typargs: "theory -> string * typ -> typ list"} \\
wenzelm@20519
   372
  @{index_ML Sign.const_instance: "theory -> string * typ list -> typ"} \\
wenzelm@20514
   373
  \end{mldecls}
wenzelm@18537
   374
wenzelm@20514
   375
  \begin{description}
wenzelm@18537
   376
wenzelm@20537
   377
  \item @{ML_type term} represents de-Bruijn terms, with comments in
wenzelm@20537
   378
  abstractions, and explicitly named free variables and constants;
wenzelm@20537
   379
  this is a datatype with constructors @{ML Bound}, @{ML Free}, @{ML
wenzelm@20537
   380
  Var}, @{ML Const}, @{ML Abs}, @{ML "op $"}.
wenzelm@20519
   381
wenzelm@36166
   382
  \item @{text "t"}~@{ML_text aconv}~@{text "u"} checks @{text
wenzelm@20519
   383
  "\<alpha>"}-equivalence of two terms.  This is the basic equality relation
wenzelm@20519
   384
  on type @{ML_type term}; raw datatype equality should only be used
wenzelm@20519
   385
  for operations related to parsing or printing!
wenzelm@20519
   386
wenzelm@39846
   387
  \item @{ML Term.map_types}~@{text "f t"} applies the mapping @{text
wenzelm@20537
   388
  "f"} to all types occurring in @{text "t"}.
wenzelm@20537
   389
wenzelm@39846
   390
  \item @{ML Term.fold_types}~@{text "f t"} iterates the operation
wenzelm@39846
   391
  @{text "f"} over all occurrences of types in @{text "t"}; the term
wenzelm@20537
   392
  structure is traversed from left to right.
wenzelm@20519
   393
wenzelm@39846
   394
  \item @{ML Term.map_aterms}~@{text "f t"} applies the mapping @{text
wenzelm@39846
   395
  "f"} to all atomic terms (@{ML Bound}, @{ML Free}, @{ML Var}, @{ML
wenzelm@20537
   396
  Const}) occurring in @{text "t"}.
wenzelm@20537
   397
wenzelm@39846
   398
  \item @{ML Term.fold_aterms}~@{text "f t"} iterates the operation
wenzelm@39846
   399
  @{text "f"} over all occurrences of atomic terms (@{ML Bound}, @{ML
wenzelm@39846
   400
  Free}, @{ML Var}, @{ML Const}) in @{text "t"}; the term structure is
wenzelm@20519
   401
  traversed from left to right.
wenzelm@20519
   402
wenzelm@20537
   403
  \item @{ML fastype_of}~@{text "t"} determines the type of a
wenzelm@20537
   404
  well-typed term.  This operation is relatively slow, despite the
wenzelm@20537
   405
  omission of any sanity checks.
wenzelm@20519
   406
wenzelm@20519
   407
  \item @{ML lambda}~@{text "a b"} produces an abstraction @{text
wenzelm@20537
   408
  "\<lambda>a. b"}, where occurrences of the atomic term @{text "a"} in the
wenzelm@20537
   409
  body @{text "b"} are replaced by bound variables.
wenzelm@20519
   410
wenzelm@20537
   411
  \item @{ML betapply}~@{text "(t, u)"} produces an application @{text
wenzelm@20537
   412
  "t u"}, with topmost @{text "\<beta>"}-conversion if @{text "t"} is an
wenzelm@20537
   413
  abstraction.
wenzelm@20519
   414
haftmann@33174
   415
  \item @{ML Sign.declare_const}~@{text "((c, \<sigma>), mx)"}
wenzelm@24972
   416
  declares a new constant @{text "c :: \<sigma>"} with optional mixfix
wenzelm@24972
   417
  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@39832
   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@36134
   643
  @{index_ML Thm.add_axiom: "binding * term -> theory -> (string * thm) * theory"} \\
wenzelm@39821
   644
  @{index_ML Thm.add_oracle: "binding * ('a -> cterm) -> theory ->
wenzelm@39821
   645
  (string * ('a -> thm)) * theory"} \\
wenzelm@39821
   646
  @{index_ML Thm.add_def: "bool -> bool -> binding * term -> theory ->
wenzelm@39821
   647
  (string * thm) * theory"} \\
wenzelm@20547
   648
  \end{mldecls}
wenzelm@20547
   649
  \begin{mldecls}
wenzelm@39821
   650
  @{index_ML Theory.add_deps: "string -> string * typ -> (string * typ) list ->
wenzelm@39821
   651
  theory -> theory"} \\
wenzelm@20521
   652
  \end{mldecls}
wenzelm@20521
   653
wenzelm@20521
   654
  \begin{description}
wenzelm@20521
   655
wenzelm@20542
   656
  \item @{ML_type ctyp} and @{ML_type cterm} represent certified types
wenzelm@20542
   657
  and terms, respectively.  These are abstract datatypes that
wenzelm@20542
   658
  guarantee that its values have passed the full well-formedness (and
wenzelm@20542
   659
  well-typedness) checks, relative to the declarations of type
wenzelm@20542
   660
  constructors, constants etc. in the theory.
wenzelm@20542
   661
wenzelm@29768
   662
  \item @{ML Thm.ctyp_of}~@{text "thy \<tau>"} and @{ML
wenzelm@29768
   663
  Thm.cterm_of}~@{text "thy t"} explicitly checks types and terms,
wenzelm@29768
   664
  respectively.  This also involves some basic normalizations, such
wenzelm@29768
   665
  expansion of type and term abbreviations from the theory context.
wenzelm@20547
   666
wenzelm@20547
   667
  Re-certification is relatively slow and should be avoided in tight
wenzelm@20547
   668
  reasoning loops.  There are separate operations to decompose
wenzelm@20547
   669
  certified entities (including actual theorems).
wenzelm@20542
   670
wenzelm@20542
   671
  \item @{ML_type thm} represents proven propositions.  This is an
wenzelm@20542
   672
  abstract datatype that guarantees that its values have been
wenzelm@20542
   673
  constructed by basic principles of the @{ML_struct Thm} module.
wenzelm@39281
   674
  Every @{ML_type thm} value contains a sliding back-reference to the
wenzelm@20543
   675
  enclosing theory, cf.\ \secref{sec:context-theory}.
wenzelm@20542
   676
wenzelm@34929
   677
  \item @{ML proofs} specifies the detail of proof recording within
wenzelm@29768
   678
  @{ML_type thm} values: @{ML 0} records only the names of oracles,
wenzelm@29768
   679
  @{ML 1} records oracle names and propositions, @{ML 2} additionally
wenzelm@29768
   680
  records full proof terms.  Officially named theorems that contribute
wenzelm@34929
   681
  to a result are recorded in any case.
wenzelm@20542
   682
wenzelm@20542
   683
  \item @{ML Thm.assume}, @{ML Thm.forall_intr}, @{ML
wenzelm@20542
   684
  Thm.forall_elim}, @{ML Thm.implies_intr}, and @{ML Thm.implies_elim}
wenzelm@20542
   685
  correspond to the primitive inferences of \figref{fig:prim-rules}.
wenzelm@20542
   686
wenzelm@20542
   687
  \item @{ML Thm.generalize}~@{text "(\<^vec>\<alpha>, \<^vec>x)"}
wenzelm@20542
   688
  corresponds to the @{text "generalize"} rules of
wenzelm@20543
   689
  \figref{fig:subst-rules}.  Here collections of type and term
wenzelm@20543
   690
  variables are generalized simultaneously, specified by the given
wenzelm@20543
   691
  basic names.
wenzelm@20521
   692
wenzelm@20542
   693
  \item @{ML Thm.instantiate}~@{text "(\<^vec>\<alpha>\<^isub>s,
wenzelm@20542
   694
  \<^vec>x\<^isub>\<tau>)"} corresponds to the @{text "instantiate"} rules
wenzelm@20542
   695
  of \figref{fig:subst-rules}.  Type variables are substituted before
wenzelm@20542
   696
  term variables.  Note that the types in @{text "\<^vec>x\<^isub>\<tau>"}
wenzelm@20542
   697
  refer to the instantiated versions.
wenzelm@20542
   698
wenzelm@35927
   699
  \item @{ML Thm.add_axiom}~@{text "(name, A) thy"} declares an
wenzelm@35927
   700
  arbitrary proposition as axiom, and retrieves it as a theorem from
wenzelm@35927
   701
  the resulting theory, cf.\ @{text "axiom"} in
wenzelm@35927
   702
  \figref{fig:prim-rules}.  Note that the low-level representation in
wenzelm@35927
   703
  the axiom table may differ slightly from the returned theorem.
wenzelm@20542
   704
wenzelm@30288
   705
  \item @{ML Thm.add_oracle}~@{text "(binding, oracle)"} produces a named
wenzelm@28290
   706
  oracle rule, essentially generating arbitrary axioms on the fly,
wenzelm@28290
   707
  cf.\ @{text "axiom"} in \figref{fig:prim-rules}.
wenzelm@20521
   708
wenzelm@35927
   709
  \item @{ML Thm.add_def}~@{text "unchecked overloaded (name, c
wenzelm@35927
   710
  \<^vec>x \<equiv> t)"} states a definitional axiom for an existing constant
wenzelm@35927
   711
  @{text "c"}.  Dependencies are recorded via @{ML Theory.add_deps},
wenzelm@35927
   712
  unless the @{text "unchecked"} option is set.  Note that the
wenzelm@35927
   713
  low-level representation in the axiom table may differ slightly from
wenzelm@35927
   714
  the returned theorem.
wenzelm@20542
   715
wenzelm@20542
   716
  \item @{ML Theory.add_deps}~@{text "name c\<^isub>\<tau>
wenzelm@20543
   717
  \<^vec>d\<^isub>\<sigma>"} declares dependencies of a named specification
wenzelm@20543
   718
  for constant @{text "c\<^isub>\<tau>"}, relative to existing
wenzelm@20543
   719
  specifications for constants @{text "\<^vec>d\<^isub>\<sigma>"}.
wenzelm@20542
   720
wenzelm@20521
   721
  \end{description}
wenzelm@20521
   722
*}
wenzelm@20521
   723
wenzelm@20521
   724
wenzelm@39832
   725
text %mlantiq {*
wenzelm@39832
   726
  \begin{matharray}{rcl}
wenzelm@39832
   727
  @{ML_antiquotation_def "ctyp"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   728
  @{ML_antiquotation_def "cterm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   729
  @{ML_antiquotation_def "cprop"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   730
  @{ML_antiquotation_def "thm"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   731
  @{ML_antiquotation_def "thms"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   732
  @{ML_antiquotation_def "lemma"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   733
  \end{matharray}
wenzelm@39832
   734
wenzelm@39832
   735
  \begin{rail}
wenzelm@39832
   736
  'ctyp' typ
wenzelm@39832
   737
  ;
wenzelm@39832
   738
  'cterm' term
wenzelm@39832
   739
  ;
wenzelm@39832
   740
  'cprop' prop
wenzelm@39832
   741
  ;
wenzelm@39832
   742
  'thm' thmref
wenzelm@39832
   743
  ;
wenzelm@39832
   744
  'thms' thmrefs
wenzelm@39832
   745
  ;
wenzelm@39832
   746
  'lemma' ('(open)')? ((prop +) + 'and') \\ 'by' method method?
wenzelm@39832
   747
  \end{rail}
wenzelm@39832
   748
wenzelm@39832
   749
  \begin{description}
wenzelm@39832
   750
wenzelm@39832
   751
  \item @{text "@{ctyp \<tau>}"} produces a certified type wrt.\ the
wenzelm@39832
   752
  current background theory --- as abstract value of type @{ML_type
wenzelm@39832
   753
  ctyp}.
wenzelm@39832
   754
wenzelm@39832
   755
  \item @{text "@{cterm t}"} and @{text "@{cprop \<phi>}"} produce a
wenzelm@39832
   756
  certified term wrt.\ the current background theory --- as abstract
wenzelm@39832
   757
  value of type @{ML_type cterm}.
wenzelm@39832
   758
wenzelm@39832
   759
  \item @{text "@{thm a}"} produces a singleton fact --- as abstract
wenzelm@39832
   760
  value of type @{ML_type thm}.
wenzelm@39832
   761
wenzelm@39832
   762
  \item @{text "@{thms a}"} produces a general fact --- as abstract
wenzelm@39832
   763
  value of type @{ML_type "thm list"}.
wenzelm@39832
   764
wenzelm@39832
   765
  \item @{text "@{lemma \<phi> by meth}"} produces a fact that is proven on
wenzelm@39832
   766
  the spot according to the minimal proof, which imitates a terminal
wenzelm@39832
   767
  Isar proof.  The result is an abstract value of type @{ML_type thm}
wenzelm@39832
   768
  or @{ML_type "thm list"}, depending on the number of propositions
wenzelm@39832
   769
  given here.
wenzelm@39832
   770
wenzelm@39832
   771
  The internal derivation object lacks a proper theorem name, but it
wenzelm@39832
   772
  is formally closed, unless the @{text "(open)"} option is specified
wenzelm@39832
   773
  (this may impact performance of applications with proof terms).
wenzelm@39832
   774
wenzelm@39832
   775
  Since ML antiquotations are always evaluated at compile-time, there
wenzelm@39832
   776
  is no run-time overhead even for non-trivial proofs.  Nonetheless,
wenzelm@39832
   777
  the justification is syntactically limited to a single @{command
wenzelm@39832
   778
  "by"} step.  More complex Isar proofs should be done in regular
wenzelm@39832
   779
  theory source, before compiling the corresponding ML text that uses
wenzelm@39832
   780
  the result.
wenzelm@39832
   781
wenzelm@39832
   782
  \end{description}
wenzelm@39832
   783
wenzelm@39832
   784
*}
wenzelm@39832
   785
wenzelm@39832
   786
wenzelm@20543
   787
subsection {* Auxiliary definitions *}
wenzelm@20521
   788
wenzelm@20521
   789
text {*
wenzelm@20543
   790
  Theory @{text "Pure"} provides a few auxiliary definitions, see
wenzelm@20543
   791
  \figref{fig:pure-aux}.  These special constants are normally not
wenzelm@20543
   792
  exposed to the user, but appear in internal encodings.
wenzelm@20501
   793
wenzelm@20501
   794
  \begin{figure}[htb]
wenzelm@20501
   795
  \begin{center}
wenzelm@20498
   796
  \begin{tabular}{ll}
wenzelm@34929
   797
  @{text "conjunction :: prop \<Rightarrow> prop \<Rightarrow> prop"} & (infix @{text "&&&"}) \\
wenzelm@34929
   798
  @{text "\<turnstile> A &&& B \<equiv> (\<And>C. (A \<Longrightarrow> B \<Longrightarrow> C) \<Longrightarrow> C)"} \\[1ex]
wenzelm@20543
   799
  @{text "prop :: prop \<Rightarrow> prop"} & (prefix @{text "#"}, suppressed) \\
wenzelm@20521
   800
  @{text "#A \<equiv> A"} \\[1ex]
wenzelm@20521
   801
  @{text "term :: \<alpha> \<Rightarrow> prop"} & (prefix @{text "TERM"}) \\
wenzelm@20521
   802
  @{text "term x \<equiv> (\<And>A. A \<Longrightarrow> A)"} \\[1ex]
wenzelm@20521
   803
  @{text "TYPE :: \<alpha> itself"} & (prefix @{text "TYPE"}) \\
wenzelm@20521
   804
  @{text "(unspecified)"} \\
wenzelm@20498
   805
  \end{tabular}
wenzelm@20521
   806
  \caption{Definitions of auxiliary connectives}\label{fig:pure-aux}
wenzelm@20501
   807
  \end{center}
wenzelm@20501
   808
  \end{figure}
wenzelm@20501
   809
wenzelm@34929
   810
  The introduction @{text "A \<Longrightarrow> B \<Longrightarrow> A &&& B"}, and eliminations
wenzelm@34929
   811
  (projections) @{text "A &&& B \<Longrightarrow> A"} and @{text "A &&& B \<Longrightarrow> B"} are
wenzelm@34929
   812
  available as derived rules.  Conjunction allows to treat
wenzelm@34929
   813
  simultaneous assumptions and conclusions uniformly, e.g.\ consider
wenzelm@34929
   814
  @{text "A \<Longrightarrow> B \<Longrightarrow> C &&& D"}.  In particular, the goal mechanism
wenzelm@34929
   815
  represents multiple claims as explicit conjunction internally, but
wenzelm@34929
   816
  this is refined (via backwards introduction) into separate sub-goals
wenzelm@34929
   817
  before the user commences the proof; the final result is projected
wenzelm@34929
   818
  into a list of theorems using eliminations (cf.\
wenzelm@20537
   819
  \secref{sec:tactical-goals}).
wenzelm@20498
   820
wenzelm@20537
   821
  The @{text "prop"} marker (@{text "#"}) makes arbitrarily complex
wenzelm@20537
   822
  propositions appear as atomic, without changing the meaning: @{text
wenzelm@20537
   823
  "\<Gamma> \<turnstile> A"} and @{text "\<Gamma> \<turnstile> #A"} are interchangeable.  See
wenzelm@20537
   824
  \secref{sec:tactical-goals} for specific operations.
wenzelm@20521
   825
wenzelm@20543
   826
  The @{text "term"} marker turns any well-typed term into a derivable
wenzelm@20543
   827
  proposition: @{text "\<turnstile> TERM t"} holds unconditionally.  Although
wenzelm@20543
   828
  this is logically vacuous, it allows to treat terms and proofs
wenzelm@20543
   829
  uniformly, similar to a type-theoretic framework.
wenzelm@20498
   830
wenzelm@20537
   831
  The @{text "TYPE"} constructor is the canonical representative of
wenzelm@20537
   832
  the unspecified type @{text "\<alpha> itself"}; it essentially injects the
wenzelm@20537
   833
  language of types into that of terms.  There is specific notation
wenzelm@20537
   834
  @{text "TYPE(\<tau>)"} for @{text "TYPE\<^bsub>\<tau>
wenzelm@20521
   835
 itself\<^esub>"}.
wenzelm@34929
   836
  Although being devoid of any particular meaning, the term @{text
wenzelm@20537
   837
  "TYPE(\<tau>)"} accounts for the type @{text "\<tau>"} within the term
wenzelm@20537
   838
  language.  In particular, @{text "TYPE(\<alpha>)"} may be used as formal
wenzelm@20537
   839
  argument in primitive definitions, in order to circumvent hidden
wenzelm@20537
   840
  polymorphism (cf.\ \secref{sec:terms}).  For example, @{text "c
wenzelm@20537
   841
  TYPE(\<alpha>) \<equiv> A[\<alpha>]"} defines @{text "c :: \<alpha> itself \<Rightarrow> prop"} in terms of
wenzelm@20537
   842
  a proposition @{text "A"} that depends on an additional type
wenzelm@20537
   843
  argument, which is essentially a predicate on types.
wenzelm@20521
   844
*}
wenzelm@20501
   845
wenzelm@20521
   846
text %mlref {*
wenzelm@20521
   847
  \begin{mldecls}
wenzelm@20521
   848
  @{index_ML Conjunction.intr: "thm -> thm -> thm"} \\
wenzelm@20521
   849
  @{index_ML Conjunction.elim: "thm -> thm * thm"} \\
wenzelm@20521
   850
  @{index_ML Drule.mk_term: "cterm -> thm"} \\
wenzelm@20521
   851
  @{index_ML Drule.dest_term: "thm -> cterm"} \\
wenzelm@20521
   852
  @{index_ML Logic.mk_type: "typ -> term"} \\
wenzelm@20521
   853
  @{index_ML Logic.dest_type: "term -> typ"} \\
wenzelm@20521
   854
  \end{mldecls}
wenzelm@20521
   855
wenzelm@20521
   856
  \begin{description}
wenzelm@20521
   857
wenzelm@34929
   858
  \item @{ML Conjunction.intr} derives @{text "A &&& B"} from @{text
wenzelm@20542
   859
  "A"} and @{text "B"}.
wenzelm@20542
   860
wenzelm@20543
   861
  \item @{ML Conjunction.elim} derives @{text "A"} and @{text "B"}
wenzelm@34929
   862
  from @{text "A &&& B"}.
wenzelm@20542
   863
wenzelm@20543
   864
  \item @{ML Drule.mk_term} derives @{text "TERM t"}.
wenzelm@20542
   865
wenzelm@20543
   866
  \item @{ML Drule.dest_term} recovers term @{text "t"} from @{text
wenzelm@20543
   867
  "TERM t"}.
wenzelm@20542
   868
wenzelm@20542
   869
  \item @{ML Logic.mk_type}~@{text "\<tau>"} produces the term @{text
wenzelm@20542
   870
  "TYPE(\<tau>)"}.
wenzelm@20542
   871
wenzelm@20542
   872
  \item @{ML Logic.dest_type}~@{text "TYPE(\<tau>)"} recovers the type
wenzelm@20542
   873
  @{text "\<tau>"}.
wenzelm@20521
   874
wenzelm@20521
   875
  \end{description}
wenzelm@20491
   876
*}
wenzelm@18537
   877
wenzelm@20480
   878
wenzelm@28784
   879
section {* Object-level rules \label{sec:obj-rules} *}
wenzelm@18537
   880
wenzelm@29768
   881
text {*
wenzelm@29768
   882
  The primitive inferences covered so far mostly serve foundational
wenzelm@29768
   883
  purposes.  User-level reasoning usually works via object-level rules
wenzelm@29768
   884
  that are represented as theorems of Pure.  Composition of rules
wenzelm@29771
   885
  involves \emph{backchaining}, \emph{higher-order unification} modulo
wenzelm@29771
   886
  @{text "\<alpha>\<beta>\<eta>"}-conversion of @{text "\<lambda>"}-terms, and so-called
wenzelm@29771
   887
  \emph{lifting} of rules into a context of @{text "\<And>"} and @{text
wenzelm@29774
   888
  "\<Longrightarrow>"} connectives.  Thus the full power of higher-order Natural
wenzelm@29774
   889
  Deduction in Isabelle/Pure becomes readily available.
wenzelm@29769
   890
*}
wenzelm@20491
   891
wenzelm@29769
   892
wenzelm@29769
   893
subsection {* Hereditary Harrop Formulae *}
wenzelm@29769
   894
wenzelm@29769
   895
text {*
wenzelm@29768
   896
  The idea of object-level rules is to model Natural Deduction
wenzelm@29768
   897
  inferences in the style of Gentzen \cite{Gentzen:1935}, but we allow
wenzelm@29768
   898
  arbitrary nesting similar to \cite{extensions91}.  The most basic
wenzelm@29768
   899
  rule format is that of a \emph{Horn Clause}:
wenzelm@29768
   900
  \[
wenzelm@29768
   901
  \infer{@{text "A"}}{@{text "A\<^sub>1"} & @{text "\<dots>"} & @{text "A\<^sub>n"}}
wenzelm@29768
   902
  \]
wenzelm@29768
   903
  where @{text "A, A\<^sub>1, \<dots>, A\<^sub>n"} are atomic propositions
wenzelm@29768
   904
  of the framework, usually of the form @{text "Trueprop B"}, where
wenzelm@29768
   905
  @{text "B"} is a (compound) object-level statement.  This
wenzelm@29768
   906
  object-level inference corresponds to an iterated implication in
wenzelm@29768
   907
  Pure like this:
wenzelm@29768
   908
  \[
wenzelm@29768
   909
  @{text "A\<^sub>1 \<Longrightarrow> \<dots> A\<^sub>n \<Longrightarrow> A"}
wenzelm@29768
   910
  \]
wenzelm@29769
   911
  As an example consider conjunction introduction: @{text "A \<Longrightarrow> B \<Longrightarrow> A \<and>
wenzelm@29769
   912
  B"}.  Any parameters occurring in such rule statements are
wenzelm@29769
   913
  conceptionally treated as arbitrary:
wenzelm@29768
   914
  \[
wenzelm@29769
   915
  @{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
   916
  \]
wenzelm@20491
   917
wenzelm@29769
   918
  Nesting of rules means that the positions of @{text "A\<^sub>i"} may
wenzelm@29770
   919
  again hold compound rules, not just atomic propositions.
wenzelm@29769
   920
  Propositions of this format are called \emph{Hereditary Harrop
wenzelm@29769
   921
  Formulae} in the literature \cite{Miller:1991}.  Here we give an
wenzelm@29769
   922
  inductive characterization as follows:
wenzelm@29768
   923
wenzelm@29768
   924
  \medskip
wenzelm@29768
   925
  \begin{tabular}{ll}
wenzelm@29768
   926
  @{text "\<^bold>x"} & set of variables \\
wenzelm@29768
   927
  @{text "\<^bold>A"} & set of atomic propositions \\
wenzelm@29768
   928
  @{text "\<^bold>H  =  \<And>\<^bold>x\<^sup>*. \<^bold>H\<^sup>* \<Longrightarrow> \<^bold>A"} & set of Hereditary Harrop Formulas \\
wenzelm@29768
   929
  \end{tabular}
wenzelm@29768
   930
  \medskip
wenzelm@29768
   931
wenzelm@39861
   932
  Thus we essentially impose nesting levels on propositions formed
wenzelm@39861
   933
  from @{text "\<And>"} and @{text "\<Longrightarrow>"}.  At each level there is a prefix
wenzelm@39861
   934
  of parameters and compound premises, concluding an atomic
wenzelm@29770
   935
  proposition.  Typical examples are @{text "\<longrightarrow>"}-introduction @{text
wenzelm@29770
   936
  "(A \<Longrightarrow> B) \<Longrightarrow> A \<longrightarrow> B"} or mathematical induction @{text "P 0 \<Longrightarrow> (\<And>n. P n
wenzelm@29770
   937
  \<Longrightarrow> P (Suc n)) \<Longrightarrow> P n"}.  Even deeper nesting occurs in well-founded
wenzelm@29770
   938
  induction @{text "(\<And>x. (\<And>y. y \<prec> x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P x"}, but this
wenzelm@34929
   939
  already marks the limit of rule complexity that is usually seen in
wenzelm@34929
   940
  practice.
wenzelm@29769
   941
wenzelm@29770
   942
  \medskip Regular user-level inferences in Isabelle/Pure always
wenzelm@29770
   943
  maintain the following canonical form of results:
wenzelm@29769
   944
wenzelm@29769
   945
  \begin{itemize}
wenzelm@29768
   946
wenzelm@29774
   947
  \item Normalization by @{text "(A \<Longrightarrow> (\<And>x. B x)) \<equiv> (\<And>x. A \<Longrightarrow> B x)"},
wenzelm@29774
   948
  which is a theorem of Pure, means that quantifiers are pushed in
wenzelm@29774
   949
  front of implication at each level of nesting.  The normal form is a
wenzelm@29774
   950
  Hereditary Harrop Formula.
wenzelm@29769
   951
wenzelm@29769
   952
  \item The outermost prefix of parameters is represented via
wenzelm@29770
   953
  schematic variables: instead of @{text "\<And>\<^vec>x. \<^vec>H \<^vec>x
wenzelm@29774
   954
  \<Longrightarrow> A \<^vec>x"} we have @{text "\<^vec>H ?\<^vec>x \<Longrightarrow> A ?\<^vec>x"}.
wenzelm@29774
   955
  Note that this representation looses information about the order of
wenzelm@29774
   956
  parameters, and vacuous quantifiers vanish automatically.
wenzelm@29769
   957
wenzelm@29769
   958
  \end{itemize}
wenzelm@29769
   959
*}
wenzelm@29769
   960
wenzelm@29771
   961
text %mlref {*
wenzelm@29771
   962
  \begin{mldecls}
wenzelm@30552
   963
  @{index_ML Simplifier.norm_hhf: "thm -> thm"} \\
wenzelm@29771
   964
  \end{mldecls}
wenzelm@29771
   965
wenzelm@29771
   966
  \begin{description}
wenzelm@29771
   967
wenzelm@30552
   968
  \item @{ML Simplifier.norm_hhf}~@{text thm} normalizes the given
wenzelm@29771
   969
  theorem according to the canonical form specified above.  This is
wenzelm@29771
   970
  occasionally helpful to repair some low-level tools that do not
wenzelm@29771
   971
  handle Hereditary Harrop Formulae properly.
wenzelm@29771
   972
wenzelm@29771
   973
  \end{description}
wenzelm@29771
   974
*}
wenzelm@29771
   975
wenzelm@29769
   976
wenzelm@29769
   977
subsection {* Rule composition *}
wenzelm@29769
   978
wenzelm@29769
   979
text {*
wenzelm@29771
   980
  The rule calculus of Isabelle/Pure provides two main inferences:
wenzelm@29771
   981
  @{inference resolution} (i.e.\ back-chaining of rules) and
wenzelm@29771
   982
  @{inference assumption} (i.e.\ closing a branch), both modulo
wenzelm@29771
   983
  higher-order unification.  There are also combined variants, notably
wenzelm@29771
   984
  @{inference elim_resolution} and @{inference dest_resolution}.
wenzelm@20491
   985
wenzelm@29771
   986
  To understand the all-important @{inference resolution} principle,
wenzelm@29771
   987
  we first consider raw @{inference_def composition} (modulo
wenzelm@29771
   988
  higher-order unification with substitution @{text "\<vartheta>"}):
wenzelm@20498
   989
  \[
wenzelm@29771
   990
  \infer[(@{inference_def composition})]{@{text "\<^vec>A\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
   991
  {@{text "\<^vec>A \<Longrightarrow> B"} & @{text "B' \<Longrightarrow> C"} & @{text "B\<vartheta> = B'\<vartheta>"}}
wenzelm@20498
   992
  \]
wenzelm@29771
   993
  Here the conclusion of the first rule is unified with the premise of
wenzelm@29771
   994
  the second; the resulting rule instance inherits the premises of the
wenzelm@29771
   995
  first and conclusion of the second.  Note that @{text "C"} can again
wenzelm@29771
   996
  consist of iterated implications.  We can also permute the premises
wenzelm@29771
   997
  of the second rule back-and-forth in order to compose with @{text
wenzelm@29771
   998
  "B'"} in any position (subsequently we shall always refer to
wenzelm@29771
   999
  position 1 w.l.o.g.).
wenzelm@20498
  1000
wenzelm@29774
  1001
  In @{inference composition} the internal structure of the common
wenzelm@29774
  1002
  part @{text "B"} and @{text "B'"} is not taken into account.  For
wenzelm@29774
  1003
  proper @{inference resolution} we require @{text "B"} to be atomic,
wenzelm@29774
  1004
  and explicitly observe the structure @{text "\<And>\<^vec>x. \<^vec>H
wenzelm@29774
  1005
  \<^vec>x \<Longrightarrow> B' \<^vec>x"} of the premise of the second rule.  The
wenzelm@29774
  1006
  idea is to adapt the first rule by ``lifting'' it into this context,
wenzelm@29774
  1007
  by means of iterated application of the following inferences:
wenzelm@20498
  1008
  \[
wenzelm@29771
  1009
  \infer[(@{inference_def imp_lift})]{@{text "(\<^vec>H \<Longrightarrow> \<^vec>A) \<Longrightarrow> (\<^vec>H \<Longrightarrow> B)"}}{@{text "\<^vec>A \<Longrightarrow> B"}}
wenzelm@20498
  1010
  \]
wenzelm@20498
  1011
  \[
wenzelm@29771
  1012
  \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
  1013
  \]
wenzelm@29771
  1014
  By combining raw composition with lifting, we get full @{inference
wenzelm@29771
  1015
  resolution} as follows:
wenzelm@20498
  1016
  \[
wenzelm@29771
  1017
  \infer[(@{inference_def resolution})]
wenzelm@20498
  1018
  {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>A (?\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
  1019
  {\begin{tabular}{l}
wenzelm@20498
  1020
    @{text "\<^vec>A ?\<^vec>a \<Longrightarrow> B ?\<^vec>a"} \\
wenzelm@20498
  1021
    @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
wenzelm@20498
  1022
    @{text "(\<lambda>\<^vec>x. B (?\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
wenzelm@20498
  1023
   \end{tabular}}
wenzelm@20498
  1024
  \]
wenzelm@20498
  1025
wenzelm@29774
  1026
  Continued resolution of rules allows to back-chain a problem towards
wenzelm@29774
  1027
  more and sub-problems.  Branches are closed either by resolving with
wenzelm@29774
  1028
  a rule of 0 premises, or by producing a ``short-circuit'' within a
wenzelm@29774
  1029
  solved situation (again modulo unification):
wenzelm@29771
  1030
  \[
wenzelm@29771
  1031
  \infer[(@{inference_def assumption})]{@{text "C\<vartheta>"}}
wenzelm@29771
  1032
  {@{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
  1033
  \]
wenzelm@20498
  1034
wenzelm@29771
  1035
  FIXME @{inference_def elim_resolution}, @{inference_def dest_resolution}
wenzelm@18537
  1036
*}
wenzelm@18537
  1037
wenzelm@29768
  1038
text %mlref {*
wenzelm@29768
  1039
  \begin{mldecls}
wenzelm@29768
  1040
  @{index_ML "op RS": "thm * thm -> thm"} \\
wenzelm@29768
  1041
  @{index_ML "op OF": "thm * thm list -> thm"} \\
wenzelm@29768
  1042
  \end{mldecls}
wenzelm@29768
  1043
wenzelm@29768
  1044
  \begin{description}
wenzelm@29768
  1045
wenzelm@34929
  1046
  \item @{text "rule\<^sub>1 RS rule\<^sub>2"} resolves @{text "rule\<^sub>1"} with @{text
wenzelm@34929
  1047
  "rule\<^sub>2"} according to the @{inference resolution} principle
wenzelm@34929
  1048
  explained above.  Note that the corresponding rule attribute in the
wenzelm@34929
  1049
  Isar language is called @{attribute THEN}.
wenzelm@29768
  1050
wenzelm@29771
  1051
  \item @{text "rule OF rules"} resolves a list of rules with the
wenzelm@29774
  1052
  first rule, addressing its premises @{text "1, \<dots>, length rules"}
wenzelm@29774
  1053
  (operating from last to first).  This means the newly emerging
wenzelm@29774
  1054
  premises are all concatenated, without interfering.  Also note that
wenzelm@29774
  1055
  compared to @{text "RS"}, the rule argument order is swapped: @{text
wenzelm@29774
  1056
  "rule\<^sub>1 RS rule\<^sub>2 = rule\<^sub>2 OF [rule\<^sub>1]"}.
wenzelm@29768
  1057
wenzelm@29768
  1058
  \end{description}
wenzelm@29768
  1059
*}
wenzelm@30272
  1060
wenzelm@18537
  1061
end