2014年2月26日水曜日

開発環境

Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の10章(コード事例研究: バイナリデータフォーマットの構文解析)、10.4(暗黙の状態)、10.9(今後の方向性)の練習問題 1.を解いてみる。

その他参考書籍

練習問題 1.

前回までのコード

コード(BBEdit, Emacs)

ParseP2.hs

{-# OPTIONS -Wall -Werror #-}
module ParseP2 where

import Control.Applicative ((<$>))
import Data.Char (isSpace, isDigit)
import System.Environment (getArgs)

main :: IO ()
main = do
    (a:_) <- getArgs
    contents <- readFile a
    let g = runParse parseP2 $ ParseState contents
    print $ fmap fst g
    putStrLn "データ"
    print $ greyData <$> fst <$> g

data GreymapP2 = GreymapP2 {greyWith :: Int,
                            greyHeight :: Int,
                            greyMax :: Int,
                            greyData :: [Int]}
                 deriving (Eq)

instance Show GreymapP2 where
    show (GreymapP2 w h m _) = "GreymapP2 " ++ show w ++ "x" ++ show h ++
                               " " ++ show m

data ParseState = ParseState {string :: String} deriving (Show)

-- 構文解析器の型をnewtypek宣言を使って名前を付けて隠す
-- 各解析関数の型宣言等を書き換える
newtype Parse a = Parse {runParse :: ParseState -> Maybe (a, ParseState)}

parseP2 :: Parse GreymapP2
parseP2 =
    parseHeader ==> \header -> skipSpaces ==>&
    assert (header == "P2")               ==>&
    parseNat ==> \width -> skipSpaces     ==>&
    parseNat ==> \height -> skipSpaces    ==>&
    parseNat ==> \maxGrey -> skipSpaces   ==>&
    assert (maxGrey <= 255)               ==>&
    parseInts (width * height) ==> \ns -> 
        Parse $ \s -> Just (GreymapP2 width height maxGrey ns, s)

(==>) :: Parse a -> (a -> Parse b) -> Parse b
firstParser ==> secondParser = Parse chainedParser
    where chainedParser initState =
              case runParse firstParser initState of
                   Nothing -> Nothing
                   Just (firstResult, newState) ->
                       runParse (secondParser firstResult) newState

(==>&) :: Parse a -> Parse b -> Parse b
p ==>& f = p ==> \_ -> f

skipSpaces :: Parse ()
skipSpaces = Parse $ \s ->
    Just ((), ParseState (dropWhile isSpace (string s)))

assert :: Bool -> Parse ()
assert True  = Parse $ \s -> Just ((), s)
assert False = Parse $ \_ -> Nothing

parseHeader :: Parse String
parseHeader = Parse $ \s ->
    let str = string s
        header = takeWhile (not . isSpace) str
    in Just (header, ParseState (drop (length header) str))

parseNat :: Parse Int
parseNat = Parse $ \s ->
    let (num, str) = span isDigit (string s)
    in if null num 
       then Nothing
       else Just (read num :: Int, ParseState str)

parseInts :: Int -> Parse [Int]
parseInts n = Parse $ \s -> iter n (' ':string s) []

iter :: Int -> String -> [Int] -> Maybe ([Int], ParseState)
iter n s ns
    | n == 0 && (null s || null (dropWhile isSpace s))
        = Just (ns, ParseState s)
    | n /= 0 && null s = Nothing
    | n /= 0 && (isSpace (head s)) =
        let (num, str) = span isDigit (tail s)
        in if null num
        then Nothing
        else iter (n - 1) str (ns ++ [read num :: Int])
    | otherwise = Nothing

入出力結果(Terminal, runghc)

$ runghc ParseP2.hs test.pgm 
Just GreymapP2 24x7 15
データ
Just [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,0,0,7,7,7,7,0,0,11,11,11,11,0,0,15,15,15,15,0,0,3,0,0,0,0,0,7,0,0,0,0,0,11,0,0,0,0,0,15,0,0,15,0,0,3,3,3,0,0,0,7,7,7,0,0,0,11,11,11,0,0,0,15,15,15,15,0,0,3,0,0,0,0,0,7,0,0,0,0,0,11,0,0,0,0,0,15,0,0,0,0,0,3,0,0,0,0,0,7,7,7,7,0,0,11,11,11,11,0,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
$ runghc ParseP2.hs ParseP2.hs 
Nothing
データ
Nothing
$ cat test.pgm 
P2
24 7
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$

次回は新たな解析器を実装して、その解析器で各解析器の共通する部分を書き換えてみることに。

0 コメント:

コメントを投稿