SlideShare une entreprise Scribd logo
1  sur  64
하스켈 프로그래밍
입문 3
하스켈 학교 2016년 5월 28일
서광열
Identity, State, Writer
Monad
Term
data Term = Con Int | Div Term Term
변종0: Basic Evaluator
eval :: Term -> Int
eval (Con a) = a
eval (Div t u) = eval t `div` eval u
변종1: 예외 처리
data M a = Raise Exception | Return a
type Exception = String
변종1: 예외 처리
eval :: Term -> M Int
eval (Con a) = Return a
eval (Div t u) = case eval t of
Raise e -> Raise e
Return a ->
case eval u of
Raise e -> Raise e
Return b ->
if b == 0
then Raise “divide by zero”
else Return (a `div` b)
변종2: division 카운터
type M a = State -> (a, State)
type State = Int
변종2: division 카운터
eval :: Term -> M Int
eval (Con a) x = (a, x)
eval (Div t u) x = let (a, y) = eval t x in
let (b, z) = eval u y in
(a `div` b, z + 1)
변종3: 실행 과정 출력
type M a = (Output, a)
type Output = String
변종3: 실행 과정 출력
eval :: Term -> M Int
eval @term(Con a) = (line term a, a)
eval @term(Div t u) = let (x, a) = eval t in
let (y, b) = eval u in
(x ++ y ++ line term (a `div` b), a `div` b)
line :: Term -> Int -> Output
line t a = “eval (“ ++ show t ++ “) <-” ++ show a ++ “n”
Monadic Evaluator
eval :: (Moand m) => Term -> m Int
eval (Con a) = return a
eval (Div t u) = do a <- eval t
b <- eval u
return (a `div` b)
Monadic Evaluator
eval :: (Moand m) => Term -> m Int
eval (Con a) = return a
eval (Div t u) = do a <- eval t
b <- eval u
return (a `div` b)
변종0: Basic Evaluator
newtype M a = I a
instance Monad M where
return a = I a
(I a) >>= k = k a
변종1: 예외 처리
data M a = Raise Exception | Return a
type Exception = String
instance Monad M where
return a = Return a
m >>= k = case m of
Raise e -> Raise e
Return a -> k a
raise :: Exception -> M a
raise e = Raise e
변종1: 예외 처리
eval :: Term -> M Int
eval (Con a) = return a
eval (Div t u) = do a <- eval t
b <- eval u
if b == 0
then raise “divide by zero”
else return (a `div` b)
변종2: division 카운터
type M a = State -> (a, State)
type State = Int
instance Monad M where
return a = x -> (a, x)
m >>= k = x -> let (a, y) = m x in
let (b, z) = k a y in
(b, z)
tick :: M ()
tick = x -> ((), x + 1)
변종2: division 카운터
eval :: Term -> M Int
eval (Con a) = return a
eval (Div t u) = do a <- eval t
b <- eval u
tick
return (a `div` b)
변종3: 실행 과정 출력
type M a = (Output, a)
type Output = String
instance Monad m where
return a = (“”, a)
m >>= k = let (x, a) = m in
let (y, b) = k a in
(x ++ y, b)
out :: Output -> M ()
out x = (x, ())
변종3: 실행 과정 출력
eval :: Term -> M Int
eval term@(Con a) = do out (line term a)
return a
eval term@(Div t u) = do a <- eval t
b <- eval u
out (line term (a `div` b))
return (a `div` b)
Control.Monad
• mapM :: Monad m => (a -> m b) -> [a] -> m [b]
• forM :: Monad m => [a] -> (a -> m b) -> m [b]
• sequence :: Monad m => [m a] -> m [a]
Note: [t] generalizes to Traversable t
Functional Parser
출처: http://www.willamette.edu/~fruehr/haskell/seuss.html
Parser
newtype Parser a = Parser (String -> [a, String])
• empty 리스트는 실패, 아니면 성공
• (a, s)에서 a는 파싱한 결과 s는 파싱하고 남은 문자
열
• 리스트를 리턴하여 ambiguous grammar 처리
item
• 문자열이 empty가 아니면 첫 번째 문자를 리턴
• empty이면 에러
item :: Parser Char
item = Parser (cs -> case cs of
"" -> []
(c:cs) -> [(c, cs)])
모나드 정의
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
instance Monad Parser where
return a = (Parser (cs -> [(a, cs)])
p >>= f = (Parser (cs -> concat [parse (f a) cs’ |
(a, cs’) <- parse p cs])
parse (Parser p) = p
모나드 법칙
1. return a >> f = f a
2. p >>= return = p
3. p >>= (a -> (f a) >>= g)) = (p >>= (a -> f a)) >>= g
Do 표기법
p1 >>= a1 ->
p2 >>= a2 ->
…
pn >>= an ->
f a1 a2 … an
do a1 <- p1
a2 <- p2
…
an <- pn
f a1 a2 … an
Do 표기법 사용 예제
p :: Parser (Char, Char)
p = do c <- item
item
d <- item
return (c, d)
MonadZero, MonadPlus
class Monad m => MonadZero m where
zero :: m a
class MonadZero m => MonadPlus m where
(++) :: m a -> m a -> m a
Choice Operator
instance MonadZero Parser where
zero = Parser (cs -> [])
instance MonadPlus Parser where
p ++ q = (Parser (cs ->
parse p cs ++ parse q cs))
법칙
• zero ++ p = p
• p ++ zero = p
• p ++ (q ++ r) = (p ++ q) ++ r
• zero >>= f = zero
• p >>= const zero = zero
• (p ++ q) >>= f = (p >>= f) ++ (q >>= f)
• p >>= (a -> f a ++ g a) = (p >>= f) ++ (q >>= g)
Deterministic Choice
Operator
(+++) :: Parser a -> Parser a -> Parser a
p +++ q = Parser (cs -> case parse (p ++ q) of
[] -> []
(x:xs) -> [x])
Conditional Parsing
sat :: (Char -> Bool) -> Parser Char
sat p = do c <- item
if p c then return c
else zero
char :: Char -> Parser Char
char c = sat (c ==)
재귀 Combinator
string :: String -> Parser String
string "" = return ""
string (c:cs) = do char c
string cs
return (c:cs)
재귀 Combinator
many :: Parser a -> Parser [a]
many p = many1 p +++ return []
many1 :: Parser a -> Parser [a]
many1 p = do a <- p
as <- many p
return (a:as)
재귀 Combinator
sepby :: Parser a -> Parser b -> Parser [a]
p `sepby` q = (p `sepby1` q) +++ return []
sepby1 :: Parser a -> Parser b -> Parser [a]
p `sepby1` q = do a <- p
as <- many (do {sep; p})
return (a:as)
재귀 Combinator
chainl :: Parser a -> Parser (a -> a -> a) -> a -> Parser a
chainl p op a = (p `chainl1` op) +++ return a
chainl1 :: Parser a -> (Parser a -> a -> a) -> Parser a
p `chainl1` op = do { a <- p; rest a }
where rest a = (do f <- op
b <- p
return f a b)
+++ return a
Lexical Combinators
space :: Parser String
space = many (sat isSpace)
token :: Parser a -> Parser a
token p = do a <- p
space
return a
Lexical Combinators
symb :: String -> Parser String
symb cs = token (string cs)
apply :: Parser a -> String -> [(a, String)]
apply p = parse (do {space; p})
Expression Grammar
• expr ::= expr addop term | term
• term ::= term mulop factor | factor
• factor ::= digit | (expr)
• digit ::= 0 | 1 | … | 9
• addop ::= + | -
• mulop ::= * | /
Expression Parser
expr :: Parser Int
addop :: Parser (Int -> Int -> Int)
mulop :: Parser (Int -> Int -> Int)
expr = term `chainl1` addop
term = factor `chainl1` mulop
factor = digit +++ do { symb "("; n <- expr; symb ")"; return n }
digit = do { x <- token (sat isDigit); return (ord x - ord ‘0’)}
addop = do { symb "+"; return (+)} +++ do {symb "-"; return (-)}
mulop = do { symb "*"; return (*)} +++ do {symb "/"; return (div)}
Expression Parser
> apply expr " 1 - 2 * 3 + 4 "
[(-1, "")]
Library
1. parsec
2. megaparsec
3. attoparsec
Zipper
Tree
data Tree a = Empty
| Node a (Tree a) (Tree a)
deriving (Show)
freeTree
출처: http://learnyouahaskell.com/zippers
Tree 업데이트
changeToP :: Tree Char -> Tree Char
changeToP (Node x l (Node y (Node _ m n) r)) =
Node x l (Node y (Node 'P' m n) r)
Tree 업데이트
data Direction = L | R deriving (Show)
type Directions = [Direction]
changeToP :: Directions -> Tree Char -> Tree Char
changeToP (L:ds) (Node x l r) = Node x (changeToP ds l) r
changeToP (R:ds) (Node x l r) = Node x l (changeToP ds r)
changeToP [] (Node _ l r) = Node 'P' l r
element
elemAt :: Directions -> Tree a -> a
elemAt (L:ds) (Node _ l _) = elemAt ds l
elemAt (R:ds) (Node _ _ r) = elemAt ds r
elemAt [] (Node x _ _) = x
예제
> let newTree = changeToP [R,L] freeTree
> elemAt [R,L] newTree
'P'
Breadcrumbs
type Breadcrumbs = [Direction]
goLeft :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs)
goLeft (Node _ l _, bs) = (l, L:bs)
goRight :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs)
goRight (Node _ _ r, bs) = (r, R:bs)
Breadcrumbs
> goLeft (goRight (freeTree, []))
(Node 'W' (Node 'C' Empty Empty) (Node 'R' Empty Empty),[L,R])
Zipper 정의
data Crumb a =
LeftCrumb a (Tree a)
| RightCrumb a (Tree a)
deriving (Show)
type Breadcrumbs a = [Crumb a]
Zipper 함수
goLeft :: (Tree a, Breadcrumbs a)
-> (Tree a, Breadcrumbs a)
goLeft (Node x l r, bs) = (l, LeftCrumb x r:bs)
goRight :: (Tree a, Breadcrumbs a)
-> (Tree a, Breadcrumbs a)
goRight (Node x l r, bs) = (r, RightCrumb x l:bs)
Zipper 함수
goUp :: (Tree a, Breadcrumbs a)
-> (Tree a, Breadcrumbs a)
goUp (t, LeftCrumb x r:bs) =
(Node x t r, bs)
goUp (t, RightCrumb x l:bs) =
(Node x l t, bs)
Zipper 정의
type Zipper a = (Tree a, Breadcrumbs a)
Zipper 함수
modify :: (a -> a)
-> Zipper a
-> Zipper a
modify f (Node x l r, bs) =
(Node (f x) l r, bs)
modify f (Empty, bs) = (Empty, bs)
Zipper 함수
topMost :: Zipper a -> Zipper a
topMost (t,[]) = (t,[])
topMost z = topMost (goUp z)
Zipper 사용 예
> let newFocus = modify (_ -> 'P') (goRight (goLeft
(freeTree,[])))
> let newFocus2 = modify (_ -> 'X') (goUp newFocus)
리스트
data List a =
Empty
| Cons a (List a)
deriving (Show, Read, Eq, Ord)
리스트 Zipper의 정의
type ListZipper a = ([a],[a])
리스트 Zipper 함수
goForward :: ListZipper a -> ListZipper a
goForward (x:xs, bs) = (xs, x:bs)
goBack :: ListZipper a -> ListZipper a
goBack (xs, b:bs) = (b:xs, bs)
리스트 Zipper 사용 예
> let xs = [1,2,3,4]
> goForward (xs,[])
([2,3,4],[1])
> goForward ([2,3,4],[1])
([3,4],[2,1])
> goForward ([3,4],[2,1])
([4],[3,2,1])
> goBack ([4],[3,2,1])
([3,4],[2,1])
참고 자료
1. Monads for functional programming
• http://homepages.inf.ed.ac.uk/wadler/papers/markto
berdorf/baastad.pdf
2. Monadic Parsing in Haskell
• http://www.cs.nott.ac.uk/~pszgmh/pearl.pdf
3. Learn You a Haskell for Great Good!
• http://learnyouahaskell.com/zippers

Contenu connexe

Tendances

Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!priort
 
Linear Types in Haskell
Linear Types in HaskellLinear Types in Haskell
Linear Types in HaskellDavid Overton
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Codestasimus
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentalsMauro Palsgraaf
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to MonadsLawrence Evans
 
Functional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianFunctional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianBrian Lonsdorf
 
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with SwiftStanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with SwiftStanfy
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskellLuca Molteni
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monadskenbot
 

Tendances (13)

08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
Linear Types in Haskell
Linear Types in HaskellLinear Types in Haskell
Linear Types in Haskell
 
Millionways
MillionwaysMillionways
Millionways
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentals
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
 
Functional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianFunctional Patterns for the non-mathematician
Functional Patterns for the non-mathematician
 
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with SwiftStanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 

En vedette

Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)Joong Ho Lee
 
Real-World Functional Programming @ Incubaid
Real-World Functional Programming @ IncubaidReal-World Functional Programming @ Incubaid
Real-World Functional Programming @ IncubaidNicolas Trangez
 
Haskell study 15
Haskell study 15Haskell study 15
Haskell study 15Nam Hyeonuk
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGDavid Galichet
 
Playing with State Monad
Playing with State MonadPlaying with State Monad
Playing with State MonadDavid Galichet
 

En vedette (7)

Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)
 
Real-World Functional Programming @ Incubaid
Real-World Functional Programming @ IncubaidReal-World Functional Programming @ Incubaid
Real-World Functional Programming @ Incubaid
 
Haskell study 15
Haskell study 15Haskell study 15
Haskell study 15
 
The monad fear
The monad fearThe monad fear
The monad fear
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUG
 
Playing with State Monad
Playing with State MonadPlaying with State Monad
Playing with State Monad
 

Similaire à 하스켈 프로그래밍 입문 3

Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMatthew Pickering
 
mat lab introduction and basics to learn
mat lab introduction and basics to learnmat lab introduction and basics to learn
mat lab introduction and basics to learnpavan373
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8Kousuke Ruichi
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Dr. Volkan OBAN
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monadsrkaippully
 
Applied machine learning for search engine relevance 3
Applied machine learning for search engine relevance 3Applied machine learning for search engine relevance 3
Applied machine learning for search engine relevance 3Charles Martin
 
Cs221 lecture4-fall11
Cs221 lecture4-fall11Cs221 lecture4-fall11
Cs221 lecture4-fall11darwinrlo
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture gVlad Patryshev
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdfkesav24
 
The Road To Monad Transformers
The Road To Monad TransformersThe Road To Monad Transformers
The Road To Monad TransformersPawel Lisewski
 
The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84Mahmoud Samir Fayed
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABShameer Ahmed Koya
 
The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.10 book - Part 33 of 212The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.10 book - Part 33 of 212Mahmoud Samir Fayed
 

Similaire à 하스켈 프로그래밍 입문 3 (20)

Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template Haskell
 
mat lab introduction and basics to learn
mat lab introduction and basics to learnmat lab introduction and basics to learn
mat lab introduction and basics to learn
 
Monadologie
MonadologieMonadologie
Monadologie
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
Matdis 3.4
Matdis 3.4Matdis 3.4
Matdis 3.4
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
Applied machine learning for search engine relevance 3
Applied machine learning for search engine relevance 3Applied machine learning for search engine relevance 3
Applied machine learning for search engine relevance 3
 
Cs221 lecture4-fall11
Cs221 lecture4-fall11Cs221 lecture4-fall11
Cs221 lecture4-fall11
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
The Road To Monad Transformers
The Road To Monad TransformersThe Road To Monad Transformers
The Road To Monad Transformers
 
The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 14 of 84
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
 
The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.10 book - Part 33 of 212The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.10 book - Part 33 of 212
 

Dernier

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Dernier (20)

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

하스켈 프로그래밍 입문 3

  • 1. 하스켈 프로그래밍 입문 3 하스켈 학교 2016년 5월 28일 서광열
  • 3. Term data Term = Con Int | Div Term Term
  • 4. 변종0: Basic Evaluator eval :: Term -> Int eval (Con a) = a eval (Div t u) = eval t `div` eval u
  • 5. 변종1: 예외 처리 data M a = Raise Exception | Return a type Exception = String
  • 6. 변종1: 예외 처리 eval :: Term -> M Int eval (Con a) = Return a eval (Div t u) = case eval t of Raise e -> Raise e Return a -> case eval u of Raise e -> Raise e Return b -> if b == 0 then Raise “divide by zero” else Return (a `div` b)
  • 7. 변종2: division 카운터 type M a = State -> (a, State) type State = Int
  • 8. 변종2: division 카운터 eval :: Term -> M Int eval (Con a) x = (a, x) eval (Div t u) x = let (a, y) = eval t x in let (b, z) = eval u y in (a `div` b, z + 1)
  • 9. 변종3: 실행 과정 출력 type M a = (Output, a) type Output = String
  • 10. 변종3: 실행 과정 출력 eval :: Term -> M Int eval @term(Con a) = (line term a, a) eval @term(Div t u) = let (x, a) = eval t in let (y, b) = eval u in (x ++ y ++ line term (a `div` b), a `div` b) line :: Term -> Int -> Output line t a = “eval (“ ++ show t ++ “) <-” ++ show a ++ “n”
  • 11. Monadic Evaluator eval :: (Moand m) => Term -> m Int eval (Con a) = return a eval (Div t u) = do a <- eval t b <- eval u return (a `div` b)
  • 12. Monadic Evaluator eval :: (Moand m) => Term -> m Int eval (Con a) = return a eval (Div t u) = do a <- eval t b <- eval u return (a `div` b)
  • 13. 변종0: Basic Evaluator newtype M a = I a instance Monad M where return a = I a (I a) >>= k = k a
  • 14. 변종1: 예외 처리 data M a = Raise Exception | Return a type Exception = String instance Monad M where return a = Return a m >>= k = case m of Raise e -> Raise e Return a -> k a raise :: Exception -> M a raise e = Raise e
  • 15. 변종1: 예외 처리 eval :: Term -> M Int eval (Con a) = return a eval (Div t u) = do a <- eval t b <- eval u if b == 0 then raise “divide by zero” else return (a `div` b)
  • 16. 변종2: division 카운터 type M a = State -> (a, State) type State = Int instance Monad M where return a = x -> (a, x) m >>= k = x -> let (a, y) = m x in let (b, z) = k a y in (b, z) tick :: M () tick = x -> ((), x + 1)
  • 17. 변종2: division 카운터 eval :: Term -> M Int eval (Con a) = return a eval (Div t u) = do a <- eval t b <- eval u tick return (a `div` b)
  • 18. 변종3: 실행 과정 출력 type M a = (Output, a) type Output = String instance Monad m where return a = (“”, a) m >>= k = let (x, a) = m in let (y, b) = k a in (x ++ y, b) out :: Output -> M () out x = (x, ())
  • 19. 변종3: 실행 과정 출력 eval :: Term -> M Int eval term@(Con a) = do out (line term a) return a eval term@(Div t u) = do a <- eval t b <- eval u out (line term (a `div` b)) return (a `div` b)
  • 20. Control.Monad • mapM :: Monad m => (a -> m b) -> [a] -> m [b] • forM :: Monad m => [a] -> (a -> m b) -> m [b] • sequence :: Monad m => [m a] -> m [a] Note: [t] generalizes to Traversable t
  • 23. Parser newtype Parser a = Parser (String -> [a, String]) • empty 리스트는 실패, 아니면 성공 • (a, s)에서 a는 파싱한 결과 s는 파싱하고 남은 문자 열 • 리스트를 리턴하여 ambiguous grammar 처리
  • 24. item • 문자열이 empty가 아니면 첫 번째 문자를 리턴 • empty이면 에러 item :: Parser Char item = Parser (cs -> case cs of "" -> [] (c:cs) -> [(c, cs)])
  • 25. 모나드 정의 class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b instance Monad Parser where return a = (Parser (cs -> [(a, cs)]) p >>= f = (Parser (cs -> concat [parse (f a) cs’ | (a, cs’) <- parse p cs]) parse (Parser p) = p
  • 26. 모나드 법칙 1. return a >> f = f a 2. p >>= return = p 3. p >>= (a -> (f a) >>= g)) = (p >>= (a -> f a)) >>= g
  • 27. Do 표기법 p1 >>= a1 -> p2 >>= a2 -> … pn >>= an -> f a1 a2 … an do a1 <- p1 a2 <- p2 … an <- pn f a1 a2 … an
  • 28. Do 표기법 사용 예제 p :: Parser (Char, Char) p = do c <- item item d <- item return (c, d)
  • 29. MonadZero, MonadPlus class Monad m => MonadZero m where zero :: m a class MonadZero m => MonadPlus m where (++) :: m a -> m a -> m a
  • 30. Choice Operator instance MonadZero Parser where zero = Parser (cs -> []) instance MonadPlus Parser where p ++ q = (Parser (cs -> parse p cs ++ parse q cs))
  • 31. 법칙 • zero ++ p = p • p ++ zero = p • p ++ (q ++ r) = (p ++ q) ++ r • zero >>= f = zero • p >>= const zero = zero • (p ++ q) >>= f = (p >>= f) ++ (q >>= f) • p >>= (a -> f a ++ g a) = (p >>= f) ++ (q >>= g)
  • 32. Deterministic Choice Operator (+++) :: Parser a -> Parser a -> Parser a p +++ q = Parser (cs -> case parse (p ++ q) of [] -> [] (x:xs) -> [x])
  • 33. Conditional Parsing sat :: (Char -> Bool) -> Parser Char sat p = do c <- item if p c then return c else zero char :: Char -> Parser Char char c = sat (c ==)
  • 34. 재귀 Combinator string :: String -> Parser String string "" = return "" string (c:cs) = do char c string cs return (c:cs)
  • 35. 재귀 Combinator many :: Parser a -> Parser [a] many p = many1 p +++ return [] many1 :: Parser a -> Parser [a] many1 p = do a <- p as <- many p return (a:as)
  • 36. 재귀 Combinator sepby :: Parser a -> Parser b -> Parser [a] p `sepby` q = (p `sepby1` q) +++ return [] sepby1 :: Parser a -> Parser b -> Parser [a] p `sepby1` q = do a <- p as <- many (do {sep; p}) return (a:as)
  • 37. 재귀 Combinator chainl :: Parser a -> Parser (a -> a -> a) -> a -> Parser a chainl p op a = (p `chainl1` op) +++ return a chainl1 :: Parser a -> (Parser a -> a -> a) -> Parser a p `chainl1` op = do { a <- p; rest a } where rest a = (do f <- op b <- p return f a b) +++ return a
  • 38. Lexical Combinators space :: Parser String space = many (sat isSpace) token :: Parser a -> Parser a token p = do a <- p space return a
  • 39. Lexical Combinators symb :: String -> Parser String symb cs = token (string cs) apply :: Parser a -> String -> [(a, String)] apply p = parse (do {space; p})
  • 40. Expression Grammar • expr ::= expr addop term | term • term ::= term mulop factor | factor • factor ::= digit | (expr) • digit ::= 0 | 1 | … | 9 • addop ::= + | - • mulop ::= * | /
  • 41. Expression Parser expr :: Parser Int addop :: Parser (Int -> Int -> Int) mulop :: Parser (Int -> Int -> Int) expr = term `chainl1` addop term = factor `chainl1` mulop factor = digit +++ do { symb "("; n <- expr; symb ")"; return n } digit = do { x <- token (sat isDigit); return (ord x - ord ‘0’)} addop = do { symb "+"; return (+)} +++ do {symb "-"; return (-)} mulop = do { symb "*"; return (*)} +++ do {symb "/"; return (div)}
  • 42. Expression Parser > apply expr " 1 - 2 * 3 + 4 " [(-1, "")]
  • 45. Tree data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show)
  • 47. Tree 업데이트 changeToP :: Tree Char -> Tree Char changeToP (Node x l (Node y (Node _ m n) r)) = Node x l (Node y (Node 'P' m n) r)
  • 48. Tree 업데이트 data Direction = L | R deriving (Show) type Directions = [Direction] changeToP :: Directions -> Tree Char -> Tree Char changeToP (L:ds) (Node x l r) = Node x (changeToP ds l) r changeToP (R:ds) (Node x l r) = Node x l (changeToP ds r) changeToP [] (Node _ l r) = Node 'P' l r
  • 49. element elemAt :: Directions -> Tree a -> a elemAt (L:ds) (Node _ l _) = elemAt ds l elemAt (R:ds) (Node _ _ r) = elemAt ds r elemAt [] (Node x _ _) = x
  • 50. 예제 > let newTree = changeToP [R,L] freeTree > elemAt [R,L] newTree 'P'
  • 51. Breadcrumbs type Breadcrumbs = [Direction] goLeft :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs) goLeft (Node _ l _, bs) = (l, L:bs) goRight :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs) goRight (Node _ _ r, bs) = (r, R:bs)
  • 52. Breadcrumbs > goLeft (goRight (freeTree, [])) (Node 'W' (Node 'C' Empty Empty) (Node 'R' Empty Empty),[L,R])
  • 53. Zipper 정의 data Crumb a = LeftCrumb a (Tree a) | RightCrumb a (Tree a) deriving (Show) type Breadcrumbs a = [Crumb a]
  • 54. Zipper 함수 goLeft :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a) goLeft (Node x l r, bs) = (l, LeftCrumb x r:bs) goRight :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a) goRight (Node x l r, bs) = (r, RightCrumb x l:bs)
  • 55. Zipper 함수 goUp :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a) goUp (t, LeftCrumb x r:bs) = (Node x t r, bs) goUp (t, RightCrumb x l:bs) = (Node x l t, bs)
  • 56. Zipper 정의 type Zipper a = (Tree a, Breadcrumbs a)
  • 57. Zipper 함수 modify :: (a -> a) -> Zipper a -> Zipper a modify f (Node x l r, bs) = (Node (f x) l r, bs) modify f (Empty, bs) = (Empty, bs)
  • 58. Zipper 함수 topMost :: Zipper a -> Zipper a topMost (t,[]) = (t,[]) topMost z = topMost (goUp z)
  • 59. Zipper 사용 예 > let newFocus = modify (_ -> 'P') (goRight (goLeft (freeTree,[]))) > let newFocus2 = modify (_ -> 'X') (goUp newFocus)
  • 60. 리스트 data List a = Empty | Cons a (List a) deriving (Show, Read, Eq, Ord)
  • 61. 리스트 Zipper의 정의 type ListZipper a = ([a],[a])
  • 62. 리스트 Zipper 함수 goForward :: ListZipper a -> ListZipper a goForward (x:xs, bs) = (xs, x:bs) goBack :: ListZipper a -> ListZipper a goBack (xs, b:bs) = (b:xs, bs)
  • 63. 리스트 Zipper 사용 예 > let xs = [1,2,3,4] > goForward (xs,[]) ([2,3,4],[1]) > goForward ([2,3,4],[1]) ([3,4],[2,1]) > goForward ([3,4],[2,1]) ([4],[3,2,1]) > goBack ([4],[3,2,1]) ([3,4],[2,1])
  • 64. 참고 자료 1. Monads for functional programming • http://homepages.inf.ed.ac.uk/wadler/papers/markto berdorf/baastad.pdf 2. Monadic Parsing in Haskell • http://www.cs.nott.ac.uk/~pszgmh/pearl.pdf 3. Learn You a Haskell for Great Good! • http://learnyouahaskell.com/zippers