Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programming Haskell Chapter8

6,715 views

Published on

第4回スタートHaskell (11/12)資料

Published in: Technology, Education
  • Be the first to comment

Programming Haskell Chapter8

  1. 1. 8 : ruicc (ruicc.rail@gmail.com)
  2. 2. • ( )•@ruicc•http://d.hatena.ne.jp/ruicc•LinkThink ! Altplus
  3. 3. Haskell 8• Google Suggest
  4. 4. •Haskell
  5. 5. P.90 †2[ ]• Web 10
  6. 6. P.90 †2[ ]• Web 10
  7. 7. P.90 †2[ ]
  8. 8. P.90 †2[ ]
  9. 9. P.90 †2[ ]• =>• import Prelude hiding(return,(>>=),(>>))• do (Mona* )•
  10. 10. •••Haskell
  11. 11. • • *••advanced :
  12. 12.
  13. 13. •Parser [pärsəәr]•
  14. 14. “-32.4” Parser -32.4
  15. 15. •type Parser = String -> Tree ※Tree
  16. 16. •type Parser = String -> (Tree, String)
  17. 17. •type Parser = String -> [(Tree, String)]
  18. 18. • atype Parser a = String -> [(a, String)] ※ advanced:
  19. 19. •“1.4aaa” Parser [(1.4,“aaa”)]
  20. 20. •“+.4aaa” Parser []
  21. 21. • return :: a -> Parser a •• failure :: Parser a •• item :: Parser Char •
  22. 22. parse• Parser String • Parser parse:: Parser a -> String -> [(a,String)]
  23. 23. parse •parse“1.4aaa” Parser [(1.4,“aaa”)] parse !
  24. 24. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  25. 25. •• • •
  26. 26. sign digits“-3.24” [(-3.24,“”)] pointparse 1 digits ※
  27. 27. • •
  28. 28. •p, then q double =digits >>= point >>= digits ※
  29. 29. • (>>=) p(>>=):: Parser a -> (a -> Parser b) -> Parser bp >>= f = inp -> case parse p inp of [] -> [] [(v,out)] -> parse (f v) out (f v)
  30. 30. •p, or q char ‘+’ sign = +++ char ‘-’
  31. 31. • p(+++):: Parser a -> Parser a -> Parser ap +++ q = inp -> case parse p inp of [] -> parse q inp [(v,out)] -> [(v,out)] q advanced: p
  32. 32. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  33. 33. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  34. 34. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  35. 35. • (>>=)double = sign >>= s -> digits >>= num -> point >>= p -> digits >>= num2 -> return (func s num p num2) ※sign,digits,point given
  36. 36. • returndouble = sign >>= s -> digits >>= num -> point >>= p -> digits >>= num2 -> return (func s num p num2)
  37. 37. do • Parserdouble :: Parser Doubledouble = do s <- sign num <- digits p <- point num2 <- digits return (func s num p num2)
  38. 38. • type Parser a = String -> [(a, String)] • Char • Parser a • Parser
  39. 39. do • double :: Parser Double double = do Char s <- sign :: Parser Char num <- digits :: Parser StringString p <- point :: Parser Char Char num2 <- digits :: Parser StringString return (func s num p num2) :: Parser Double
  40. 40. do••• String• (>>=)
  41. 41. sign digits“-3.24” [(-3.24,“”)] point digits
  42. 42. • return, failure, item• (>>=)• (+++)• (do )
  43. 43. •• (>>=)• (+++)• ( )
  44. 44. • item sat itemsat :: (Char -> Bool) -> Parser Charsat p = do x <- item if p x then return x else failure (p x) True False ※p predicate
  45. 45. • sat digit = sat isDigit lower = sat isLower upper = sat isUpper letter = sat isAlpha alphanum = sat isAlphanum char x = sat (==x)
  46. 46. • char string string :: String -> Parser String string [] = return [] string (x:xs) = do char x string xs return (x:xs)
  47. 47. • many, many1 many, many1 :: Parser a -> Parser [a] many p = many1 p +++ return [] many1 p = do v <- p vs <- many p return (v:vs)
  48. 48. • many ident = do x <- lower xs <- many alphanum return (x:xs) nat = do xs <- many1 digit return (read xs) space = do many (sat isSpace) return ()
  49. 49. • token :: Parser a -> Parser a token p = do space v <- p space return v
  50. 50. • token identifier :: Parser String identifier = token ident natural :: Parser Int natural = token nat symbol :: Parser String symbol xs = token (string xs)
  51. 51.
  52. 52. :•BNF(Backus-Naur Form) • • (|) ( )
  53. 53. • BNF..? expr ::= expr ‘+’ expr | expr ‘*’ expr | ‘(‘ expr ‘)’ | nat nat ::= ‘0’ | ‘1’ | ‘2’ | ...
  54. 54. •(*) (+)• • (expr), (term), (factor)
  55. 55. • BNF expr ::= expr ‘+’ expr | term term ::= term ‘*’ term | factor factor ::= ‘(‘ expr ‘)’ | nat nat ::= ‘0’ | ‘1’ | ‘2’ | ...
  56. 56. ••1 + 2 + 3 == 1 + (2 + 3)••1 - 2 - 3 == (1 - 2) - 3•
  57. 57. • BNF( ) expr ::= term ‘+’ expr | term term ::= factor ‘*’ term | factor factor ::= ‘(‘ expr ‘)’ | nat nat ::= ‘0’ | ‘1’ | ‘2’ | ...
  58. 58. • BNF( ) expr ::= term ‘+’ expr | term term ::= factor ‘*’ term | factor →
  59. 59. •BNF 1 -- expr ::= term ‘+’ expr | term expr :: Parser Int expr = do t <- (term :: Parser Int) symbol “+” e <- (expr :: Parser Int) return (t + e) +++ term
  60. 60. •BNF 2 -- term ::= factor ‘*’ term | factor term :: Parser Int term = do f <- (factor :: Parser Int) symbol “*” t <- (term :: Parser Int) return (f * t) +++ factor
  61. 61. •BNF 3 -- factor ::= ‘(‘ expr ‘)’ | nat factor :: Parser Int factor = do symbol “(“ e <- (expr :: Parser Int) symbol “)” return e +++ natural
  62. 62. • expr ghci> parse expr "5 * (2) + ( 4 +(2 * 2 + 4) * 2) * 5 " [(110,””)]
  63. 63. HaskellParser
  64. 64. Package•Parsec3 ••attoparsec •
  65. 65. • • letter, digit, char, string, many, ...• (+++) (<|>), try • • import Control.Applicative (<|>) • try
  66. 66. BNF BNF BNF (wikipedia )<syntax> ::= <rule> | <rule> <syntax><rule> ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end><opt-whitespace> ::= " " <opt-whitespace> | ""<expression> ::= <list> | <list> "|" <expression><line-end> ::= <opt-whitespace> <eol> | <line-end> <line-end><list> ::= <term> | <term> <opt-whitespace> <list><term> ::= <literal> | "<" <rule-name> ">"<literal> ::= " <text> " | "" <text> ""
  67. 67. cabal install attoparseccabal install attoparsec-text
  68. 68. {-# LANGUAGE OverloadedStrings #-}module BNFParser whereimport Data.Attoparsec.Textimport Data.Text.Internal (Text)import Data.Text as Timport Control.Applicative hiding(many)
  69. 69. BNFtype BNF = Syntaxtype Syntax = [Rule]data Rule = Rule Text Exprtype Expr = [List]type List = [Term]data Term = Lit Text | RuleName Text
  70. 70. BNF (10 !)syntax = many1 rulerule = Rule <$> (spaces *> "<" .*> text > <*. ">") <* spaces <* string "::=" <* spaces <*> expression <* line_endspaces = T.pack <$> many (char )expression = sepBy1 list (spaces *> string "|" <* spaces)line_end = many1 $ spaces *> endOfLinelist = sepBy1 term spacesterm = Lit <$> literal <|> RuleName <$> "<" .*> text > <*. ">"literal = "" .*> text <*. "" <|> """ .*> text " <*. """text c = takeTill (==c)
  71. 71. 3HaskellParser
  72. 72. BNFsyntax = many1 rulerule = Rule <$> (spaces *> "<" .*> text > <*. ">") <* spaces <* string "::=" <* spaces <*> expression <* line_endspaces = T.pack <$> many (char )expression = sepBy1 list (spaces *> string "|" <* spaces)line_end = many1 $ spaces *> endOfLinelist = sepBy1 term spacesterm = Lit <$> literal <|> RuleName <$> "<" .*> text > <*. ">"literal = "" .*> text <*. "" <|> """ .*> text " <*. """text c = takeTill (==c)
  73. 73. 3
  74. 74. HaskellMona*,Applicative,Functor,Arrow,...
  75. 75. Happy Hacking Parserin Haskell!

×