0
8   :    ruicc (ruicc.rail@gmail.com)
•        (             )•@ruicc•http://d.hatena.ne.jp/ruicc•LinkThink ! Altplus
Haskell 8•   Google Suggest
•Haskell
P.90 †2[        ]•    Web                     10
P.90 †2[        ]•    Web                     10
P.90 †2[   ]
P.90 †2[   ]
P.90 †2[                 ]•              =>•   import Prelude hiding(return,(>>=),(>>))•   do         (Mona*              ...
•••Haskell
• •   *••advanced   :
•
•Parser   [pärsəәr]•
“-32.4”   Parser   -32.4
•type Parser = String -> Tree                  ※Tree
•type Parser = String -> (Tree, String)
•type Parser = String -> [(Tree, String)]
•                          atype Parser a = String -> [(a, String)]   ※                            advanced:
•“1.4aaa”   Parser   [(1.4,“aaa”)]
•“+.4aaa”   Parser   []
• return    :: a -> Parser a •• failure    :: Parser a •• item   :: Parser Char •
parse• Parser   String   • Parser  parse:: Parser a -> String -> [(a,String)]
parse  •parse“1.4aaa”     Parser       [(1.4,“aaa”)]           parse      !
“-3.24”    sign    [(‘-’,“3.24”)]“3.24”    digits   [(“3”,“.24”)]“.24”     point    [(‘.’,“24”)]
••    •    •
sign            digits“-3.24”              [(-3.24,“”)]            pointparse   1            digits                       ※
• •
•p,   then q               double                 =digits   >>= point >>=   digits                           ※
•                     (>>=)                               p(>>=):: Parser a -> (a -> Parser b) -> Parser bp >>= f = inp ->...
•p,   or q                 char ‘+’  sign       =     +++                 char ‘-’
•                          p(+++):: Parser a -> Parser a -> Parser ap +++ q = inp -> case parse p inp of                  ...
“-3.24”    sign    [(‘-’,“3.24”)]“3.24”    digits   [(“3”,“.24”)]“.24”     point    [(‘.’,“24”)]
“-3.24”    sign    [(‘-’,“3.24”)]“3.24”    digits   [(“3”,“.24”)]“.24”     point    [(‘.’,“24”)]
“-3.24”    sign    [(‘-’,“3.24”)]“3.24”    digits   [(“3”,“.24”)]“.24”     point    [(‘.’,“24”)]
•         (>>=)double =  sign     >>= s    ->  digits   >>= num ->  point    >>= p    ->  digits   >>= num2 ->  return   (...
•     returndouble =  sign     >>= s    ->  digits   >>= num ->  point    >>= p    ->  digits   >>= num2 ->  return   (fun...
do •        Parserdouble :: Parser Doubledouble = do    s    <- sign    num <- digits    p    <- point    num2 <- digits  ...
• type Parser a = String -> [(a, String)] •          Char • Parser            a • Parser
do      •    double :: Parser Double    double = do Char   s    <- sign   :: Parser Char        num <- digits :: Parser St...
do•••           String•   (>>=)
sign          digits“-3.24”            [(-3.24,“”)]          point          digits
• return, failure,   item•     (>>=)•     (+++)• (do )
••   (>>=)•   (+++)•   (   )
• item                          sat             itemsat :: (Char -> Bool) -> Parser Charsat p = do x <- item            if...
• sat  digit    = sat isDigit  lower    = sat isLower  upper    = sat isUpper  letter   = sat isAlpha  alphanum = sat isAl...
• char                  string  string :: String -> Parser String  string []     = return []  string (x:xs) = do char x   ...
•                           many, many1    many, many1 :: Parser a -> Parser [a]    many p   = many1 p +++ return []    ma...
• many  ident = do x <- lower              xs <- many alphanum              return (x:xs)  nat    = do xs <- many1 digit  ...
•    token :: Parser a -> Parser a    token p = do space                 v <- p                 space                 retu...
• token  identifier :: Parser String  identifier = token ident  natural :: Parser Int  natural = token nat  symbol :: Pars...
•
:•BNF(Backus-Naur   Form) • • (|)                     (   )
•                                     BNF..?    expr ::= expr ‘+’ expr            | expr ‘*’ expr            | ‘(‘ expr ‘)...
•(*)      (+)• • (expr), (term),   (factor)
•                       BNF    expr   ::= expr ‘+’ expr | term    term   ::= term ‘*’ term | factor    factor ::= ‘(‘ expr...
••1   + 2 + 3 == 1 + (2 + 3)••1   - 2 - 3 == (1 - 2) - 3•
•                     BNF(             )    expr   ::= term ‘+’ expr | term    term   ::= factor ‘*’ term | factor    fact...
•                     BNF(            )    expr   ::= term ‘+’ expr | term    term   ::= factor ‘*’ term | factor         ...
•BNF                                   1 -- expr ::= term ‘+’ expr | term expr :: Parser Int expr = do t <- (term :: Parse...
•BNF                                     2 -- term ::= factor ‘*’ term | factor term :: Parser Int term = do f <- (factor ...
•BNF                                     3 -- factor ::= ‘(‘ expr ‘)’ | nat factor :: Parser Int factor = do symbol “(“   ...
•                    expr    ghci> parse expr "5 * (2) +   ( 4   +(2 * 2 + 4) * 2)   * 5 "    [(110,””)]
HaskellParser
Package•Parsec3 ••attoparsec •
•    • letter,    digit, char, string, many, ...•      (+++)              (<|>), try    •    • import    Control.Applicati...
BNF  BNF         BNF       (wikipedia       )<syntax> ::= <rule> | <rule> <syntax><rule> ::= <opt-whitespace> "<" <rule-na...
cabal install attoparseccabal install attoparsec-text
{-# LANGUAGE OverloadedStrings #-}module BNFParser whereimport Data.Attoparsec.Textimport Data.Text.Internal (Text)import ...
BNFtype BNF = Syntaxtype Syntax = [Rule]data Rule = Rule Text Exprtype Expr = [List]type List = [Term]data Term = Lit Text...
BNF                      (10          !)syntax = many1 rulerule = Rule <$> (spaces *> "<" .*> text > <*. ">") <* spaces   ...
3HaskellParser
BNFsyntax = many1 rulerule = Rule <$> (spaces *> "<" .*> text > <*. ">") <* spaces     <* string "::=" <* spaces <*> expre...
3
HaskellMona*,Applicative,Functor,Arrow,...
Happy Hacking Parserin Haskell!
Programming Haskell Chapter8
Programming Haskell Chapter8
Upcoming SlideShare
Loading in...5
×

Programming Haskell Chapter8

5,272

Published on

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

Published in: Technology, Education
0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,272
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
59
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "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!
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×