Joy programming language
Encyclopedia
The Joy programming language in computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

 is a purely functional programming language that was produced by Manfred von Thun of La Trobe University
La Trobe University
La Trobe University is a multi-campus university in Victoria, Australia. It was established in 1964 by an Act of Parliament to become the third oldest university in the state of Victoria. The main campus of La Trobe is located in the Melbourne suburb of Bundoora; two other major campuses are...

 in Melbourne
Melbourne
Melbourne is the capital and most populous city in the state of Victoria, and the second most populous city in Australia. The Melbourne City Centre is the hub of the greater metropolitan area and the Census statistical division—of which "Melbourne" is the common name. As of June 2009, the greater...

, Australia
Australia
Australia , officially the Commonwealth of Australia, is a country in the Southern Hemisphere comprising the mainland of the Australian continent, the island of Tasmania, and numerous smaller islands in the Indian and Pacific Oceans. It is the world's sixth-largest country by total area...

. Joy is based on composition of functions rather than lambda calculus
Lambda calculus
In mathematical logic and computer science, lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion. The portion of lambda calculus relevant to computation is now called the untyped lambda calculus...

. It has turned out to have many similarities to Forth, due not to design but to a sort of parallel evolution and convergence. Joy is notable as the canonical example of a concatenative programming language
Concatenative programming language
A concatenative programming language is a point-free programming language in which all expressions denote functions and the juxtaposition of expressions denotes function composition...

.

How it works

Joy is unusual (except for function-level programming
Function-level programming
In computer science, function-level programming refers to one of the two contrasting programming paradigms identified by John Backus in his work on programs as mathematical objects, the other being value-level programming....

 languages and some esoteric ones, such as unlambda
Unlambda
Unlambda is a minimal, "nearly pure" 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 and an "apply" operator...

) in its lack of a lambda
Lambda calculus
In mathematical logic and computer science, lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion. The portion of lambda calculus relevant to computation is now called the untyped lambda calculus...

 operator, and therefore lack of formal parameters
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...

. To illustrate this with a common example, here is how the square function might be defined in an imperative programming language (C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

):


int square(int x)
{
return x * x;
}


The variable x is a formal parameter which is replaced by the actual value to be squared when the function is called. In a functional
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

 language (Scheme) the same function would be defined:


(define square
(lambda (x)
(* x x)))


This is different in many ways, but it still uses the formal parameter x in the same way. In Joy the square function is defined:

DEFINE square dup * .

In Joy, everything is a function that takes a stack
Stack (data structure)
In computer science, a stack is a last in, first out abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack,...

 as an argument and returns a stack as a result. For instance, the numeral '5' does not represent an integer constant, but instead a short program that pushes the number 5 onto the stack.
  • The dup operator simply duplicates the top element of the stack by pushing a copy of it.
  • The * operator pops two numbers off the stack and pushes their product.


So the square function makes a copy of the top element, and then multiplies the two top elements of the stack, leaving the square of the original top element at the top of the stack, with no need for a formal parameter. This makes Joy concise, as illustrated by this definition of quicksort:



DEFINE qsort
[small]
[]
[uncons [>] split]
wap] dip cons concat]
binrec .



"binrec" is one of Joy's many recursive
Recursion
Recursion is the process of repeating items in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other the nested images that occur are a form of infinite recursion. The term has a variety of meanings specific to a variety of disciplines ranging from...

 combinators, implementing binary recursion. It expects four quoted programs on top of the stack which represent:
  • the termination condition (if a list is "small" (1 or 0 elements) it is already sorted),
  • what to do if the termination condition is met (in this case nothing),
  • what to do by default (split the list into two halves by comparing each element with the pivot), and finally
  • what to do at the end (insert the pivot between the two sorted halves).

Mathematical purity

In Joy, the meaning function is a homomorphism
Homomorphism
In abstract algebra, a homomorphism is a structure-preserving map between two algebraic structures . The word homomorphism comes from the Greek language: ὁμός meaning "same" and μορφή meaning "shape".- Definition :The definition of homomorphism depends on the type of algebraic structure under...

 from the syntactic
Syntax
In linguistics, syntax is the study of the principles and rules for constructing phrases and sentences in natural languages....

 monoid
Monoid
In abstract algebra, a branch of mathematics, a monoid is an algebraic structure with a single associative binary operation and an identity element. Monoids are studied in semigroup theory as they are naturally semigroups with identity. Monoids occur in several branches of mathematics; for...

 onto the semantic
Semantics
Semantics is the study of meaning. It focuses on the relation between signifiers, such as words, phrases, signs and symbols, and what they stand for, their denotata....

 monoid
Monoid
In abstract algebra, a branch of mathematics, a monoid is an algebraic structure with a single associative binary operation and an identity element. Monoids are studied in semigroup theory as they are naturally semigroups with identity. Monoids occur in several branches of mathematics; for...

. That is, the syntactic relation of concatenation
Concatenation
In computer programming, string concatenation is the operation of joining two character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball"...

 of symbol
Symbol
A symbol is something which represents an idea, a physical entity or a process but is distinct from it. The purpose of a symbol is to communicate meaning. For example, a red octagon may be a symbol for "STOP". On a map, a picture of a tent might represent a campsite. Numerals are symbols for...

s maps directly onto the semantic relation of composition
Function composition
In mathematics, function composition is the application of one function to the results of another. For instance, the functions and can be composed by computing the output of g when it has an argument of f instead of x...

 of functions
Function (mathematics)
In mathematics, a function associates one quantity, the argument of the function, also known as the input, with another quantity, the value of the function, also known as the output. A function assigns exactly one output to each input. The argument and the value may be real numbers, but they can...

. It is a homomorphism
Homomorphism
In abstract algebra, a homomorphism is a structure-preserving map between two algebraic structures . The word homomorphism comes from the Greek language: ὁμός meaning "same" and μορφή meaning "shape".- Definition :The definition of homomorphism depends on the type of algebraic structure under...

 instead of an isomorphism
Isomorphism
In abstract algebra, an isomorphism is a mapping between objects that shows a relationship between two properties or operations.  If there exists an isomorphism between two structures, the two structures are said to be isomorphic.  In a certain sense, isomorphic structures are...

 because it is onto but not one-to-one, that is, some sequences of symbols have the same meaning (e.g. "dup +" and "2 *") but no symbol has more than one meaning.

Its library routines mirror those of ISO C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, though the current implementation is not easily extensible with functions written in C.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK