oldgltest.sl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. % GLTEST.SL.8 17 January 1983
  2. % GLISP TEST FUNCTIONS, PSL VERSION.
  3. % Object descriptions for a Company database.
  4. (GLISPOBJECTS
  5. (EMPLOYEE % Name of the object type
  6. (LIST (NAME STRING) % Actual storage structure
  7. (DATE-HIRED (A DATE))
  8. (SALARY REAL)
  9. (JOBTITLE ATOM)
  10. (TRAINEE BOOLEAN))
  11. PROP ((SENIORITY ((THE YEAR OF (CURRENTDATE)) % Computed properties
  12. -
  13. (THE YEAR OF DATE-HIRED)))
  14. (MONTHLY-SALARY (SALARY * 174)))
  15. ADJ ((HIGH-PAID (MONTHLY-SALARY > 2000))) % Computed adjectives
  16. ISA ((TRAINEE (TRAINEE))
  17. (GREENHORN (TRAINEE AND SENIORITY < 2)))
  18. MSG ((YOURE-FIRED (SALARY _ 0))) ) % Message definitions
  19. (Date
  20. (List (MONTH INTEGER)
  21. (DAY INTEGER)
  22. (YEAR INTEGER))
  23. PROP ((MONTHNAME ((NTH '(JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY
  24. AUGUST SEPTEMBER OCTOBER NOVEMBER DECEMBER)
  25. MONTH)))
  26. (PRETTYFORM ((LIST DAY MONTHNAME YEAR)))
  27. (SHORTYEAR (YEAR - 1900))) )
  28. (COMPANY
  29. (ATOM (PROPLIST (PRESIDENT (AN EMPLOYEE))
  30. (EMPLOYEES (LISTOF EMPLOYEE) )))
  31. PROP ((ELECTRICIANS ((THOSE EMPLOYEES WITH JOBTITLE='ELECTRICIAN)))) )
  32. )
  33. % Some test data for the above functions.
  34. (setq company1 (a company with
  35. President = (An Employee with Name = "Oscar the Grouch"
  36. Salary = 88.0
  37. Jobtitle = 'President
  38. Date-Hired = (A Date with Month = 3
  39. Day = 15 Year = 1907))
  40. Employees = (list
  41. (An Employee with Name = "Cookie Monster"
  42. Salary = 12.50
  43. Jobtitle = 'Electrician
  44. Date-Hired = (A Date with Month = 7
  45. Day = 21 Year = 1947))
  46. (An Employee with Name = "Betty Lou"
  47. Salary = 9.00
  48. Jobtitle = 'Electrician
  49. Date-Hired = (A Date with Month = 5
  50. Day = 15 Year = 1980))
  51. (An Employee with Name = "Grover"
  52. Salary = 3.00
  53. Jobtitle = 'Electrician
  54. Trainee = T
  55. Date-Hired = (A Date with Month = 6
  56. Day = 13 Year = 1978))
  57. )))
  58. % Program to give raises to the electricians.
  59. (DG GIVE-RAISE
  60. (:COMPANY)
  61. (FOR EACH ELECTRICIAN WHO IS NOT A TRAINEE
  62. DO (SALARY _+(IF SENIORITY > 1
  63. THEN 2.5
  64. ELSE 1.5))
  65. (PRINT (THE NAME OF THE ELECTRICIAN))
  66. (PRINT (THE PRETTYFORM OF DATE-HIRED))
  67. (PRINT MONTHLY-SALARY) ))
  68. (DG CURRENTDATE () (Result DATE)
  69. (A DATE WITH YEAR = 1981 MONTH = 11 DAY = 30))
  70. % The following object descriptions are used in a graphics object test
  71. % program (derived from one written by D.G. Bobrow as a LOOPS example).
  72. % The test program MGO-TEST runs on a Xerox D-machine, but won't run on
  73. % other machines.
  74. (GLISPOBJECTS
  75. % The actual stored structure for a Vector is simple, but it is overloaded
  76. % with many properties.
  77. (VECTOR
  78. (LIST (X INTEGER)
  79. (Y INTEGER))
  80. PROP ((MAGNITUDE ((SQRT X^2 + Y^2)))
  81. (DIRECTION ((IF X IS ZERO THEN (IF Y IS NEGATIVE THEN -90.0
  82. ELSE 90.0)
  83. ELSE (ATAN2D Y X))) RESULT DEGREES)
  84. )
  85. ADJ ((ZERO (X IS ZERO AND Y IS ZERO))
  86. (NORMALIZED (MAGNITUDE = 1.0)))
  87. MSG ((+ VECTORPLUS OPEN T) % Defining operators as messages
  88. % causes the compiler to automatically
  89. % overload the operators.
  90. (- VECTORDIFF OPEN T)
  91. (* VECTORTIMES OPEN T ARGTYPES (NUMBER))
  92. (* vectordotproduct open t argtypes (vector))
  93. (/ VECTORQUOTIENT OPEN T)
  94. (_+ VECTORMOVE OPEN T)
  95. (PRIN1 ((PRIN1 "(")
  96. (PRIN1 X)
  97. (PRIN1 ",")
  98. (PRIN1 Y)
  99. (PRIN1 ")")))
  100. (PRINT ((SEND SELF PRIN1) % PRINT is defined in terms of the
  101. (TERPRI))) ) ) % PRIN1 message of this object.
  102. (DEGREES REAL % Stored value is just a real number.
  103. PROP ((RADIANS (self*(3.1415926 / 180.0)) RESULT RADIANS)))
  104. (RADIANS REAL
  105. PROP ((DEGREES (self*(180.0 / 3.1415926)) RESULT DEGREES)))
  106. % The definition of GraphicsObject builds on that of Vector.
  107. (GRAPHICSOBJECT
  108. (LIST (SHAPE ATOM)
  109. (START VECTOR)
  110. (SIZE VECTOR))
  111. PROP ((LEFT (START:X)) % A property defined in terms of a
  112. % property of a substructure
  113. (BOTTOM (START:Y))
  114. (RIGHT (LEFT+WIDTH)) % Vector addition.
  115. (TOP (BOTTOM+HEIGHT))
  116. (WIDTH (SIZE:X))
  117. (HEIGHT (SIZE:Y))
  118. (CENTER (START+SIZE/2)) % Vector arithmetic
  119. (AREA (WIDTH*HEIGHT)))
  120. MSG ((DRAW ((APPLY (GET SHAPE 'DRAWFN) % A way to get runtime message
  121. (List SELF % behavior without using the
  122. (QUOTE PAINT))))) % message mechanism.
  123. (ERASE ((APPLY (GET SHAPE 'DRAWFN)
  124. (LIST SELF
  125. (QUOTE ERASE)))))
  126. (MOVE GRAPHICSOBJECTMOVE OPEN T)) )
  127. (MOVINGGRAPHICSOBJECT
  128. (LIST (TRANSPARENT GRAPHICSOBJECT) % Includes properties of a
  129. (VELOCITY VECTOR)) % GraphicsObject due to the
  130. % TRANSPARENT declaration.
  131. Msg ((ACCELERATE MGO-ACCELERATE OPEN T)
  132. (STEP ((SEND SELF MOVE VELOCITY)))) )
  133. )
  134. % The following functions define arithmetic operations on Vectors.
  135. % These functions are generally called OPEN (macro-expanded) rather
  136. % than being called directly.
  137. (DG VECTORPLUS
  138. (V1:vector V2:VECTOR)
  139. (A (typeof v1) WITH X = V1:X + V2:X Y = V1:Y + V2:Y))
  140. (DG VECTORDIFF
  141. (V1:vector V2:VECTOR)
  142. (A (typeof v1) WITH X = V1:X - V2:X Y = V1:Y - V2:Y))
  143. (DG VECTORTIMES
  144. (V:VECTOR N:NUMBER)
  145. (A (typeof v) WITH X = X*N Y = Y*N))
  146. (DG VECTORDOTPRODUCT
  147. (V1:vector V2:VECTOR)
  148. (A (typeof v1) WITH X = V1:X * V2:X Y = V1:Y * V2:Y))
  149. (DG VECTORQUOTIENT
  150. (V:VECTOR N:NUMBER)
  151. (A (typeof v) WITH X = X/N Y = Y/N))
  152. % VectorMove, which defines the _+ operator for vectors, does a destructive
  153. % addition to the vector which is its first argument. Thus, the expression
  154. % U_+V will destructively change U, while U_U+V will make a new vector with
  155. % the value U+V and assign its value to U.
  156. (DG VECTORMOVE
  157. (V:vector DELTA:VECTOR)
  158. (V:X _+ DELTA:X)
  159. (V:Y _+ DELTA:Y)
  160. V)
  161. % An object is moved by erasing it, changing its starting point, and
  162. % then redrawing it.
  163. (DG GRAPHICSOBJECTMOVE
  164. (SELF:GRAPHICSOBJECT DELTA:VECTOR)
  165. (SEND SELF ERASE) % Erase the object
  166. (START _+ DELTA) % Destructively move start point by delta
  167. (SEND SELF DRAW)) % Redraw the object in new location
  168. (DG MGO-ACCELERATE
  169. (SELF: MOVINGGRAPHICSOBJECT ACCELERATION: VECTOR)
  170. VELOCITY _+ ACCELERATION)
  171. % Now we define some test functions which use the above definitions.
  172. % First there are some simple functions which test vector operations.
  173. (DG TVPLUS (U:VECTOR V:VECTOR) U+V)
  174. (DG TVMOVE (U:VECTOR V:VECTOR) U_+V)
  175. (DG TVTIMESN (U:VECTOR N:NUMBER) U*N)
  176. (DG TVTIMESV (U:VECTOR V:VECTOR) U*V)
  177. % This test function creates a MovingGraphicsObject and then moves it
  178. % across the screen by sending it MOVE messages. Everything in this
  179. % example is compiled open; the STEP message involves a great deal of
  180. % message inheritance.
  181. (DG MGO-TEST ()
  182. (PROG (MGO N)
  183. (MGO _(A MOVINGGRAPHICSOBJECT WITH
  184. SHAPE = (QUOTE RECTANGLE)
  185. SIZE = (A VECTOR WITH X = 4 Y = 3)
  186. VELOCITY = (A VECTOR WITH X = 3 Y = 4)))
  187. (N _ 0)
  188. (WHILE (N_+1)<100 (SEND MGO STEP))
  189. (SEND (THE START OF MGO) PRINT)))
  190. % This function tests the properties of a GraphicsObject.
  191. (DG TESTFN2 (:GRAPHICSOBJECT)
  192. (LIST SHAPE START SIZE LEFT BOTTOM RIGHT TOP
  193. WIDTH HEIGHT CENTER AREA))
  194. % Function to draw a rectangle. Computed properties of the rectangle are
  195. % used within calls to the graphics functions, making the code easy to
  196. % write and understand.
  197. (DG DRAWRECT (SELF:GRAPHICSOBJECT DSPOP:ATOM)
  198. (PROG (OLDDS)
  199. (OLDDS _(CURRENTDISPLAYSTREAM DSPS))
  200. (DSPOPERATION DSPOP)
  201. (MOVETO LEFT BOTTOM)
  202. (DRAWTO LEFT TOP)
  203. (DRAWTO RIGHT TOP)
  204. (DRAWTO RIGHT BOTTOM)
  205. (DRAWTO LEFT BOTTOM)
  206. (CURRENTDISPLAYSTREAM OLDDS) ))
  207. % The LispTree and PreorderSearchRecord objects illustrate how generators
  208. % can be written.
  209. (GLISPOBJECTS
  210. % In defining a LispTree, which can actually be of multiple types (atom or
  211. % dotted pair), we define it as the more complex dotted-pair type and take
  212. % care of the simpler case in the PROPerty definitions.
  213. (LISPTREE
  214. (CONS (CAR LISPTREE) % Defines a LispTree structure as the CONS
  215. (CDR LISPTREE)) % of two fields named CAR and CDR.
  216. PROP ((LEFTSON ((IF SELF IS ATOMIC THEN NIL ELSE CAR)))
  217. (RIGHTSON ((IF SELF IS ATOMIC THEN NIL ELSE CDR))))
  218. ADJ ((EMPTY (~SELF))) )
  219. % PreorderSearchRecord is defined to be a generator. Its data structure holds
  220. % the current node and a stack of previous nodes, and its NEXT message is
  221. % defined as code to step through the preorder search.
  222. (PREORDERSEARCHRECORD
  223. (CONS (NODE LISPTREE)
  224. (PREVIOUSNODES (LISTOF LISPTREE)))
  225. MSG ((NEXT ((PROG (TMP)
  226. (IF TMP_NODE:LEFTSON
  227. THEN (IF NODE:RIGHTSON THEN PREVIOUSNODES+_NODE)
  228. NODE_TMP
  229. ELSE TMP-_PREVIOUSNODES
  230. NODE_TMP:RIGHTSON))))) )
  231. )
  232. % PRINTLEAVES prints the leaves of the tree, using a PreorderSearchRecord
  233. % as the generator for searching the tree.
  234. (DG PRINTLEAVES (:LISPTREE)
  235. (PROG (PSR)
  236. (PSR _(A PREORDERSEARCHRECORD WITH NODE =(THE LISPTREE)))
  237. (WHILE NODE (IF NODE IS ATOMIC (PRINT NODE))
  238. (SEND PSR NEXT))))
  239. % The Circle objects illustrate the definition of a number of mathematical
  240. % properties of an object in terms of stored data and other properties.
  241. (Glispobjects
  242. (CIRCLE (LIST (START VECTOR) (RADIUS REAL))
  243. PROP ((PI (3.1415926)) % A PROPerty can be a constant.
  244. (DIAMETER (RADIUS*2))
  245. (CIRCUMFERENCE (PI*DIAMETER)) % Defined in terms of other prop.
  246. (AREA (PI*RADIUS^2)) )
  247. ADJ ((BIG (AREA>120)) % BIG defined in terms of AREA
  248. (MEDIUM (AREA >= 60 AND AREA <= 120))
  249. (SMALL (AREA<60)))
  250. MSG ((STANDARD (AREA_100)) % "Storing into" computed property
  251. (GROW (AREA_+100))
  252. (SHRINK (AREA_AREA/2)) )
  253. )
  254. % A DCIRCLE is implemented differently from a circle.
  255. % The data structure is different, and DIAMETER is stored instead of RADIUS.
  256. % By defining RADIUS as a PROPerty, all of the CIRCLE properties defined
  257. % in terms of radius can be inherited.
  258. (DCIRCLE (LISTOBJECT (START VECTOR) (DIAMETER REAL))
  259. PROP ((RADIUS (DIAMETER/2)))
  260. SUPERS (CIRCLE) )
  261. )
  262. % Make a DCIRCLE for testing
  263. (setq dc (a dcircle with diameter = 10.0))
  264. % Since DCIRCLE is an Object type, it can be used with interpreted messages,
  265. % e.g., (send dc area) to get the area property,
  266. % (send dc standard) to set the area to the standard value,
  267. % (send dc diameter) to get the stored diameter value.
  268. % EXAMPLE OF ASSIGNMENT TO COMPUTED PROPERTY
  269. (DG GROWCIRCLE (C:CIRCLE)
  270. (C:AREA_+100)
  271. (PRINT RADIUS) )
  272. (SETQ MYCIRCLE (A CIRCLE))
  273. % Since SQRT is not defined in the bare-PSL system, we redefine it here.
  274. (DG SQRT (X)
  275. (PROG (S)
  276. (S_X)
  277. (IF X < 0 THEN (ERROR)
  278. ELSE (WHILE (ABS S*S - X) > 0.000001 DO (S _ (S+X/S) * 0.5)))
  279. (RETURN S)))
  280. % Function SQUASH illustrates elimination of compile-time constants.
  281. % Of course, nobody would write such a function directly. However, such forms
  282. % can arise when inherited properties are compiled. Conditional compilation
  283. % occurs automatically when appropriate variables are defined to the GLISP
  284. % compiler as compile-time constants because the post-optimization phase of
  285. % the compiler makes the unwanted code disappear.
  286. (DG SQUASH ()
  287. (IF 1>3 THEN 'AMAZING
  288. ELSEIF 6<2 THEN 'INCREDIBLE
  289. ELSEIF 2 + 2 = 4 THEN 'OKAY
  290. ELSE 'JEEZ))
  291. % The following object definitions describe a student records database.
  292. (glispobjects
  293. (student (atom (proplist (name string)
  294. (sex atom)
  295. (major atom)
  296. (grades (listof integer))))
  297. prop ((average student-average)
  298. (grade-average student-grade-average))
  299. adj ((male (sex='male))
  300. (female (sex='female))
  301. (winning (average>=95))
  302. (losing (average<60)))
  303. isa ((winner (self is winning))))
  304. (student-group (listof student)
  305. prop ((n-students length) % This property is implemented by
  306. % the Lisp function LENGTH.
  307. (Average Student-group-average)))
  308. (class (atom (proplist (department atom)
  309. (number integer)
  310. (instructor string)
  311. (students student-group)))
  312. prop ((n-students (students:n-students))
  313. (men ((those students who are male)))
  314. (women ((those students who are female)))
  315. (winners ((those students who are winning)))
  316. (losers ((those students who are losing)))
  317. (class-average (students:average))))
  318. )
  319. (dg student-average (s:student)
  320. (prog ((sum 0.0)(n 0.0))
  321. (for g in grades do n _+ 1.0 sum_+g)
  322. (return sum/n) ))
  323. (dg student-grade-average (s:student)
  324. (prog ((av s:average))
  325. (return (if av >= 90.0 then 'a
  326. elseif av >= 80.0 then 'b
  327. elseif av >= 70.0 then 'c
  328. elseif av >= 60.0 then 'd
  329. else 'f))))
  330. (dg student-group-average (sg:student-group)
  331. (prog ((sum 0.0))
  332. (for s in sg do sum_+s:average)
  333. (return sum/sg:n-students) ))
  334. % Print name and grade average for each student
  335. (dg test1 (c:class)
  336. (for s in c:students (prin1 s:name)
  337. (prin2 '! )
  338. (print s:grade-average)))
  339. % Another version of the above function
  340. (dg test1b (:class)
  341. (for each student (prin1 name)
  342. (prin2 '! )
  343. (print grade-average)))
  344. % Print name and average of the winners in the class
  345. (dg test2 (c:class)
  346. (for s in c:winners (prin1 s:name)
  347. (prin2 '! )
  348. (print s:average)))
  349. % The average of all the male students' grades
  350. (dg test3 (c:class)
  351. c:men:average)
  352. % The name and average of the winning women
  353. (dg test4 (c:class)
  354. (for s in c:women when s is winning
  355. (prin1 s:name)
  356. (prin2 '! )
  357. (print s:average)))
  358. % Another version of the above function. The * operator in this case
  359. % denotes the intersection of the sets of women and winners. The
  360. % GLISP compiler optimizes the code so that these intermediate sets are
  361. % not actually constructed.
  362. (dg test5 (c:class)
  363. (for s in c:women*c:winners
  364. (prin1 s:name)
  365. (prin2 '! )
  366. (print s:average)))
  367. % Make a list of the easy professors.
  368. (dg easy-profs (classes:(listof class))
  369. (for each class with class-average > 90.0 collect (the instructor)))
  370. % A more Pascal-like version of easy-profs:
  371. (dg easy-profs-b (classes:(listof class))
  372. (for c in classes when c:class-average > 90.0 collect c:instructor))
  373. % Some test data for testing the above functions.
  374. (setq class1 (a class with instructor = "G. Novak" department = 'cs
  375. number = 102 students =
  376. (list
  377. (a student with name = "John Doe" sex = 'male major = 'cs
  378. grades = '(99 98 97 93))
  379. (a student with name = "Fred Failure" sex = 'male major = 'cs
  380. grades = '(52 54 43 27))
  381. (a student with name = "Mary Star" sex = 'female major = 'cs
  382. grades = '(100 100 99 98))
  383. (a student with name = "Doris Dummy" sex = 'female major = 'cs
  384. grades = '(73 52 46 28))
  385. (a student with name = "Jane Average" sex = 'female major = 'cs
  386. grades = '(75 82 87 78))
  387. (a student with name = "Lois Lane" sex = 'female major = 'cs
  388. grades = '(98 95 97 96)) )))
  389. % The following object definitions illustrate inheritance of properties
  390. % from multiple parent classes. The three "bottom" classes Planet, Brick,
  391. % and Bowling-Ball all inherit the same definition of the property Density,
  392. % although they are represented in very different ways.
  393. (glispobjects
  394. (physical-object anything
  395. prop ((density (mass/volume))))
  396. (ordinary-object anything
  397. prop ((mass (weight / 9.88))) % Compute mass as weight/gravity
  398. supers (physical-object))
  399. (sphere anything
  400. prop ((volume ((4.0 / 3.0) * 3.1415926 * radius ^ 3))))
  401. (parallelepiped anything
  402. prop ((volume (length*width*height))))
  403. (planet (listobject (mass real)(radius real))
  404. supers (physical-object sphere)) % A planet is a physical-object
  405. % and a sphere.
  406. (brick (object (length real)(width real)(height real)(weight real))
  407. supers (ordinary-object parallelepiped))
  408. (bowling-ball (atomobject (type atom)(weight real))
  409. prop ((radius ((if type='adult then 0.1 else 0.07))))
  410. supers (ordinary-object sphere))
  411. )
  412. % Three test functions to demonstrate inheritance of the Density property.
  413. (dg dplanet (p:planet) density)
  414. (dg dbrick (b:brick) density)
  415. (dg dbb (b:bowling-ball) density)
  416. % Some objects to test the functions on.
  417. (setq earth (a planet with mass = 5.98e24 radius = 6.37e6))
  418. (setq brick1 (a brick with weight = 20.0 width = 0.10 height = 0.05
  419. length = 0.20))
  420. (setq bb1 (a bowling-ball with type = 'adult weight = 60.0))
  421. % Since the object types Planet, Brick, and Bowling-Ball are defined as
  422. % Object types (i.e., they contain the Class name as part of their stored
  423. % data), messages can be sent to them directly from the keyboard for
  424. % interactive examination of the objects. For example, the following
  425. % messages could be used:
  426. % (send earth density)
  427. % (send brick1 weight: 25.0)
  428. % (send brick1 mass: 2.0)
  429. % (send bb1 radius)
  430. % (send bb1 type: 'child)