doc-src/Logics/defining.tex
changeset 135 493308514ea8
parent 108 e332c5bf9e1f
child 142 6dfae8cddec7
equal deleted inserted replaced
134:595fda4879b6 135:493308514ea8
   142      &$|$& {\tt!!} $idts$ {\tt.} $prop$ & (0) \\\\
   142      &$|$& {\tt!!} $idts$ {\tt.} $prop$ & (0) \\\\
   143 $logic$ &=& $prop$ ~~$|$~~ $fun$ \\\\
   143 $logic$ &=& $prop$ ~~$|$~~ $fun$ \\\\
   144 $aprop$ &=& $id$ ~~$|$~~ $var$
   144 $aprop$ &=& $id$ ~~$|$~~ $var$
   145     ~~$|$~~ $fun@{max_pri}$ {\tt(} $logic$ {\tt,} \dots {\tt,} $logic$ {\tt)} \\\\
   145     ~~$|$~~ $fun@{max_pri}$ {\tt(} $logic$ {\tt,} \dots {\tt,} $logic$ {\tt)} \\\\
   146 $fun$ &=& $id$ ~~$|$~~ $var$ ~~$|$~~ {\tt(} $fun$ {\tt)} \\
   146 $fun$ &=& $id$ ~~$|$~~ $var$ ~~$|$~~ {\tt(} $fun$ {\tt)} \\
       
   147     &$|$& $fun@{max_pri}$ {\tt(} $logic$ {\tt,} \dots {\tt,} $logic$ {\tt)} \\
       
   148     &$|$& $fun@{max_pri}$ {\tt::} $type$ \\
   147     &$|$& \ttindex{\%} $idts$ {\tt.} $logic$ & (0) \\\\
   149     &$|$& \ttindex{\%} $idts$ {\tt.} $logic$ & (0) \\\\
   148 $idts$ &=& $idt$ ~~$|$~~ $idt@1$ $idts$ \\\\
   150 $idts$ &=& $idt$ ~~$|$~~ $idt@1$ $idts$ \\\\
   149 $idt$ &=& $id$ ~~$|$~~ {\tt(} $idt$ {\tt)} \\
   151 $idt$ &=& $id$ ~~$|$~~ {\tt(} $idt$ {\tt)} \\
   150     &$|$& $id$ \ttindex{::} $type$ & (0) \\\\
   152     &$|$& $id$ \ttindex{::} $type$ & (0) \\\\
   151 $type$ &=& $tfree$ ~~$|$~~ $tvar$ \\
   153 $type$ &=& $tfree$ ~~$|$~~ $tvar$ ~~$|$~~ $tfree$ {\tt::} $sort$
   152      &$|$& $tfree$ {\tt::} $sort$ ~~$|$~~ $tvar$ {\tt::} $sort$ \\
   154   ~~$|$~~ $tvar$ {\tt::} $sort$ \\
   153      &$|$& $id$ ~~$|$~~ $type@{max_pri}$ $id$
   155      &$|$& $id$ ~~$|$~~ $type@{max_pri}$ $id$
   154                 ~~$|$~~ {\tt(} $type$ {\tt,} \dots {\tt,} $type$ {\tt)} $id$ \\
   156                 ~~$|$~~ {\tt(} $type$ {\tt,} \dots {\tt,} $type$ {\tt)} $id$ \\
   155      &$|$& $type@1$ \ttindex{=>} $type$ & (0) \\
   157      &$|$& $type@1$ \ttindex{=>} $type$ & (0) \\
   156      &$|$& {\tt[}  $type$ {\tt,} \dots {\tt,} $type$ {\tt]} {\tt=>} $type$&(0)\\
   158      &$|$& {\tt[}  $type$ {\tt,} \dots {\tt,} $type$ {\tt]} {\tt=>} $type$&(0)\\
   157      &$|$& {\tt(} $type$ {\tt)} \\\\
   159      &$|$& {\tt(} $type$ {\tt)} \\\\
   178 
   180 
   179   \item[$fun$] Terms potentially of function type.
   181   \item[$fun$] Terms potentially of function type.
   180 
   182 
   181   \item[$type$] Meta-types.
   183   \item[$type$] Meta-types.
   182 
   184 
   183   \item[$idts$] a list of identifiers, possibly constrained by types. Note
   185   \item[$idts$] A list of identifiers, possibly constrained by types. Note
   184     that \verb|x :: nat y| is parsed as \verb|x :: (nat y)|, i.e.\ {\tt y}
   186     that \verb|x :: nat y| is parsed as \verb|x :: (nat y)|, i.e.\ {\tt y}
   185     would be treated like a type constructor applied to {\tt nat}.
   187     would be treated like a type constructor applied to {\tt nat}.
   186 \end{description}
   188 \end{description}
   187 
   189 
   188 
   190 
   190 
   192 
   191 Isabelle is concerned with mathematical languages which have a certain
   193 Isabelle is concerned with mathematical languages which have a certain
   192 minimal vocabulary: identifiers, variables, parentheses, and the lambda
   194 minimal vocabulary: identifiers, variables, parentheses, and the lambda
   193 calculus. Logical types, i.e.\ those of class $logic$, are automatically
   195 calculus. Logical types, i.e.\ those of class $logic$, are automatically
   194 equipped with this basic syntax. More precisely, for any type constructor
   196 equipped with this basic syntax. More precisely, for any type constructor
   195 $ty$ with arity $(\dots)c$, where $c$ is a subclass of $logic$, the following
   197 $ty$ with arity $(\vec{s})c$, where $c$ is a subclass of $logic$, the
   196 productions are added:
   198 following productions are added:
   197 \begin{center}
   199 \begin{center}
   198 \begin{tabular}{rclc}
   200 \begin{tabular}{rclc}
   199 $ty$ &=& $id$ ~~$|$~~ $var$ ~~$|$~~ {\tt(} $ty$ {\tt)} \\
   201 $ty$ &=& $id$ ~~$|$~~ $var$ ~~$|$~~ {\tt(} $ty$ {\tt)} \\
   200   &$|$& $fun@{max_pri}$ {\tt(} $logic$ {\tt,} \dots {\tt,} $logic$ {\tt)}\\
   202   &$|$& $fun@{max_pri}$ {\tt(} $logic$ {\tt,} \dots {\tt,} $logic$ {\tt)}\\
   201   &$|$& $ty@{max_pri}$ {\tt::} $type$\\\\
   203   &$|$& $ty@{max_pri}$ {\tt::} $type$\\\\
   282 \end{ttbox}
   284 \end{ttbox}
   283 {\tt Syntax.print_syntax} shows virtually all information contained in a
   285 {\tt Syntax.print_syntax} shows virtually all information contained in a
   284 syntax, therefore being quite verbose. Its output is divided into labeled
   286 syntax, therefore being quite verbose. Its output is divided into labeled
   285 sections. The syntax proper is represented by {\tt lexicon}, {\tt roots} and
   287 sections. The syntax proper is represented by {\tt lexicon}, {\tt roots} and
   286 {\tt prods}. The rest refers to the manifold facilities to apply syntactic
   288 {\tt prods}. The rest refers to the manifold facilities to apply syntactic
   287 translations (macro expansion etc.). See \S\ref{sec:macros} and
   289 translations (macro expansion etc.).
   288 \S\ref{sec:tr_funs} for more details on translations.
       
   289 
   290 
   290 To simplify coping with the verbosity of {\tt Syntax.print_syntax}, there are
   291 To simplify coping with the verbosity of {\tt Syntax.print_syntax}, there are
   291 \ttindex{Syntax.print_gram} to print the syntax proper only and
   292 \ttindex{Syntax.print_gram} to print the syntax proper only and
   292 \ttindex{Syntax.print_trans} to print the translation related information
   293 \ttindex{Syntax.print_trans} to print the translation related information
   293 only.
   294 only.
   331 
   332 
   332       \item $root_of_type((\tau@1, \dots, \tau@n)ty) = ty$.
   333       \item $root_of_type((\tau@1, \dots, \tau@n)ty) = ty$.
   333 
   334 
   334       \item $root_of_type(\alpha) = \mtt{logic}$.
   335       \item $root_of_type(\alpha) = \mtt{logic}$.
   335     \end{itemize}
   336     \end{itemize}
   336     Thereby $\tau@1, \dots, \tau@n$ are types, $ty$ a type infix or ordinary
   337     Thereby $\tau@1, \dots, \tau@n$ are types, $ty$ an infix or ordinary type
   337     type constructor and $\alpha$ a type variable or unknown. Note that only
   338     constructor and $\alpha$ a type variable or unknown. Note that only the
   338     the outermost type constructor is taken into account.
   339     outermost type constructor is taken into account.
   339 
   340 
   340   \item[\ttindex{prods}]
   341   \item[\ttindex{prods}]
   341     The list of productions describing the precedence grammar. Nonterminals
   342     The list of productions describing the precedence grammar. Nonterminals
   342     $A@n$ are rendered in ASCII as {\tt $A$[$n$]}, literal tokens are quoted.
   343     $A@n$ are rendered in {\sc ascii} as {\tt $A$[$n$]}, literal tokens are
   343     Note that some productions have strings attached after an {\tt =>}. These
   344     quoted. Some productions have strings attached after an {\tt =>}. These
   344     strings later become the heads of parse trees, but they also play a vital
   345     strings later become the heads of parse trees, but they also play a vital
   345     role when terms are printed (see \S\ref{sec:asts}).
   346     role when terms are printed (see \S\ref{sec:asts}).
   346 
   347 
   347     Productions which do not have string attached and thus do not create a
   348     Productions which do not have string attached and thus do not create a
   348     new parse tree node are called {\bf copy productions}\indexbold{copy
   349     new parse tree node are called {\bf copy productions}\indexbold{copy
   437   {\rm is written as} ("_constrain" ("_abs" x t) ("fun" 'a 'b))
   438   {\rm is written as} ("_constrain" ("_abs" x t) ("fun" 'a 'b))
   438 \end{ttbox}
   439 \end{ttbox}
   439 
   440 
   440 Note that {\tt ()} and {\tt (f)} are both illegal.
   441 Note that {\tt ()} and {\tt (f)} are both illegal.
   441 
   442 
   442 The resemblance of LISP's S-expressions is intentional, but notice the two
   443 The resemblance of Lisp's S-expressions is intentional, but notice the two
   443 kinds of atomic symbols: $\Constant x$ and $\Variable x$. This distinction
   444 kinds of atomic symbols: $\Constant x$ and $\Variable x$. This distinction
   444 has some more obscure reasons and you can ignore it about half of the time.
   445 has some more obscure reasons and you can ignore it about half of the time.
   445 You should not take the names ``{\tt Constant}'' and ``{\tt Variable}'' too
   446 You should not take the names ``{\tt Constant}'' and ``{\tt Variable}'' too
   446 literally. In the later translation to terms, $\Variable x$ may become a
   447 literally. In the later translation to terms, $\Variable x$ may become a
   447 constant, free or bound variable, even a type constructor or class name; the
   448 constant, free or bound variable, even a type constructor or class name; the
   454 
   455 
   455 Forms like {\tt (("_abs" x $t$) $u$)} are perfectly legal, but asts are not
   456 Forms like {\tt (("_abs" x $t$) $u$)} are perfectly legal, but asts are not
   456 higher-order: the {\tt "_abs"} does not yet bind the {\tt x} in any way,
   457 higher-order: the {\tt "_abs"} does not yet bind the {\tt x} in any way,
   457 though later at the term level, {\tt ("_abs" x $t$)} will become an {\tt Abs}
   458 though later at the term level, {\tt ("_abs" x $t$)} will become an {\tt Abs}
   458 node and occurrences of {\tt x} in $t$ will be replaced by {\tt Bound}s. Even
   459 node and occurrences of {\tt x} in $t$ will be replaced by {\tt Bound}s. Even
   459 if non constant heads of applications may seem unusual, asts should be
   460 if non-constant heads of applications may seem unusual, asts should be
   460 regarded as first-order. Purists may think of ${\tt (} f~x@1~\ldots~x@n{\tt
   461 regarded as first-order. Purists may think of ${\tt (} f~x@1~\ldots~x@n{\tt
   461 )}$ as a first-order application of some invisible $(n+1)$-ary constant.
   462 )}$ as a first-order application of some invisible $(n+1)$-ary constant.
   462 
   463 
   463 
   464 
   464 \subsection{Parse trees to asts}
   465 \subsection{Parse trees to asts}
   508 
   509 
   509 Some of these examples illustrate why further translations are desirable in
   510 Some of these examples illustrate why further translations are desirable in
   510 order to provide some nice standard form of asts before macro expansion takes
   511 order to provide some nice standard form of asts before macro expansion takes
   511 place. Hence the Pure syntax provides predefined parse ast
   512 place. Hence the Pure syntax provides predefined parse ast
   512 translations\index{parse ast translation!of Pure} for ordinary applications,
   513 translations\index{parse ast translation!of Pure} for ordinary applications,
   513 type applications, nested abstraction, meta implication and function types.
   514 type applications, nested abstractions, meta implications and function types.
   514 Their net effect on some representative input strings is shown in
   515 Their net effect on some representative input strings is shown in
   515 Figure~\ref{fig:parse_ast_tr}.
   516 Figure~\ref{fig:parse_ast_tr}.
   516 
   517 
   517 \begin{figure}[htb]
   518 \begin{figure}[htb]
   518 \begin{center}
   519 \begin{center}
   607   \item $constrain(x, \tau) = \Appl{\Constant \mtt{"_constrain"}, x, ty}$,
   608   \item $constrain(x, \tau) = \Appl{\Constant \mtt{"_constrain"}, x, ty}$,
   608     where $ty$ is the ast encoding of $\tau$. That is: type constructors as
   609     where $ty$ is the ast encoding of $\tau$. That is: type constructors as
   609     {\tt Constant}s, type variables as {\tt Variable}s and type applications
   610     {\tt Constant}s, type variables as {\tt Variable}s and type applications
   610     as {\tt Appl}s with the head type constructor as first element.
   611     as {\tt Appl}s with the head type constructor as first element.
   611     Additionally, if \ttindex{show_sorts} is set to {\tt true}, some type
   612     Additionally, if \ttindex{show_sorts} is set to {\tt true}, some type
   612     variables are decorated with an ast encoding their sort.
   613     variables are decorated with an ast encoding of their sort.
   613 \end{itemize}
   614 \end{itemize}
   614 
   615 
   615 \medskip
   616 \medskip
   616 After an ast has been normalized wrt.\ the print macros, it is transformed
   617 After an ast has been normalized wrt.\ the print macros, it is transformed
   617 into the final output string. The built-in {\bf print ast
   618 into the final output string. The built-in {\bf print ast
   633 non-constant head or without a corresponding production are printed as
   634 non-constant head or without a corresponding production are printed as
   634 $f(x@1, \ldots, x@l)$ or $(\alpha@1, \ldots, \alpha@l) ty$. A single
   635 $f(x@1, \ldots, x@l)$ or $(\alpha@1, \ldots, \alpha@l) ty$. A single
   635 $\Variable x$ is simply printed as $x$.
   636 $\Variable x$ is simply printed as $x$.
   636 
   637 
   637 Note that the system does {\em not\/} insert blanks automatically. They
   638 Note that the system does {\em not\/} insert blanks automatically. They
   638 should be part of the mixfix declaration (which provide the user interface
   639 should be part of the mixfix declaration the production has been derived
   639 for defining syntax) if they are required to separate tokens. Mixfix
   640 from, if they are required to separate tokens. Mixfix declarations may also
   640 declarations may also contain pretty printing annotations. See
   641 contain pretty printing annotations.
   641 \S\ref{sec:mixfix} for details.
       
   642 
   642 
   643 
   643 
   644 
   644 
   645 \section{Mixfix declarations} \label{sec:mixfix}
   645 \section{Mixfix declarations} \label{sec:mixfix}
   646 
   646 
   657 translation into the abstract syntax and a pretty printing scheme, all in
   657 translation into the abstract syntax and a pretty printing scheme, all in
   658 one. Isabelle syntax definitions are inspired by \OBJ's~\cite{OBJ} {\em
   658 one. Isabelle syntax definitions are inspired by \OBJ's~\cite{OBJ} {\em
   659 mixfix\/} syntax. Each mixfix annotation defines a precedence grammar
   659 mixfix\/} syntax. Each mixfix annotation defines a precedence grammar
   660 production and optionally associates a constant with it.
   660 production and optionally associates a constant with it.
   661 
   661 
   662 There is a general form of mixfix annotation exhibiting the full power of
   662 There is a general form of mixfix annotation and some shortcuts for common
   663 extending a theory's syntax, and some shortcuts for common cases like infix
   663 cases like infix operators.
   664 operators.
       
   665 
   664 
   666 The general \bfindex{mixfix declaration} as it may occur within the {\tt
   665 The general \bfindex{mixfix declaration} as it may occur within the {\tt
   667 consts} section\index{consts section@{\tt consts} section} of a {\tt .thy}
   666 consts} section\index{consts section@{\tt consts} section} of a {\tt .thy}
   668 file, specifies a constant declaration and a grammar production at the same
   667 file, specifies a constant declaration and a grammar production at the same
   669 time. It has the form {\tt $c$ ::\ "$\tau$" ("$sy$" $ps$ $p$)} and is
   668 time. It has the form {\tt $c$ ::\ "$\tau$" ("$sy$" $ps$ $p$)} and is
   728   "-" :: "exp => exp"         ("- _" [3] 3)
   727   "-" :: "exp => exp"         ("- _" [3] 3)
   729 end
   728 end
   730 \end{ttbox}
   729 \end{ttbox}
   731 Note that the {\tt arities} declaration causes {\tt exp} to be added to the
   730 Note that the {\tt arities} declaration causes {\tt exp} to be added to the
   732 syntax' roots. If you put the above text into a file {\tt exp.thy} and load
   731 syntax' roots. If you put the above text into a file {\tt exp.thy} and load
   733 it via {\tt use_thy "exp"}, you can run some tests:
   732 it via {\tt use_thy "EXP"}, you can run some tests:
   734 \begin{ttbox}
   733 \begin{ttbox}
   735 val read_exp = Syntax.test_read (syn_of EXP.thy) "exp";
   734 val read_exp = Syntax.test_read (syn_of EXP.thy) "exp";
   736 read_exp "0 * 0 * 0 * 0 + 0 + 0 + 0";
   735 read_exp "0 * 0 * 0 * 0 + 0 + 0 + 0";
   737 {\out tokens: "0" "*" "0" "*" "0" "*" "0" "+" "0" "+" "0" "+" "0"}
   736 {\out tokens: "0" "*" "0" "*" "0" "*" "0" "+" "0" "+" "0" "+" "0"}
   738 {\out raw: ("+" ("+" ("+" ("*" "0" ("*" "0" ("*" "0" "0"))) "0") "0") "0")}
   737 {\out raw: ("+" ("+" ("+" ("*" "0" ("*" "0" ("*" "0" "0"))) "0") "0") "0")}
   814 "op \(c\)" ::\ "\(\tau\)"   ("(_ \(c\)/ _)" [\(p + 1\), \(p\)] \(p\))
   813 "op \(c\)" ::\ "\(\tau\)"   ("(_ \(c\)/ _)" [\(p + 1\), \(p\)] \(p\))
   815 \end{ttbox}
   814 \end{ttbox}
   816 
   815 
   817 Thus, prefixing infixes with \ttindex{op} makes them behave like ordinary
   816 Thus, prefixing infixes with \ttindex{op} makes them behave like ordinary
   818 function symbols. Special characters occurring in $c$ have to be escaped as
   817 function symbols. Special characters occurring in $c$ have to be escaped as
   819 in delimiters. Also note that the expanded forms above are illegal at the
   818 in delimiters. Also note that the expanded forms above would be actually
   820 user level because of duplicate declarations of constants.
   819 illegal at the user level because of duplicate declarations of constants.
   821 
   820 
   822 
   821 
   823 \subsection{Binders}
   822 \subsection{Binders}
   824 
   823 
   825 A \bfindex{binder} is a variable-binding construct, such as a
   824 A \bfindex{binder} is a variable-binding construct, such as a
   922 with {\tt\at} to stress their pure syntactic purpose; they should never occur
   921 with {\tt\at} to stress their pure syntactic purpose; they should never occur
   923 within the final well-typed terms. Another consequence is that the user
   922 within the final well-typed terms. Another consequence is that the user
   924 cannot refer to such names directly, since they are not legal identifiers.
   923 cannot refer to such names directly, since they are not legal identifiers.
   925 
   924 
   926 The translations cause the replacement of external forms by internal forms
   925 The translations cause the replacement of external forms by internal forms
   927 after parsing and before printing of terms.
   926 after parsing, and vice versa before printing of terms.
   928 \end{example}
   927 \end{example}
   929 
   928 
   930 This is only a very simple but common instance of a more powerful mechanism.
   929 This is only a very simple but common instance of a more powerful mechanism.
   931 As a specification of what is to be translated, it should be comprehensible
   930 As a specification of what is to be translated, it should be comprehensible
   932 without further explanations. But there are also some snags and other
   931 without further explanations. But there are also some snags and other
   948 $string$}.
   947 $string$}.
   949 \end{center}
   948 \end{center}
   950 
   949 
   951 This specifies a \rmindex{parse rule} ({\tt =>}) a \rmindex{print rule} ({\tt
   950 This specifies a \rmindex{parse rule} ({\tt =>}) a \rmindex{print rule} ({\tt
   952 <=}) or both ({\tt ==}). The two $string$s preceded by optional parenthesized
   951 <=}) or both ({\tt ==}). The two $string$s preceded by optional parenthesized
   953 $root$s denote the left-hand and right-hand side of the rule as 'source
   952 $root$s denote the left-hand and right-hand side of the rule as `source
   954 code', i.e.\ in the usual syntax of terms.
   953 code', i.e.\ in the usual syntax of terms.
   955 
   954 
   956 Rules are internalized wrt.\ an intermediate signature that is obtained from
   955 Rules are internalized wrt.\ an intermediate signature that is obtained from
   957 the parent theories' ones by adding all material of all sections preceding
   956 the parent theories' ones by adding all material of all sections preceding
   958 {\tt translations} in the {\tt .thy} file, especially new syntax defined in
   957 {\tt translations} in the {\tt .thy} file. Especially, new syntax defined in
   959 {\tt consts} is already effective.
   958 {\tt consts} is already effective.
   960 
   959 
   961 Then part of the process that transforms input strings into terms is applied:
   960 Then part of the process that transforms input strings into terms is applied:
   962 lexing, parsing and parse ast translations (see \S\ref{sec:asts}). Macros
   961 lexing, parsing and parse ast translations (see \S\ref{sec:asts}). Macros
   963 specified in the parents are {\em not\/} expanded. Also note that the lexer
   962 specified in the parents are {\em not\/} expanded. Also note that the lexer
   969 should be treated as constants during matching (see below). These names are
   968 should be treated as constants during matching (see below). These names are
   970 extracted from all class, type and constant declarations made so far.
   969 extracted from all class, type and constant declarations made so far.
   971 
   970 
   972 \medskip
   971 \medskip
   973 The result are two lists of translation rules in internal form, that is pairs
   972 The result are two lists of translation rules in internal form, that is pairs
   974 of asts. They can be viewed using {\tt Syntax.print_syntax}
   973 of asts. They can be viewed using {\tt Syntax.print_syntax} (sections
   975 (\ttindex{parse_rules} and \ttindex{print_rules}). For {\tt SET} of
   974 \ttindex{parse_rules} and \ttindex{print_rules}). For {\tt SET} of
   976 Example~\ref{ex:set_trans} these are:
   975 Example~\ref{ex:set_trans} these are:
   977 \begin{ttbox}
   976 \begin{ttbox}
   978 parse_rules:
   977 parse_rules:
   979   ("{\at}Collect" x A P)  ->  ("Collect" A ("_abs" x P))
   978   ("{\at}Collect" x A P)  ->  ("Collect" A ("_abs" x P))
   980   ("{\at}Replace" y x A Q)  ->  ("Replace" A ("_abs" x ("_abs" y Q)))
   979   ("{\at}Replace" y x A Q)  ->  ("Replace" A ("_abs" x ("_abs" y Q)))
   999 \subsection{Applying rules}
   998 \subsection{Applying rules}
  1000 
   999 
  1001 In the course of parsing and printing terms, asts are generated as an
  1000 In the course of parsing and printing terms, asts are generated as an
  1002 intermediate form as pictured in Figure~\ref{fig:parse_print}. These asts are
  1001 intermediate form as pictured in Figure~\ref{fig:parse_print}. These asts are
  1003 normalized wrt.\ the given lists of translation rules in a uniform manner. As
  1002 normalized wrt.\ the given lists of translation rules in a uniform manner. As
  1004 stated earlier, asts are supposed to be first-order 'terms'. The rewriting
  1003 stated earlier, asts are supposed to be first-order `terms'. The rewriting
  1005 systems derived from {\tt translations} sections essentially resemble
  1004 systems derived from {\tt translations} sections essentially resemble
  1006 traditional first-order term rewriting systems. We first examine how a single
  1005 traditional first-order term rewriting systems. We first examine how a single
  1007 rule is applied.
  1006 rule is applied.
  1008 
  1007 
  1009 Let $t$ be the ast to be normalized and $(l, r)$ some translation rule. A
  1008 Let $t$ be the ast to be normalized and $(l, r)$ some translation rule. A
  1053 
  1052 
  1054 \medskip
  1053 \medskip
  1055 Having first-order matching in mind, the second case of $match$ may look a
  1054 Having first-order matching in mind, the second case of $match$ may look a
  1056 bit odd. But this is exactly the place, where {\tt Variable}s of non-rule
  1055 bit odd. But this is exactly the place, where {\tt Variable}s of non-rule
  1057 asts behave like {\tt Constant}s. The deeper meaning of this is related with
  1056 asts behave like {\tt Constant}s. The deeper meaning of this is related with
  1058 asts being very 'primitive' in some sense, ignorant of the underlying
  1057 asts being very `primitive' in some sense, ignorant of the underlying
  1059 'semantics', not far removed from parse trees. At this level it is not yet
  1058 `semantics', not far removed from parse trees. At this level it is not yet
  1060 known, which $id$s will become constants, bounds, frees, types or classes. As
  1059 known, which $id$s will become constants, bounds, frees, types or classes. As
  1061 $ast_of_pt$ (see \S\ref{sec:asts}) shows, former parse tree heads appear in
  1060 $ast_of_pt$ (see \S\ref{sec:asts}) shows, former parse tree heads appear in
  1062 asts as {\tt Constant}s, while $id$s, $var$s, $tfree$s and $tvar$s become
  1061 asts as {\tt Constant}s, while $id$s, $var$s, $tfree$s and $tvar$s become
  1063 {\tt Variable}s.
  1062 {\tt Variable}s.
  1064 
  1063 
  1098 rewriting systems, but this would often complicate things unnecessarily.
  1097 rewriting systems, but this would often complicate things unnecessarily.
  1099 Therefore, we reveal part of the actual rewriting strategy: The normalizer
  1098 Therefore, we reveal part of the actual rewriting strategy: The normalizer
  1100 always applies the first matching rule reducing an unspecified redex chosen
  1099 always applies the first matching rule reducing an unspecified redex chosen
  1101 first.
  1100 first.
  1102 
  1101 
  1103 Thereby, 'first rule' is roughly speaking meant wrt.\ the appearance of the
  1102 Thereby, `first rule' is roughly speaking meant wrt.\ the appearance of the
  1104 rules in the {\tt translations} sections. But this is more tricky than it
  1103 rules in the {\tt translations} sections. But this is more tricky than it
  1105 seems: If a given theory is {\em extended}, new rules are simply appended to
  1104 seems: If a given theory is {\em extended}, new rules are simply appended to
  1106 the end. But if theories are {\em merged}, it is not clear which list of
  1105 the end. But if theories are {\em merged}, it is not clear which list of
  1107 rules has priority over the other. In fact the merge order is left
  1106 rules has priority over the other. In fact the merge order is left
  1108 unspecified. This shouldn't cause any problems in practice, since
  1107 unspecified. This shouldn't cause any problems in practice, since
  1213 \verb|%empty insert. {x}|. This problem arises, because the ast rewriter
  1212 \verb|%empty insert. {x}|. This problem arises, because the ast rewriter
  1214 cannot discern constants, frees, bounds etc.\ and looks only for names of
  1213 cannot discern constants, frees, bounds etc.\ and looks only for names of
  1215 atoms.
  1214 atoms.
  1216 
  1215 
  1217 Thus the names of {\tt Constant}s occurring in the (internal) left-hand side
  1216 Thus the names of {\tt Constant}s occurring in the (internal) left-hand side
  1218 of translation rules should be regarded as 'reserved keywords'. It is good
  1217 of translation rules should be regarded as `reserved keywords'. It is good
  1219 practice to choose non-identifiers here like {\tt\at Finset} or sufficiently
  1218 practice to choose non-identifiers here like {\tt\at Finset} or sufficiently
  1220 long and strange names.
  1219 long and strange names.
  1221 \end{example}
  1220 \end{example}
  1222 
  1221 
  1223 \begin{example} \label{ex:prod_trans}
  1222 \begin{example} \label{ex:prod_trans}
  1249 
  1248 
  1250 Now the second parse rule is where the trick comes in: {\tt _K(B)} is
  1249 Now the second parse rule is where the trick comes in: {\tt _K(B)} is
  1251 introduced during ast rewriting, which later becomes \verb|%x.B| due to a
  1250 introduced during ast rewriting, which later becomes \verb|%x.B| due to a
  1252 parse translation associated with \ttindex{_K}. Note that a leading {\tt _}
  1251 parse translation associated with \ttindex{_K}. Note that a leading {\tt _}
  1253 in $id$s is allowed in translation rules, but not in ordinary terms. This
  1252 in $id$s is allowed in translation rules, but not in ordinary terms. This
  1254 special behaviour of the lexer is very useful for 'forging' asts containing
  1253 special behaviour of the lexer is very useful for `forging' asts containing
  1255 names that are not directly accessible normally. Unfortunately, there is no
  1254 names that are not directly accessible normally.
  1256 such trick for printing, so we have to add a {\tt ML} section for the print
  1255 
  1257 translation \ttindex{dependent_tr'}.
  1256 Unfortunately, there is no such trick for printing, so we have to add a {\tt
       
  1257 ML} section for the print translation \ttindex{dependent_tr'}.
  1258 
  1258 
  1259 The parse translation for {\tt _K} is already installed in Pure, and {\tt
  1259 The parse translation for {\tt _K} is already installed in Pure, and {\tt
  1260 dependent_tr'} is exported by the syntax module for public use. See
  1260 dependent_tr'} is exported by the syntax module for public use. See
  1261 \S\ref{sec:tr_funs} for more of the arcane lore of translation functions.
  1261 \S\ref{sec:tr_funs} for more of the arcane lore of translation functions.
  1262 \end{example}
  1262 \end{example}
  1319 also Figure~\ref{fig:parse_print}): Whenever --- during the transformations
  1319 also Figure~\ref{fig:parse_print}): Whenever --- during the transformations
  1320 between parse trees, asts and terms --- a combination of the form
  1320 between parse trees, asts and terms --- a combination of the form
  1321 $(\mtt"c\mtt"~x@1 \ldots x@n)$ is encountered, and a translation function $f$
  1321 $(\mtt"c\mtt"~x@1 \ldots x@n)$ is encountered, and a translation function $f$
  1322 of appropriate kind exists for $c$, the result will be $f \mtt[ x@1, \ldots,
  1322 of appropriate kind exists for $c$, the result will be $f \mtt[ x@1, \ldots,
  1323 x@n \mtt]$. Thereby, $x@1, \ldots, x@n$ (with $n \ge 0$) are asts for ast
  1323 x@n \mtt]$. Thereby, $x@1, \ldots, x@n$ (with $n \ge 0$) are asts for ast
  1324 translations and terms for term translations. A 'combination' at ast level is
  1324 translations and terms for term translations. A `combination' at ast level is
  1325 of the form $\Constant c$ or $\Appl{\Constant c, x@1, \ldots, x@n}$, and at
  1325 of the form $\Constant c$ or $\Appl{\Constant c, x@1, \ldots, x@n}$, and at
  1326 term level $\ttfct{Const} (c, \tau)$ or $\ttfct{Const} (c, \tau) \ttrel{\$}
  1326 term level $\ttfct{Const} (c, \tau)$ or $\ttfct{Const} (c, \tau) \ttrel{\$}
  1327 x@1 \ttrel{\$} \dots \ttrel{\$} x@n$.
  1327 x@1 \ttrel{\$} \dots \ttrel{\$} x@n$.
  1328 
  1328 
  1329 \medskip
  1329 \medskip
  1336 (ast) translations more fundamentally:
  1336 (ast) translations more fundamentally:
  1337 \begin{description}
  1337 \begin{description}
  1338   \item[Parse (ast) translations] are applied bottom-up, i.e.\ the arguments
  1338   \item[Parse (ast) translations] are applied bottom-up, i.e.\ the arguments
  1339     supplied ($x@1, \ldots, x@n$ above) are already in translated form.
  1339     supplied ($x@1, \ldots, x@n$ above) are already in translated form.
  1340     Additionally, they may not fail, exceptions are re-raised after printing
  1340     Additionally, they may not fail, exceptions are re-raised after printing
  1341     of an error message.
  1341     an error message.
  1342 
  1342 
  1343   \item[Print (ast) translations] are applied top-down, i.e.\ supplied with
  1343   \item[Print (ast) translations] are applied top-down, i.e.\ supplied with
  1344     arguments that are partly still in internal form. The result is again fed
  1344     arguments that are partly still in internal form. The result is again fed
  1345     into the translation machinery as a whole. Therefore a print (ast)
  1345     into the translation machinery as a whole. Therefore a print (ast)
  1346     translation should not introduce as head a constant of the same name that
  1346     translation should not introduce as head a constant of the same name that
  1419 body $B$ with {\tt Bound}s referring to our {\tt Abs} node replaced by
  1419 body $B$ with {\tt Bound}s referring to our {\tt Abs} node replaced by
  1420 $\ttfct{Free} (x', \mtt{dummyT})$.
  1420 $\ttfct{Free} (x', \mtt{dummyT})$.
  1421 
  1421 
  1422 We have to be more careful with types here. While types of {\tt Const}s are
  1422 We have to be more careful with types here. While types of {\tt Const}s are
  1423 completely ignored, type constraints may be printed for some {\tt Free}s and
  1423 completely ignored, type constraints may be printed for some {\tt Free}s and
  1424 {\tt Var}s (only if \ttindex{show_types} is set to {\tt true}). Variables of
  1424 {\tt Var}s (if \ttindex{show_types} is set to {\tt true}). Variables of type
  1425 type \ttindex{dummyT} are never printed with constraint, though. Thus, a
  1425 \ttindex{dummyT} are never printed with constraint, though. Thus, a
  1426 constraint of $x'$ may only appear at its binding place, since {\tt Free}s of
  1426 constraint of $x'$ may only appear at its binding place, since {\tt Free}s of
  1427 $B'$ replacing the appropriate {\tt Bound}s of $B$ via \ttindex{variant_abs}
  1427 $B'$ replacing the appropriate {\tt Bound}s of $B$ via \ttindex{variant_abs}
  1428 have all type {\tt dummyT}.
  1428 have all type {\tt dummyT}. \end{example}
  1429 \end{example}
       
  1430 
  1429 
  1431 
  1430 
  1432 
  1431 
  1433 \section{Example: some minimal logics} \label{sec:min_logics}
  1432 \section{Example: some minimal logics} \label{sec:min_logics}
  1434 
  1433 
  1452   Trueprop :: "o => prop"   ("_" 5)
  1451   Trueprop :: "o => prop"   ("_" 5)
  1453 end
  1452 end
  1454 \end{ttbox}
  1453 \end{ttbox}
  1455 The constant {\tt Trueprop} (the name is arbitrary) acts as an invisible
  1454 The constant {\tt Trueprop} (the name is arbitrary) acts as an invisible
  1456 coercion function. Assuming this definition resides in a file {\tt base.thy},
  1455 coercion function. Assuming this definition resides in a file {\tt base.thy},
  1457 you have to load it with the command {\tt use_thy "base"}.
  1456 you have to load it with the command {\tt use_thy "Base"}.
  1458 
  1457 
  1459 One of the simplest nontrivial logics is {\em minimal logic\/} of
  1458 One of the simplest nontrivial logics is {\em minimal logic\/} of
  1460 implication. Its definition in Isabelle needs no advanced features but
  1459 implication. Its definition in Isabelle needs no advanced features but
  1461 illustrates the overall mechanism quite nicely:
  1460 illustrates the overall mechanism quite nicely:
  1462 \begin{ttbox}
  1461 \begin{ttbox}