Jump to content

Unlambda: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Addbot (talk | contribs)
m Bot: Migrating 8 interwiki links, now provided by Wikidata on d:q670180 (Report Errors)
 
(21 intermediate revisions by 19 users not shown)
Line 1: Line 1:
{{Refimprove|date=August 2020}}
'''Unlambda''' is a minimal, "nearly [[Purely functional|pure]]"<ref>{{cite web|title=Friday Pathological Programming: Unlambda, or Programming Without Variables|url=http://scienceblogs.com/goodmath/2006/08/11/friday-pathological-programmin-3/|publisher=ScienceBlogs}}</ref> [[functional programming language|functional]] [[programming language]] invented by [[David Madore]]. It is based on [[combinatory logic]], a version of the [[lambda calculus]] that omits the lambda operator. It relies mainly on two built-in functions (''s'' and ''k'') and an "apply" operator (written ''`'', the [[backquote]] character). These alone make it [[Turing-complete]], but there are also some I/O functions to make it possible to interact with the user, some shortcut functions and a function for lazy evaluation. There are no variables in the language.
{{short description|Functional programming language}}
{{Infobox programming language
| name = Unlambda
| logo = <!-- (filename only) -->
| logo caption =
| screenshot = <!-- (filename only) -->
| screenshot caption =
| paradigm = Nearly [[Purely functional programming|pure]] [[Functional programming|functional]]
| family =
| designer = David Madore
| developer = David Madore
| released = {{Start date and age|1999|06|28|df=yes}}
| latest release version = 2.0.0
| latest release date = {{Start date and age|1999|12|20|df=yes}}
| latest preview version =
| latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes}} -->
| typing = [[Type system|Untyped]]
| scope =
| programming language = [[Scheme (programming language)|Scheme]], [[C (programming language)|C]], [[Java (programming language)|Java]]
| discontinued =
| platform =
| operating system =
| license = [[GNU General Public License|GPL]] 2.0 or later
| file ext =
| file format = <!-- or: | file formats = -->
| website = {{URL|www.madore.org/~david/programs/unlambda}}
| implementations =
| dialects =
| influenced by =
| influenced =
}}
'''Unlambda''' is a minimal, "nearly [[Purely functional language|pure]]"<ref name="chu2006">{{cite web|work=Good Math, Bad Math (blog)|first=Mark C.|last=Chu-Carroll|date=2006-08-11|title=Friday Pathological Programming: Unlambda, or Programming Without Variables|url=http://scienceblogs.com/goodmath/2006/08/11/friday-pathological-programmin-3/|publisher=ScienceBlogs}}</ref> [[functional programming language]] invented by David Madore. It is based on [[combinatory logic]], an expression system without the [[Lambda calculus|lambda operator]] or free variables. It relies mainly on two built-in functions (<code>s</code> and <code>k</code>) and an apply operator (written <code>`</code>, the [[backquote]] character). These alone make it [[Turing-complete]], but there are also some [[input/output]] (I/O) functions to enable interacting with the user, some shortcut functions, and a [[lazy evaluation]] function. Variables are unsupported.

Unlambda is [[free and open-source software]] distributed under a [[GNU General Public License]] (GPL) 2.0 or later.{{clarify|date=March 2022}}


==Basic principles==
==Basic principles==
As an [[esoteric programming language]], Unlambda is meant as a demonstration of very pure functional programming rather than for practical use. Its main feature is the lack of conventional operators and data types&mdash;the only kind of data in the program are one-parameter functions. Data can nevertheless be simulated with appropriate functions as in the [[lambda calculus]]. Multi-parameter functions can be represented with the technique of [[currying]].
As an [[esoteric programming language]], Unlambda is meant as a demonstration of very pure functional programming rather than for practical use. Its main feature is the lack of conventional operators and data types&mdash;the only kind of data in the program are one-parameter functions. Data can nevertheless be simulated with appropriate functions as in the [[lambda calculus]]. Multi-parameter functions can be represented via the method of [[currying]].


Unlambda is based around the principle of [[abstraction elimination]], or the elimination of all saved variables, including functions. As a purely functional language, not only are Unlambda's functions first-class objects, they are the ''only'' first-class objects.
Unlambda is based on the principle of [[abstraction elimination]], or the elimination of all saved variables, including functions. As a purely functional language, Unlambda's functions are [[first-class object]]s, and are the ''only'' such objects.


An implementation of the [[hello world program]] in Unlambda follows:
Here is an implementation of a [[hello world program]] in Unlambda:<ref name="chu2006"/>


`r```````````.H.e.l.l.o. .w.o.r.l.di
`r```````````.H.e.l.l.o. .w.o.r.l.di
Line 15: Line 49:
Other important features provided by Unlambda include the <code>k</code> and <code>s</code> functions. <code>k</code> manufactures constant functions: the result of <code>`k''x''</code> is a function which, when invoked, returns ''x''. Thus the value of <code>``k''xy''</code> is ''x'' for any ''x'' and ''y''.
Other important features provided by Unlambda include the <code>k</code> and <code>s</code> functions. <code>k</code> manufactures constant functions: the result of <code>`k''x''</code> is a function which, when invoked, returns ''x''. Thus the value of <code>``k''xy''</code> is ''x'' for any ''x'' and ''y''.


<code>s</code> is a generalized evaluation operator. <code>```s''xyz''</code> evaluates to <code>``''xz''`''yz''</code> for any ''x'', ''y'', and ''z''. It is a remarkable fact that <code>s</code> and <code>k</code> are sufficient to perform any calculation, as described in [[SKI combinator calculus]]. As a brief example, note that the identity function <code>i</code> can be implemented as <code>``skk</code>, since <code>```skk''x''</code> yields ''x'' for all ''x''.
<code>s</code> is a generalized evaluation operator. <code>```s''xyz''</code> evaluates to <code>``''xz''`''yz''</code> for any ''x'', ''y'', and ''z''. It is a remarkable fact that <code>s</code> and <code>k</code> are sufficient to perform any calculation, as described in [[SKI combinator calculus]]. As a brief example, the identity function <code>i</code> can be implemented as <code>``skk</code>, since <code>```skk''x''</code> yields ''x'' for all ''x''.


Unlambda's one flow control construction is [[call with current continuation]], denoted <code>c</code>. When an expression of the form <code>`c''x''</code> is evaluated, a special "continuation" object is constructed, representing the state of the interpreter at that moment. Then ''x'' is evaluated, and then the result is given the continuation object as an argument. If the continuation is never applied to an argument, the value of the <code>`c''x''</code> expression is the same as the value of ''x''. But if the continuation object is applied to a value ''y'', execution of ''x'' is immediately aborted, and the value of the entire <code>`c''x''</code> expression is ''y''.
Unlambda's one flow control construct is [[call with current continuation]], denoted <code>c</code>. When an expression of the form <code>`c''x''</code> is evaluated, a special ''continuation'' object is constructed, representing the state of the interpreter at that moment. Then ''x'' is evaluated, and then the result is given the continuation object as an argument. If the continuation is never applied to an argument, the value of the <code>`c''x''</code> expression is the same as the value of ''x''. But if the continuation object is applied to a value ''y'', execution of ''x'' is immediately aborted, and the value of the entire <code>`c''x''</code> expression is ''y''.


Although Unlambda's execution semantics are normally [[eager evaluation|eager]], there is a [[lazy evaluation]] option, indicated by the use of the <code>d</code> operator. Usually, to evaluate an expression of the form <code>`''xy''</code>, unlambda first evaluates ''x'', then ''y'', and then applies ''x'' to ''y''. However, if ''x'' evaluates to the special value <code>d</code>, then ''y'' is ''not'' evaluated; instead, the value of the expression <code>`d''y''</code> is a special "delayed computation" object, which, when applied to an argument ''z'', evaluates ''y'', and then applies its value to ''z''. Note that in the absence of side effects, this is exactly the same as <code>`i''y''</code>. The difference is that <code>`i''y''</code> executes any side effects in ''y'' immediately, whereas <code>`d''y''</code> defers the side effects until the result is applied to another argument.
Unlambda's execution semantics are normally [[eager evaluation]], but a [[lazy evaluation]] option exists, indicated by the use of the <code>d</code> operator. Usually, to evaluate an expression of the form <code>`''xy''</code>, unlambda first evaluates ''x'', then ''y'', and then applies ''x'' to ''y''. However, if ''x'' evaluates to the special value <code>d</code>, then ''y'' is ''not'' evaluated; instead, the value of the expression <code>`d''y''</code> is a special "delayed computation" object, which, when applied to an argument ''z'', evaluates ''y'', and then applies its value to ''z''. In the absence of side effects, this is exactly the same as <code>`i''y''</code>. The difference is that <code>`i''y''</code> executes any side effects in ''y'' immediately, whereas <code>`d''y''</code> defers the side effects until the result is applied to another argument.


Unlambda's next built-in operator is <code>v</code>, which ignores its argument and returns <code>v</code>. This feature is not strictly necessary, since <code>v</code> could be implemented as <code>```s``k``sii``s``s`ksk`k``siik</code>, but it is supplied as a convenience. (This expression above is simply <code>`Yk</code>, where <code>Y</code> denotes a [[fixed point combinator]].)
Unlambda's next built-in operator is <code>v</code>, which ignores its argument and returns <code>v</code>. This feature is not strictly necessary, since <code>v</code> could be implemented as <code>``s`k``s``s`kskk`k``s``s`kskk</code>, but it is supplied as a convenience. (This expression above is simply <code>`Yk</code>, where <code>Y</code> denotes a [[fixed point combinator]].)


==Unlambda 2 built-in functions==
==Version 2 built-in functions==
Additional built-ins were introduced in version 2 of the Unlambda language. [[Input/output|Input]] in Unlambda is facilitated by operators <code>@</code> and <code>?''u''</code>. When <code>@</code> is applied to a function ''x'', a character is read from input, and stored as the "current character"; then ''x'' is applied to <code>i</code>. However, if no more characters were available on input, the "current character" is left undefined, and ''x'' is applied to <code>v</code> instead. When a function <code>?''u''</code> is applied to a function ''x'', the result is the evaluation of <code>`''x''i</code> if the current character is ''u'', otherwise <code>`''x''v</code> is evaluated.
More built-ins were introduced in Unlambda version 2. [[Input/output|Input]] is facilitated by operators <code>@</code> and <code>?''u''</code>. When <code>@</code> is applied to a function ''x'', a character is read from input, and stored as the "current character"; then ''x'' is applied to <code>i</code>. However, if no more characters were available on input, the ''current character'' is left undefined, and ''x'' is applied to <code>v</code> instead. When a function <code>?''u''</code> is applied to a function ''x'', the result is the evaluation of <code>`''x''i</code> if the current character is ''u'', otherwise <code>`''x''v</code> is evaluated.


There is also a "reprint" operator <code>|</code>. When <code>`|''x''</code> is evaluated, the function ''x'' is applied to <code>.''u''</code> if ''u'' is the current character, or to <code>v</code> if there is no current character.
There is also a "reprint" operator <code>|</code>. When <code>`|''x''</code> is evaluated, the function ''x'' is applied to <code>.''u''</code> if ''u'' is the current character, or to <code>v</code> if there is no current character.
Line 31: Line 65:


==See also==
==See also==
Similar languages:
*[[Iota and Jot]]
*[[Iota and Jot]]
*[http://esoteric.voxelperfect.net/wiki/Lazy_K Lazy K]

Computational system on which Unlambda is based:
*[[SKI combinator calculus]]
*[[SKI combinator calculus]]


==References==
==References==
{{reflist}}
{{Reflist}}
* Felix-Hernandez Campos (1 April 2002), ''[http://www.cs.unc.edu/~stotts/144/lectures/lect28apr.pdf Lecture 28: More on functional programming]'', University of North Carolina COMP144
* Felix-Hernandez Campos (1 April 2002), ''[http://www.cs.unc.edu/~stotts/144/lectures/lect28apr.pdf Lecture 28: More on functional programming]'', University of North Carolina COMP144
* {{cite book |url=http://books.google.com/books?id=LlR6__OpAxoC&pg=PA205&dq=Unlambda&cd=2#v=onepage&q=%22Unlambda%22&f=false |title=Rubyで作る奇妙なプログラミング言語 |language=Japanese |author=原 悠 (Yutaka Hara)|page=205&ndash;214|isbn=4-8399-2784-7|publisher=Tōkyō: Mainichikomyunikēshonzu|year=2008}}
* {{cite book |url=https://books.google.com/books?id=LlR6__OpAxoC&dq=%22Unlambda%22&pg=PA205 |title=Rubyで作る奇妙なプログラミング言語 |language=Japanese |author=原 悠 (Yutaka Hara)|pages=205–214|isbn=978-4-8399-2784-4|publisher=Tōkyō: Mainichikomyunikēshonzu|year=2008}}


==External links==
==External links==
*[http://www.madore.org/~david/programs/unlambda/ Unlambda homepage]
*{{Official website|www.madore.org/~david/programs/unlambda}}
*[http://repl.it/#:languages Online Unlambda REPL]


[[Category:Esoteric programming languages]]
[[Category:Esoteric programming languages]]
[[Category:Functional languages]]
[[Category:Functional languages]]
{{Esoteric programming languages}}

Latest revision as of 02:59, 11 August 2024

Unlambda
ParadigmNearly pure functional
Designed byDavid Madore
DeveloperDavid Madore
First appeared28 June 1999; 25 years ago (1999-06-28)
Stable release
2.0.0 / 20 December 1999; 24 years ago (1999-12-20)
Typing disciplineUntyped
Implementation languageScheme, C, Java
LicenseGPL 2.0 or later
Websitewww.madore.org/~david/programs/unlambda

Unlambda is a minimal, "nearly pure"[1] functional programming language invented by David Madore. It is based on combinatory logic, an expression system without the lambda operator or free variables. It relies mainly on two built-in functions (s and k) and an apply operator (written `, the backquote character). These alone make it Turing-complete, but there are also some input/output (I/O) functions to enable interacting with the user, some shortcut functions, and a lazy evaluation function. Variables are unsupported.

Unlambda is free and open-source software distributed under a GNU General Public License (GPL) 2.0 or later.[clarification needed]

Basic principles

[edit]

As an esoteric programming language, Unlambda is meant as a demonstration of very pure functional programming rather than for practical use. Its main feature is the lack of conventional operators and data types—the only kind of data in the program are one-parameter functions. Data can nevertheless be simulated with appropriate functions as in the lambda calculus. Multi-parameter functions can be represented via the method of currying.

Unlambda is based on the principle of abstraction elimination, or the elimination of all saved variables, including functions. As a purely functional language, Unlambda's functions are first-class objects, and are the only such objects.

Here is an implementation of a hello world program in Unlambda:[1]

`r```````````.H.e.l.l.o. .w.o.r.l.di

Original built-in functions

[edit]

The notation .x denotes a function which takes one argument and returns it unchanged, printing the single character x as a side effect when it is invoked. i represents the version of the identity function that has no such side effect; it is used here as a dummy argument. The program `.di applies the d-printing function to a dummy argument of i, returning i and printing the letter d as a side effect. Similarly, ``.l.di first applies .l to .d, printing the letter l and returning .d; this result of .d is then applied to i as in the previous example. The function r is syntactic sugar for the function that prints a newline character.

Other important features provided by Unlambda include the k and s functions. k manufactures constant functions: the result of `kx is a function which, when invoked, returns x. Thus the value of ``kxy is x for any x and y.

s is a generalized evaluation operator. ```sxyz evaluates to ``xz`yz for any x, y, and z. It is a remarkable fact that s and k are sufficient to perform any calculation, as described in SKI combinator calculus. As a brief example, the identity function i can be implemented as ``skk, since ```skkx yields x for all x.

Unlambda's one flow control construct is call with current continuation, denoted c. When an expression of the form `cx is evaluated, a special continuation object is constructed, representing the state of the interpreter at that moment. Then x is evaluated, and then the result is given the continuation object as an argument. If the continuation is never applied to an argument, the value of the `cx expression is the same as the value of x. But if the continuation object is applied to a value y, execution of x is immediately aborted, and the value of the entire `cx expression is y.

Unlambda's execution semantics are normally eager evaluation, but a lazy evaluation option exists, indicated by the use of the d operator. Usually, to evaluate an expression of the form `xy, unlambda first evaluates x, then y, and then applies x to y. However, if x evaluates to the special value d, then y is not evaluated; instead, the value of the expression `dy is a special "delayed computation" object, which, when applied to an argument z, evaluates y, and then applies its value to z. In the absence of side effects, this is exactly the same as `iy. The difference is that `iy executes any side effects in y immediately, whereas `dy defers the side effects until the result is applied to another argument.

Unlambda's next built-in operator is v, which ignores its argument and returns v. This feature is not strictly necessary, since v could be implemented as ``s`k``s``s`kskk`k``s``s`kskk, but it is supplied as a convenience. (This expression above is simply `Yk, where Y denotes a fixed point combinator.)

Version 2 built-in functions

[edit]

More built-ins were introduced in Unlambda version 2. Input is facilitated by operators @ and ?u. When @ is applied to a function x, a character is read from input, and stored as the "current character"; then x is applied to i. However, if no more characters were available on input, the current character is left undefined, and x is applied to v instead. When a function ?u is applied to a function x, the result is the evaluation of `xi if the current character is u, otherwise `xv is evaluated.

There is also a "reprint" operator |. When `|x is evaluated, the function x is applied to .u if u is the current character, or to v if there is no current character.

Finally, there is an exit operator e. When e is applied to x, the execution of the program is terminated, and x is taken as the result of the program (most of the currently existing interpreters ignore the result anyway).

See also

[edit]

References

[edit]
  1. ^ a b Chu-Carroll, Mark C. (2006-08-11). "Friday Pathological Programming: Unlambda, or Programming Without Variables". Good Math, Bad Math (blog). ScienceBlogs.
[edit]