finding functional pearls - lambda days€¦ · finding functional pearls detecting recursion...
TRANSCRIPT
![Page 1: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/1.jpg)
Finding Functional PearlsDetecting Recursion Schemes in Haskell Functions via Anti-Uni�cation
Adam D. Barwell, Christopher Brown, and Kevin HammondUniversity of St Andrews
Email: [email protected]
Lambda Days9 February 2017
![Page 2: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/2.jpg)
Parallelism
I Parallel devices are ubiquitousI Phones, tablets, laptops, &c. are all multicoreI Heterogeneity
By Béria L. Rodríguez, CC BY-SA 3.0, Wikipedia By Béria L. Rodríguez, CC BY-SA 3.0, Wikipedia
2
![Page 3: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/3.jpg)
Matrix Multiplication
3
![Page 4: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/4.jpg)
Matrix Multiplication
type Matrix = [[a]]
data Action = DHL | DVR | DB
data Tree = Leaf Matrix Matrix
| Node Action Tree Tree
matmult :: Matrix -> Matrix -> Matrix
matmult a b = (join . split) a b
3
![Page 5: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/5.jpg)
Matrix Multiplication
join :: Tree -> Matrix
join t = foldTree multiply h t
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
3
![Page 6: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/6.jpg)
Matrix Multiplication
parChunkTree :: Int
-> (Matrix -> Matrix -> Matrix)
-> (Action -> Matrix -> Matrix -> Matrix)
-> Strategy (Either Tree Matrix)
parChunkTree d f g (Leaf a b) = do
m’ <- rpar (h a b)
return (Right m’)
parChunkTree 0 f g (Node c l r) = do
(Right a) <- evalFoldTree f g l
(Right b) <- evalFoldTree f g r
m <- rdeepseq (g c a b)
return (Right m)
parChunkTree d f g (Node c l r) = do
(Right a) <- parChunkTree (d-1) f g l
(Right b) <- parChunkTree (d-1) f g r
m <- rpar (g c a b)
return (Right m)
3
![Page 7: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/7.jpg)
Matrix Multiplication
12 4 8 12 16 18 24 28
11.52
2.53
3.54
4.55
5.56
6.57
Cores
Speedup
1552x1552 matrices, average of 10 runs.
3
![Page 8: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/8.jpg)
Alternative Parallelisations
I Adjust depth, size of matrices at leaves, functions par’d
I Split the fold into a map & a fold
I Use the Par monad, Eden, &c.
I Call to a GPU (Accelerate)
I Call to a distributed system
4
![Page 9: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/9.jpg)
The Good
join :: Tree -> Matrix
join t = foldTree multiply h t
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
I Only need to swap the fold for a parallel versionI Applicable to other recursion schemes
I map, unfold, &c.
5
![Page 10: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/10.jpg)
The Inconvenient
I There may not be a fold to begin with…
I The spectral set of Haskell programs in NoFib suiteI 48 programs of varying design and functionalityI At least 19 have at least one function that can be rewritten as a map or fold
I Why?I (Le� over from) an initial implementationI ‘No need to de�ne it, I’m only going to use it here.’I Don’t know of their existence; e.g. unfoldI Near patterns
I Not every recursion scheme is worth parallelising, but if they’re there,we can pick the relevant ones
6
![Page 11: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/11.jpg)
Anti-Uni�cation
I First described by Plotkin and Reynolds in 1970
I Primarily used in clone detection & elimination
I Finds the least general generalisation of two terms
t1 = a+ (b− c)
t2 = 5 ∗ (b+ c)t = α β (b γ c)
7
![Page 12: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/12.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
join :: Tree -> Matrix
join t = foldTree multiply h t
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
8
![Page 13: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/13.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
join :: Tree -> Matrix
join (Leaf a b) = multiply a b
join (Node x a b) = h x (join a) (join b)
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
8
![Page 14: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/14.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
foldTree :: (Matrix -> Matrix -> Matrix)
-> (Action -> Matrix -> Matrix -> Matrix)
-> Tree
-> Matrix
foldTree f g (Leaf a b) = f a b
foldTree f g (Node a l r) =
g a (foldTree f g l) (foldTree f g r)
8
![Page 15: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/15.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
foldTree :: (Matrix -> Matrix -> Matrix)
-> (Action -> Matrix -> Matrix -> Matrix)
-> Tree
-> Matrix
foldTree f g (Leaf a b) = f a b
foldTree f g (Node a l r) =
g a (foldTree f g l) (foldTree f g r)
join :: Tree -> Matrix
join (Leaf a b) = multiply a b
join (Node x a b) = h x (join a) (join b)
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
8
![Page 16: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/16.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
au f g (Leaf a b) = f a b
au f g (Node a l r) = g a (x l) (y r)
8
![Page 17: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/17.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
join t = au multiply h t
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
treeFold f g t = au f g t
8
![Page 18: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/18.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
foldTree :: (Matrix -> Matrix -> Matrix)
-> (Action -> Matrix -> Matrix -> Matrix)
-> Tree
-> Matrix
foldTree f g (Leaf a b) = f a b
foldTree f g (Node a l r) =
g a (foldTree f g l) (foldTree f g r)
au f g (Leaf a b) = f a b
au f g (Node a l r) = g a (x l) (y r)
8
![Page 19: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/19.jpg)
Applying Anti-Uni�cation to Matrix Multiplication
join :: Tree -> Matrix
join t = foldTree multiply h t
where
h :: Action -> Matrix -> Matrix -> Matrix
h DHL a b = a ++ b
h DVR a b = zipWith (++) a b
h DB a b = sum’ a b
8
![Page 20: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/20.jpg)
Not Just Matrix Multiplication
I Implemented a prototype of our approach in HaRe
I Applied our prototype to a range of functions inspired by the Haskellprelude
I Also to functions in Matrix Multiplication, N-Body, and Quicksort
9
![Page 21: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/21.jpg)
Future Work
I More examplesI NoFibI Real Haskell programs
I More patternsI Currently working on unfold
I Use equational reasoning, reduction, rewriting, &c. to make patterndiscovery and argument derivation more �exible
10
![Page 22: Finding Functional Pearls - Lambda Days€¦ · Finding Functional Pearls Detecting Recursion Schemes in Haskell Functions via Anti-Unication Adam D. Barwell, Christopher Brown, and](https://reader033.vdocuments.us/reader033/viewer/2022060602/6056f1df72040525021ee88f/html5/thumbnails/22.jpg)
Summary
I Use anti-uni�cation to automatically discover recursion schemes inHaskell code
I Prototype of our approach implemented in HaRe
I Recursion schemes can be used as a ‘stepping stone’ for parallelisation
I Parallelisation becomes as simple as swapping sequential patterns forparallel ones.
[email protected] @rephrase_eu
11