doc-src/IsarRef/Thy/Inner_Syntax.thy
changeset 48816 754b09cd616f
parent 48792 4aa5b965f70e
equal deleted inserted replaced
48815:eed6698b2ba0 48816:754b09cd616f
  1050 
  1050 
  1051   These intermediate syntax tree formats eventually lead to a pre-term
  1051   These intermediate syntax tree formats eventually lead to a pre-term
  1052   with all names and binding scopes resolved, but most type
  1052   with all names and binding scopes resolved, but most type
  1053   information still missing.  Explicit type constraints might be given by
  1053   information still missing.  Explicit type constraints might be given by
  1054   the user, or implicit position information by the system --- both
  1054   the user, or implicit position information by the system --- both
  1055   needs to be passed-through carefully by syntax transformations.
  1055   need to be passed-through carefully by syntax transformations.
  1056 
  1056 
  1057   Pre-terms are further processed by the so-called \emph{check} and
  1057   Pre-terms are further processed by the so-called \emph{check} and
  1058   \emph{unckeck} phases that are intertwined with type-inference (see
  1058   \emph{unckeck} phases that are intertwined with type-inference (see
  1059   also \cite{isabelle-implementation}).  The latter allows to operate
  1059   also \cite{isabelle-implementation}).  The latter allows to operate
  1060   on higher-order abstract syntax with proper binding and type
  1060   on higher-order abstract syntax with proper binding and type
  1168   \item Input of term constants (or fixed variables) that are
  1168   \item Input of term constants (or fixed variables) that are
  1169   introduced by concrete syntax via @{command notation}: the
  1169   introduced by concrete syntax via @{command notation}: the
  1170   correspondence of a particular grammar production to some known term
  1170   correspondence of a particular grammar production to some known term
  1171   entity is preserved.
  1171   entity is preserved.
  1172 
  1172 
  1173   \item Input type constants (constructors) and type classes ---
  1173   \item Input of type constants (constructors) and type classes ---
  1174   thanks to explicit syntactic distinction independently on the
  1174   thanks to explicit syntactic distinction independently on the
  1175   context.
  1175   context.
  1176 
  1176 
  1177   \item Output of term constants, type constants, type classes ---
  1177   \item Output of term constants, type constants, type classes ---
  1178   this information is already available from the internal term to be
  1178   this information is already available from the internal term to be
  1179   printed.
  1179   printed.
  1180 
  1180 
  1181   \end{itemize}
  1181   \end{itemize}
  1182 
  1182 
  1183   In other words, syntax transformations that operate on input terms
  1183   In other words, syntax transformations that operate on input terms
  1184   written as prefix applications are difficult to achieve.  Luckily,
  1184   written as prefix applications are difficult to make robust.
  1185   this case rarely occurs in practice, because syntax forms to be
  1185   Luckily, this case rarely occurs in practice, because syntax forms
  1186   translated usually correspond to some bits of concrete notation. *}
  1186   to be translated usually correspond to some bits of concrete
       
  1187   notation. *}
  1187 
  1188 
  1188 
  1189 
  1189 subsection {* Raw syntax and translations \label{sec:syn-trans} *}
  1190 subsection {* Raw syntax and translations \label{sec:syn-trans} *}
  1190 
  1191 
  1191 text {*
  1192 text {*
  1269 
  1270 
  1270   Such syntactic constants are invented on the spot, without formal
  1271   Such syntactic constants are invented on the spot, without formal
  1271   check wrt.\ existing declarations.  It is conventional to use plain
  1272   check wrt.\ existing declarations.  It is conventional to use plain
  1272   identifiers prefixed by a single underscore (e.g.\ @{text
  1273   identifiers prefixed by a single underscore (e.g.\ @{text
  1273   "_foobar"}).  Names should be chosen with care, to avoid clashes
  1274   "_foobar"}).  Names should be chosen with care, to avoid clashes
  1274   with unrelated syntax declarations.
  1275   with other syntax declarations.
  1275 
  1276 
  1276   \medskip The special case of copy production is specified by @{text
  1277   \medskip The special case of copy production is specified by @{text
  1277   "c = "}@{verbatim "\"\""} (empty string).  It means that the
  1278   "c = "}@{verbatim "\"\""} (empty string).  It means that the
  1278   resulting parse tree @{text "t"} is copied directly, without any
  1279   resulting parse tree @{text "t"} is copied directly, without any
  1279   further decoration.
  1280   further decoration.
  1282   declarations (and translations) resulting from @{text decls}, which
  1283   declarations (and translations) resulting from @{text decls}, which
  1283   are interpreted in the same manner as for @{command "syntax"} above.
  1284   are interpreted in the same manner as for @{command "syntax"} above.
  1284 
  1285 
  1285   \item @{command "translations"}~@{text rules} specifies syntactic
  1286   \item @{command "translations"}~@{text rules} specifies syntactic
  1286   translation rules (i.e.\ macros) as first-order rewrite rules on
  1287   translation rules (i.e.\ macros) as first-order rewrite rules on
  1287   ASTs (see also \secref{sec:ast}).  The theory context maintains two
  1288   ASTs (\secref{sec:ast}).  The theory context maintains two
  1288   independent lists translation rules: parse rules (@{verbatim "=>"}
  1289   independent lists translation rules: parse rules (@{verbatim "=>"}
  1289   or @{text "\<rightharpoonup>"}) and print rules (@{verbatim "<="} or @{text "\<leftharpoondown>"}).
  1290   or @{text "\<rightharpoonup>"}) and print rules (@{verbatim "<="} or @{text "\<leftharpoondown>"}).
  1290   For convenience, both can be specified simultaneously as parse~/
  1291   For convenience, both can be specified simultaneously as parse~/
  1291   print rules (@{verbatim "=="} or @{text "\<rightleftharpoons>"}).
  1292   print rules (@{verbatim "=="} or @{text "\<rightleftharpoons>"}).
  1292 
  1293 
  1410   until none are applicable.  This works yoyo-like: top-down,
  1411   until none are applicable.  This works yoyo-like: top-down,
  1411   bottom-up, top-down, etc.  At each subtree position, rules are
  1412   bottom-up, top-down, etc.  At each subtree position, rules are
  1412   chosen in order of appearance in the theory definitions.
  1413   chosen in order of appearance in the theory definitions.
  1413 
  1414 
  1414   The configuration options @{attribute syntax_ast_trace} and
  1415   The configuration options @{attribute syntax_ast_trace} and
  1415   @{attribute syntax_ast_stats} might help understand this process
  1416   @{attribute syntax_ast_stats} might help to understand this process
  1416   and diagnose problems.
  1417   and diagnose problems.
  1417 
  1418 
  1418   \begin{warn}
  1419   \begin{warn}
  1419   If syntax translation rules work incorrectly, the output of
  1420   If syntax translation rules work incorrectly, the output of
  1420   @{command_ref print_syntax} with its \emph{rules} sections reveals the
  1421   @{command_ref print_syntax} with its \emph{rules} sections reveals the
  1482   @{command typed_print_translation} & : @{ML_type "(string * (typ -> term list -> term)) list"} \\
  1483   @{command typed_print_translation} & : @{ML_type "(string * (typ -> term list -> term)) list"} \\
  1483   @{command print_ast_translation} & : @{ML_type "(string * (Ast.ast list -> Ast.ast)) list"} \\
  1484   @{command print_ast_translation} & : @{ML_type "(string * (Ast.ast list -> Ast.ast)) list"} \\
  1484   \end{tabular}}
  1485   \end{tabular}}
  1485   \medskip
  1486   \medskip
  1486 
  1487 
  1487   The argument list consist of @{text "(c, tr)"} pairs, where @{text
  1488   The argument list consists of @{text "(c, tr)"} pairs, where @{text
  1488   "c"} is the syntax name of the syntax constant, term constant or
  1489   "c"} is the syntax name of the formal entity involved, and @{text
  1489   type constructor involved, and @{text "tr"} a function that
  1490   "tr"} a function that translates a syntax form @{text "c args"} into
  1490   translates a syntax form @{text "c args"} into @{text "tr args"}.
  1491   @{text "tr args"}.  The ML naming convention for parse translations
  1491   For print translations, the naming convention for such functions is
  1492   is @{text "c_tr"} and for print translations @{text "c_tr'"}.
  1492   @{text "tr'"} instead of @{text "tr"}.
       
  1493 
  1493 
  1494   The @{command_ref print_syntax} command displays the sets of names
  1494   The @{command_ref print_syntax} command displays the sets of names
  1495   associated with the translation functions of a theory under @{text
  1495   associated with the translation functions of a theory under @{text
  1496   "parse_ast_translation"} etc.
  1496   "parse_ast_translation"} etc.
  1497 
  1497 
  1519 *}
  1519 *}
  1520 
  1520 
  1521 
  1521 
  1522 subsubsection {* The translation strategy *}
  1522 subsubsection {* The translation strategy *}
  1523 
  1523 
  1524 text {* The different kinds of translation functions are called during
  1524 text {* The different kinds of translation functions are invoked during
  1525   the transformations between parse trees, ASTs and syntactic terms
  1525   the transformations between parse trees, ASTs and syntactic terms
  1526   (cf.\ \figref{fig:parse-print}).  Whenever a combination of the form
  1526   (cf.\ \figref{fig:parse-print}).  Whenever a combination of the form
  1527   @{text "c x\<^sub>1 \<dots> x\<^sub>n"} is encountered, and a translation function
  1527   @{text "c x\<^sub>1 \<dots> x\<^sub>n"} is encountered, and a translation function
  1528   @{text "f"} of appropriate kind is declared for @{text "c"}, the
  1528   @{text "f"} of appropriate kind is declared for @{text "c"}, the
  1529   result is produced by evaluation of @{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} in ML.
  1529   result is produced by evaluation of @{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} in ML.
  1534   For term translations, the arguments are terms and a combination has
  1534   For term translations, the arguments are terms and a combination has
  1535   the form @{ML Const}~@{text "(c, \<tau>)"} or @{ML Const}~@{text "(c, \<tau>)
  1535   the form @{ML Const}~@{text "(c, \<tau>)"} or @{ML Const}~@{text "(c, \<tau>)
  1536   $ x\<^sub>1 $ \<dots> $ x\<^sub>n"}.  Terms allow more sophisticated transformations
  1536   $ x\<^sub>1 $ \<dots> $ x\<^sub>n"}.  Terms allow more sophisticated transformations
  1537   than ASTs do, typically involving abstractions and bound
  1537   than ASTs do, typically involving abstractions and bound
  1538   variables. \emph{Typed} print translations may even peek at the type
  1538   variables. \emph{Typed} print translations may even peek at the type
  1539   @{text "\<tau>"} of the constant they are invoked on.
  1539   @{text "\<tau>"} of the constant they are invoked on, although that information
       
  1540   may be inaccurate.
  1540 
  1541 
  1541   Regardless of whether they act on ASTs or terms, translation
  1542   Regardless of whether they act on ASTs or terms, translation
  1542   functions called during the parsing process differ from those for
  1543   functions called during the parsing process differ from those for
  1543   printing in their overall behaviour:
  1544   printing in their overall behaviour:
  1544 
  1545