doc-src/Ref/defining.tex
author wenzelm
Mon, 22 Sep 1997 17:37:03 +0200
changeset 3694 fe7b812837ad
parent 3485 f27a30a18a17
child 3801 5ba459e15dd7
permissions -rw-r--r--
fixed pttrn syntax;
lcp@320
     1
%% $Id$
lcp@320
     2
\chapter{Defining Logics} \label{Defining-Logics}
lcp@320
     3
This chapter explains how to define new formal systems --- in particular,
lcp@320
     4
their concrete syntax.  While Isabelle can be regarded as a theorem prover
lcp@320
     5
for set theory, higher-order logic or the sequent calculus, its
lcp@320
     6
distinguishing feature is support for the definition of new logics.
lcp@320
     7
lcp@320
     8
Isabelle logics are hierarchies of theories, which are described and
wenzelm@864
     9
illustrated in
lcp@320
    10
\iflabelundefined{sec:defining-theories}{{\em Introduction to Isabelle}}%
lcp@320
    11
{\S\ref{sec:defining-theories}}.  That material, together with the theory
lcp@320
    12
files provided in the examples directories, should suffice for all simple
lcp@320
    13
applications.  The easiest way to define a new theory is by modifying a
lcp@320
    14
copy of an existing theory.
lcp@320
    15
lcp@320
    16
This chapter documents the meta-logic syntax, mixfix declarations and
lcp@320
    17
pretty printing.  The extended examples in \S\ref{sec:min_logics}
lcp@320
    18
demonstrate the logical aspects of the definition of theories.
lcp@320
    19
lcp@320
    20
lcp@320
    21
\section{Priority grammars} \label{sec:priority_grammars}
wenzelm@864
    22
\index{priority grammars|(}
lcp@320
    23
lcp@320
    24
A context-free grammar contains a set of {\bf nonterminal symbols}, a set of
lcp@320
    25
{\bf terminal symbols} and a set of {\bf productions}\index{productions}.
lcp@320
    26
Productions have the form ${A=\gamma}$, where $A$ is a nonterminal and
lcp@320
    27
$\gamma$ is a string of terminals and nonterminals.  One designated
lcp@320
    28
nonterminal is called the {\bf start symbol}.  The language defined by the
lcp@320
    29
grammar consists of all strings of terminals that can be derived from the
lcp@320
    30
start symbol by applying productions as rewrite rules.
lcp@320
    31
lcp@320
    32
The syntax of an Isabelle logic is specified by a {\bf priority
lcp@320
    33
  grammar}.\index{priorities} Each nonterminal is decorated by an integer
lcp@320
    34
priority, as in~$A^{(p)}$.  A nonterminal $A^{(p)}$ in a derivation may be
lcp@320
    35
rewritten using a production $A^{(q)} = \gamma$ only if~$p \le q$.  Any
lcp@320
    36
priority grammar can be translated into a normal context free grammar by
lcp@320
    37
introducing new nonterminals and productions.
lcp@320
    38
lcp@320
    39
Formally, a set of context free productions $G$ induces a derivation
lcp@320
    40
relation $\longrightarrow@G$.  Let $\alpha$ and $\beta$ denote strings of
lcp@320
    41
terminal or nonterminal symbols.  Then
wenzelm@864
    42
\[ \alpha\, A^{(p)}\, \beta ~\longrightarrow@G~ \alpha\,\gamma\,\beta \]
lcp@320
    43
if and only if $G$ contains some production $A^{(q)}=\gamma$ for~$p \le q$.
lcp@320
    44
lcp@320
    45
The following simple grammar for arithmetic expressions demonstrates how
lcp@320
    46
binding power and associativity of operators can be enforced by priorities.
lcp@320
    47
\begin{center}
lcp@320
    48
\begin{tabular}{rclr}
lcp@320
    49
  $A^{(9)}$ & = & {\tt0} \\
lcp@320
    50
  $A^{(9)}$ & = & {\tt(} $A^{(0)}$ {\tt)} \\
lcp@320
    51
  $A^{(0)}$ & = & $A^{(0)}$ {\tt+} $A^{(1)}$ \\
lcp@320
    52
  $A^{(2)}$ & = & $A^{(3)}$ {\tt*} $A^{(2)}$ \\
lcp@320
    53
  $A^{(3)}$ & = & {\tt-} $A^{(3)}$
lcp@320
    54
\end{tabular}
lcp@320
    55
\end{center}
lcp@320
    56
The choice of priorities determines that {\tt -} binds tighter than {\tt *},
lcp@320
    57
which binds tighter than {\tt +}.  Furthermore {\tt +} associates to the
lcp@320
    58
left and {\tt *} to the right.
lcp@320
    59
lcp@320
    60
For clarity, grammars obey these conventions:
lcp@320
    61
\begin{itemize}
lcp@320
    62
\item All priorities must lie between~0 and \ttindex{max_pri}, which is a
lcp@320
    63
  some fixed integer.  Sometimes {\tt max_pri} is written as $\infty$.
lcp@320
    64
\item Priority 0 on the right-hand side and priority \ttindex{max_pri} on
lcp@320
    65
  the left-hand side may be omitted.
lcp@320
    66
\item The production $A^{(p)} = \alpha$ is written as $A = \alpha~(p)$; the
lcp@320
    67
  priority of the left-hand side actually appears in a column on the far
wenzelm@864
    68
  right.
wenzelm@864
    69
\item Alternatives are separated by~$|$.
lcp@320
    70
\item Repetition is indicated by dots~(\dots) in an informal but obvious
lcp@320
    71
  way.
lcp@320
    72
\end{itemize}
lcp@320
    73
lcp@320
    74
Using these conventions and assuming $\infty=9$, the grammar
lcp@320
    75
takes the form
lcp@320
    76
\begin{center}
lcp@320
    77
\begin{tabular}{rclc}
lcp@320
    78
$A$ & = & {\tt0} & \hspace*{4em} \\
lcp@320
    79
 & $|$ & {\tt(} $A$ {\tt)} \\
lcp@320
    80
 & $|$ & $A$ {\tt+} $A^{(1)}$ & (0) \\
lcp@320
    81
 & $|$ & $A^{(3)}$ {\tt*} $A^{(2)}$ & (2) \\
lcp@320
    82
 & $|$ & {\tt-} $A^{(3)}$ & (3)
lcp@320
    83
\end{tabular}
lcp@320
    84
\end{center}
lcp@320
    85
\index{priority grammars|)}
lcp@320
    86
lcp@320
    87
lcp@320
    88
\begin{figure}
lcp@320
    89
\begin{center}
lcp@320
    90
\begin{tabular}{rclc}
clasohm@711
    91
$any$ &=& $prop$ ~~$|$~~ $logic$ \\\\
wenzelm@864
    92
$prop$ &=& {\tt(} $prop$ {\tt)} \\
wenzelm@864
    93
     &$|$& $prop^{(4)}$ {\tt::} $type$ & (3) \\
wenzelm@864
    94
     &$|$& {\tt PROP} $aprop$ \\
clasohm@711
    95
     &$|$& $any^{(3)}$ {\tt ==} $any^{(2)}$ & (2) \\
clasohm@711
    96
     &$|$& $any^{(3)}$ {\tt =?=} $any^{(2)}$ & (2) \\
lcp@320
    97
     &$|$& $prop^{(2)}$ {\tt ==>} $prop^{(1)}$ & (1) \\
lcp@320
    98
     &$|$& {\tt[|} $prop$ {\tt;} \dots {\tt;} $prop$ {\tt|]} {\tt==>} $prop^{(1)}$ & (1) \\
wenzelm@864
    99
     &$|$& {\tt!!} $idts$ {\tt.} $prop$ & (0) \\
wenzelm@864
   100
     &$|$& {\tt OFCLASS} {\tt(} $type$ {\tt,} $logic$ {\tt)} \\\\
lcp@320
   101
$aprop$ &=& $id$ ~~$|$~~ $var$
clasohm@711
   102
    ~~$|$~~ $logic^{(\infty)}$ {\tt(} $any$ {\tt,} \dots {\tt,} $any$ {\tt)} \\\\
wenzelm@864
   103
$logic$ &=& {\tt(} $logic$ {\tt)} \\
wenzelm@864
   104
      &$|$& $logic^{(4)}$ {\tt::} $type$ & (3) \\
wenzelm@864
   105
      &$|$& $id$ ~~$|$~~ $var$
wenzelm@864
   106
    ~~$|$~~ $logic^{(\infty)}$ {\tt(} $any$ {\tt,} \dots {\tt,} $any$ {\tt)} \\
wenzelm@3694
   107
      &$|$& {\tt \%} $pttrns$ {\tt.} $any^{(3)}$ & (3) \\\\
lcp@320
   108
$idts$ &=& $idt$ ~~$|$~~ $idt^{(1)}$ $idts$ \\\\
lcp@320
   109
$idt$ &=& $id$ ~~$|$~~ {\tt(} $idt$ {\tt)} \\
lcp@320
   110
    &$|$& $id$ {\tt ::} $type$ & (0) \\\\
wenzelm@3694
   111
$pttrns$ &=& $pttrn$ ~~$|$~~ $pttrn^{(1)}$ $pttrns$ \\\\
wenzelm@3694
   112
$pttrn$ &=& $idt$ \\\\
wenzelm@864
   113
$type$ &=& {\tt(} $type$ {\tt)} \\
wenzelm@864
   114
     &$|$& $tid$ ~~$|$~~ $tvar$ ~~$|$~~ $tid$ {\tt::} $sort$
wenzelm@864
   115
       ~~$|$~~ $tvar$ {\tt::} $sort$ \\
lcp@320
   116
     &$|$& $id$ ~~$|$~~ $type^{(\infty)}$ $id$
lcp@320
   117
                ~~$|$~~ {\tt(} $type$ {\tt,} \dots {\tt,} $type$ {\tt)} $id$ \\
lcp@320
   118
     &$|$& $type^{(1)}$ {\tt =>} $type$ & (0) \\
wenzelm@864
   119
     &$|$& {\tt[}  $type$ {\tt,} \dots {\tt,} $type$ {\tt]} {\tt=>} $type$&(0) \\\\
lcp@320
   120
$sort$ &=& $id$ ~~$|$~~ {\tt\ttlbrace\ttrbrace}
lcp@320
   121
                ~~$|$~~ {\tt\ttlbrace} $id$ {\tt,} \dots {\tt,} $id$ {\tt\ttrbrace}
lcp@320
   122
\end{tabular}
lcp@320
   123
\index{*PROP symbol}
lcp@320
   124
\index{*== symbol}\index{*=?= symbol}\index{*==> symbol}
lcp@320
   125
\index{*:: symbol}\index{*=> symbol}
lcp@332
   126
\index{sort constraints}
lcp@332
   127
%the index command: a percent is permitted, but braces must match!
lcp@320
   128
\index{%@{\tt\%} symbol}
lcp@320
   129
\index{{}@{\tt\ttlbrace} symbol}\index{{}@{\tt\ttrbrace} symbol}
lcp@320
   130
\index{*[ symbol}\index{*] symbol}
lcp@320
   131
\index{*"!"! symbol}
lcp@320
   132
\index{*"["| symbol}
lcp@320
   133
\index{*"|"] symbol}
lcp@320
   134
\end{center}
lcp@320
   135
\caption{Meta-logic syntax}\label{fig:pure_gram}
lcp@320
   136
\end{figure}
lcp@320
   137
lcp@320
   138
lcp@320
   139
\section{The Pure syntax} \label{sec:basic_syntax}
lcp@320
   140
\index{syntax!Pure|(}
lcp@320
   141
lcp@320
   142
At the root of all object-logics lies the theory \thydx{Pure}.  It
lcp@320
   143
contains, among many other things, the Pure syntax.  An informal account of
wenzelm@864
   144
this basic syntax (types, terms and formulae) appears in
lcp@320
   145
\iflabelundefined{sec:forward}{{\em Introduction to Isabelle}}%
lcp@320
   146
{\S\ref{sec:forward}}.  A more precise description using a priority grammar
lcp@320
   147
appears in Fig.\ts\ref{fig:pure_gram}.  It defines the following
lcp@320
   148
nonterminals:
lcp@320
   149
\begin{ttdescription}
wenzelm@864
   150
  \item[\ndxbold{any}] denotes any term.
wenzelm@864
   151
clasohm@711
   152
  \item[\ndxbold{prop}] denotes terms of type {\tt prop}.  These are formulae
wenzelm@864
   153
    of the meta-logic.  Note that user constants of result type {\tt prop}
wenzelm@864
   154
    (i.e.\ $c :: \ldots \To prop$) should always provide concrete syntax.
wenzelm@864
   155
    Otherwise atomic propositions with head $c$ may be printed incorrectly.
lcp@320
   156
wenzelm@864
   157
  \item[\ndxbold{aprop}] denotes atomic propositions.
wenzelm@864
   158
wenzelm@864
   159
%% FIXME huh!?
wenzelm@864
   160
%  These typically
wenzelm@864
   161
%  include the judgement forms of the object-logic; its definition
wenzelm@864
   162
%  introduces a meta-level predicate for each judgement form.
lcp@320
   163
clasohm@711
   164
  \item[\ndxbold{logic}] denotes terms whose type belongs to class
wenzelm@864
   165
    \cldx{logic}, excluding type \tydx{prop}.
lcp@320
   166
wenzelm@864
   167
  \item[\ndxbold{idts}] denotes a list of identifiers, possibly constrained
wenzelm@864
   168
    by types.
wenzelm@3694
   169
    
wenzelm@3694
   170
  \item[\ndxbold{pttrn}, \ndxbold{pttrns}] denote patterns for
wenzelm@3694
   171
    abstraction, cases etc.  Initially the same as $idt$ and $idts$,
wenzelm@3694
   172
    these are indetended to be augmented by user extensions.
lcp@320
   173
lcp@320
   174
  \item[\ndxbold{type}] denotes types of the meta-logic.
lcp@320
   175
wenzelm@864
   176
  \item[\ndxbold{sort}] denotes meta-level sorts.
lcp@320
   177
\end{ttdescription}
lcp@320
   178
lcp@320
   179
\begin{warn}
lcp@320
   180
  In {\tt idts}, note that \verb|x::nat y| is parsed as \verb|x::(nat y)|,
lcp@320
   181
  treating {\tt y} like a type constructor applied to {\tt nat}.  The
lcp@320
   182
  likely result is an error message.  To avoid this interpretation, use
lcp@320
   183
  parentheses and write \verb|(x::nat) y|.
lcp@332
   184
  \index{type constraints}\index{*:: symbol}
lcp@320
   185
lcp@320
   186
  Similarly, \verb|x::nat y::nat| is parsed as \verb|x::(nat y::nat)| and
lcp@320
   187
  yields an error.  The correct form is \verb|(x::nat) (y::nat)|.
lcp@320
   188
\end{warn}
lcp@320
   189
nipkow@452
   190
\begin{warn}
paulson@3485
   191
  Type constraints bind very weakly.  For example, \verb!x<y::nat! is normally
clasohm@711
   192
  parsed as \verb!(x<y)::nat!, unless \verb$<$ has priority of 3 or less, in
paulson@3485
   193
  which case the string is likely to be ambiguous.  The correct form is
nipkow@452
   194
  \verb!x<(y::nat)!.
nipkow@452
   195
\end{warn}
lcp@320
   196
nipkow@867
   197
\subsection{Logical types and default syntax}\label{logical-types}
nipkow@867
   198
\index{lambda calc@$\lambda$-calculus}
nipkow@867
   199
nipkow@867
   200
Isabelle's representation of mathematical languages is based on the
nipkow@867
   201
simply typed $\lambda$-calculus.  All logical types, namely those of
nipkow@867
   202
class \cldx{logic}, are automatically equipped with a basic syntax of
nipkow@867
   203
types, identifiers, variables, parentheses, $\lambda$-abstraction and
nipkow@867
   204
application.
nipkow@867
   205
\begin{warn}
nipkow@867
   206
  Isabelle combines the syntaxes for all types of class \cldx{logic} by
nipkow@867
   207
  mapping all those types to the single nonterminal $logic$.  Thus all
nipkow@867
   208
  productions of $logic$, in particular $id$, $var$ etc, become available.
nipkow@867
   209
\end{warn}
wenzelm@864
   210
wenzelm@864
   211
lcp@320
   212
\subsection{Lexical matters}
lcp@320
   213
The parser does not process input strings directly.  It operates on token
lcp@320
   214
lists provided by Isabelle's \bfindex{lexer}.  There are two kinds of
lcp@320
   215
tokens: \bfindex{delimiters} and \bfindex{name tokens}.
lcp@320
   216
lcp@320
   217
\index{reserved words}
lcp@320
   218
Delimiters can be regarded as reserved words of the syntax.  You can
lcp@320
   219
add new ones when extending theories.  In Fig.\ts\ref{fig:pure_gram} they
lcp@320
   220
appear in typewriter font, for example {\tt ==}, {\tt =?=} and
lcp@320
   221
{\tt PROP}\@.
lcp@320
   222
wenzelm@864
   223
Name tokens have a predefined syntax.  The lexer distinguishes six disjoint
wenzelm@864
   224
classes of names: \rmindex{identifiers}, \rmindex{unknowns}, type
wenzelm@864
   225
identifiers\index{type identifiers}, type unknowns\index{type unknowns},
paulson@3485
   226
\rmindex{numerals}, \rmindex{strings}.  They are denoted by \ndxbold{id},
wenzelm@864
   227
\ndxbold{var}, \ndxbold{tid}, \ndxbold{tvar}, \ndxbold{xnum}, \ndxbold{xstr},
wenzelm@864
   228
respectively.  Typical examples are {\tt x}, {\tt ?x7}, {\tt 'a}, {\tt ?'a3},
paulson@3485
   229
{\tt \#42}, {\tt ''foo bar''}.  Here is the precise syntax:
lcp@320
   230
\begin{eqnarray*}
lcp@320
   231
id        & =   & letter~quasiletter^* \\
lcp@320
   232
var       & =   & \mbox{\tt ?}id ~~|~~ \mbox{\tt ?}id\mbox{\tt .}nat \\
lcp@320
   233
tid       & =   & \mbox{\tt '}id \\
lcp@320
   234
tvar      & =   & \mbox{\tt ?}tid ~~|~~
wenzelm@864
   235
                  \mbox{\tt ?}tid\mbox{\tt .}nat \\
wenzelm@864
   236
xnum      & =   & \mbox{\tt \#}nat ~~|~~ \mbox{\tt \#\char`\~}nat \\
wenzelm@864
   237
xstr      & =   & \mbox{\tt ''\rm text\tt ''} \\[1ex]
lcp@320
   238
letter    & =   & \mbox{one of {\tt a}\dots {\tt z} {\tt A}\dots {\tt Z}} \\
lcp@320
   239
digit     & =   & \mbox{one of {\tt 0}\dots {\tt 9}} \\
lcp@320
   240
quasiletter & =  & letter ~~|~~ digit ~~|~~ \mbox{\tt _} ~~|~~ \mbox{\tt '} \\
lcp@320
   241
nat       & =   & digit^+
lcp@320
   242
\end{eqnarray*}
wenzelm@864
   243
The lexer repeatedly takes the maximal prefix of the input string that forms
wenzelm@864
   244
a valid token.  A maximal prefix that is both a delimiter and a name is
wenzelm@864
   245
treated as a delimiter.  Spaces, tabs, newlines and formfeeds are separators;
wenzelm@864
   246
they never occur within tokens, except those of class $xstr$.
wenzelm@864
   247
wenzelm@864
   248
\medskip
wenzelm@864
   249
Delimiters need not be separated by white space.  For example, if {\tt -}
wenzelm@864
   250
is a delimiter but {\tt --} is not, then the string {\tt --} is treated as
wenzelm@864
   251
two consecutive occurrences of the token~{\tt -}.  In contrast, \ML\
wenzelm@864
   252
treats {\tt --} as a single symbolic name.  The consequence of Isabelle's
wenzelm@864
   253
more liberal scheme is that the same string may be parsed in different ways
wenzelm@864
   254
after extending the syntax: after adding {\tt --} as a delimiter, the input
wenzelm@864
   255
{\tt --} is treated as a single token.
wenzelm@864
   256
lcp@320
   257
A \ndxbold{var} or \ndxbold{tvar} describes an unknown, which is internally
lcp@320
   258
a pair of base name and index (\ML\ type \mltydx{indexname}).  These
lcp@320
   259
components are either separated by a dot as in {\tt ?x.1} or {\tt ?x7.3} or
lcp@320
   260
run together as in {\tt ?x1}.  The latter form is possible if the base name
lcp@320
   261
does not end with digits.  If the index is 0, it may be dropped altogether:
lcp@320
   262
{\tt ?x} abbreviates both {\tt ?x0} and {\tt ?x.0}.
lcp@320
   263
wenzelm@864
   264
Tokens of class $xnum$ or $xstr$ are not used by the meta-logic.
wenzelm@864
   265
Object-logics may provide numerals and string constants by adding appropriate
wenzelm@864
   266
productions and translation functions.
lcp@320
   267
wenzelm@864
   268
\medskip
lcp@320
   269
Although name tokens are returned from the lexer rather than the parser, it
lcp@320
   270
is more logical to regard them as nonterminals.  Delimiters, however, are
lcp@320
   271
terminals; they are just syntactic sugar and contribute nothing to the
lcp@320
   272
abstract syntax tree.
lcp@320
   273
lcp@320
   274
wenzelm@3108
   275
\subsection{*Inspecting the syntax} \label{pg:print_syn}
lcp@320
   276
\begin{ttbox}
lcp@320
   277
syn_of              : theory -> Syntax.syntax
wenzelm@864
   278
print_syntax        : theory -> unit
lcp@320
   279
Syntax.print_syntax : Syntax.syntax -> unit
lcp@320
   280
Syntax.print_gram   : Syntax.syntax -> unit
lcp@320
   281
Syntax.print_trans  : Syntax.syntax -> unit
lcp@320
   282
\end{ttbox}
lcp@320
   283
The abstract type \mltydx{Syntax.syntax} allows manipulation of syntaxes
lcp@320
   284
in \ML.  You can display values of this type by calling the following
lcp@320
   285
functions:
lcp@320
   286
\begin{ttdescription}
lcp@320
   287
\item[\ttindexbold{syn_of} {\it thy}] returns the syntax of the Isabelle
lcp@320
   288
  theory~{\it thy} as an \ML\ value.
lcp@320
   289
wenzelm@864
   290
\item[\ttindexbold{print_syntax} $thy$] displays the syntax part of $thy$
wenzelm@864
   291
  using {\tt Syntax.print_syntax}.
wenzelm@864
   292
lcp@320
   293
\item[\ttindexbold{Syntax.print_syntax} {\it syn}] shows virtually all
lcp@320
   294
  information contained in the syntax {\it syn}.  The displayed output can
lcp@320
   295
  be large.  The following two functions are more selective.
lcp@320
   296
lcp@320
   297
\item[\ttindexbold{Syntax.print_gram} {\it syn}] shows the grammar part
wenzelm@864
   298
  of~{\it syn}, namely the lexicon, logical types and productions.  These are
lcp@320
   299
  discussed below.
lcp@320
   300
lcp@320
   301
\item[\ttindexbold{Syntax.print_trans} {\it syn}] shows the translation
lcp@320
   302
  part of~{\it syn}, namely the constants, parse/print macros and
lcp@320
   303
  parse/print translations.
lcp@320
   304
\end{ttdescription}
lcp@320
   305
lcp@320
   306
Let us demonstrate these functions by inspecting Pure's syntax.  Even that
lcp@320
   307
is too verbose to display in full.
lcp@320
   308
\begin{ttbox}\index{*Pure theory}
lcp@320
   309
Syntax.print_syntax (syn_of Pure.thy);
lcp@320
   310
{\out lexicon: "!!" "\%" "(" ")" "," "." "::" ";" "==" "==>" \dots}
wenzelm@864
   311
{\out logtypes: fun itself}
lcp@320
   312
{\out prods:}
lcp@320
   313
{\out   type = tid  (1000)}
lcp@320
   314
{\out   type = tvar  (1000)}
lcp@320
   315
{\out   type = id  (1000)}
lcp@320
   316
{\out   type = tid "::" sort[0]  => "_ofsort" (1000)}
lcp@320
   317
{\out   type = tvar "::" sort[0]  => "_ofsort" (1000)}
lcp@320
   318
{\out   \vdots}
lcp@320
   319
\ttbreak
wenzelm@3108
   320
{\out print modes: "symbols" "xterm"}
lcp@320
   321
{\out consts: "_K" "_appl" "_aprop" "_args" "_asms" "_bigimpl" \dots}
lcp@320
   322
{\out parse_ast_translation: "_appl" "_bigimpl" "_bracket"}
lcp@320
   323
{\out   "_idtyp" "_lambda" "_tapp" "_tappl"}
lcp@320
   324
{\out parse_rules:}
lcp@320
   325
{\out parse_translation: "!!" "_K" "_abs" "_aprop"}
lcp@320
   326
{\out print_translation: "all"}
lcp@320
   327
{\out print_rules:}
lcp@320
   328
{\out print_ast_translation: "==>" "_abs" "_idts" "fun"}
lcp@320
   329
\end{ttbox}
lcp@320
   330
lcp@332
   331
As you can see, the output is divided into labelled sections.  The grammar
wenzelm@864
   332
is represented by {\tt lexicon}, {\tt logtypes} and {\tt prods}.  The rest
lcp@320
   333
refers to syntactic translations and macro expansion.  Here is an
lcp@320
   334
explanation of the various sections.
lcp@320
   335
\begin{description}
lcp@320
   336
  \item[{\tt lexicon}] lists the delimiters used for lexical
wenzelm@864
   337
    analysis.\index{delimiters}
lcp@320
   338
wenzelm@864
   339
  \item[{\tt logtypes}] lists the types that are regarded the same as {\tt
paulson@3485
   340
    logic} syntactically.  Thus types of object-logics (e.g.\ {\tt nat}, say)
wenzelm@864
   341
    will be automatically equipped with the standard syntax of
wenzelm@864
   342
    $\lambda$-calculus.
lcp@320
   343
lcp@320
   344
  \item[{\tt prods}] lists the \rmindex{productions} of the priority grammar.
lcp@320
   345
    The nonterminal $A^{(n)}$ is rendered in {\sc ascii} as {\tt $A$[$n$]}.
lcp@320
   346
    Each delimiter is quoted.  Some productions are shown with {\tt =>} and
lcp@320
   347
    an attached string.  These strings later become the heads of parse
lcp@320
   348
    trees; they also play a vital role when terms are printed (see
lcp@320
   349
    \S\ref{sec:asts}).
lcp@320
   350
lcp@320
   351
    Productions with no strings attached are called {\bf copy
lcp@320
   352
      productions}\indexbold{productions!copy}.  Their right-hand side must
lcp@320
   353
    have exactly one nonterminal symbol (or name token).  The parser does
lcp@320
   354
    not create a new parse tree node for copy productions, but simply
lcp@320
   355
    returns the parse tree of the right-hand symbol.
lcp@320
   356
lcp@320
   357
    If the right-hand side consists of a single nonterminal with no
lcp@320
   358
    delimiters, then the copy production is called a {\bf chain
lcp@320
   359
      production}.  Chain productions act as abbreviations:
lcp@320
   360
    conceptually, they are removed from the grammar by adding new
lcp@320
   361
    productions.  Priority information attached to chain productions is
lcp@320
   362
    ignored; only the dummy value $-1$ is displayed.
wenzelm@3108
   363
    
wenzelm@3108
   364
  \item[\ttindex{print_modes}] lists the alternative print modes
wenzelm@3108
   365
    provided by this syntax (see \S\ref{sec:prmodes}).
lcp@320
   366
lcp@320
   367
  \item[{\tt consts}, {\tt parse_rules}, {\tt print_rules}]
lcp@320
   368
    relate to macros (see \S\ref{sec:macros}).
lcp@320
   369
lcp@320
   370
  \item[{\tt parse_ast_translation}, {\tt print_ast_translation}]
lcp@320
   371
    list sets of constants that invoke translation functions for abstract
lcp@320
   372
    syntax trees.  Section \S\ref{sec:asts} below discusses this obscure
lcp@320
   373
    matter.\index{constants!for translations}
lcp@320
   374
lcp@320
   375
  \item[{\tt parse_translation}, {\tt print_translation}] list sets
lcp@320
   376
    of constants that invoke translation functions for terms (see
lcp@320
   377
    \S\ref{sec:tr_funs}).
lcp@320
   378
\end{description}
lcp@320
   379
\index{syntax!Pure|)}
lcp@320
   380
lcp@320
   381
lcp@320
   382
\section{Mixfix declarations} \label{sec:mixfix}
wenzelm@864
   383
\index{mixfix declarations|(}
lcp@320
   384
lcp@320
   385
When defining a theory, you declare new constants by giving their names,
lcp@320
   386
their type, and an optional {\bf mixfix annotation}.  Mixfix annotations
lcp@320
   387
allow you to extend Isabelle's basic $\lambda$-calculus syntax with
lcp@320
   388
readable notation.  They can express any context-free priority grammar.
lcp@320
   389
Isabelle syntax definitions are inspired by \OBJ~\cite{OBJ}; they are more
wenzelm@864
   390
general than the priority declarations of \ML\ and Prolog.
lcp@320
   391
lcp@320
   392
A mixfix annotation defines a production of the priority grammar.  It
lcp@320
   393
describes the concrete syntax, the translation to abstract syntax, and the
lcp@320
   394
pretty printing.  Special case annotations provide a simple means of
wenzelm@864
   395
specifying infix operators and binders.
lcp@320
   396
lcp@320
   397
\subsection{The general mixfix form}
lcp@320
   398
Here is a detailed account of mixfix declarations.  Suppose the following
wenzelm@864
   399
line occurs within a {\tt consts} or {\tt syntax} section of a {\tt .thy}
wenzelm@864
   400
file:
lcp@320
   401
\begin{center}
lcp@320
   402
  {\tt $c$ ::\ "$\sigma$" ("$template$" $ps$ $p$)}
lcp@320
   403
\end{center}
lcp@332
   404
This constant declaration and mixfix annotation are interpreted as follows:
lcp@320
   405
\begin{itemize}\index{productions}
lcp@320
   406
\item The string {\tt $c$} is the name of the constant associated with the
lcp@320
   407
  production; unless it is a valid identifier, it must be enclosed in
lcp@320
   408
  quotes.  If $c$ is empty (given as~{\tt ""}) then this is a copy
lcp@320
   409
  production.\index{productions!copy} Otherwise, parsing an instance of the
lcp@320
   410
  phrase $template$ generates the \AST{} {\tt ("$c$" $a@1$ $\ldots$
lcp@320
   411
    $a@n$)}, where $a@i$ is the \AST{} generated by parsing the $i$-th
lcp@320
   412
  argument.
lcp@320
   413
wenzelm@864
   414
  \item The constant $c$, if non-empty, is declared to have type $\sigma$
wenzelm@864
   415
    ({\tt consts} section only).
lcp@320
   416
lcp@320
   417
  \item The string $template$ specifies the right-hand side of
lcp@320
   418
    the production.  It has the form
wenzelm@864
   419
    \[ w@0 \;_\; w@1 \;_\; \ldots \;_\; w@n, \]
lcp@320
   420
    where each occurrence of {\tt_} denotes an argument position and
lcp@320
   421
    the~$w@i$ do not contain~{\tt _}.  (If you want a literal~{\tt _} in
lcp@320
   422
    the concrete syntax, you must escape it as described below.)  The $w@i$
wenzelm@864
   423
    may consist of \rmindex{delimiters}, spaces or
lcp@320
   424
    \rmindex{pretty printing} annotations (see below).
lcp@320
   425
lcp@320
   426
  \item The type $\sigma$ specifies the production's nonterminal symbols
lcp@320
   427
    (or name tokens).  If $template$ is of the form above then $\sigma$
lcp@320
   428
    must be a function type with at least~$n$ argument positions, say
lcp@320
   429
    $\sigma = [\tau@1, \dots, \tau@n] \To \tau$.  Nonterminal symbols are
lcp@320
   430
    derived from the types $\tau@1$, \ldots,~$\tau@n$, $\tau$ as described
wenzelm@864
   431
    below.  Any of these may be function types.
lcp@320
   432
lcp@320
   433
  \item The optional list~$ps$ may contain at most $n$ integers, say {\tt
lcp@320
   434
      [$p@1$, $\ldots$, $p@m$]}, where $p@i$ is the minimal
lcp@320
   435
    priority\indexbold{priorities} required of any phrase that may appear
lcp@320
   436
    as the $i$-th argument.  Missing priorities default to~0.
lcp@320
   437
lcp@320
   438
  \item The integer $p$ is the priority of this production.  If omitted, it
lcp@320
   439
    defaults to the maximal priority.
lcp@320
   440
    Priorities range between 0 and \ttindexbold{max_pri} (= 1000).
lcp@320
   441
\end{itemize}
lcp@320
   442
%
wenzelm@864
   443
The resulting production is \[ A^{(p)}= w@0\, A@1^{(p@1)}\, w@1\,
wenzelm@864
   444
A@2^{(p@2)}\, \dots\, A@n^{(p@n)}\, w@n \] where $A$ and the $A@i$ are the
wenzelm@864
   445
nonterminals corresponding to the types $\tau$ and $\tau@i$ respectively.
wenzelm@864
   446
The nonterminal symbol associated with a type $(\ldots)ty$ is {\tt logic}, if
wenzelm@864
   447
this is a logical type (namely one of class {\tt logic} excluding {\tt
wenzelm@864
   448
prop}).  Otherwise it is $ty$ (note that only the outermost type constructor
wenzelm@864
   449
is taken into account).  Finally, the nonterminal of a type variable is {\tt
wenzelm@864
   450
any}.
wenzelm@864
   451
wenzelm@911
   452
\begin{warn}
wenzelm@864
   453
  Theories must sometimes declare types for purely syntactic purposes ---
paulson@3485
   454
  merely playing the role of nonterminals.  One example is \tydx{type}, the
wenzelm@864
   455
  built-in type of types.  This is a `type of all types' in the syntactic
wenzelm@864
   456
  sense only.  Do not declare such types under {\tt arities} as belonging to
wenzelm@864
   457
  class {\tt logic}\index{*logic class}, for that would make them useless as
wenzelm@864
   458
  separate nonterminal symbols.
wenzelm@864
   459
\end{warn}
wenzelm@864
   460
wenzelm@864
   461
Associating nonterminals with types allows a constant's type to specify
wenzelm@864
   462
syntax as well.  We can declare the function~$f$ to have type $[\tau@1,
wenzelm@864
   463
\ldots, \tau@n]\To \tau$ and, through a mixfix annotation, specify the layout
wenzelm@864
   464
of the function's $n$ arguments.  The constant's name, in this case~$f$, will
wenzelm@864
   465
also serve as the label in the abstract syntax tree.
wenzelm@864
   466
wenzelm@864
   467
You may also declare mixfix syntax without adding constants to the theory's
wenzelm@864
   468
signature, by using a {\tt syntax} section instead of {\tt consts}.  Thus a
wenzelm@864
   469
production need not map directly to a logical function (this typically
wenzelm@864
   470
requires additional syntactic translations, see also
wenzelm@864
   471
Chapter~\ref{chap:syntax}).
wenzelm@864
   472
wenzelm@864
   473
wenzelm@911
   474
\medskip
wenzelm@911
   475
As a special case of the general mixfix declaration, the form
wenzelm@864
   476
\begin{center}
wenzelm@911
   477
  {\tt $c$ ::\ "$\sigma$" ("$template$")}
wenzelm@864
   478
\end{center}
wenzelm@864
   479
specifies no priorities.  The resulting production puts no priority
wenzelm@864
   480
constraints on any of its arguments and has maximal priority itself.
wenzelm@864
   481
Omitting priorities in this manner is prone to syntactic ambiguities unless
berghofe@3098
   482
the production's right-hand side is fully bracketed, as in
berghofe@3098
   483
\verb|"if _ then _ else _ fi"|.
lcp@320
   484
lcp@320
   485
Omitting the mixfix annotation completely, as in {\tt $c$ ::\ "$\sigma$"},
lcp@320
   486
is sensible only if~$c$ is an identifier.  Otherwise you will be unable to
lcp@320
   487
write terms involving~$c$.
lcp@320
   488
lcp@320
   489
lcp@320
   490
\subsection{Example: arithmetic expressions}
lcp@320
   491
\index{examples!of mixfix declarations}
wenzelm@864
   492
This theory specification contains a {\tt syntax} section with mixfix
lcp@320
   493
declarations encoding the priority grammar from
lcp@320
   494
\S\ref{sec:priority_grammars}:
lcp@320
   495
\begin{ttbox}
wenzelm@3108
   496
ExpSyntax = Pure +
lcp@320
   497
types
lcp@320
   498
  exp
wenzelm@864
   499
syntax
clasohm@1387
   500
  "0" :: exp                 ("0"      9)
clasohm@1387
   501
  "+" :: [exp, exp] => exp   ("_ + _"  [0, 1] 0)
clasohm@1387
   502
  "*" :: [exp, exp] => exp   ("_ * _"  [3, 2] 2)
clasohm@1387
   503
  "-" :: exp => exp          ("- _"    [3] 3)
lcp@320
   504
end
lcp@320
   505
\end{ttbox}
wenzelm@3108
   506
If you put this into a file {\tt ExpSyntax.thy} and load it via {\tt
wenzelm@3108
   507
  use_thy"ExpSyntax"}, you can run some tests:
lcp@320
   508
\begin{ttbox}
wenzelm@3108
   509
val read_exp = Syntax.test_read (syn_of ExpSyntax.thy) "exp";
lcp@320
   510
{\out val it = fn : string -> unit}
lcp@320
   511
read_exp "0 * 0 * 0 * 0 + 0 + 0 + 0";
lcp@320
   512
{\out tokens: "0" "*" "0" "*" "0" "*" "0" "+" "0" "+" "0" "+" "0"}
lcp@320
   513
{\out raw: ("+" ("+" ("+" ("*" "0" ("*" "0" ("*" "0" "0"))) "0") "0") "0")}
lcp@320
   514
{\out \vdots}
lcp@320
   515
read_exp "0 + - 0 + 0";
lcp@320
   516
{\out tokens: "0" "+" "-" "0" "+" "0"}
lcp@320
   517
{\out raw: ("+" ("+" "0" ("-" "0")) "0")}
lcp@320
   518
{\out \vdots}
lcp@320
   519
\end{ttbox}
lcp@320
   520
The output of \ttindex{Syntax.test_read} includes the token list ({\tt
lcp@320
   521
  tokens}) and the raw \AST{} directly derived from the parse tree,
lcp@320
   522
ignoring parse \AST{} translations.  The rest is tracing information
lcp@320
   523
provided by the macro expander (see \S\ref{sec:macros}).
lcp@320
   524
wenzelm@864
   525
Executing {\tt Syntax.print_gram} reveals the productions derived from the
wenzelm@864
   526
above mixfix declarations (lots of additional information deleted):
lcp@320
   527
\begin{ttbox}
wenzelm@3108
   528
Syntax.print_gram (syn_of ExpSyntax.thy);
lcp@320
   529
{\out exp = "0"  => "0" (9)}
lcp@320
   530
{\out exp = exp[0] "+" exp[1]  => "+" (0)}
lcp@320
   531
{\out exp = exp[3] "*" exp[2]  => "*" (2)}
lcp@320
   532
{\out exp = "-" exp[3]  => "-" (3)}
lcp@320
   533
\end{ttbox}
lcp@320
   534
wenzelm@3108
   535
Note that because {\tt exp} is not of class {\tt logic}, it has been
paulson@3485
   536
retained as a separate nonterminal.  This also entails that the syntax
wenzelm@3108
   537
does not provide for identifiers or paranthesized expressions.
wenzelm@3108
   538
Normally you would also want to add the declaration {\tt arities
wenzelm@3108
   539
  exp::logic} after {\tt types} and use {\tt consts} instead of {\tt
paulson@3485
   540
  syntax}.  Try this as an exercise and study the changes in the
nipkow@867
   541
grammar.
lcp@320
   542
lcp@320
   543
\subsection{The mixfix template}
wenzelm@864
   544
Let us now take a closer look at the string $template$ appearing in mixfix
lcp@320
   545
annotations.  This string specifies a list of parsing and printing
lcp@320
   546
directives: delimiters\index{delimiters}, arguments, spaces, blocks of
lcp@320
   547
indentation and line breaks.  These are encoded by the following character
lcp@320
   548
sequences:
lcp@320
   549
\index{pretty printing|(}
lcp@320
   550
\begin{description}
lcp@320
   551
\item[~$d$~] is a delimiter, namely a non-empty sequence of characters
lcp@320
   552
  other than the special characters {\tt _}, {\tt(}, {\tt)} and~{\tt/}.
lcp@320
   553
  Even these characters may appear if escaped; this means preceding it with
lcp@320
   554
  a~{\tt '} (single quote).  Thus you have to write {\tt ''} if you really
wenzelm@911
   555
  want a single quote.  Furthermore, a~{\tt '} followed by a space separates
wenzelm@911
   556
  delimiters without extra white space being added for printing.
lcp@320
   557
lcp@320
   558
\item[~{\tt_}~] is an argument position, which stands for a nonterminal symbol
lcp@320
   559
  or name token.
lcp@320
   560
lcp@320
   561
\item[~$s$~] is a non-empty sequence of spaces for printing.  This and the
lcp@320
   562
  following specifications do not affect parsing at all.
lcp@320
   563
lcp@320
   564
\item[~{\tt(}$n$~] opens a pretty printing block.  The optional number $n$
lcp@320
   565
  specifies how much indentation to add when a line break occurs within the
lcp@320
   566
  block.  If {\tt(} is not followed by digits, the indentation defaults
lcp@320
   567
  to~0.
lcp@320
   568
lcp@320
   569
\item[~{\tt)}~] closes a pretty printing block.
lcp@320
   570
lcp@320
   571
\item[~{\tt//}~] forces a line break.
lcp@320
   572
lcp@320
   573
\item[~{\tt/}$s$~] allows a line break.  Here $s$ stands for the string of
lcp@320
   574
  spaces (zero or more) right after the {\tt /} character.  These spaces
lcp@320
   575
  are printed if the break is not taken.
lcp@320
   576
\end{description}
lcp@320
   577
For example, the template {\tt"(_ +/ _)"} specifies an infix operator.
lcp@320
   578
There are two argument positions; the delimiter~{\tt+} is preceded by a
lcp@320
   579
space and followed by a space or line break; the entire phrase is a pretty
lcp@320
   580
printing block.  Other examples appear in Fig.\ts\ref{fig:set_trans} below.
lcp@320
   581
Isabelle's pretty printer resembles the one described in
lcp@320
   582
Paulson~\cite{paulson91}.
lcp@320
   583
lcp@320
   584
\index{pretty printing|)}
lcp@320
   585
lcp@320
   586
lcp@320
   587
\subsection{Infixes}
lcp@320
   588
\indexbold{infixes}
lcp@320
   589
wenzelm@3108
   590
Infix operators associating to the left or right can be declared using
wenzelm@3108
   591
{\tt infixl} or {\tt infixr}.  Basically, the form {\tt $c$ ::\ 
wenzelm@3108
   592
  $\sigma$ (infixl $p$)} abbreviates the mixfix declarations
lcp@320
   593
\begin{ttbox}
clasohm@1387
   594
"op \(c\)" :: \(\sigma\)   ("(_ \(c\)/ _)" [\(p\), \(p+1\)] \(p\))
clasohm@1387
   595
"op \(c\)" :: \(\sigma\)   ("op \(c\)")
lcp@320
   596
\end{ttbox}
clasohm@1387
   597
and {\tt $c$ ::\ $\sigma$ (infixr $p$)} abbreviates the mixfix declarations
lcp@320
   598
\begin{ttbox}
clasohm@1387
   599
"op \(c\)" :: \(\sigma\)   ("(_ \(c\)/ _)" [\(p+1\), \(p\)] \(p\))
clasohm@1387
   600
"op \(c\)" :: \(\sigma\)   ("op \(c\)")
lcp@320
   601
\end{ttbox}
lcp@320
   602
The infix operator is declared as a constant with the prefix {\tt op}.
lcp@320
   603
Thus, prefixing infixes with \sdx{op} makes them behave like ordinary
lcp@320
   604
function symbols, as in \ML.  Special characters occurring in~$c$ must be
lcp@320
   605
escaped, as in delimiters, using a single quote.
lcp@320
   606
wenzelm@3108
   607
A slightly more general form of infix declarations allows constant
wenzelm@3108
   608
names to be independent from their concrete syntax, namely \texttt{$c$
paulson@3485
   609
  ::\ $\sigma$\ (infixl "$sy$" $p$)}, the same for \texttt{infixr}.  As
wenzelm@3108
   610
an example consider:
wenzelm@3108
   611
\begin{ttbox}
wenzelm@3108
   612
and :: [bool, bool] => bool  (infixr "&" 35)
wenzelm@3108
   613
\end{ttbox}
wenzelm@3108
   614
The internal constant name will then be just \texttt{and}, without any
wenzelm@3108
   615
\texttt{op} prefixed.
wenzelm@3108
   616
lcp@320
   617
lcp@320
   618
\subsection{Binders}
lcp@320
   619
\indexbold{binders}
lcp@320
   620
\begingroup
lcp@320
   621
\def\Q{{\cal Q}}
lcp@320
   622
A {\bf binder} is a variable-binding construct such as a quantifier.  The
lcp@320
   623
constant declaration
lcp@320
   624
\begin{ttbox}
clasohm@1387
   625
\(c\) :: \(\sigma\)   (binder "\(\Q\)" [\(pb\)] \(p\))
lcp@320
   626
\end{ttbox}
lcp@320
   627
introduces a constant~$c$ of type~$\sigma$, which must have the form
lcp@320
   628
$(\tau@1 \To \tau@2) \To \tau@3$.  Its concrete syntax is $\Q~x.P$, where
lcp@320
   629
$x$ is a bound variable of type~$\tau@1$, the body~$P$ has type $\tau@2$
paulson@3485
   630
and the whole term has type~$\tau@3$.  The optional integer $pb$
lcp@1060
   631
specifies the body's priority, by default~$p$.  Special characters
clasohm@877
   632
in $\Q$ must be escaped using a single quote.
lcp@320
   633
wenzelm@864
   634
The declaration is expanded internally to something like
lcp@320
   635
\begin{ttbox}
berghofe@3098
   636
\(c\)\hskip3pt    :: (\(\tau@1\) => \(\tau@2\)) => \(\tau@3\)
berghofe@3098
   637
"\(\Q\)"  :: [idts, \(\tau@2\)] => \(\tau@3\)   ("(3\(\Q\)_./ _)" [0, \(pb\)] \(p\))
lcp@320
   638
\end{ttbox}
lcp@320
   639
Here \ndx{idts} is the nonterminal symbol for a list of identifiers with
lcp@332
   640
\index{type constraints}
lcp@320
   641
optional type constraints (see Fig.\ts\ref{fig:pure_gram}).  The
lcp@320
   642
declaration also installs a parse translation\index{translations!parse}
lcp@320
   643
for~$\Q$ and a print translation\index{translations!print} for~$c$ to
lcp@320
   644
translate between the internal and external forms.
lcp@320
   645
lcp@320
   646
A binder of type $(\sigma \To \tau) \To \tau$ can be nested by giving a
lcp@320
   647
list of variables.  The external form $\Q~x@1~x@2 \ldots x@n. P$
lcp@320
   648
corresponds to the internal form
lcp@320
   649
\[ c(\lambda x@1. c(\lambda x@2. \ldots c(\lambda x@n. P) \ldots)). \]
lcp@320
   650
lcp@320
   651
\medskip
lcp@320
   652
For example, let us declare the quantifier~$\forall$:\index{quantifiers}
lcp@320
   653
\begin{ttbox}
clasohm@1387
   654
All :: ('a => o) => o   (binder "ALL " 10)
lcp@320
   655
\end{ttbox}
lcp@320
   656
This lets us write $\forall x.P$ as either {\tt All(\%$x$.$P$)} or {\tt ALL
lcp@320
   657
  $x$.$P$}.  When printing, Isabelle prefers the latter form, but must fall
lcp@320
   658
back on ${\tt All}(P)$ if $P$ is not an abstraction.  Both $P$ and {\tt ALL
lcp@320
   659
  $x$.$P$} have type~$o$, the type of formulae, while the bound variable
lcp@320
   660
can be polymorphic.
lcp@320
   661
\endgroup
lcp@320
   662
lcp@320
   663
\index{mixfix declarations|)}
lcp@320
   664
wenzelm@3108
   665
wenzelm@3108
   666
\section{*Alternative print modes} \label{sec:prmodes}
wenzelm@3108
   667
\index{print modes|(}
wenzelm@3108
   668
%
paulson@3485
   669
Isabelle's pretty printer supports alternative output syntaxes.  These
paulson@3485
   670
may be used independently or in cooperation.  The currently active
wenzelm@3108
   671
print modes (with precedence from left to right) are determined by a
wenzelm@3108
   672
reference variable.
wenzelm@3108
   673
\begin{ttbox}\index{*print_mode}
wenzelm@3108
   674
print_mode: string list ref
wenzelm@3108
   675
\end{ttbox}
wenzelm@3108
   676
Initially this may already contain some print mode identifiers,
wenzelm@3108
   677
depending on how Isabelle has been invoked (e.g.\ by some user
paulson@3485
   678
interface).  So changes should be incremental --- adding or deleting
wenzelm@3108
   679
modes relative to the current value.
wenzelm@3108
   680
wenzelm@3108
   681
Any \ML{} string is a legal print mode identifier, without any
wenzelm@3108
   682
predeclaration required.  The following names should be considered
wenzelm@3108
   683
reserved, though: \texttt{""} (yes, the empty string),
wenzelm@3108
   684
\texttt{symbols}, \texttt{latex}, \texttt{xterm}.
wenzelm@3108
   685
wenzelm@3108
   686
There is a separate table of mixfix productions for pretty printing
paulson@3485
   687
associated with each print mode.  The currently active ones are
wenzelm@3108
   688
conceptually just concatenated from left to right, with the standard
wenzelm@3108
   689
syntax output table always coming last as default.  Thus mixfix
wenzelm@3108
   690
productions of preceding modes in the list may override those of later
wenzelm@3108
   691
ones.  Also note that token translations are always relative to some
wenzelm@3108
   692
print mode (see \S\ref{sec:tok_tr}).
wenzelm@3108
   693
wenzelm@3108
   694
\medskip The canonical application of print modes is optional printing
wenzelm@3108
   695
of mathematical symbols from a special screen font instead of {\sc
paulson@3485
   696
  ascii}.  Another example is to re-use Isabelle's advanced
wenzelm@3108
   697
$\lambda$-term printing mechanisms to generate completely different
wenzelm@3228
   698
output, say for interfacing external tools like \rmindex{model
wenzelm@3228
   699
  checkers} (see also \texttt{HOL/Modelcheck}).
wenzelm@3108
   700
wenzelm@3108
   701
\index{print modes|)}
wenzelm@3108
   702
wenzelm@3108
   703
clasohm@711
   704
\section{Ambiguity of parsed expressions} \label{sec:ambiguity}
clasohm@711
   705
\index{ambiguity!of parsed expressions}
clasohm@711
   706
clasohm@711
   707
To keep the grammar small and allow common productions to be shared
wenzelm@864
   708
all logical types (except {\tt prop}) are internally represented
paulson@3485
   709
by one nonterminal, namely {\tt logic}.  This and omitted or too freely
clasohm@711
   710
chosen priorities may lead to ways of parsing an expression that were
paulson@3485
   711
not intended by the theory's maker.  In most cases Isabelle is able to
wenzelm@864
   712
select one of multiple parse trees that an expression has lead
paulson@3485
   713
to by checking which of them can be typed correctly.  But this may not
clasohm@711
   714
work in every case and always slows down parsing.
wenzelm@864
   715
The warning and error messages that can be produced during this process are
clasohm@711
   716
as follows:
clasohm@711
   717
clasohm@880
   718
If an ambiguity can be resolved by type inference the following
clasohm@880
   719
warning is shown to remind the user that parsing is (unnecessarily)
paulson@3485
   720
slowed down.  In cases where it's not easily possible to eliminate the
clasohm@880
   721
ambiguity the frequency of the warning can be controlled by changing
clasohm@883
   722
the value of {\tt Syntax.ambiguity_level} which has type {\tt int
paulson@3485
   723
ref}.  Its default value is 1 and by increasing it one can control how
clasohm@883
   724
many parse trees are necessary to generate the warning.
clasohm@711
   725
clasohm@711
   726
\begin{ttbox}
clasohm@711
   727
{\out Warning: Ambiguous input "..."}
clasohm@711
   728
{\out produces the following parse trees:}
clasohm@711
   729
{\out ...}
clasohm@711
   730
{\out Fortunately, only one parse tree is type correct.}
clasohm@711
   731
{\out It helps (speed!) if you disambiguate your grammar or your input.}
clasohm@711
   732
\end{ttbox}
clasohm@711
   733
clasohm@711
   734
The following message is normally caused by using the same
clasohm@711
   735
syntax in two different productions:
clasohm@711
   736
clasohm@711
   737
\begin{ttbox}
clasohm@711
   738
{\out Warning: Ambiguous input "..."}
clasohm@711
   739
{\out produces the following parse trees:}
clasohm@711
   740
{\out ...}
clasohm@711
   741
{\out Error: More than one term is type correct:}
clasohm@711
   742
{\out ...}
clasohm@711
   743
\end{ttbox}
clasohm@711
   744
clasohm@866
   745
Ambiguities occuring in syntax translation rules cannot be resolved by
clasohm@866
   746
type inference because it is not necessary for these rules to be type
paulson@3485
   747
correct.  Therefore Isabelle always generates an error message and the
clasohm@866
   748
ambiguity should be eliminated by changing the grammar or the rule.
clasohm@711
   749
lcp@320
   750
lcp@320
   751
\section{Example: some minimal logics} \label{sec:min_logics}
lcp@320
   752
\index{examples!of logic definitions}
lcp@320
   753
lcp@320
   754
This section presents some examples that have a simple syntax.  They
lcp@320
   755
demonstrate how to define new object-logics from scratch.
lcp@320
   756
clasohm@711
   757
First we must define how an object-logic syntax is embedded into the
wenzelm@864
   758
meta-logic.  Since all theorems must conform to the syntax for~\ndx{prop}
wenzelm@864
   759
(see Fig.\ts\ref{fig:pure_gram}), that syntax has to be extended with the
lcp@320
   760
object-level syntax.  Assume that the syntax of your object-logic defines a
wenzelm@864
   761
meta-type~\tydx{o} of formulae which refers to the nonterminal {\tt logic}.
wenzelm@864
   762
These formulae can now appear in axioms and theorems wherever \ndx{prop} does
wenzelm@864
   763
if you add the production
wenzelm@864
   764
\[ prop ~=~ logic. \]
wenzelm@864
   765
This is not supposed to be a copy production but an implicit coercion from
wenzelm@864
   766
formulae to propositions:
lcp@320
   767
\begin{ttbox}
lcp@320
   768
Base = Pure +
lcp@320
   769
types
lcp@320
   770
  o
lcp@320
   771
arities
lcp@320
   772
  o :: logic
lcp@320
   773
consts
clasohm@1387
   774
  Trueprop :: o => prop   ("_" 5)
lcp@320
   775
end
lcp@320
   776
\end{ttbox}
lcp@320
   777
The constant \cdx{Trueprop} (the name is arbitrary) acts as an invisible
lcp@332
   778
coercion function.  Assuming this definition resides in a file {\tt Base.thy},
lcp@320
   779
you have to load it with the command {\tt use_thy "Base"}.
lcp@320
   780
lcp@320
   781
One of the simplest nontrivial logics is {\bf minimal logic} of
lcp@320
   782
implication.  Its definition in Isabelle needs no advanced features but
lcp@320
   783
illustrates the overall mechanism nicely:
lcp@320
   784
\begin{ttbox}
lcp@320
   785
Hilbert = Base +
lcp@320
   786
consts
clasohm@1387
   787
  "-->" :: [o, o] => o   (infixr 10)
lcp@320
   788
rules
lcp@320
   789
  K     "P --> Q --> P"
lcp@320
   790
  S     "(P --> Q --> R) --> (P --> Q) --> P --> R"
lcp@320
   791
  MP    "[| P --> Q; P |] ==> Q"
lcp@320
   792
end
lcp@320
   793
\end{ttbox}
lcp@332
   794
After loading this definition from the file {\tt Hilbert.thy}, you can
lcp@320
   795
start to prove theorems in the logic:
lcp@320
   796
\begin{ttbox}
lcp@320
   797
goal Hilbert.thy "P --> P";
lcp@320
   798
{\out Level 0}
lcp@320
   799
{\out P --> P}
lcp@320
   800
{\out  1.  P --> P}
lcp@320
   801
\ttbreak
lcp@320
   802
by (resolve_tac [Hilbert.MP] 1);
lcp@320
   803
{\out Level 1}
lcp@320
   804
{\out P --> P}
lcp@320
   805
{\out  1.  ?P --> P --> P}
lcp@320
   806
{\out  2.  ?P}
lcp@320
   807
\ttbreak
lcp@320
   808
by (resolve_tac [Hilbert.MP] 1);
lcp@320
   809
{\out Level 2}
lcp@320
   810
{\out P --> P}
lcp@320
   811
{\out  1.  ?P1 --> ?P --> P --> P}
lcp@320
   812
{\out  2.  ?P1}
lcp@320
   813
{\out  3.  ?P}
lcp@320
   814
\ttbreak
lcp@320
   815
by (resolve_tac [Hilbert.S] 1);
lcp@320
   816
{\out Level 3}
lcp@320
   817
{\out P --> P}
lcp@320
   818
{\out  1.  P --> ?Q2 --> P}
lcp@320
   819
{\out  2.  P --> ?Q2}
lcp@320
   820
\ttbreak
lcp@320
   821
by (resolve_tac [Hilbert.K] 1);
lcp@320
   822
{\out Level 4}
lcp@320
   823
{\out P --> P}
lcp@320
   824
{\out  1.  P --> ?Q2}
lcp@320
   825
\ttbreak
lcp@320
   826
by (resolve_tac [Hilbert.K] 1);
lcp@320
   827
{\out Level 5}
lcp@320
   828
{\out P --> P}
lcp@320
   829
{\out No subgoals!}
lcp@320
   830
\end{ttbox}
lcp@320
   831
As we can see, this Hilbert-style formulation of minimal logic is easy to
lcp@320
   832
define but difficult to use.  The following natural deduction formulation is
lcp@320
   833
better:
lcp@320
   834
\begin{ttbox}
lcp@320
   835
MinI = Base +
lcp@320
   836
consts
clasohm@1387
   837
  "-->" :: [o, o] => o   (infixr 10)
lcp@320
   838
rules
lcp@320
   839
  impI  "(P ==> Q) ==> P --> Q"
lcp@320
   840
  impE  "[| P --> Q; P |] ==> Q"
lcp@320
   841
end
lcp@320
   842
\end{ttbox}
lcp@320
   843
Note, however, that although the two systems are equivalent, this fact
lcp@320
   844
cannot be proved within Isabelle.  Axioms {\tt S} and {\tt K} can be
lcp@320
   845
derived in {\tt MinI} (exercise!), but {\tt impI} cannot be derived in {\tt
lcp@320
   846
  Hilbert}.  The reason is that {\tt impI} is only an {\bf admissible} rule
lcp@320
   847
in {\tt Hilbert}, something that can only be shown by induction over all
lcp@320
   848
possible proofs in {\tt Hilbert}.
lcp@320
   849
lcp@320
   850
We may easily extend minimal logic with falsity:
lcp@320
   851
\begin{ttbox}
lcp@320
   852
MinIF = MinI +
lcp@320
   853
consts
clasohm@1387
   854
  False :: o
lcp@320
   855
rules
lcp@320
   856
  FalseE "False ==> P"
lcp@320
   857
end
lcp@320
   858
\end{ttbox}
lcp@320
   859
On the other hand, we may wish to introduce conjunction only:
lcp@320
   860
\begin{ttbox}
lcp@320
   861
MinC = Base +
lcp@320
   862
consts
clasohm@1387
   863
  "&" :: [o, o] => o   (infixr 30)
lcp@320
   864
\ttbreak
lcp@320
   865
rules
lcp@320
   866
  conjI  "[| P; Q |] ==> P & Q"
lcp@320
   867
  conjE1 "P & Q ==> P"
lcp@320
   868
  conjE2 "P & Q ==> Q"
lcp@320
   869
end
lcp@320
   870
\end{ttbox}
lcp@320
   871
And if we want to have all three connectives together, we create and load a
wenzelm@3108
   872
theory file consisting of a single line:
lcp@320
   873
\begin{ttbox}
lcp@320
   874
MinIFC = MinIF + MinC
lcp@320
   875
\end{ttbox}
lcp@320
   876
Now we can prove mixed theorems like
lcp@320
   877
\begin{ttbox}
lcp@320
   878
goal MinIFC.thy "P & False --> Q";
lcp@320
   879
by (resolve_tac [MinI.impI] 1);
lcp@320
   880
by (dresolve_tac [MinC.conjE2] 1);
lcp@320
   881
by (eresolve_tac [MinIF.FalseE] 1);
lcp@320
   882
\end{ttbox}
lcp@320
   883
Try this as an exercise!