src/Doc/Isar_Ref/Inner_Syntax.thy
changeset 58724 e5f809f52f26
parent 58618 782f0b662cae
child 58726 cee57ab1f76f
equal deleted inserted replaced
58723:33be43d70147 58724:e5f809f52f26
   286   Mode names can be arbitrary, but the following ones have a specific
   286   Mode names can be arbitrary, but the following ones have a specific
   287   meaning by convention:
   287   meaning by convention:
   288 
   288 
   289   \begin{itemize}
   289   \begin{itemize}
   290 
   290 
   291   \item @{verbatim "\"\""} (the empty string): default mode;
   291   \item @{verbatim \<open>""\<close>} (the empty string): default mode;
   292   implicitly active as last element in the list of modes.
   292   implicitly active as last element in the list of modes.
   293 
   293 
   294   \item @{verbatim input}: dummy print mode that is never active; may
   294   \item @{verbatim input}: dummy print mode that is never active; may
   295   be used to specify notation that is only available for input.
   295   be used to specify notation that is only available for input.
   296 
   296 
   445   abbreviate general mixfix annotations as follows:
   445   abbreviate general mixfix annotations as follows:
   446 
   446 
   447   \begin{center}
   447   \begin{center}
   448   \begin{tabular}{lll}
   448   \begin{tabular}{lll}
   449 
   449 
   450   @{verbatim "("}@{keyword_def "infix"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   450   @{verbatim "("}@{keyword_def "infix"}~@{verbatim \<open>"\<close>}@{text sy}@{verbatim \<open>"\<close>} @{text "p"}@{verbatim ")"}
   451   & @{text "\<mapsto>"} &
   451   & @{text "\<mapsto>"} &
   452   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   452   @{verbatim \<open>("(_\<close>}~@{text sy}@{verbatim \<open>/ _)" [\<close>}@{text "p + 1"}@{verbatim ","}~@{text "p + 1"}@{verbatim "]"}~@{text "p"}@{verbatim ")"} \\
   453   @{verbatim "("}@{keyword_def "infixl"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   453   @{verbatim "("}@{keyword_def "infixl"}~@{verbatim \<open>"\<close>}@{text sy}@{verbatim \<open>"\<close>} @{text "p"}@{verbatim ")"}
   454   & @{text "\<mapsto>"} &
   454   & @{text "\<mapsto>"} &
   455   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   455   @{verbatim \<open>("(_\<close>}~@{text sy}@{verbatim \<open>/ _)" [\<close>}@{text "p"}@{verbatim ","}~@{text "p + 1"}@{verbatim "]"}~@{text "p"}@{verbatim ")"} \\
   456   @{verbatim "("}@{keyword_def "infixr"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   456   @{verbatim "("}@{keyword_def "infixr"}~@{verbatim \<open>"\<close>}@{text sy}@{verbatim \<open>"\<close>}~@{text "p"}@{verbatim ")"}
   457   & @{text "\<mapsto>"} &
   457   & @{text "\<mapsto>"} &
   458   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   458   @{verbatim \<open>("(_\<close>}~@{text sy}@{verbatim \<open>/ _)" [\<close>}@{text "p + 1"}@{verbatim ","}~@{text "p"}@{verbatim "]"}~@{text "p"}@{verbatim ")"} \\
   459 
   459 
   460   \end{tabular}
   460   \end{tabular}
   461   \end{center}
   461   \end{center}
   462 
   462 
   463   The mixfix template @{verbatim "\"(_ "}@{text sy}@{verbatim "/ _)\""}
   463   The mixfix template @{verbatim \<open>"(_\<close>}~@{text sy}@{verbatim \<open>/ _)"\<close>}
   464   specifies two argument positions; the delimiter is preceded by a
   464   specifies two argument positions; the delimiter is preceded by a
   465   space and followed by a space or line break; the entire phrase is a
   465   space and followed by a space or line break; the entire phrase is a
   466   pretty printing block.
   466   pretty printing block.
   467 
   467 
   468   The alternative notation @{verbatim "op"}~@{text sy} is introduced
   468   The alternative notation @{verbatim "op"}~@{text sy} is introduced
   479   to @{cite church40}.  Isabelle declarations of certain higher-order
   479   to @{cite church40}.  Isabelle declarations of certain higher-order
   480   operators may be annotated with @{keyword_def "binder"} annotations
   480   operators may be annotated with @{keyword_def "binder"} annotations
   481   as follows:
   481   as follows:
   482 
   482 
   483   \begin{center}
   483   \begin{center}
   484   @{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 ")"}
   484   @{text "c :: "}@{verbatim \<open>"\<close>}@{text "(\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2) \<Rightarrow> \<tau>\<^sub>3"}@{verbatim \<open>"  (\<close>}@{keyword "binder"}~@{verbatim \<open>"\<close>}@{text "sy"}@{verbatim \<open>" [\<close>}@{text "p"}@{verbatim "]"}~@{text "q"}@{verbatim ")"}
   485   \end{center}
   485   \end{center}
   486 
   486 
   487   This introduces concrete binder syntax @{text "sy x. b"}, where
   487   This introduces concrete binder syntax @{text "sy x. b"}, where
   488   @{text x} is a bound variable of type @{text "\<tau>\<^sub>1"}, the body @{text
   488   @{text x} is a bound variable of type @{text "\<tau>\<^sub>1"}, the body @{text
   489   b} has type @{text "\<tau>\<^sub>2"} and the whole term has type @{text "\<tau>\<^sub>3"}.
   489   b} has type @{text "\<tau>\<^sub>2"} and the whole term has type @{text "\<tau>\<^sub>3"}.
   491   the body; the default is @{text "q"}, which is also the priority of
   491   the body; the default is @{text "q"}, which is also the priority of
   492   the whole construct.
   492   the whole construct.
   493 
   493 
   494   Internally, the binder syntax is expanded to something like this:
   494   Internally, the binder syntax is expanded to something like this:
   495   \begin{center}
   495   \begin{center}
   496   @{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 ")"}
   496   @{text "c_binder :: "}@{verbatim \<open>"\<close>}@{text "idts \<Rightarrow> \<tau>\<^sub>2 \<Rightarrow> \<tau>\<^sub>3"}@{verbatim \<open>"  ("(3\<close>}@{text sy}@{verbatim \<open>_./ _)" [0,\<close>}~@{text "p"}@{verbatim "]"}~@{text "q"}@{verbatim ")"}
   497   \end{center}
   497   \end{center}
   498 
   498 
   499   Here @{syntax (inner) idts} is the nonterminal symbol for a list of
   499   Here @{syntax (inner) idts} is the nonterminal symbol for a list of
   500   identifiers with optional type constraints (see also
   500   identifiers with optional type constraints (see also
   501   \secref{sec:pure-grammar}).  The mixfix template @{verbatim
   501   \secref{sec:pure-grammar}).  The mixfix template @{verbatim
   502   "\"(3"}@{text sy}@{verbatim "_./ _)\""} defines argument positions
   502   \<open>"(3\<close>}@{text sy}@{verbatim \<open>_./ _)"\<close>} defines argument positions
   503   for the bound identifiers and the body, separated by a dot with
   503   for the bound identifiers and the body, separated by a dot with
   504   optional line break; the entire phrase is a pretty printing block of
   504   optional line break; the entire phrase is a pretty printing block of
   505   indentation level 3.  Note that there is no extra space after @{text
   505   indentation level 3.  Note that there is no extra space after @{text
   506   "sy"}, so it needs to be included user specification if the binder
   506   "sy"}, so it needs to be included user specification if the binder
   507   syntax ends with a token that may be continued by an identifier
   507   syntax ends with a token that may be continued by an identifier
   600     @{syntax_def (inner) tid} & = & @{syntax_ref typefree} \\
   600     @{syntax_def (inner) tid} & = & @{syntax_ref typefree} \\
   601     @{syntax_def (inner) tvar} & = & @{syntax_ref typevar} \\
   601     @{syntax_def (inner) tvar} & = & @{syntax_ref typevar} \\
   602     @{syntax_def (inner) num_token} & = & @{syntax_ref nat} \\
   602     @{syntax_def (inner) num_token} & = & @{syntax_ref nat} \\
   603     @{syntax_def (inner) float_token} & = & @{syntax_ref nat}@{verbatim "."}@{syntax_ref nat} \\
   603     @{syntax_def (inner) float_token} & = & @{syntax_ref nat}@{verbatim "."}@{syntax_ref nat} \\
   604     @{syntax_def (inner) str_token} & = & @{verbatim "''"} @{text "\<dots>"} @{verbatim "''"} \\
   604     @{syntax_def (inner) str_token} & = & @{verbatim "''"} @{text "\<dots>"} @{verbatim "''"} \\
   605     @{syntax_def (inner) string_token} & = & @{verbatim "\""} @{text "\<dots>"} @{verbatim "\""} \\
   605     @{syntax_def (inner) string_token} & = & @{verbatim \<open>"\<close>} @{text "\<dots>"} @{verbatim \<open>"\<close>} \\
   606     @{syntax_def (inner) cartouche} & = & @{verbatim "\<open>"} @{text "\<dots>"} @{verbatim "\<close>"} \\
   606     @{syntax_def (inner) cartouche} & = & @{verbatim "\<open>"} @{text "\<dots>"} @{verbatim "\<close>"} \\
   607   \end{supertabular}
   607   \end{supertabular}
   608   \end{center}
   608   \end{center}
   609 
   609 
   610   The token categories @{syntax (inner) num_token}, @{syntax (inner)
   610   The token categories @{syntax (inner) num_token}, @{syntax (inner)
  1061   least two) subtrees.  Occasional diagnostic output of ASTs uses
  1061   least two) subtrees.  Occasional diagnostic output of ASTs uses
  1062   notation that resembles S-expression of LISP.  Constant atoms are
  1062   notation that resembles S-expression of LISP.  Constant atoms are
  1063   shown as quoted strings, variable atoms as non-quoted strings and
  1063   shown as quoted strings, variable atoms as non-quoted strings and
  1064   applications as a parenthesized list of subtrees.  For example, the
  1064   applications as a parenthesized list of subtrees.  For example, the
  1065   AST
  1065   AST
  1066   @{ML [display] "Ast.Appl
  1066   @{ML [display] \<open>Ast.Appl [Ast.Constant "_abs", Ast.Variable "x", Ast.Variable "t"]\<close>}
  1067   [Ast.Constant \"_abs\", Ast.Variable \"x\", Ast.Variable \"t\"]"}
  1067   is pretty-printed as @{verbatim \<open>("_abs" x t)\<close>}.  Note that
  1068   is pretty-printed as @{verbatim "(\"_abs\" x t)"}.  Note that
       
  1069   @{verbatim "()"} and @{verbatim "(x)"} are excluded as ASTs, because
  1068   @{verbatim "()"} and @{verbatim "(x)"} are excluded as ASTs, because
  1070   they have too few subtrees.
  1069   they have too few subtrees.
  1071 
  1070 
  1072   \medskip AST application is merely a pro-forma mechanism to indicate
  1071   \medskip AST application is merely a pro-forma mechanism to indicate
  1073   certain syntactic structures.  Thus @{verbatim "(c a b)"} could mean
  1072   certain syntactic structures.  Thus @{verbatim "(c a b)"} could mean
  1074   either term application or type application, depending on the
  1073   either term application or type application, depending on the
  1075   syntactic context.
  1074   syntactic context.
  1076 
  1075 
  1077   Nested application like @{verbatim "((\"_abs\" x t) u)"} is also
  1076   Nested application like @{verbatim \<open>(("_abs" x t) u)\<close>} is also
  1078   possible, but ASTs are definitely first-order: the syntax constant
  1077   possible, but ASTs are definitely first-order: the syntax constant
  1079   @{verbatim "\"_abs\""} does not bind the @{verbatim x} in any way.
  1078   @{verbatim \<open>"_abs"\<close>} does not bind the @{verbatim x} in any way.
  1080   Proper bindings are introduced in later stages of the term syntax,
  1079   Proper bindings are introduced in later stages of the term syntax,
  1081   where @{verbatim "(\"_abs\" x t)"} becomes an @{ML Abs} node and
  1080   where @{verbatim \<open>("_abs" x t)\<close>} becomes an @{ML Abs} node and
  1082   occurrences of @{verbatim x} in @{verbatim t} are replaced by bound
  1081   occurrences of @{verbatim x} in @{verbatim t} are replaced by bound
  1083   variables (represented as de-Bruijn indices).
  1082   variables (represented as de-Bruijn indices).
  1084 \<close>
  1083 \<close>
  1085 
  1084 
  1086 
  1085 
  1207   constructor @{text c} (without arguments) to act as purely syntactic
  1206   constructor @{text c} (without arguments) to act as purely syntactic
  1208   type: a nonterminal symbol of the inner syntax.
  1207   type: a nonterminal symbol of the inner syntax.
  1209 
  1208 
  1210   \item @{command "syntax"}~@{text "(mode) c :: \<sigma> (mx)"} augments the
  1209   \item @{command "syntax"}~@{text "(mode) c :: \<sigma> (mx)"} augments the
  1211   priority grammar and the pretty printer table for the given print
  1210   priority grammar and the pretty printer table for the given print
  1212   mode (default @{verbatim "\"\""}). An optional keyword @{keyword_ref
  1211   mode (default @{verbatim \<open>""\<close>}). An optional keyword @{keyword_ref
  1213   "output"} means that only the pretty printer table is affected.
  1212   "output"} means that only the pretty printer table is affected.
  1214 
  1213 
  1215   Following \secref{sec:mixfix}, the mixfix annotation @{text "mx =
  1214   Following \secref{sec:mixfix}, the mixfix annotation @{text "mx =
  1216   template ps q"} together with type @{text "\<sigma> = \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} and
  1215   template ps q"} together with type @{text "\<sigma> = \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} and
  1217   specify a grammar production.  The @{text template} contains
  1216   specify a grammar production.  The @{text template} contains
  1249   identifiers prefixed by a single underscore (e.g.\ @{text
  1248   identifiers prefixed by a single underscore (e.g.\ @{text
  1250   "_foobar"}).  Names should be chosen with care, to avoid clashes
  1249   "_foobar"}).  Names should be chosen with care, to avoid clashes
  1251   with other syntax declarations.
  1250   with other syntax declarations.
  1252 
  1251 
  1253   \medskip The special case of copy production is specified by @{text
  1252   \medskip The special case of copy production is specified by @{text
  1254   "c = "}@{verbatim "\"\""} (empty string).  It means that the
  1253   "c = "}@{verbatim \<open>""\<close>} (empty string).  It means that the
  1255   resulting parse tree @{text "t"} is copied directly, without any
  1254   resulting parse tree @{text "t"} is copied directly, without any
  1256   further decoration.
  1255   further decoration.
  1257 
  1256 
  1258   \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
  1257   \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
  1259   declarations (and translations) resulting from @{text decls}, which
  1258   declarations (and translations) resulting from @{text decls}, which
  1585   input source & AST \\
  1584   input source & AST \\
  1586   \hline
  1585   \hline
  1587   @{text "f x y z"} & @{verbatim "(f x y z)"} \\
  1586   @{text "f x y z"} & @{verbatim "(f x y z)"} \\
  1588   @{text "'a ty"} & @{verbatim "(ty 'a)"} \\
  1587   @{text "'a ty"} & @{verbatim "(ty 'a)"} \\
  1589   @{text "('a, 'b)ty"} & @{verbatim "(ty 'a 'b)"} \\
  1588   @{text "('a, 'b)ty"} & @{verbatim "(ty 'a 'b)"} \\
  1590   @{text "\<lambda>x y z. t"} & @{verbatim "(\"_abs\" x (\"_abs\" y (\"_abs\" z t)))"} \\
  1589   @{text "\<lambda>x y z. t"} & @{verbatim \<open>("_abs" x ("_abs" y ("_abs" z t)))\<close>} \\
  1591   @{text "\<lambda>x :: 'a. t"} & @{verbatim "(\"_abs\" (\"_constrain\" x 'a) t)"} \\
  1590   @{text "\<lambda>x :: 'a. t"} & @{verbatim \<open>("_abs" ("_constrain" x 'a) t)\<close>} \\
  1592   @{text "\<lbrakk>P; Q; R\<rbrakk> \<Longrightarrow> S"} & @{verbatim "(\"==>\" P (\"==>\" Q (\"==>\" R S)))"} \\
  1591   @{text "\<lbrakk>P; Q; R\<rbrakk> \<Longrightarrow> S"} & @{verbatim \<open>("==>" P ("==>" Q ("==>" R S)))\<close>} \\
  1593    @{text "['a, 'b, 'c] \<Rightarrow> 'd"} & @{verbatim "(\"fun\" 'a (\"fun\" 'b (\"fun\" 'c 'd)))"} \\
  1592    @{text "['a, 'b, 'c] \<Rightarrow> 'd"} & @{verbatim \<open>("fun" 'a ("fun" 'b ("fun" 'c 'd)))\<close>} \\
  1594   \end{tabular}
  1593   \end{tabular}
  1595   \end{center}
  1594   \end{center}
  1596 
  1595 
  1597   Note that type and sort constraints may occur in further places ---
  1596   Note that type and sort constraints may occur in further places ---
  1598   translations need to be ready to cope with them.  The built-in
  1597   translations need to be ready to cope with them.  The built-in
  1616   variables or constants (according to the name space), and AST
  1615   variables or constants (according to the name space), and AST
  1617   applications to iterated term applications.
  1616   applications to iterated term applications.
  1618 
  1617 
  1619   The outcome is still a first-order term.  Proper abstractions and
  1618   The outcome is still a first-order term.  Proper abstractions and
  1620   bound variables are introduced by parse translations associated with
  1619   bound variables are introduced by parse translations associated with
  1621   certain syntax constants.  Thus @{verbatim "(_abs x x)"} eventually
  1620   certain syntax constants.  Thus @{verbatim \<open>("_abs" x x)\<close>} eventually
  1622   becomes a de-Bruijn term @{verbatim "Abs (\"x\", _, Bound 0)"}.
  1621   becomes a de-Bruijn term @{verbatim \<open>Abs ("x", _, Bound 0)\<close>}.
  1623 \<close>
  1622 \<close>
  1624 
  1623 
  1625 
  1624 
  1626 subsubsection \<open>Printing of terms\<close>
  1625 subsubsection \<open>Printing of terms\<close>
  1627 
  1626