1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
\section{Optimization of Curry Programs}
After the invocation of the Curry front end,
which parses a Curry program and translates it into the intermediate FlatCurry
representation, \CYS applies a transformation
to optimize Boolean equalities occurring in the Curry program.
The ideas and details of this optimization are described
in \cite{AntoyHanus15LOPSTR}.
Therefore, we sketch only some basic ideas and options
to influence this optimization.
Consider the following definition of the operation \code{last}
to extract the last element in list:
%
\begin{curry}
last :: Data a => [a] -> a
last xs | xs === _ ++ [x]
= x
where x free
\end{curry}
%
In order to evaluate the condition \ccode{xs === \us{}++[x]},
the Boolean equality is evaluated to \code{True} or \code{False}
by instantiating the free variables \code{\us} and \code{x}.
However, since we know that a condition must be evaluated to
\code{True} only and all evaluations to \code{False} can be ignored,
we can use the constrained equality to obtain a more efficient program:
%
\begin{curry}
last :: Data a => [a] -> a
last xs | xs =:= _++[x]
= x
where x free
\end{curry}
%
Since the selection of the appropriate equality operator
is not obvious and might be tedious, \CYS encourages
programmers to use only the Boolean equality operator \ccode{===}
in programs.
The constraint equality operator \ccode{=:=} can be considered
as an optimization of \ccode{===} if it is ensured that only
positive results are required, e.g., in conditions of program rules.
To support this programming style, \CYS has a built-in optimization phase
on FlatCurry files. For this purpose, the optimizer analyzes
the FlatCurry programs for occurrences of \ccode{===}
and replaces them by \ccode{=:=} whenever the result \code{False}
is not required.\footnote{The current optimizer
also replaces occurrences of \texttt{(==)} although this
transformation is valid only if the corresponding \texttt{Eq} instances
define equality rather than equivalence.}
The usage of the optimizer can be influenced by setting
the property flag \code{bindingoptimization} in the
configuration file \code{\curryrc}.
The following values are recognized for this flag:
\begin{description}
\item[\code{no}:] Do not apply this transformation.
\item[\code{fast}:] This is the default value.
The transformation is based on pre-computed values for
the prelude operations in order to decide whether the
value \code{False} is not required as a result of a Boolean equality.
Hence, the transformation can be efficiently performed
without any complex analysis.
\item[\code{full}:] Perform a complete ``required values'' analysis
of the program (see \cite{AntoyHanus15LOPSTR})
and use this information to optimize programs.
In most cases, this does not yield better results so that
the \code{fast} mode is sufficient.
\end{description}
%
Hence, to turn off this optimization, one can either modify
the flag \code{bindingoptimization} in the
configuration file \code{\curryrc} or dynamically pass this change
to the invocation of \CYS by
\begin{quote}
\ldots{} \code{-Dbindingoptimization=no} \ldots
\end{quote}
|