Homoiconicity
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, homoiconicity (from the
Greek Greek may refer to: Greece Anything of, from, or related to Greece, a country in Southern Europe: *Greeks, an ethnic group. *Greek language, a branch of the Indo-European language family. **Proto-Greek language, the assumed last common ancestor ...
words ''homo-'' meaning "the same" and ''icon'' meaning "representation") is a property of some
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s. A language is homoiconic if a program written in it can be manipulated as data using the language, and thus the program's internal representation can be inferred just by reading the program itself. This property is often summarized by saying that the language treats "code as data". In a homoiconic language, the primary representation of programs is also a
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, ...
in a
primitive type In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled pr ...
of the language itself. This makes
metaprogramming Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself ...
easier than in a language without this property:
reflection Reflection or reflexion may refer to: Science and technology * Reflection (physics), a common wave phenomenon ** Specular reflection, reflection from a smooth surface *** Mirror image, a reflection in a mirror or in water ** Signal reflection, in ...
in the language (examining the program's entities at runtime) depends on a single, homogeneous structure, and it does not have to handle several different structures that would appear in a complex syntax. Homoiconic languages typically include full support of syntactic macros, allowing the programmer to express transformations of programs in a concise way. A commonly cited example is
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
, which was created to allow for easy list manipulations and where the structure is given by
S-expression In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming la ...
s that take the form of
nested ''Nested'' is the seventh studio album by Bronx-born singer, songwriter and pianist Laura Nyro, released in 1978 on Columbia Records. Following on from her extensive tour to promote 1976's ''Smile'', which resulted in the 1977 live album '' Sea ...
lists, and can be manipulated by other Lisp code. Other examples are the programming languages
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
(a contemporary dialect of Lisp), Rebol (also its successor Red), Refal,
Prolog Prolog is a logic programming language associated with artificial intelligence and computational linguistics. Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is intended primarily ...
, and possibly
Julia Julia is usually a feminine given name. It is a Latinate feminine form of the name Julio and Julius. (For further details on etymology, see the Wiktionary entry "Julius".) The given name ''Julia'' had been in use throughout Late Antiquity (e.g ...
(see the section “Implementation methods” for more details).


History

The original source is the paper ''Macro Instruction Extensions of Compiler Languages'', according to the early and influential paper ''TRAC, A Text-Handling Language'':
Alan Kay Alan Curtis Kay (born May 17, 1940) published by the Association for Computing Machinery 2012 is an American computer scientist best known for his pioneering work on object-oriented programming and windowing graphical user interface (GUI) d ...
used and possibly popularized the term "homoiconic" through his use of the term in his 1969 PhD thesis:


Uses and advantages

One advantage of homoiconicity is that extending the language with new concepts typically becomes simpler, as data representing code can be passed between the
meta Meta (from the Greek μετά, '' meta'', meaning "after" or "beyond") is a prefix meaning "more comprehensive" or "transcending". In modern nomenclature, ''meta''- can also serve as a prefix meaning self-referential, as a field of study or end ...
and base layer of the program. The
abstract syntax tree In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurr ...
of a function may be composed and manipulated as a data structure in the meta layer, and then evaluated. It can be much easier to understand how to manipulate the code since it can be more easily understood as simple data (since the format of the language itself is as a data format). A typical demonstration of homoiconicity is the
meta-circular evaluator In computing, a meta-circular evaluator (MCE) or meta-circular interpreter (MCI) is an interpreter which defines each feature of the interpreted language using a similar facility of the interpreter's host language. For example, interpreting a lamb ...
.


Implementation methods

All
Von Neumann architecture The von Neumann architecture — also known as the von Neumann model or Princeton architecture — is a computer architecture based on a 1945 description by John von Neumann, and by others, in the '' First Draft of a Report on the EDVAC''. T ...
systems, which includes the vast majority of general purpose computers today, can implicitly be described as homoiconic due to the way that raw machine code executes in memory, the data type being bytes in memory. However, this feature can also be abstracted to the programming language level. Languages such as
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
and its dialects, such as
Scheme A scheme is a systematic plan for the implementation of a certain idea. Scheme or schemer may refer to: Arts and entertainment * ''The Scheme'' (TV series), a BBC Scotland documentary series * The Scheme (band), an English pop band * ''The Schem ...
,Homoiconic languages (archived)
in ''true Blue'' blog at Oracle
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
, and Racket employ
S-expression In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming la ...
s to achieve homoiconicity. Other languages which are considered to be homoiconic include: * APL *
Curl cURL (pronounced like "curl", UK: , US: ) is a computer software project providing a library (libcurl) and command-line tool (curl) for transferring data using various network protocols. The name stands for "Client URL". History cURL was ...
Homoiconic Languages
/ref> *
Elixir ELIXIR (the European life-sciences Infrastructure for biological Information) is an initiative that will allow life science laboratories across Europe to share and store their research data as part of an organised network. Its goal is to bring t ...
* Io *
Julia Julia is usually a feminine given name. It is a Latinate feminine form of the name Julio and Julius. (For further details on etymology, see the Wiktionary entry "Julius".) The given name ''Julia'' had been in use throughout Late Antiquity (e.g ...
was initially marketed as a homoiconic language. However, at least one of the creators, Stefan Karpinski, has since taken an agnostic position on the label, saying that “you can decide for yourself whether
ulia Ulia Fidentia or simply Ulia was a Roman municipium in the province of Hispania Baetica. The site is the location of the current city of Montemayor, Córdoba. During the Second Hispanian campaign of Caesar's Civil War almost all the towns of Hi ...
qualifies” as homoiconic. *
Prolog Prolog is a logic programming language associated with artificial intelligence and computational linguistics. Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is intended primarily ...
* Rebol * Red *
SNOBOL SNOBOL ("StriNg Oriented and symBOlic Language") is a series of programming languages developed between 1962 and 1967 at AT&T Bell Laboratories by David J. Farber, Ralph E. Griswold and Ivan P. Polonsky, culminating in SNOBOL4. It was one of ...
* Tcl *
XSLT XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subseq ...
* REFAL *
Rexx Rexx (Restructured Extended Executor) is a programming language that can be interpreted or compiled. It was developed at IBM by Mike Cowlishaw. It is a structured, high-level programming language designed for ease of learning and reading. P ...
*
Wolfram Language The Wolfram Language ( ) is a general multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary structures and data. It ...
* R language


In Lisp

Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
uses
S-expression In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming la ...
s as an external representation for data and code. S-expressions can be read with the primitive Lisp function READ. READ returns Lisp data: lists,
symbols A symbol is a mark, sign, or word that indicates, signifies, or is understood as representing an idea, object, or relationship. Symbols allow people to go beyond what is known or seen by creating linkages between otherwise very different co ...
, numbers, strings. The primitive Lisp function EVAL uses Lisp code represented as Lisp data, computes side-effects and returns a result. The result will be printed by the primitive function PRINT, which creates an external S-expression from Lisp data. Lisp data, a list using different data types: (sub)lists, symbols, strings and integer numbers. ((:name "john" :age 20) (:name "mary" :age 18) (:name "alice" :age 22)) Lisp code. The example uses lists, symbols and numbers. (* (sin 1.1) (cos 2.03)) ; in infix: sin(1.1)*cos(2.03) Create above expression with the primitive Lisp function LIST and set the variable EXPRESSION to the result (setf expression (list '* (list 'sin 1.1) (list 'cos 2.03)) ) -> (* (SIN 1.1) (COS 2.03)) ; Lisp returns and prints the result (third expression) ; the third element of the expression -> (COS 2.03) Change the COS term to SIN (setf (first (third expression)) 'SIN) ; The expression is now (* (SIN 1.1) (SIN 2.03)). Evaluate the expression (eval expression) -> 0.7988834 Print the expression to a string (print-to-string expression) -> "(* (SIN 1.1) (SIN 2.03))" Read the expression from a string (read-from-string "(* (SIN 1.1) (SIN 2.03))") -> (* (SIN 1.1) (SIN 2.03)) ; returns a list of lists, numbers and symbols


In Prolog

1 ?- X is 2*5. X = 10. 2 ?- L = (X is 2*5), write_canonical(L). is(_, *(2, 5)) L = (X is 2*5). 3 ?- L = (ten(X):-(X is 2*5)), write_canonical(L). :-(ten(A), is(A, *(2, 5))) L = (ten(X):-X is 2*5). 4 ?- L = (ten(X):-(X is 2*5)), assert(L). L = (ten(X):-X is 2*5). 5 ?- ten(X). X = 10. 6 ?- On line 4 we create a new clause. The operator :- separates the head and the body of a clause. With assert/1* we add it to the existing clauses (add it to the "database"), so we can call it later. In other languages we would call it "creating a function during runtime". We can also remove clauses from the database with abolish/1, or retract/1. * The number after the clause's name is the number of arguments it can take. It is also called
arity Arity () is the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science. In mathematics, arity may also be named ''rank'', but this word can have many other meanings in mathematics. ...
. We can also query the database to get the body of a clause: 7 ?- clause(ten(X),Y). Y = (X is 2*5). 8 ?- clause(ten(X),Y), Y = (X is Z). Y = (X is 2*5), Z = 2*5. 9 ?- clause(ten(X),Y), call(Y). X = 10, Y = (10 is 2*5). call is analogous to Lisp's eval function.


In Rebol

The concept of treating code as data and the manipulation and evaluation thereof can be demonstrated very neatly in Rebol. (Rebol, unlike Lisp, does not require parentheses to separate expressions). The following is an example of code in Rebol (Note that >> represents the interpreter prompt; spaces between some elements have been added for readability): >> repeat i 3 print_[_i_"hello"_.html" ;"title="i_"hello"_.html" ;"title="print [ i "hello" ">print [ i "hello" ">i_"hello"_.html" ;"title="print [ i "hello" ">print [ i "hello" /syntaxhighlight> 1 hello 2 hello 3 hello (repeat is in fact a built-in function in Rebol and is not a language construct or keyword). By enclosing the code in square brackets, the interpreter does not evaluate it, but merely treats it as a block containing words: print_[_i_"hello"_.html" ;"title="repeat i 3 print [ i "hello" ">repeat i 3 print [ i "hello" ] This block has the type block! and can furthermore be assigned as the value of a word by using what appears to be a syntax for assignment, but is actually understood by the interpreter as a special type (set-word!) and takes the form of a word followed by a colon: >> block1: print_[_i_"hello"_.html" ;"title="repeat i 3 print [ i "hello" ">repeat i 3 print [ i "hello" ] ;; Assign the value of the block to the word `block1`

[repeat i 3 [print [i "hello"] >> type? block1 ;; Evaluate the type of the word `block1`

block! The block can still be interpreted by using the do function provided in Rebol (similar to eval in Lisp). It is possible to interrogate the elements of the block and change their values, thus altering the behavior of the code if it were to be evaluated: >> block1/3 ;; The third element of the block

3 >> block1/3: 5 ;; Set the value of the 3rd element to 5

5 >> probe block1 ;; Show the changed block

epeat_i_5_[print_[i_"hello".html" ;"title="rint_[i_"hello".html" ;"title="epeat i 5 [print [i "hello"">epeat i 5 [print [i "hello"">rint_[i_"hello".html" ;"title="epeat i 5 [print [i "hello"">epeat i 5 [print [i "hello" >> do block1 ;; Evaluate the block 1 hello 2 hello 3 hello 4 hello 5 hello


See also

* Cognitive dimensions of notations, design principles for programming languages' syntax * Concatenative programming language * Language-oriented programming * Symbolic programming * Self-modifying code * LISP (programming language), perhaps the most well-known example of a homoiconic language *
Metaprogramming Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself ...
, a programming technique for which homoiconicity is very useful *
Reification (computer science) Reification is the process by which an abstract idea about a computer program is turned into an explicit data model or other object created in a programming language. A computable/addressable object—a resource—is created in a system as a prox ...


References

{{reflist


External links


Definition of Homoiconic at the C2 Wiki
Programming language topics