333 |
333 |
334 section {* Mixfix annotations \label{sec:mixfix} *} |
334 section {* Mixfix annotations \label{sec:mixfix} *} |
335 |
335 |
336 text {* Mixfix annotations specify concrete \emph{inner syntax} of |
336 text {* Mixfix annotations specify concrete \emph{inner syntax} of |
337 Isabelle types and terms. Locally fixed parameters in toplevel |
337 Isabelle types and terms. Locally fixed parameters in toplevel |
338 theorem statements, locale specifications etc.\ also admit mixfix |
338 theorem statements, locale and class specifications also admit |
339 annotations. |
339 mixfix annotations in a fairly uniform manner. A mixfix annotation |
|
340 describes describes the concrete syntax, the translation to abstract |
|
341 syntax, and the pretty printing. Special case annotations provide a |
|
342 simple means of specifying infix operators and binders. |
|
343 |
|
344 Isabelle mixfix syntax is inspired by {\OBJ} \cite{OBJ}. It allows |
|
345 to specify any context-free priority grammar, which is more general |
|
346 than the fixity declarations of ML and Prolog. |
340 |
347 |
341 @{rail " |
348 @{rail " |
342 @{syntax_def mixfix}: '(' mfix ')' |
349 @{syntax_def mixfix}: '(' mfix ')' |
343 ; |
350 ; |
344 @{syntax_def struct_mixfix}: '(' ( mfix | @'structure' ) ')' |
351 @{syntax_def struct_mixfix}: '(' ( mfix | @'structure' ) ')' |
345 ; |
352 ; |
346 |
353 |
347 mfix: @{syntax string} prios? @{syntax nat}? | |
354 mfix: @{syntax template} prios? @{syntax nat}? | |
348 (@'infix' | @'infixl' | @'infixr') @{syntax string} @{syntax nat} | |
355 (@'infix' | @'infixl' | @'infixr') @{syntax template} @{syntax nat} | |
349 @'binder' @{syntax string} prios? @{syntax nat} |
356 @'binder' @{syntax template} prios? @{syntax nat} |
|
357 ; |
|
358 template: string |
350 ; |
359 ; |
351 prios: '[' (@{syntax nat} + ',') ']' |
360 prios: '[' (@{syntax nat} + ',') ']' |
352 "} |
361 "} |
353 |
362 |
354 Here the @{syntax string} specifications refer to the actual mixfix |
363 The string given as @{text template} may include literal text, |
355 template, which may include literal text, spacing, blocks, and |
364 spacing, blocks, and arguments (denoted by ``@{text _}''); the |
356 arguments (denoted by ``@{text _}''); the special symbol |
365 special symbol ``@{verbatim "\<index>"}'' (printed as ``@{text "\<index>"}'') |
357 ``@{verbatim "\<index>"}'' (printed as ``@{text "\<index>"}'') represents an index |
366 represents an index argument that specifies an implicit structure |
358 argument that specifies an implicit structure reference (see also |
367 reference (see also \secref{sec:locale}). Infix and binder |
359 \secref{sec:locale}). Infix and binder declarations provide common |
368 declarations provide common abbreviations for particular mixfix |
360 abbreviations for particular mixfix declarations. So in practice, |
369 declarations. So in practice, mixfix templates mostly degenerate to |
361 mixfix templates mostly degenerate to literal text for concrete |
370 literal text for concrete syntax, such as ``@{verbatim "++"}'' for |
362 syntax, such as ``@{verbatim "++"}'' for an infix symbol. |
371 an infix symbol. |
363 |
372 *} |
364 \medskip In full generality, mixfix declarations work as follows. |
373 |
365 Suppose a constant @{text "c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} is |
374 |
366 annotated by @{text "(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)"}, where @{text |
375 subsection {* The general mixfix form *} |
367 "mixfix"} is a string @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of |
376 |
368 delimiters that surround argument positions as indicated by |
377 text {* In full generality, mixfix declarations work as follows. |
369 underscores. |
378 Suppose a constant @{text "c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} is annotated by |
|
379 @{text "(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)"}, where @{text "mixfix"} is a string |
|
380 @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of delimiters that surround |
|
381 argument positions as indicated by underscores. |
370 |
382 |
371 Altogether this determines a production for a context-free priority |
383 Altogether this determines a production for a context-free priority |
372 grammar, where for each argument @{text "i"} the syntactic category |
384 grammar, where for each argument @{text "i"} the syntactic category |
373 is determined by @{text "\<tau>\<^sub>i"} (with priority @{text "p\<^sub>i"}), and |
385 is determined by @{text "\<tau>\<^sub>i"} (with priority @{text "p\<^sub>i"}), and |
374 the result category is determined from @{text "\<tau>"} (with |
386 the result category is determined from @{text "\<tau>"} (with |
435 stands for the string of spaces (zero or more) right after the |
447 stands for the string of spaces (zero or more) right after the |
436 slash. These spaces are printed if the break is \emph{not} taken. |
448 slash. These spaces are printed if the break is \emph{not} taken. |
437 |
449 |
438 \end{description} |
450 \end{description} |
439 |
451 |
440 For example, the template @{verbatim "(_ +/ _)"} specifies an infix |
|
441 operator. There are two argument positions; the delimiter |
|
442 @{verbatim "+"} is preceded by a space and followed by a space or |
|
443 line break; the entire phrase is a pretty printing block. |
|
444 |
|
445 The general idea of pretty printing with blocks and breaks is also |
452 The general idea of pretty printing with blocks and breaks is also |
446 described in \cite{paulson-ml2}; it goes back to \cite{Oppen:1980}. |
453 described in \cite{paulson-ml2}; it goes back to \cite{Oppen:1980}. |
447 *} |
454 *} |
|
455 |
|
456 |
|
457 subsection {* Infixes *} |
|
458 |
|
459 text {* Infix operators are specified by convenient short forms that |
|
460 abbreviate general mixfix annotations as follows: |
|
461 |
|
462 \begin{center} |
|
463 \begin{tabular}{lll} |
|
464 |
|
465 @{verbatim "("}@{keyword "infix"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"} |
|
466 & @{text "\<mapsto>"} & |
|
467 @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\ |
|
468 @{verbatim "("}@{keyword "infixl"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"} |
|
469 & @{text "\<mapsto>"} & |
|
470 @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\ |
|
471 @{verbatim "("}@{keyword "infixr"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"} |
|
472 & @{text "\<mapsto>"} & |
|
473 @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\ |
|
474 |
|
475 \end{tabular} |
|
476 \end{center} |
|
477 |
|
478 The mixfix template @{verbatim "\"(_ "}@{text sy}@{verbatim "/ |
|
479 _)\""} specifies two argument positions; the delimiter is preceded |
|
480 by a space and followed by a space or line break; the entire phrase |
|
481 is a pretty printing block. |
|
482 |
|
483 The alternative notation @{verbatim "op"}~@{text sy} is introduced |
|
484 in addition. Thus any infix operator may be written in prefix form |
|
485 (as in ML), independently of the number of arguments in the term. |
|
486 *} |
|
487 |
|
488 |
|
489 subsection {* Binders *} |
|
490 |
|
491 text {* A \emph{binder} is a variable-binding construct such as a |
|
492 quantifier. The idea to formalize @{text "\<forall>x. b"} as @{text "All |
|
493 (\<lambda>x. b)"} for @{text "All :: ('a \<Rightarrow> bool) \<Rightarrow> bool"} already goes back |
|
494 to \cite{church40}. Isabelle declarations of certain higher-order |
|
495 operators may be annotated with @{keyword "binder"} annotations as |
|
496 follows: |
|
497 |
|
498 \begin{center} |
|
499 @{text "c :: "}@{verbatim "\""}@{text "(\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2) \<Rightarrow> \<tau>\<^sub>3"}@{verbatim "\" ("}@{keyword "binder"}@{verbatim " \""}@{text "sy"}@{verbatim "\" ["}@{text "p"}@{verbatim "] "}@{text "q"}@{verbatim ")"} |
|
500 \end{center} |
|
501 |
|
502 This introduces concrete binder syntax @{text "sy x. b"}, where |
|
503 @{text x} is a bound variable of type @{text "\<tau>\<^sub>1"}, the body @{text |
|
504 b} has type @{text "\<tau>\<^sub>2"} and the whole term has type @{text "\<tau>\<^sub>3"}. |
|
505 The optional integer @{text p} specifies the syntactic priority of |
|
506 the body; the default is @{text "q"}, which is also the priority of |
|
507 the whole construct. |
|
508 |
|
509 Internally, the binder syntax is expanded to something like this: |
|
510 \begin{center} |
|
511 @{text "c_binder :: "}@{verbatim "\""}@{text "idts \<Rightarrow> \<tau>\<^sub>2 \<Rightarrow> \<tau>\<^sub>3"}@{verbatim "\" (\"(3"}@{text sy}@{verbatim "_./ _)\" [0, "}@{text "p"}@{verbatim "] "}@{text "q"}@{verbatim ")"} |
|
512 \end{center} |
|
513 |
|
514 Here @{syntax (inner) idts} is the nonterminal symbol for a list of |
|
515 identifiers with optional type constraints (see also |
|
516 \secref{sec:pure-grammar}). The mixfix template @{verbatim |
|
517 "\"(3"}@{text sy}@{verbatim "_./ _)\""} defines argument positions |
|
518 for the bound identifiers and the body, separated by a dot with |
|
519 optional line break; the entire phrase is a pretty printing block of |
|
520 indentation level 3. Note that there is no extra space after @{text |
|
521 "sy"}, so it needs to be included user specification if the binder |
|
522 syntax ends with a token that may be continued by an identifier |
|
523 token at the start of @{syntax (inner) idts}. |
|
524 |
|
525 Furthermore, a syntax translation to transforms @{text "c_binder x\<^sub>1 |
|
526 \<dots> x\<^sub>n b"} into iterated application @{text "c (\<lambda>x\<^sub>1. \<dots> c (\<lambda>x\<^sub>n. b)\<dots>)"}. |
|
527 This works in both directions, for parsing and printing. *} |
448 |
528 |
449 |
529 |
450 section {* Explicit notation \label{sec:notation} *} |
530 section {* Explicit notation \label{sec:notation} *} |
451 |
531 |
452 text {* |
532 text {* |