Professional Documents
Culture Documents
Homo Icon I City
Homo Icon I City
As noted above, a commonly cited example is the programming language Lisp, which
was created to be easy for lists manipulation and where the structure is given by
S-expressions that take the form of nested lists. Lisp programs are written in the
form of lists; the result is that the program can access its own functions and
procedures while running, and programmatically alter itself on the fly. Homoiconic
languages typically include full support of syntactic macros, allowing the
programmer to express transformations of programs in a concise way. Examples are
the programming languages Clojure (a contemporary dialect of Lisp), Rebol (also its
successor Red), Refal, and more recently Julia.
Contents
1 History
2 Uses and advantages
3 Implementation methods
3.1 In Lisp
3.2 In Prolog
3.3 In Rebol
4 See also
5 References
6 External links
History
The original source is the paper Macro Instruction Extensions of Compiler
Languages,[2] according to the early and influential paper TRAC, A Text-Handling
Language:[3]
One of the main design goals was that the input script of TRAC (what is typed in by
the user) should be identical to the text which guides the internal action of the
TRAC processor. In other words, TRAC procedures should be stored in memory as a
string of characters exactly as the user typed them at the keyboard. If the TRAC
procedures themselves evolve new procedures, these new procedures should also be
stated in the same script. The TRAC processor in its action interprets this script
as its program. In other words, the TRAC translator program (the processor)
effectively converts the computer into a new computer with a new program language
-- the TRAC language. At any time, it should be possible to display program or
procedural information in the same form as the TRAC processor will act upon it
during its execution. It is desirable that the internal character code
representation be identical to, or very similar to, the external code
representation. In the present TRAC implementation, the internal character
representation is based upon ASCII. Because TRAC procedures and text have the same
representation inside and outside the processor, the term homoiconic is applicable,
from homo meaning the same, and icon meaning representation.
[...]
Alan Kay used and possibly popularized the term "homoiconic" through his use of the
term in his 1969 PhD thesis:[4]
A notable group of exceptions to all the previous systems are Interactive LISP
[...] and TRAC. Both are functionally oriented (one list, the other string), both
talk to the user with one language, and both are "homoiconic" in that their
internal and external representations are essentially the same. They both have the
ability to dynamically create new functions which may then be elaborated at the
users's pleasure.
Their only great drawback is that programs written in them look like King
Burniburiach's letter to the Sumerians done in Babylonian cuniform! [...]
Implementation methods
All Von Neumann architecture 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.
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.
(setf expression (list '* (list 'sin 1.1) (list 'cos 2.03)) )
-> (* (SIN 1.1) (COS 2.03)) ; Lisp returns and prints the result
(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
2 ?- L = (X is 2*5), write_canonical(L).
is(_, *(2, 5))
L = (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.
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):
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:
>> block1: [ 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: