on 63afafa help sign in
Idris-dev/tutorial/syntax.tex
96 lines 3.4 kB history raw
1\section{Syntax Extensions}
3\Idris{} supports the implementation of Embedded Domain Specific Languages (EDSLs) in
4several ways~\cite{res-dsl-padl12}. One way, as we have already seen, is through
5extending \texttt{do} notation. Another important way is to allow extension of the core
6syntax. In this section we describe two ways of extending the syntax: \texttt{syntax}
7rules and \texttt{dsl} notation.
9\subsection{\texttt{syntax} rules}
11We have seen \texttt{if...then...else} expressions, but these
12are not built in --- instead, we define a function in the prelude\ldots
14\begin{SaveVerbatim}{boolelim}
16boolElim : (x:Bool) -> |(t : a) -> |(f : a) -> a;
17boolElim True t e = t;
18boolElim False t e = e;
20\end{SaveVerbatim}
21\useverb{boolelim}
23\noindent
24\ldots and extend the core syntax with a \texttt{syntax} declaration:
26\begin{SaveVerbatim}{syntaxif}
28syntax if [test] then [t] else [e] = boolElim test t e;
30\end{SaveVerbatim}
31\useverb{syntaxif}
33\noindent
34The left hand side of a \texttt{syntax} declaration describes the syntax rule, and the right
35hand side describes its expansion. The syntax rule itself consists of:
37\begin{itemize}
38\item \textbf{Keywords} --- here, \texttt{if}, \texttt{then} and \texttt{else}, which must
39be valid identifiers
40\item \textbf{Non-terminals} --- included in square brackets, \texttt{[test]}, \texttt{[t]}
41and \texttt{[e]} here, which stand for arbitrary expressions. To avoid parsing ambiguities,
42these expressions cannot use syntax extensions at the top level (though they can be used
43in parentheses).
44\item \textbf{Symbols} --- included in quotations marks, e.g. \texttt{":="}
45\end{itemize}
47\noindent
48The limitations on the form of a syntax rule are that it must include at least one
49symbol or keyword, and there must be no repeated variables standing for non-terminals.
50Rules can use previously defined rules, but may not be recursive.
51The following syntax extensions would therefore be valid:
53\begin{SaveVerbatim}{syntaxex}
55syntax [var] ":=" [val] = Assign var val;
56syntax [test] "?" [t] ":" [e] = if test then t else e;
57syntax select [x] from [t] where [w] = SelectWhere x t w;
58syntax select [x] from [t] = Select x t;
60\end{SaveVerbatim}
61\useverb{syntaxex}
63\noindent
64Syntax macros can be further restricted to apply only in patterns (i.e., only on the left
65hand side of a pattern match clause) or only in terms (i.e. everywhere but the left hand side
66of a pattern match clause) by being marked as \texttt{pattern} or \texttt{term} syntax
67rules. For example, we might define an interval as follows, with a static check
68that the lower bound is below the upper bound using \texttt{so}:
70\begin{SaveVerbatim}{interval}
72data Interval : Set where
73 MkInterval : (lower : Float) -> (upper : Float) ->
74 so (lower < upper) -> Interval
76\end{SaveVerbatim}
77\useverb{interval}
79\noindent
80We can define a syntax which, in patterns, always matches \texttt{oh} for the proof
81argument, and in terms requires a proof term to be provided:
83\begin{SaveVerbatim}{intervalsyn}
85pattern syntax "[" [x] "..." [y] "]" = MkInterval x y oh
86term syntax "[" [x] "..." [y] "]" = MkInterval x y ?bounds_lemma
88\end{SaveVerbatim}
89\useverb{intervalsyn}
91\noindent
92In terms, the syntax \texttt{[x...y]} will generate a proof obligation
93\texttt{bounds\_lemma} (possibly renamed).
95\input{dsl}