stab at some chapters
[phd-thesis.git] / appendix / clean_for_haskell_programmers.tex
1 While \gls{CLEAN} and \gls{HASKELL} were both conceived around 1987 and have similar syntax, there are some subtle differences in syntax and functionality.
2 This section describes some of the history of \gls{CLEAN} and provides a crash course in \gls{CLEAN} pecularities writen for \gls{HASKELL} programmers.
3
4 \Gls{CLEAN}---acronym for Clean \acrlong{LEAN}~\cite{barendregt_towards_1987}---, was originally designed as a \gls{GRS} core language but quickly served as an intermediate language for other functional languages~\cite{brus_clean_1987}.
5 In the early days it has also been called \emph{Concurrent} \gls{CLEAN}~\cite{nocker_concurrent_1991} but these days the language has no support for this anymore.
6 Fast forward thirty years, \gls{CLEAN} is now a robust language with state-of-the-art features and is actually used in industry as well as academia---albeit in select areas of the world~\cite{plasmeijer_clean_2021}.
7
8 Initially, when it was used mostly as an intermediate language, it had a fairly spartan syntax.
9 However, over the years, the syntax got friendlier and it currently it looks a lot like \gls{HASKELL}.
10 In the past, a \emph{double-edged} fronted even existed that allowed \gls{CLEAN} to be extended with \gls{HASKELL98} syntax and vice versa, however this frontend is no longer maintained~\cite{groningen_exchanging_2010}.
11 This chapter therefore gives a brief syntactical and functional comparison, a complete specification of the \gls{CLEAN} language can be found in the latest language report~\cite{plasmeijer_clean_2021}.
12 Many of this is based on work by Achten although that was based on \gls{CLEAN} 2.1 and \gls{HASKELL98}~\cite{achten_clean_2007}.
13 When \gls{HASKELL} is mentioned I actually mean \gls{GHC}'s \gls{HASKELL} and by \gls{CLEAN} I mean \gls{CLEAN} 3.1's \gls{ITASK} compiler.
14
15 \section{Features}
16 \subsection{Modules}
17 \Gls{CLEAN} has separate implementation and definition modules.
18 The definition module contains the class definitions, instances, function types and type definitions (possibly abstract).
19 Implementation modules contain the function implementations as well.
20 This means that only what is defined in the definition module is exported in \gls{CLEAN}.
21 This differs greatly from \gls{HASKELL}, as there is only a module file there.
22 Choosing what is exported in \gls{HASKELL} is done using the \haskellinline{module Mod(...)} syntax.
23
24 \subsection{Strictness}
25 In \gls{CLEAN}, by default, all expressions are evaluated lazily.
26 Types can be annotated with a strictness attribute (\cleaninline{!}), resulting in the values being evaluated to head-normal form before the function is entered.
27 In \gls{HASKELL}, in patterns, strictness can be enforced using \haskellinline{!}\requiresGHCmod{BangPatterns}.
28 Within functions the strict let (\cleaninline{#!}) can be used to force evaluate an expression, in \gls{HASKELL} \haskellinline{seq} or \haskellinline{\$!} is used for this.
29
30 \subsection{Uniqueness typing}
31 Types in \gls{CLEAN} may be \emph{unique}, which means that they may not be shared\todo{cite}.
32 The uniqueness type system allows the compiler to generate efficient code because unique data structures can be destructively updated.
33 Furthermore, uniqueness typing serves as a model for side effects as well.
34 \Gls{CLEAN} uses the \emph{world-as-value} paradigm where \cleaninline{World} represents the external environment and is always unique.
35 A program with side effects is characterised by a \cleaninline{Start :: *World -> *World} start function.
36 In \gls{HASKELL}, interaction with the world is done using the \haskellinline{IO} monad.
37 The \haskellinline{IO} monad could very well be---and actually is---implemented in \gls{CLEAN} using a state monad with the \cleaninline{World} as a state.
38 Besides marking types as unique, it is also possible to mark them with uniqueness attributes variables \cleaninline{u:} and define constraints on them.
39 For example, to make sure that an argument of a function is at least as unique as another argument.
40 Finally, using \cleaninline{.} (a dot), it is possible to state that several variables are equally unique.
41 Uniqueness is propagated automatically in function types but must be marked manually in data types.
42 Examples can be seen in \cref{lst:unique_examples}.
43
44 \begin{lstClean}[label={lst:unique_examples},caption={Examples of uniqueness annotations}]
45 f :: *a -> *a // f works on unique values only
46 f :: .a -> .a // f works on unique and non-unique values
47 f :: v:a u:b -> u:b, [v<=u] // f works when a is less unique than b
48 \end{lstClean}
49 %f :: (Int, *World) -> *World // The uniqueness is propagated automatically (i.e. *(Int, *World)))
50 %:: T = T *(Int, *World) // Writing :: T = T (Int, *World) won't work
51 %:: T = T (Int -> *(*World -> *World)) // Writing :: T = T (Int *World -> *World) won't work
52
53 \subsection{Expressions}
54 \todo[inline]{Matches pattern expression \texttt{=:}}
55
56 \todo[inline]{Let before}
57
58 \subsection{Generics}
59 Polytypic functions~\cite{jeuring_polytypic_1996}---also known as generic or kind-indexed fuctions---are built into \gls{CLEAN}~\cite[Chp.~7.1]{plasmeijer_clean_2021}\cite{alimarine_generic_2005} whereas in \gls{HASKELL} they are implemented as a library~\cite[Chp.~6.19.1]{ghc_team_ghc_2021}.
60 %When calling a generic function, the kind must always be specified and depending on the kind, the function may require more arguments.
61
62 For example, defining a generic equality is done as in \cref{lst:generic_eq}.
63 \lstinputlisting[language=Clean,firstline=4,label={lst:generic_eq},caption={Generic equality function in \gls{CLEAN}.}.]{lst/generic_eq.icl}
64
65 Metadata about the types is available using the \cleaninline{of} syntax that gives the function access to metadata records, as can be seen in \cref{lst:generic_print} showing a generic print function. This abundance of metadata allows for very complex generic functions that near the expression level of template metaprogramming\todo[inline]{crossref chapter c-code generation}.
66 \lstinputlisting[language=Clean,firstline=4,label={lst:generic_print},caption={Generic print function in \gls{CLEAN}.}]{lst/generic_print.icl}
67
68 \subsection{\glsentrytext{GADT}s}
69 GADTs are enriched data types that allow the type instantiation of the constructor to be explicitly defined~\cite{cheney_first-class_2003,hinze_fun_2003}.
70 While \glspl{GADT} are not natively supported in \gls{CLEAN}, they can be simulated using embedding-projection pairs or equivalence types~\cite[Sec.~2.2]{cheney_lightweight_2002}.
71 To illustrate this, \cref{lst:gadt_clean} shows an example \gls{GADT} that would be implemented in \gls{HASKELL} as done in \cref{lst:gadt_haskell}\requiresGHCmod{GADTs}.
72
73 \lstinputlisting[language=Clean,firstline=4,label={lst:gadt_clean},caption={Expression \gls{GADT} using equivalence types in \gls{CLEAN}.}]{lst/expr_gadt.icl}
74 \lstinputlisting[language=Haskell,style=haskell,firstline=4,label={lst:gadt_haskell},caption={Expression \gls{GADT} in \gls{HASKELL}.}]{lst/expr_gadt.hs}
75
76 \section{Syntax}
77 \begin{longtable}{p{.45\linewidth}p{.5\linewidth}}
78 \caption[]{Syntactical differences between \gls{CLEAN} and \gls{HASKELL}.}%
79 \label{tbl:syn_clean_haskell}\\
80 \toprule
81 \gls{CLEAN} & \gls{HASKELL}\\
82 \midrule
83 \endfirsthead%
84 \caption[]{(continued)}\\
85 \toprule
86 \gls{CLEAN} & \gls{HASKELL}\\
87 \midrule
88 \endhead%
89
90 \midrule
91 \multicolumn{2}{c}{Comments}\\
92 \midrule
93 \cleaninline{// single line} & \haskellinline{-- single line}\\
94 \cleaninline{/* multi line /* nested */ */} & \haskellinline{\{- multi line \{- nested -\} \}}\\
95
96 \midrule
97 \multicolumn{2}{c}{Imports}\\
98 \midrule
99 \cleaninline{import Mod => qualified f1, :: t} & \haskellinline{import qualified Mod (f1, t)}\\
100 & \haskellinline{import Mod hiding (f1, t)}\\
101 \cleaninline{/* multi line /* nested */ */} & \haskellinline{\{- multi line \{- nested -\} \}}\\
102
103 \midrule
104 \multicolumn{2}{c}{Basic types}\\
105 \midrule
106 \cleaninline{42 :: Int} & \haskellinline{42 :: Int}\\
107 \cleaninline{True :: Bool} & \haskellinline{True :: Bool}\\
108 \cleaninline{toInteger 42 :: Integer} & \haskellinline{42 :: Integer}\\
109 \cleaninline{38.0 :: Real} & \haskellinline{38.0 :: Float -- or Double}\\
110 \cleaninline{"Hello" +++ "World" :: String}\footnote{Strings are represented as unboxed character arrays.}
111 & \haskellinline{"Hello" ++ "World" :: String}\footnote{Strings are represented as lists of characters by default but may be overloaded as well if \GHCmod{OverloadedStrings} is enabled.}\\
112 \cleaninline{['Hello'] :: [Char]} & \haskellinline{"Hello" :: String}\\
113 \cleaninline{?t} & \haskellinline{Maybe t}\\
114 \cleaninline{(?None, ?Just e)} & \haskellinline{(Nothing, Just e)}\\
115
116 \midrule
117 \multicolumn{2}{c}{Type definitions}\\
118 \midrule
119 \cleaninline{:: T a0 ... :== t} & \haskellinline{type T a0 ... = t}\\
120 \cleaninline{:: T a0 ... } & \haskellinline{data T a1 ...}\\
121 \quad\cleaninline{= C1 f0 ... fn \| ... \| Cn f0 ... fn} & \quad\haskellinline{= C1 f0 ... fn \| ... \| Cn f0 ... fn}\\
122 \cleaninline{:: T a0 ...} & \haskellinline{data T a0 ...}\\
123 \quad\cleaninline{= \{ f0 :: t0, ..., fn :: tn \} } & \quad\haskellinline{= T \{ f0 :: t0, ..., fn :: tn \} }\\
124 \cleaninline{:: T a0 ... =: t} & \haskellinline{newtype T a0 ... = t}\\
125 \cleaninline{:: T = E.t Box t \& C t} & \haskellinline{data T = forall t.C t => Box t}\requiresGHCmod{ExistentialQuantification}\\
126
127 \midrule
128 \multicolumn{2}{c}{Function types}\\
129 \midrule
130 \cleaninline{f0 :: a0 a1 ... -> t}
131 & \haskellinline{f0 :: (c0 v0, c1 v1, c2 v2) =>}\\
132 \quad\cleaninline{\| c0 v0 \& c1, c2 v1}
133 & \quad\haskellinline{a0 -> a1 ... -> t}\\
134 \cleaninline{(+) infixl 6 :: Int Int -> Int} & \haskellinline{infixl 6 +}\\
135 & \haskellinline{(+) :: Int -> Int -> Int}\\
136 \cleaninline{qid :: (A.a: a -> a) -> (Bool, Int)}
137 & \haskellinline{qid :: (forall a: a -> a) -> (Bool, Int)}\requiresGHCmod{RankNTypes}\\
138 \cleaninline{qid id = (id True, id 42)} &
139 \haskellinline{qid id = (id True, id 42)}\\
140
141 \midrule
142 \multicolumn{2}{c}{Type classes}\\
143 \midrule
144 \cleaninline{class f a :: t} & \haskellinline{class f a where f :: t}\\
145 \cleaninline{class C a \| C0, ... , Cn a}\footnote{In contrast to the \gls{HASKELL} variant, this does not require an instance.} & \haskellinline{class (C0 a, ..., Cn, a) => C a}\\
146 \cleaninline{class C s ~m where ...} & \haskellinline{class C s m \| m -> s where ...}\requiresGHCmod{MultiParamTypeClasses}\\
147 \cleaninline{instance C t \| C0, ..., Cn a} & \haskellinline{instance (C0 a, ..., Cn a) => C t}\\
148 \quad\cleaninline{where ...} & \quad\haskellinline{where ...}\\
149
150 \midrule
151 \multicolumn{2}{c}{As pattern}\\
152 \midrule
153 \cleaninline{x=:p} & \haskellinline{x@p}\\
154
155 \midrule
156 \multicolumn{2}{c}{Lists}\\
157 \midrule
158 \cleaninline{[1,2,3]} & \haskellinline{[1,2,3]}\\
159 \cleaninline{[x:xs]} & \haskellinline{x:xs}\\
160 \cleaninline{[e \\\\ e <- xs \| p e]} & \haskellinline{[e \| e <- xs, p e]}\\
161 \cleaninline{[l \\\\ l <- xs, r <- ys]} & \haskellinline{[l \| l <- xs, r <- ys]}\\
162 \cleaninline{[(l, r) \\\\ l <- xs \& r <- ys]} & \haskellinline{[(l, r) \| (l, r) <- zip xs ys]}\\
163 & or \haskellinline{[(l, r) \| l <- xs \| r <- ys]}\requiresGHCmod{ParallelListComp}\\
164
165 \midrule
166 \multicolumn{2}{c}{Lambda expressions}\\
167 \midrule
168 \cleaninline{\\a0 a1 ...->e} or \cleaninline{\\....e} or \cleaninline{\\...=e} & \haskellinline{\\a0 a1 ...->e}\\
169
170 \midrule
171 \multicolumn{2}{c}{Case distinction}\\
172 \midrule
173 \cleaninline{if p e0 e1} & \haskellinline{if p then e0 else e1}\\
174 \cleaninline{case e of p0 -> e0; ...} & \haskellinline{case e of p0 -> e0; ...}\\
175 \quad or \cleaninline{case e of p0 = e0; ...}\\
176 \cleaninline{f p0 ... pn} & \haskellinline{f p0 ... pn}\\
177 \quad\cleaninline{\| c = t} & \quad\haskellinline{\| c = t}\\
178 \quad\cleaninline{\| otherwise = t} or \cleaninline{= t} & \quad\haskellinline{\| otherwise = t}\\
179
180 \midrule
181 \multicolumn{2}{c}{Record expressions}\\
182 \midrule
183 \cleaninline{:: R = \{ f :: t \}} & \haskellinline{data R = R \{ f :: t \}}\\
184 \cleaninline{r = \{ f = e \}} & \haskellinline{r = R \{e\}}\\
185 \cleaninline{r.f} & \haskellinline{f r}\\
186 \cleaninline{r!f}\footnote{This operator allows for field selection from unique records.} & \haskellinline{(\\v->(f v, v)) r}\\
187 \cleaninline{\{r \& f = e \}} & \haskellinline{r \{ f = e \}}\\
188
189 \midrule
190 \multicolumn{2}{c}{Record patterns}\\
191 \midrule
192 \cleaninline{:: R0 = \{ f0 :: R1 \}} & \haskellinline{data R0 = R0 \{ f0 :: R1 \}}\\
193 \cleaninline{:: R1 = \{ f1 :: t \}} & \haskellinline{data R1 = R1 \{ f1 :: t \}}\\
194 \cleaninline{g \{ f0 \} = e f0} & \haskellinline{g (R0 \{f0=x\}) = e x}\\
195 & or \haskellinline{g (R0 \{f0\}) = e f0}\requiresGHCmod{RecordPuns}\\
196 \cleaninline{g \{ f0 = \{f1\} \} = e f1} & \haskellinline{g (R0 \{f0=R1 \{f1=x\}\}) = e x}\\
197
198 \midrule
199 \multicolumn{2}{c}{Arrays}\\
200 \midrule
201 \cleaninline{:: A :== \{t\}} & \haskellinline{type A = Array Int t}\\
202 \cleaninline{a = \{v0, ..., vn\}} & \haskellinline{a = array (0, n+1)}\\
203 & \quad\haskellinline{[(0, v0), ..., (n, vn)]}\\
204 \cleaninline{a = \{e \\\\ p <-: a\}} & \haskellinline{a = array (0, length a-1)}\\
205 & \quad\haskellinline{[e \| (i, a) <- [0..] `zip` a]}\\
206 \cleaninline{a.[i]} & \haskellinline{a!i}\\
207 \cleaninline{a![i]}\footnote{This operator allows for field selection from unique arrays.} & \haskellinline{(\v->(v!i, v)) a}\\
208 \cleaninline{\{ a \& [i] = e\}} & \haskellinline{a//[(i, e)]}\\
209
210 \midrule
211 \multicolumn{2}{c}{Dynamics}\\
212 \midrule
213 \cleaninline{f :: a -> Dynamic \| TC a} & \haskellinline{f :: Typeable a => a -> Dynamic}\\
214 \cleaninline{f e = dynamic e} & \haskellinline{f e = toDyn (e)}\\
215 \cleaninline{g :: Dynamic -> t} & \haskellinline{g :: Dynamic -> t}\\
216 \cleaninline{g (e :: t) = e0} & \haskellinline{g d = case fromDynamic d}\\
217 \cleaninline{g e = e1} & \quad\haskellinline{Just e -> e0}\\
218 & \quad\haskellinline{Nothing -> e1}\\
219
220 \bottomrule
221 \end{longtable}
222