Clean is a general-purpose purely functional computer programming language. For much of the language's active development history it was called Concurrent Clean, but this was dropped at some point.
Maps, Directions, and Place Reviews
Features
The language Clean first appeared in 1987 and is still being further developed. It shares many properties with Haskell: referential transparency, list comprehension, guards, garbage collection, higher order functions, currying and lazy evaluation.
On Windows, an integrated development environment (IDE) is included in the Clean distribution.
Clean's method for dealing with mutable state and I/O is done through a uniqueness typing system, in contrast to Haskell's use of monads. The compiler takes advantage of the uniqueness type system to generate more efficient code, because it knows that anything with a uniqueness type can only be used once. Therefore, a unique value can be changed in place.
C Lean Video
Examples
Hello world:
Factorial:
Fibonacci sequence:
Infix operator:
The type declaration states that the function is a right associative infix operator with priority 8: this states that x*x^(n-1)
is equivalent to x*(x^(n-1))
as opposed to (x*x)^(n-1)
. This operator is pre-defined in StdEnv, the Clean standard library.
How Clean works
Computation is based on graph rewriting and reduction. Constants such as numbers are graphs and functions are graph rewriting formulas. This, combined with compilation to native code, makes even Clean programs which use high abstraction run relatively fast according to the Computer Language Benchmarks Game.
Compiling
- Source files (.icl) and definition files (.dcl) are translated into Core Clean, a basic variant of Clean, in Clean.
- Core clean is converted into Clean's platform-independent bytecode (.abc), implemented in C and Clean.
- Bytecode is converted to object code (.o) using C.
- Object code is linked with other files in the module and the runtime system and converted into a normal executable in Clean.
Earlier Clean system versions were written completely in C, thus avoiding bootstrapping issues.
The SAPL system compiles Core Clean to JavaScript and does not use ABC code.
The ABC-Machine
To close the gap between Core Clean, a high-level functional language, and machine code, the ABC-machine is used. This is an imperative abstract graph rewriting machine. Generating concrete machine code from abstract ABC code is a relatively small step, so by using the ABC-machine it is much easier to target multiple architectures for code generation.
The ABC-machine has an uncommon memory model. It has a graph store to hold the Clean graph that is being rewritten. The A(rgument)-stack holds arguments that refer to nodes in the graph store. This way, a node's arguments can be rewritten, which is needed for pattern matching. The B(asic value)-stack holds basic values (integers, characters, reals, etc.). While not strictly necessary (all these elements could be nodes in the graph store as well), using a separate stack is much more efficient. The C(ontrol)-stack holds return addresses for flow control.
The runtime system, which is linked into every executable, has a print
rule which prints a node to the output channel. When a program is executed, the Start
node is printed. For this, it has to be rewritten to root normal form, after which its children are rewritten to root normal form, etc., until the whole node is printed.
Platforms
Clean is available for Microsoft Windows, Apple Macintosh, Solaris and Linux.
Some libraries are not available on all platforms, like ObjectIO which is only available on Windows and Mac. The feature to write dynamics to files is only available on Windows.
License
Clean is dual licensed: it is available under the terms of the GNU LGPL, and also under a proprietary license. For the libraries, runtime system and examples, not the GNU LGPL but the Simplified BSD License applies.
Versus Haskell
Speed
A benchmark from 2008 shows that Clean is faster than Haskell in most cases:
As can be seen, Clean outruns Haskell (GHC) on almost all test cases. Only parser combinators are faster in Haskell. Using GHC -O we get some optimisations, making pattern matching and higher order functions faster than in Clean as well. In most cases, however, Clean outperforms GHC -O or at least isn't slower.
The programs used were:
- Pri: Prime sieve;
primes !! 5000
- Sym: Prime sieve using Peano numbers;
sprimes !! p280
- Inter: A small SAPL interpreter, calculating the 100th prime number using a sieve
- Fib: Fibonacci; the naive Fibonacci function, calculating
fib 35
- Match: Nested pattern matching (5 levels deep), repeated 2000000 times
- Ham: Hamming; the generation of the list of Hamming numbers and taking the 1000th number, 10000 times
- Twi: Twice; a higher order function (
twice twice twice twice (add 1) 0
), repeated 400 times - Qns: The Queens problem; number of placements of 11 queens on an 11 × 11 chess board
- Kns: Knights; finding all knight tours on a 5 × 5 chess board
- Parse: Parser combinators; a parser for Prolog parsing a 17000 lines Prolog program
- Plog: Prolog; a small Prolog interpreter based on unification only (no arithmetic operations), calculating all descendants in a six generations family tree
- Sort: Quicksort (20000 elements), Merge sort (200000 elements) and Insertion sort (10000 elements)
Syntactic differences
The syntax of Clean is very similar to Haskell, with some notable differences:
In general, Haskell has introduced more syntactic sugar than Clean.
Community
- IRC channel: #cleanlang on freenode
Source of the article : Wikipedia
EmoticonEmoticon