Template Haskell とか

2,447 views
2,315 views

Published on

Template Haskell について即席でまとめた資料

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,447
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide






























  • Template Haskell とか

    1. 1. Template Haskell konn
    2. 2. Template Haskell
    3. 3. Template Haskell Haskell
    4. 4. Template Haskell Haskell
    5. 5. Template Haskell Haskell Q
    6. 6. Template Haskell Haskell Q C preprocessor
    7. 7. TH
    8. 8. TH Haskell
    9. 9. TH Haskell IO compile-time wxWidgets Network...
    10. 10. RandomDef.hs {-# LANGUAGE TemplateHaskell #-} module Main where import Language.Haskell.TH import System.Random $( do rnd <- runIO $ randomRIO (0,1) let nm = mkName (["a", "b"] !! rnd) m <- [d| main = $(varE $ mkName "a") |] t <- valD (varP nm) (normalB [| putStrLn " ( ´ `) " |]) [] return (t:m) )
    11. 11. RandomDef.hs {-# LANGUAGE TemplateHaskell #-} module Main where import Language.Haskell.TH import System.Random IO $( do rnd <- runIO $ randomRIO (0,1) let nm = mkName (["a", "b"] !! rnd) m <- [d| main = $(varE $ mkName "a") |] t <- valD (varP nm) (normalB [| putStrLn " ( ´ `) " |]) [] return (t:m) )
    12. 12. Splice( ) {-# LANGUAGE TemplateHaskell #-} module Main where import Language.Haskell.TH import System.Random $( do rnd <- runIO $ randomRIO (0,1) let nm = mkName (["a", "b"] !! rnd) m <- [d| main = $(varE $ mkName "a") |] t <- valD (varP nm) (normalB [| putStrLn " ( ´ `) " |]) [] return (t:m) )
    13. 13. Splice
    14. 14. Splice (Q )
    15. 15. Splice (Q ) splice splice
    16. 16. Splice (Q ) splice splice “ ”
    17. 17. {-# LANGUAGE TemplateHaskell #-} module Main where import Language.Haskell.TH import System.Random $( do rnd <- runIO $ randomRIO (0,1) let nm = mkName (["a", "b"] !! rnd) m <- [d| main = $(varE $ mkName "a") |] t <- valD (varP nm) (normalB [| putStrLn " ( ´ `) " |]) [] return (t:m) )
    18. 18. [ |
    19. 19. [ | [| putStrLn “hogehoge” |]
    20. 20. [ | [| putStrLn “hogehoge” |] [t| String |]
    21. 21. [ | [| putStrLn “hogehoge” |] [t| String |] [d| main = putStrLn |]
    22. 22. [ | [| putStrLn “hogehoge” |] [t| String |] [d| main = putStrLn |] [$ident| function f() {..} |]
    23. 23. [ | [| putStrLn “hogehoge” |] [t| String |] [d| main = putStrLn |] [$ident| function f() {..} |] DSL
    24. 24. [ | [| putStrLn “hogehoge” |] [t| String |] [d| main = putStrLn |] [$ident| function f() {..} |] DSL
    25. 25. [ | [| putStrLn “hogehoge” |] [t| String |] [d| main = putStrLn |] [$ident| function f() {..} |] DSL )JavaScript (
    26. 26. splice [d| main = $(varE $ mkName "a") |] splice [d| main = ‘a |] TH a `a ``String
    27. 27. {-# LANGUAGE TemplateHaskell #-} module Main where import Language.Haskell.TH import System.Random $( do rnd <- runIO $ randomRIO (0,1) let nm = mkName (["a", "b"] !! rnd) m <- [d| main = $(varE $ mkName "a") |] t <- valD (varP nm) (normalB [| putStrLn " ( ´ `) " |]) [] return (t:m) )
    28. 28. valD (varP nm) (normalB [| putStrLn " ( ´ `) " |]) [] [d| $(varP nm) = putStrLn “ ( ´ `) ” |] …… …… ghci > runQ [d| main = putStrLn “hoge” |]
    29. 29. Sucks GHC TH Ver.UP (ex. HOC )
    30. 30. lib2 = let l=LetS[ValD(VarP $ mkName"it")(NormalB(f"be"))[]] f=VarE . mkName in DoE [ l, l, l, NoBindS $ f"oh",l, NoBindS $ InfixE(Just$ f"speaking")(f"words")(Just $ f "wisdom"),l]
    31. 31. lib2 = let l=LetS[ValD(VarP $ mkName"it")(NormalB(f"be"))[]] f=VarE . mkName in DoE [ l, l, l, NoBindS $ f"oh",l, NoBindS $ InfixE(Just$ f"speaking")(f"words")(Just $ f "wisdom"),l]
    32. 32. lib2 = let l=LetS[ValD(VarP $ mkName"it")(NormalB(f"be"))[]] f=VarE . mkName in DoE [ l, l, l, NoBindS $ f"oh",l, NoBindS $ InfixE(Just$ f"speaking")(f"words")(Just $ f "wisdom"),l] do let it = be let it = be let it = be oh let it = be speaking `words` wisdom let it = be
    33. 33. reify IO reify Q

    ×