how to print intermediate result in Functor and Applicative in Haskell

前端 未结 2 819
灰色年华
灰色年华 2021-01-07 03:34

I am reading the book Programming in Haskell 2nd by Graham Hutton. https://www.cs.nott.ac.uk/~pszgmh/pih.html#slides

When it comes to chapter 13.4 Sequencing parser

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 04:15

    It's a good start. Your last step doesn't look quite right. Stripping off the shared parts, you've written that

    \inp -> case (\inp -> [(g,inp)]) inp of [] -> []; [(g, out)] -> parse (fmap g item) out
    =
                                                                    parse (fmap g item) out
    

    This equation doesn't look quite right to me: the former is a function, and the latter isn't. Additionally, the latter refers to the free variable out, while the former doesn't (because out is bound by the pattern match it's enclosed in). A more careful continuation looks like this:

    \inp -> case (\inp -> [(g,inp)]) inp of [] -> []; [(g, out)] -> parse (fmap g item) out
    = { beta reduction, substituting inp for inp }
    \inp -> case [(g, inp)]              of [] -> []; [(g, out)] -> parse (fmap g item) out
    = { case reduction, substituting g for g and inp for out }
    \inp ->                                                         parse (fmap g item) inp
    

    If you like, you could then eta-reduce this to parse (fmap g item). Plugging this back into the shared parts we dropped above, we have:

    three
    =
    P (parse (fmap g item)) <*> item <*> item
    

    The result can be verified in ghci:

    *Parsing> parse three "abc"
    [(('a','c'),"")]
    
    *Parsing> let g x y z = (x,z)
    
    *Parsing> parse (P (parse (fmap g item)) <*> item <*> item) "abc"
    [(('a','c'),"")]
    
    

    As next steps, there are three places you could do your next definition-expansion to enable further progress:

    1. You could expand the fmap in fmap g item.
    2. You could expand the inner (<*>) in P (...) <*> item.
    3. You could expand the outer (<*>) in (P (...) <*> item) <*> item.

    Good luck!

提交回复
热议问题