710 There are four kinds of translation functions, with one of these |
710 There are four kinds of translation functions, with one of these |
711 coming in two variants. Each such function is associated with a name, |
711 coming in two variants. Each such function is associated with a name, |
712 which triggers calls to it. Such names can be constants (logical or |
712 which triggers calls to it. Such names can be constants (logical or |
713 syntactic) or type constructors. |
713 syntactic) or type constructors. |
714 |
714 |
715 {\tt print_syntax} displays the sets of names associated with the translation |
715 Function {\tt print_syntax} displays the sets of names associated with the |
716 functions of a theory under \texttt{parse_ast_translation}, etc. You can add |
716 translation functions of a theory under \texttt{parse_ast_translation}, etc. |
717 new ones via the {\tt ML} section\index{*ML section} of a theory definition |
717 You can add new ones via the {\tt ML} section\index{*ML section} of a theory |
718 file. There may never be more than one function of the same kind per name. |
718 definition file. Even though the {\tt ML} section is the very last part of |
719 Even though the {\tt ML} section is the very last part of the file, newly |
719 the file, newly installed translation functions are already effective when |
720 installed translation functions are already effective when processing all of |
720 processing all of the preceding sections. |
721 the preceding sections. |
|
722 |
721 |
723 The {\tt ML} section's contents are simply copied verbatim near the |
722 The {\tt ML} section's contents are simply copied verbatim near the |
724 beginning of the \ML\ file generated from a theory definition file. |
723 beginning of the \ML\ file generated from a theory definition file. |
725 Definitions made here are accessible as components of an \ML\ |
724 Definitions made here are accessible as components of an \ML\ |
726 structure; to make some parts private, use an \ML{} {\tt local} |
725 structure; to make some parts private, use an \ML{} {\tt local} |
755 |
754 |
756 Regardless of whether they act on terms or \AST{}s, translation |
755 Regardless of whether they act on terms or \AST{}s, translation |
757 functions called during the parsing process differ from those for |
756 functions called during the parsing process differ from those for |
758 printing more fundamentally in their overall behaviour: |
757 printing more fundamentally in their overall behaviour: |
759 \begin{description} |
758 \begin{description} |
760 \item[Parse translations] are applied bottom-up. The arguments are already |
759 \item[Parse translations] are applied bottom-up. The arguments are already in |
761 in translated form. The translations must not fail; exceptions trigger |
760 translated form. The translations must not fail; exceptions trigger an |
762 an error message. |
761 error message. There may never be more than one function associated with |
763 |
762 any syntactic name. |
|
763 |
764 \item[Print translations] are applied top-down. They are supplied with |
764 \item[Print translations] are applied top-down. They are supplied with |
765 arguments that are partly still in internal form. The result again |
765 arguments that are partly still in internal form. The result again |
766 undergoes translation; therefore a print translation should not introduce |
766 undergoes translation; therefore a print translation should not introduce as |
767 as head the very constant that invoked it. The function may raise |
767 head the very constant that invoked it. The function may raise exception |
768 exception \xdx{Match} to indicate failure; in this event it has no |
768 \xdx{Match} to indicate failure; in this event it has no effect. Multiple |
769 effect. |
769 functions associated with some syntactic name are tried in an unspecified |
|
770 order. |
770 \end{description} |
771 \end{description} |
771 |
772 |
772 Only constant atoms --- constructor \ttindex{Constant} for \AST{}s and |
773 Only constant atoms --- constructor \ttindex{Constant} for \AST{}s and |
773 \ttindex{Const} for terms --- can invoke translation functions. This |
774 \ttindex{Const} for terms --- can invoke translation functions. This |
774 causes another difference between parsing and printing. |
775 causes another difference between parsing and printing. |
881 be well-behaved in this respect. Free identifiers introduced to |
882 be well-behaved in this respect. Free identifiers introduced to |
882 represent bound variables have to be marked appropriately (cf.\ the |
883 represent bound variables have to be marked appropriately (cf.\ the |
883 example at the end of \S\ref{sec:tr_funs}). |
884 example at the end of \S\ref{sec:tr_funs}). |
884 |
885 |
885 \medskip Token translations may be installed by declaring the |
886 \medskip Token translations may be installed by declaring the |
886 \ttindex{token_translation} value within the \texttt{ML} section of a |
887 \ttindex{token_translation} value within the \texttt{ML} section of a theory |
887 theory definition file: |
888 definition file: |
888 \begin{ttbox} |
889 \begin{ttbox} |
889 val token_translation: (string * string * (string -> string * int)) list |
890 val token_translation: (string * string * (string -> string * real)) list |
890 \end{ttbox}\index{token_translation} |
891 \end{ttbox}\index{token_translation} |
891 The elements of this list are of the form $(m, c, f)$, where $m$ is a |
892 The elements of this list are of the form $(m, c, f)$, where $m$ is a print |
892 print mode identifier, $c$ a token class, and $f\colon string \to |
893 mode identifier, $c$ a token class, and $f\colon string \to string \times |
893 string \times int$ the actual translation function. Assuming that $x$ |
894 real$ the actual translation function. Assuming that $x$ is of identifier |
894 is of identifier class $c$, and print mode $m$ is the first one of all |
895 class $c$, and print mode $m$ is the first (active) mode providing some |
895 currently active modes that provide some translation for $c$, then $x$ |
896 translation for $c$, then $x$ is output according to $f(x) = (x', len)$. |
896 is output according to $f(x) = (x', len)$. Thereby $x'$ is the |
897 Thereby $x'$ is the modified identifier name and $len$ its visual length in |
897 modified identifier name and $len$ its visual length approximated in |
898 terms of characters (e.g.\ length 1.0 would correspond to $1/2$\,em in |
898 terms of whole characters. Thus $x'$ may include non-printing parts |
899 \LaTeX). Thus $x'$ may include non-printing parts like control sequences or |
899 like control sequences or markup information for typesetting systems. |
900 markup information for typesetting systems. |
900 |
901 |
901 %FIXME example (?) |
|
902 |
902 |
903 \index{token translations|)} |
903 \index{token translations|)} |
904 |
904 |
905 |
905 |
906 %%% Local Variables: |
906 %%% Local Variables: |