f07f1d9cd8a13da4052001a9232315fd8f4830d4
[matthijs/master-project/report.git] / Chapters / Context.tex
1 \chapter[chap:context]{Context}
2   An obvious question that arises when starting any research is \quote{Has
3   this not been done before?}. Using a functional language for describing hardware
4   is not a new idea at all. In fact, there has been research into functional
5   hardware description even before the conventional hardware description
6   languages were created. \todo{Reference about early FHDLs} However,
7   functional languages were not nearly as advanced as they are now, and
8   functional hardware description never really got off. 
9
10 \todo{Add references}
11   Recently, there have been some renewed efforts, especially using the Haskell
12   functional language. Examples are Lava, ForSyde, ..., which are all a form of an
13   embedded domain specific language. Each of these have a slightly different
14   approach, but all of these do some trickery inside the Haskell language
15   itself, meaning you write a program that generates a hardware circuit,
16   instead of describing the circuit directly (either by running the haskell
17   code after compilation, or using Template Haskell to inspect parts of the
18   code you have written). This allows the full power of Haskell for generating
19   a circuit. However it also creates severe limitations in the use of the
20   language (you can't use case expressions in Lava, since they would be
21   executed only once during circuit generation) and extra notational overhead.
22
23   We will now have a look at the existing hardware description languages,
24   both conventional and functional to see the fields in which Cλash is
25   operating.
26
27   \section{Conventional hardware description languages}
28     Considering that we already have some hardware description languages like
29     \small{VHDL} and Verilog, why would we need anything else? By introducing
30     the functional style to hardware description, we hope to obtain a hardware
31     description language that is:
32     \startitemize
33       \item More consise. Functional programs are known for their conciseness
34       and ability to abstract away  common patterns.  This is largely enabled
35       by features like an advanced type system with polymorphism and higher
36       order functions.
37       \item Type-safer. Functional programs typically have a highly expressive
38       type system, which makes it harder to write incorrect code.
39       \item Easy to process. Functional languages have nice properties like
40       purity \refdef{purity} and single binding behaviour, which make it easy
41       to apply program transformations and optimizations and could potentially
42       simplify program verification.
43     \stopitemize
44   
45     \placeintermezzo{}{
46       \defref{EDSL}
47       \startframedtext[width=8.5cm,background=box,frame=no]
48       \startalignment[center]
49         {\tfa Embedded domain-specific languages (\small{EDSL})}
50       \stopalignment
51       \blank[medium]
52      
53       \startcitedquotation[deursen00]
54       A domain-specific language (\small{DSL}) is a program-
55       ming language or executable specification language
56       that offers, through appropriate notations and ab-
57       stractions, expressive power focused on, and usu-
58       ally restricted to, a particular problem domain.
59       \stopcitedquotation
60
61       An embedded \small{DSL} is a \small{DSL} that is embedded in
62       another language. Haskell is commonly used to embed \small{DSL}s
63       in, which typically means a number of Haskell functions and types
64       are made available that can be called to construct a large value
65       of some domain-specific datatype (\eg, a circuit datatype). This
66       generated datatype can then be processed further by the
67       \small{EDSL} \quote{compiler} (which runs in the same environment
68       as the \small{EDSL} itself. The embedded language is then a, mostly
69       applicative, subset of Haskell where the library functions are the
70       primitives. Sometimes advanced haskell features such as
71       polymorphism, higher order values or type classes can be used in
72       the embedded language. \cite[hudak96]
73       \stopframedtext
74     }
75
76   \section[sec:context:fhdls]{Existing functional hardware description languages}
77     As noted above, we're not the first to walk this path. However, current
78     embedded functional hardware description languages (in particular those
79     using Haskell) are limited by:\todo{Separate TH and EDSL approaches
80     better}
81     \startitemize
82       \item Not all of Haskell's constructs can be captured by embedded domain
83       specific languages. For example, an if or case expression is typically
84       executed only once and only its result is reflected in the embedded
85       description, not the if or case expression itself. Also, sharing of
86       variables (\eg, using the same variable twice while only calculating it
87       once) and cycles in circuits are non-trivial to properly and safely
88       translate (though there is some work to fix this, but that has not been
89       possible in a completely reliable way yet \cite[gill09]).
90       \item Some things are verbose to express. Especially ForSyDe suffers
91       from a lot of notational overhead due to the Template Haskell approach
92       used. Since conditional expressions are not supported, a lot of Haskell's
93       syntax sugar (if expressions, pattern matching, guards) cannot be used
94       either, leading to more verbose notation as well.
95       \item Polymorphism and higher order values are not supported within the
96       embedded language. The use of Haskell as a host language allows the use
97       of polymorphism and higher order functions at circuit generation time
98       (even for free, without any additional cost on the \small{EDSL}
99       programmers), but the described circuits do not have any polymorphism
100       or higher order functions, which can be limiting. \todo{How true or
101       appropriate is this point?}
102       \todo{Function structure gets lost (in Lava)}
103     \stopitemize
104
105     \todo{Complete translation in TH is complex: Works with Haskell AST
106     instead of Core}
107
108 % vim: set sw=2 sts=2 expandtab: