Thursday, October 4, 2012

clean as CLEAN


Clean is a general purpose, state-of-the-art, pure and lazy functional programming language designed for making real-world applications.

The language Clean first appeared in 1987 and is still being further developed; it shares many properties with Haskellreferential transparencylist comprehensionguardsgarbage collectionhigher order functions and currying and lazy evaluation.

Clean is a general purpose, state-of-the-art, pure and lazy functional programming language designed for making real-world applications. Here is a list of the most notable language features:

  • Clean is a lazy, pure, and higher-order functional programming language with explicit graph-rewriting semantics.
  • Although Clean is by default a lazy language, one can smoothly turn it into a strict language to obtain optimal time/space behavior: functions can be defined lazy as well as (partially) strict in their arguments; any (recursive) data structure can be defined lazy as well as (partially) strict in any of its arguments.
  • Clean is a strongly typed language based on an extension of the well-known Milner/Hindley/Mycroft type inferencing/checking scheme including the common higher-order types, polymorphic types, abstract types, algebraic types, type synonyms, and existentially quantified types.
  • Clean has pattern matching, guards, list comprehensions, array comprehensions and a lay-out sensitive mode.
  • Clean supports type classes and type constructor classes to make overloaded use of functions and operators possible.
  • The uniqueness typing system of Clean makes it possible to develop efficient applications. In particular, it allows a refined control over the single-threaded use of objects which can influence the time and space behavior of programs. Uniqueness typing can also be used to incorporate destructive updates of objects within a pure functional framework. It allows destructive transformation of state information and enables efficient interfacing to the nonfunctional world (to C but also to I/O systems like X-Windows) offering direct access to file systems and operating systems.
  • Clean offers records and (destructively updateable) arrays and files.
  • The Clean type system supports dynamic types, allowing values of arbitrary types to be wrapped in a uniform package and unwrapped via a type annotation at run time. Using dynamics, code and data can be exchanged between Clean applications in a flexible and type-safe way.
  • Clean provides a built-in mechanism for generic functions.



Function definitions in functional languages like Clean look very similar to function definitions in mathematics. Much of the syntactical ballast that is customary in many programming languages can be avoided. Even the parenthesis surrounding the arguments may be avoided. The function to square numbers can be defined as:

square : : Int -> Int
square n = n* n

The first line gives the type of this function: one integer as argument and an integers as result. The second line defines how the result of the application of the function square applied to an arbitrary argument n should be computed: multiply that arguments by itself.
To express choice one can define a function in several function alternatives. For instance the factorial function can be defined as:

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

In such a definition, the first alternative that is applicable to the actual arguments will be used. This strategy forces that the first alternative to be applied to the expression fac 0, although also the second alternative matches this expression. Each program in Clean computes the value of the expression Start. By providing an appropriate definition for this function, the value of any expression can be computed. The factorial of six is computed by the program:

Start : : Int
Start = fac 6

The value of the Start expression is shown to the user. For this example the program will write 720 to the console. Since the value of this expression is shown to the user, the famous "Hello world"-program is just:

Start : : String
Start : : "Hello world"

Although distinguishing various cases in the definition of a function by patterns is very powerful, it is not enough. For instance, using patterns it is impossible to check whether an integer argument is positive, or larger than another argument. This can be solved using guards. A guard is a boolean expression that can be inserted between the patterns of a function alternative and the symbol =. The symbol | separates the patterns and the guard. An right hand side is only applied if its guard yields True. Each function alternative can have a sequence of guarded right-hand sides. Consider the function maximum as example, this function yields the largest of its two arguments.

maximum : : Int Int -> Int
maximum n m
     | n < m = m
     | n >= m = n

When you know that the last guard inside such a function alternative will always yield True, as in the given example, it can be omitted or replaced by the keyword otherwise. The type of the function maximum indicates that it can be used for any two elements of the same type t provided that arguments of this type can be compared.

"If you compare Clean and Haskell in the Debian shootout, Clean is consistently faster than Haskell. These languages are spitting images of each other, save that Clean has some way of avoiding Monads. In addition, Clean I believe is based on graphs instead of lambda calculus - but both of these compute the same things so why would that matter?
The summary table (at bottom, horribly rendered in Firefox), shows Clean winning 10-0.

Also, which language came first? Clean or Haskell?"
-metaperl,blogger(http://sequence.complete.org/node/119)

"Clean was first. I was at the FPCA meeting where Clean was presented while at the same meeting it was proposed to think about a new standard functional language since Miranda was under trademark."

-Rinus Plasmeijer



References: 

http://en.wikipedia.org/wiki/Clean_(programming_language)
http://wiki.clean.cs.ru.nl

Author:
2009-33934 MIGK

No comments:

Post a Comment