SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
君ならどう書く - Haskell –
       規定演技編
             酒井 政裕
トピック

パーサ
  パーサ・コンビネータ
  漢数字のパーサ
  式のパーサ
式の評価
まとめ
パーサ・コンビネータ

パーサを定義し、また単純なパーサからより
複雑なパーサを定義するための一連の関数。
EDSL(Embeded Domain Specific
Language)の典型的な例。
 BNF風の表記がそのままプログラムに!?
実装は色々あるけど、今回は自前で実装。
パーサ・コンビネータ (cont’d)
式          言語       説明
return a            常に成功しaを返すパーサ
           ε
p <|> q    p|q      pとqの選択
mzero      ∅        常に失敗するパーサ
char c     {c}      文字cを受理するパーサ
string s   {s}      文字列sを受理するパーサ
p >>= f    p f(x)   pとf(x)の連接 (xはpのパース結果)
p >> q     pq       pとqの連接
many p     p*       0回以上の繰り返し
モナド(Monad)

 compuatationを抽象化するインターフェース
  普通の計算だけでなく、色々な計算(「例外」「副作
  用」「非決定性」があったり無かったりとか)を統一し
  て扱いたい。
  元々は圏論の概念だけど、それはどうでもいい。
 このパーサコンビネータもモナドになっている
  returnと(>>=)が存在してある法則を満たす。
  既存のライブラリと糖衣構文(do記法)が使える。
漢数字のパーサ

最初に考えた文法
 number ::= n1
 n1 ::= n2 | n2? “無量大数” n2?
 n2 ::= n3 | n3? “不可思議” n3?
 …
 nn ::= digit
効率が悪い
漢数字のパーサ (cont’d)

 実装した文法
  number ::= n1\{ε}
  n1 ::= n2 (“無量大数” n2)?
  n2 ::= n3 (“不可思議” n3)?
  …
  nn ::= digit | ε
 特徴
  「一」は省略してもOK
  「一万億」なんてのも受け付けてしまう
    これは後で気づいた。とりあえず放置。
漢数字のパーサ (cont’d)
number :: Parser Integer
number = do x <- number' units
            case x of
             Just y -> return y
             Nothing -> mzero

number' :: [(String, Integer)] -> Parser (Maybe Integer)
number' [] = liftM Just digit <|> return Nothing
number' ((s,u) : us) =
  do x <- number' us
   let p = do string s
            y <- number' us
            return (Just (fromMaybe 1 x * u + fromMaybe 0 y))
   p <|> return x
式のパーサ

左再帰を除去した形の文法でパース
 expr ::= term (('+'|'-‘) term)*
 term ::= factor (('×'|'÷‘) factor)*
 factor ::= ‘(’ expr ‘)’ | number
式のパーサ (cont’d)
-- genExpr(sub,op) ::= sub (op sub)*
genExpr :: Parser Expr -> Parser BinOp -> Parser Expr
genExpr sub op = do e <- sub
                     xs <- many p
                     return (foldl (¥a (o,b) -> BinOpApp o a b) e xs)
   where p = do o <- op
                x <- sub
                return (o,x)

-- expr ::= term (('+'|'-‘) term)*
expr :: Parser Expr
expr = genExpr term op
   where op = (string quot;+quot; >> return Add)
             <|> (string quot;-quot; >> return Sub)
式のパーサ (cont’d)
-- term ::= factor (('×'|'÷‘) factor)*
term :: Parser Expr
term = genExpr factor op
    where op = (string quot;×quot; >> return Mul)
             <|> (string quot;÷quot; >> return Div)

-- factor ::= '(' expr ')' | number
factor :: Parser Expr
factor = parenthesize expr
      <|> do x <- number
             return (Number x)
パース結果のデータ構造

data Expr
  = Number Integer
  | BinOpApp BinOp Expr Expr
data BinOp = Add | Sub | Mul | Div

代数的データ型
  タグ付のunionみたいなもの
  パラメタを持たない場合はenumみたいなもの
式の評価
このExprに表示的意味論を定義する

  data Value = Maybe Rational
 意味領域

  ….

  eval :: Expr -> Value
 意味関数

  eval (Number x) = fromIntegral x
  eval (BinOpApp op a b) =
     evalBinOp op (eval a) (eval b)
  ....
実行霊もとい実行例
> 八÷(一-一÷五)

> 一×二×四×十六×二百五十六×六万五千五百
十

  三十六×四十二億九千四百九十六万七千二百九
  十六×千八百四十四京六千七百四十四兆七百三
  十七億九百五十五万千六百十六-一
百七十澗千四百十一溝八千三百四十六穣四百六十
  九禾予二千三百十七垓三千百六十八京七千三百
  三兆七千百五十八億八千四百十万五千七百二十
  七
まとめ

Haskellの高い抽象化能力
パーサ・コンビネータによるパーサの定義
代数的データ型による再帰的なデータ構造の
表現

Weitere ähnliche Inhalte

Was ist angesagt?

Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974
Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974
Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974muzaffertahir9
 
Benamazi ka anjam by maulana shakir ali noori
Benamazi ka anjam by maulana shakir ali nooriBenamazi ka anjam by maulana shakir ali noori
Benamazi ka anjam by maulana shakir ali nooriAtaurrahman Noori
 
187 Takhlees Usoo Le Shashi - brelvi madaris
187 Takhlees Usoo Le Shashi - brelvi madaris187 Takhlees Usoo Le Shashi - brelvi madaris
187 Takhlees Usoo Le Shashi - brelvi madarisnabeelsahab
 
Hazrat Khalid Bin Waleed Book By Ataurrahman Noori
Hazrat Khalid Bin Waleed Book By Ataurrahman NooriHazrat Khalid Bin Waleed Book By Ataurrahman Noori
Hazrat Khalid Bin Waleed Book By Ataurrahman NooriAtaurrahman Noori
 
Faiz e alam december 2015
Faiz e alam december 2015Faiz e alam december 2015
Faiz e alam december 2015Muhammad Tariq
 
قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر
قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر
قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر muzaffertahir9
 
Al Fazl International - weekly 19 September 2014
Al Fazl International - weekly 19 September 2014Al Fazl International - weekly 19 September 2014
Al Fazl International - weekly 19 September 2014muzaffertahir9
 
اعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیں
اعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیںاعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیں
اعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیںIlyas Qadri Ziaee
 
A Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4th
A Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4thA Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4th
A Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4thmuzaffertahir9
 
کربلا کا خونیں منظر
کربلا کا خونیں منظرکربلا کا خونیں منظر
کربلا کا خونیں منظرIlyas Qadri Ziaee
 
The tabernacle of Moses (Telugu)
The tabernacle of Moses (Telugu)The tabernacle of Moses (Telugu)
The tabernacle of Moses (Telugu)Shalem Arasavelli
 
Lataif e ashrafi malfoozat e syed makhdoom ashraf 54
Lataif e ashrafi malfoozat e syed makhdoom ashraf 54Lataif e ashrafi malfoozat e syed makhdoom ashraf 54
Lataif e ashrafi malfoozat e syed makhdoom ashraf 54Aale Rasool Ahmad
 

Was ist angesagt? (16)

Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974
Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974
Friday Sermon Delivered by Hazrat Mirza Nasir Ahmed (RA) 05 July1974
 
Benamazi ka anjam by maulana shakir ali noori
Benamazi ka anjam by maulana shakir ali nooriBenamazi ka anjam by maulana shakir ali noori
Benamazi ka anjam by maulana shakir ali noori
 
187 Takhlees Usoo Le Shashi - brelvi madaris
187 Takhlees Usoo Le Shashi - brelvi madaris187 Takhlees Usoo Le Shashi - brelvi madaris
187 Takhlees Usoo Le Shashi - brelvi madaris
 
Ganesha manasapuja
Ganesha manasapujaGanesha manasapuja
Ganesha manasapuja
 
039 az-zumar
039 az-zumar039 az-zumar
039 az-zumar
 
My manasapuja
My manasapujaMy manasapuja
My manasapuja
 
Hazrat Khalid Bin Waleed Book By Ataurrahman Noori
Hazrat Khalid Bin Waleed Book By Ataurrahman NooriHazrat Khalid Bin Waleed Book By Ataurrahman Noori
Hazrat Khalid Bin Waleed Book By Ataurrahman Noori
 
Faiz e alam december 2015
Faiz e alam december 2015Faiz e alam december 2015
Faiz e alam december 2015
 
قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر
قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر
قرار دینا صرف اللہ کا کام ہے - خطبہ جمعہ - 8 مئی 1981متقی اورمطہر
 
Al Fazl International - weekly 19 September 2014
Al Fazl International - weekly 19 September 2014Al Fazl International - weekly 19 September 2014
Al Fazl International - weekly 19 September 2014
 
اعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیں
اعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیںاعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیں
اعلٰحضرت (رحمۃ اللہ علیہ) کی انفرادی کوششیں
 
A Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4th
A Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4thA Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4th
A Life in the Day of Hazrat Mirza Tahir Ahmad (RA) Kahalifa tul Masih the 4th
 
Shikshapatri
ShikshapatriShikshapatri
Shikshapatri
 
کربلا کا خونیں منظر
کربلا کا خونیں منظرکربلا کا خونیں منظر
کربلا کا خونیں منظر
 
The tabernacle of Moses (Telugu)
The tabernacle of Moses (Telugu)The tabernacle of Moses (Telugu)
The tabernacle of Moses (Telugu)
 
Lataif e ashrafi malfoozat e syed makhdoom ashraf 54
Lataif e ashrafi malfoozat e syed makhdoom ashraf 54Lataif e ashrafi malfoozat e syed makhdoom ashraf 54
Lataif e ashrafi malfoozat e syed makhdoom ashraf 54
 

Mehr von Masahiro Sakai

DeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningMasahiro Sakai
 
Towards formal verification of neural networks
Towards formal verification of neural networksTowards formal verification of neural networks
Towards formal verification of neural networksMasahiro Sakai
 
関数プログラマから見たPythonと機械学習
関数プログラマから見たPythonと機械学習関数プログラマから見たPythonと機械学習
関数プログラマから見たPythonと機械学習Masahiro Sakai
 
Writing a SAT solver as a hobby project
Writing a SAT solver as a hobby projectWriting a SAT solver as a hobby project
Writing a SAT solver as a hobby projectMasahiro Sakai
 
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...Masahiro Sakai
 
SAT/SMT solving in Haskell
SAT/SMT solving in HaskellSAT/SMT solving in Haskell
SAT/SMT solving in HaskellMasahiro Sakai
 
SAT/SMTソルバの仕組み
SAT/SMTソルバの仕組みSAT/SMTソルバの仕組み
SAT/SMTソルバの仕組みMasahiro Sakai
 
Introduction to Max-SAT and Max-SAT Evaluation
Introduction to Max-SAT and Max-SAT EvaluationIntroduction to Max-SAT and Max-SAT Evaluation
Introduction to Max-SAT and Max-SAT EvaluationMasahiro Sakai
 
Aluminum: Principled Scenario Exploration through Minimality
Aluminum: Principled Scenario Exploration through MinimalityAluminum: Principled Scenario Exploration through Minimality
Aluminum: Principled Scenario Exploration through MinimalityMasahiro Sakai
 
代数的実数とCADの実装紹介
代数的実数とCADの実装紹介代数的実数とCADの実装紹介
代数的実数とCADの実装紹介Masahiro Sakai
 
How a CDCL SAT solver works
How a CDCL SAT solver worksHow a CDCL SAT solver works
How a CDCL SAT solver worksMasahiro Sakai
 
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)Masahiro Sakai
 
自動定理証明の紹介
自動定理証明の紹介自動定理証明の紹介
自動定理証明の紹介Masahiro Sakai
 
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...Masahiro Sakai
 
“Design and Implementation of Generics for the .NET Common Language Runtime”他...
“Design and Implementation of Generics for the .NET Common Language Runtime”他...“Design and Implementation of Generics for the .NET Common Language Runtime”他...
“Design and Implementation of Generics for the .NET Common Language Runtime”他...Masahiro Sakai
 
Relaxed Dependency Analysis
Relaxed Dependency AnalysisRelaxed Dependency Analysis
Relaxed Dependency AnalysisMasahiro Sakai
 
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...Masahiro Sakai
 
自然言語をラムダ式で解釈する体系PTQのHaskell実装
自然言語をラムダ式で解釈する体系PTQのHaskell実装自然言語をラムダ式で解釈する体系PTQのHaskell実装
自然言語をラムダ式で解釈する体系PTQのHaskell実装Masahiro Sakai
 

Mehr von Masahiro Sakai (20)

DeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep Learning
 
Towards formal verification of neural networks
Towards formal verification of neural networksTowards formal verification of neural networks
Towards formal verification of neural networks
 
関数プログラマから見たPythonと機械学習
関数プログラマから見たPythonと機械学習関数プログラマから見たPythonと機械学習
関数プログラマから見たPythonと機械学習
 
Writing a SAT solver as a hobby project
Writing a SAT solver as a hobby projectWriting a SAT solver as a hobby project
Writing a SAT solver as a hobby project
 
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
 
ゼロピッチ: MOOC
ゼロピッチ: MOOCゼロピッチ: MOOC
ゼロピッチ: MOOC
 
SAT/SMT solving in Haskell
SAT/SMT solving in HaskellSAT/SMT solving in Haskell
SAT/SMT solving in Haskell
 
SAT/SMTソルバの仕組み
SAT/SMTソルバの仕組みSAT/SMTソルバの仕組み
SAT/SMTソルバの仕組み
 
Introduction to Max-SAT and Max-SAT Evaluation
Introduction to Max-SAT and Max-SAT EvaluationIntroduction to Max-SAT and Max-SAT Evaluation
Introduction to Max-SAT and Max-SAT Evaluation
 
Aluminum: Principled Scenario Exploration through Minimality
Aluminum: Principled Scenario Exploration through MinimalityAluminum: Principled Scenario Exploration through Minimality
Aluminum: Principled Scenario Exploration through Minimality
 
代数的実数とCADの実装紹介
代数的実数とCADの実装紹介代数的実数とCADの実装紹介
代数的実数とCADの実装紹介
 
How a CDCL SAT solver works
How a CDCL SAT solver worksHow a CDCL SAT solver works
How a CDCL SAT solver works
 
Omega test and beyond
Omega test and beyondOmega test and beyond
Omega test and beyond
 
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
 
自動定理証明の紹介
自動定理証明の紹介自動定理証明の紹介
自動定理証明の紹介
 
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
 
“Design and Implementation of Generics for the .NET Common Language Runtime”他...
“Design and Implementation of Generics for the .NET Common Language Runtime”他...“Design and Implementation of Generics for the .NET Common Language Runtime”他...
“Design and Implementation of Generics for the .NET Common Language Runtime”他...
 
Relaxed Dependency Analysis
Relaxed Dependency AnalysisRelaxed Dependency Analysis
Relaxed Dependency Analysis
 
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
 
自然言語をラムダ式で解釈する体系PTQのHaskell実装
自然言語をラムダ式で解釈する体系PTQのHaskell実装自然言語をラムダ式で解釈する体系PTQのHaskell実装
自然言語をラムダ式で解釈する体系PTQのHaskell実装
 

LLDN / キミならどう書く Haskell 編 (規定演技)