Parallel , APL Lecture 1 Parallel Functional Programming with APL

Martin Elsman

Department of University of Copenhagen DIKU

December 19, 2016

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 1 / 18 Outline

1 Outline Course Outline

2 Introduction to APL What is APL APL Implementations and Material APL Scalar Operations APL (1-Dimensional) Vector Computations Declaring APL Functions (dfns) APL Multi-Dimensional Arrays Iterations Declaring APL Operators Function Trains Examples Reading

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 2 / 18 Outline Course Outline

Teachers Martin Elsman (ME), Ken Friis Larsen (KFL), Andrzej Filinski (AF), and Troels Henriksen (TH)

Location Lectures in Small Aud, Universitetsparken 1 (UP1); Labs in Old Library, UP1

Course Description See http://kurser.ku.dk/course/ndak14009u/2016-2017

Course Outline

Week 47 48 49 50 51 1–3 Mon 13–15 Intro, Futhark Parallel SNESL APL (ME) Project Futhark (ME) Haskell (AF) (ME) (KFL) Mon 15–17 Lab Lab Lab Lab Project Wed 13–15 Futhark Parallel SNESL Invited APL Project (ME) Haskell (AF) Lecture (ME) / (KFL) (John Projects Reppy)

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 3 / 18 Introduction to APL What is APL

APL—An Ancient Array —But Still Used!

Pioneered by Ken E. Iverson in the 1960’s. E. Dijkstra: “APL is a mistake, carried through to perfection.” There are quite a few APL programmers around (e.g., HIPERFIT partners). Very concise notation for expressing array operations. Has a large set of functional, essentially parallel, multi- dimensional, second-order array combinators. E.g., reduce, scan, reduce, map, zipWith (implicit vectorisation)

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 4 / 18 Introduction to APL APL Implementations and Material

Implementations of APL

Dyalog APL: http://dyalog.com/ TryAPL: http://www.tryapl.org/ Gnu-APL: https://www.gnu.org/software/apl/ NGN APL: https://ngn.github.io/apl/web/ (zero-indexed) TAIL-APL: https://github.com/melsman/apltail

Material

Bernard Legrand. Mastering Dyalog APL. Online. http://dyalog.com/uploads/ documents/MasteringDyalogAPL.pdf.

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 5 / 18 Introduction to APL APL Scalar Operations

APL Scalar Operations

3+4 " Scalar op 7 2#3+4 " Right-parsed 14 %2#3+4 " Reciprocal Essentially only one 0.07142857143 numeric type (B → Z → a _%2#3+4 " Assignment → ). a + 1 " Var use 1.07142857143 Many built-in functions are 1.05a " Max (dyadic) overloaded (e.g., %, ). 1.07142857143 The character type is a " Floor (monadic) 1 another basic type. 0J1*2 " Complex squared APL is in principle a 1 dynamically typed language. 2 a " Comparison 0 ’a’ + 1 " Character DOMAIN ERROR

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 6 / 18 Introduction to APL APL (1-Dimensional) Vector Computations

APL (1-Dimensional) Vector Computations

v _ 5 4 2 1 " Vector declaration v#2 " Scalar expansion 10 8 4 2 +/v#2 " Reduction (/ is an ‘‘operator’’) 24 %v " Vectorised monadic op 0.2 0.25 0.5 1 4 " Index (one-indexed) 1 2 3 4 v+| 4 " Point-wise + of reversed iota 9 7 4 2 1|v+| 4 " Rotate 1 7 4 2 9 (1|v+| 4)[2 1] " Index 4 7 £|1|v+| 4 " Grade-up (sort indexes) 3 2 1 4 u[£|(u_1|v+| 4)] " Sort 2 4 7 9

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 7 / 18 Introduction to APL APL (1-Dimensional) Vector Computations

More APL (1-Dimensional) Vector Computations

v _ 3 " Vector declaration 5^v " Take 5 elements 1 2 3 0 0 5^v " Undertake (from the right) 0 0 1 2 3 5 8 " Drop 6 7 8 y_(5# 2),5 8 " Concatenation 5 10 6 7 8 /y " Find maximum (/ is an ‘‘operator’’) 10 (y>6)/y " Compress 10 7 8 +\y " Inclusive prefix scan (\ operator) 5 15 21 28 36

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 8 / 18 Introduction to APL Declaring APL Functions (dfns)

Declaring APL Functions f _ {¥+2} 8 3 4 " Monadic function 10 5 6 g _ {((¤*2)+¥*2)*0.5} " Dyadic function 3 g 4 5 Example—Signal Processing diff _ {1 ¥-1|¥} signal _ {505050#(diff 0,¥)%0.01+¥} +/ signal 9 8 6 8 7 4 4 3 2 2 1 2 4 5 6 27.870466

Example—Integration

f _ { 2%¥+2 } " Function λx.2/(x+2) X _ 1000000 " Points dom _ 10#( X)%X " Integrate in [0;10] (10%X)#+/f dom " Compute integral 3.583514772 ≈ 2(ln 12 − ln 2) = 3.5835189

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 9 / 18 Introduction to APL APL Multi-Dimensional Arrays

APL Multi-Dimensional Arrays a _ 2 3 5 " Reshape a Most vector operations are 1 2 3 4 5 1 generalised straightforwardly b _ \a © b " to work on multi-dimensional 1 4 arrays (e.g., |, ^, , +/) 2 5 3 1 All values are arrays and each array has a shape, a vector b " Shape of containing the index range of 3 2 each dimension. The shape of a " 1 b Drop scalar is the empty vector. 2 5 3 1 The of an array is the shape of the shape of the array.  # " ( 3) . 4 Outer prod Scalars have rank 0 and vectors 1 2 3 4 2 4 6 8 have rank 1. 3 6 9 12

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 10 / 18 Introduction to APL APL Multi-Dimensional Arrays

Inner Products a _ 3 2 5 1 3 5 b _ \ a 2 4 1 a +.# b 5 11 7 1 2 5 11 7 11 25 19 3 4 11 25 19 7 19 26 5 1 7 19 26

Inner Product is Generic The inner product operator is f _ { ¥ &. 100 } f 1 2 3 100 232 22 3 generic in the applied dyadic 0 functions used. f 1 2 3 12 232 22 3 Inner product works on 1 compatible arrays of any rank!

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 11 / 18 Introduction to APL APL Multi-Dimensional Arrays

Example—Calculating Primes A_1 10 primes _ (1=+/-0=A.|A)/A primes 2 3 5 7 +/ primes 17

0=A.|A Dyadic | is the residual 1 0 1 0 1 0 1 0 1 function. 0 1 0 0 1 0 0 1 0 +/- reduces along the last axis. 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 12 / 18 Introduction to APL Iterations

The APL Power Operator The power operator is a dyadic operator that, when applied, ({ ¥ + 3 } §*4) 100 results in a monadic function. 112 ({ ¥ ,’-’, ¥ } §*3) 'ab' The operator takes a function ab-ab-ab-ab-ab-ab-ab-ab as its left argument and an “iteration count” as its right argument.

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 13 / 18 Introduction to APL Iterations

Example—Sierpinski f _ {(¥,( ¥) ’ ’),-¥,¥} S _ {(f§*¥)1 1 '+'} S 1 + ++ S 4 + ++ If S is the triangle at level n, + + then the triangle at level n + 1 ++++ S0 is . + + SS ++ ++ + + + + ++++++++ + + ++ ++ + + + + ++++ ++++

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 14 / 18 Introduction to APL Declaring APL Operators

Declaring APL Operators APL operators are second-order functions, which may take functions (monadic or dyadic) as arguments and results in a function (monadic or dyadic). Operators in APL are declared similarly to how functions are declared. In the body of the operator, the function parameters are referred to by ¤¤ and ¥¥ and the residual function’s parameters are referred to by ¤ and ¥.

Example

op _ {¤¤/2+¥} # op 5 2520

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 15 / 18 Introduction to APL Function Trains

Function Trains Function trains are used for writing APL code in “points-free notation”. Function trains can make code very concise. A “train” fgh is identical (in λ-calculus) to λx.g(f x, h x).

Example

mean _ +/%6≡ " f is +/ ; g is dyadic % mean 1 3 7 3 8 5 " h is 6≡ (tally) 4.5

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 16 / 18 Introduction to APL Examples

Game of Life life _ { rowsum _ {(1|¥) + ¥ + 1|¥ } ...... neighbor _ { ...... (rowsum 1¥- ) + (rowsum ¥) + rowsum 1¥- ..###..... } ..#...... n _ neighbor ¥ ...#...... (n=3) ¨ (n=4) & ¥ ...... } ...... nlife _ { (life §*¤) ¥ } ...... glider _ 3 3 1 1 1 1 0 0 0 1 0 ...... board _ \10 ^ \10 ^ glider ...... ’.#’[1+20 nlife board]

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 17 / 18 Introduction to APL Reading

Possible Reading Ken E. Iverson. 1979 Turing Award Lecture: Notation as a Tool of Thought. http://www.eecg.toronto.edu/˜jzhu/csc326/readings/ iverson.pdf. In addition to the executability and universality emphasized in the introduction, a good notation should embody characteristics familiar to any user of mathematical notation:” Ease of expressing constructs arising in problems. Suggestivity. Ability to subordinate detail. Economy. Amenability to formal proofs.

Martin Elsman (DIKU) Parallel Functional Programming, APL Lecture 1 December 19, 2016 18 / 18