Clean (programming language)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Clean
Cleanlang logo.jpg
Paradigm functional
Designed by Software Technology Research Group of Radboud University Nijmegen
First appeared 1987
Stable release 2.4 / December 23, 2011 (2011-12-23)
Typing discipline strong, static, dynamic
OS Cross-platform
License GNU LGPL, Simplified BSD, commercial software
Filename extensions .icl, .dcl, .abc, .o, .sapl
Website http://clean.cs.ru.nl/
Influenced by
Lean, Miranda, Haskell
Influenced
Haskell

In computer science, 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.

Features[edit]

The language Clean first appeared in 1987 and is still being further developed.[1] 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. [2]

Examples[edit]

Hello world:

 module hello
 Start :: {#Char}
 Start = "Hello, world!"

Factorial:

  module factorial
  import StdEnv
  fac 0 = 1
  fac n = n * fac (n-1)

  // find the factorial of 10
  Start = fac 10
  module factorial2
  import StdEnv
  fac 0 = 1
  fac n = prod [1..n] // The product of the numbers 1 to n

  // find the factorial of 6
  Start = fac 6

Fibonacci sequence:

  module fibonacci
  fib 0 = 0
  fib 1 = 1
  fib n = fib (n - 2) + fib (n - 1) 
  Start = fib 7

Infix operator:

  (^) infixr 8 :: Int Int -> Int
  (^) x 0 = 1
  (^) x n = x * x ^ (n-1)

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[edit]

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 Clean programs run relatively fast, even with high abstraction.[3]

Compiling[edit]

  1. Source files (.icl) and project files (.dcl) are converted into Clean's platform-independent bytecode (.abc), implemented in C and Clean.
  2. Bytecode is converted to object code (.o) using C.
  3. 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.

Platforms[edit]

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[edit]

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[edit]

Speed[edit]

A benchmark from 2008 shows that Clean is faster than Haskell in most cases:[4]

Speed comparison of five compilers (time in seconds)
Language Pri Sym Inter Fib Match Ham Twi Qns Kns Parse Plog Qsort Isort Msort
SAPL Int 6.1 17.6 7.8 7.3 8.5 15.7 7.9 6.5 47.1 4.4 4.0 16.4 9.4 4.4
SAPL Bas 4.3 13.2 6.0 6.5 5.9 9.8 5.6 5.1 38.3 3.8 2.6 10.1 6.7 2.6
GHC 2.0 1.7 8.2 4.0 4.1 8.4 6.6 3.7 17.7 2.8 0.7 4.4 2.3 3.2
GHC -O 0.9 1.5 1.8 0.2 1.0 4.0 0.1 0.4 5.7 1.9 0.4 3.2 1.9 1.0
Clean 0.9 0.8 0.8 0.2 1.4 2.4 2.4 0.4 3.0 4.5 0.4 1.6 1.0 0.6

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[edit]

The syntax of Clean is very similar to Haskell, with some notable differences:[2]

Haskell Clean Remarks
[ x | x <- [1..10] , isOdd x]
[ x \\ x <- [1..10] | isOdd x]
list comprehension
x:xs
[x:xs]
cons operator
data Tree a
  = Empty
  | Node (Tree a) a (Tree a)
:: Tree a
  = Empty
  | Node (Tree a) a (Tree a)
algebraic data type
(Eq a, Eq b) => ...
... | Eq a & Eq b
class assertions and contexts
fun t@(Node l x r) = ...
fun t=:(Node l x r) = ...
as-patterns
if x > 10 then 10 else x
if (x > 10) 10 x
if

In general, Haskell has introduced more syntactic sugar than Clean.

See also[edit]

Community[edit]

  • IRC channel: #cleanlang on freenode

References[edit]

External links[edit]