doc-src/Ref/defining.tex
changeset 332 01b87a921967
parent 320 76ae17549558
child 452 395bbf6e55f9
equal deleted inserted replaced
331:13660d5f6a77 332:01b87a921967
   115                 ~~$|$~~ {\tt\ttlbrace} $id$ {\tt,} \dots {\tt,} $id$ {\tt\ttrbrace}
   115                 ~~$|$~~ {\tt\ttlbrace} $id$ {\tt,} \dots {\tt,} $id$ {\tt\ttrbrace}
   116 \end{tabular}
   116 \end{tabular}
   117 \index{*PROP symbol}
   117 \index{*PROP symbol}
   118 \index{*== symbol}\index{*=?= symbol}\index{*==> symbol}
   118 \index{*== symbol}\index{*=?= symbol}\index{*==> symbol}
   119 \index{*:: symbol}\index{*=> symbol}
   119 \index{*:: symbol}\index{*=> symbol}
   120 %index command: percent is permitted, but braces must match!
   120 \index{sort constraints}
       
   121 %the index command: a percent is permitted, but braces must match!
   121 \index{%@{\tt\%} symbol}
   122 \index{%@{\tt\%} symbol}
   122 \index{{}@{\tt\ttlbrace} symbol}\index{{}@{\tt\ttrbrace} symbol}
   123 \index{{}@{\tt\ttlbrace} symbol}\index{{}@{\tt\ttrbrace} symbol}
   123 \index{*[ symbol}\index{*] symbol}
   124 \index{*[ symbol}\index{*] symbol}
   124 \index{*"!"! symbol}
   125 \index{*"!"! symbol}
   125 \index{*"["| symbol}
   126 \index{*"["| symbol}
   162 \begin{warn}
   163 \begin{warn}
   163   In {\tt idts}, note that \verb|x::nat y| is parsed as \verb|x::(nat y)|,
   164   In {\tt idts}, note that \verb|x::nat y| is parsed as \verb|x::(nat y)|,
   164   treating {\tt y} like a type constructor applied to {\tt nat}.  The
   165   treating {\tt y} like a type constructor applied to {\tt nat}.  The
   165   likely result is an error message.  To avoid this interpretation, use
   166   likely result is an error message.  To avoid this interpretation, use
   166   parentheses and write \verb|(x::nat) y|.
   167   parentheses and write \verb|(x::nat) y|.
       
   168   \index{type constraints}\index{*:: symbol}
   167 
   169 
   168   Similarly, \verb|x::nat y::nat| is parsed as \verb|x::(nat y::nat)| and
   170   Similarly, \verb|x::nat y::nat| is parsed as \verb|x::(nat y::nat)| and
   169   yields an error.  The correct form is \verb|(x::nat) (y::nat)|.
   171   yields an error.  The correct form is \verb|(x::nat) (y::nat)|.
   170 \end{warn}
   172 \end{warn}
   171 
   173 
   293 {\out print_translation: "all"}
   295 {\out print_translation: "all"}
   294 {\out print_rules:}
   296 {\out print_rules:}
   295 {\out print_ast_translation: "==>" "_abs" "_idts" "fun"}
   297 {\out print_ast_translation: "==>" "_abs" "_idts" "fun"}
   296 \end{ttbox}
   298 \end{ttbox}
   297 
   299 
   298 As you can see, the output is divided into labeled sections.  The grammar
   300 As you can see, the output is divided into labelled sections.  The grammar
   299 is represented by {\tt lexicon}, {\tt roots} and {\tt prods}.  The rest
   301 is represented by {\tt lexicon}, {\tt roots} and {\tt prods}.  The rest
   300 refers to syntactic translations and macro expansion.  Here is an
   302 refers to syntactic translations and macro expansion.  Here is an
   301 explanation of the various sections.
   303 explanation of the various sections.
   302 \begin{description}
   304 \begin{description}
   303   \item[{\tt lexicon}] lists the delimiters used for lexical
   305   \item[{\tt lexicon}] lists the delimiters used for lexical
   376 
   378 
   377 A simple type is typically declared for each nonterminal symbol.  In
   379 A simple type is typically declared for each nonterminal symbol.  In
   378 first-order logic, type~$i$ stands for terms and~$o$ for formulae.  Only
   380 first-order logic, type~$i$ stands for terms and~$o$ for formulae.  Only
   379 the outermost type constructor is taken into account.  For example, any
   381 the outermost type constructor is taken into account.  For example, any
   380 type of the form $\sigma list$ stands for a list;  productions may refer
   382 type of the form $\sigma list$ stands for a list;  productions may refer
   381 to the symbol {\tt list} and will apply lists of any type.
   383 to the symbol {\tt list} and will apply to lists of any type.
   382 
   384 
   383 The symbol associated with a type is called its {\bf root} since it may
   385 The symbol associated with a type is called its {\bf root} since it may
   384 serve as the root of a parse tree.  Precisely, the root of $(\tau@1, \dots,
   386 serve as the root of a parse tree.  Precisely, the root of $(\tau@1, \dots,
   385 \tau@n)ty$ is $ty$, where $\tau@1$, \ldots, $\tau@n$ are types and $ty$ is
   387 \tau@n)ty$ is $ty$, where $\tau@1$, \ldots, $\tau@n$ are types and $ty$ is
   386 a type constructor.  Type infixes are a special case of this; in
   388 a type constructor.  Type infixes are a special case of this; in
   409 Here is a detailed account of mixfix declarations.  Suppose the following
   411 Here is a detailed account of mixfix declarations.  Suppose the following
   410 line occurs within the {\tt consts} section of a {\tt .thy} file:
   412 line occurs within the {\tt consts} section of a {\tt .thy} file:
   411 \begin{center}
   413 \begin{center}
   412   {\tt $c$ ::\ "$\sigma$" ("$template$" $ps$ $p$)}
   414   {\tt $c$ ::\ "$\sigma$" ("$template$" $ps$ $p$)}
   413 \end{center}
   415 \end{center}
   414 This constant declaration and mixfix annotation is interpreted as follows:
   416 This constant declaration and mixfix annotation are interpreted as follows:
   415 \begin{itemize}\index{productions}
   417 \begin{itemize}\index{productions}
   416 \item The string {\tt $c$} is the name of the constant associated with the
   418 \item The string {\tt $c$} is the name of the constant associated with the
   417   production; unless it is a valid identifier, it must be enclosed in
   419   production; unless it is a valid identifier, it must be enclosed in
   418   quotes.  If $c$ is empty (given as~{\tt ""}) then this is a copy
   420   quotes.  If $c$ is empty (given as~{\tt ""}) then this is a copy
   419   production.\index{productions!copy} Otherwise, parsing an instance of the
   421   production.\index{productions!copy} Otherwise, parsing an instance of the
   486   "*" :: "[exp, exp] => exp"  ("_ * _"  [3, 2] 2)
   488   "*" :: "[exp, exp] => exp"  ("_ * _"  [3, 2] 2)
   487   "-" :: "exp => exp"         ("- _"    [3] 3)
   489   "-" :: "exp => exp"         ("- _"    [3] 3)
   488 end
   490 end
   489 \end{ttbox}
   491 \end{ttbox}
   490 The {\tt arities} declaration causes {\tt exp} to be added as a new root.
   492 The {\tt arities} declaration causes {\tt exp} to be added as a new root.
   491 If you put this into a file {\tt exp.thy} and load it via {\tt
   493 If you put this into a file {\tt EXP.thy} and load it via {\tt
   492   use_thy "EXP"}, you can run some tests:
   494   use_thy "EXP"}, you can run some tests:
   493 \begin{ttbox}
   495 \begin{ttbox}
   494 val read_exp = Syntax.test_read (syn_of EXP.thy) "exp";
   496 val read_exp = Syntax.test_read (syn_of EXP.thy) "exp";
   495 {\out val it = fn : string -> unit}
   497 {\out val it = fn : string -> unit}
   496 read_exp "0 * 0 * 0 * 0 + 0 + 0 + 0";
   498 read_exp "0 * 0 * 0 * 0 + 0 + 0 + 0";
   605 \begin{ttbox}
   607 \begin{ttbox}
   606 \(c\)    :: "(\(\tau@1\) => \(\tau@2\)) => \(\tau@3\)"
   608 \(c\)    :: "(\(\tau@1\) => \(\tau@2\)) => \(\tau@3\)"
   607 "\(\Q\)"\hskip-3pt  :: "[idts, \(\tau@2\)] => \(\tau@3\)"   ("(3\(\Q\)_./ _)" \(p\))
   609 "\(\Q\)"\hskip-3pt  :: "[idts, \(\tau@2\)] => \(\tau@3\)"   ("(3\(\Q\)_./ _)" \(p\))
   608 \end{ttbox}
   610 \end{ttbox}
   609 Here \ndx{idts} is the nonterminal symbol for a list of identifiers with
   611 Here \ndx{idts} is the nonterminal symbol for a list of identifiers with
       
   612 \index{type constraints}
   610 optional type constraints (see Fig.\ts\ref{fig:pure_gram}).  The
   613 optional type constraints (see Fig.\ts\ref{fig:pure_gram}).  The
   611 declaration also installs a parse translation\index{translations!parse}
   614 declaration also installs a parse translation\index{translations!parse}
   612 for~$\Q$ and a print translation\index{translations!print} for~$c$ to
   615 for~$\Q$ and a print translation\index{translations!print} for~$c$ to
   613 translate between the internal and external forms.
   616 translate between the internal and external forms.
   614 
   617 
   655 consts
   658 consts
   656   Trueprop :: "o => prop"   ("_" 5)
   659   Trueprop :: "o => prop"   ("_" 5)
   657 end
   660 end
   658 \end{ttbox}
   661 \end{ttbox}
   659 The constant \cdx{Trueprop} (the name is arbitrary) acts as an invisible
   662 The constant \cdx{Trueprop} (the name is arbitrary) acts as an invisible
   660 coercion function.  Assuming this definition resides in a file {\tt base.thy},
   663 coercion function.  Assuming this definition resides in a file {\tt Base.thy},
   661 you have to load it with the command {\tt use_thy "Base"}.
   664 you have to load it with the command {\tt use_thy "Base"}.
   662 
   665 
   663 One of the simplest nontrivial logics is {\bf minimal logic} of
   666 One of the simplest nontrivial logics is {\bf minimal logic} of
   664 implication.  Its definition in Isabelle needs no advanced features but
   667 implication.  Its definition in Isabelle needs no advanced features but
   665 illustrates the overall mechanism nicely:
   668 illustrates the overall mechanism nicely:
   671   K     "P --> Q --> P"
   674   K     "P --> Q --> P"
   672   S     "(P --> Q --> R) --> (P --> Q) --> P --> R"
   675   S     "(P --> Q --> R) --> (P --> Q) --> P --> R"
   673   MP    "[| P --> Q; P |] ==> Q"
   676   MP    "[| P --> Q; P |] ==> Q"
   674 end
   677 end
   675 \end{ttbox}
   678 \end{ttbox}
   676 After loading this definition from the file {\tt hilbert.thy}, you can
   679 After loading this definition from the file {\tt Hilbert.thy}, you can
   677 start to prove theorems in the logic:
   680 start to prove theorems in the logic:
   678 \begin{ttbox}
   681 \begin{ttbox}
   679 goal Hilbert.thy "P --> P";
   682 goal Hilbert.thy "P --> P";
   680 {\out Level 0}
   683 {\out Level 0}
   681 {\out P --> P}
   684 {\out P --> P}