Slide 1

Slide 1 text

tacit programming Point-free, Concatenatives & J

Slide 2

Slide 2 text

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙ ☆)

Slide 3

Slide 3 text

I'll talking about tacit programming

Slide 4

Slide 4 text

tacit? tacit : silent, unspoken

Slide 5

Slide 5 text

tacit programming is a programming method to assembly functions with no free variables. a.k.a. point-free style

Slide 6

Slide 6 text

point-free style in Haskell

Slide 7

Slide 7 text

Let's do point-free style Let's define functions without arguments.

Slide 8

Slide 8 text

Example 1 : factorial Non-point-free factorial n = n * factorial (n - 1) factorial 0 = 1 factorial 9

Slide 9

Slide 9 text

Example 1 : factorial foldr (*) 1 [1..9] ↓ (foldr (*) 1) . (enumFromTo 1) $ 9

Slide 10

Slide 10 text

Example 1 : factorial point-free factorial = (foldr (*) 1) . (enumFromTo 1) factorial 9

Slide 11

Slide 11 text

Let's do point-free style Compose functions. Don't think about values.

Slide 12

Slide 12 text

Example 2 : average Non-point-free average xs = foldr (+) 0 xs / (fromIntegral . length $ xs) average [2, 3]

Slide 13

Slide 13 text

Example 2 : average point-free average = ((/) . foldr (+) 0) <*> (fromIntegral . length) average [2, 3]

Slide 14

Slide 14 text

https://github.com/ghc/ghc/blob/b4fb232892ec420059e767bbf464bd09361aaefa/lib raries/base/GHC/Base.hs#L973 instance Applicative ((->) a) where pure = const (<*>) f g x = f x (g x) liftA2 q f g x = q (f x) (g x) (<*>) f g x = f x (g x) is S.

Slide 15

Slide 15 text

(<*>) f g x = f x (g x) What is S?

Slide 16

Slide 16 text

point-free style is under combinatory logic

Slide 17

Slide 17 text

combinatory logic You know λ-calculus? λx.1+x This term has a variable x .

Slide 18

Slide 18 text

combinatory logic An raugh image of combinators is, that λx.1+x becomes + 1 x . + 1 the part of it makes sence. 1 x also makes sence.

Slide 19

Slide 19 text

One of a form of combinatory logic is SKI combinator calculus Sxyz=xz(yz) S copies z. Kxy=x K discards y. Ix=x I is identity. SKI is Turing complete.

Slide 20

Slide 20 text

SKI combinator calculus I=SKK because SKKx=Kx(Kx)=x . I=SKS because SKSx=Kx(Sx)=x .

Slide 21

Slide 21 text

SKI combinator calculus self-application SIIx=Ix(Ix)=xx

Slide 22

Slide 22 text

SKI combinator calculus recrusion Y=S(K(SII))(S(S(KS)K)(K(SII))) Yx=xYx

Slide 23

Slide 23 text

combinatory logic has no "function application". It has only function composition & quotation.

Slide 24

Slide 24 text

quotaion?

Slide 25

Slide 25 text

concatenative programming language is based on combinatory logic

Slide 26

Slide 26 text

concatenative programming language You know Forth? Forth is a stack-based programming language.

Slide 27

Slide 27 text

concatenative programming language Add in Forth. 2 3 + 1. Put 2 on the stack. 2. Put 3 on the stack. 3. Pop 3 & 2 from the stack, then plus them, put the result 5 on the stack.

Slide 28

Slide 28 text

concatenative programming language If 3 + evaluated lazily it makes sence. 1. Put 3 on the stack. 2. Pop 3 & another value from the stack, then plus them, put the result ont the stack. concatenative programming language like Factor & Popr do this.

Slide 29

Slide 29 text

Example 1 : average in Popr. average: dup sum swap length /f [2 3] averge concatenatives are always tacit.

Slide 30

Slide 30 text

Example 2 : factorial in Factor. : factorial_rec ( n n -- n ) dup 0 = [ drop ] [ [| n m | n m * m 1 - factorial_rec ] call ] if ; : factorial ( n -- n ) dup 0 < [ drop 0 ] [ 1 swap factorial_rec ] if ; 9 factorial [ ] is quotatin. It's evaluated in lazy.

Slide 31

Slide 31 text

Example 2 : factorial another one in Factor. MEMO: factorial ( n -- n! ) dup 1 > [ [1,b] product ] [ drop 1 ] if ; 9 factorial

Slide 32

Slide 32 text

Example 2 : factorial in Popr factorial: [1 == 1 swap !] [dup dup 1 - factorial * swap 1 > !] | pushl head 9 factorial 1 == 1 swap ! fails when the value is greater than 1. dup dup 1 - factorial * swap 1 > ! fails when the value is 1.

Slide 33

Slide 33 text

concatenative programming language Unix pipe & filter is tacit. Concatenatives are extends pipe & filter using stacks.

Slide 34

Slide 34 text

concatenative programming language is a only way to extends conbinatory logic? is a only way to extends pipe & filter?

Slide 35

Slide 35 text

J

Slide 36

Slide 36 text

J quicksort (the most famous example of J). quicksort=:(($:@(<#[),(=#[),$:@(>#[))({~?@#))^:(1<#) quicksort 1 _9 2 _8 3 _7 4 _6 5 _9 _8 _7 _6 1 2 3 4 5

Slide 37

Slide 37 text

Example 1 : add 2+3 5 dyadic + plus 2 numbers.

Slide 38

Slide 38 text

Example 1 : add +/2 3 5 2 3 is a vector. / is fold. So +/ sums the vector.

Slide 39

Slide 39 text

Example 2 : average average:=(+/%#) average 2 3 2.5 This is tacit.

Slide 40

Slide 40 text

Example 2 : average Evaluation flow. (+/ % #) 2 3 (+/ 2 3) % (# 2 3) NB. monadic fork monadic fork : (fgh)x=(fx)g(hx)

Slide 41

Slide 41 text

Example 3 : factorial !9 362880 :)

Slide 42

Slide 42 text

Example 3 : factorial */1+i.9 362880

Slide 43

Slide 43 text

Example 3 : factorial Evaluation flow. J evaluates a program from right. */ 1 + i. 9 */ 1 + 0 1 2 3 4 5 6 7 8 NB. `i.` creates a vector */ 1 2 3 4 5 6 7 8 9 NB. vector operation 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 NB. fold `*` 362880

Slide 44

Slide 44 text

Example 3 : factorial another tacit one. factorial =: 1: ` (* factorial@<:) @. * factorial 9 362880

Slide 45

Slide 45 text

Example 3 : factorial Evaluation flow. factorial 2 1: ` (* factorial@<:) @. * 2 (* factorial@<:) 2 NB. `@.` selects the second 2 * (factorial@<: 2) NB. monadic hook monadic hook : (fg)x=xf(gx)

Slide 46

Slide 46 text

Example 3 : factorial Evaluation flow. 2 * (factorial@<: 2) 2 * (factorial 1) 2 * (1 * (factorial 0)) NB. same as `factorial 2` 2 * (1 * (1: ` (* factorial@<:) @. * 0)) 2 * (1 * (1: 0)) NB. `@.` selects the first 2 * (1 * 1) 2 * 1 2

Slide 47

Slide 47 text

Example 4 : permitation in Ruby "mswd".chars.permutation.map { |cs| cs.join("") } - ["mswd"]

Slide 48

Slide 48 text

Example 4 : permitation in J str=:'mswd' ((i.!#str)A.str)-.str

Slide 49

Slide 49 text

Example 4 : permitation tacit J ((([:i.[:!#)A.])-.])'mswd'

Slide 50

Slide 50 text

J J is a vein of gold. https://scrapbox.io/ne-sachirou/J