1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
\section{Data Types}
\markright{\arabic{section}. Data Types}
Like other Lisps, it is data objects that are typed, not variables.
Any variable can have any object as its value.
Of course, it is possible to
declare the type of object which is bound to a variable, but in many cases
it is only advisory information to the compiler to generate faster code.
In EusLisp, it needs to distinguish pointers and objects.
Numbers are immediately represented by pointers and all the others
are represented by objects referenced by pointers.
\subsection{Numbers}
There are two kinds of numbers, that is,
integer and float (floating-point number), both are represented
with 29 bits value and 1 bit sign.
Thus, integers range from -536,870,912 to 536,870,911.
Floats can represent plus/minus from 4.8E-38 to 3.8E38 with the
approximate accuracy of 6 digits in decimal, i.e.,
floating-point epsilon is about 1/1,000,000.
Integer and float numbers are always represented by pointers, and not by objects,
which is the only exception of EusLisp's object orientation.
However, since numbers never waste heap memory, number crunching application
runs efficiently without causing garbage collection.
Other number types such as ratio and complex numbers are normal EusLisp objects.
EusLisp does not have the character type,
and characters are represented by integers.
In order to write a program independent of character code sets,
%\#$\backslash$
\verb+#\+ reader dispatch macro is useful, but when the character is read,
it is converted to numerical representation, and the printer does not
know how to reconvert it to
%\#$\backslash$
\verb+#\+ notation.
\subsection{Class Hierarchy}
Data structures are defined by classes, and their behaviors are defined by
methods.
In EusLisp, several dozens of classes are predefined in tree order
as depicted in table \ref{ClassHierarchy}.
The class 'object' at the leftmost is the ultimate super-class of
all the classes in EusLisp.
User-defined classe can inherit any of these built-in classes.
\begin{table}
\begin{verbatim}
+--- cons
+--- propertied-object
| +--- symbol ------ foreign-pod
| +--- metaclass --- vectorclass --- cstructclass
| +--- readtable
| +--- package
| +--- hash-table
| +--- array + ----- matrix
| + ----- pixel-image
| +--- stream --+-- file-stream
| +-- broadcast-stream
| +--- io-stream --- socket-stream
object-----+--- compiled-code --+-- foreign-code
| +-- foreign-module
| +-- closure
+--- label-reference
+--- socket-port
+--- pathname
+--- vector --------+--- float-vector
+--- integer-vector
+--- string --+-- cstructure
| +-- socket-address
+--- bit-vector
+--- foreign-string
\end{verbatim}
\caption{\label{ClassHierarchy}Hierarchy of Predefined Classes}
\end{table}
A class is defined by the following defclass macro, or defstruct macro.
\ptext{
(defclass class-name \&key \= :super \hspace{15mm} \= class \\
\> :slots \> () \\
\> :metaclass \> metaclass \\
\> :element-type \> t \\
\> :size -1\\ ) \\
(defstruct struct-name slots...) \\
(defstruct (struct-name [struct-options ...]) \\
\hspace{25mm} (slot-name1 [slot-option...]) \\
\hspace{25mm} (slot-name2 [slot-option...]) \\
\hspace{25mm} ...) \\}
Methods are defined by the defmethod special form of following syntax.
\ptext{
(defmethod class-name \\
(:method-name1 (parameter...) . body1) \\
(:method-name2 (parameter...) . body2) \\
...)
}
Field definitions for each class are found in eus.h header file,
and by {\bf describ}ing the symbol-value of each class symbol
the slot names are obtained on line.
Followings are definitions of built-in classes.
\ptext{
(defclass {\bf object} :super {\bf nil} :slots ())
}
\ptext{
(defclass {\bf cons} :super {\bf object} :slots (car cdr))
}
\ptext{
(defclass {\bf propertied-object} :super {\bf object} \\
\hspace{20mm} \= :slots (plist)) \hspace{10mm} ;property list \\
}
\ptext{
(defclass {\bf symbol} :super {\bf propertied-object} \\
\hspace{20mm} :slots (\=value \hspace{15mm} \= ;specially bound value \\
\> vtype \> ;const(0),var(1),special(2) \\
\> function \> ;global func def \\
\> pname \> ;print name string \\
\> homepkg)) \> ;home package \\}
\ptext{
(defclass {\bf package} :super {\bf propertied-object} \\
\hspace{20mm} :slots (\= names \hspace{15mm} \= ;list of package name and nicknames\\
\> uses \> ;spreaded use-package list \\
\> symvector \> ;hashed obvector \\
\> symcount)) \> ;number of interned symbols \\
}
\ptext{
(defclass {\bf stream} :super {\bf propertied-object} \hspace{20mm} \\
\hspace{20mm} :slots (\= direction \hspace{3mm} \= ;:input or :output, nil if closed \\
\> buffer \> ;buffer string \\
\> count \> ;current character index \\
\> tail)) \> ;last character index \\
}
\ptext{
(defclass {\bf filestream} :super {\bf stream} \\
\hspace{20mm} :slots (\= fd \hspace{10mm} \= ;file descriptor (integer)\\
\> fname))\> ;file name str; qid for msgq \\
}
\ptext{
(defclass {\bf iostream} \= :super {\bf propertied-object}\\
\> :slots (instream outstream))}
\ptext{
(defclass {\bf readtable} \= :super {\bf propertied-object} \\
\> :slots (syntax macro dispatchmacro))}
\ptext{
(defclass {\bf array} :super {\bf propertied-object} \\
\hspace{20mm} :slots (\= entity \hspace{12mm}\= ;simple vector to hold array entity \\
\> rank \> ;number of dimensions: 0-7 \\
\> fillpointer \> ;pointer to push next element \\
\> offset \> ;offset for displaced array \\
\> dim0,dim1,dim2,dim3,dim4,dim5,dim6)) ;dimensions \\}
\ptext{
(defclass {\bf metaclass} :super {\bf propertied-object} \\
\hspace{20mm} :slots (\= name \hspace{8mm} \= ;class name symbol \\
\> super \> ;super class \\
\> cix \> ;class id \\
\> vars \> ;var name vector including inherited vars \\
\> types \> ;type vector of object variables \\
\> forwards \> ;componets to which messages are forwarded \\
\> methods)) \> ;method list \\ }
\ptext{
(defclass {\bf vectorclass} :super {\bf metaclass} \\
\hspace{20mm} :slots (\= elment-type \hspace{4mm} \= ;vector element type 0-7\\
\> size)) \> ;vector size; 0 if unspecified \\ }
\ptext{
(defclass {\bf label-reference} \hspace{8mm} ;for reading \#n=, \#n\# objects \\
\hspace{20mm} \= :super {\bf object} \\
\> :slots (label value unsolved next)) \\}
\ptext{
(defclass {\bf compiled-code} :super {\bf object} \\
\hspace{20mm} :slots (\= codevector \\
\> quotevector \\
\> type \hspace{15mm} \= ;0=func, 1=macro, 2=special \\
\> entry)) \> ;entry offset }
\ptext{
(defclass {\bf closure} \= :super {\bf compiled-code} \\
\> :slots (env1 env2));environment}
\ptext{
(defclass {\bf foreign-code} :super {\bf compiled-code} \\
\hspace{20mm} :slots (\= paramtypes \hspace{10mm} \= ;list of parameter types\\
\> resulttype)) \> ;function result type}
\ptext{
(defclass {\bf foreign-module} :super {\bf compiled-code} \\
\hspace{20mm} :slots (\= symbol-table \hspace{5mm} \= ;hashtable of symbols defined \\
\> object-file)) \> ;name of the object file loaded}
\ptext{
(defclass {\bf vector} :super {\bf object} :slots (size))}
\ptext{
(defclass {\bf floatvector} :super {\bf vector} :element-type :float)}
\ptext{
(defclass {\bf string} :super {\bf vector} :element-type :char)}
\newpage
\section{Forms and Evaluation}
\markright{\arabic{section}. Forms and Evaluation}
\subsection{Atoms}
A data object which is not a cons is always an atom, no matter what complex
structure it may have.
Note that NIL, which is sometimes noted as () to represent an empty
list, is also an atom.
Every atom except a symbol is always evaluated to itself,
although quoting is required in some other Common Lisp implementations.
\subsection{Scoping}
Every symbol has the nature of a variable.
A symbol is evaluated to its value determined in the current binding context.
There are two kinds of variable bindings;
the lexical or static binding and the special or dynamic binding.
Lexically bound variables are introduced by lambda or let,let* special forms
unless they are declared special.
Lexical binding can be nested and the only one binding which is introduced
innermost level is visible, hiding outer lexical bindings and the special
binding.
Special variables are used in two ways:
one is for global variables, and the other is for dynamicaly scoped
local variables which are visible even at the outside of
the lexical definitions as long as the binding is in effect.
In the latter case, special variables are needed to be declared special.
The declaration is recognized not only by the compiler, but also by
the interpreter.
According to the Common Lisp's terms, special variables are said to have
indefinite scope and dynamic extent.
\footnote{In Maclisp, all the variables are taken to be special by the
interpreter, and lexical by the compiler if not declared special.
In Etalisp, all variables are special and no declaration is recognized.}
Even if there exists a lexical variable in a certain scope,
the same variable name can be redeclared inner scope
to refer to specially bound value.
Function {\bf symbol-value} is also available to access the special value.
Note that {\bf set} function works only for special variable, i.e.
it cannot be used to change the value of lambda or let variables
unless they are declared special.
\begin{verbatim}
(let ((x 1))
(declare (special x))
(let* ((x (+ x x)) (y x))
(let* ((y (+ y y)) (z (+ x x)))
(declare (special x))
(format t "x=~S y=~s z=~s~%" x y z) ) ) )
--> x=1 y=4 z=2
\end{verbatim}
A symbol can be declared to be a constant by {\bf defconstant} macro.
Once declared, an attempt to change the value signals an error hereafter.
Moreover, such a constant symbol is inhibitted being used as
a name of variable even for a local variable.
NIL and T are examples of such constants.
Symbols in the keyword package are always declared to be constants
when they are created.
In contrast, {\bf defvar} and {\bf defparameter} macro declare
a symbol to be a special variable.
{\bf Defvar} initializes the value only if the symbol is unbound,
and does nothing when it already has a value assigned,
while {\bf defparameter} always resets the value.
When a symbol is referenced and there is no lexical binding for the symbol,
then its special value is taken.
However, if no value has ever been given to its special value yet,
unbound variable error is signaled.
\subsection{Generalized Variables}
Each value of a variable is remembered in the slot of the object or in
the stack frame.
Two primitive operations, {\em access} and {\em update}, must be defined
to recall and alter the value of a slot.
Instead of defining two distinct primitives for every slot of objects,
EusLisp, like Common Lisp, provides uniform update operations
based on the generalized variable concept.
In this concept, a common form is recognized as either a value access form
or a slot location specifier.
Thus, you only need to remember accessing form for each slot and
update is achieved by {\bf setf} macro used in conjunction with the access form.
For an example, {\tt (car x)} can be used to replace the value
in the car slot of {\tt x} when used with {\bf setf},
as well as to take the car value out of the list.
This method is also applicable for all the user defined objects.
When a class or a structure is defined, the access and undate forms
for each slot are automatically defined.
Each of those forms is defined as a macro whose name is the concatenation of
the class name and slot name.
For example, car of a cons can be addressed by {\tt (cons-car '(a b c))}.
\begin{verbatim}
(defclass person :super object :slots (name age))
(defclass programmer :super person :slots (language machine))
(setq x (instantiate programmer))
(setf (programmer-name x) "MATSUI"
(person-age x) 30)
(incf (programmer-age x))
(programmer-age x) --> 31
(setf (programmer-language x) 'EUSLISP
(programmer-machine x) 'SUN4)
\end{verbatim}
Array elements can be accessed in the same manner.
\begin{verbatim}
(setq a (make-array '(3 3) :element-type :float))
(setf (aref a 0 0) 1.0 (aref a 1 1) 1.0 (aref a 2 2) 1.0)
a --> #2f((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
(setq b (instantiate bit-vector 10)) --> #*0000000000
(setf (bit b 5) 1)
b --> #*0000010000
\end{verbatim}
In order to define special setf methods for particular objects,
{\bf defsetf} macro is provided.
\begin{verbatim}
(defsetf symbol-value set)
(defsetf get (sym prop) (val) `(putprop ,sym ,val ,prop))
\end{verbatim}
\subsection{Special Forms}
\begin{table}
\begin{tabular}{|l l l|} \hline
AND & FLET & QUOTE \\
BLOCK & FUNCTION & RETURN-FROM\\
CATCH & GO & SETQ \\
COND & IF & TAGBODY \\
DECLARE & LABELS & THE \\
DEFMACRO & LET & THROW \\
DEFMETHOD & LET* & UNWIND-PROTECT \\
DEFUN & PROGN & WHILE \\
EVAL-WHEN & OR & \\
\hline
\end{tabular}
\caption{list of all special forms}
\end{table}
All the special forms are listed in table-1.
Special forms are essential language constructs for the management of
evaluation contexts and control flows.
The interpreter and compiler must have special knowledge to
process each of these constructs properly, while the application
method is uniform for all functions.
Users cannot add their own special form definition.
\subsection{Macros}
Macro is a convenient method to expand language constructs.
When a macro is called, arguments are passed to the macro body, that is a
macro expansion function, without evaluating them.
Then, the macro expansion function expands the arguments,
and returns the new form.
The resulted form is then evaluated again outside the macro.
It is an error to apply a macro or special form to a list of arguments.
{\bf Macroexpand} function can be used for the explicit macro expansion.
Though macro runs slowly when interpreted,
it speeds up compiled code execution,
because macro expansion is taken at compile-time only once
and no overhead is left to run-time.
Note that explicit call to eval or apply in the macro function may
produce different results between interpreted execution
and the compiled execution.
\subsection{Functions}
A function is expressed by a lambda form which is merely a list
whose first element is {\bf lambda}.
If a lambda form is defined for a symbol using {\bf defun}, it can be referred
as a global function name.
Lambda form takes following syntax.
\ptext{
(lambda (\= \{var\}* \\
\> [\&optional \{var $|$ (var [initform])\}*] \\
\> [\&rest form] \\
\> [\&key \= \{var $|$ (var [initform]) $|$ ((:keyword var) [initform])\}* \\
\> \> [\&allow-other-keys]] \\
\> [\&aux \{var $|$ (var [initform])\}*]) \\
\hspace{10mm} \{declaration\}* \\
\hspace{10mm} \{form\}*) \\
}
There is no function type such as EXPR, LEXPR, FEXPR, etc.:
arguments to functions are always evaluated before its application,
and the number of acceptable arguments is determined by lambda-list.
Lambda-list specifies the sequence of parameters to the lambda form.
Each of {\bf \&optional, \&rest, \&key } and {\bf \&aux} has special
meaning in lambda-lists, and these symbols cannot be used as variable
names.
Supplied-p variables for \&optional or \&key parameters are not supported.
Since a lambda form is indistinguishable from normal list data,
{\bf function} special form should be used to inform the interpreter and
compiler the form is intended to be a function.
{\bf function} is also important to freeze the environment onto the function,
so that all the lexical variables can be accesible in the function,
even the function is passed to another function of different lexical scope.
The following program does not work either interpretedly nor after compiled,
since {\tt sum} of {\tt let} be seen inside lambda form.
\begin{verbatim}
(let ((x '(1 2 3)) (sum 0))
(mapc '(lambda (x) (setq sum (+ sum x))) x))
\end{verbatim}
To get expected result, it should be written as follows:
\begin{verbatim}
(let ((x '(1 2 3)) (sum 0))
(mapc #'(lambda (x) (setq sum (+ sum x))) x ))
\end{verbatim}
\#' is abrebiated expression for {\bf function},
i.e. {\tt \#'(lambda (x) x)} is equivalent to
{\tt (function (lambda (x) x))}.
Here is another example of what is called a funarg problem:
\begin{verbatim}
(defun mapvector (f v)
(do ((i 0 (1+ i)))
((>= i (length v)))
(funcall f (aref v i))))
(defun vector-sum (v)
(let ((i 0))
(mapvector #'(lambda (x) (setq i (+ i x))) v)
i))
(vector-sum #(1 2 3 4)) --> 10
\end{verbatim}
EusLisp's closure cannot have indefinite extent:
i.e. a closure can only survive as long as its outer extent is in effect.
This means that a closure cannot be used for programming generators.
The following program does not work.
\begin{verbatim}
(proclaim '(special gen))
(let ((index 0))
(setq gen #'(lambda () (setq index (1+ index)))))
(funcall gen)
\end{verbatim}
However, the same purpose is accomplished by object oriented programming,
because an object can hold its own static variables:
\begin{verbatim}
(defclass generator object (index))
(defmethod generator
(:next () (setq index (1+ index)))
(:init (&optional (start 0)) (setq index start) self))
(defvar gen (instance generator :init 0))
(send gen :next)
\end{verbatim}
\newpage
|