src/Doc/Sugar/Sugar.thy
changeset 69597 ff784d5a5bfb
parent 69505 cc2d676d5395
child 76987 4c275405faae
equal deleted inserted replaced
69596:c8a2755bf220 69597:ff784d5a5bfb
    53 
    53 
    54 \section{HOL syntax}
    54 \section{HOL syntax}
    55 
    55 
    56 \subsection{Logic}
    56 \subsection{Logic}
    57 
    57 
    58 The formula @{prop[source]"\<not>(\<exists>x. P x)"} is typeset as @{prop"\<not>(\<exists>x. P x)"}.
    58 The formula @{prop[source]"\<not>(\<exists>x. P x)"} is typeset as \<^prop>\<open>\<not>(\<exists>x. P x)\<close>.
    59 
    59 
    60 The predefined constructs \<open>if\<close>, \<open>let\<close> and
    60 The predefined constructs \<open>if\<close>, \<open>let\<close> and
    61 \<open>case\<close> are set in sans serif font to distinguish them from
    61 \<open>case\<close> are set in sans serif font to distinguish them from
    62 other functions. This improves readability:
    62 other functions. This improves readability:
    63 \begin{itemize}
    63 \begin{itemize}
    64 \item @{term"if b then e\<^sub>1 else e\<^sub>2"} instead of \<open>if b then e\<^sub>1 else e\<^sub>2\<close>.
    64 \item \<^term>\<open>if b then e\<^sub>1 else e\<^sub>2\<close> instead of \<open>if b then e\<^sub>1 else e\<^sub>2\<close>.
    65 \item @{term"let x = e\<^sub>1 in e\<^sub>2"} instead of \<open>let x = e\<^sub>1 in e\<^sub>2\<close>.
    65 \item \<^term>\<open>let x = e\<^sub>1 in e\<^sub>2\<close> instead of \<open>let x = e\<^sub>1 in e\<^sub>2\<close>.
    66 \item @{term"case x of True \<Rightarrow> e\<^sub>1 | False \<Rightarrow> e\<^sub>2"} instead of\\
    66 \item \<^term>\<open>case x of True \<Rightarrow> e\<^sub>1 | False \<Rightarrow> e\<^sub>2\<close> instead of\\
    67       \<open>case x of True \<Rightarrow> e\<^sub>1 | False \<Rightarrow> e\<^sub>2\<close>.
    67       \<open>case x of True \<Rightarrow> e\<^sub>1 | False \<Rightarrow> e\<^sub>2\<close>.
    68 \end{itemize}
    68 \end{itemize}
    69 
    69 
    70 \subsection{Sets}
    70 \subsection{Sets}
    71 
    71 
    72 Although set syntax in HOL is already close to
    72 Although set syntax in HOL is already close to
    73 standard, we provide a few further improvements:
    73 standard, we provide a few further improvements:
    74 \begin{itemize}
    74 \begin{itemize}
    75 \item @{term"{x. P}"} instead of \<open>{x. P}\<close>.
    75 \item \<^term>\<open>{x. P}\<close> instead of \<open>{x. P}\<close>.
    76 \item @{term"{}"} instead of \<open>{}\<close>, where
    76 \item \<^term>\<open>{}\<close> instead of \<open>{}\<close>, where
    77  @{term"{}"} is also input syntax.
    77  \<^term>\<open>{}\<close> is also input syntax.
    78 \item @{term"insert a (insert b (insert c M))"} instead of \<open>insert a (insert b (insert c M))\<close>.
    78 \item \<^term>\<open>insert a (insert b (insert c M))\<close> instead of \<open>insert a (insert b (insert c M))\<close>.
    79 \item @{term"card A"} instead of \<open>card A\<close>.
    79 \item \<^term>\<open>card A\<close> instead of \<open>card A\<close>.
    80 \end{itemize}
    80 \end{itemize}
    81 
    81 
    82 
    82 
    83 \subsection{Lists}
    83 \subsection{Lists}
    84 
    84 
    85 If lists are used heavily, the following notations increase readability:
    85 If lists are used heavily, the following notations increase readability:
    86 \begin{itemize}
    86 \begin{itemize}
    87 \item @{term"x # xs"} instead of \<open>x # xs\<close>,
    87 \item \<^term>\<open>x # xs\<close> instead of \<open>x # xs\<close>,
    88       where @{term"x # xs"} is also input syntax.
    88       where \<^term>\<open>x # xs\<close> is also input syntax.
    89 \item @{term"length xs"} instead of \<open>length xs\<close>.
    89 \item \<^term>\<open>length xs\<close> instead of \<open>length xs\<close>.
    90 \item @{term"nth xs n"} instead of \<open>nth xs n\<close>,
    90 \item \<^term>\<open>nth xs n\<close> instead of \<open>nth xs n\<close>,
    91       the $n$th element of \<open>xs\<close>.
    91       the $n$th element of \<open>xs\<close>.
    92 
    92 
    93 \item Human readers are good at converting automatically from lists to
    93 \item Human readers are good at converting automatically from lists to
    94 sets. Hence \texttt{OptionalSugar} contains syntax for suppressing the
    94 sets. Hence \texttt{OptionalSugar} contains syntax for suppressing the
    95 conversion function @{const set}: for example, @{prop[source]"x \<in> set xs"}
    95 conversion function \<^const>\<open>set\<close>: for example, @{prop[source]"x \<in> set xs"}
    96 becomes @{prop"x \<in> set xs"}.
    96 becomes \<^prop>\<open>x \<in> set xs\<close>.
    97 
    97 
    98 \item The \<open>@\<close> operation associates implicitly to the right,
    98 \item The \<open>@\<close> operation associates implicitly to the right,
    99 which leads to unpleasant line breaks if the term is too long for one
    99 which leads to unpleasant line breaks if the term is too long for one
   100 line. To avoid this, \texttt{OptionalSugar} contains syntax to group
   100 line. To avoid this, \texttt{OptionalSugar} contains syntax to group
   101 \<open>@\<close>-terms to the left before printing, which leads to better
   101 \<open>@\<close>-terms to the left before printing, which leads to better
   106 
   106 
   107 
   107 
   108 \subsection{Numbers}
   108 \subsection{Numbers}
   109 
   109 
   110 Coercions between numeric types are alien to mathematicians who
   110 Coercions between numeric types are alien to mathematicians who
   111 consider, for example, @{typ nat} as a subset of @{typ int}.
   111 consider, for example, \<^typ>\<open>nat\<close> as a subset of \<^typ>\<open>int\<close>.
   112 \texttt{OptionalSugar} contains syntax for suppressing numeric coercions such
   112 \texttt{OptionalSugar} contains syntax for suppressing numeric coercions such
   113 as @{const int} \<open>::\<close> @{typ"nat \<Rightarrow> int"}. For example,
   113 as \<^const>\<open>int\<close> \<open>::\<close> \<^typ>\<open>nat \<Rightarrow> int\<close>. For example,
   114 @{term[source]"int 5"} is printed as @{term "int 5"}. Embeddings of types
   114 @{term[source]"int 5"} is printed as \<^term>\<open>int 5\<close>. Embeddings of types
   115 @{typ nat}, @{typ int}, @{typ real} are covered; non-injective coercions such
   115 \<^typ>\<open>nat\<close>, \<^typ>\<open>int\<close>, \<^typ>\<open>real\<close> are covered; non-injective coercions such
   116 as @{const nat} \<open>::\<close> @{typ"int \<Rightarrow> nat"} are not and should not be
   116 as \<^const>\<open>nat\<close> \<open>::\<close> \<^typ>\<open>int \<Rightarrow> nat\<close> are not and should not be
   117 hidden.
   117 hidden.
   118 
   118 
   119 
   119 
   120 \section{Printing constants and their type}
   120 \section{Printing constants and their type}
   121 
   121 
   122 Instead of
   122 Instead of
   123 \verb!@!\verb!{const myconst}! \verb!@!\verb!{text "::"}! \verb!@!\verb!{typeof myconst}!,
   123 \verb!@!\verb!{const myconst}! \verb!@!\verb!{text "::"}! \verb!@!\verb!{typeof myconst}!,
   124 you can write \verb!@!\verb!{const_typ myconst}! using the new antiquotation
   124 you can write \verb!@!\verb!{const_typ myconst}! using the new antiquotation
   125 \texttt{const\_typ} defined in \texttt{LaTeXsugar}. For example,
   125 \texttt{const\_typ} defined in \texttt{LaTeXsugar}. For example,
   126 \verb!@!\verb!{const_typ length}! produces @{const_typ length} (see below for how to suppress
   126 \verb!@!\verb!{const_typ length}! produces \<^const_typ>\<open>length\<close> (see below for how to suppress
   127 the question mark).
   127 the question mark).
   128 This works both for genuine constants and for variables fixed in some context,
   128 This works both for genuine constants and for variables fixed in some context,
   129 especially in a locale.
   129 especially in a locale.
   130 
   130 
   131 
   131 
   132 \section{Printing theorems}
   132 \section{Printing theorems}
   133 
   133 
   134 The @{prop "P \<Longrightarrow> Q \<Longrightarrow> R"} syntax is a bit idiosyncratic. If you would like
   134 The \<^prop>\<open>P \<Longrightarrow> Q \<Longrightarrow> R\<close> syntax is a bit idiosyncratic. If you would like
   135 to avoid it, you can easily print the premises as a conjunction:
   135 to avoid it, you can easily print the premises as a conjunction:
   136 @{prop "P \<and> Q \<Longrightarrow> R"}. See \texttt{OptionalSugar} for the required ``code''.
   136 \<^prop>\<open>P \<and> Q \<Longrightarrow> R\<close>. See \texttt{OptionalSugar} for the required ``code''.
   137 
   137 
   138 \subsection{Question marks}
   138 \subsection{Question marks}
   139 
   139 
   140 If you print anything, especially theorems, containing
   140 If you print anything, especially theorems, containing
   141 schematic variables they are prefixed with a question mark:
   141 schematic variables they are prefixed with a question mark:
   370 
   370 
   371 
   371 
   372 \subsection{Patterns}
   372 \subsection{Patterns}
   373 
   373 
   374 
   374 
   375 In \S\ref{sec:varnames} we shows how to create patterns containing ``@{term DUMMY}''.
   375 In \S\ref{sec:varnames} we shows how to create patterns containing ``\<^term>\<open>DUMMY\<close>''.
   376 You can drive this game even further and extend the syntax of let
   376 You can drive this game even further and extend the syntax of let
   377 bindings such that certain functions like @{term fst}, @{term hd}, 
   377 bindings such that certain functions like \<^term>\<open>fst\<close>, \<^term>\<open>hd\<close>, 
   378 etc.\ are printed as patterns. \texttt{OptionalSugar} provides the following:
   378 etc.\ are printed as patterns. \texttt{OptionalSugar} provides the following:
   379 
   379 
   380 \begin{center}
   380 \begin{center}
   381 \begin{tabular}{l@ {~~produced by~~}l}
   381 \begin{tabular}{l@ {~~produced by~~}l}
   382 @{term "let x = fst p in t"} & \verb!@!\verb!{term "let x = fst p in t"}!\\
   382 \<^term>\<open>let x = fst p in t\<close> & \verb!@!\verb!{term "let x = fst p in t"}!\\
   383 @{term "let x = snd p in t"} & \verb!@!\verb!{term "let x = snd p in t"}!\\
   383 \<^term>\<open>let x = snd p in t\<close> & \verb!@!\verb!{term "let x = snd p in t"}!\\
   384 @{term "let x = hd xs in t"} & \verb!@!\verb!{term "let x = hd xs in t"}!\\
   384 \<^term>\<open>let x = hd xs in t\<close> & \verb!@!\verb!{term "let x = hd xs in t"}!\\
   385 @{term "let x = tl xs in t"} & \verb!@!\verb!{term "let x = tl xs in t"}!\\
   385 \<^term>\<open>let x = tl xs in t\<close> & \verb!@!\verb!{term "let x = tl xs in t"}!\\
   386 @{term "let x = the y in t"} & \verb!@!\verb!{term "let x = the y in t"}!\\
   386 \<^term>\<open>let x = the y in t\<close> & \verb!@!\verb!{term "let x = the y in t"}!\\
   387 \end{tabular}
   387 \end{tabular}
   388 \end{center}
   388 \end{center}
   389 
   389 
   390 
   390 
   391 \section {Styles\label{sec:styles}}
   391 \section {Styles\label{sec:styles}}
   447 \end{quote}
   447 \end{quote}
   448 Beware that any options must be placed \emph{before} the style, as in this example.
   448 Beware that any options must be placed \emph{before} the style, as in this example.
   449 
   449 
   450 Further use cases can be found in \S\ref{sec:yourself}.
   450 Further use cases can be found in \S\ref{sec:yourself}.
   451 If you are not afraid of ML, you may also define your own styles.
   451 If you are not afraid of ML, you may also define your own styles.
   452 Have a look at module @{ML_structure Term_Style}.
   452 Have a look at module \<^ML_structure>\<open>Term_Style\<close>.
   453 
   453 
   454 
   454 
   455 \section {Proofs}
   455 \section {Proofs}
   456 
   456 
   457 Full proofs, even if written in beautiful Isar style, are
   457 Full proofs, even if written in beautiful Isar style, are