doc-src/IsarImplementation/Thy/Logic.thy
author wenzelm
Mon Feb 16 21:39:19 2009 +0100 (2009-02-16)
changeset 29761 2b658e50683a
parent 29758 7a3b5bbed313
child 29768 64a50ff3f308
permissions -rw-r--r--
minor tuning and typographic fixes;
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@20480
     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@20537
    27
  of the core calculus.}
wenzelm@20480
    28
*}
wenzelm@20480
    29
wenzelm@20480
    30
wenzelm@20451
    31
section {* Types \label{sec:types} *}
wenzelm@20437
    32
wenzelm@20437
    33
text {*
wenzelm@20480
    34
  The language of types is an uninterpreted order-sorted first-order
wenzelm@20480
    35
  algebra; types are qualified by ordered type classes.
wenzelm@20480
    36
wenzelm@20480
    37
  \medskip A \emph{type class} is an abstract syntactic entity
wenzelm@20480
    38
  declared in the theory context.  The \emph{subclass relation} @{text
wenzelm@20480
    39
  "c\<^isub>1 \<subseteq> c\<^isub>2"} is specified by stating an acyclic
wenzelm@20491
    40
  generating relation; the transitive closure is maintained
wenzelm@20491
    41
  internally.  The resulting relation is an ordering: reflexive,
wenzelm@20491
    42
  transitive, and antisymmetric.
wenzelm@20451
    43
wenzelm@20537
    44
  A \emph{sort} is a list of type classes written as @{text "s =
wenzelm@20537
    45
  {c\<^isub>1, \<dots>, c\<^isub>m}"}, which represents symbolic
wenzelm@20480
    46
  intersection.  Notationally, the curly braces are omitted for
wenzelm@20480
    47
  singleton intersections, i.e.\ any class @{text "c"} may be read as
wenzelm@20480
    48
  a sort @{text "{c}"}.  The ordering on type classes is extended to
wenzelm@20491
    49
  sorts according to the meaning of intersections: @{text
wenzelm@20491
    50
  "{c\<^isub>1, \<dots> c\<^isub>m} \<subseteq> {d\<^isub>1, \<dots>, d\<^isub>n}"} iff
wenzelm@20491
    51
  @{text "\<forall>j. \<exists>i. c\<^isub>i \<subseteq> d\<^isub>j"}.  The empty intersection
wenzelm@20491
    52
  @{text "{}"} refers to the universal sort, which is the largest
wenzelm@20491
    53
  element wrt.\ the sort order.  The intersections of all (finitely
wenzelm@20491
    54
  many) classes declared in the current theory are the minimal
wenzelm@20491
    55
  elements wrt.\ the sort order.
wenzelm@20480
    56
wenzelm@20491
    57
  \medskip A \emph{fixed type variable} is a pair of a basic name
wenzelm@20537
    58
  (starting with a @{text "'"} character) and a sort constraint, e.g.\
wenzelm@20537
    59
  @{text "('a, s)"} which is usually printed as @{text "\<alpha>\<^isub>s"}.
wenzelm@20537
    60
  A \emph{schematic type variable} is a pair of an indexname and a
wenzelm@20537
    61
  sort constraint, e.g.\ @{text "(('a, 0), s)"} which is usually
wenzelm@20537
    62
  printed as @{text "?\<alpha>\<^isub>s"}.
wenzelm@20451
    63
wenzelm@20480
    64
  Note that \emph{all} syntactic components contribute to the identity
wenzelm@20493
    65
  of type variables, including the sort constraint.  The core logic
wenzelm@20493
    66
  handles type variables with the same name but different sorts as
wenzelm@20493
    67
  different, although some outer layers of the system make it hard to
wenzelm@20493
    68
  produce anything like this.
wenzelm@20451
    69
wenzelm@20493
    70
  A \emph{type constructor} @{text "\<kappa>"} is a @{text "k"}-ary operator
wenzelm@20493
    71
  on types declared in the theory.  Type constructor application is
wenzelm@20537
    72
  written postfix as @{text "(\<alpha>\<^isub>1, \<dots>, \<alpha>\<^isub>k)\<kappa>"}.  For
wenzelm@20537
    73
  @{text "k = 0"} the argument tuple is omitted, e.g.\ @{text "prop"}
wenzelm@20537
    74
  instead of @{text "()prop"}.  For @{text "k = 1"} the parentheses
wenzelm@20537
    75
  are omitted, e.g.\ @{text "\<alpha> list"} instead of @{text "(\<alpha>)list"}.
wenzelm@20537
    76
  Further notation is provided for specific constructors, notably the
wenzelm@20537
    77
  right-associative infix @{text "\<alpha> \<Rightarrow> \<beta>"} instead of @{text "(\<alpha>,
wenzelm@20537
    78
  \<beta>)fun"}.
wenzelm@20480
    79
  
wenzelm@20537
    80
  A \emph{type} is defined inductively over type variables and type
wenzelm@20537
    81
  constructors as follows: @{text "\<tau> = \<alpha>\<^isub>s | ?\<alpha>\<^isub>s |
wenzelm@20543
    82
  (\<tau>\<^sub>1, \<dots>, \<tau>\<^sub>k)\<kappa>"}.
wenzelm@20451
    83
wenzelm@20514
    84
  A \emph{type abbreviation} is a syntactic definition @{text
wenzelm@20494
    85
  "(\<^vec>\<alpha>)\<kappa> = \<tau>"} of an arbitrary type expression @{text "\<tau>"} over
wenzelm@20537
    86
  variables @{text "\<^vec>\<alpha>"}.  Type abbreviations appear as type
wenzelm@20537
    87
  constructors in the syntax, but are expanded before entering the
wenzelm@20537
    88
  logical core.
wenzelm@20480
    89
wenzelm@20480
    90
  A \emph{type arity} declares the image behavior of a type
wenzelm@20494
    91
  constructor wrt.\ the algebra of sorts: @{text "\<kappa> :: (s\<^isub>1, \<dots>,
wenzelm@20494
    92
  s\<^isub>k)s"} means that @{text "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>k)\<kappa>"} is
wenzelm@20494
    93
  of sort @{text "s"} if every argument type @{text "\<tau>\<^isub>i"} is
wenzelm@20494
    94
  of sort @{text "s\<^isub>i"}.  Arity declarations are implicitly
wenzelm@20494
    95
  completed, i.e.\ @{text "\<kappa> :: (\<^vec>s)c"} entails @{text "\<kappa> ::
wenzelm@20491
    96
  (\<^vec>s)c'"} for any @{text "c' \<supseteq> c"}.
wenzelm@20491
    97
wenzelm@20491
    98
  \medskip The sort algebra is always maintained as \emph{coregular},
wenzelm@20491
    99
  which means that type arities are consistent with the subclass
wenzelm@20537
   100
  relation: for any type constructor @{text "\<kappa>"}, and classes @{text
wenzelm@20537
   101
  "c\<^isub>1 \<subseteq> c\<^isub>2"}, and arities @{text "\<kappa> ::
wenzelm@20537
   102
  (\<^vec>s\<^isub>1)c\<^isub>1"} and @{text "\<kappa> ::
wenzelm@20537
   103
  (\<^vec>s\<^isub>2)c\<^isub>2"} holds @{text "\<^vec>s\<^isub>1 \<subseteq>
wenzelm@20537
   104
  \<^vec>s\<^isub>2"} component-wise.
wenzelm@20451
   105
wenzelm@20491
   106
  The key property of a coregular order-sorted algebra is that sort
wenzelm@20537
   107
  constraints can be solved in a most general fashion: for each type
wenzelm@20537
   108
  constructor @{text "\<kappa>"} and sort @{text "s"} there is a most general
wenzelm@20537
   109
  vector of argument sorts @{text "(s\<^isub>1, \<dots>, s\<^isub>k)"} such
wenzelm@20537
   110
  that a type scheme @{text "(\<alpha>\<^bsub>s\<^isub>1\<^esub>, \<dots>,
wenzelm@20537
   111
  \<alpha>\<^bsub>s\<^isub>k\<^esub>)\<kappa>"} is of sort @{text "s"}.
wenzelm@20543
   112
  Consequently, type unification has most general solutions (modulo
wenzelm@20543
   113
  equivalence of sorts), so type-inference produces primary types as
wenzelm@20543
   114
  expected \cite{nipkow-prehofer}.
wenzelm@20480
   115
*}
wenzelm@20451
   116
wenzelm@20480
   117
text %mlref {*
wenzelm@20480
   118
  \begin{mldecls}
wenzelm@20480
   119
  @{index_ML_type class} \\
wenzelm@20480
   120
  @{index_ML_type sort} \\
wenzelm@20494
   121
  @{index_ML_type arity} \\
wenzelm@20480
   122
  @{index_ML_type typ} \\
wenzelm@20519
   123
  @{index_ML map_atyps: "(typ -> typ) -> typ -> typ"} \\
wenzelm@20494
   124
  @{index_ML fold_atyps: "(typ -> 'a -> 'a) -> typ -> 'a -> 'a"} \\
wenzelm@20547
   125
  \end{mldecls}
wenzelm@20547
   126
  \begin{mldecls}
wenzelm@20480
   127
  @{index_ML Sign.subsort: "theory -> sort * sort -> bool"} \\
wenzelm@20480
   128
  @{index_ML Sign.of_sort: "theory -> typ * sort -> bool"} \\
wenzelm@20520
   129
  @{index_ML Sign.add_types: "(string * int * mixfix) list -> theory -> theory"} \\
wenzelm@20480
   130
  @{index_ML Sign.add_tyabbrs_i: "
wenzelm@20520
   131
  (string * string list * typ * mixfix) list -> theory -> theory"} \\
wenzelm@20480
   132
  @{index_ML Sign.primitive_class: "string * 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@20480
   139
  \item @{ML_type class} represents type classes; this is an alias for
wenzelm@20480
   140
  @{ML_type string}.
wenzelm@20480
   141
wenzelm@20480
   142
  \item @{ML_type sort} represents sorts; this is an alias for
wenzelm@20480
   143
  @{ML_type "class list"}.
wenzelm@20451
   144
wenzelm@20480
   145
  \item @{ML_type arity} represents type arities; this is an alias for
wenzelm@20494
   146
  triples of the form @{text "(\<kappa>, \<^vec>s, s)"} for @{text "\<kappa> ::
wenzelm@20480
   147
  (\<^vec>s)s"} 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@20537
   152
  \item @{ML map_atyps}~@{text "f \<tau>"} applies the mapping @{text "f"}
wenzelm@20537
   153
  to all atomic types (@{ML TFree}, @{ML TVar}) occurring in @{text
wenzelm@20537
   154
  "\<tau>"}.
wenzelm@20519
   155
wenzelm@20537
   156
  \item @{ML fold_atyps}~@{text "f \<tau>"} iterates the operation @{text
wenzelm@20537
   157
  "f"} over all occurrences of atomic types (@{ML TFree}, @{ML TVar})
wenzelm@20537
   158
  in @{text "\<tau>"}; the type structure is traversed from left to right.
wenzelm@20494
   159
wenzelm@20480
   160
  \item @{ML Sign.subsort}~@{text "thy (s\<^isub>1, s\<^isub>2)"}
wenzelm@20480
   161
  tests the subsort relation @{text "s\<^isub>1 \<subseteq> s\<^isub>2"}.
wenzelm@20480
   162
wenzelm@20537
   163
  \item @{ML Sign.of_sort}~@{text "thy (\<tau>, s)"} tests whether type
wenzelm@20537
   164
  @{text "\<tau>"} is of sort @{text "s"}.
wenzelm@20480
   165
wenzelm@20537
   166
  \item @{ML Sign.add_types}~@{text "[(\<kappa>, k, mx), \<dots>]"} declares a new
wenzelm@20494
   167
  type constructors @{text "\<kappa>"} with @{text "k"} arguments and
wenzelm@20480
   168
  optional mixfix syntax.
wenzelm@20451
   169
wenzelm@20494
   170
  \item @{ML Sign.add_tyabbrs_i}~@{text "[(\<kappa>, \<^vec>\<alpha>, \<tau>, mx), \<dots>]"}
wenzelm@20494
   171
  defines a new type abbreviation @{text "(\<^vec>\<alpha>)\<kappa> = \<tau>"} with
wenzelm@20491
   172
  optional mixfix syntax.
wenzelm@20480
   173
wenzelm@20480
   174
  \item @{ML Sign.primitive_class}~@{text "(c, [c\<^isub>1, \<dots>,
wenzelm@20537
   175
  c\<^isub>n])"} declares a new class @{text "c"}, together with class
wenzelm@20494
   176
  relations @{text "c \<subseteq> c\<^isub>i"}, for @{text "i = 1, \<dots>, n"}.
wenzelm@20480
   177
wenzelm@20480
   178
  \item @{ML Sign.primitive_classrel}~@{text "(c\<^isub>1,
wenzelm@20543
   179
  c\<^isub>2)"} declares the class relation @{text "c\<^isub>1 \<subseteq>
wenzelm@20480
   180
  c\<^isub>2"}.
wenzelm@20480
   181
wenzelm@20494
   182
  \item @{ML Sign.primitive_arity}~@{text "(\<kappa>, \<^vec>s, s)"} declares
wenzelm@20537
   183
  the arity @{text "\<kappa> :: (\<^vec>s)s"}.
wenzelm@20480
   184
wenzelm@20480
   185
  \end{description}
wenzelm@20437
   186
*}
wenzelm@20437
   187
wenzelm@20437
   188
wenzelm@20451
   189
section {* Terms \label{sec:terms} *}
wenzelm@18537
   190
wenzelm@18537
   191
text {*
wenzelm@20491
   192
  The language of terms is that of simply-typed @{text "\<lambda>"}-calculus
wenzelm@20520
   193
  with de-Bruijn indices for bound variables (cf.\ \cite{debruijn72}
wenzelm@29761
   194
  or \cite{paulson-ml2}), with the types being determined by the
wenzelm@29761
   195
  corresponding binders.  In contrast, free variables and constants
wenzelm@29761
   196
  are have an explicit name and type in each occurrence.
wenzelm@20514
   197
wenzelm@20514
   198
  \medskip A \emph{bound variable} is a natural number @{text "b"},
wenzelm@20537
   199
  which accounts for the number of intermediate binders between the
wenzelm@20537
   200
  variable occurrence in the body and its binding position.  For
wenzelm@20543
   201
  example, the de-Bruijn term @{text
wenzelm@20543
   202
  "\<lambda>\<^bsub>nat\<^esub>. \<lambda>\<^bsub>nat\<^esub>. 1 + 0"} would
wenzelm@20543
   203
  correspond to @{text
wenzelm@20543
   204
  "\<lambda>x\<^bsub>nat\<^esub>. \<lambda>y\<^bsub>nat\<^esub>. x + y"} in a named
wenzelm@20543
   205
  representation.  Note that a bound variable may be represented by
wenzelm@20543
   206
  different de-Bruijn indices at different occurrences, depending on
wenzelm@20543
   207
  the nesting of abstractions.
wenzelm@20537
   208
wenzelm@20543
   209
  A \emph{loose variable} is a bound variable that is outside the
wenzelm@20537
   210
  scope of local binders.  The types (and names) for loose variables
wenzelm@20543
   211
  can be managed as a separate context, that is maintained as a stack
wenzelm@20543
   212
  of hypothetical binders.  The core logic operates on closed terms,
wenzelm@20543
   213
  without any loose variables.
wenzelm@20514
   214
wenzelm@20537
   215
  A \emph{fixed variable} is a pair of a basic name and a type, e.g.\
wenzelm@20537
   216
  @{text "(x, \<tau>)"} which is usually printed @{text "x\<^isub>\<tau>"}.  A
wenzelm@20537
   217
  \emph{schematic variable} is a pair of an indexname and a type,
wenzelm@20537
   218
  e.g.\ @{text "((x, 0), \<tau>)"} which is usually printed as @{text
wenzelm@20537
   219
  "?x\<^isub>\<tau>"}.
wenzelm@20491
   220
wenzelm@20537
   221
  \medskip A \emph{constant} is a pair of a basic name and a type,
wenzelm@20537
   222
  e.g.\ @{text "(c, \<tau>)"} which is usually printed as @{text
wenzelm@20537
   223
  "c\<^isub>\<tau>"}.  Constants are declared in the context as polymorphic
wenzelm@20543
   224
  families @{text "c :: \<sigma>"}, meaning that all substitution instances
wenzelm@20543
   225
  @{text "c\<^isub>\<tau>"} for @{text "\<tau> = \<sigma>\<vartheta>"} are valid.
wenzelm@20514
   226
wenzelm@20537
   227
  The vector of \emph{type arguments} of constant @{text "c\<^isub>\<tau>"}
wenzelm@20537
   228
  wrt.\ the declaration @{text "c :: \<sigma>"} is defined as the codomain of
wenzelm@20537
   229
  the matcher @{text "\<vartheta> = {?\<alpha>\<^isub>1 \<mapsto> \<tau>\<^isub>1, \<dots>,
wenzelm@20537
   230
  ?\<alpha>\<^isub>n \<mapsto> \<tau>\<^isub>n}"} presented in canonical order @{text
wenzelm@20537
   231
  "(\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n)"}.  Within a given theory context,
wenzelm@20537
   232
  there is a one-to-one correspondence between any constant @{text
wenzelm@20537
   233
  "c\<^isub>\<tau>"} and the application @{text "c(\<tau>\<^isub>1, \<dots>,
wenzelm@20537
   234
  \<tau>\<^isub>n)"} of its type arguments.  For example, with @{text "plus
wenzelm@20537
   235
  :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"}, the instance @{text "plus\<^bsub>nat \<Rightarrow> nat \<Rightarrow>
wenzelm@20537
   236
  nat\<^esub>"} corresponds to @{text "plus(nat)"}.
wenzelm@20480
   237
wenzelm@20514
   238
  Constant declarations @{text "c :: \<sigma>"} may contain sort constraints
wenzelm@20514
   239
  for type variables in @{text "\<sigma>"}.  These are observed by
wenzelm@20514
   240
  type-inference as expected, but \emph{ignored} by the core logic.
wenzelm@20514
   241
  This means the primitive logic is able to reason with instances of
wenzelm@20537
   242
  polymorphic constants that the user-level type-checker would reject
wenzelm@20537
   243
  due to violation of type class restrictions.
wenzelm@20480
   244
wenzelm@20543
   245
  \medskip An \emph{atomic} term is either a variable or constant.  A
wenzelm@20543
   246
  \emph{term} is defined inductively over atomic terms, with
wenzelm@20543
   247
  abstraction and application as follows: @{text "t = b | x\<^isub>\<tau> |
wenzelm@20543
   248
  ?x\<^isub>\<tau> | c\<^isub>\<tau> | \<lambda>\<^isub>\<tau>. t | t\<^isub>1 t\<^isub>2"}.
wenzelm@20543
   249
  Parsing and printing takes care of converting between an external
wenzelm@20543
   250
  representation with named bound variables.  Subsequently, we shall
wenzelm@20543
   251
  use the latter notation instead of internal de-Bruijn
wenzelm@20543
   252
  representation.
wenzelm@20498
   253
wenzelm@20537
   254
  The inductive relation @{text "t :: \<tau>"} assigns a (unique) type to a
wenzelm@20537
   255
  term according to the structure of atomic terms, abstractions, and
wenzelm@20537
   256
  applicatins:
wenzelm@20498
   257
  \[
wenzelm@20501
   258
  \infer{@{text "a\<^isub>\<tau> :: \<tau>"}}{}
wenzelm@20498
   259
  \qquad
wenzelm@20501
   260
  \infer{@{text "(\<lambda>x\<^sub>\<tau>. t) :: \<tau> \<Rightarrow> \<sigma>"}}{@{text "t :: \<sigma>"}}
wenzelm@20501
   261
  \qquad
wenzelm@20501
   262
  \infer{@{text "t u :: \<sigma>"}}{@{text "t :: \<tau> \<Rightarrow> \<sigma>"} & @{text "u :: \<tau>"}}
wenzelm@20498
   263
  \]
wenzelm@20514
   264
  A \emph{well-typed term} is a term that can be typed according to these rules.
wenzelm@20498
   265
wenzelm@20514
   266
  Typing information can be omitted: type-inference is able to
wenzelm@20514
   267
  reconstruct the most general type of a raw term, while assigning
wenzelm@20514
   268
  most general types to all of its variables and constants.
wenzelm@20514
   269
  Type-inference depends on a context of type constraints for fixed
wenzelm@20514
   270
  variables, and declarations for polymorphic constants.
wenzelm@20514
   271
wenzelm@20537
   272
  The identity of atomic terms consists both of the name and the type
wenzelm@20537
   273
  component.  This means that different variables @{text
wenzelm@20537
   274
  "x\<^bsub>\<tau>\<^isub>1\<^esub>"} and @{text
wenzelm@20537
   275
  "x\<^bsub>\<tau>\<^isub>2\<^esub>"} may become the same after type
wenzelm@20537
   276
  instantiation.  Some outer layers of the system make it hard to
wenzelm@20537
   277
  produce variables of the same name, but different types.  In
wenzelm@20543
   278
  contrast, mixed instances of polymorphic constants occur frequently.
wenzelm@20514
   279
wenzelm@20514
   280
  \medskip The \emph{hidden polymorphism} of a term @{text "t :: \<sigma>"}
wenzelm@20514
   281
  is the set of type variables occurring in @{text "t"}, but not in
wenzelm@20537
   282
  @{text "\<sigma>"}.  This means that the term implicitly depends on type
wenzelm@20543
   283
  arguments that are not accounted in the result type, i.e.\ there are
wenzelm@20537
   284
  different type instances @{text "t\<vartheta> :: \<sigma>"} and @{text
wenzelm@20537
   285
  "t\<vartheta>' :: \<sigma>"} with the same type.  This slightly
wenzelm@20543
   286
  pathological situation notoriously demands additional care.
wenzelm@20514
   287
wenzelm@20514
   288
  \medskip A \emph{term abbreviation} is a syntactic definition @{text
wenzelm@20537
   289
  "c\<^isub>\<sigma> \<equiv> t"} of a closed term @{text "t"} of type @{text "\<sigma>"},
wenzelm@20537
   290
  without any hidden polymorphism.  A term abbreviation looks like a
wenzelm@20543
   291
  constant in the syntax, but is expanded before entering the logical
wenzelm@20543
   292
  core.  Abbreviations are usually reverted when printing terms, using
wenzelm@20543
   293
  @{text "t \<rightarrow> c\<^isub>\<sigma>"} as rules for higher-order rewriting.
wenzelm@20519
   294
wenzelm@20519
   295
  \medskip Canonical operations on @{text "\<lambda>"}-terms include @{text
wenzelm@20537
   296
  "\<alpha>\<beta>\<eta>"}-conversion: @{text "\<alpha>"}-conversion refers to capture-free
wenzelm@20519
   297
  renaming of bound variables; @{text "\<beta>"}-conversion contracts an
wenzelm@20537
   298
  abstraction applied to an argument term, substituting the argument
wenzelm@20519
   299
  in the body: @{text "(\<lambda>x. b)a"} becomes @{text "b[a/x]"}; @{text
wenzelm@20519
   300
  "\<eta>"}-conversion contracts vacuous application-abstraction: @{text
wenzelm@20519
   301
  "\<lambda>x. f x"} becomes @{text "f"}, provided that the bound variable
wenzelm@20537
   302
  does not occur in @{text "f"}.
wenzelm@20519
   303
wenzelm@20537
   304
  Terms are normally treated modulo @{text "\<alpha>"}-conversion, which is
wenzelm@20537
   305
  implicit in the de-Bruijn representation.  Names for bound variables
wenzelm@20537
   306
  in abstractions are maintained separately as (meaningless) comments,
wenzelm@20537
   307
  mostly for parsing and printing.  Full @{text "\<alpha>\<beta>\<eta>"}-conversion is
wenzelm@28784
   308
  commonplace in various standard operations (\secref{sec:obj-rules})
wenzelm@28784
   309
  that are based on higher-order unification and matching.
wenzelm@18537
   310
*}
wenzelm@18537
   311
wenzelm@20514
   312
text %mlref {*
wenzelm@20514
   313
  \begin{mldecls}
wenzelm@20514
   314
  @{index_ML_type term} \\
wenzelm@20519
   315
  @{index_ML "op aconv": "term * term -> bool"} \\
wenzelm@20547
   316
  @{index_ML map_types: "(typ -> typ) -> term -> term"} \\
wenzelm@20519
   317
  @{index_ML fold_types: "(typ -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@20514
   318
  @{index_ML map_aterms: "(term -> term) -> term -> term"} \\
wenzelm@20514
   319
  @{index_ML fold_aterms: "(term -> 'a -> 'a) -> term -> 'a -> 'a"} \\
wenzelm@20547
   320
  \end{mldecls}
wenzelm@20547
   321
  \begin{mldecls}
wenzelm@20514
   322
  @{index_ML fastype_of: "term -> typ"} \\
wenzelm@20519
   323
  @{index_ML lambda: "term -> term -> term"} \\
wenzelm@20519
   324
  @{index_ML betapply: "term * term -> term"} \\
haftmann@29581
   325
  @{index_ML Sign.declare_const: "Properties.T -> (binding * typ) * mixfix ->
wenzelm@24972
   326
  theory -> term * theory"} \\
haftmann@29581
   327
  @{index_ML Sign.add_abbrev: "string -> Properties.T -> binding * term ->
wenzelm@24972
   328
  theory -> (term * term) * theory"} \\
wenzelm@20519
   329
  @{index_ML Sign.const_typargs: "theory -> string * typ -> typ list"} \\
wenzelm@20519
   330
  @{index_ML Sign.const_instance: "theory -> string * typ list -> typ"} \\
wenzelm@20514
   331
  \end{mldecls}
wenzelm@18537
   332
wenzelm@20514
   333
  \begin{description}
wenzelm@18537
   334
wenzelm@20537
   335
  \item @{ML_type term} represents de-Bruijn terms, with comments in
wenzelm@20537
   336
  abstractions, and explicitly named free variables and constants;
wenzelm@20537
   337
  this is a datatype with constructors @{ML Bound}, @{ML Free}, @{ML
wenzelm@20537
   338
  Var}, @{ML Const}, @{ML Abs}, @{ML "op $"}.
wenzelm@20519
   339
wenzelm@20519
   340
  \item @{text "t"}~@{ML aconv}~@{text "u"} checks @{text
wenzelm@20519
   341
  "\<alpha>"}-equivalence of two terms.  This is the basic equality relation
wenzelm@20519
   342
  on type @{ML_type term}; raw datatype equality should only be used
wenzelm@20519
   343
  for operations related to parsing or printing!
wenzelm@20519
   344
wenzelm@20547
   345
  \item @{ML map_types}~@{text "f t"} applies the mapping @{text
wenzelm@20537
   346
  "f"} to all types occurring in @{text "t"}.
wenzelm@20537
   347
wenzelm@20537
   348
  \item @{ML fold_types}~@{text "f t"} iterates the operation @{text
wenzelm@20537
   349
  "f"} over all occurrences of types in @{text "t"}; the term
wenzelm@20537
   350
  structure is traversed from left to right.
wenzelm@20519
   351
wenzelm@20537
   352
  \item @{ML map_aterms}~@{text "f t"} applies the mapping @{text "f"}
wenzelm@20537
   353
  to all atomic terms (@{ML Bound}, @{ML Free}, @{ML Var}, @{ML
wenzelm@20537
   354
  Const}) occurring in @{text "t"}.
wenzelm@20537
   355
wenzelm@20537
   356
  \item @{ML fold_aterms}~@{text "f t"} iterates the operation @{text
wenzelm@20537
   357
  "f"} over all occurrences of atomic terms (@{ML Bound}, @{ML Free},
wenzelm@20537
   358
  @{ML Var}, @{ML Const}) in @{text "t"}; the term structure is
wenzelm@20519
   359
  traversed from left to right.
wenzelm@20519
   360
wenzelm@20537
   361
  \item @{ML fastype_of}~@{text "t"} determines the type of a
wenzelm@20537
   362
  well-typed term.  This operation is relatively slow, despite the
wenzelm@20537
   363
  omission of any sanity checks.
wenzelm@20519
   364
wenzelm@20519
   365
  \item @{ML lambda}~@{text "a b"} produces an abstraction @{text
wenzelm@20537
   366
  "\<lambda>a. b"}, where occurrences of the atomic term @{text "a"} in the
wenzelm@20537
   367
  body @{text "b"} are replaced by bound variables.
wenzelm@20519
   368
wenzelm@20537
   369
  \item @{ML betapply}~@{text "(t, u)"} produces an application @{text
wenzelm@20537
   370
  "t u"}, with topmost @{text "\<beta>"}-conversion if @{text "t"} is an
wenzelm@20537
   371
  abstraction.
wenzelm@20519
   372
wenzelm@28110
   373
  \item @{ML Sign.declare_const}~@{text "properties ((c, \<sigma>), mx)"}
wenzelm@24972
   374
  declares a new constant @{text "c :: \<sigma>"} with optional mixfix
wenzelm@24972
   375
  syntax.
wenzelm@20519
   376
wenzelm@24828
   377
  \item @{ML Sign.add_abbrev}~@{text "print_mode properties (c, t)"}
wenzelm@21827
   378
  introduces a new term abbreviation @{text "c \<equiv> t"}.
wenzelm@20519
   379
wenzelm@20520
   380
  \item @{ML Sign.const_typargs}~@{text "thy (c, \<tau>)"} and @{ML
wenzelm@20520
   381
  Sign.const_instance}~@{text "thy (c, [\<tau>\<^isub>1, \<dots>, \<tau>\<^isub>n])"}
wenzelm@20543
   382
  convert between two representations of polymorphic constants: full
wenzelm@20543
   383
  type instance vs.\ compact type arguments form.
wenzelm@18537
   384
wenzelm@20514
   385
  \end{description}
wenzelm@18537
   386
*}
wenzelm@18537
   387
wenzelm@18537
   388
wenzelm@20451
   389
section {* Theorems \label{sec:thms} *}
wenzelm@18537
   390
wenzelm@18537
   391
text {*
wenzelm@20543
   392
  A \emph{proposition} is a well-typed term of type @{text "prop"}, a
wenzelm@20521
   393
  \emph{theorem} is a proven proposition (depending on a context of
wenzelm@20521
   394
  hypotheses and the background theory).  Primitive inferences include
wenzelm@20521
   395
  plain natural deduction rules for the primary connectives @{text
wenzelm@20537
   396
  "\<And>"} and @{text "\<Longrightarrow>"} of the framework.  There is also a builtin
wenzelm@20537
   397
  notion of equality/equivalence @{text "\<equiv>"}.
wenzelm@20521
   398
*}
wenzelm@20521
   399
wenzelm@29758
   400
wenzelm@26872
   401
subsection {* Primitive connectives and rules \label{sec:prim-rules} *}
wenzelm@18537
   402
wenzelm@20521
   403
text {*
wenzelm@20543
   404
  The theory @{text "Pure"} contains constant declarations for the
wenzelm@20543
   405
  primitive connectives @{text "\<And>"}, @{text "\<Longrightarrow>"}, and @{text "\<equiv>"} of
wenzelm@20543
   406
  the logical framework, see \figref{fig:pure-connectives}.  The
wenzelm@20543
   407
  derivability judgment @{text "A\<^isub>1, \<dots>, A\<^isub>n \<turnstile> B"} is
wenzelm@20543
   408
  defined inductively by the primitive inferences given in
wenzelm@20543
   409
  \figref{fig:prim-rules}, with the global restriction that the
wenzelm@20543
   410
  hypotheses must \emph{not} contain any schematic variables.  The
wenzelm@20543
   411
  builtin equality is conceptually axiomatized as shown in
wenzelm@20521
   412
  \figref{fig:pure-equality}, although the implementation works
wenzelm@20543
   413
  directly with derived inferences.
wenzelm@20521
   414
wenzelm@20521
   415
  \begin{figure}[htb]
wenzelm@20521
   416
  \begin{center}
wenzelm@20501
   417
  \begin{tabular}{ll}
wenzelm@20501
   418
  @{text "all :: (\<alpha> \<Rightarrow> prop) \<Rightarrow> prop"} & universal quantification (binder @{text "\<And>"}) \\
wenzelm@20501
   419
  @{text "\<Longrightarrow> :: prop \<Rightarrow> prop \<Rightarrow> prop"} & implication (right associative infix) \\
wenzelm@20521
   420
  @{text "\<equiv> :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> prop"} & equality relation (infix) \\
wenzelm@20501
   421
  \end{tabular}
wenzelm@20537
   422
  \caption{Primitive connectives of Pure}\label{fig:pure-connectives}
wenzelm@20521
   423
  \end{center}
wenzelm@20521
   424
  \end{figure}
wenzelm@18537
   425
wenzelm@20501
   426
  \begin{figure}[htb]
wenzelm@20501
   427
  \begin{center}
wenzelm@20498
   428
  \[
wenzelm@20498
   429
  \infer[@{text "(axiom)"}]{@{text "\<turnstile> A"}}{@{text "A \<in> \<Theta>"}}
wenzelm@20498
   430
  \qquad
wenzelm@20498
   431
  \infer[@{text "(assume)"}]{@{text "A \<turnstile> A"}}{}
wenzelm@20498
   432
  \]
wenzelm@20498
   433
  \[
wenzelm@20537
   434
  \infer[@{text "(\<And>_intro)"}]{@{text "\<Gamma> \<turnstile> \<And>x. b[x]"}}{@{text "\<Gamma> \<turnstile> b[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   435
  \qquad
wenzelm@20537
   436
  \infer[@{text "(\<And>_elim)"}]{@{text "\<Gamma> \<turnstile> b[a]"}}{@{text "\<Gamma> \<turnstile> \<And>x. b[x]"}}
wenzelm@20498
   437
  \]
wenzelm@20498
   438
  \[
wenzelm@20498
   439
  \infer[@{text "(\<Longrightarrow>_intro)"}]{@{text "\<Gamma> - A \<turnstile> A \<Longrightarrow> B"}}{@{text "\<Gamma> \<turnstile> B"}}
wenzelm@20498
   440
  \qquad
wenzelm@20498
   441
  \infer[@{text "(\<Longrightarrow>_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
   442
  \]
wenzelm@20521
   443
  \caption{Primitive inferences of Pure}\label{fig:prim-rules}
wenzelm@20521
   444
  \end{center}
wenzelm@20521
   445
  \end{figure}
wenzelm@20521
   446
wenzelm@20521
   447
  \begin{figure}[htb]
wenzelm@20521
   448
  \begin{center}
wenzelm@20521
   449
  \begin{tabular}{ll}
wenzelm@20537
   450
  @{text "\<turnstile> (\<lambda>x. b[x]) a \<equiv> b[a]"} & @{text "\<beta>"}-conversion \\
wenzelm@20521
   451
  @{text "\<turnstile> x \<equiv> x"} & reflexivity \\
wenzelm@20521
   452
  @{text "\<turnstile> x \<equiv> y \<Longrightarrow> P x \<Longrightarrow> P y"} & substitution \\
wenzelm@20521
   453
  @{text "\<turnstile> (\<And>x. f x \<equiv> g x) \<Longrightarrow> f \<equiv> g"} & extensionality \\
wenzelm@20537
   454
  @{text "\<turnstile> (A \<Longrightarrow> B) \<Longrightarrow> (B \<Longrightarrow> A) \<Longrightarrow> A \<equiv> B"} & logical equivalence \\
wenzelm@20521
   455
  \end{tabular}
wenzelm@20542
   456
  \caption{Conceptual axiomatization of Pure equality}\label{fig:pure-equality}
wenzelm@20501
   457
  \end{center}
wenzelm@20501
   458
  \end{figure}
wenzelm@18537
   459
wenzelm@20501
   460
  The introduction and elimination rules for @{text "\<And>"} and @{text
wenzelm@20537
   461
  "\<Longrightarrow>"} are analogous to formation of dependently typed @{text
wenzelm@20501
   462
  "\<lambda>"}-terms representing the underlying proof objects.  Proof terms
wenzelm@20543
   463
  are irrelevant in the Pure logic, though; they cannot occur within
wenzelm@20543
   464
  propositions.  The system provides a runtime option to record
wenzelm@20537
   465
  explicit proof terms for primitive inferences.  Thus all three
wenzelm@20537
   466
  levels of @{text "\<lambda>"}-calculus become explicit: @{text "\<Rightarrow>"} for
wenzelm@20537
   467
  terms, and @{text "\<And>/\<Longrightarrow>"} for proofs (cf.\
wenzelm@20537
   468
  \cite{Berghofer-Nipkow:2000:TPHOL}).
wenzelm@20491
   469
wenzelm@20537
   470
  Observe that locally fixed parameters (as in @{text "\<And>_intro"}) need
wenzelm@20537
   471
  not be recorded in the hypotheses, because the simple syntactic
wenzelm@20543
   472
  types of Pure are always inhabitable.  ``Assumptions'' @{text "x ::
wenzelm@20543
   473
  \<tau>"} for type-membership are only present as long as some @{text
wenzelm@20543
   474
  "x\<^isub>\<tau>"} occurs in the statement body.\footnote{This is the key
wenzelm@20543
   475
  difference to ``@{text "\<lambda>HOL"}'' in the PTS framework
wenzelm@20543
   476
  \cite{Barendregt-Geuvers:2001}, where hypotheses @{text "x : A"} are
wenzelm@20543
   477
  treated uniformly for propositions and types.}
wenzelm@20501
   478
wenzelm@20501
   479
  \medskip The axiomatization of a theory is implicitly closed by
wenzelm@20537
   480
  forming all instances of type and term variables: @{text "\<turnstile>
wenzelm@20537
   481
  A\<vartheta>"} holds for any substitution instance of an axiom
wenzelm@20543
   482
  @{text "\<turnstile> A"}.  By pushing substitutions through derivations
wenzelm@20543
   483
  inductively, we also get admissible @{text "generalize"} and @{text
wenzelm@20543
   484
  "instance"} rules as shown in \figref{fig:subst-rules}.
wenzelm@20501
   485
wenzelm@20501
   486
  \begin{figure}[htb]
wenzelm@20501
   487
  \begin{center}
wenzelm@20498
   488
  \[
wenzelm@20501
   489
  \infer{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}{@{text "\<Gamma> \<turnstile> B[\<alpha>]"} & @{text "\<alpha> \<notin> \<Gamma>"}}
wenzelm@20501
   490
  \quad
wenzelm@20501
   491
  \infer[\quad@{text "(generalize)"}]{@{text "\<Gamma> \<turnstile> B[?x]"}}{@{text "\<Gamma> \<turnstile> B[x]"} & @{text "x \<notin> \<Gamma>"}}
wenzelm@20498
   492
  \]
wenzelm@20498
   493
  \[
wenzelm@20501
   494
  \infer{@{text "\<Gamma> \<turnstile> B[\<tau>]"}}{@{text "\<Gamma> \<turnstile> B[?\<alpha>]"}}
wenzelm@20501
   495
  \quad
wenzelm@20501
   496
  \infer[\quad@{text "(instantiate)"}]{@{text "\<Gamma> \<turnstile> B[t]"}}{@{text "\<Gamma> \<turnstile> B[?x]"}}
wenzelm@20498
   497
  \]
wenzelm@20501
   498
  \caption{Admissible substitution rules}\label{fig:subst-rules}
wenzelm@20501
   499
  \end{center}
wenzelm@20501
   500
  \end{figure}
wenzelm@18537
   501
wenzelm@20537
   502
  Note that @{text "instantiate"} does not require an explicit
wenzelm@20537
   503
  side-condition, because @{text "\<Gamma>"} may never contain schematic
wenzelm@20537
   504
  variables.
wenzelm@20537
   505
wenzelm@20537
   506
  In principle, variables could be substituted in hypotheses as well,
wenzelm@20543
   507
  but this would disrupt the monotonicity of reasoning: deriving
wenzelm@20543
   508
  @{text "\<Gamma>\<vartheta> \<turnstile> B\<vartheta>"} from @{text "\<Gamma> \<turnstile> B"} is
wenzelm@20543
   509
  correct, but @{text "\<Gamma>\<vartheta> \<supseteq> \<Gamma>"} does not necessarily hold:
wenzelm@20543
   510
  the result belongs to a different proof context.
wenzelm@20542
   511
wenzelm@20543
   512
  \medskip An \emph{oracle} is a function that produces axioms on the
wenzelm@20543
   513
  fly.  Logically, this is an instance of the @{text "axiom"} rule
wenzelm@20543
   514
  (\figref{fig:prim-rules}), but there is an operational difference.
wenzelm@20543
   515
  The system always records oracle invocations within derivations of
wenzelm@20543
   516
  theorems.  Tracing plain axioms (and named theorems) is optional.
wenzelm@20542
   517
wenzelm@20542
   518
  Axiomatizations should be limited to the bare minimum, typically as
wenzelm@20542
   519
  part of the initial logical basis of an object-logic formalization.
wenzelm@20543
   520
  Later on, theories are usually developed in a strictly definitional
wenzelm@20543
   521
  fashion, by stating only certain equalities over new constants.
wenzelm@20542
   522
wenzelm@20542
   523
  A \emph{simple definition} consists of a constant declaration @{text
wenzelm@20543
   524
  "c :: \<sigma>"} together with an axiom @{text "\<turnstile> c \<equiv> t"}, where @{text "t
wenzelm@20543
   525
  :: \<sigma>"} is a closed term without any hidden polymorphism.  The RHS
wenzelm@20543
   526
  may depend on further defined constants, but not @{text "c"} itself.
wenzelm@20543
   527
  Definitions of functions may be presented as @{text "c \<^vec>x \<equiv>
wenzelm@20543
   528
  t"} instead of the puristic @{text "c \<equiv> \<lambda>\<^vec>x. t"}.
wenzelm@20542
   529
wenzelm@20543
   530
  An \emph{overloaded definition} consists of a collection of axioms
wenzelm@20543
   531
  for the same constant, with zero or one equations @{text
wenzelm@20543
   532
  "c((\<^vec>\<alpha>)\<kappa>) \<equiv> t"} for each type constructor @{text "\<kappa>"} (for
wenzelm@20543
   533
  distinct variables @{text "\<^vec>\<alpha>"}).  The RHS may mention
wenzelm@20543
   534
  previously defined constants as above, or arbitrary constants @{text
wenzelm@20543
   535
  "d(\<alpha>\<^isub>i)"} for some @{text "\<alpha>\<^isub>i"} projected from @{text
wenzelm@20543
   536
  "\<^vec>\<alpha>"}.  Thus overloaded definitions essentially work by
wenzelm@20543
   537
  primitive recursion over the syntactic structure of a single type
wenzelm@20543
   538
  argument.
wenzelm@20521
   539
*}
wenzelm@20498
   540
wenzelm@20521
   541
text %mlref {*
wenzelm@20521
   542
  \begin{mldecls}
wenzelm@20521
   543
  @{index_ML_type ctyp} \\
wenzelm@20521
   544
  @{index_ML_type cterm} \\
wenzelm@20547
   545
  @{index_ML Thm.ctyp_of: "theory -> typ -> ctyp"} \\
wenzelm@20547
   546
  @{index_ML Thm.cterm_of: "theory -> term -> cterm"} \\
wenzelm@20547
   547
  \end{mldecls}
wenzelm@20547
   548
  \begin{mldecls}
wenzelm@20521
   549
  @{index_ML_type thm} \\
wenzelm@20542
   550
  @{index_ML proofs: "int ref"} \\
wenzelm@20542
   551
  @{index_ML Thm.assume: "cterm -> thm"} \\
wenzelm@20542
   552
  @{index_ML Thm.forall_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   553
  @{index_ML Thm.forall_elim: "cterm -> thm -> thm"} \\
wenzelm@20542
   554
  @{index_ML Thm.implies_intr: "cterm -> thm -> thm"} \\
wenzelm@20542
   555
  @{index_ML Thm.implies_elim: "thm -> thm -> thm"} \\
wenzelm@20542
   556
  @{index_ML Thm.generalize: "string list * string list -> int -> thm -> thm"} \\
wenzelm@20542
   557
  @{index_ML Thm.instantiate: "(ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm"} \\
wenzelm@28674
   558
  @{index_ML Thm.axiom: "theory -> string -> thm"} \\
wenzelm@28290
   559
  @{index_ML Thm.add_oracle: "bstring * ('a -> cterm) -> theory
wenzelm@28290
   560
  -> (string * ('a -> thm)) * theory"} \\
wenzelm@20547
   561
  \end{mldecls}
wenzelm@20547
   562
  \begin{mldecls}
haftmann@29615
   563
  @{index_ML Theory.add_axioms_i: "(binding * term) list -> theory -> theory"} \\
wenzelm@20542
   564
  @{index_ML Theory.add_deps: "string -> string * typ -> (string * typ) list -> theory -> theory"} \\
haftmann@29615
   565
  @{index_ML Theory.add_defs_i: "bool -> bool -> (binding * term) list -> theory -> theory"} \\
wenzelm@20521
   566
  \end{mldecls}
wenzelm@20521
   567
wenzelm@20521
   568
  \begin{description}
wenzelm@20521
   569
wenzelm@20542
   570
  \item @{ML_type ctyp} and @{ML_type cterm} represent certified types
wenzelm@20542
   571
  and terms, respectively.  These are abstract datatypes that
wenzelm@20542
   572
  guarantee that its values have passed the full well-formedness (and
wenzelm@20542
   573
  well-typedness) checks, relative to the declarations of type
wenzelm@20542
   574
  constructors, constants etc. in the theory.
wenzelm@20542
   575
wenzelm@20547
   576
  \item @{ML ctyp_of}~@{text "thy \<tau>"} and @{ML cterm_of}~@{text "thy
wenzelm@20547
   577
  t"} explicitly checks types and terms, respectively.  This also
wenzelm@20547
   578
  involves some basic normalizations, such expansion of type and term
wenzelm@20547
   579
  abbreviations from the theory context.
wenzelm@20547
   580
wenzelm@20547
   581
  Re-certification is relatively slow and should be avoided in tight
wenzelm@20547
   582
  reasoning loops.  There are separate operations to decompose
wenzelm@20547
   583
  certified entities (including actual theorems).
wenzelm@20542
   584
wenzelm@20542
   585
  \item @{ML_type thm} represents proven propositions.  This is an
wenzelm@20542
   586
  abstract datatype that guarantees that its values have been
wenzelm@20542
   587
  constructed by basic principles of the @{ML_struct Thm} module.
wenzelm@20543
   588
  Every @{ML thm} value contains a sliding back-reference to the
wenzelm@20543
   589
  enclosing theory, cf.\ \secref{sec:context-theory}.
wenzelm@20542
   590
wenzelm@20543
   591
  \item @{ML proofs} determines the detail of proof recording within
wenzelm@20543
   592
  @{ML_type thm} values: @{ML 0} records only oracles, @{ML 1} records
wenzelm@20543
   593
  oracles, axioms and named theorems, @{ML 2} records full proof
wenzelm@20543
   594
  terms.
wenzelm@20542
   595
wenzelm@20542
   596
  \item @{ML Thm.assume}, @{ML Thm.forall_intr}, @{ML
wenzelm@20542
   597
  Thm.forall_elim}, @{ML Thm.implies_intr}, and @{ML Thm.implies_elim}
wenzelm@20542
   598
  correspond to the primitive inferences of \figref{fig:prim-rules}.
wenzelm@20542
   599
wenzelm@20542
   600
  \item @{ML Thm.generalize}~@{text "(\<^vec>\<alpha>, \<^vec>x)"}
wenzelm@20542
   601
  corresponds to the @{text "generalize"} rules of
wenzelm@20543
   602
  \figref{fig:subst-rules}.  Here collections of type and term
wenzelm@20543
   603
  variables are generalized simultaneously, specified by the given
wenzelm@20543
   604
  basic names.
wenzelm@20521
   605
wenzelm@20542
   606
  \item @{ML Thm.instantiate}~@{text "(\<^vec>\<alpha>\<^isub>s,
wenzelm@20542
   607
  \<^vec>x\<^isub>\<tau>)"} corresponds to the @{text "instantiate"} rules
wenzelm@20542
   608
  of \figref{fig:subst-rules}.  Type variables are substituted before
wenzelm@20542
   609
  term variables.  Note that the types in @{text "\<^vec>x\<^isub>\<tau>"}
wenzelm@20542
   610
  refer to the instantiated versions.
wenzelm@20542
   611
wenzelm@28674
   612
  \item @{ML Thm.axiom}~@{text "thy name"} retrieves a named
wenzelm@20542
   613
  axiom, cf.\ @{text "axiom"} in \figref{fig:prim-rules}.
wenzelm@20542
   614
wenzelm@28290
   615
  \item @{ML Thm.add_oracle}~@{text "(name, oracle)"} produces a named
wenzelm@28290
   616
  oracle rule, essentially generating arbitrary axioms on the fly,
wenzelm@28290
   617
  cf.\ @{text "axiom"} in \figref{fig:prim-rules}.
wenzelm@20521
   618
wenzelm@20543
   619
  \item @{ML Theory.add_axioms_i}~@{text "[(name, A), \<dots>]"} declares
wenzelm@20543
   620
  arbitrary propositions as axioms.
wenzelm@20542
   621
wenzelm@20542
   622
  \item @{ML Theory.add_deps}~@{text "name c\<^isub>\<tau>
wenzelm@20543
   623
  \<^vec>d\<^isub>\<sigma>"} declares dependencies of a named specification
wenzelm@20543
   624
  for constant @{text "c\<^isub>\<tau>"}, relative to existing
wenzelm@20543
   625
  specifications for constants @{text "\<^vec>d\<^isub>\<sigma>"}.
wenzelm@20542
   626
wenzelm@20542
   627
  \item @{ML Theory.add_defs_i}~@{text "unchecked overloaded [(name, c
wenzelm@20543
   628
  \<^vec>x \<equiv> t), \<dots>]"} states a definitional axiom for an existing
wenzelm@20543
   629
  constant @{text "c"}.  Dependencies are recorded (cf.\ @{ML
wenzelm@20543
   630
  Theory.add_deps}), unless the @{text "unchecked"} option is set.
wenzelm@20521
   631
wenzelm@20521
   632
  \end{description}
wenzelm@20521
   633
*}
wenzelm@20521
   634
wenzelm@20521
   635
wenzelm@20543
   636
subsection {* Auxiliary definitions *}
wenzelm@20521
   637
wenzelm@20521
   638
text {*
wenzelm@20543
   639
  Theory @{text "Pure"} provides a few auxiliary definitions, see
wenzelm@20543
   640
  \figref{fig:pure-aux}.  These special constants are normally not
wenzelm@20543
   641
  exposed to the user, but appear in internal encodings.
wenzelm@20501
   642
wenzelm@20501
   643
  \begin{figure}[htb]
wenzelm@20501
   644
  \begin{center}
wenzelm@20498
   645
  \begin{tabular}{ll}
wenzelm@20521
   646
  @{text "conjunction :: prop \<Rightarrow> prop \<Rightarrow> prop"} & (infix @{text "&"}) \\
wenzelm@20521
   647
  @{text "\<turnstile> A & B \<equiv> (\<And>C. (A \<Longrightarrow> B \<Longrightarrow> C) \<Longrightarrow> C)"} \\[1ex]
wenzelm@20543
   648
  @{text "prop :: prop \<Rightarrow> prop"} & (prefix @{text "#"}, suppressed) \\
wenzelm@20521
   649
  @{text "#A \<equiv> A"} \\[1ex]
wenzelm@20521
   650
  @{text "term :: \<alpha> \<Rightarrow> prop"} & (prefix @{text "TERM"}) \\
wenzelm@20521
   651
  @{text "term x \<equiv> (\<And>A. A \<Longrightarrow> A)"} \\[1ex]
wenzelm@20521
   652
  @{text "TYPE :: \<alpha> itself"} & (prefix @{text "TYPE"}) \\
wenzelm@20521
   653
  @{text "(unspecified)"} \\
wenzelm@20498
   654
  \end{tabular}
wenzelm@20521
   655
  \caption{Definitions of auxiliary connectives}\label{fig:pure-aux}
wenzelm@20501
   656
  \end{center}
wenzelm@20501
   657
  \end{figure}
wenzelm@20501
   658
wenzelm@20537
   659
  Derived conjunction rules include introduction @{text "A \<Longrightarrow> B \<Longrightarrow> A &
wenzelm@20537
   660
  B"}, and destructions @{text "A & B \<Longrightarrow> A"} and @{text "A & B \<Longrightarrow> B"}.
wenzelm@20537
   661
  Conjunction allows to treat simultaneous assumptions and conclusions
wenzelm@20537
   662
  uniformly.  For example, multiple claims are intermediately
wenzelm@20543
   663
  represented as explicit conjunction, but this is refined into
wenzelm@20543
   664
  separate sub-goals before the user continues the proof; the final
wenzelm@20543
   665
  result is projected into a list of theorems (cf.\
wenzelm@20537
   666
  \secref{sec:tactical-goals}).
wenzelm@20498
   667
wenzelm@20537
   668
  The @{text "prop"} marker (@{text "#"}) makes arbitrarily complex
wenzelm@20537
   669
  propositions appear as atomic, without changing the meaning: @{text
wenzelm@20537
   670
  "\<Gamma> \<turnstile> A"} and @{text "\<Gamma> \<turnstile> #A"} are interchangeable.  See
wenzelm@20537
   671
  \secref{sec:tactical-goals} for specific operations.
wenzelm@20521
   672
wenzelm@20543
   673
  The @{text "term"} marker turns any well-typed term into a derivable
wenzelm@20543
   674
  proposition: @{text "\<turnstile> TERM t"} holds unconditionally.  Although
wenzelm@20543
   675
  this is logically vacuous, it allows to treat terms and proofs
wenzelm@20543
   676
  uniformly, similar to a type-theoretic framework.
wenzelm@20498
   677
wenzelm@20537
   678
  The @{text "TYPE"} constructor is the canonical representative of
wenzelm@20537
   679
  the unspecified type @{text "\<alpha> itself"}; it essentially injects the
wenzelm@20537
   680
  language of types into that of terms.  There is specific notation
wenzelm@20537
   681
  @{text "TYPE(\<tau>)"} for @{text "TYPE\<^bsub>\<tau>
wenzelm@20521
   682
 itself\<^esub>"}.
wenzelm@20537
   683
  Although being devoid of any particular meaning, the @{text
wenzelm@20537
   684
  "TYPE(\<tau>)"} accounts for the type @{text "\<tau>"} within the term
wenzelm@20537
   685
  language.  In particular, @{text "TYPE(\<alpha>)"} may be used as formal
wenzelm@20537
   686
  argument in primitive definitions, in order to circumvent hidden
wenzelm@20537
   687
  polymorphism (cf.\ \secref{sec:terms}).  For example, @{text "c
wenzelm@20537
   688
  TYPE(\<alpha>) \<equiv> A[\<alpha>]"} defines @{text "c :: \<alpha> itself \<Rightarrow> prop"} in terms of
wenzelm@20537
   689
  a proposition @{text "A"} that depends on an additional type
wenzelm@20537
   690
  argument, which is essentially a predicate on types.
wenzelm@20521
   691
*}
wenzelm@20501
   692
wenzelm@20521
   693
text %mlref {*
wenzelm@20521
   694
  \begin{mldecls}
wenzelm@20521
   695
  @{index_ML Conjunction.intr: "thm -> thm -> thm"} \\
wenzelm@20521
   696
  @{index_ML Conjunction.elim: "thm -> thm * thm"} \\
wenzelm@20521
   697
  @{index_ML Drule.mk_term: "cterm -> thm"} \\
wenzelm@20521
   698
  @{index_ML Drule.dest_term: "thm -> cterm"} \\
wenzelm@20521
   699
  @{index_ML Logic.mk_type: "typ -> term"} \\
wenzelm@20521
   700
  @{index_ML Logic.dest_type: "term -> typ"} \\
wenzelm@20521
   701
  \end{mldecls}
wenzelm@20521
   702
wenzelm@20521
   703
  \begin{description}
wenzelm@20521
   704
wenzelm@20542
   705
  \item @{ML Conjunction.intr} derives @{text "A & B"} from @{text
wenzelm@20542
   706
  "A"} and @{text "B"}.
wenzelm@20542
   707
wenzelm@20543
   708
  \item @{ML Conjunction.elim} derives @{text "A"} and @{text "B"}
wenzelm@20542
   709
  from @{text "A & B"}.
wenzelm@20542
   710
wenzelm@20543
   711
  \item @{ML Drule.mk_term} derives @{text "TERM t"}.
wenzelm@20542
   712
wenzelm@20543
   713
  \item @{ML Drule.dest_term} recovers term @{text "t"} from @{text
wenzelm@20543
   714
  "TERM t"}.
wenzelm@20542
   715
wenzelm@20542
   716
  \item @{ML Logic.mk_type}~@{text "\<tau>"} produces the term @{text
wenzelm@20542
   717
  "TYPE(\<tau>)"}.
wenzelm@20542
   718
wenzelm@20542
   719
  \item @{ML Logic.dest_type}~@{text "TYPE(\<tau>)"} recovers the type
wenzelm@20542
   720
  @{text "\<tau>"}.
wenzelm@20521
   721
wenzelm@20521
   722
  \end{description}
wenzelm@20491
   723
*}
wenzelm@18537
   724
wenzelm@20480
   725
wenzelm@28784
   726
section {* Object-level rules \label{sec:obj-rules} *}
wenzelm@18537
   727
wenzelm@20929
   728
text %FIXME {*
wenzelm@18537
   729
wenzelm@18537
   730
FIXME
wenzelm@18537
   731
wenzelm@20491
   732
  A \emph{rule} is any Pure theorem in HHF normal form; there is a
wenzelm@20491
   733
  separate calculus for rule composition, which is modeled after
wenzelm@20491
   734
  Gentzen's Natural Deduction \cite{Gentzen:1935}, but allows
wenzelm@20491
   735
  rules to be nested arbitrarily, similar to \cite{extensions91}.
wenzelm@20491
   736
wenzelm@20491
   737
  Normally, all theorems accessible to the user are proper rules.
wenzelm@20491
   738
  Low-level inferences are occasional required internally, but the
wenzelm@20491
   739
  result should be always presented in canonical form.  The higher
wenzelm@20491
   740
  interfaces of Isabelle/Isar will always produce proper rules.  It is
wenzelm@20491
   741
  important to maintain this invariant in add-on applications!
wenzelm@20491
   742
wenzelm@20491
   743
  There are two main principles of rule composition: @{text
wenzelm@20491
   744
  "resolution"} (i.e.\ backchaining of rules) and @{text
wenzelm@20491
   745
  "by-assumption"} (i.e.\ closing a branch); both principles are
wenzelm@20519
   746
  combined in the variants of @{text "elim-resolution"} and @{text
wenzelm@20491
   747
  "dest-resolution"}.  Raw @{text "composition"} is occasionally
wenzelm@20491
   748
  useful as well, also it is strictly speaking outside of the proper
wenzelm@20491
   749
  rule calculus.
wenzelm@20491
   750
wenzelm@20491
   751
  Rules are treated modulo general higher-order unification, which is
wenzelm@20491
   752
  unification modulo the equational theory of @{text "\<alpha>\<beta>\<eta>"}-conversion
wenzelm@20491
   753
  on @{text "\<lambda>"}-terms.  Moreover, propositions are understood modulo
wenzelm@20491
   754
  the (derived) equivalence @{text "(A \<Longrightarrow> (\<And>x. B x)) \<equiv> (\<And>x. A \<Longrightarrow> B x)"}.
wenzelm@20491
   755
wenzelm@20491
   756
  This means that any operations within the rule calculus may be
wenzelm@20491
   757
  subject to spontaneous @{text "\<alpha>\<beta>\<eta>"}-HHF conversions.  It is common
wenzelm@20491
   758
  practice not to contract or expand unnecessarily.  Some mechanisms
wenzelm@20491
   759
  prefer an one form, others the opposite, so there is a potential
wenzelm@20491
   760
  danger to produce some oscillation!
wenzelm@20491
   761
wenzelm@20491
   762
  Only few operations really work \emph{modulo} HHF conversion, but
wenzelm@20491
   763
  expect a normal form: quantifiers @{text "\<And>"} before implications
wenzelm@20491
   764
  @{text "\<Longrightarrow>"} at each level of nesting.
wenzelm@20491
   765
wenzelm@29758
   766
  The set of propositions in HHF format is defined inductively as
wenzelm@29758
   767
  @{text "H = (\<And>x\<^sup>*. H\<^sup>* \<Longrightarrow> A)"}, for variables @{text "x"}
wenzelm@29758
   768
  and atomic propositions @{text "A"}.  Any proposition may be put
wenzelm@29758
   769
  into HHF form by normalizing with the rule @{text "(A \<Longrightarrow> (\<And>x. B x)) \<equiv>
wenzelm@29758
   770
  (\<And>x. A \<Longrightarrow> B x)"}.  In Isabelle, the outermost quantifier prefix is
wenzelm@29758
   771
  represented via schematic variables, such that the top-level
wenzelm@29758
   772
  structure is merely that of a Horn Clause.
wenzelm@18537
   773
wenzelm@20498
   774
wenzelm@20498
   775
  \[
wenzelm@20498
   776
  \infer[@{text "(assumption)"}]{@{text "C\<vartheta>"}}
wenzelm@20498
   777
  {@{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@20498
   778
  \]
wenzelm@20498
   779
wenzelm@20498
   780
wenzelm@20498
   781
  \[
wenzelm@20498
   782
  \infer[@{text "(compose)"}]{@{text "\<^vec>A\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
   783
  {@{text "\<^vec>A \<Longrightarrow> B"} & @{text "B' \<Longrightarrow> C"} & @{text "B\<vartheta> = B'\<vartheta>"}}
wenzelm@20498
   784
  \]
wenzelm@20498
   785
wenzelm@20498
   786
wenzelm@20498
   787
  \[
wenzelm@20498
   788
  \infer[@{text "(\<And>_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
   789
  \]
wenzelm@20498
   790
  \[
wenzelm@20498
   791
  \infer[@{text "(\<Longrightarrow>_lift)"}]{@{text "(\<^vec>H \<Longrightarrow> \<^vec>A) \<Longrightarrow> (\<^vec>H \<Longrightarrow> B)"}}{@{text "\<^vec>A \<Longrightarrow> B"}}
wenzelm@20498
   792
  \]
wenzelm@20498
   793
wenzelm@20498
   794
  The @{text resolve} scheme is now acquired from @{text "\<And>_lift"},
wenzelm@20498
   795
  @{text "\<Longrightarrow>_lift"}, and @{text compose}.
wenzelm@20498
   796
wenzelm@20498
   797
  \[
wenzelm@20498
   798
  \infer[@{text "(resolution)"}]
wenzelm@20498
   799
  {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>A (?\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
wenzelm@20498
   800
  {\begin{tabular}{l}
wenzelm@20498
   801
    @{text "\<^vec>A ?\<^vec>a \<Longrightarrow> B ?\<^vec>a"} \\
wenzelm@20498
   802
    @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
wenzelm@20498
   803
    @{text "(\<lambda>\<^vec>x. B (?\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
wenzelm@20498
   804
   \end{tabular}}
wenzelm@20498
   805
  \]
wenzelm@20498
   806
wenzelm@20498
   807
wenzelm@20498
   808
  FIXME @{text "elim_resolution"}, @{text "dest_resolution"}
wenzelm@18537
   809
*}
wenzelm@18537
   810
wenzelm@18537
   811
end