Slide 1

Slide 1 text

Connecting the JVM to Haskell Facundo Dom´ ınguez May 3, 2018 Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 1 / 20

Slide 2

Slide 2 text

En esta presentaci´ on 1 Una soluci´ on para combinar los runtimes de Haskell y la JVM 2 Caso de estudio: Spark Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 2 / 20

Slide 3

Slide 3 text

Por qu´ e integrar Haskell con la JVM Haskell Application Java libraries Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 3 / 20

Slide 4

Slide 4 text

inline-java main = withJVM [] $ do correct <- challengeDialog 2 3 print correct Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 4 / 20

Slide 5

Slide 5 text

inline-java main = withJVM [] $ do correct <- challengeDialog 2 3 print correct Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 4 / 20

Slide 6

Slide 6 text

inline-java main = withJVM [] $ do correct <- challengeDialog 2 3 print correct challengeDialog :: Int32 -> Int32 -> IO Bool challengeDialog x y = [java| { String question = "¿Cu´ anto es " + $x + " + " + $y + "?"; String response = javax.swing.JOptionPane.showInputDialog(question); try { return Integer.parseInt(response) == $x + $y; } catch (NumberFormatException e) { return false; } } |] Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 4 / 20

Slide 7

Slide 7 text

Por qu´ e no usar bindings? String showInputDialog(Object message) Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 5 / 20

Slide 8

Slide 8 text

Por qu´ e no usar bindings? String showInputDialog(Object message) showInputDialog :: JObject -> IO String Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 5 / 20

Slide 9

Slide 9 text

Por qu´ e no usar bindings? String showInputDialog(Object message) showInputDialog :: JObject -> IO String Bindings Application Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 5 / 20

Slide 10

Slide 10 text

Por qu´ e no usar bindings? module A where challengeDialog :: ... Application class Challenge { boolean challengeDialog(...) { ... showInputDialog ... ... parseInt ... } } Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 6 / 20

Slide 11

Slide 11 text

Java Native Interface (JNI) defineClass :: ClassName -> ByteString -> IO JClass findClass :: ClassName -> IO JClass getStaticMethodID :: JClass -> MethodName -> MethodTypeSignature -> IO JMethodID callStaticMethod :: JClass -> JMethodID -> [...] -> IO ... Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 7 / 20

Slide 12

Slide 12 text

inline-java challengeDialog :: Int32 -> Int32 -> IO Bool challengeDialog x y = [java| { String question = "¿Cu´ anto es " + $x + " + " + $y + "?"; String response = javax.swing.JOptionPane.showInputDialog(question); try { return Integer.parseInt(response) == $x + $y; } catch (NumberFormatException e) { return false; } } |] Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 8 / 20

Slide 13

Slide 13 text

inline-java module A where ... [java| { foreign code } |] ... class ClassForModuleA { static boolean m0(...) { foreign code } } Bytecode de ClassForModuleA module A where ... JNI para darle el bytecode a la JVM e invocar m0 ... javac inline-java A.o Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 9 / 20

Slide 14

Slide 14 text

En resumen inline-java encuentra errores de tipo al compilar, reduce la necesidad de grandes librer´ ıas de bindings, y automatiza la compilaci´ on y enlazado de Java. Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 10 / 20

Slide 15

Slide 15 text

Spark https://spark.apache.org/ Spark la interfase de programaci´ on Spark el entorno para ejecutar computaciones distribuidas Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 11 / 20

Slide 16

Slide 16 text

Spark https://spark.apache.org/ Spark la interfase de programaci´ on Spark el entorno para ejecutar computaciones distribuidas Worker 1 Worker 2 Worker N Dataset Driver Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 11 / 20

Slide 17

Slide 17 text

sparkle https://github.com/tweag/sparkle Bindings para Spark Soluci´ on para ejecutar Haskell en una aplicaci´ on de Spark Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 12 / 20

Slide 18

Slide 18 text

sparkle https://github.com/tweag/sparkle Bindings para Spark Soluci´ on para ejecutar Haskell en una aplicaci´ on de Spark spark application.jar Main.class ... Other.class Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 12 / 20

Slide 19

Slide 19 text

sparkle https://github.com/tweag/sparkle Bindings para Spark Soluci´ on para ejecutar Haskell en una aplicaci´ on de Spark spark application.jar Main.class ... Other.class app.zip shared haskell library.so dependency1.so ... dependencyN.so Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 12 / 20

Slide 20

Slide 20 text

Bindings de Spark newtype Dataset a = Dataset (J ('Class "org.apache.spark.sql.Dataset")) count :: Dataset a -> IO Int64 map :: (a -> b) -> Dataset a -> IO (Dataset b) reduce :: (a -> a -> a) -> Dataset a -> IO a filter ::(a -> Bool) -> Dataset a -> IO (Dataset a) Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 13 / 20

Slide 21

Slide 21 text

Conversi´ on en bloque Convertir [Int] es menos costoso que convertir los enteros de a uno. Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 14 / 20

Slide 22

Slide 22 text

Conversi´ on en bloque Convertir [Int] es menos costoso que convertir los enteros de a uno. Convertir ([a], [b]) es m´ as barato que convertir [(a,b)]. Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 14 / 20

Slide 23

Slide 23 text

Conversi´ on en bloque Convertir [Int] es menos costoso que convertir los enteros de a uno. Convertir ([a], [b]) es m´ as barato que convertir [(a,b)]. Convertir [[a]] es m´ as barato que convertir las listas de a una. Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 14 / 20

Slide 24

Slide 24 text

Conversi´ on en bloque Convertir [Int] es menos costoso que convertir los enteros de a uno. Convertir ([a], [b]) es m´ as barato que convertir [(a,b)]. Convertir [[a]] es m´ as barato que convertir las listas de a una. [Compuesto] -> ([A], [B], [C], ...) ----> ([ForeignA], [ForeignB], ...) -> [ForeignComposed] Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 14 / 20

Slide 25

Slide 25 text

Conversi´ on en bloque Convertir [Int] es menos costoso que convertir los enteros de a uno. Convertir ([a], [b]) es m´ as barato que convertir [(a,b)]. Convertir [[a]] es m´ as barato que convertir las listas de a una. [Compuesto] -> ([A], [B], [C], ...) ----> ([ForeignA], [ForeignB], ...) -> [ForeignComposed] https: //github.com/tweag/inline-java/tree/master/jvm-batching https: //github.com/tweag/inline-java/tree/master/jvm-streaming Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 14 / 20

Slide 26

Slide 26 text

Bindings de Spark newtype Row a = Row (J ('Class "org.apache.spark.sql.Row")) newtype Column = Column (J ('Class "org.apache.spark.sql.Column")) select :: Dataset Row -> [Column] -> IO (Dataset Row) whereDS :: Dataset Row -> Column -> IO (Dataset Row) Examples: embelished <- select employees [col "name", col "wage" * 10] expensive <- whereDS employees (col "wage" > 10000) Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 15 / 20

Slide 27

Slide 27 text

Bindinds de Spark map :: (a -> b) -> Dataset a -> IO (Dataset b) reduce :: (a -> a -> a) -> Dataset a -> IO a filter ::(a -> Bool) -> Dataset a -> IO (Dataset a) Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 16 / 20

Slide 28

Slide 28 text

Recolecci´ on de basura ... coordinada ... Haskell JVM Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 17 / 20

Slide 29

Slide 29 text

Recolecci´ on de basura ... coordinada ... Recuerde borrar cada referencia luego de usarla Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 18 / 20

Slide 30

Slide 30 text

Recolecci´ on de basura ... coordinada ... Recuerde borrar cada referencia luego de usarla bracket createRef deleteRef useRef Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 18 / 20

Slide 31

Slide 31 text

Recolecci´ on de basura ... coordinada ... Recuerde borrar cada referencia luego de usarla bracket createRef deleteRef useRef Linear types https://github.com/ghc-proposals/ghc-proposals/pull/111 Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 18 / 20

Slide 32

Slide 32 text

Recolecci´ on de basura ... coordinada ... Recuerde borrar cada referencia luego de usarla bracket createRef deleteRef useRef Linear types https://github.com/ghc-proposals/ghc-proposals/pull/111 Notificaciones del recolector en la JVM Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 18 / 20

Slide 33

Slide 33 text

Impresiones finales sparkle permite un uso eficiente de Spark desde Haskell con algunas restricciones. Queda por explorar una soluci´ on mejor para la coordinaci´ on de GCs. Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 19 / 20

Slide 34

Slide 34 text

Referencias inline-java: https://github.com/tweag/inline-java Tutorial de inline-java: https://www.tweag.io/posts/ 2017-09-15-inline-java-tutorial.html Sobre la implementaci´ on de inline-java: https://www.tweag. io/posts/2017-09-22-inline-java-ghc-plugin.html Spark: https://spark.apache.org/ sparkle: https://github.com/tweag/sparkle Propuesta de tipos lineales: https://github.com/ghc-proposals/ghc-proposals/pull/111 Sobre coordinaci´ on de recolectores de basura: https://www.tweag.io/posts/2017-11-29-linear-jvm.html Facundo Dom´ ınguez (tweag.io) Connecting the JVM to Haskell May 3, 2018 20 / 20