In last post, it was shown that in Lisp practice, the term "symbolic expression", shorter "sexpression", "sepxr", "sexp" is used on few different, although similar meanings.
It is not unusual. Other terms, even mathematical ones, like "lines" or "numbers" are not uniquely defined as well. Ambiguity usually motivates the search for the essence of the discussed entities; the result of the search is axiomatic theory.
For example, the axioms of natural numbers are developed in late 1880's by R. Dedekind and G. Peano.
Axioms of Natural Numbers
There is a set N ("numbers"), distinctive element of the N denoted with 1 ("base", "initial number") and mapping
successor: N → N
such that
Search for axioms of symbolic expressions might be equally justified. I designed following axioms (version in which lists are only shorter way of writing "dotted pairs") to emphasize the similarities to axioms of natural numbers.
Axioms of Symbolic Expressions
There is a set SEXPR ("symbolic expressions"), infinite set of distinct elements of the SEXPR denoted with A ("atoms") and mapping
cons: SEXPR × SEXPR → SEXPR
such that
Symbolic expressions in all three meanings satisfy the axioms; cons structures satisfy axiom (3) only if cyclic structures are not allowed, like in original, McCarthy's Lisp. I'm not aware of unintended, perverse models that satisfy given axioms; but I am not sure that such models do not exist.
There are only two differences between these axiom systems.
It is not obvious that symbolic expressions require infinitely many atoms; it could be only convenience. Perhaps Sexpressions like (A . (B . (C . D))) can be used instead of symbols like ABCD, eliminating need for infinitely many atoms.
It remains unclear why these two systems of axioms are so similar.

The Similarities Between Axioms of Natural Numbers and Axioms of Sexpressions.
.
Three Meanings of The Term 'Sexpression.'
.
Three meanings of the term 'Sexpression' "Symbolic expressions" or "Sexpressions" are the basic data type in Lisp. Particularly, Lisp programs are Sexpressions as well. The notion has immense importance in Lisp community. Surprisingly, designers of recent Lisp dialects avoid the term "symbolic expression" or "Sexpression". It is used once in Picolisp documentation; only twice, almost accidentally, in CLtL2, and it isn't used in CL Hyperspec or recent Scheme standards. Clojure, according to its web site "extends the codeasdata system beyond parenthesized lists (sexpressions) to vectors and maps." Only Newlisp documentation appear to regularly uses the term. However, the term Sexpression is still extensively used in daily communication and literature. Unfortunately, there is no universal, unique meaning. Inconsistent use is sometimes noted; for instance, in P. Siebel's "Practical Common Lisp." More frequently, it is ignored. For J. McCarthy, Sexpression is finite sequence consisting of dots and parentheses and symbols. The symbols, truly atomic, cannot be analysed on characters. For instance, Sexpression (left.right) is sequence of five elements: (, left, ., right and ). More often^{1}, Sexpression is seen as sequence of characters. In that meaning, Sexpression (left . right) is sequence consisting of 14 elements. The most usually^{2}, Sexpression is data structure, perhaps tree consisting of cons cells and symbols. In that meaning Sexpression (left . right) is cons cell, containing adresses of symbols left and right in memory. 1 For instance, in E. Shaphiro, "Common Lisp  an interactive approach"; F. Turbak and D. Gifford, "Design concept of programming languages." 2 For instance, in J. and G. Sussman and H. Abelson, "SICP"; D. Touretsky, "Common Lisp  A gentle introduction to symbolic computation"; R. Finkel, "Advanced programming languages design". 
Implementing Data Structures with Symbols.
.
For instance, matrices 2 x 2 can be implemented with symbols as a11 instead of a[1,1] in some other language, or (a 1 1) in Lisp and names could be created as needed. The code will be more complicated and less efficient than if matrices are directly supported by language, but neither one is essential problem  complexity of the code can be "abstracted away" with functions, and computational complexity is increased only for factor of constant * log n where n is the number of used symbols (if symbol reaching in Lisp is implemented efficiently.) Newlisp code follows. (define (matrixelement m i j) (sym (append (string m) (string i) (string j)))) (define (setmatrix m m11 m12 m21 m22) (for(i 1 2) (for(j 1 2) (set (matrixelement m i j) (nth (+ (* 2 ( i 1)) ( j 1)) (list m11 m12 m21 m22)))))) (define (printlnmatrix m) (for(i 1 2) (for(j 1 2) (println m "[" i "," j "]=" (eval (matrixelement m i j)))))) (define (multiplymatrix a b c) (for(i 1 2) (for(j 1 2) (set (matrixelement c i j) 0) (for(k 1 2) (set (matrixelement c i j) (+ (eval (matrixelement c i j)) (* (eval (matrixelement a i k)) (eval (matrixelement b k j))))))))) (setmatrix (quote X) 1 2 3 4) (setmatrix (quote Y) 2 3 4 5) (multiplymatrix (quote X) (quote Y) (quote Z)) (printlnmatrix (quote Z))  Program output Z[1,1]=10 Z[1,2]=13 Z[2,1]=22 Z[2,2]=29 
More on Another Encoding of Sexpressions in Symbols.
.
More on Another Encoding of Sexpressions in Symbols
In previous post, I have shown another interesting method for encoding of Sexpressions into symbols. In this post, I'll show how it can be implemented in Lisp dialects that print symbols on different way from Sexpressions, i.e. encapsulated in vertical bars or other symbols (Common Lisp, ISLisp, Picolisp, some implementations of Scheme). This is, normally, good feature. However, repeated encoding of the symbols results in exponential growth. So, we must get rid of those vertical bars inside our symbols.The code, here in Common Lisp, is sort of trickier than it is in Newlisp or it would be in Clojure. (defun decodedeep (r) (cond ((symbolp r) (let((result (readfromstring (string r)))) (if (symbolp result) result (decodedeep result)))) ((listp r) (mapcar (quote decodedeep) r)))) (defun sexpr>symbol (r) (makesymbol (writetostring (list 'q (decodedeep r))))) (defun encodedeep (r) (cond ((symbolp r) r) ((listp r)(if (eq (first r) (quote q)) (makesymbol (writetostring r)) (mapcar (quote encodedeep) r))))) (defun stripq (r) (first (rest r))) (defun symbol>sexpr (r) (encodedeep (stripq (readfromstring (string r)))))  [43]> (setq q0 (sexpr>symbol (list (quote b) (quote c)))) #:(Q (B C)) [44]> (setq s (sexpr>symbol (list (quote a) q0))) #:(Q (A (Q (B C)))) [45]> (setq s0 (symbol>sexpr s)) (A #:(Q (B C))) 
Another Encoding of Sexpressions in Symbols.
.
Another Encoding of Sexpressions in Symbols.
Unlike encoding from the last post, this one maintains form of Sexpression. If e is Sexpression, then its code is symbol (Q e). Encoding is relatively simple, because built in functions for conversion of symbols and strings into symbols can be used; especially if symbols converted to strings are not encapsulated within vertical bars or quotation marks. Decoding is slightly more complicated. For instance, symbol
(Q (a (Q b)))
should be decoded into list (a (Q b)) where (Q b) isn't list, but symbol. Hence, built in conversion functions are not enough, but one must define his own "code walker." The code in Newlisp follows. (define (sexpr>symbol r) (sym (string (list (quote Q) r)))) (define (qlist? r) (and (list? r) (not (empty? r)) (= (first r) (quote Q)))) (define (symbol>sexpr r) (let((codewalker (lambda(r) (cond ((symbol? r) r) ((qlist? r) (sym (string r))) ((not (qlist? r)) (map codewalker r)))))) (codewalker (last (readexpr (string r))))))  > (setq s (sexpr>symbol (quote (a b)))) (Q (a b)) > (symbol? s) true > (setq q0 (sexpr>symbol (quote b))) (Q b) > (setq s (sexpr>symbol (list (quote a) q0))) (Q (a (Q b))) > (symbol? s) true > (setq s0 (symbol>sexpr s)) (a (Q b)) > (symbol? s0) nil > (symbol? (last s0)) true Behaviour of Q strongly reminds on QUOTE. 
More Sophisticated Encoding of Sexprs into Symbols.
More Sophisticated Encoding of Sexprs into Symbols
Encoding of Sexpressions into symbols is frequent topic of
discussion in this blog. It is motivated by the idea that
names should be equally flexible as Sexpressions. (Actually,
it might be the best if not only every symbol is Sexpression,
but also every Sexpression is the symbol. OK, it is almost
mystical statement.)Trivial encoding was demonstrated in last post: Sexpression is encoded in symbol which has exactly same characters as printed representation of Sexpression. For instance, Sexpression (+ a b) is encoded into symbol (+ a b). This trivial encoding has some limitations. For instance, if we look at symbol
((+ a b) c)
and try to decode it, we'll find that it cannot be determined whether (+ a b) is Sexpression, or it is symbol. In some Lisp dialects (Common Lisp, Picolisp, ISLisp) there is no such problem. Symbol (+ a b) is written as (+ a b) so difference between Sexpression and symbol is obvious. However, in these dialects, repeated encoding results in the symbols that grow exponentially. In this post more sophisticated encoding, without that problem, is presented. Let us define encoding k, such that for every Sexpression e, k(e) is defined as follows.
k(a) = a;
k((a b)) = [k(a) k(b)]; = [a;b;]; If only codes of the Sexpressions contain characters [, ] and ;, then encoding k is injection, i.e. for two Sexpressions e and f,
k( e ) = k( f ) => e = f.
Furthermore, there is no exponential explosion in case of multiple encoding. For instance,
k((a b)) = [a;b;];
k(k((a b))) = [a;b;];; k(k(k((a b)))) = [a;b];;; Here is implementation of encoding and decoding in R^{5}RS Scheme. (define (sexpr>string L) (stringappend (if (symbol? L) (symbol>string L) (stringappend "[" (apply stringappend (map sexpr>string L)) "]")) ";")) (define (sexpr>symbol L) (string>symbol (sexpr>string L))) (define (string>sexpr S) (let((S1 (substring S 0 ( (stringlength S) 1)))) (if (equal? (stringref S1 ( (stringlength S1) 1)) #\]) (let((substringbegin 1) (level 0) (result (list))) (do ((i 1 (+ i 1))) ((= i (stringlength S1)) result) (if (and (= level 0) (equal? (stringref S1 i) #\;) (not (equal? (stringref S1 (+ i 1)) #\;))) (begin (set! result (append result (list (string>sexpr (substring S1 substringbegin (+ i 1)))))) (set! substringbegin (+ i 1)))) (cond ((equal? (stringref S1 i) #\[) (set! level (+ level 1))) ((equal? (stringref S1 i) #\]) (set! level ( level 1)))))) (string>symbol (substring S 0 ( (stringlength S) 1)))))) (define (symbol>sexpr s) (string>sexpr (symbol>string s)))  > (sexpr>symbol (quote (a b))) [a;b;]; > (define s2 (sexpr>symbol (quote ([a;b;]; c)))) > s2 [[a;b;];;c;]; > (symbol>sexpr s2) ([a;b;]; c) 
Exponential Explosion of Escape Characters.
Exponential Explosion of Escape Characters
This time, Common Lisp:

Tangent.
.

Subscribe to:
Posts (Atom)