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