doc-src/IsarRef/Thy/Spec.thy
author wenzelm
Sat, 17 Mar 2012 22:46:19 +0100
changeset 47870 1c3c185bab4e
parent 46471 1bbbac9a0cb0
child 47985 7c9e31ffcd9e
permissions -rw-r--r--
more precise syntax;
wenzelm@26869
     1
theory Spec
wenzelm@43522
     2
imports Base Main
wenzelm@26869
     3
begin
wenzelm@26869
     4
wenzelm@44140
     5
chapter {* Specifications *}
wenzelm@26869
     6
wenzelm@30071
     7
text {*
wenzelm@30071
     8
  The Isabelle/Isar theory format integrates specifications and
wenzelm@30071
     9
  proofs, supporting interactive development with unlimited undo
wenzelm@30071
    10
  operation.  There is an integrated document preparation system (see
wenzelm@30071
    11
  \chref{ch:document-prep}), for typesetting formal developments
wenzelm@30071
    12
  together with informal text.  The resulting hyper-linked PDF
wenzelm@30071
    13
  documents can be used both for WWW presentation and printed copies.
wenzelm@30071
    14
wenzelm@30071
    15
  The Isar proof language (see \chref{ch:proofs}) is embedded into the
wenzelm@30071
    16
  theory language as a proper sub-language.  Proof mode is entered by
wenzelm@30071
    17
  stating some @{command theorem} or @{command lemma} at the theory
wenzelm@30071
    18
  level, and left again with the final conclusion (e.g.\ via @{command
wenzelm@30071
    19
  qed}).  Some theory specification mechanisms also require a proof,
wenzelm@30071
    20
  such as @{command typedef} in HOL, which demands non-emptiness of
wenzelm@30071
    21
  the representing sets.
wenzelm@30071
    22
*}
wenzelm@30071
    23
wenzelm@30071
    24
wenzelm@26870
    25
section {* Defining theories \label{sec:begin-thy} *}
wenzelm@26870
    26
wenzelm@26870
    27
text {*
wenzelm@26870
    28
  \begin{matharray}{rcl}
wenzelm@28761
    29
    @{command_def "theory"} & : & @{text "toplevel \<rightarrow> theory"} \\
wenzelm@28761
    30
    @{command_def (global) "end"} & : & @{text "theory \<rightarrow> toplevel"} \\
wenzelm@26870
    31
  \end{matharray}
wenzelm@26870
    32
wenzelm@28745
    33
  Isabelle/Isar theories are defined via theory files, which may
wenzelm@28745
    34
  contain both specifications and proofs; occasionally definitional
wenzelm@28745
    35
  mechanisms also require some explicit proof.  The theory body may be
wenzelm@28745
    36
  sub-structured by means of \emph{local theory targets}, such as
wenzelm@28745
    37
  @{command "locale"} and @{command "class"}.
wenzelm@26870
    38
wenzelm@28745
    39
  The first proper command of a theory is @{command "theory"}, which
wenzelm@28745
    40
  indicates imports of previous theories and optional dependencies on
wenzelm@28745
    41
  other source files (usually in ML).  Just preceding the initial
wenzelm@28745
    42
  @{command "theory"} command there may be an optional @{command
wenzelm@28745
    43
  "header"} declaration, which is only relevant to document
wenzelm@28745
    44
  preparation: see also the other section markup commands in
wenzelm@28745
    45
  \secref{sec:markup}.
wenzelm@28745
    46
wenzelm@28745
    47
  A theory is concluded by a final @{command (global) "end"} command,
wenzelm@28745
    48
  one that does not belong to a local theory target.  No further
wenzelm@28745
    49
  commands may follow such a global @{command (global) "end"},
wenzelm@28745
    50
  although some user-interfaces might pretend that trailing input is
wenzelm@28745
    51
  admissible.
wenzelm@26870
    52
wenzelm@43467
    53
  @{rail "
wenzelm@43575
    54
    @@{command theory} @{syntax name} \\ @'imports' (@{syntax name} +) uses? @'begin'
wenzelm@26870
    55
    ;
wenzelm@26870
    56
wenzelm@43467
    57
    uses: @'uses' ((@{syntax name} | @{syntax parname}) +)
wenzelm@43467
    58
  "}
wenzelm@26870
    59
wenzelm@28760
    60
  \begin{description}
wenzelm@26870
    61
wenzelm@28760
    62
  \item @{command "theory"}~@{text "A \<IMPORTS> B\<^sub>1 \<dots> B\<^sub>n \<BEGIN>"}
wenzelm@28760
    63
  starts a new theory @{text A} based on the merge of existing
wenzelm@28760
    64
  theories @{text "B\<^sub>1 \<dots> B\<^sub>n"}.
wenzelm@26870
    65
  
wenzelm@28745
    66
  Due to the possibility to import more than one ancestor, the
wenzelm@28745
    67
  resulting theory structure of an Isabelle session forms a directed
wenzelm@28745
    68
  acyclic graph (DAG).  Isabelle's theory loader ensures that the
wenzelm@28745
    69
  sources contributing to the development graph are always up-to-date:
wenzelm@28745
    70
  changed files are automatically reloaded whenever a theory header
wenzelm@28745
    71
  specification is processed.
wenzelm@26870
    72
  
wenzelm@26870
    73
  The optional @{keyword_def "uses"} specification declares additional
wenzelm@26870
    74
  dependencies on extra files (usually ML sources).  Files will be
wenzelm@28745
    75
  loaded immediately (as ML), unless the name is parenthesized.  The
wenzelm@28745
    76
  latter case records a dependency that needs to be resolved later in
wenzelm@28745
    77
  the text, usually via explicit @{command_ref "use"} for ML files;
wenzelm@28745
    78
  other file formats require specific load commands defined by the
wenzelm@28745
    79
  corresponding tools or packages.
wenzelm@26870
    80
  
wenzelm@28760
    81
  \item @{command (global) "end"} concludes the current theory
wenzelm@28745
    82
  definition.  Note that local theory targets involve a local
wenzelm@28745
    83
  @{command (local) "end"}, which is clear from the nesting.
wenzelm@26870
    84
wenzelm@28760
    85
  \end{description}
wenzelm@26870
    86
*}
wenzelm@26870
    87
wenzelm@27040
    88
wenzelm@27040
    89
section {* Local theory targets \label{sec:target} *}
wenzelm@27040
    90
wenzelm@27040
    91
text {*
wenzelm@27040
    92
  A local theory target is a context managed separately within the
wenzelm@27040
    93
  enclosing theory.  Contexts may introduce parameters (fixed
wenzelm@27040
    94
  variables) and assumptions (hypotheses).  Definitions and theorems
wenzelm@27040
    95
  depending on the context may be added incrementally later on.  Named
wenzelm@27040
    96
  contexts refer to locales (cf.\ \secref{sec:locale}) or type classes
wenzelm@27040
    97
  (cf.\ \secref{sec:class}); the name ``@{text "-"}'' signifies the
wenzelm@27040
    98
  global theory context.
wenzelm@27040
    99
wenzelm@27040
   100
  \begin{matharray}{rcll}
wenzelm@28761
   101
    @{command_def "context"} & : & @{text "theory \<rightarrow> local_theory"} \\
wenzelm@28761
   102
    @{command_def (local) "end"} & : & @{text "local_theory \<rightarrow> theory"} \\
wenzelm@27040
   103
  \end{matharray}
wenzelm@27040
   104
wenzelm@43467
   105
  @{rail "
wenzelm@47870
   106
    @@{command context} @{syntax nameref} @'begin'
wenzelm@27040
   107
    ;
wenzelm@27040
   108
wenzelm@47870
   109
    @{syntax_def target}: '(' @'in' @{syntax nameref} ')'
wenzelm@43467
   110
  "}
wenzelm@27040
   111
wenzelm@28760
   112
  \begin{description}
wenzelm@27040
   113
  
wenzelm@28760
   114
  \item @{command "context"}~@{text "c \<BEGIN>"} recommences an
wenzelm@27040
   115
  existing locale or class context @{text c}.  Note that locale and
wenzelm@27051
   116
  class definitions allow to include the @{keyword "begin"} keyword as
wenzelm@27051
   117
  well, in order to continue the local theory immediately after the
wenzelm@27051
   118
  initial specification.
wenzelm@27040
   119
  
wenzelm@28760
   120
  \item @{command (local) "end"} concludes the current local theory
wenzelm@27040
   121
  and continues the enclosing global theory.  Note that a global
wenzelm@27040
   122
  @{command (global) "end"} has a different meaning: it concludes the
wenzelm@27040
   123
  theory itself (\secref{sec:begin-thy}).
wenzelm@27040
   124
  
wenzelm@30071
   125
  \item @{text "("}@{keyword_def "in"}~@{text "c)"} given after any
wenzelm@30071
   126
  local theory command specifies an immediate target, e.g.\
wenzelm@30071
   127
  ``@{command "definition"}~@{text "(\<IN> c) \<dots>"}'' or ``@{command
wenzelm@27040
   128
  "theorem"}~@{text "(\<IN> c) \<dots>"}''.  This works both in a local or
wenzelm@27040
   129
  global theory context; the current target context will be suspended
wenzelm@27040
   130
  for this command only.  Note that ``@{text "(\<IN> -)"}'' will
wenzelm@27040
   131
  always produce a global result independently of the current target
wenzelm@27040
   132
  context.
wenzelm@27040
   133
wenzelm@28760
   134
  \end{description}
wenzelm@27040
   135
wenzelm@27040
   136
  The exact meaning of results produced within a local theory context
wenzelm@27040
   137
  depends on the underlying target infrastructure (locale, type class
wenzelm@27040
   138
  etc.).  The general idea is as follows, considering a context named
wenzelm@27040
   139
  @{text c} with parameter @{text x} and assumption @{text "A[x]"}.
wenzelm@27040
   140
  
wenzelm@27040
   141
  Definitions are exported by introducing a global version with
wenzelm@27040
   142
  additional arguments; a syntactic abbreviation links the long form
wenzelm@27040
   143
  with the abstract version of the target context.  For example,
wenzelm@27040
   144
  @{text "a \<equiv> t[x]"} becomes @{text "c.a ?x \<equiv> t[?x]"} at the theory
wenzelm@27040
   145
  level (for arbitrary @{text "?x"}), together with a local
wenzelm@27040
   146
  abbreviation @{text "c \<equiv> c.a x"} in the target context (for the
wenzelm@27040
   147
  fixed parameter @{text x}).
wenzelm@27040
   148
wenzelm@27040
   149
  Theorems are exported by discharging the assumptions and
wenzelm@27040
   150
  generalizing the parameters of the context.  For example, @{text "a:
wenzelm@27040
   151
  B[x]"} becomes @{text "c.a: A[?x] \<Longrightarrow> B[?x]"}, again for arbitrary
wenzelm@27040
   152
  @{text "?x"}.
wenzelm@27040
   153
*}
wenzelm@27040
   154
wenzelm@27040
   155
wenzelm@27040
   156
section {* Basic specification elements *}
wenzelm@27040
   157
wenzelm@27040
   158
text {*
wenzelm@27040
   159
  \begin{matharray}{rcll}
wenzelm@40496
   160
    @{command_def "axiomatization"} & : & @{text "theory \<rightarrow> theory"} & (axiomatic!) \\
wenzelm@28761
   161
    @{command_def "definition"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   162
    @{attribute_def "defn"} & : & @{text attribute} \\
wenzelm@28761
   163
    @{command_def "abbreviation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   164
    @{command_def "print_abbrevs"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow> "} \\
wenzelm@27040
   165
  \end{matharray}
wenzelm@27040
   166
wenzelm@27040
   167
  These specification mechanisms provide a slightly more abstract view
wenzelm@27040
   168
  than the underlying primitives of @{command "consts"}, @{command
wenzelm@27040
   169
  "defs"} (see \secref{sec:consts}), and @{command "axioms"} (see
wenzelm@27040
   170
  \secref{sec:axms-thms}).  In particular, type-inference is commonly
wenzelm@27040
   171
  available, and result names need not be given.
wenzelm@27040
   172
wenzelm@43467
   173
  @{rail "
wenzelm@43496
   174
    @@{command axiomatization} @{syntax \"fixes\"}? (@'where' specs)?
wenzelm@27040
   175
    ;
wenzelm@43575
   176
    @@{command definition} @{syntax target}? \\
wenzelm@43575
   177
      (decl @'where')? @{syntax thmdecl}? @{syntax prop}
wenzelm@27040
   178
    ;
wenzelm@43575
   179
    @@{command abbreviation} @{syntax target}? @{syntax mode}? \\
wenzelm@43467
   180
      (decl @'where')? @{syntax prop}
wenzelm@27040
   181
    ;
wenzelm@27040
   182
wenzelm@43467
   183
    @{syntax_def \"fixes\"}: ((@{syntax name} ('::' @{syntax type})?
wenzelm@43467
   184
      @{syntax mixfix}? | @{syntax vars}) + @'and')
wenzelm@27040
   185
    ;
wenzelm@43467
   186
    specs: (@{syntax thmdecl}? @{syntax props} + @'and')
wenzelm@27040
   187
    ;
wenzelm@43467
   188
    decl: @{syntax name} ('::' @{syntax type})? @{syntax mixfix}?
wenzelm@43467
   189
  "}
wenzelm@27040
   190
wenzelm@28760
   191
  \begin{description}
wenzelm@27040
   192
  
wenzelm@28760
   193
  \item @{command "axiomatization"}~@{text "c\<^sub>1 \<dots> c\<^sub>m \<WHERE> \<phi>\<^sub>1 \<dots> \<phi>\<^sub>n"}
wenzelm@28760
   194
  introduces several constants simultaneously and states axiomatic
wenzelm@28760
   195
  properties for these.  The constants are marked as being specified
wenzelm@28760
   196
  once and for all, which prevents additional specifications being
wenzelm@28760
   197
  issued later on.
wenzelm@27040
   198
  
wenzelm@27040
   199
  Note that axiomatic specifications are only appropriate when
wenzelm@28114
   200
  declaring a new logical system; axiomatic specifications are
wenzelm@28114
   201
  restricted to global theory contexts.  Normal applications should
wenzelm@28114
   202
  only use definitional mechanisms!
wenzelm@27040
   203
wenzelm@28760
   204
  \item @{command "definition"}~@{text "c \<WHERE> eq"} produces an
wenzelm@27040
   205
  internal definition @{text "c \<equiv> t"} according to the specification
wenzelm@27040
   206
  given as @{text eq}, which is then turned into a proven fact.  The
wenzelm@27040
   207
  given proposition may deviate from internal meta-level equality
wenzelm@27040
   208
  according to the rewrite rules declared as @{attribute defn} by the
wenzelm@27040
   209
  object-logic.  This usually covers object-level equality @{text "x =
wenzelm@27040
   210
  y"} and equivalence @{text "A \<leftrightarrow> B"}.  End-users normally need not
wenzelm@27040
   211
  change the @{attribute defn} setup.
wenzelm@27040
   212
  
wenzelm@27040
   213
  Definitions may be presented with explicit arguments on the LHS, as
wenzelm@27040
   214
  well as additional conditions, e.g.\ @{text "f x y = t"} instead of
wenzelm@27040
   215
  @{text "f \<equiv> \<lambda>x y. t"} and @{text "y \<noteq> 0 \<Longrightarrow> g x y = u"} instead of an
wenzelm@27040
   216
  unrestricted @{text "g \<equiv> \<lambda>x y. u"}.
wenzelm@27040
   217
  
wenzelm@28760
   218
  \item @{command "abbreviation"}~@{text "c \<WHERE> eq"} introduces a
wenzelm@28760
   219
  syntactic constant which is associated with a certain term according
wenzelm@28760
   220
  to the meta-level equality @{text eq}.
wenzelm@27040
   221
  
wenzelm@27040
   222
  Abbreviations participate in the usual type-inference process, but
wenzelm@27040
   223
  are expanded before the logic ever sees them.  Pretty printing of
wenzelm@27040
   224
  terms involves higher-order rewriting with rules stemming from
wenzelm@27040
   225
  reverted abbreviations.  This needs some care to avoid overlapping
wenzelm@27040
   226
  or looping syntactic replacements!
wenzelm@27040
   227
  
wenzelm@27040
   228
  The optional @{text mode} specification restricts output to a
wenzelm@27040
   229
  particular print mode; using ``@{text input}'' here achieves the
wenzelm@27040
   230
  effect of one-way abbreviations.  The mode may also include an
wenzelm@27040
   231
  ``@{keyword "output"}'' qualifier that affects the concrete syntax
wenzelm@27040
   232
  declared for abbreviations, cf.\ @{command "syntax"} in
wenzelm@27040
   233
  \secref{sec:syn-trans}.
wenzelm@27040
   234
  
wenzelm@28760
   235
  \item @{command "print_abbrevs"} prints all constant abbreviations
wenzelm@27040
   236
  of the current context.
wenzelm@27040
   237
  
wenzelm@28760
   238
  \end{description}
wenzelm@27040
   239
*}
wenzelm@27040
   240
wenzelm@27040
   241
wenzelm@27040
   242
section {* Generic declarations *}
wenzelm@27040
   243
wenzelm@27040
   244
text {*
wenzelm@27040
   245
  Arbitrary operations on the background context may be wrapped-up as
wenzelm@27040
   246
  generic declaration elements.  Since the underlying concept of local
wenzelm@27040
   247
  theories may be subject to later re-interpretation, there is an
wenzelm@27040
   248
  additional dependency on a morphism that tells the difference of the
wenzelm@27040
   249
  original declaration context wrt.\ the application context
wenzelm@27040
   250
  encountered later on.  A fact declaration is an important special
wenzelm@27040
   251
  case: it consists of a theorem which is applied to the context by
wenzelm@27040
   252
  means of an attribute.
wenzelm@27040
   253
wenzelm@27040
   254
  \begin{matharray}{rcl}
wenzelm@28761
   255
    @{command_def "declaration"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@41034
   256
    @{command_def "syntax_declaration"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   257
    @{command_def "declare"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@27040
   258
  \end{matharray}
wenzelm@27040
   259
wenzelm@43467
   260
  @{rail "
wenzelm@43467
   261
    (@@{command declaration} | @@{command syntax_declaration})
wenzelm@43575
   262
      ('(' @'pervasive' ')')? \\ @{syntax target}? @{syntax text}
wenzelm@27040
   263
    ;
wenzelm@43467
   264
    @@{command declare} @{syntax target}? (@{syntax thmrefs} + @'and')
wenzelm@43467
   265
  "}
wenzelm@27040
   266
wenzelm@28760
   267
  \begin{description}
wenzelm@27040
   268
wenzelm@28760
   269
  \item @{command "declaration"}~@{text d} adds the declaration
wenzelm@27040
   270
  function @{text d} of ML type @{ML_type declaration}, to the current
wenzelm@27040
   271
  local theory under construction.  In later application contexts, the
wenzelm@27040
   272
  function is transformed according to the morphisms being involved in
wenzelm@27040
   273
  the interpretation hierarchy.
wenzelm@27040
   274
wenzelm@33516
   275
  If the @{text "(pervasive)"} option is given, the corresponding
wenzelm@33516
   276
  declaration is applied to all possible contexts involved, including
wenzelm@33516
   277
  the global background theory.
wenzelm@33516
   278
wenzelm@41034
   279
  \item @{command "syntax_declaration"} is similar to @{command
wenzelm@41034
   280
  "declaration"}, but is meant to affect only ``syntactic'' tools by
wenzelm@41034
   281
  convention (such as notation and type-checking information).
wenzelm@41034
   282
wenzelm@28760
   283
  \item @{command "declare"}~@{text thms} declares theorems to the
wenzelm@27040
   284
  current local theory context.  No theorem binding is involved here,
wenzelm@27040
   285
  unlike @{command "theorems"} or @{command "lemmas"} (cf.\
wenzelm@27040
   286
  \secref{sec:axms-thms}), so @{command "declare"} only has the effect
wenzelm@27040
   287
  of applying attributes as included in the theorem specification.
wenzelm@27040
   288
wenzelm@28760
   289
  \end{description}
wenzelm@27040
   290
*}
wenzelm@27040
   291
wenzelm@27040
   292
wenzelm@27040
   293
section {* Locales \label{sec:locale} *}
wenzelm@27040
   294
wenzelm@27040
   295
text {*
ballarin@33845
   296
  Locales are parametric named local contexts, consisting of a list of
wenzelm@27040
   297
  declaration elements that are modeled after the Isar proof context
wenzelm@27040
   298
  commands (cf.\ \secref{sec:proof-context}).
wenzelm@27040
   299
*}
wenzelm@27040
   300
wenzelm@27040
   301
ballarin@33845
   302
subsection {* Locale expressions \label{sec:locale-expr} *}
ballarin@33845
   303
ballarin@33845
   304
text {*
ballarin@33845
   305
  A \emph{locale expression} denotes a structured context composed of
ballarin@33845
   306
  instances of existing locales.  The context consists of a list of
ballarin@33845
   307
  instances of declaration elements from the locales.  Two locale
ballarin@33845
   308
  instances are equal if they are of the same locale and the
ballarin@33845
   309
  parameters are instantiated with equivalent terms.  Declaration
ballarin@33845
   310
  elements from equal instances are never repeated, thus avoiding
ballarin@33845
   311
  duplicate declarations.
ballarin@33845
   312
wenzelm@43467
   313
  @{rail "
wenzelm@43488
   314
    @{syntax_def locale_expr}: (instance + '+') (@'for' (@{syntax \"fixes\"} + @'and'))?
ballarin@33845
   315
    ;
wenzelm@43488
   316
    instance: (qualifier ':')? @{syntax nameref} (pos_insts | named_insts)
ballarin@33845
   317
    ;
wenzelm@43467
   318
    qualifier: @{syntax name} ('?' | '!')?
ballarin@33845
   319
    ;
wenzelm@43488
   320
    pos_insts: ('_' | @{syntax term})*
ballarin@33845
   321
    ;
wenzelm@43488
   322
    named_insts: @'where' (@{syntax name} '=' @{syntax term} + @'and')
wenzelm@43467
   323
  "}
ballarin@33845
   324
ballarin@33845
   325
  A locale instance consists of a reference to a locale and either
ballarin@33845
   326
  positional or named parameter instantiations.  Identical
ballarin@33845
   327
  instantiations (that is, those that instante a parameter by itself)
wenzelm@40517
   328
  may be omitted.  The notation `@{text "_"}' enables to omit the
wenzelm@40517
   329
  instantiation for a parameter inside a positional instantiation.
ballarin@33845
   330
ballarin@33845
   331
  Terms in instantiations are from the context the locale expressions
ballarin@33845
   332
  is declared in.  Local names may be added to this context with the
ballarin@33845
   333
  optional for clause.  In addition, syntax declarations from one
ballarin@33845
   334
  instance are effective when parsing subsequent instances of the same
ballarin@33845
   335
  expression.
ballarin@33845
   336
ballarin@33845
   337
  Instances have an optional qualifier which applies to names in
ballarin@33845
   338
  declarations.  Names include local definitions and theorem names.
ballarin@33845
   339
  If present, the qualifier itself is either optional
ballarin@33845
   340
  (``\texttt{?}''), which means that it may be omitted on input of the
ballarin@33845
   341
  qualified name, or mandatory (``\texttt{!}'').  If neither
ballarin@33845
   342
  ``\texttt{?}'' nor ``\texttt{!}'' are present, the command's default
ballarin@33845
   343
  is used.  For @{command "interpretation"} and @{command "interpret"}
ballarin@33845
   344
  the default is ``mandatory'', for @{command "locale"} and @{command
ballarin@33845
   345
  "sublocale"} the default is ``optional''.
ballarin@33845
   346
*}
ballarin@33845
   347
ballarin@33845
   348
ballarin@33845
   349
subsection {* Locale declarations *}
wenzelm@27040
   350
wenzelm@27040
   351
text {*
wenzelm@27040
   352
  \begin{matharray}{rcl}
wenzelm@28761
   353
    @{command_def "locale"} & : & @{text "theory \<rightarrow> local_theory"} \\
wenzelm@28761
   354
    @{command_def "print_locale"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
   355
    @{command_def "print_locales"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
   356
    @{method_def intro_locales} & : & @{text method} \\
wenzelm@28761
   357
    @{method_def unfold_locales} & : & @{text method} \\
wenzelm@27040
   358
  \end{matharray}
wenzelm@27040
   359
wenzelm@27040
   360
  \indexisarelem{fixes}\indexisarelem{constrains}\indexisarelem{assumes}
wenzelm@28787
   361
  \indexisarelem{defines}\indexisarelem{notes}
wenzelm@43467
   362
  @{rail "
wenzelm@43467
   363
    @@{command locale} @{syntax name} ('=' @{syntax locale})? @'begin'?
wenzelm@27040
   364
    ;
wenzelm@43467
   365
    @@{command print_locale} '!'? @{syntax nameref}
wenzelm@27040
   366
    ;
wenzelm@43488
   367
    @{syntax_def locale}: @{syntax context_elem}+ |
wenzelm@43488
   368
      @{syntax locale_expr} ('+' (@{syntax context_elem}+))?
wenzelm@27040
   369
    ;
wenzelm@43488
   370
    @{syntax_def context_elem}:
wenzelm@43467
   371
      @'fixes' (@{syntax \"fixes\"} + @'and') |
wenzelm@43467
   372
      @'constrains' (@{syntax name} '::' @{syntax type} + @'and') |
wenzelm@43467
   373
      @'assumes' (@{syntax props} + @'and') |
wenzelm@43576
   374
      @'defines' (@{syntax thmdecl}? @{syntax prop} @{syntax prop_pat}? + @'and') |
wenzelm@43467
   375
      @'notes' (@{syntax thmdef}? @{syntax thmrefs} + @'and')
wenzelm@43467
   376
  "}
wenzelm@27040
   377
wenzelm@28760
   378
  \begin{description}
wenzelm@27040
   379
  
wenzelm@28760
   380
  \item @{command "locale"}~@{text "loc = import + body"} defines a
wenzelm@27040
   381
  new locale @{text loc} as a context consisting of a certain view of
wenzelm@27040
   382
  existing locales (@{text import}) plus some additional elements
wenzelm@27040
   383
  (@{text body}).  Both @{text import} and @{text body} are optional;
wenzelm@27040
   384
  the degenerate form @{command "locale"}~@{text loc} defines an empty
wenzelm@27040
   385
  locale, which may still be useful to collect declarations of facts
wenzelm@27040
   386
  later on.  Type-inference on locale expressions automatically takes
wenzelm@27040
   387
  care of the most general typing that the combined context elements
wenzelm@27040
   388
  may acquire.
wenzelm@27040
   389
ballarin@33845
   390
  The @{text import} consists of a structured locale expression; see
ballarin@33845
   391
  \secref{sec:proof-context} above.  Its for clause defines the local
ballarin@33845
   392
  parameters of the @{text import}.  In addition, locale parameters
ballarin@33845
   393
  whose instantance is omitted automatically extend the (possibly
ballarin@33845
   394
  empty) for clause: they are inserted at its beginning.  This means
ballarin@33845
   395
  that these parameters may be referred to from within the expression
ballarin@33845
   396
  and also in the subsequent context elements and provides a
ballarin@33845
   397
  notational convenience for the inheritance of parameters in locale
ballarin@33845
   398
  declarations.
wenzelm@27040
   399
ballarin@33845
   400
  The @{text body} consists of context elements.
wenzelm@27040
   401
wenzelm@28760
   402
  \begin{description}
wenzelm@27040
   403
wenzelm@28760
   404
  \item @{element "fixes"}~@{text "x :: \<tau> (mx)"} declares a local
wenzelm@27040
   405
  parameter of type @{text \<tau>} and mixfix annotation @{text mx} (both
wenzelm@27040
   406
  are optional).  The special syntax declaration ``@{text
wenzelm@27040
   407
  "(\<STRUCTURE>)"}'' means that @{text x} may be referenced
wenzelm@27040
   408
  implicitly in this context.
wenzelm@27040
   409
wenzelm@28760
   410
  \item @{element "constrains"}~@{text "x :: \<tau>"} introduces a type
ballarin@33845
   411
  constraint @{text \<tau>} on the local parameter @{text x}.  This
ballarin@38356
   412
  element is deprecated.  The type constraint should be introduced in
ballarin@33845
   413
  the for clause or the relevant @{element "fixes"} element.
wenzelm@27040
   414
wenzelm@28760
   415
  \item @{element "assumes"}~@{text "a: \<phi>\<^sub>1 \<dots> \<phi>\<^sub>n"}
wenzelm@27040
   416
  introduces local premises, similar to @{command "assume"} within a
wenzelm@27040
   417
  proof (cf.\ \secref{sec:proof-context}).
wenzelm@27040
   418
wenzelm@28760
   419
  \item @{element "defines"}~@{text "a: x \<equiv> t"} defines a previously
wenzelm@27040
   420
  declared parameter.  This is similar to @{command "def"} within a
wenzelm@27040
   421
  proof (cf.\ \secref{sec:proof-context}), but @{element "defines"}
wenzelm@27040
   422
  takes an equational proposition instead of variable-term pair.  The
wenzelm@27040
   423
  left-hand side of the equation may have additional arguments, e.g.\
wenzelm@27040
   424
  ``@{element "defines"}~@{text "f x\<^sub>1 \<dots> x\<^sub>n \<equiv> t"}''.
wenzelm@27040
   425
wenzelm@28760
   426
  \item @{element "notes"}~@{text "a = b\<^sub>1 \<dots> b\<^sub>n"}
wenzelm@27040
   427
  reconsiders facts within a local context.  Most notably, this may
wenzelm@27040
   428
  include arbitrary declarations in any attribute specifications
wenzelm@27040
   429
  included here, e.g.\ a local @{attribute simp} rule.
wenzelm@27040
   430
wenzelm@28787
   431
  The initial @{text import} specification of a locale expression
wenzelm@28787
   432
  maintains a dynamic relation to the locales being referenced
wenzelm@28787
   433
  (benefiting from any later fact declarations in the obvious manner).
wenzelm@27040
   434
wenzelm@28760
   435
  \end{description}
wenzelm@27040
   436
  
wenzelm@27040
   437
  Note that ``@{text "(\<IS> p\<^sub>1 \<dots> p\<^sub>n)"}'' patterns given
wenzelm@27040
   438
  in the syntax of @{element "assumes"} and @{element "defines"} above
wenzelm@27040
   439
  are illegal in locale definitions.  In the long goal format of
wenzelm@27040
   440
  \secref{sec:goals}, term bindings may be included as expected,
wenzelm@27040
   441
  though.
wenzelm@27040
   442
  
ballarin@33845
   443
  \medskip Locale specifications are ``closed up'' by
wenzelm@27040
   444
  turning the given text into a predicate definition @{text
wenzelm@27040
   445
  loc_axioms} and deriving the original assumptions as local lemmas
wenzelm@27040
   446
  (modulo local definitions).  The predicate statement covers only the
wenzelm@27040
   447
  newly specified assumptions, omitting the content of included locale
wenzelm@27040
   448
  expressions.  The full cumulative view is only provided on export,
wenzelm@27040
   449
  involving another predicate @{text loc} that refers to the complete
wenzelm@27040
   450
  specification text.
wenzelm@27040
   451
  
wenzelm@27040
   452
  In any case, the predicate arguments are those locale parameters
wenzelm@27040
   453
  that actually occur in the respective piece of text.  Also note that
wenzelm@27040
   454
  these predicates operate at the meta-level in theory, but the locale
wenzelm@27040
   455
  packages attempts to internalize statements according to the
wenzelm@27040
   456
  object-logic setup (e.g.\ replacing @{text \<And>} by @{text \<forall>}, and
wenzelm@27040
   457
  @{text "\<Longrightarrow>"} by @{text "\<longrightarrow>"} in HOL; see also
wenzelm@27040
   458
  \secref{sec:object-logic}).  Separate introduction rules @{text
wenzelm@27040
   459
  loc_axioms.intro} and @{text loc.intro} are provided as well.
wenzelm@27040
   460
  
ballarin@33867
   461
  \item @{command "print_locale"}~@{text "locale"} prints the
ballarin@33867
   462
  contents of the named locale.  The command omits @{element "notes"}
ballarin@33867
   463
  elements by default.  Use @{command "print_locale"}@{text "!"} to
ballarin@33867
   464
  have them included.
wenzelm@27040
   465
wenzelm@28760
   466
  \item @{command "print_locales"} prints the names of all locales
wenzelm@27040
   467
  of the current theory.
wenzelm@27040
   468
wenzelm@28760
   469
  \item @{method intro_locales} and @{method unfold_locales}
wenzelm@27040
   470
  repeatedly expand all introduction rules of locale predicates of the
wenzelm@27040
   471
  theory.  While @{method intro_locales} only applies the @{text
wenzelm@27040
   472
  loc.intro} introduction rules and therefore does not decend to
wenzelm@27040
   473
  assumptions, @{method unfold_locales} is more aggressive and applies
wenzelm@27040
   474
  @{text loc_axioms.intro} as well.  Both methods are aware of locale
wenzelm@28787
   475
  specifications entailed by the context, both from target statements,
wenzelm@28787
   476
  and from interpretations (see below).  New goals that are entailed
wenzelm@28787
   477
  by the current context are discharged automatically.
wenzelm@27040
   478
wenzelm@28760
   479
  \end{description}
wenzelm@27040
   480
*}
wenzelm@27040
   481
wenzelm@27040
   482
ballarin@33845
   483
subsection {* Locale interpretations *}
wenzelm@27040
   484
wenzelm@27040
   485
text {*
ballarin@33845
   486
  Locale expressions may be instantiated, and the instantiated facts
ballarin@33845
   487
  added to the current context.  This requires a proof of the
ballarin@33845
   488
  instantiated specification and is called \emph{locale
ballarin@33845
   489
  interpretation}.  Interpretation is possible in locales @{command
ballarin@33845
   490
  "sublocale"}, theories (command @{command "interpretation"}) and
ballarin@33845
   491
  also within a proof body (command @{command "interpret"}).
wenzelm@27040
   492
wenzelm@27040
   493
  \begin{matharray}{rcl}
wenzelm@28761
   494
    @{command_def "interpretation"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
ballarin@33845
   495
    @{command_def "interpret"} & : & @{text "proof(state) | proof(chain) \<rightarrow> proof(prove)"} \\
ballarin@41682
   496
    @{command_def "sublocale"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
ballarin@41683
   497
    @{command_def "print_dependencies"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
ballarin@33845
   498
    @{command_def "print_interps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@27040
   499
  \end{matharray}
wenzelm@27040
   500
wenzelm@43467
   501
  @{rail "
wenzelm@43488
   502
    @@{command interpretation} @{syntax locale_expr} equations?
wenzelm@27040
   503
    ;
wenzelm@43488
   504
    @@{command interpret} @{syntax locale_expr} equations?
wenzelm@27040
   505
    ;
wenzelm@43575
   506
    @@{command sublocale} @{syntax nameref} ('<' | '\<subseteq>') @{syntax locale_expr} \\
wenzelm@43575
   507
      equations?
ballarin@33845
   508
    ;
wenzelm@43488
   509
    @@{command print_dependencies} '!'? @{syntax locale_expr}
wenzelm@27040
   510
    ;
wenzelm@43467
   511
    @@{command print_interps} @{syntax nameref}
ballarin@41682
   512
    ;
wenzelm@43467
   513
wenzelm@43467
   514
    equations: @'where' (@{syntax thmdecl}? @{syntax prop} + @'and')
wenzelm@43467
   515
  "}
wenzelm@27040
   516
wenzelm@28760
   517
  \begin{description}
wenzelm@27040
   518
ballarin@33867
   519
  \item @{command "interpretation"}~@{text "expr \<WHERE> eqns"}
ballarin@33845
   520
  interprets @{text expr} in the theory.  The command generates proof
ballarin@33845
   521
  obligations for the instantiated specifications (assumes and defines
ballarin@33845
   522
  elements).  Once these are discharged by the user, instantiated
ballarin@33845
   523
  facts are added to the theory in a post-processing phase.
wenzelm@27040
   524
ballarin@33845
   525
  Additional equations, which are unfolded during
wenzelm@27040
   526
  post-processing, may be given after the keyword @{keyword "where"}.
wenzelm@27040
   527
  This is useful for interpreting concepts introduced through
ballarin@41682
   528
  definitions.  The equations must be proved.
wenzelm@27040
   529
wenzelm@27040
   530
  The command is aware of interpretations already active in the
ballarin@28085
   531
  theory, but does not simplify the goal automatically.  In order to
ballarin@28085
   532
  simplify the proof obligations use methods @{method intro_locales}
ballarin@28085
   533
  or @{method unfold_locales}.  Post-processing is not applied to
ballarin@28085
   534
  facts of interpretations that are already active.  This avoids
ballarin@28085
   535
  duplication of interpreted facts, in particular.  Note that, in the
ballarin@28085
   536
  case of a locale with import, parts of the interpretation may
ballarin@28085
   537
  already be active.  The command will only process facts for new
ballarin@28085
   538
  parts.
wenzelm@27040
   539
wenzelm@27040
   540
  Adding facts to locales has the effect of adding interpreted facts
ballarin@41682
   541
  to the theory for all interpretations as well.  That is,
wenzelm@27040
   542
  interpretations dynamically participate in any facts added to
ballarin@41682
   543
  locales.  Note that if a theory inherits additional facts for a
ballarin@41682
   544
  locale through one parent and an interpretation of that locale
ballarin@41682
   545
  through another parent, the additional facts will not be
ballarin@41682
   546
  interpreted.
wenzelm@27040
   547
ballarin@38356
   548
  \item @{command "interpret"}~@{text "expr \<WHERE> eqns"} interprets
ballarin@38356
   549
  @{text expr} in the proof context and is otherwise similar to
ballarin@38356
   550
  interpretation in theories.  Note that rewrite rules given to
ballarin@41682
   551
  @{command "interpret"} after the @{keyword "where"} keyword should be
ballarin@41682
   552
  explicitly universally quantified.
ballarin@41682
   553
ballarin@41682
   554
  \item @{command "sublocale"}~@{text "name \<subseteq> expr \<WHERE>
ballarin@41682
   555
  eqns"}
ballarin@41682
   556
  interprets @{text expr} in the locale @{text name}.  A proof that
ballarin@41682
   557
  the specification of @{text name} implies the specification of
ballarin@41682
   558
  @{text expr} is required.  As in the localized version of the
ballarin@41682
   559
  theorem command, the proof is in the context of @{text name}.  After
ballarin@41682
   560
  the proof obligation has been discharged, the facts of @{text expr}
ballarin@41682
   561
  become part of locale @{text name} as \emph{derived} context
ballarin@41682
   562
  elements and are available when the context @{text name} is
ballarin@41682
   563
  subsequently entered.  Note that, like import, this is dynamic:
ballarin@41682
   564
  facts added to a locale part of @{text expr} after interpretation
ballarin@41682
   565
  become also available in @{text name}.
ballarin@41682
   566
ballarin@41682
   567
  Only specification fragments of @{text expr} that are not already
ballarin@41682
   568
  part of @{text name} (be it imported, derived or a derived fragment
ballarin@41682
   569
  of the import) are considered in this process.  This enables
ballarin@41682
   570
  circular interpretations provided that no infinite chains are
ballarin@41682
   571
  generated in the locale hierarchy.
ballarin@41682
   572
ballarin@41682
   573
  If interpretations of @{text name} exist in the current theory, the
ballarin@41682
   574
  command adds interpretations for @{text expr} as well, with the same
ballarin@41682
   575
  qualifier, although only for fragments of @{text expr} that are not
ballarin@41682
   576
  interpreted in the theory already.
ballarin@41682
   577
ballarin@41682
   578
  Equations given after @{keyword "where"} amend the morphism through
ballarin@41682
   579
  which @{text expr} is interpreted.  This enables to map definitions
ballarin@41682
   580
  from the interpreted locales to entities of @{text name}.  This
ballarin@41682
   581
  feature is experimental.
wenzelm@27040
   582
ballarin@41683
   583
  \item @{command "print_dependencies"}~@{text "expr"} is useful for
ballarin@41683
   584
  understanding the effect of an interpretation of @{text "expr"}.  It
ballarin@41683
   585
  lists all locale instances for which interpretations would be added
ballarin@41683
   586
  to the current context.  Variant @{command
ballarin@41683
   587
  "print_dependencies"}@{text "!"} prints all locale instances that
ballarin@41683
   588
  would be considered for interpretation, and would be interpreted in
ballarin@41683
   589
  an empty context (that is, without interpretations).
ballarin@41683
   590
ballarin@33867
   591
  \item @{command "print_interps"}~@{text "locale"} lists all
ballarin@38356
   592
  interpretations of @{text "locale"} in the current theory or proof
ballarin@38356
   593
  context, including those due to a combination of a @{command
ballarin@38356
   594
  "interpretation"} or @{command "interpret"} and one or several
ballarin@38356
   595
  @{command "sublocale"} declarations.
ballarin@33867
   596
wenzelm@28760
   597
  \end{description}
wenzelm@27040
   598
wenzelm@27040
   599
  \begin{warn}
wenzelm@27040
   600
    Since attributes are applied to interpreted theorems,
wenzelm@27040
   601
    interpretation may modify the context of common proof tools, e.g.\
ballarin@33867
   602
    the Simplifier or Classical Reasoner.  As the behavior of such
ballarin@33867
   603
    tools is \emph{not} stable under interpretation morphisms, manual
ballarin@33867
   604
    declarations might have to be added to the target context of the
ballarin@33867
   605
    interpretation to revert such declarations.
wenzelm@27040
   606
  \end{warn}
wenzelm@27040
   607
wenzelm@27040
   608
  \begin{warn}
ballarin@38356
   609
    An interpretation in a theory or proof context may subsume previous
wenzelm@27040
   610
    interpretations.  This happens if the same specification fragment
wenzelm@27040
   611
    is interpreted twice and the instantiation of the second
wenzelm@27040
   612
    interpretation is more general than the interpretation of the
ballarin@33845
   613
    first.  The locale package does not attempt to remove subsumed
ballarin@33845
   614
    interpretations.
wenzelm@27040
   615
  \end{warn}
wenzelm@27040
   616
*}
wenzelm@27040
   617
wenzelm@27040
   618
wenzelm@27040
   619
section {* Classes \label{sec:class} *}
wenzelm@27040
   620
wenzelm@27040
   621
text {*
wenzelm@27040
   622
  A class is a particular locale with \emph{exactly one} type variable
wenzelm@27040
   623
  @{text \<alpha>}.  Beyond the underlying locale, a corresponding type class
wenzelm@27040
   624
  is established which is interpreted logically as axiomatic type
wenzelm@27040
   625
  class \cite{Wenzel:1997:TPHOL} whose logical content are the
wenzelm@27040
   626
  assumptions of the locale.  Thus, classes provide the full
wenzelm@27040
   627
  generality of locales combined with the commodity of type classes
wenzelm@27040
   628
  (notably type-inference).  See \cite{isabelle-classes} for a short
wenzelm@27040
   629
  tutorial.
wenzelm@27040
   630
wenzelm@27040
   631
  \begin{matharray}{rcl}
wenzelm@28761
   632
    @{command_def "class"} & : & @{text "theory \<rightarrow> local_theory"} \\
wenzelm@28761
   633
    @{command_def "instantiation"} & : & @{text "theory \<rightarrow> local_theory"} \\
wenzelm@28761
   634
    @{command_def "instance"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@43497
   635
    @{command "instance"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@28761
   636
    @{command_def "subclass"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   637
    @{command_def "print_classes"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@29706
   638
    @{command_def "class_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
   639
    @{method_def intro_classes} & : & @{text method} \\
wenzelm@27040
   640
  \end{matharray}
wenzelm@27040
   641
wenzelm@43467
   642
  @{rail "
wenzelm@43575
   643
    @@{command class} class_spec @'begin'?
wenzelm@43575
   644
    ;
wenzelm@43575
   645
    class_spec: @{syntax name} '='
wenzelm@43488
   646
      ((@{syntax nameref} '+' (@{syntax context_elem}+)) |
wenzelm@43575
   647
        @{syntax nameref} | (@{syntax context_elem}+))      
wenzelm@27040
   648
    ;
wenzelm@43467
   649
    @@{command instantiation} (@{syntax nameref} + @'and') '::' @{syntax arity} @'begin'
wenzelm@27040
   650
    ;
wenzelm@43575
   651
    @@{command instance} (() | (@{syntax nameref} + @'and') '::' @{syntax arity} |
wenzelm@43575
   652
      @{syntax nameref} ('<' | '\<subseteq>') @{syntax nameref} )
haftmann@31681
   653
    ;
wenzelm@43467
   654
    @@{command subclass} @{syntax target}? @{syntax nameref}
wenzelm@43488
   655
  "}
wenzelm@27040
   656
wenzelm@28760
   657
  \begin{description}
wenzelm@27040
   658
wenzelm@28760
   659
  \item @{command "class"}~@{text "c = superclasses + body"} defines
wenzelm@27040
   660
  a new class @{text c}, inheriting from @{text superclasses}.  This
wenzelm@27040
   661
  introduces a locale @{text c} with import of all locales @{text
wenzelm@27040
   662
  superclasses}.
wenzelm@27040
   663
wenzelm@27040
   664
  Any @{element "fixes"} in @{text body} are lifted to the global
wenzelm@27040
   665
  theory level (\emph{class operations} @{text "f\<^sub>1, \<dots>,
wenzelm@27040
   666
  f\<^sub>n"} of class @{text c}), mapping the local type parameter
wenzelm@27040
   667
  @{text \<alpha>} to a schematic type variable @{text "?\<alpha> :: c"}.
wenzelm@27040
   668
wenzelm@27040
   669
  Likewise, @{element "assumes"} in @{text body} are also lifted,
wenzelm@27040
   670
  mapping each local parameter @{text "f :: \<tau>[\<alpha>]"} to its
wenzelm@27040
   671
  corresponding global constant @{text "f :: \<tau>[?\<alpha> :: c]"}.  The
wenzelm@27040
   672
  corresponding introduction rule is provided as @{text
wenzelm@27040
   673
  c_class_axioms.intro}.  This rule should be rarely needed directly
wenzelm@27040
   674
  --- the @{method intro_classes} method takes care of the details of
wenzelm@27040
   675
  class membership proofs.
wenzelm@27040
   676
wenzelm@28768
   677
  \item @{command "instantiation"}~@{text "t :: (s\<^sub>1, \<dots>, s\<^sub>n)s
wenzelm@28760
   678
  \<BEGIN>"} opens a theory target (cf.\ \secref{sec:target}) which
wenzelm@28760
   679
  allows to specify class operations @{text "f\<^sub>1, \<dots>, f\<^sub>n"} corresponding
wenzelm@28760
   680
  to sort @{text s} at the particular type instance @{text "(\<alpha>\<^sub>1 :: s\<^sub>1,
wenzelm@28760
   681
  \<dots>, \<alpha>\<^sub>n :: s\<^sub>n) t"}.  A plain @{command "instance"} command in the
wenzelm@28760
   682
  target body poses a goal stating these type arities.  The target is
wenzelm@28760
   683
  concluded by an @{command_ref (local) "end"} command.
wenzelm@27040
   684
wenzelm@27040
   685
  Note that a list of simultaneous type constructors may be given;
haftmann@31908
   686
  this corresponds nicely to mutually recursive type definitions, e.g.\
wenzelm@27040
   687
  in Isabelle/HOL.
wenzelm@27040
   688
wenzelm@28760
   689
  \item @{command "instance"} in an instantiation target body sets
wenzelm@27040
   690
  up a goal stating the type arities claimed at the opening @{command
wenzelm@27040
   691
  "instantiation"}.  The proof would usually proceed by @{method
wenzelm@27040
   692
  intro_classes}, and then establish the characteristic theorems of
wenzelm@27040
   693
  the type classes involved.  After finishing the proof, the
wenzelm@27040
   694
  background theory will be augmented by the proven type arities.
wenzelm@27040
   695
haftmann@31681
   696
  On the theory level, @{command "instance"}~@{text "t :: (s\<^sub>1, \<dots>,
haftmann@31681
   697
  s\<^sub>n)s"} provides a convenient way to instantiate a type class with no
webertj@37096
   698
  need to specify operations: one can continue with the
haftmann@31681
   699
  instantiation proof immediately.
haftmann@31681
   700
wenzelm@28760
   701
  \item @{command "subclass"}~@{text c} in a class context for class
wenzelm@27040
   702
  @{text d} sets up a goal stating that class @{text c} is logically
wenzelm@27040
   703
  contained in class @{text d}.  After finishing the proof, class
wenzelm@27040
   704
  @{text d} is proven to be subclass @{text c} and the locale @{text
wenzelm@27040
   705
  c} is interpreted into @{text d} simultaneously.
wenzelm@27040
   706
haftmann@31681
   707
  A weakend form of this is available through a further variant of
haftmann@31681
   708
  @{command instance}:  @{command instance}~@{text "c\<^sub>1 \<subseteq> c\<^sub>2"} opens
haftmann@31681
   709
  a proof that class @{text "c\<^isub>2"} implies @{text "c\<^isub>1"} without reference
haftmann@31681
   710
  to the underlying locales;  this is useful if the properties to prove
haftmann@31681
   711
  the logical connection are not sufficent on the locale level but on
haftmann@31681
   712
  the theory level.
haftmann@31681
   713
wenzelm@28760
   714
  \item @{command "print_classes"} prints all classes in the current
wenzelm@27040
   715
  theory.
wenzelm@27040
   716
haftmann@29706
   717
  \item @{command "class_deps"} visualizes all classes and their
haftmann@29706
   718
  subclass relations as a Hasse diagram.
haftmann@29706
   719
wenzelm@28760
   720
  \item @{method intro_classes} repeatedly expands all class
wenzelm@27040
   721
  introduction rules of this theory.  Note that this method usually
wenzelm@27040
   722
  needs not be named explicitly, as it is already included in the
wenzelm@27040
   723
  default proof step (e.g.\ of @{command "proof"}).  In particular,
wenzelm@27040
   724
  instantiation of trivial (syntactic) classes may be performed by a
wenzelm@27040
   725
  single ``@{command ".."}'' proof step.
wenzelm@27040
   726
wenzelm@28760
   727
  \end{description}
wenzelm@27040
   728
*}
wenzelm@27040
   729
wenzelm@27040
   730
wenzelm@27040
   731
subsection {* The class target *}
wenzelm@27040
   732
wenzelm@27040
   733
text {*
wenzelm@27040
   734
  %FIXME check
wenzelm@27040
   735
wenzelm@27040
   736
  A named context may refer to a locale (cf.\ \secref{sec:target}).
wenzelm@27040
   737
  If this locale is also a class @{text c}, apart from the common
wenzelm@27040
   738
  locale target behaviour the following happens.
wenzelm@27040
   739
wenzelm@27040
   740
  \begin{itemize}
wenzelm@27040
   741
wenzelm@27040
   742
  \item Local constant declarations @{text "g[\<alpha>]"} referring to the
wenzelm@27040
   743
  local type parameter @{text \<alpha>} and local parameters @{text "f[\<alpha>]"}
wenzelm@27040
   744
  are accompanied by theory-level constants @{text "g[?\<alpha> :: c]"}
wenzelm@27040
   745
  referring to theory-level class operations @{text "f[?\<alpha> :: c]"}.
wenzelm@27040
   746
wenzelm@27040
   747
  \item Local theorem bindings are lifted as are assumptions.
wenzelm@27040
   748
wenzelm@27040
   749
  \item Local syntax refers to local operations @{text "g[\<alpha>]"} and
wenzelm@27040
   750
  global operations @{text "g[?\<alpha> :: c]"} uniformly.  Type inference
wenzelm@27040
   751
  resolves ambiguities.  In rare cases, manual type annotations are
wenzelm@27040
   752
  needed.
wenzelm@27040
   753
  
wenzelm@27040
   754
  \end{itemize}
wenzelm@27040
   755
*}
wenzelm@27040
   756
wenzelm@27040
   757
haftmann@37768
   758
subsection {* Co-regularity of type classes and arities *}
haftmann@37768
   759
haftmann@37768
   760
text {* The class relation together with the collection of
haftmann@37768
   761
  type-constructor arities must obey the principle of
haftmann@37768
   762
  \emph{co-regularity} as defined below.
haftmann@37768
   763
haftmann@37768
   764
  \medskip For the subsequent formulation of co-regularity we assume
haftmann@37768
   765
  that the class relation is closed by transitivity and reflexivity.
haftmann@37768
   766
  Moreover the collection of arities @{text "t :: (\<^vec>s)c"} is
haftmann@37768
   767
  completed such that @{text "t :: (\<^vec>s)c"} and @{text "c \<subseteq> c'"}
haftmann@37768
   768
  implies @{text "t :: (\<^vec>s)c'"} for all such declarations.
haftmann@37768
   769
haftmann@37768
   770
  Treating sorts as finite sets of classes (meaning the intersection),
haftmann@37768
   771
  the class relation @{text "c\<^sub>1 \<subseteq> c\<^sub>2"} is extended to sorts as
haftmann@37768
   772
  follows:
haftmann@37768
   773
  \[
haftmann@37768
   774
    @{text "s\<^sub>1 \<subseteq> s\<^sub>2 \<equiv> \<forall>c\<^sub>2 \<in> s\<^sub>2. \<exists>c\<^sub>1 \<in> s\<^sub>1. c\<^sub>1 \<subseteq> c\<^sub>2"}
haftmann@37768
   775
  \]
haftmann@37768
   776
haftmann@37768
   777
  This relation on sorts is further extended to tuples of sorts (of
haftmann@37768
   778
  the same length) in the component-wise way.
haftmann@37768
   779
haftmann@37768
   780
  \smallskip Co-regularity of the class relation together with the
haftmann@37768
   781
  arities relation means:
haftmann@37768
   782
  \[
haftmann@37768
   783
    @{text "t :: (\<^vec>s\<^sub>1)c\<^sub>1 \<Longrightarrow> t :: (\<^vec>s\<^sub>2)c\<^sub>2 \<Longrightarrow> c\<^sub>1 \<subseteq> c\<^sub>2 \<Longrightarrow> \<^vec>s\<^sub>1 \<subseteq> \<^vec>s\<^sub>2"}
haftmann@37768
   784
  \]
haftmann@37768
   785
  \noindent for all such arities.  In other words, whenever the result
haftmann@37768
   786
  classes of some type-constructor arities are related, then the
haftmann@37768
   787
  argument sorts need to be related in the same way.
haftmann@37768
   788
haftmann@37768
   789
  \medskip Co-regularity is a very fundamental property of the
haftmann@37768
   790
  order-sorted algebra of types.  For example, it entails principle
haftmann@37768
   791
  types and most general unifiers, e.g.\ see \cite{nipkow-prehofer}.
haftmann@37768
   792
*}
haftmann@37768
   793
haftmann@37768
   794
wenzelm@27040
   795
section {* Unrestricted overloading *}
wenzelm@27040
   796
wenzelm@27040
   797
text {*
wenzelm@27040
   798
  Isabelle/Pure's definitional schemes support certain forms of
haftmann@31047
   799
  overloading (see \secref{sec:consts}).  Overloading means that a
haftmann@31047
   800
  constant being declared as @{text "c :: \<alpha> decl"} may be
haftmann@31047
   801
  defined separately on type instances
haftmann@31047
   802
  @{text "c :: (\<beta>\<^sub>1, \<dots>, \<beta>\<^sub>n) t decl"}
haftmann@31047
   803
  for each type constructor @{text t}.  At most occassions
wenzelm@27040
   804
  overloading will be used in a Haskell-like fashion together with
wenzelm@27040
   805
  type classes by means of @{command "instantiation"} (see
wenzelm@27040
   806
  \secref{sec:class}).  Sometimes low-level overloading is desirable.
wenzelm@27040
   807
  The @{command "overloading"} target provides a convenient view for
wenzelm@27040
   808
  end-users.
wenzelm@27040
   809
wenzelm@27040
   810
  \begin{matharray}{rcl}
wenzelm@28761
   811
    @{command_def "overloading"} & : & @{text "theory \<rightarrow> local_theory"} \\
wenzelm@27040
   812
  \end{matharray}
wenzelm@27040
   813
wenzelm@43467
   814
  @{rail "
wenzelm@43575
   815
    @@{command overloading} ( spec + ) @'begin'
wenzelm@43575
   816
    ;
wenzelm@43575
   817
    spec: @{syntax name} ( '==' | '\<equiv>' ) @{syntax term} ( '(' @'unchecked' ')' )?
wenzelm@43488
   818
  "}
wenzelm@27040
   819
wenzelm@28760
   820
  \begin{description}
wenzelm@27040
   821
wenzelm@28760
   822
  \item @{command "overloading"}~@{text "x\<^sub>1 \<equiv> c\<^sub>1 :: \<tau>\<^sub>1 \<AND> \<dots> x\<^sub>n \<equiv> c\<^sub>n :: \<tau>\<^sub>n \<BEGIN>"}
wenzelm@27040
   823
  opens a theory target (cf.\ \secref{sec:target}) which allows to
wenzelm@27040
   824
  specify constants with overloaded definitions.  These are identified
wenzelm@28760
   825
  by an explicitly given mapping from variable names @{text "x\<^sub>i"} to
wenzelm@28760
   826
  constants @{text "c\<^sub>i"} at particular type instances.  The
wenzelm@28760
   827
  definitions themselves are established using common specification
wenzelm@28760
   828
  tools, using the names @{text "x\<^sub>i"} as reference to the
wenzelm@28760
   829
  corresponding constants.  The target is concluded by @{command
wenzelm@28760
   830
  (local) "end"}.
wenzelm@27040
   831
wenzelm@27040
   832
  A @{text "(unchecked)"} option disables global dependency checks for
wenzelm@27040
   833
  the corresponding definition, which is occasionally useful for
haftmann@31047
   834
  exotic overloading (see \secref{sec:consts} for a precise description).
haftmann@31047
   835
  It is at the discretion of the user to avoid
wenzelm@27040
   836
  malformed theory specifications!
wenzelm@27040
   837
wenzelm@28760
   838
  \end{description}
wenzelm@27040
   839
*}
wenzelm@27040
   840
wenzelm@27040
   841
wenzelm@27040
   842
section {* Incorporating ML code \label{sec:ML} *}
wenzelm@27040
   843
wenzelm@27040
   844
text {*
wenzelm@27040
   845
  \begin{matharray}{rcl}
wenzelm@28761
   846
    @{command_def "use"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   847
    @{command_def "ML"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   848
    @{command_def "ML_prf"} & : & @{text "proof \<rightarrow> proof"} \\
wenzelm@28761
   849
    @{command_def "ML_val"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28761
   850
    @{command_def "ML_command"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28761
   851
    @{command_def "setup"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@30461
   852
    @{command_def "local_setup"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@30528
   853
    @{command_def "attribute_setup"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28760
   854
  \end{matharray}
wenzelm@28760
   855
wenzelm@43467
   856
  @{rail "
wenzelm@43467
   857
    @@{command use} @{syntax name}
wenzelm@27040
   858
    ;
wenzelm@43467
   859
    (@@{command ML} | @@{command ML_prf} | @@{command ML_val} |
wenzelm@43467
   860
      @@{command ML_command} | @@{command setup} | @@{command local_setup}) @{syntax text}
wenzelm@27040
   861
    ;
wenzelm@43684
   862
    @@{command attribute_setup} @{syntax name} '=' @{syntax text} @{syntax text}?
wenzelm@43467
   863
  "}
wenzelm@27040
   864
wenzelm@28760
   865
  \begin{description}
wenzelm@27040
   866
wenzelm@28760
   867
  \item @{command "use"}~@{text "file"} reads and executes ML
wenzelm@27040
   868
  commands from @{text "file"}.  The current theory context is passed
wenzelm@30461
   869
  down to the ML toplevel and may be modified, using @{ML
wenzelm@27040
   870
  "Context.>>"} or derived ML commands.  The file name is checked with
wenzelm@27040
   871
  the @{keyword_ref "uses"} dependency declaration given in the theory
wenzelm@27040
   872
  header (see also \secref{sec:begin-thy}).
wenzelm@28281
   873
wenzelm@28281
   874
  Top-level ML bindings are stored within the (global or local) theory
wenzelm@28281
   875
  context.
wenzelm@27040
   876
  
wenzelm@28760
   877
  \item @{command "ML"}~@{text "text"} is similar to @{command "use"},
wenzelm@28760
   878
  but executes ML commands directly from the given @{text "text"}.
wenzelm@28760
   879
  Top-level ML bindings are stored within the (global or local) theory
wenzelm@28760
   880
  context.
wenzelm@28281
   881
wenzelm@28760
   882
  \item @{command "ML_prf"} is analogous to @{command "ML"} but works
wenzelm@28760
   883
  within a proof context.
wenzelm@28281
   884
wenzelm@28281
   885
  Top-level ML bindings are stored within the proof context in a
wenzelm@28281
   886
  purely sequential fashion, disregarding the nested proof structure.
wenzelm@28281
   887
  ML bindings introduced by @{command "ML_prf"} are discarded at the
wenzelm@28281
   888
  end of the proof.
wenzelm@27040
   889
wenzelm@28760
   890
  \item @{command "ML_val"} and @{command "ML_command"} are diagnostic
wenzelm@28760
   891
  versions of @{command "ML"}, which means that the context may not be
wenzelm@28760
   892
  updated.  @{command "ML_val"} echos the bindings produced at the ML
wenzelm@28760
   893
  toplevel, but @{command "ML_command"} is silent.
wenzelm@27040
   894
  
wenzelm@28760
   895
  \item @{command "setup"}~@{text "text"} changes the current theory
wenzelm@27040
   896
  context by applying @{text "text"}, which refers to an ML expression
wenzelm@30461
   897
  of type @{ML_type "theory -> theory"}.  This enables to initialize
wenzelm@30461
   898
  any object-logic specific tools and packages written in ML, for
wenzelm@30461
   899
  example.
wenzelm@30461
   900
wenzelm@30461
   901
  \item @{command "local_setup"} is similar to @{command "setup"} for
wenzelm@30461
   902
  a local theory context, and an ML expression of type @{ML_type
wenzelm@30461
   903
  "local_theory -> local_theory"}.  This allows to
wenzelm@30461
   904
  invoke local theory specification packages without going through
wenzelm@30461
   905
  concrete outer syntax, for example.
wenzelm@28758
   906
wenzelm@30528
   907
  \item @{command "attribute_setup"}~@{text "name = text description"}
wenzelm@30528
   908
  defines an attribute in the current theory.  The given @{text
wenzelm@30528
   909
  "text"} has to be an ML expression of type
wenzelm@30528
   910
  @{ML_type "attribute context_parser"}, cf.\ basic parsers defined in
wenzelm@30528
   911
  structure @{ML_struct Args} and @{ML_struct Attrib}.
wenzelm@30528
   912
wenzelm@30528
   913
  In principle, attributes can operate both on a given theorem and the
wenzelm@30528
   914
  implicit context, although in practice only one is modified and the
wenzelm@30528
   915
  other serves as parameter.  Here are examples for these two cases:
wenzelm@30528
   916
wenzelm@30528
   917
  \end{description}
wenzelm@30528
   918
*}
wenzelm@30528
   919
wenzelm@43575
   920
  attribute_setup my_rule = {*
wenzelm@43575
   921
    Attrib.thms >> (fn ths =>
wenzelm@43575
   922
      Thm.rule_attribute
wenzelm@43575
   923
        (fn context: Context.generic => fn th: thm =>
wenzelm@30528
   924
          let val th' = th OF ths
wenzelm@44140
   925
          in th' end)) *}
wenzelm@30528
   926
wenzelm@43575
   927
  attribute_setup my_declaration = {*
wenzelm@43575
   928
    Attrib.thms >> (fn ths =>
wenzelm@43575
   929
      Thm.declaration_attribute
wenzelm@43575
   930
        (fn th: thm => fn context: Context.generic =>
wenzelm@30528
   931
          let val context' = context
wenzelm@44140
   932
          in context' end)) *}
wenzelm@30528
   933
wenzelm@27040
   934
wenzelm@27040
   935
section {* Primitive specification elements *}
wenzelm@27040
   936
wenzelm@27040
   937
subsection {* Type classes and sorts \label{sec:classes} *}
wenzelm@27040
   938
wenzelm@27040
   939
text {*
wenzelm@27040
   940
  \begin{matharray}{rcll}
wenzelm@28761
   941
    @{command_def "classes"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
   942
    @{command_def "classrel"} & : & @{text "theory \<rightarrow> theory"} & (axiomatic!) \\
haftmann@37768
   943
    @{command_def "default_sort"} & : & @{text "local_theory \<rightarrow> local_theory"}
wenzelm@27040
   944
  \end{matharray}
wenzelm@27040
   945
wenzelm@43467
   946
  @{rail "
wenzelm@43467
   947
    @@{command classes} (@{syntax classdecl} +)
wenzelm@27040
   948
    ;
wenzelm@43467
   949
    @@{command classrel} (@{syntax nameref} ('<' | '\<subseteq>') @{syntax nameref} + @'and')
wenzelm@27040
   950
    ;
wenzelm@43467
   951
    @@{command default_sort} @{syntax sort}
wenzelm@43467
   952
  "}
wenzelm@27040
   953
wenzelm@28760
   954
  \begin{description}
wenzelm@27040
   955
wenzelm@28760
   956
  \item @{command "classes"}~@{text "c \<subseteq> c\<^sub>1, \<dots>, c\<^sub>n"} declares class
wenzelm@28760
   957
  @{text c} to be a subclass of existing classes @{text "c\<^sub>1, \<dots>, c\<^sub>n"}.
wenzelm@28767
   958
  Isabelle implicitly maintains the transitive closure of the class
wenzelm@28767
   959
  hierarchy.  Cyclic class structures are not permitted.
wenzelm@27040
   960
wenzelm@28760
   961
  \item @{command "classrel"}~@{text "c\<^sub>1 \<subseteq> c\<^sub>2"} states subclass
wenzelm@28760
   962
  relations between existing classes @{text "c\<^sub>1"} and @{text "c\<^sub>2"}.
haftmann@37768
   963
  This is done axiomatically!  The @{command_ref "subclass"} and
haftmann@37768
   964
  @{command_ref "instance"} commands (see \secref{sec:class}) provide
haftmann@37768
   965
  a way to introduce proven class relations.
wenzelm@27040
   966
wenzelm@36454
   967
  \item @{command "default_sort"}~@{text s} makes sort @{text s} the
wenzelm@28767
   968
  new default sort for any type variable that is given explicitly in
wenzelm@28767
   969
  the text, but lacks a sort constraint (wrt.\ the current context).
wenzelm@28767
   970
  Type variables generated by type inference are not affected.
wenzelm@28767
   971
wenzelm@28767
   972
  Usually the default sort is only changed when defining a new
wenzelm@28767
   973
  object-logic.  For example, the default sort in Isabelle/HOL is
wenzelm@40270
   974
  @{class type}, the class of all HOL types.
wenzelm@28767
   975
wenzelm@28767
   976
  When merging theories, the default sorts of the parents are
wenzelm@28767
   977
  logically intersected, i.e.\ the representations as lists of classes
wenzelm@28767
   978
  are joined.
wenzelm@27040
   979
wenzelm@28760
   980
  \end{description}
wenzelm@27040
   981
*}
wenzelm@27040
   982
wenzelm@27040
   983
wenzelm@27040
   984
subsection {* Types and type abbreviations \label{sec:types-pure} *}
wenzelm@27040
   985
wenzelm@27040
   986
text {*
wenzelm@27040
   987
  \begin{matharray}{rcll}
wenzelm@41497
   988
    @{command_def "type_synonym"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@35681
   989
    @{command_def "typedecl"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   990
    @{command_def "arities"} & : & @{text "theory \<rightarrow> theory"} & (axiomatic!) \\
wenzelm@27040
   991
  \end{matharray}
wenzelm@27040
   992
wenzelm@43467
   993
  @{rail "
wenzelm@43467
   994
    @@{command type_synonym} (@{syntax typespec} '=' @{syntax type} @{syntax mixfix}?)
wenzelm@27040
   995
    ;
wenzelm@43467
   996
    @@{command typedecl} @{syntax typespec} @{syntax mixfix}?
wenzelm@27040
   997
    ;
wenzelm@43467
   998
    @@{command arities} (@{syntax nameref} '::' @{syntax arity} +)
wenzelm@43467
   999
  "}
wenzelm@27040
  1000
wenzelm@28760
  1001
  \begin{description}
wenzelm@27040
  1002
wenzelm@41497
  1003
  \item @{command "type_synonym"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = \<tau>"}
wenzelm@41497
  1004
  introduces a \emph{type synonym} @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} for the
wenzelm@41497
  1005
  existing type @{text "\<tau>"}.  Unlike actual type definitions, as are
wenzelm@41497
  1006
  available in Isabelle/HOL for example, type synonyms are merely
wenzelm@41497
  1007
  syntactic abbreviations without any logical significance.
wenzelm@41497
  1008
  Internally, type synonyms are fully expanded.
wenzelm@27040
  1009
  
wenzelm@28760
  1010
  \item @{command "typedecl"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} declares a new
wenzelm@28767
  1011
  type constructor @{text t}.  If the object-logic defines a base sort
wenzelm@28767
  1012
  @{text s}, then the constructor is declared to operate on that, via
wenzelm@28767
  1013
  the axiomatic specification @{command arities}~@{text "t :: (s, \<dots>,
wenzelm@28768
  1014
  s)s"}.
wenzelm@27040
  1015
wenzelm@28768
  1016
  \item @{command "arities"}~@{text "t :: (s\<^sub>1, \<dots>, s\<^sub>n)s"} augments
wenzelm@28760
  1017
  Isabelle's order-sorted signature of types by new type constructor
haftmann@35282
  1018
  arities.  This is done axiomatically!  The @{command_ref "instantiation"}
haftmann@35282
  1019
  target (see \secref{sec:class}) provides a way to introduce
wenzelm@28768
  1020
  proven type arities.
wenzelm@27040
  1021
wenzelm@28760
  1022
  \end{description}
wenzelm@27040
  1023
*}
wenzelm@27040
  1024
wenzelm@27040
  1025
wenzelm@27040
  1026
subsection {* Constants and definitions \label{sec:consts} *}
wenzelm@27040
  1027
wenzelm@27040
  1028
text {*
wenzelm@27040
  1029
  Definitions essentially express abbreviations within the logic.  The
wenzelm@27040
  1030
  simplest form of a definition is @{text "c :: \<sigma> \<equiv> t"}, where @{text
wenzelm@27040
  1031
  c} is a newly declared constant.  Isabelle also allows derived forms
wenzelm@27040
  1032
  where the arguments of @{text c} appear on the left, abbreviating a
wenzelm@27040
  1033
  prefix of @{text \<lambda>}-abstractions, e.g.\ @{text "c \<equiv> \<lambda>x y. t"} may be
wenzelm@27040
  1034
  written more conveniently as @{text "c x y \<equiv> t"}.  Moreover,
wenzelm@27040
  1035
  definitions may be weakened by adding arbitrary pre-conditions:
wenzelm@27040
  1036
  @{text "A \<Longrightarrow> c x y \<equiv> t"}.
wenzelm@27040
  1037
wenzelm@27040
  1038
  \medskip The built-in well-formedness conditions for definitional
wenzelm@27040
  1039
  specifications are:
wenzelm@27040
  1040
wenzelm@27040
  1041
  \begin{itemize}
wenzelm@27040
  1042
wenzelm@27040
  1043
  \item Arguments (on the left-hand side) must be distinct variables.
wenzelm@27040
  1044
wenzelm@27040
  1045
  \item All variables on the right-hand side must also appear on the
wenzelm@27040
  1046
  left-hand side.
wenzelm@27040
  1047
wenzelm@27040
  1048
  \item All type variables on the right-hand side must also appear on
wenzelm@27040
  1049
  the left-hand side; this prohibits @{text "0 :: nat \<equiv> length ([] ::
wenzelm@27040
  1050
  \<alpha> list)"} for example.
wenzelm@27040
  1051
wenzelm@27040
  1052
  \item The definition must not be recursive.  Most object-logics
wenzelm@27040
  1053
  provide definitional principles that can be used to express
wenzelm@27040
  1054
  recursion safely.
wenzelm@27040
  1055
wenzelm@27040
  1056
  \end{itemize}
wenzelm@27040
  1057
haftmann@31047
  1058
  The right-hand side of overloaded definitions may mention overloaded constants
wenzelm@27040
  1059
  recursively at type instances corresponding to the immediate
wenzelm@27040
  1060
  argument types @{text "\<beta>\<^sub>1, \<dots>, \<beta>\<^sub>n"}.  Incomplete
wenzelm@27040
  1061
  specification patterns impose global constraints on all occurrences,
wenzelm@27040
  1062
  e.g.\ @{text "d :: \<alpha> \<times> \<alpha>"} on the left-hand side means that all
wenzelm@27040
  1063
  corresponding occurrences on some right-hand side need to be an
wenzelm@27040
  1064
  instance of this, general @{text "d :: \<alpha> \<times> \<beta>"} will be disallowed.
wenzelm@27040
  1065
wenzelm@27040
  1066
  \begin{matharray}{rcl}
wenzelm@28761
  1067
    @{command_def "consts"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1068
    @{command_def "defs"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@27040
  1069
  \end{matharray}
wenzelm@27040
  1070
wenzelm@43467
  1071
  @{rail "
wenzelm@43467
  1072
    @@{command consts} ((@{syntax name} '::' @{syntax type} @{syntax mixfix}?) +)
wenzelm@27040
  1073
    ;
wenzelm@43575
  1074
    @@{command defs} opt? (@{syntax axmdecl} @{syntax prop} +)
wenzelm@43575
  1075
    ;
wenzelm@43575
  1076
    opt: '(' @'unchecked'? @'overloaded'? ')'
wenzelm@43467
  1077
  "}
wenzelm@27040
  1078
wenzelm@28760
  1079
  \begin{description}
wenzelm@27040
  1080
wenzelm@28760
  1081
  \item @{command "consts"}~@{text "c :: \<sigma>"} declares constant @{text
wenzelm@28760
  1082
  c} to have any instance of type scheme @{text \<sigma>}.  The optional
wenzelm@28760
  1083
  mixfix annotations may attach concrete syntax to the constants
wenzelm@28760
  1084
  declared.
wenzelm@27040
  1085
  
wenzelm@28760
  1086
  \item @{command "defs"}~@{text "name: eqn"} introduces @{text eqn}
wenzelm@27040
  1087
  as a definitional axiom for some existing constant.
wenzelm@27040
  1088
  
wenzelm@27040
  1089
  The @{text "(unchecked)"} option disables global dependency checks
wenzelm@27040
  1090
  for this definition, which is occasionally useful for exotic
wenzelm@27040
  1091
  overloading.  It is at the discretion of the user to avoid malformed
wenzelm@27040
  1092
  theory specifications!
wenzelm@27040
  1093
  
wenzelm@27040
  1094
  The @{text "(overloaded)"} option declares definitions to be
wenzelm@27040
  1095
  potentially overloaded.  Unless this option is given, a warning
wenzelm@27040
  1096
  message would be issued for any definitional equation with a more
wenzelm@27040
  1097
  special type than that of the corresponding constant declaration.
wenzelm@27040
  1098
  
wenzelm@28760
  1099
  \end{description}
wenzelm@27040
  1100
*}
wenzelm@27040
  1101
wenzelm@27040
  1102
wenzelm@27040
  1103
section {* Axioms and theorems \label{sec:axms-thms} *}
wenzelm@27040
  1104
wenzelm@27040
  1105
text {*
wenzelm@27040
  1106
  \begin{matharray}{rcll}
wenzelm@28761
  1107
    @{command_def "axioms"} & : & @{text "theory \<rightarrow> theory"} & (axiomatic!) \\
wenzelm@28761
  1108
    @{command_def "lemmas"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
  1109
    @{command_def "theorems"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@27040
  1110
  \end{matharray}
wenzelm@27040
  1111
wenzelm@43467
  1112
  @{rail "
wenzelm@43467
  1113
    @@{command axioms} (@{syntax axmdecl} @{syntax prop} +)
wenzelm@27040
  1114
    ;
wenzelm@46471
  1115
    (@@{command lemmas} | @@{command theorems}) @{syntax target}? \\
wenzelm@43467
  1116
      (@{syntax thmdef}? @{syntax thmrefs} + @'and')
wenzelm@46471
  1117
      (@'for' (@{syntax vars} + @'and'))?
wenzelm@43467
  1118
  "}
wenzelm@27040
  1119
wenzelm@28760
  1120
  \begin{description}
wenzelm@27040
  1121
  
wenzelm@28760
  1122
  \item @{command "axioms"}~@{text "a: \<phi>"} introduces arbitrary
wenzelm@27040
  1123
  statements as axioms of the meta-logic.  In fact, axioms are
wenzelm@27040
  1124
  ``axiomatic theorems'', and may be referred later just as any other
wenzelm@27040
  1125
  theorem.
wenzelm@27040
  1126
  
wenzelm@27040
  1127
  Axioms are usually only introduced when declaring new logical
wenzelm@27040
  1128
  systems.  Everyday work is typically done the hard way, with proper
wenzelm@27040
  1129
  definitions and proven theorems.
wenzelm@27040
  1130
  
wenzelm@46471
  1131
  \item @{command "lemmas"}~@{text "a = b\<^sub>1 \<dots> b\<^sub>n"}~@{keyword_def
wenzelm@46471
  1132
  "for"}~@{text "x\<^sub>1 \<dots> x\<^sub>m"} evaluates given facts (with attributes) in
wenzelm@46471
  1133
  the current context, which may be augmented by local variables.
wenzelm@46471
  1134
  Results are standardized before being stored, i.e.\ schematic
wenzelm@46471
  1135
  variables are renamed to enforce index @{text "0"} uniformly.
wenzelm@46471
  1136
wenzelm@46471
  1137
  \item @{command "theorems"} is the same as @{command "lemmas"}, but
wenzelm@46471
  1138
  marks the result as a different kind of facts.
wenzelm@27040
  1139
wenzelm@28760
  1140
  \end{description}
wenzelm@27040
  1141
*}
wenzelm@27040
  1142
wenzelm@27040
  1143
wenzelm@27040
  1144
section {* Oracles *}
wenzelm@27040
  1145
wenzelm@28756
  1146
text {* Oracles allow Isabelle to take advantage of external reasoners
wenzelm@28756
  1147
  such as arithmetic decision procedures, model checkers, fast
wenzelm@28756
  1148
  tautology checkers or computer algebra systems.  Invoked as an
wenzelm@28756
  1149
  oracle, an external reasoner can create arbitrary Isabelle theorems.
wenzelm@28756
  1150
wenzelm@28756
  1151
  It is the responsibility of the user to ensure that the external
wenzelm@28756
  1152
  reasoner is as trustworthy as the application requires.  Another
wenzelm@28756
  1153
  typical source of errors is the linkup between Isabelle and the
wenzelm@28756
  1154
  external tool, not just its concrete implementation, but also the
wenzelm@28756
  1155
  required translation between two different logical environments.
wenzelm@28756
  1156
wenzelm@28756
  1157
  Isabelle merely guarantees well-formedness of the propositions being
wenzelm@28756
  1158
  asserted, and records within the internal derivation object how
wenzelm@28756
  1159
  presumed theorems depend on unproven suppositions.
wenzelm@28756
  1160
wenzelm@40496
  1161
  \begin{matharray}{rcll}
wenzelm@40496
  1162
    @{command_def "oracle"} & : & @{text "theory \<rightarrow> theory"} & (axiomatic!) \\
wenzelm@27040
  1163
  \end{matharray}
wenzelm@27040
  1164
wenzelm@43467
  1165
  @{rail "
wenzelm@43467
  1166
    @@{command oracle} @{syntax name} '=' @{syntax text}
wenzelm@43467
  1167
  "}
wenzelm@27040
  1168
wenzelm@28760
  1169
  \begin{description}
wenzelm@27040
  1170
wenzelm@28760
  1171
  \item @{command "oracle"}~@{text "name = text"} turns the given ML
wenzelm@28290
  1172
  expression @{text "text"} of type @{ML_text "'a -> cterm"} into an
wenzelm@28290
  1173
  ML function of type @{ML_text "'a -> thm"}, which is bound to the
wenzelm@28756
  1174
  global identifier @{ML_text name}.  This acts like an infinitary
wenzelm@28756
  1175
  specification of axioms!  Invoking the oracle only works within the
wenzelm@28756
  1176
  scope of the resulting theory.
wenzelm@27040
  1177
wenzelm@28760
  1178
  \end{description}
wenzelm@28756
  1179
wenzelm@41048
  1180
  See @{file "~~/src/HOL/ex/Iff_Oracle.thy"} for a worked example of
wenzelm@28756
  1181
  defining a new primitive rule as oracle, and turning it into a proof
wenzelm@28756
  1182
  method.
wenzelm@27040
  1183
*}
wenzelm@27040
  1184
wenzelm@27040
  1185
wenzelm@27040
  1186
section {* Name spaces *}
wenzelm@27040
  1187
wenzelm@27040
  1188
text {*
wenzelm@27040
  1189
  \begin{matharray}{rcl}
wenzelm@36177
  1190
    @{command_def "hide_class"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@36177
  1191
    @{command_def "hide_type"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@36177
  1192
    @{command_def "hide_const"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@36177
  1193
    @{command_def "hide_fact"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@27040
  1194
  \end{matharray}
wenzelm@27040
  1195
wenzelm@43467
  1196
  @{rail "
wenzelm@43467
  1197
    ( @{command hide_class} | @{command hide_type} |
wenzelm@43467
  1198
      @{command hide_const} | @{command hide_fact} ) ('(' @'open' ')')? (@{syntax nameref} + )
wenzelm@43467
  1199
  "}
wenzelm@27040
  1200
wenzelm@27040
  1201
  Isabelle organizes any kind of name declarations (of types,
wenzelm@27040
  1202
  constants, theorems etc.) by separate hierarchically structured name
wenzelm@27040
  1203
  spaces.  Normally the user does not have to control the behavior of
wenzelm@27040
  1204
  name spaces by hand, yet the following commands provide some way to
wenzelm@27040
  1205
  do so.
wenzelm@27040
  1206
wenzelm@28760
  1207
  \begin{description}
wenzelm@27040
  1208
wenzelm@36177
  1209
  \item @{command "hide_class"}~@{text names} fully removes class
wenzelm@36177
  1210
  declarations from a given name space; with the @{text "(open)"}
krauss@40158
  1211
  option, only the base name is hidden.
wenzelm@36177
  1212
wenzelm@27040
  1213
  Note that hiding name space accesses has no impact on logical
wenzelm@28756
  1214
  declarations --- they remain valid internally.  Entities that are no
wenzelm@27040
  1215
  longer accessible to the user are printed with the special qualifier
wenzelm@27040
  1216
  ``@{text "??"}'' prefixed to the full internal name.
wenzelm@27040
  1217
wenzelm@36177
  1218
  \item @{command "hide_type"}, @{command "hide_const"}, and @{command
wenzelm@36177
  1219
  "hide_fact"} are similar to @{command "hide_class"}, but hide types,
wenzelm@36177
  1220
  constants, and facts, respectively.
wenzelm@36177
  1221
  
wenzelm@28760
  1222
  \end{description}
wenzelm@27040
  1223
*}
wenzelm@27040
  1224
wenzelm@26869
  1225
end