Algebraic datatypes with multiple constructors, but without any
fields are essentially a way to get an enumeration-like type
containing alternatives. Note that Haskell's \hs{Bool} type is also
- defined as an enumeration type, but we have a fixed translation for
- that. An example of such an enum type is the type that represents the
- colors in a traffic light:
+ defined as an enumeration type, but that there a fixed translation for
+ that type within the \CLaSH\ compiler. An example of such an
+ enumeration type is the type that represents the colors in a traffic
+ light:
\begin{code}
data TrafficLight = Red | Orange | Green
\end{code}
\end{xlist}
\subsection{Polymorphism}
- A powerful construct in most functional languages is polymorphism, it
- allows a function to handle values of different data types in a uniform
- way. Haskell supports \emph{parametric polymorphism}~\cite{polymorphism},
- meaning functions can be written without mention of any specific type and
- can be used transparently with any number of new types.
+ A powerful feature of most (functional) programming languages is
+ polymorphism, it allows a function to handle values of different data
+ types in a uniform way. Haskell supports \emph{parametric
+ polymorphism}~\cite{polymorphism}, meaning functions can be written
+ without mention of any specific type and can be used transparently with
+ any number of new types.
As an example of a parametric polymorphic function, consider the type of
the following \hs{append} function, which appends an element to a vector:
+
\begin{code}
append :: [a|n] -> a -> [a|n + 1]
\end{code}
type classes, where a class definition provides the general interface of a
function, and class instances define the functionality for the specific
types. An example of such a type class is the \hs{Num} class, which
- contains all of Haskell's numerical operations. A developer can make use
+ contains all of Haskell's numerical operations. A designer can make use
of this ad-hoc polymorphism by adding a constraint to a parametrically
polymorphic type variable. Such a constraint indicates that the type
variable can only be instantiated to a type whose members supports the
In \CLaSH, parametric polymorphism is completely supported. Any function
defined can have any number of unconstrained type parameters. The \CLaSH\
compiler will infer the type of every such argument depending on how the
- function is applied. There is one exception to this: The top level
- function that is translated, can not have any polymorphic arguments (as
- they are never applied, so there is no way to find out the actual types
- for the type parameters).
+ function is applied. There is however one constraint: the top level
+ function that is being translated can not have any polymorphic arguments.
+ The arguments can not be polymorphic as they are never applied and
+ consequently there is no way to determine the actual types for the type
+ parameters.
\CLaSH\ does not support user-defined type classes, but does use some
- of the built-in type classes for its built-in function, such as: \hs{Num}
- for numerical operations, \hs{Eq} for the equality operators, and
+ of the standard Haskell type classes for its built-in function, such as:
+ \hs{Num} for numerical operations, \hs{Eq} for the equality operators, and
\hs{Ord} for the comparison/order operators.
\subsection{Higher-order functions \& values}
function. The following example should clarify this concept:
\begin{code}
- negVector xs = map not xs
+ negateVector xs = map not xs
\end{code}
- The code above defines a function \hs{negVector}, which takes a vector of
- booleans, and returns a vector where all the values are negated. It
- achieves this by calling the \hs{map} function, and passing it
+ The code above defines the \hs{negateVector} function, which takes a
+ vector of booleans, \hs{xs}, and returns a vector where all the values are
+ negated. It achieves this by calling the \hs{map} function, and passing it
\emph{another function}, boolean negation, and the vector of booleans,
\hs{xs}. The \hs{map} function applies the negation function to all the
elements in the vector.
The \hs{map} function is called a higher-order function, since it takes
another function as an argument. Also note that \hs{map} is again a
- parametric polymorphic function: It does not pose any constraints on the
+ parametric polymorphic function: it does not pose any constraints on the
type of the vector elements, other than that it must be the same type as
the input type of the function passed to \hs{map}. The element type of the
resulting vector is equal to the return type of the function passed, which
\end{code}
Finally, higher order arguments are not limited to just built-in
- functions, but any function defined in \CLaSH\ can have function
+ functions, but any function defined by a developer can have function
arguments. This allows the hardware designer to use a powerful
abstraction mechanism in his designs and have an optimal amount of
- code reuse.
+ code reuse. The only exception is again the top-level function: if a
+ function-typed argument is not applied with an actual function, no
+ hardware can be generated.
- \comment{TODO: Describe ALU example (no code)}
+ % \comment{TODO: Describe ALU example (no code)}
\subsection{State}
A very important concept in hardware it the concept of state. In a