doc-src/IsarRef/Thy/Inner_Syntax.thy
changeset 46292 4eb48958b50f
parent 46291 a1827b8b45ae
child 46293 f248b5f2783a
equal deleted inserted replaced
46291:a1827b8b45ae 46292:4eb48958b50f
   380   @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of delimiters that surround
   380   @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of delimiters that surround
   381   argument positions as indicated by underscores.
   381   argument positions as indicated by underscores.
   382 
   382 
   383   Altogether this determines a production for a context-free priority
   383   Altogether this determines a production for a context-free priority
   384   grammar, where for each argument @{text "i"} the syntactic category
   384   grammar, where for each argument @{text "i"} the syntactic category
   385   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 the
   386   the result category is determined from @{text "\<tau>"} (with
   386   result category is determined from @{text "\<tau>"} (with priority @{text
   387   priority @{text "p"}).  Priority specifications are optional, with
   387   "p"}).  Priority specifications are optional, with default 0 for
   388   default 0 for arguments and 1000 for the result.
   388   arguments and 1000 for the result.\footnote{Omitting priorities is
       
   389   prone to syntactic ambiguities unless the delimiter tokens determine
       
   390   fully bracketed notation, as in @{text "if _ then _ else _ fi"}.}
   389 
   391 
   390   Since @{text "\<tau>"} may be again a function type, the constant
   392   Since @{text "\<tau>"} may be again a function type, the constant
   391   type scheme may have more argument positions than the mixfix
   393   type scheme may have more argument positions than the mixfix
   392   pattern.  Printing a nested application @{text "c t\<^sub>1 \<dots> t\<^sub>m"} for
   394   pattern.  Printing a nested application @{text "c t\<^sub>1 \<dots> t\<^sub>m"} for
   393   @{text "m > n"} works by attaching concrete notation only to the
   395   @{text "m > n"} works by attaching concrete notation only to the
   460   abbreviate general mixfix annotations as follows:
   462   abbreviate general mixfix annotations as follows:
   461 
   463 
   462   \begin{center}
   464   \begin{center}
   463   \begin{tabular}{lll}
   465   \begin{tabular}{lll}
   464 
   466 
   465   @{verbatim "("}@{keyword "infix"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   467   @{verbatim "("}@{keyword_def "infix"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   466   & @{text "\<mapsto>"} &
   468   & @{text "\<mapsto>"} &
   467   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   469   @{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 ")"}
   470   @{verbatim "("}@{keyword_def "infixl"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   469   & @{text "\<mapsto>"} &
   471   & @{text "\<mapsto>"} &
   470   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   472   @{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 ")"}
   473   @{verbatim "("}@{keyword_def "infixr"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
   472   & @{text "\<mapsto>"} &
   474   & @{text "\<mapsto>"} &
   473   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   475   @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
   474 
   476 
   475   \end{tabular}
   477   \end{tabular}
   476   \end{center}
   478   \end{center}
   477 
   479 
   478   The mixfix template @{verbatim "\"(_ "}@{text sy}@{verbatim "/
   480   The mixfix template @{verbatim "\"(_ "}@{text sy}@{verbatim "/ _)\""}
   479   _)\""} specifies two argument positions; the delimiter is preceded
   481   specifies two argument positions; the delimiter is preceded by a
   480   by a space and followed by a space or line break; the entire phrase
   482   space and followed by a space or line break; the entire phrase is a
   481   is a pretty printing block.
   483   pretty printing block.
   482 
   484 
   483   The alternative notation @{verbatim "op"}~@{text sy} is introduced
   485   The alternative notation @{verbatim "op"}~@{text sy} is introduced
   484   in addition.  Thus any infix operator may be written in prefix form
   486   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.
   487   (as in ML), independently of the number of arguments in the term.
   486 *}
   488 *}
   490 
   492 
   491 text {* A \emph{binder} is a variable-binding construct such as a
   493 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
   494   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
   495   (\<lambda>x. b)"} for @{text "All :: ('a \<Rightarrow> bool) \<Rightarrow> bool"} already goes back
   494   to \cite{church40}.  Isabelle declarations of certain higher-order
   496   to \cite{church40}.  Isabelle declarations of certain higher-order
   495   operators may be annotated with @{keyword "binder"} annotations as
   497   operators may be annotated with @{keyword_def "binder"} annotations
   496   follows:
   498   as follows:
   497 
   499 
   498   \begin{center}
   500   \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 ")"}
   501   @{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}
   502   \end{center}
   501 
   503 
  1022     @{command_def "no_syntax"} & : & @{text "theory \<rightarrow> theory"} \\
  1024     @{command_def "no_syntax"} & : & @{text "theory \<rightarrow> theory"} \\
  1023     @{command_def "translations"} & : & @{text "theory \<rightarrow> theory"} \\
  1025     @{command_def "translations"} & : & @{text "theory \<rightarrow> theory"} \\
  1024     @{command_def "no_translations"} & : & @{text "theory \<rightarrow> theory"} \\
  1026     @{command_def "no_translations"} & : & @{text "theory \<rightarrow> theory"} \\
  1025   \end{matharray}
  1027   \end{matharray}
  1026 
  1028 
       
  1029   Unlike mixfix notation for existing formal entities
       
  1030   (\secref{sec:notation}), raw syntax declarations provide full access
       
  1031   to the priority grammar of the inner syntax.  This includes
       
  1032   additional syntactic categories (via @{command nonterminal}) and
       
  1033   free-form grammar productions (via @{command syntax}).  Additional
       
  1034   syntax translations (or macros, via @{command translations}) are
       
  1035   required to turn resulting parse trees into proper representations
       
  1036   of formal entities again.
       
  1037 
  1027   @{rail "
  1038   @{rail "
  1028     @@{command nonterminal} (@{syntax name} + @'and')
  1039     @@{command nonterminal} (@{syntax name} + @'and')
  1029     ;
  1040     ;
  1030     (@@{command syntax} | @@{command no_syntax}) @{syntax mode}? (@{syntax constdecl} +)
  1041     (@@{command syntax} | @@{command no_syntax}) @{syntax mode}? (@{syntax constdecl} +)
  1031     ;
  1042     ;
  1042 
  1053 
  1043   \item @{command "nonterminal"}~@{text c} declares a type
  1054   \item @{command "nonterminal"}~@{text c} declares a type
  1044   constructor @{text c} (without arguments) to act as purely syntactic
  1055   constructor @{text c} (without arguments) to act as purely syntactic
  1045   type: a nonterminal symbol of the inner syntax.
  1056   type: a nonterminal symbol of the inner syntax.
  1046 
  1057 
  1047   \item @{command "syntax"}~@{text "(mode) decls"} is similar to
  1058   \item @{command "syntax"}~@{text "(mode) c :: \<sigma> (mx)"} augments the
  1048   @{command "consts"}~@{text decls}, except that the actual logical
  1059   priority grammar and the pretty printer table for the given print
  1049   signature extension is omitted.  Thus the context free grammar of
  1060   mode (default @{verbatim "\"\""}). An optional keyword @{keyword_ref
  1050   Isabelle's inner syntax may be augmented in arbitrary ways,
  1061   "output"} means that only the pretty printer table is affected.
  1051   independently of the logic.  The @{text mode} argument refers to the
  1062 
  1052   print mode that the grammar rules belong; unless the @{keyword_ref
  1063   Following \secref{sec:mixfix}, the mixfix annotation @{text "mx =
  1053   "output"} indicator is given, all productions are added both to the
  1064   template ps q"} together with type @{text "\<sigma> = \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} and
  1054   input and output grammar.
  1065   specify a grammar production.  The @{text template} contains
       
  1066   delimiter tokens that surround @{text "n"} argument positions
       
  1067   (@{verbatim "_"}).  The latter correspond to nonterminal symbols
       
  1068   @{text "A\<^sub>i"} derived from the argument types @{text "\<tau>\<^sub>i"} as
       
  1069   follows:
       
  1070   \begin{itemize}
       
  1071 
       
  1072   \item @{text "prop"} if @{text "\<tau>\<^sub>i = prop"}
       
  1073 
       
  1074   \item @{text "logic"} if @{text "\<tau>\<^sub>i = (\<dots>)\<kappa>"} for logical type
       
  1075   constructor @{text "\<kappa> \<noteq> prop"}
       
  1076 
       
  1077   \item @{text any} if @{text "\<tau>\<^sub>i = \<alpha>"} for type variables
       
  1078 
       
  1079   \item @{text "\<kappa>"} if @{text "\<tau>\<^sub>i = \<kappa>"} for nonterminal @{text "\<kappa>"}
       
  1080   (syntactic type constructor)
       
  1081 
       
  1082   \end{itemize}
       
  1083 
       
  1084   Each @{text "A\<^sub>i"} is decorated by priority @{text "p\<^sub>i"} from the
       
  1085   given list @{text "ps"}; misssing priorities default to 0.
       
  1086 
       
  1087   The resulting nonterminal of the production is determined similarly
       
  1088   from type @{text "\<tau>"}, with priority @{text "q"} and default 1000.
       
  1089 
       
  1090   \medskip Parsing via this production produces parse trees @{text
       
  1091   "t\<^sub>1, \<dots>, t\<^sub>n"} for the argument slots.  The resulting parse tree is
       
  1092   composed as @{text "c t\<^sub>1 \<dots> t\<^sub>n"}, by using the syntax constant @{text
       
  1093   "c"} of the syntax declaration.
       
  1094 
       
  1095   Such syntactic constants are invented on the spot, without formal
       
  1096   check wrt.\ existing declarations.  It is conventional to use plain
       
  1097   identifiers prefixed by a single underscore (e.g.\ @{text
       
  1098   "_foobar"}).  Names should be chosen with care, to avoid clashes
       
  1099   with unrelated syntax declarations.
       
  1100 
       
  1101   \medskip The special case of copy production is specified by @{text
       
  1102   "c = "}@{verbatim "\"\""} (empty string).  It means that the
       
  1103   resulting parse tree @{text "t"} is copied directly, without any
       
  1104   further decoration.
  1055 
  1105 
  1056   \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
  1106   \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
  1057   declarations (and translations) resulting from @{text decls}, which
  1107   declarations (and translations) resulting from @{text decls}, which
  1058   are interpreted in the same manner as for @{command "syntax"} above.
  1108   are interpreted in the same manner as for @{command "syntax"} above.
  1059 
  1109