Jump to content

Clean (programming language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Kaĉjo (talk | contribs)
m copy editing (punctuation before references)
Kaĉjo (talk | contribs)
Features: fix explanation of uniqueness
Line 22: Line 22:


Clean shares many properties and syntax with a younger sibling language, [[Haskell (programming language)|Haskell]]:
Clean shares many properties and syntax with a younger sibling language, [[Haskell (programming language)|Haskell]]:
[[referential transparency]], [[list comprehension]], [[Guard (computing)|guard]]s, [[garbage collection (computer science)|garbage collection]], [[higher order functions]], [[currying]] and [[lazy evaluation]]. However, Clean deals with mutable state and I/O through a [[Uniqueness type|uniqueness typing system]], in contrast to Haskell's use of [[Monads in functional programming|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 [[destructive update|changed in place]].<ref name="CleanHaskellGuide">ftp://ftp.cs.ru.nl/pub/Clean/papers/2007/achp2007-CleanHaskellQuickGuide.pdf</ref>
[[referential transparency]], [[list comprehension]], [[Guard (computing)|guard]]s, [[garbage collection (computer science)|garbage collection]], [[higher order functions]], [[currying]] and [[lazy evaluation]]. However, Clean deals with mutable state and I/O through a [[Uniqueness type|uniqueness typing system]], in contrast to Haskell's use of [[Monads in functional programming|monads]]. The compiler takes advantage of the uniqueness type system to generate more efficient code, because it knows that at any point during the execution of the program, only one reference can exist to a value with a unique type. Therefore, a unique value can be [[destructive update|changed in place]].<ref name="CleanHaskellGuide">ftp://ftp.cs.ru.nl/pub/Clean/papers/2007/achp2007-CleanHaskellQuickGuide.pdf</ref>


An [[integrated development environment]] (IDE) for [[Microsoft Windows]] is included in the Clean distribution.
An [[integrated development environment]] (IDE) for [[Microsoft Windows]] is included in the Clean distribution.

Revision as of 09:43, 26 November 2021

Clean
Paradigmfunctional
Designed bySoftware Technology Research Group of Radboud University Nijmegen
First appeared1987; 37 years ago (1987)
Stable release
3.0 / October 19, 2018; 6 years ago (2018-10-19)
Typing disciplinestrong, static, dynamic
OSCross-platform
LicenseSimplified BSD[1]
Filename extensions.icl, .dcl, .abc
Websiteclean.cs.ru.nl
Influenced by
Lean, Miranda, Haskell
Influenced
Haskell, Idris[2]

Clean is a general-purpose purely functional computer programming language. It was called the Concurrent Clean System,[3] then the Clean System,[4][5] later just Clean. Clean is being developed by a group of researchers from the Radboud University in Nijmegen since 1987.[6]

Features

The language Clean first appeared in 1987,[7]. Although development of the language itself has slowed down, some researchers are still working in the language.[8] In 2018, a spin-off company was founded that uses Clean as well.[9]

Clean shares many properties and syntax with a younger sibling language, Haskell: referential transparency, list comprehension, guards, garbage collection, higher order functions, currying and lazy evaluation. However, Clean deals with mutable state and I/O 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 at any point during the execution of the program, only one reference can exist to a value with a unique type. Therefore, a unique value can be changed in place.[10]

An integrated development environment (IDE) for Microsoft Windows is included in the Clean distribution.

Examples

Hello world:

 Start = "Hello, world!"

Factorial:

fac :: Int -> Int
fac 0 = 1
fac n = n * fac (n-1)

Start = fac 10
fac :: Int -> Int
fac n = prod [1..n] // The product of the numbers 1 to n

Start = fac 10

Fibonacci sequence:

fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n = fib (n - 2) + fib (n - 1) 

Start = fib 7
fibs :: Int Int -> [Int]
fibs x_2 x_1 = [x_2:fibs x_1 (x_2 + x_1)]

fib :: Int -> Int
fib n = (fibs 1 1) !! n

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

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 which use high abstraction run relatively fast according to the Computer Language Benchmarks Game.[11]

Compiling

  1. Source files (.icl) and definition files (.dcl) are translated into Core Clean, a basic variant of Clean, in Clean.
  2. Core clean is converted into Clean's platform-independent intermediate language (.abc), implemented in C and Clean.
  3. Intermediate ABC code is converted to object code (.o) using C.
  4. Object code is linked with other files in the module and the runtime system and converted into a normal executable using the system linker (when available) or a dedicated linker written in Clean on Windows.

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.[12] 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.

Comparison to Haskell

A 2008 benchmark showed that Clean native code performs roughly equally well as Haskell (GHC), depending on the benchmark.[13]

Syntactic differences

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

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.

References

  1. ^ "Download Clean". Clean. Retrieved 23 July 2019.
  2. ^ "Idris - Uniqueness Types". Retrieved 2018-11-20.
  3. ^ "Clean 0.7: README".
  4. ^ "Clean 1.0: README".
  5. ^ "Clean 1.3: README".
  6. ^ "Radboud University Nijmegen: Department of Software Science: Software".
  7. ^ "FAQ". Clean. Retrieved 2021-11-26.
  8. ^ "Publications". Clean. Retrieved 2021-11-26.
  9. ^ "Home". TOP Software Technology. Retrieved 26 November 2021.
  10. ^ a b ftp://ftp.cs.ru.nl/pub/Clean/papers/2007/achp2007-CleanHaskellQuickGuide.pdf
  11. ^ "Which programming languages are fastest?". Computer Language Benchmarks Game. Archived from the original on 28 June 2011.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  12. ^ Koopman, Pieter (December 10, 1990). Functional Programs as Executable Specifications (PhD). Katholieke Universiteit Nijmegen. p. 35. ISBN 90-9003689-X.
  13. ^ Jansen, Jan Martin; Koopman, Pieter; Plasmeijer, Rinus (2008). "From Interpretation to Compilation" (PDF). Retrieved 2016-05-21. {{cite journal}}: Cite journal requires |journal= (help)