Programming Haskell Chapter8

  • 4,899 views
Uploaded on

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

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

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,899
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
55
Comments
0
Likes
9

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

  • 1. 8 : ruicc (ruicc.rail@gmail.com)
  • 2. • ( )•@ruicc•http://d.hatena.ne.jp/ruicc•LinkThink ! Altplus
  • 3. Haskell 8• Google Suggest
  • 4. •Haskell
  • 5. P.90 †2[ ]• Web 10
  • 6. P.90 †2[ ]• Web 10
  • 7. P.90 †2[ ]
  • 8. P.90 †2[ ]
  • 9. P.90 †2[ ]• =>• import Prelude hiding(return,(>>=),(>>))• do (Mona* )•
  • 10. •••Haskell
  • 11. • • *••advanced :
  • 12.
  • 13. •Parser [pärsəәr]•
  • 14. “-32.4” Parser -32.4
  • 15. •type Parser = String -> Tree ※Tree
  • 16. •type Parser = String -> (Tree, String)
  • 17. •type Parser = String -> [(Tree, String)]
  • 18. • atype Parser a = String -> [(a, String)] ※ advanced:
  • 19. •“1.4aaa” Parser [(1.4,“aaa”)]
  • 20. •“+.4aaa” Parser []
  • 21. • return :: a -> Parser a •• failure :: Parser a •• item :: Parser Char •
  • 22. parse• Parser String • Parser parse:: Parser a -> String -> [(a,String)]
  • 23. parse •parse“1.4aaa” Parser [(1.4,“aaa”)] parse !
  • 24. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  • 25. •• • •
  • 26. sign digits“-3.24” [(-3.24,“”)] pointparse 1 digits ※
  • 27. • •
  • 28. •p, then q double =digits >>= point >>= digits ※
  • 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. •p, or q char ‘+’ sign = +++ char ‘-’
  • 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. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  • 33. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  • 34. “-3.24” sign [(‘-’,“3.24”)]“3.24” digits [(“3”,“.24”)]“.24” point [(‘.’,“24”)]
  • 35. • (>>=)double = sign >>= s -> digits >>= num -> point >>= p -> digits >>= num2 -> return (func s num p num2) ※sign,digits,point given
  • 36. • returndouble = sign >>= s -> digits >>= num -> point >>= p -> digits >>= num2 -> return (func s num p num2)
  • 37. do • Parserdouble :: Parser Doubledouble = do s <- sign num <- digits p <- point num2 <- digits return (func s num p num2)
  • 38. • type Parser a = String -> [(a, String)] • Char • Parser a • Parser
  • 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. do••• String• (>>=)
  • 41. sign digits“-3.24” [(-3.24,“”)] point digits
  • 42. • return, failure, item• (>>=)• (+++)• (do )
  • 43. •• (>>=)• (+++)• ( )
  • 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. • sat digit = sat isDigit lower = sat isLower upper = sat isUpper letter = sat isAlpha alphanum = sat isAlphanum char x = sat (==x)
  • 46. • char string string :: String -> Parser String string [] = return [] string (x:xs) = do char x string xs return (x:xs)
  • 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. • 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. • token :: Parser a -> Parser a token p = do space v <- p space return v
  • 50. • token identifier :: Parser String identifier = token ident natural :: Parser Int natural = token nat symbol :: Parser String symbol xs = token (string xs)
  • 51.
  • 52. :•BNF(Backus-Naur Form) • • (|) ( )
  • 53. • BNF..? expr ::= expr ‘+’ expr | expr ‘*’ expr | ‘(‘ expr ‘)’ | nat nat ::= ‘0’ | ‘1’ | ‘2’ | ...
  • 54. •(*) (+)• • (expr), (term), (factor)
  • 55. • BNF expr ::= expr ‘+’ expr | term term ::= term ‘*’ term | factor factor ::= ‘(‘ expr ‘)’ | nat nat ::= ‘0’ | ‘1’ | ‘2’ | ...
  • 56. ••1 + 2 + 3 == 1 + (2 + 3)••1 - 2 - 3 == (1 - 2) - 3•
  • 57. • BNF( ) expr ::= term ‘+’ expr | term term ::= factor ‘*’ term | factor factor ::= ‘(‘ expr ‘)’ | nat nat ::= ‘0’ | ‘1’ | ‘2’ | ...
  • 58. • BNF( ) expr ::= term ‘+’ expr | term term ::= factor ‘*’ term | factor →
  • 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. •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. •BNF 3 -- factor ::= ‘(‘ expr ‘)’ | nat factor :: Parser Int factor = do symbol “(“ e <- (expr :: Parser Int) symbol “)” return e +++ natural
  • 62. • expr ghci> parse expr "5 * (2) + ( 4 +(2 * 2 + 4) * 2) * 5 " [(110,””)]
  • 63. HaskellParser
  • 64. Package•Parsec3 ••attoparsec •
  • 65. • • letter, digit, char, string, many, ...• (+++) (<|>), try • • import Control.Applicative (<|>) • try
  • 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. cabal install attoparseccabal install attoparsec-text
  • 68. {-# LANGUAGE OverloadedStrings #-}module BNFParser whereimport Data.Attoparsec.Textimport Data.Text.Internal (Text)import Data.Text as Timport Control.Applicative hiding(many)
  • 69. BNFtype BNF = Syntaxtype Syntax = [Rule]data Rule = Rule Text Exprtype Expr = [List]type List = [Term]data Term = Lit Text | RuleName Text
  • 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. 3HaskellParser
  • 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. 3
  • 74. HaskellMona*,Applicative,Functor,Arrow,...
  • 75. Happy Hacking Parserin Haskell!