CS 342 Lecture -*- Outline -*- * APL ad: ultimate calculator power of larger values world of expressions (important for functional programming) few statements little recursion implicit consideration of parallelism power of data as opposed to control brings up questions of readability effect: understand how to reason about expressions equationally understand how APL style differs from LISP style understand concepts of regularity, simplicity, orthoganality importance of "idomatic expressions" (paradigms) ** History and Goals 1956-60 Ken Iverson begins design of notation goal: tool for describing and analyzing data processing 1961-63 use for describing computer systems (hardware) goal: precise description of machine instruction sets IBM 360 (allows designers freedom to implement in different ways) representation of parallel processes 1964-68 implementation goal: simplicity -uniform rules, generality, familiarity, brevity practicality -easy to use the language, -easy to implement ** data in our version of APL *** scalars (integers) example shape input notation 3 scalar 3 *** vectors (1-dimensional arrays) ------------------- example shape input notation 3 4 5 3-vector '(3 4 5) 3 1-vector '(3) 0-vector '() ------------------- 1-vectors are often treated as scalars *** matricies (2-dimensional arrays) ------------------- example shape input notation 11 12 3,2-matrix (restruct '(3 2) '(11 12 21 22 31 32)) 21 22 31 32 3 1,1-matrix (restruct '(1 1) '(3)) 0,0-matrix (restruct '(0 0) '(0)) ------------------- *** rank = # of dimensions: scalar, vector, matrix *** shape = size of each dimension + rank e.g., #rows,#cols-matrix have to memorize what shape and rank mean. ** operators the power of APL lots of them: have to understand why they are designed as they are, don't just memorize all the defs. do type (shape) analysis for each class *** standard operators: +, -, *, /, max, or, and, =, <, > **** for scalars, as in chapter 1 0 represents false, 1 represents true **** for scalar and vector or matrix what should (+ 3 '(1 2 3)) be? does that make sense for (max 3 '(1 2 3 4))? what about (* '(1 2 3) 4)? what about (- (restruct '(3 3) '(1 0 0 0)) 2)? **** for vector and vector, matrix and matrix: what should (+ '(1 2 3) '(4 5 6)) be? does that make sense for max? "pointwise" application of these operators what should (+ '(1 2) '(1 2 3)) be? similarly for matricies thus (* m1 m2) does not do "matrix multiplication" as in linear algebra (same rule must work for "max"...) **** implicit loops or parallelism in all of them e.g., neg on p. 70 mod on p. 70 exercise: find this in the interpreters *** reduction over standard operators like Sigma (summation), Pi (product) in math e.g., fac, min/ on p. 70 think of / as making a new operator (+/, */, etc.) primary action is on vectors (or rows of matricies) (+/ '(1 2 3 4)) = 10 (max/ '(1 2 3 4)) = 4 (-/ '(1 2 3 4)) = -2 = (- 1 (- 2 (- 3 4))) note that rank is reduced by 1 so maps n-vectors -> scalars m,n-matrix -> m-vector *** data observers (an artificial distinction with constructors) **** shape size of a vector vector of sizes for a matrix what should this return for a scalar? (define rank (x) (shape (shape x))) **** ravel values -> vector with same elements **** [] (subscripting) see page 67 Aij is found by ([] ([] A i) j) *** data constructors derive the following equations ask how one would program these operations in LISP **** indx makes a vector (1 2 3 4...) of specified length how to express lhs in terms of rhs parts? ------------- (shape (indx n)) = n, for n >= 0 ([] (indx n) i) = i, for 1 <= i <= n ------------- **** cat value, value -> vector ------------- (cat m1 m2) = (cat (ravel m1) (ravel m2)) (shape (cat m1 m2)) = (+ (shape (ravel m1)) (shape (ravel m2))) if 1 <= i <= (shape (ravel m1)), then ([] (cat m1 m2) i) = ([] (ravel m1) i) if (shape (ravel m2))+1 <= i <= (shape (cat m1 m2)), then ([] (cat m1 m2) i) = ([] (ravel m2) (- i (shape (ravel m1))))) ------------- **** compress like project in RDB logical vector arg (1st arg) picks elements (rows) see page 67 ----------- (shape (compress lv value)) = (+/ lv), for logical vectors lv (compress (restruct (size value) '(1)) value) = value where (size A) = ([] (shape A) 1) ([] (compress lv value) i) = ?? (in terms of [], lv, value, i) ----------- **** restruct ------------ (restruct (shape m) (ravel m)) = m (shape (restruct v e)) = v (ravel (restruct v e)) = e, if (shape e) >= (*/ v) ([] (ravel (restruct v e)) i) = ([] (cat ([] e (shape e)) e) (+ 1 (mod i (shape e)))) for all 1 <= i <= (*/ v) ------------ **** trans like reflecting through "identity line" m,n-matrix -> n,m-matrix -------------- (shape (trans A)) = (reverse (shape A)) where (reverse A) =([] A (+ 1 (- (size A) (indx (size A)))))) and (size A) = ([] (shape A) 1) ([] ([] (trans A) j) i) = ([] ([] A i) j) -------------- ** work examples from page 70 ff