foreign inline code in haskell
play

Foreign Inline Code in Haskell Manuel M T Chakravarty University of - PowerPoint PPT Presentation

Foreign Inline Code in Haskell Manuel M T Chakravarty University of New South Wales mchakravarty TacticalGrace TacticalGrace justtesting.org 1 30 minute time slot: 25min talking + 5min [15min The Problem; 5min TH+Quasiquoting; 5min


  1. Foreign Inline Code in Haskell Manuel M T Chakravarty University of New South Wales mchakravarty α TacticalGrace TacticalGrace justtesting.org 1 30 minute time slot: 25min talking + 5min [15min The Problem; 5min TH+Quasiquoting; 5min Inline Objective-C]

  2. Shiny new functional language λ 2 » Imagine, you have got a shiny, new functional language...

  3. 3 » ...and you want to use it to write a great new app...

  4. λ 4 » ...then you will need to use many existing frameworks and libraries. » Luckily, any serious language will have a foreign function interface! » Haskell standard includes a simple, but versatile FFI

  5. “Problem solved?” 5 » Does that solve the problem of language interoperability? » Let me explain that at an example...

  6. ! o N “Problem solved?” 5 » Does that solve the problem of language interoperability? » Let me explain that at an example...

  7. What we want to write dumpURL :: String -> IO () dumpURL urlString = do urlData <- stringWithContentsOfUrl urlString putStr urlData 6

  8. What we want to write dumpURL :: String -> IO () dumpURL urlString = do urlData <- stringWithContentsOfUrl urlString putStr urlData What we want to call (& need to put into an extra file) char *stringWithContentsOfUrlCstub(char *urlString) { NSURL *url = [NSURL URLWithString:urlString]; [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL]; } 6

  9. What we have to write as well… foreign import stringWithContentsOfURLCstub :: CString -> IO CString stringWithContentsOfURL :: String -> IO String stringWithContentsOfURL url = withCString url $ \urlC -> do resultC <- stringWithContentsOfURLCstub urlC result <- peek resultC free resultC return result 7 » FFI code: If you find it confusing, that’s fine, as I want to argue that you shouldn’t write it in the first place. * Anything extensively relying on foreign frameworks will be a pain [Hybrid languages (eg, F# & Scala) have a di fg erent set of trade o fg s, but don’t solve it either.]

  10. Bridging Libraries 1-to-1 transliteration of types & functions 8 * Bridging or binding libraries transliterate types & functions * Need to be maintained and documented; track multiple versions of base library * Lack of type safety * Tools and dynamic transliteration (by reflection) help

  11. A case study C ➙ Haskell c2hs GTK+ 9 * c2hs: automation for bridging C libraries to Haskell <https://hackage.haskell.org/package/ c2hs> * Implements large parts of a C compiler front-end * Bridge for the cross-platform Gnome GUI library GTK+ <http://projects.haskell.org/ gtk2hs/>

  12. 10 * AFAIK, currently the only fully featured and properly maintained Haskell GUI library * Haskell GTK+ library is used for realistic applications

  13. “Does this approach scale?” 11 » Read question * It requires significant resources & constant e fg ort to track the original library * Application frameworks: enormous and growing footprint

  14. February 2011 Introduction of GTK+ 3 12 * Base GTK+ development isn’t even particularly fast * Bridging libraries lag behind their base libraries * Another example: Haskell OpenGL library

  15. February 2011 Introduction of GTK+ 3 December 2013 GTK+ 3 in Haskell Bridge 12 * Base GTK+ development isn’t even particularly fast * Bridging libraries lag behind their base libraries * Another example: Haskell OpenGL library

  16. 13 * Compared to modern application frameworks, GTK+ is small!

  17. Bridging libraries Too much weight! don’t scale 14

  18. “Interoperability is an old problem — maybe an old solution can help?” 15

  19. 16 * Modula-2 with inline 68000 assembly * Assembly code can symbolically refer to Modula-2 entities (e.g., variables)

  20. 16 * Modula-2 with inline 68000 assembly * Assembly code can symbolically refer to Modula-2 entities (e.g., variables)

  21. 16 * Modula-2 with inline 68000 assembly * Assembly code can symbolically refer to Modula-2 entities (e.g., variables)

  22. “Inline C, C++, Objective-C, … in Haskell?” 17 * We don’t want to build front ends for a few more languages into GHC * How do we share entities between the languages?

  23. Meta-programming Template Haskell 18 » Generic infrastructure for program manipulation NOTE: I'll run through this quickly; I'll explain the details at the workshop.

  24. HASKELL WORKSHOP 2002 19 * Template Haskell: Haskell extension implemented by GHC * Useful for: defining macros, code generators, code transformations… * Other languages have their own variants; eg., MetaOCaml » Let’s look at an example…

  25. HASKELL WORKSHOP 2002 #define macros 19 * Template Haskell: Haskell extension implemented by GHC * Useful for: defining macros, code generators, code transformations… * Other languages have their own variants; eg., MetaOCaml » Let’s look at an example…

  26. HASKELL WORKSHOP 2002 #define [| … |] code macros generators 19 * Template Haskell: Haskell extension implemented by GHC * Useful for: defining macros, code generators, code transformations… * Other languages have their own variants; eg., MetaOCaml » Let’s look at an example…

  27. HASKELL WORKSHOP 2002 #define [| … |] trafo (ConE name) = … code code macros generators transformations 19 * Template Haskell: Haskell extension implemented by GHC * Useful for: defining macros, code generators, code transformations… * Other languages have their own variants; eg., MetaOCaml » Let’s look at an example…

  28. $(sel 1 3) (a, b, c) = a 20 * Meta function executed at splice point, generating spliced code

  29. meta-programming function $(sel 1 3) (a, b, c) = a 20 * Meta function executed at splice point, generating spliced code

  30. meta-programming function $(sel 1 3) (a, b, c) = a splice 20 * Meta function executed at splice point, generating spliced code

  31. meta-programming function $(sel 1 3) (a, b, c) = a splice \tup -> case tup of {(x, y, z) -> x} :: (a, b, c) -> a 20 * Meta function executed at splice point, generating spliced code

  32. meta-programming function $(sel 1 3) (a, b, c) = a splice \tup -> case tup of {(x, y, z) -> x} :: (a, b, c) -> a $(sel 5 5) (a, b, c, d, e) = e 20 * Meta function executed at splice point, generating spliced code

  33. meta-programming function $(sel 1 3) (a, b, c) = a splice \tup -> case tup of {(x, y, z) -> x} :: (a, b, c) -> a $(sel 5 5) (a, b, c, d, e) = e \tup -> case tup of {(x, y, z, v, w) -> w} :: (a, b, c, d, e) -> e 20 * Meta function executed at splice point, generating spliced code

  34. sel :: Int -> Int -> ExpQ sel i n = [| \tup -> case tup of {$pat -> $res} |] where pat = tupP (map varP names) res = varE (names !! (i - 1)) names = [mkName $ "v" ++ show i | i <- [1..n]] 21 * Quasiquotations in [|..|] brackets * Explain TH in more detail in the workshop

  35. type of Haskell expressions sel :: Int -> Int -> ExpQ sel i n = [| \tup -> case tup of {$pat -> $res} |] where pat = tupP (map varP names) res = varE (names !! (i - 1)) names = [mkName $ "v" ++ show i | i <- [1..n]] 21 * Quasiquotations in [|..|] brackets * Explain TH in more detail in the workshop

  36. type of Haskell expressions quasi-quotation sel :: Int -> Int -> ExpQ sel i n = [| \tup -> case tup of {$pat -> $res} |] where pat = tupP (map varP names) res = varE (names !! (i - 1)) names = [mkName $ "v" ++ show i | i <- [1..n]] 21 * Quasiquotations in [|..|] brackets * Explain TH in more detail in the workshop

  37. type of Haskell expressions quasi-quotation sel :: Int -> Int -> ExpQ sel i n = [| \tup -> case tup of {$pat -> $res} |] where pat = tupP (map varP names) res = varE (names !! (i - 1)) names = [mkName $ "v" ++ show i | i <- [1..n]] spliced expression (anti quote) 21 * Quasiquotations in [|..|] brackets * Explain TH in more detail in the workshop

  38. Processing other languages Generic quasi-quotation 22 * Quoting arbitrary languages

  39. HASKELL WORKSHOP 2007 Quasiquoting for any language you can provide a parser for 23

  40. Language.C.Quote add n = [cfun| int addConstant(int x) { return x + $int:n; } |] 24 * QQ for C including some GNU extensions, parts of CUDA & OpenCL, and all of Objective-C

  41. Language.C.Quote quasi-quotation identifier add n = [cfun| int addConstant(int x) { return x + $int:n; } |] 24 * QQ for C including some GNU extensions, parts of CUDA & OpenCL, and all of Objective-C

  42. Language.C.Quote quasi-quotation identifier add n = [cfun| int addConstant(int x) { return x + $int:n; } |] splice identifier 24 * QQ for C including some GNU extensions, parts of CUDA & OpenCL, and all of Objective-C

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend