text\<open>
The Isabelle/Isar theory format integrates specifications and proofs, with
support for interactive development by continuous document editing. There is
a separate document preparation system (see \chref{ch:document-prep}), for
typesetting formal developments together with informal text. The resulting
hyper-linked PDF documents can be used both for WWW presentation and printed
copies.
The Isar proof language (see \chref{ch:proofs}) is embedded into the theory
language as a proper sub-language. Proof mode is entered by stating some \<^theory_text>\<open>theorem\<close> or \<^theory_text>\<open>lemma\<close> at the theory level, and left again with the final
conclusion (e.g.\ via \<^theory_text>\<open>qed\<close>). \<close>
Isabelle/Isar theories are defined via theory files, which consist of an
outermost sequence of definition--statement--proof elements. Some
definitions are self-sufficient (e.g.\ \<^theory_text>\<open>fun\<close> in Isabelle/HOL), with
foundational proofs performed internally. Other definitions require an
explicit proof as justification (e.g.\ \<^theory_text>\<open>function\<close> and \<^theory_text>\<open>termination\<close> in
Isabelle/HOL). Plain statements like \<^theory_text>\<open>theorem\<close> or \<^theory_text>\<open>lemma\<close> are merely a
special case of that, defining a theoremfrom a given proposition and its proof.
The theory body may be sub-structured by means of \<^emph>\<open>local theory targets\<close>,
such as \<^theory_text>\<open>locale\<close> and \<^theory_text>\<open>class\<close>. It is also possible to use \<^theory_text>\<open>context begin \<dots> end\<close> blocks to delimited a local theory context: a \<^emph>\<open>named context\<close> to
augment a locale or classspecification, or an \<^emph>\<open>unnamed context\<close> to refer tolocal parameters and assumptions that are discharged later. See \secref{sec:target} for more details.
\<^medskip>
A theoryis commenced by the \<^theory_text>\<open>theory\<close> command, which indicates imports of
previous theories, according to an acyclic foundational order. Before the
initial \<^theory_text>\<open>theory\<close> command, there may be optional document header material
(like \<^theory_text>\<open>section\<close> or \<^theory_text>\<open>text\<close>, see \secref{sec:markup}). The document header is outside of the formal theorycontext, though.
A theoryis concluded by a final @{command (global) "end"} command, one that
does not belong to a localtheory target. No further commands may follow
such a global @{command (global) "end"}.
\<^descr> \<^theory_text>\<open>theory A imports B\<^sub>1 \<dots> B\<^sub>n begin\<close> starts a new theory \<open>A\<close> based on the
merge of existing theories \<open>B\<^sub>1 \<dots> B\<^sub>n\<close>. Due to the possibility to import
more than one ancestor, the resulting theorystructure of an Isabelle
session forms a directed acyclic graph (DAG). Isabelle takes care that
sources contributing to the development graph are always up-to-date: changed
files are automatically rechecked whenever a theoryheaderspecificationis
processed.
Empty imports are only allowed in the bootstrap process of the special theory\<^theory>\<open>Pure\<close>, which is the start of any other formal development
based on Isabelle. Regular user theories usually refer to some more complex
entry point, such as theory\<^theory>\<open>Main\<close> in Isabelle/HOL.
The @{keyword_def "keywords"} specification declares outer syntax
(\chref{ch:outer-syntax}) that is introduced in this theory later on (rare in end-user applications). Both minor keywords and major keywords of the
Isar command language need to be specified, in order to make parsing of proof documents work properly. Command keywords need to be classified
according to their structural role in the formal text. Examples may be seen in Isabelle/HOL sources itself, such as @{keyword "keywords"}~\<^verbatim>\<open>"typedef"\<close> \<open>:: thy_goal_defn\<close> or @{keyword "keywords"}~\<^verbatim>\<open>"datatype"\<close> \<open>:: thy_defn\<close> for
theory-level definitions withand without proof, respectively. Additional
@{syntax tags} provide defaults for document preparation
(\secref{sec:document-markers}).
The @{keyword_def "abbrevs"} specification declares additional abbreviations for syntactic completion. The default for a new keyword is just its name,
but completion may be avoided by defining @{keyword "abbrevs"} with empty text.
\<^descr> @{command (global) "end"} concludes the current theory definition. Note
that some other commands, e.g.\ local theory targets \<^theory_text>\<open>locale\<close> or \<^theory_text>\<open>class\<close>
may involve a \<^theory_text>\<open>begin\<close> that needs to be matched by @{command (local) "end"},
according to the usual rules for nested blocks.
\<^descr> \<^theory_text>\<open>thy_deps\<close> visualizes the theory hierarchy as a directed acyclic graph. By default, all imported theories are shown. This may be restricted by
specifying bounds wrt. the theory inclusion relation. \<close>
section \<open>Local theory targets \label{sec:target}\<close>
A localtheory target is a specificationcontext that is managed separately
within the enclosing theory. Contexts may introduce parameters (fixed
variables) and assumptions (hypotheses). Definitions andtheorems depending
on the context may be added incrementally later on.
\<^emph>\<open>Named contexts\<close> refer to locales (cf.\ \secref{sec:locale}) or type classes (cf.\ \secref{sec:class}); the name ``\<open>-\<close>'' signifies the global theorycontext.
\<^emph>\<open>Unnamed contexts\<close> may introduce additional parameters and assumptions, and
results produced in the context are generalized accordingly. Such auxiliary
contexts may be nested within other targets, like \<^theory_text>\<open>locale\<close>, \<^theory_text>\<open>class\<close>, \<^theory_text>\<open>instantiation\<close>, \<^theory_text>\<open>overloading\<close>.
\<^descr> \<^theory_text>\<open>context c bundles begin\<close> opens a named context, by recommencing an existing locale or class\<open>c\<close>. Note that locale and class definitions allow to include
the \<^theory_text>\<open>begin\<close> keyword as well, in order to continue the local theory
immediately after the initial specification. Optionally given \<open>bundles\<close> only take effect in the surface context within the \<^theory_text>\<open>begin\<close> / \<^theory_text>\<open>end\<close> block.
\<^descr> \<^theory_text>\<open>context bundles elements begin\<close> opens an unnamed context, by extending
the enclosing global or localtheory target by the given declaration bundles
(\secref{sec:bundle}) and context elements (\<^theory_text>\<open>fixes\<close>, \<^theory_text>\<open>assumes\<close> etc.). This
means any results stemming from definitions and proofs in the extended context will be exported into the enclosing target by lifting over extra
parameters and premises.
\<^descr> @{command (local) "end"} concludes the current local theory, according to
the nesting of contexts. Note that a global @{command (global) "end"} has a
different meaning: it concludes the theory itself (\secref{sec:begin-thy}).
\<^descr> \<^theory_text>\<open>private\<close> or \<^theory_text>\<open>qualified\<close> may be given as modifiers before any local theory command. This restricts name space accesses to the local scope, as
determined by the enclosing \<^theory_text>\<open>context begin \<dots> end\<close> block. Outside its scope,
a \<^theory_text>\<open>private\<close> name is inaccessible, and a \<^theory_text>\<open>qualified\<close> name is only
accessible with some qualification.
Neither a global\<^theory_text>\<open>theory\<close> nor a \<^theory_text>\<open>locale\<close> target provides a local scope by
itself: an extra unnamed contextis required touse\<^theory_text>\<open>private\<close> or \<^theory_text>\<open>qualified\<close> here.
\<^descr> \<open>(\<close>@{keyword_def "in"}~\<open>c)\<close> given after any local theory command specifies
an immediate target, e.g.\ ``\<^theory_text>\<open>definition (in c)\<close>'' or
``\<^theory_text>\<open>theorem (in c)\<close>''. This works both in a local or global theory context;
the current target context will be suspended for this command only. Note
that ``\<^theory_text>\<open>(in -)\<close>'' will always produce a global result independently of the
current target context.
Any specification element that operates on \<open>local_theory\<close> according to this
manual implicitly allows the above target syntax\<^theory_text>\<open>(in c)\<close>, but individual syntax diagrams omit that aspect for clarity.
\<^medskip>
The exact meaning of results produced within a localtheorycontext depends
on the underlying target infrastructure (locale, type class etc.). The
general idea is as follows, considering a context named \<open>c\<close> with parameter \<open>x\<close> and assumption \<open>A[x]\<close>.
Definitions are exported by introducing a global version with additional
arguments; a syntactic abbreviation links the long form with the abstract
version of the target context. For example, \<open>a \<equiv> t[x]\<close> becomes \<open>c.a ?x \<equiv>
t[?x]\<close> at the theory level (for arbitrary \<open>?x\<close>), together with a local abbreviation\<open>a \<equiv> c.a x\<close> in the target context (for the fixed parameter \<open>x\<close>).
Theorems are exported by discharging the assumptions and generalizing the
parameters of the context. For example, \<open>a: B[x]\<close> becomes \<open>c.a: A[?x] \<Longrightarrow>
B[?x]\<close>, again for arbitrary \<open>?x\<close>. \<close>
The outer syntax of fact expressions (\secref{sec:syn-att}) involves theoremsand attributes, which are evaluated in the contextand applied to
it. Attributes may declaretheoremsto the context, as in\<open>this_rule [intro]
that_rule [elim]\<close> for example. Configuration options (\secref{sec:config})
are special declaration attributes that operate on the context without a theorem, as in\<open>[[show_types = false]]\<close> for example.
Expressions of this form may be defined as \<^emph>\<open>bundled declarations\<close> in the context, and included in other situations later on. Including declaration
bundles augments a localcontext casually without logical dependencies,
which isin contrast to locales andlocaleinterpretation
(\secref{sec:locale}).
\<^descr> \<^theory_text>\<open>bundle b = decls\<close> defines a bundle of declarations in the current context. The RHS is similar to the one of the \<^theory_text>\<open>declare\<close> command. Bundles
defined inlocaltheory targets are subject to transformations via morphisms, when moved into different application contexts; this works
analogously to any other localtheoryspecification.
\<^descr> \<^theory_text>\<open>bundle b begin body end\<close> defines a bundle of declarations from the \<open>body\<close> of local theory specifications. It may consist of commands that are
technically equivalent to\<^theory_text>\<open>declare\<close> or \<^theory_text>\<open>declaration\<close>, which also includes \<^theory_text>\<open>notation\<close>, for example. Named fact declarations like ``\<^theory_text>\<open>lemmas a [simp] =
b\<close>'' or ``\<^theory_text>\<open>lemma a [simp]: B \<proof>\<close>'' are also admitted, but the name
bindings are not recorded in the bundle.
\<^descr> \<^theory_text>\<open>open_bundle b\<close> is like \<^theory_text>\<open>bundle b\<close> followed by \<^theory_text>\<open>unbundle b\<close>, so its
declarations are activated immediately, but also named for later re-use.
\<^descr> \<^theory_text>\<open>unbundle \<^vec>b\<close> activates the declarations from the given bundles in
the current localtheorycontext. This is analogous to\<^theory_text>\<open>lemmas\<close>
(\secref{sec:theorems}) with the expanded bundles.
\<^descr> \<^theory_text>\<open>print_bundles\<close> prints the named bundles that are available in the
current context; the ``\<open>!\<close>'' option indicates extra verbosity.
\<^descr> \<^theory_text>\<open>include \<^vec>b\<close> activates the declarations from the given bundles in a proof body (forward mode). This is analogous to\<^theory_text>\<open>note\<close>
(\secref{sec:proof-facts}) with the expanded bundles.
\<^descr> \<^theory_text>\<open>including \<^vec>b\<close> is similar to \<^theory_text>\<open>include\<close>, but works in proof
refinement (backward mode). This is analogous to\<^theory_text>\<open>using\<close>
(\secref{sec:proof-facts}) with the expanded bundles.
\<^descr> \<^theory_text>\<open>includes \<^vec>b\<close> is similar to \<^theory_text>\<open>include\<close>, but applies to a confined specificationcontext: unnamed \<^theory_text>\<open>context\<close>s and long statements of \<^theory_text>\<open>theorem\<close>.
\<^descr> \<^theory_text>\<open>opening \<^vec>b\<close> is similar to \<^theory_text>\<open>includes\<close>, but applies to a named specificationcontext: \<^theory_text>\<open>locale\<close>s, \<^theory_text>\<open>class\<close>es and named \<^theory_text>\<open>context\<close>s. The
effect is confined to the surface context within the specification block
itself and the corresponding \<^theory_text>\<open>begin\<close> / \<^theory_text>\<open>end\<close> block.
\<^descr> Bundle names may be prefixed by the reserved word \<^verbatim>\<open>no\<close> to indicate that
the polarity of certain declaration commands should be inverted, notably:
\<^item> @{command syntax} versus @{command no_syntax} \<^item> @{command translations} versus @{command no_translations} \<^item> @{command notation} versus @{command no_notation} \<^item> @{command type_notation} versus @{command no_type_notation} \<^item> @{command adhoc_overloading} versus @{command no_adhoc_overloading}
This also works recursively for the @{command unbundle} command as declaration inside a @{command bundle} definition: \<^verbatim>\<open>no\<close> means that
both the order and polarity of declarations is reversed (following
algebraic group laws).
Here is an artificial example of bundling various configuration options: \<close>
Term definitions may either happen within the logic (as equational axioms of
a certain form (see also\secref{sec:overloading}), or outside of it as
rewrite system on abstract syntax. The second form is called
``abbreviation''.
\<^descr> \<^theory_text>\<open>definition c where eq\<close> produces an internal definition \<open>c \<equiv> t\<close> according to the specification given as \<open>eq\<close>, which is then turned into a proven fact.
The given proposition may deviate from internal meta-level equality
according to the rewrite rules declared as @{attribute defn} by the
object-logic. This usually covers object-level equality \<open>x = y\<close> and
equivalence \<open>A \<longleftrightarrow> B\<close>. End-users normally need not change the @{attribute
defn} setup.
Definitions may be presented with explicit arguments on the LHS, as well as
additional conditions, e.g.\ \<open>f x y = t\<close> instead of \<open>f \<equiv> \<lambda>x y. t\<close> and \<open>y \<noteq> 0 \<Longrightarrow> g x y = u\<close> instead of an unrestricted \<open>g \<equiv> \<lambda>x y. u\<close>.
\<^descr> \<^theory_text>\<open>print_defn_rules\<close> prints the definitional rewrite rules declared via
@{attribute defn} in the current context.
\<^descr> \<^theory_text>\<open>abbreviation c where eq\<close> introduces a syntactic constant which is
associated with a certain term according to the meta-level equality \<open>eq\<close>.
Abbreviations participate in the usual type-inference process, but are
expanded before the logic ever sees them. Pretty printing of terms involves
higher-order rewriting with rules stemming from reverted abbreviations. This
needs some care to avoid overlapping or looping syntactic replacements!
The optional \<open>mode\<close> specification restricts output to a particular print
mode; using ``\<open>input\<close>'' here achieves the effect of one-way abbreviations.
The mode may also include an ``\<^theory_text>\<open>output\<close>'' qualifier that affects the
concrete syntax declared for abbreviations, cf.\ \<^theory_text>\<open>syntax\<close> in \secref{sec:syn-trans}.
\<^descr> \<^theory_text>\<open>print_abbrevs\<close> prints all constant abbreviations of the current context;
the ``\<open>!\<close>'' option indicates extra verbosity. \<close>
\<^descr> \<^theory_text>\<open>axiomatization c\<^sub>1 \<dots> c\<^sub>m where \<phi>\<^sub>1 \<dots> \<phi>\<^sub>n\<close> introduces several constants
simultaneously andstates axiomatic properties for these. The constants are
marked as being specified once andfor all, which prevents additional
specifications for the same constants later on, but it is always possible to
emit axiomatizations without referring to particular constants. Note that
lack of precise dependency tracking of axiomatizations may disrupt the
well-formedness of an otherwise definitional theory.
Axiomatizationis restricted to a globaltheorycontext: support forlocal theory targets \secref{sec:target} would introduce an extra dimension of
uncertainty what the written specifications really are, and make it
infeasible to argue why they are correct.
Axiomatic specifications are required when declaring a new logical system
within Isabelle/Pure, but in an application environment like Isabelle/HOL
the user normally stays within definitional mechanisms provided by the logic and its libraries. \<close>
Arbitrary operations on the background context may be wrapped-up as generic declaration elements. Since the underlying concept of local theories may be
subject to later re-interpretation, there is an additional dependency on a
morphism that tells the difference of the original declarationcontext wrt.\
the application context encountered later on. A fact declarationis an
important special case: it consists of a theorem which is applied to the contextby means of an attribute.
\<^descr> \<^theory_text>\<open>declaration d\<close> adds the declaration function \<open>d\<close> of ML type \<^ML_type>\<open>Morphism.declaration\<close>, to the current local theory under construction. In later
application contexts, the functionis transformed according to the morphisms
being involved in the interpretation hierarchy.
If the \<^theory_text>\<open>(pervasive)\<close> option is given, the corresponding declaration is
applied to all possible contexts involved, including the global background theory.
\<^descr> \<^theory_text>\<open>syntax_declaration\<close> is similar to \<^theory_text>\<open>declaration\<close>, but is meant to affect
only ``syntactic'' tools by convention (such as notationand type-checking
information).
\<^descr> \<^theory_text>\<open>declare thms\<close> declares theorems to the current local theory context. No theorem binding is involved here, unlike \<^theory_text>\<open>lemmas\<close> (cf.\ \secref{sec:theorems}), so \<^theory_text>\<open>declare\<close> only has the effect of applying
attributes as included in the theoremspecification. \<close>
section \<open>Locales \label{sec:locale}\<close>
text\<open>
A localeis a functor that maps parameters (including implicit type
parameters) and a specificationto a list of declarations. The syntax of
locales is modeled after the Isar proofcontext commands (cf.\ \secref{sec:proof-context}).
Locale hierarchies are supported by maintaining a graph of dependencies
between locale instances in the globaltheory. Dependencies may be
introduced through import (where a localeis defined as sublocale of the
imported instances) or by proving that an existing localeis a sublocale of
one or several locale instances.
A locale may be opened with the purpose of appending to its list of
declarations (cf.\ \secref{sec:target}). When opening a locale declarations from all dependencies are collected and are presented as a localtheory. In
this process, which is called \<^emph>\<open>roundup\<close>, redundant locale instances are
omitted. A localeinstanceis redundant if it is subsumed by an instance
encountered earlier. A more detailed description of this process is
available elsewhere \<^cite>\<open>Ballarin2014\<close>. \<close>
text\<open>
A \<^emph>\<open>locale expression\<close> denotes a context composed of instances of existing
locales. The context consists of the declaration elements from the locale
instances. Redundant locale instances are omitted according to roundup.
A localeinstance consists of a reference to a localeand either positional
or named parameter instantiations optionally followed by rewrites clauses.
Identical instantiations (that is, those
that instantiate a parameter by itself) may be omitted. The notation ``\<open>_\<close>''
enables to omit the instantiationfor a parameter inside a positional instantiation.
Terms in instantiations are from the context the locale expressions is
declared in. Local names may be added to this contextwith the optional \<^theory_text>\<open>for\<close> clause. This is useful for shadowing names bound in outer contexts, andfor declaring syntax. In addition, syntax declarations from one instance
are effective when parsing subsequent instances of the same expression.
Instances have an optional qualifier which applies to names in declarations.
Names include local definitions andtheorem names. If present, the qualifier
itself is either mandatory (default) or non-mandatory (when followed by
``\<^verbatim>\<open>?\<close>''). Non-mandatory means that the qualifier may be omitted on input.
Qualifiers only affect name spaces; they play no role in determining whether
one localeinstance subsumes another.
Rewrite clauses amend instances with equations that act as rewrite rules.
This is particularly useful for changing concepts introduced through
definitions. Rewrite clauses are available only ininterpretation commands
(see \secref{sec:locale-interpretation} below) and must be proved the user. \<close>
\<^descr> \<^theory_text>\<open>locale loc = import opening bundles + body\<close> defines a new locale \<open>loc\<close>
as a context consisting of a certain view of existing locales (\<open>import\<close>) plus some
additional elements (\<open>body\<close>) with declaration \<open>bundles\<close> enriching the context
of the command itself. All \<open>import\<close>, \<open>bundles\<close> and \<open>body\<close> are optional; the
degenerate form \<^theory_text>\<open>locale loc\<close> defines an empty locale, which may still be
useful to collect declarations of facts later on. Type-inference on locale
expressions automatically takes care of the most general typing that the
combined context elements may acquire.
The \<open>import\<close> consists of a locale expression; see \secref{sec:locale-expr}
above. Its \<^theory_text>\<open>for\<close> clause defines the parameters of \<open>import\<close>. These are
parameters of the defined locale. Locale parameters whose instantiationis
omitted automatically extend the (possibly empty) \<^theory_text>\<open>for\<close> clause: they are
inserted at its beginning. This means that these parameters may be referred tofrom within the expression andalsoin the subsequent context elements and provides a notational convenience for the inheritance of parameters in locale declarations.
Declarations from\<open>bundles\<close>, see \secref{sec:bundle}, are effective in the
entire command including a subsequent \<^theory_text>\<open>begin\<close> / \<^theory_text>\<open>end\<close> block, but they do
not contribute to the declarations stored in the locale.
The \<open>body\<close> consists of context elements:
\<^descr> @{element "fixes"}~\<open>x :: \<tau> (mx)\<close> declares a local parameter of type \<open>\<tau>\<close> and mixfix annotation \<open>mx\<close> (both are optional). The special syntax declaration ``\<open>(\<close>@{keyword_ref "structure"}\<open>)\<close>'' means that \<open>x\<close> may be
referenced implicitly in this context.
\<^descr> @{element "constrains"}~\<open>x :: \<tau>\<close> introduces a type constraint \<open>\<tau>\<close> on the local parameter \<open>x\<close>. This element is deprecated. The type constraint
should be introduced in the \<^theory_text>\<open>for\<close> clause or the relevant @{element "fixes"} element.
\<^descr> @{element "assumes"}~\<open>a: \<phi>\<^sub>1 \<dots> \<phi>\<^sub>n\<close> introduces local premises, similar to\<^theory_text>\<open>assume\<close> within a proof (cf.\ \secref{sec:proof-context}).
\<^descr> @{element "defines"}~\<open>a: x \<equiv> t\<close> defines a previously declared parameter.
This is similar to\<^theory_text>\<open>define\<close> within a proof (cf.\ \secref{sec:proof-context}), but @{element "defines"} is restricted to
Pure equalities and the defined variable needs to be declared beforehand
via @{element "fixes"}. The left-hand side of the equation may have
additional arguments, e.g.\ ``@{element "defines"}~\<open>f x\<^sub>1 \<dots> x\<^sub>n \<equiv> t\<close>'',
which need to be free in the context.
\<^descr> @{element "notes"}~\<open>a = b\<^sub>1 \<dots> b\<^sub>n\<close> reconsiders facts within a local context. Most notably, this may include arbitrary declarations in any
attribute specifications included here, e.g.\ a local @{attribute simp}
rule.
Both @{element "assumes"} and @{element "defines"} elements contribute to
the localespecification. When defining an operation derived from the
parameters, \<^theory_text>\<open>definition\<close> (\secref{sec:term-definitions}) is usually more
appropriate.
Note that ``\<^theory_text>\<open>(is p\<^sub>1 \<dots> p\<^sub>n)\<close>'' patterns given in the syntax of @{element "assumes"} and @{element "defines"} above are illegal inlocale definitions. In the long goal format of \secref{sec:goals}, term bindings may be included
as expected, though.
\<^medskip> Locale specifications are ``closed up''by turning the given text into a
predicate definition\<open>loc_axioms\<close> and deriving the original assumptions as locallemmas (modulo local definitions). The predicate statement covers only
the newly specified assumptions, omitting the content of included locale
expressions. The full cumulative view is only provided on export, involving
another predicate \<open>loc\<close> that refers to the complete specification text.
In any case, the predicate arguments are those locale parameters that
actually occur in the respective piece of text. Also these predicates
operate at the meta-level intheory, but the locale packages attempts to
internalize statements according to the object-logic setup (e.g.\ replacing \<open>\<And>\<close> by \<open>\<forall>\<close>, and \<open>\<Longrightarrow>\<close> by \<open>\<longrightarrow>\<close> in HOL; see also \secref{sec:object-logic}).
Separate introduction rules \<open>loc_axioms.intro\<close> and \<open>loc.intro\<close> are provided
as well.
\<^descr> \<^theory_text>\<open>experiment body begin\<close> opens an anonymous locale context with private
naming policy. Specifications in its body are inaccessible from outside.
This is useful to perform experiments, without polluting the name space.
\<^descr> \<^theory_text>\<open>print_locale "locale"\<close> prints the contents of the named locale. The
command omits @{element "notes"} elements by default. Use\<^theory_text>\<open>print_locale!\<close> tohave them included.
\<^descr> \<^theory_text>\<open>print_locales\<close> prints the names of all locales of the current theory;
the ``\<open>!\<close>'' option indicates extra verbosity.
\<^descr> \<^theory_text>\<open>locale_deps\<close> visualizes all locales and their relations as a Hasse
diagram. This includes locales defined as type classes (\secref{sec:class}). \<close>
Locales may be instantiated, and the resulting instantiated declarations
added to the current context. This requires proof (of the instantiated specification) andis called \<^emph>\<open>locale interpretation\<close>. Interpretation is
possible within arbitrary local theories (\<^theory_text>\<open>interpretation\<close>), within proof
bodies (\<^theory_text>\<open>interpret\<close>), into global theories (\<^theory_text>\<open>global_interpretation\<close>) and
into locales (\<^theory_text>\<open>sublocale\<close>).
The core of each interpretation command is a locale expression \<open>expr\<close>; the
command generates proof obligations for the instantiated specifications.
Once these are discharged by the user, instantiated declarations (in
particular, facts) are added to the contextin a post-processing phase, in a
manner specific to each command.
Interpretation commands are aware of interpretations that are already
active: post-processing is achieved through a variant of roundup that takes
interpretations of the current global or localtheory into account. In order to simplify the proof obligations according to existing interpretations use
methods @{method intro_locales} or @{method unfold_locales}.
Rewrites clauses \<^theory_text>\<open>rewrites eqns\<close> occur within expressions. They amend the
morphism through which a localeinstanceis interpreted with rewrite rules, also called rewrite morphisms. This is particularly useful for interpreting
concepts introduced through definitions. The equations must be proved the
user. To enable syntax of the instantiated locale within the equation, while
reading a locale expression, equations of a localeinstance are read in a
temporary contextwhere the instanceis already activated. If activation
fails, typically due to duplicate constant declarations, processing falls backto reading the equation first.
Given definitions \<open>defs\<close> produce corresponding definitions in the local theory's underlying target \<^emph>\and\ amend the morphism with rewrite rules
stemming from the symmetric of those definitions. Hence these need not be
proved explicitly the user. Such rewrite definitions are a even more useful
device for interpreting concepts introduced through definitions, but they
are only supported forinterpretation commands operating in a localtheory
whose implementing target actually supports this. Note that despite
the suggestive \<^theory_text>\<open>and\<close> connective, \<open>defs\<close>
are processed sequentially without mutual recursion.
\<^descr> \<^theory_text>\<open>interpretation expr\<close> interprets \<open>expr\<close> into a local theory
such that its lifetime is limited to the current context block (e.g. a locale or unnamed context). At the closing @{command end} of the block the interpretationand its declarations disappear. Hence facts based on interpretation can be established without creating permanent links to the
interpreted locale instances, as would be the casewith @{command
sublocale}.
When used on the level of a globaltheory, there is no end of a current context block, hence\<^theory_text>\<open>interpretation\<close> behaves identically to \<^theory_text>\<open>global_interpretation\<close> then.
\<^descr> \<^theory_text>\<open>interpret expr\<close> interprets \<open>expr\<close> into a proof context:
the interpretationand its declarations disappear when closing the current proof block. Note that for\<^theory_text>\<open>interpret\<close> the \<open>eqns\<close> should be explicitly
universally quantified.
\<^descr> \<^theory_text>\<open>global_interpretation expr defines defs\<close> interprets \<open>expr\<close>
into a globaltheory.
When adding declarations to locales, interpreted versions of these
declarations are added to the globaltheoryfor all interpretations in the globaltheory as well. That is, interpretations into global theories
dynamically participate in any declarations added to locales.
Free variables in the interpreted expression are allowed. They are turned
into schematic variables in the generated declarations. In order touse a
free variable whose name is already bound in the context --- for example,
because a constant of that name exists --- add it to the \<^theory_text>\<open>for\<close> clause.
When used in a nested target, resulting declarations are propagated
through the whole target stack.
\<^descr> \<^theory_text>\<open>sublocale name \<subseteq> expr defines defs\<close> interprets \<open>expr\<close>
into the locale\<open>name\<close>. A proof that the specification of \<open>name\<close> implies the specification of \<open>expr\<close> is required. As in the localized version of the theorem command, the proofisin the context of \<open>name\<close>. After the proof
obligation has been discharged, the locale hierarchy is changed as if\<open>name\<close>
imported \<open>expr\<close> (hence the name \<^theory_text>\<open>sublocale\<close>). When the context of \<open>name\<close> is
subsequently entered, traversing the locale hierarchy will involve the locale instances of \<open>expr\<close>, and their declarations will be added to the context. This makes \<^theory_text>\<open>sublocale\<close> dynamic: extensions of a locale that is
instantiated in\<open>expr\<close> may take place after the \<^theory_text>\<open>sublocale\<close> declaration and
still become available in the context. Circular \<^theory_text>\<open>sublocale\<close> declarations
are allowed as long as they do not lead to infinite chains.
If interpretations of \<open>name\<close> exist in the current global theory, the command
adds interpretations for\<open>expr\<close> as well, with the same qualifier, although
only for fragments of \<open>expr\<close> that are not interpreted in the theory already.
Rewrites clauses in the expression or rewrite definitions \<open>defs\<close> can help break
infinite chains induced by circular \<^theory_text>\<open>sublocale\<close> declarations.
In a named context block the \<^theory_text>\<open>sublocale\<close> command may also be used, but the locale argument must be omitted. The command then refers to the locale (or class) target of the context block.
\<^descr> \<^theory_text>\<open>print_interps name\<close> lists all interpretations of locale \<open>name\<close> in the
current theory or proofcontext, including those due to a combination of an \<^theory_text>\<open>interpretation\<close> or \<^theory_text>\<open>interpret\<close> and one or several \<^theory_text>\<open>sublocale\<close>
declarations.
\<^descr> @{method intro_locales} and @{method unfold_locales} repeatedly expand all
introduction rules of locale predicates of the theory. While @{method
intro_locales} only applies the \<open>loc.intro\<close> introduction rules and therefore
does not descend to assumptions, @{method unfold_locales} is more aggressive and applies \<open>loc_axioms.intro\<close> as well. Both methods are aware of locale
specifications entailed by the context, both from target statements, and from interpretations (see below). New goals that are entailed by the current context are discharged automatically.
While @{method unfold_locales} is part of the default method for\<^theory_text>\<open>proof\<close> and often invoked ``behind the scenes'', @{method intro_locales} helps
understand which proof obligations originated from which locale instances.
The latter method is useful while developing proofs but rare in finished
developments.
\<^descr> @{attribute trace_locales}, when set to \<open>true\<close>, prints the locale
instances activated during roundup. Use this when locale commands yield
obscure errors or for understanding local theories created by complex locale
hierarchies.
\begin{warn} If a globaltheory inherits declarations (body elements) for a localefrom
one parent and an interpretation of that localefrom another parent, the interpretation will not be applied to the declarations. \end{warn}
\begin{warn}
Since attributes are applied to interpreted theorems, interpretation may
modify the context of common proof tools, e.g.\ the Simplifier or
Classical Reasoner. As the behaviour of such tools is\<^emph>\<open>not\<close> stable under interpretationmorphisms, manual declarations might haveto be added to
the target context of the interpretationto revert such declarations. \end{warn}
\begin{warn}
An interpretationin a localtheory or proofcontext may subsume previous
interpretations. This happens if the same specification fragment is
interpreted twice and the instantiation of the second interpretationis
more general than the interpretation of the first. The locale package does
not attempt to remove subsumed interpretations. \end{warn}
\begin{warn}
While \<^theory_text>\<open>interpretation (in c) \<dots>\<close> is admissible, it is not useful since
its result is discarded immediately. \end{warn} \<close>
A classis a particular localewith\<^emph>\<open>exactly one\<close> type variable \<open>\<alpha>\<close>. Beyond
the underlying locale, a corresponding type classis established which is
interpreted logically as axiomatic type class\<^cite>\<open>"Wenzel:1997:TPHOL"\<close>
whose logical content are the assumptions of the locale. Thus, classes
provide the full generality of locales combined with the commodity of type classes (notably type-inference). See \<^cite>\<open>"isabelle-classes"\<close> for a short
tutorial.
\<^descr> \<^theory_text>\<open>class c = superclasses bundles + body\<close> defines a new class \<open>c\<close>, inheriting from \<open>superclasses\<close>. This introduces a locale \<open>c\<close> with import of all locales \<open>superclasses\<close>.
Any @{element "fixes"} in\<open>body\<close> are lifted to the global theory level
(\<^emph>\<open>class operations\<close> \<open>f\<^sub>1, \<dots>, f\<^sub>n\<close> of class \<open>c\<close>), mapping the local type
parameter \<open>\<alpha>\<close> to a schematic type variable \<open>?\<alpha> :: c\<close>.
Likewise, @{element "assumes"} in\<open>body\<close> are also lifted, mapping each local
parameter \<open>f :: \<tau>[\<alpha>]\<close> to its corresponding global constant \<open>f :: \<tau>[?\<alpha> ::
c]\<close>. The corresponding introduction rule is provided as \<open>c_class_axioms.intro\<close>. This rule should be rarely needed directly --- the
@{method intro_classes} method takes care of the details of class membership
proofs.
Optionally given \<open>bundles\<close> take effect in the surface context within the \<open>body\<close> and the potentially following \<^theory_text>\<open>begin\<close> / \<^theory_text>\<open>end\<close> block.
\<^descr> \<^theory_text>\<open>instantiation t :: (s\<^sub>1, \<dots>, s\<^sub>n)s begin\<close> opens a target (cf.\ \secref{sec:target}) which allows to specify class operations \<open>f\<^sub>1, \<dots>, f\<^sub>n\<close>
corresponding to sort \<open>s\<close> at the particular type instance \<open>(\<alpha>\<^sub>1 :: s\<^sub>1, \<dots>, \<alpha>\<^sub>n :: s\<^sub>n) t\<close>. A plain \<^theory_text>\<open>instance\<close> command in the target body poses a goal
stating these type arities. The target is concluded by an @{command_ref
(local) "end"} command.
Note that a list of simultaneous type constructors may be given; this
corresponds nicely to mutually recursive type definitions, e.g.\ in
Isabelle/HOL.
\<^descr> \<^theory_text>\<open>instance\<close> in an instantiation target body sets up a goal stating the
type arities claimed at the opening \<^theory_text>\<open>instantiation\<close>. The proof would
usually proceed by @{method intro_classes}, andthen establish the
characteristic theorems of the type classes involved. After finishing the proof, the background theory will be augmented by the proven type arities.
On the theory level, \<^theory_text>\<open>instance t :: (s\<^sub>1, \<dots>, s\<^sub>n)s\<close> provides a convenient
way to instantiate a type classwith no need to specify operations: one can
continue with the instantiationproof immediately.
\<^descr> \<^theory_text>\<open>subclass c\<close> in a class context for class \<open>d\<close> sets up a goal stating that class\<open>c\<close> is logically contained in class \<open>d\<close>. After finishing the proof, class\<open>d\<close> is proven to be subclass \<open>c\<close> and the locale \<open>c\<close> is interpreted
into \<open>d\<close> simultaneously.
A weakened form of this is available through a further variant of @{command instance}: \<^theory_text>\<open>instance c\<^sub>1 \<subseteq> c\<^sub>2\<close> opens a proof that class \<open>c\<^sub>2\<close> implies \<open>c\<^sub>1\<close> without reference to the underlying locales; this is useful if the
properties to prove the logical connection are not sufficient on the locale
level but on the theory level.
\<^descr> \<^theory_text>\<open>print_classes\<close> prints all classes in the current theory.
\<^descr> \<^theory_text>\<open>class_deps\<close> visualizes classes and their subclass relations as a
directed acyclic graph. By default, all classesfrom the current theory context are show. This may be restricted by optional bounds as follows: \<^theory_text>\<open>class_deps upper\<close> or \<^theory_text>\<open>class_deps upper lower\<close>. A class is visualized, iff
it is a subclass of some sort from\<open>upper\<close> and a superclass of some sort from\<open>lower\<close>.
\<^descr> @{method intro_classes} repeatedly expands all class introduction rules of
this theory. Note that this method usually needs not be named explicitly, as
it is already included in the default proof step (e.g.\ of \<^theory_text>\<open>proof\<close>). In
particular, instantiation of trivial (syntactic) classes may be performed by
a single ``\<^theory_text>\<open>..\<close>'' proof step. \<close>
subsection \<open>The class target\<close>
text\<open>
%FIXME check
A named context may refer to a locale (cf.\ \secref{sec:target}). If this localeisalso a class\<open>c\<close>, apart from the common locale target behaviour
the following happens.
\<^item> Local constant declarations \<open>g[\<alpha>]\<close> referring to the local type parameter \<open>\<alpha>\<close> and local parameters \<open>f[\<alpha>]\<close> are accompanied by theory-level constants \<open>g[?\<alpha> :: c]\<close> referring to theory-level class operations \<open>f[?\<alpha> :: c]\<close>.
\<^item> Local theorem bindings are lifted as are assumptions.
\<^item> Local syntax refers to local operations \<open>g[\<alpha>]\<close> and global operations \<open>g[?\<alpha> :: c]\<close> uniformly. Type inference resolves ambiguities. In rare
cases, manual type annotations are needed. \<close>
subsection \<open>Co-regularity of type classes and arities\<close>
text\<open>
The class relation together with the collection of type-constructor arities
must obey the principle of \<^emph>\<open>co-regularity\<close> as defined below.
\<^medskip> For the subsequent formulation of co-regularity we assume that the class
relation is closed by transitivity and reflexivity. Moreover the collection
of arities\<open>t :: (\<^vec>s)c\<close> is completed such that \<open>t :: (\<^vec>s)c\<close> and \<open>c \<subseteq> c'\<close> implies \<open>t :: (\<^vec>s)c'\<close> for all such declarations.
Treating sorts as finite sets of classes (meaning the intersection), the class relation \<open>c\<^sub>1 \<subseteq> c\<^sub>2\<close> is extended to sorts as follows: \[ \<open>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\<close> \]
This relation on sorts is further extended to tuples of sorts (of the same
length) in the component-wise way.
\<^medskip>
Co-regularity of the class relation together with the arities relation
means: \[ \<open>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\<close> \] for all such arities. In other words, whenever the result classes of some
type-constructor arities are related, then the argument sorts need to be
related in the same way.
\<^medskip>
Co-regularity is a very fundamental property of the order-sorted algebra of types. For example, it entails principal typesand most general unifiers,
e.g.\ see \<^cite>\<open>"nipkow-prehofer"\<close>. \<close>
text\<open>
Definitions essentially express abbreviations within the logic. The simplest
form of a definitionis\<open>c :: \<sigma> \<equiv> t\<close>, where \<open>c\<close> is a new constant and \<open>t\<close> is
a closed term that does not mention \<open>c\<close>. Moreover, so-called \<^emph>\<open>hidden
polymorphism\<close> is excluded: all type variables in \<open>t\<close> need to occur in its
type \<open>\<sigma>\<close>.
\<^emph>\<open>Overloading\<close> means that a constant being declared as \<open>c :: \<alpha> decl\<close> may be
defined separately on type instances \<open>c :: (\<beta>\<^sub>1, \<dots>, \<beta>\<^sub>n)\<kappa> decl\<close> for each
type constructor \<open>\<kappa>\<close>. At most occasions overloading will be used in a
Haskell-like fashion together with type classesby means of \<^theory_text>\<open>instantiation\<close>
(see \secref{sec:class}). Sometimes low-level overloading is desirable; this is supported by\<^theory_text>\<open>consts\<close> and \<^theory_text>\<open>overloading\<close> explained below.
The right-hand side of overloaded definitions may mention overloaded
constants recursively at type instances corresponding to the immediate
argument types\<open>\<beta>\<^sub>1, \<dots>, \<beta>\<^sub>n\<close>. Incomplete specification patterns impose global constraints on all occurrences. E.g.\ \<open>d :: \<alpha> \<times> \<alpha>\<close> on the left-hand
side means that all corresponding occurrences on some right-hand side need to be an instance of this, and general \<open>d :: \<alpha> \<times> \<beta>\<close> will be disallowed. Full
details are given by Kun\v{c}ar \<^cite>\<open>"Kuncar:2015"\<close>.
\<^medskip>
The \<^theory_text>\<open>consts\<close> command and the \<^theory_text>\<open>overloading\<close> target provide a convenient
interface for end-users. Regular specification elements such as @{command definition}, @{command inductive}, @{command function} may be used in the
body. It isalso possible touse\<^theory_text>\<open>consts c :: \<sigma>\<close> with later \<^theory_text>\<open>overloading c \<equiv> c :: \<sigma>\<close> to keep the declaration and definition of a constant separate.
\<^descr> \<^theory_text>\<open>consts c :: \<sigma>\<close> declares constant \<open>c\<close> to have any instance of type scheme \<open>\<sigma>\<close>. The optional mixfix annotations may attach concrete syntax to the
constants declared.
\<^descr> \<^theory_text>\<open>overloading x\<^sub>1 \<equiv> c\<^sub>1 :: \<tau>\<^sub>1 \<dots> x\<^sub>n \<equiv> c\<^sub>n :: \<tau>\<^sub>n begin \<dots> end\<close> defines
a theory target (cf.\ \secref{sec:target}) which allows to specify already
declared constants via definitions in the body. These are identified by an
explicitly given mapping from variable names \<open>x\<^sub>i\<close> to constants \<open>c\<^sub>i\<close> at
particular type instances. The definitions themselves are established using
common specification tools, using the names \<open>x\<^sub>i\<close> as reference to the
corresponding constants.
Option \<^theory_text>\<open>(unchecked)\<close> disables global dependency checks for the
corresponding definition, which is occasionally useful for exotic overloading; this is a form of axiomatic specification. It is at the
discretion of the user to avoid malformed theory specifications! \<close>
subsubsection \<open>Example\<close>
consts Length :: "'a \ nat"
overloading
Length\<^sub>0 \<equiv> "Length :: unit \<Rightarrow> nat"
Length\<^sub>1 \<equiv> "Length :: 'a \<times> unit \<Rightarrow> nat"
Length\<^sub>2 \<equiv> "Length :: 'a \<times> 'b \<times> unit \<Rightarrow> nat"
Length\<^sub>3 \<equiv> "Length :: 'a \<times> 'b \<times> 'c \<times> unit \<Rightarrow> nat" begin
fun Length\<^sub>0 :: "unit \<Rightarrow> nat" where "Length\<^sub>0 () = 0" fun Length\<^sub>1 :: "'a \<times> unit \<Rightarrow> nat" where "Length\<^sub>1 (a, ()) = 1" fun Length\<^sub>2 :: "'a \<times> 'b \<times> unit \<Rightarrow> nat" where "Length\<^sub>2 (a, b, ()) = 2" fun Length\<^sub>3 :: "'a \<times> 'b \<times> 'c \<times> unit \<Rightarrow> nat" where "Length\<^sub>3 (a, b, c, ()) = 3"
\<^medskip>
Adhoc overloading allows to overload a constant depending on its type.
Typically this involves the introduction of an uninterpreted constant (used for input andoutput) and the addition of some variants (used internally). For examples see \<^file>\<open>~~/src/HOL/Examples/Adhoc_Overloading.thy\<close> and \<^file>\<open>~~/src/HOL/Library/Monad_Syntax.thy\<close>.
\<^descr> @{command "adhoc_overloading"}~\<open>c \<rightleftharpoons> v\<^sub>1 ... v\<^sub>n\<close> associates variants with an
existing constant.
\<^descr> @{command "no_adhoc_overloading"} is similar to @{command "adhoc_overloading"}, but removes the specified variants from the present context.
\<^descr> @{attribute "show_variants"} controls printing of variants of overloaded
constants. If enabled, the internally used variants are printed instead of
their respective overloaded constants. This is occasionally useful to check
whether the system agrees with a user's expectations about derived variants. \<close>
section \<open>Incorporating ML code \label{sec:ML}\<close>
\<^descr> \<^theory_text>\<open>SML_file name\<close> reads and evaluates the given Standard ML file. Top-level
--> --------------------
--> maximum size reached
--> --------------------
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.16Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.