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