Clean (programming language): Difference between revisions
→Compiling: clarify state of SAPL; copy-edit rest of the section |
|||
Line 81: | Line 81: | ||
==Compiling== |
==Compiling== |
||
Compilation of Clean to [[machine code]] is performed as follows: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# Object code is linked with other files in the module and the runtime system and converted into a normal executable using the system [[Linker (computing)|linker]] (when available) or a dedicated linker written in Clean on [[Windows]]. |
# Object code is linked with other files in the module and the runtime system and converted into a normal executable using the system [[Linker (computing)|linker]] (when available) or a dedicated linker written in Clean on [[Windows]]. |
||
Earlier |
Earlier versions of the Clean compiler were written completely in [[C (programming language)|C]], thus avoiding bootstrapping issues. |
||
The [[SAPL]]<!-- Domain expert DISAMBIGUATE! --> system compiles Core Clean to [[JavaScript]]<ref>https://clean.cs.ru.nl/SAPL#SAPL%20Linker</ref> and does not use ABC code. |
|||
=== The ABC machine === |
=== The ABC machine === |
||
The ABC code mentioned above is an [[intermediate representation]] for an [[abstract machine]]. Because machine code generation for ABC code is relatively straightforward, this makes it easy to support new architectures. The ABC machine is an [[Imperative programming|imperative]] abstract [[graph rewriting]] machine.<ref>{{cite thesis|type=PhD|last1=Koopman|first1=Pieter|title=Functional Programs as Executable Specifications|date=December 10, 1990|publisher=Katholieke Universiteit Nijmegen|isbn=90-9003689-X|page=35}}</ref> It consists of a graph store to hold the Clean graph that is being rewritten and three stacks. The A(rgument)-stack holds arguments that refer to nodes in the graph store. 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, builds a <code>Start</code> node in the graph store and pushes it on the A-stack. It then begins printing it, evaluating it as needed. |
|||
To close the gap between Core Clean, a high-level functional language, and [[machine code]], the ABC machine is used. |
|||
This is an [[Imperative programming|imperative]] abstract [[graph rewriting]] machine.<ref>{{cite thesis|type=PhD|last1=Koopman|first1=Pieter|title=Functional Programs as Executable Specifications|date=December 10, 1990|publisher=Katholieke Universiteit Nijmegen|isbn=90-9003689-X|page=35}}</ref> 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 address#Memory models|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. |
|||
=== Running Clean in the browser === |
|||
The [[runtime system]], which is linked into every executable, has a <code>print</code> rule which prints a node to the output channel. When a program is executed, the <code>Start</code> 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. |
|||
Although Clean is typically used to generative native executables, several projects have enabled applications in [[Web browser|web browsers]]. |
|||
The now abandoned [https://clean.cs.ru.nl/SAPL SAPL] project compiled Core Clean to [[JavaScript]] and did not use ABC code. Since 2019, an interpreter for ABC code, written in [[WebAssembly]], is used instead.<ref>{{cite web |title=Clean and iTasks / ABC Interpreter · GitLab |url=https://gitlab.com/clean-and-itasks/abc-interpreter |website=Clean and iTasks on GitLab |access-date=13 April 2023 |language=en}}</ref><ref>{{cite journal |last1=Staps |first1=Camil |last2=van Groningen |first2=John |last3=Plasmeijer |first3=Rinus |title=Lazy interworking of compiled and interpreted code for sandboxing and distributed systems |journal=Proceedings of the 31st Symposium on Implementation and Application of Functional Languages |date=15 July 2021 |pages=1–12 |doi=10.1145/3412932.3412941}}</ref> |
|||
==Platforms== |
==Platforms== |
Revision as of 14:57, 13 April 2023
![]() | |
Paradigm | functional |
---|---|
Designed by | Software Technology Research Group of Radboud University Nijmegen |
First appeared | 1987 |
Stable release | 3.1
/ 5 January 2022 |
Typing discipline | strong, static, dynamic |
OS | Cross-platform |
License | Simplified BSD[1] |
Filename extensions | .icl, .dcl, .abc |
Website | clean |
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 has been 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 has slowed, some researchers are still working in the language.[8] In 2018, a spin-off company was founded that uses Clean.[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 input/output (I/O) through a uniqueness type 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
Start = "Hello, world!"
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
|
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
Computing is based on graph rewriting and reduction. Constants such as numbers are graphs and functions are graph rewriting formulas. This, combined with compiling to native code, makes Clean programs which use high abstraction run relatively fast according to The Computer Language Benchmarks Game.[11]
Compiling
Compilation of Clean to machine code is performed as follows:
- Source files (.icl) and definition files (.dcl) are translated into Core Clean, a basic variant of Clean, by the compiler frontend written in Clean.
- Core clean is converted into Clean's platform-independent intermediate language (.abc), by the compiler backend written in Clean and C.
- Intermediate ABC code is converted to object code (.o) by the code generator written in C.
- 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 versions of the Clean compiler were written completely in C, thus avoiding bootstrapping issues.
The ABC machine
The ABC code mentioned above is an intermediate representation for an abstract machine. Because machine code generation for ABC code is relatively straightforward, this makes it easy to support new architectures. The ABC machine is an imperative abstract graph rewriting machine.[12] It consists of a graph store to hold the Clean graph that is being rewritten and three stacks. The A(rgument)-stack holds arguments that refer to nodes in the graph store. 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, builds a Start
node in the graph store and pushes it on the A-stack. It then begins printing it, evaluating it as needed.
Running Clean in the browser
Although Clean is typically used to generative native executables, several projects have enabled applications in web browsers. The now abandoned SAPL project compiled Core Clean to JavaScript and did not use ABC code. Since 2019, an interpreter for ABC code, written in WebAssembly, is used instead.[13][14]
Platforms
Clean is available for Microsoft Windows (IA-32 and X86-64), macOS (X86-64), and Linux (IA-32, X86-64, and AArch64).
Some libraries are not available on all platforms, like ObjectIO which is only available on Windows. Also the feature to write dynamics to files is only available on Windows.
The availability of Clean per platform varies with each version:[15][16]
Version | Date | Linux | macOS | Oracle Solaris | Windows | Miscellaneous | |||||
---|---|---|---|---|---|---|---|---|---|---|---|
IA-32 | x86-64 | AArch64 | Motorola 68040 | PowerPC | x86-64 | SPARC | IA-32 | x86-64 | |||
3.1 | January 5, 2022 | Yes | Yes | Yes | No | No | Yes | No | Yes | Yes | |
3.0 | October 2, 2018 | Yes | Yes | No | No | No | Yes | No | Yes | Yes | |
2.4 | December 23, 2011 | Yes | Yes | No | No | No | Yes | No | Yes | Yes | |
2.3 | December 22, 2010 | Yes | Yes | No | No | No | No | No | Yes | Yes | |
2.2 | December 19, 2006 | Yes | Yes | No | No | Yes | No | Yes | Yes | Yes | |
2.1.1 | May 31, 2005 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
2.1.0 | October 31, 2003 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
2.0.2 | December 12, 2002 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
2.0.1 | July 4, 2002 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
2.0 | December 21, 2001 | No | No | No | No | No | No | No | Yes | No | |
1.3.3 | September 13, 2000 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
1.3.2 | July 1, 1999 | No | No | No | Yes | Yes | No | Yes | Yes | No | |
1.3.1 | January 1999 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
1.3 | May 22, 1998 | Yes | No | No | No | Yes | No | Yes | Yes | No | |
1.2.4 | June 1997 | No | No | No | Yes | Yes | No | No | Yes | No | |
1.2.3 | May 1997 | No | No | No | Yes | Yes | No | No | Yes | No | |
1.2 | January 13, 1997 | No | No | No | Yes | Yes | No | No | No | No | |
1.1.3 | October 1996 | No | No | No | No | No | No | Yes | No | No | OS/2 (i80386) |
1.1.2 | September 1996 | Yes | No | No | No | No | No | Yes | No | No | SunOS 4 (SPARC) |
1.1 | March 1996 | Yes | No | No | Yes | No | No | No | No | No | |
1.0.2 | September 1995 | Yes | No | No | Yes | No | No | Yes | No | No | OS/2 (i80386); SunOS 4 (SPARC) |
1.0 | May 1995 | No | No | No | Yes | No | No | No | No | No | OS/2 (i80386) |
0.8.4 | May 11, 1993 | Yes | No | No | Yes | No | No | No | No | No | Experimental T800 transputer release |
0.8.3 | February 26, 1993 | No | No | No | Yes | No | No | No | No | No | |
0.8.1 | October 19, 1992 | No | No | No | Yes | No | No | No | No | No | |
0.8 | July 13, 1992 | No | No | No | Yes | No | No | No | No | No | OS/2 (i80386); SunOS 3–4 (SPARC) |
0.7 | May 1991 | No | No | No | Yes | No | No | No | No | No | SunOS 3–4 (SPARC) |
Comparison to Haskell
A 2008 benchmark showed that Clean native code performs similarly to the Haskell (Glasgow Haskell Compiler (GHC)), depending on the benchmark.[17]
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
- ^ "Download Clean". Clean. Retrieved 23 July 2019.
- ^ "Idris - Uniqueness Types". Retrieved 20 November 2018.
- ^ "Clean 0.7: Readme". Archived from the original on 24 May 2019.
- ^ "Clean 1.0: Readme". Archived from the original on 5 May 2019.
- ^ "Clean 1.3: Readme". Archived from the original on 27 April 2019.
- ^ "Radboud University Nijmegen: Department of Software Science: Software".
- ^ "FAQ". Clean. Retrieved 26 November 2021.
- ^ "Publications". Clean. Retrieved 26 November 2021.
- ^ "Home". TOP Software Technology. Retrieved 26 November 2021.
- ^ a b ftp://ftp.cs.ru.nl/pub/Clean/papers/2007/achp2007-CleanHaskellQuickGuide.pdf
- ^ "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) - ^ Koopman, Pieter (10 December 1990). Functional Programs as Executable Specifications (PhD). Katholieke Universiteit Nijmegen. p. 35. ISBN 90-9003689-X.
- ^ "Clean and iTasks / ABC Interpreter · GitLab". Clean and iTasks on GitLab. Retrieved 13 April 2023.
- ^ Staps, Camil; van Groningen, John; Plasmeijer, Rinus (15 July 2021). "Lazy interworking of compiled and interpreted code for sandboxing and distributed systems". Proceedings of the 31st Symposium on Implementation and Application of Functional Languages: 1–12. doi:10.1145/3412932.3412941.
- ^ "Release history". Clean. Retrieved 7 January 2022.
- ^ "Index of /Clean". Retrieved 7 January 2022.
- ^ Jansen, Jan Martin; Koopman, Pieter; Plasmeijer, Rinus (2008). "From Interpretation to Compilation" (PDF). Retrieved 21 May 2016.
{{cite journal}}
: Cite journal requires|journal=
(help)
External links
- Clean Wiki
- clean-lang.org: public registry with Clean packages
- cloogle.org: a search engine to search in Clean packages