Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Short, Painful Introduction to Function-Level Programming with J

A Short, Painful Introduction to Function-Level Programming with J

Joe R. Smith

January 27, 2016
Tweet

More Decks by Joe R. Smith

Other Decks in Programming

Transcript

  1. A Short, Painful*, Introduction to Function-level Programming with J *that's

    your mind expanding! Joe R Smith January 26 2016 Lincoln Lambda Lounge
  2. –Ken Iverson “Overemphasis of efficiency leads to an unfortunate circularity

    in design: for reasons of efficiency early programming languages reflected the characteristics of the early computers, and each generation of computers reflects the needs of the programming languages of the preceding generation.”
  3. Function-Level Programming • Concise mathematical foundations • Useful for proving

    facts concerning a program's behavior • Reduction semantics • Programs composed of expressions
  4. • No named arguments (tacit style) • No side-effects, order

    of operations is implicit • Operates on arrays of data (whole conceptual units), not pointers/ references to single words • Implicitly parallelizable • Equivalence and correctness can be demonstrated algebraically
  5. What is J? • Dialect of APL • An array

    programming language • Using only ASCII characters • Function-Level programming using a tacit, point-free, style • inspired by FP and FFP • High performance general purpose programming language
  6. Goals of this Presentation • Give you a high-level understanding

    of function-level programming • Teach you enough about J to understand the semantics, if not the language (who learns an entire language in 40 minutes anyway?) • Set the tone for Lambda Lounge. ;)
  7. Alphabet a – z A – Z 0 1 2

    3 4 5 6 7 8 9 = < > _ + * - % ^ $ ~ | . : , ; # ! / \ [ ] { } " ` @ & ? ( ) '
  8. How to read my slides NB. This is a code

    block 10*7+0.2 NB. code (note the indentation) 72 NB. result #'I will use purple to call things out' 36 + y is the conjugate of y . For example, +3j4 is 3j_4 . This is code documentation:
  9. Primitives A word defined by the system Some examples #

    Tally / Copy {. Head / Take {: Tail : and . are used as inflections to build digraphs to expand the vocabulary
  10. Names Words defined by you firstname =: 'Joseph' #firstname 6

    roll_dice =: ?&12 roll_dice 1 2 roll_dice 10 10 8 0 11 4 2 5 7 6 3
  11. Precedence and Order of Evaluation J code is evaluated right

    to left 2 + 5 * 8 - 9 ^ 2 + 1 _3603 (2 + (5 * (8 - (9 ^ (2 + 1))))) _3603
  12. Dyads, Monads, and Ambivalence Verbs have dyadic and monadic, taking

    2 or 1 arguments, respectively. Ambivalence refers to verbs' ability to be used dyadically or monadically. 12-5 7 -7 _7 4#'a' aaaa 1 2 3 4 * 5 6 7 8 5 12 21 32
  13. Atom 72 'a' x =. 36 A noun that is

    a single entity is an atom An atom has 0 axes
  14. List 2 4 8 16 'Welcome to the lounge' p

    = . 1 2 3 A noun that is a list of atoms is a list A list has 1 axis
  15. Table A noun that is a 2 axis array of

    atoms is a table. The dyad $ known as "shape" can create tables. the monad i. is "integers" and takes n arguments, where n the number of axes i.3 4 0 1 2 3 4 5 6 7 8 9 10 11 2 3 $ 7 7 7 7 7 7 7 2 2 $ 7 8 6 5 7 8 6 5
  16. Array Atoms, lists, and tables are all arrays. All nouns

    in J are arrays. Arrays can be of any dimension (any number of axes) 2 3 5 $ i. 30 0 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 i. 2 3 5 0 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
  17. Array Arguments 1 2 3 4 * 5 6 7

    8 5 12 21 32 'Lambda','Lounge' Lambda Lounge
  18. Array Arguments (i.3 4) + i.3 4 0 2 4

    6 8 10 12 14 16 18 20 22
  19. Array Arguments (i.3 4) + 0 50 10 0 1

    2 3 54 55 56 57 18 19 20 21 Does this do what you'd expect?
  20. Shape and Rank The monadic $ gives the "shape" of

    a noun, where shape is a list of the counts of atoms in each of its axes $ i.3 4 3 4 NB. rank 2 $ 0 50 10 3 NB. rank 1 $ 5 NB. rank 0 The number of axes is the noun's rank
  21. Frame and Cell An array can be thought of as

    consisting of cells, each part of the array, e.g., sub-arrays, that, when placed in a frame make up the entire array 2 3 $ i. 6 0 1 2 3 4 5 This array can be thought of as having 6 cells, where each is an atom. The frame therefore has the shape 2 3
  22. Frame and Cell 2 3 $ i. 6 0 1

    2 3 4 5 It can also be thought of as having 2 cells, where each is a list, i.e., 0 1 2 and 3 4 5 The frame would then have a shape of 2, and each cell a shape of 3. It could also be thought of as an array of 1 cell, where the cell is a table. The frame shape would be empty
  23. Frame and Cell • To review, a table with shape

    2 3 can be thought of as: • a 2 3 frame of cells that are atoms • a 2 frame of cells that are lists of shape 3 • an empty frame of a cell that is a table of shape 2 3 • The frame is a prefix of the shape of the array • The cell shape is the array shape with the frame prefix removed. • The length of the cell shape is the cell rank • The cells of an array are the subarrays that, when assembled into the corresponding frame, create the entire array
  24. Verb Rank A verb has a rank that that determines

    how it is applied to its monads + 0 0 0 Monadic Rank Dyadic Left and Right Rank + applies to cells of rank 0
  25. Verb Rank (i.3 4) + 1 1 2 3 4

    5 6 7 8 9 10 11 12 + will treat its left argument as a collection of cells of rank 0 in a 3 4 frame and its right argument as a cell of rank 0 (and an empty frame)
  26. Agreement • For a dyad, the left rank of the

    verb and rank of the left argument determine the frame of the left argument. Same goes for the right. • If the frames of the left and right arguments match or if one is a prefix of the other then they are compatible.
  27. Agreement (i.3 4) + 1 1 2 3 4 5

    6 7 8 9 10 11 12 Left argument treated as having a frame of 2 3 and right argument as having a frame of empty. Empty is a prefix of 2 3
  28. Agreement (i.3 4) + 0 50 10 0 1 2

    3 54 55 56 57 18 19 20 21 Left argument treated as having a frame of 3 4 and right argument as having a frame of 3. 3 is a prefix of 3 4 0 1 2 3 + 0 4 5 6 7 + 50 8 9 10 11 + 10
  29. Adverbs • Only have a left argument (monadic) • Can

    be applied to nouns or verbs (whereas verbs only take nouns) • Typically has a verb as a result (a derived verb) • Derived at a higher precedence than regular verb application
  30. Adverbs +/ 1 2 5 8 the / adverb is

    "insert". It take a verb as its left argument and returns a monad that folds its argument together using the original verb */ 1 2 5 10
  31. Definition sum_list =: +/ sort_ascending =. /:~ sum_list 9 1j4

    12 22j4 sort_ascending 7 3 9 2 30 50 4 _2 6 _2 2 3 4 6 7 9 30 50 =: defines a global definition (in a locale) =. defines a local definition
  32. Conjunctions • Take two arguments (dyadic) • Take nouns and

    verbs • Results in a verb (derived verb) • Derived at a higher precedence than regular verb application
  33. Hook (f g) y evaluates as y f g y

    x (f g) y evaluates as x f g y (%>./)10 20 35 67 100 0.1 0.2 0.35 0.67 1
  34. Hook (f g) y evaluates as y f g y

    x (f g) y evaluates as x f g y (%>./)10 20 35 67 100 0.1 0.2 0.35 0.67 1
  35. Fork (f g h) y evaluates as (f y) g

    (h y) x (f g h) y evaluates as (x f y) g (x h y) (+/%#)100?100 49.5
  36. Fork (f g h) y evaluates as (f y) g

    (h y) x (f g h) y evaluates as (x f y) g (x h y) (+/%#)100?100 49.5