<div dir="ltr">Hi Yassine,<div><br></div><div>I prefer to explain you with an abstract view of these definitions. Unrolling this stuff in your mind (or paper) can be complex and, IMO, it might be useless as it does not give you any specific hints to build even more complex ones.</div><div><br></div><div>You can view Parser a as an object with a certain structure (or form, or definition if you prefer). However, you do not want to know how complex its structure or definition is. You are certain about one thing, it holds some value of type a.</div><div>Let's say that such value is "hidden" by Parser. The same idea applies to Maybe a.</div><div><br></div><div>Then, you want to work with that value no matter the structure of Parser. As you already know, fmap allows you to do that. You can go from Parser a to Parser b with a function from a to b.<br></div><div>The applicative allows you to go further. If you have a hidden function (e.g. Parser (a->b)) and a hidden parameter (Parser a). Then you want to apply that hidden function to the hidden parameter in order to obtain a Parser b.</div><div>That is what the expression parserF <*> ParserA would do if parserF hides a function and parserA its parameter.</div><div><br></div><div>Now, you need to know more about the meaning of Parser a. It is an object that reads the input and produce a result (or token) accordingly.</div><div>The returned value of the parser is a list of (result, remaining_input).</div><div>The interesting part is the result value, which is of type a. You want to play around with it. Again, fmap is an easy way. Going from Parser a to Parser b via fmap does not change the parsing action of Parser a, you will have a Parser b but the behavior remains the same. You just played with the result of the first parser.<br></div><div><br></div><div>The functor instance tells that more precisely (fixed):</div><div><br></div><div><span style="font-size:12.8px">instance Functor Parser where</span><br style="font-size:12.8px"><span style="font-size:12.8px">fmap g (P p) = P (\inp -> case p inp of</span><br style="font-size:12.8px"><span style="font-size:12.8px">[] -> []</span><br style="font-size:12.8px"><span style="font-size:12.8px"> [(v, out)] -> [(g v, out)])</span><br></div><div><br></div><div>Now look at the definition of the applicative</div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">instance Applicative Parser where</span><br style="font-size:12.8px"><span style="font-size:12.8px">pure v = P (\inp -> [(v, inp)])</span><br style="font-size:12.8px"><span style="font-size:12.8px">pg <*> px = P (\inp -> case parse pg inp of</span><br style="font-size:12.8px"><span style="font-size:12.8px">[] -> []</span><br style="font-size:12.8px"><span style="font-size:12.8px">[(g, out)] -> parse (fmap g px) out)</span><br></div><div><br></div><div>First of all, the function "parser" applies the parser, i.e., it parses the input and returns the list [(g, out)]. Here, we have two applications of "parser".</div><div>The first one applies parser pg, "case parse pg inp". Obviously, pg hides a function "g".</div><div>Now you preserve the same behavior of px but you fmap on it function "g". That is, the parser "fmap g px" will do the same as px but its result is changed by g.</div><div>And finally, you apply such parser.</div><div><br></div><div>Let us take the first two terms of your example</div><div><br></div><div>first_item = pure (\x y -> (x,y)) <*> item<br></div><div><br></div><div>then g = \x y -> (x,y)</div><div>which is a higher order function. In the expression, g is applied partially over the result of item. You know that item returns the first char of the input, 'a'.</div><div>first_item is then a parser that hides a function of the form h = \y -> ('a', y).</div><div><br></div><div>Because it hides a function, you can use the applicative again</div><div><br></div><div>first_term <*> item</div><div><br></div><div>and h will be applied to the result of item again. Because we already applied item once, the remaining input is "bc". Then, item will give you 'b' as a result.</div><div>Now h 'b' = ('a, 'b'), which is the result of your final parser plus the remaining input "c". Applying the final parser, you obtain</div><div><br></div><div>[('a', 'b'), "c")]</div><div><br></div><div>I hope this will help you !</div><div><br></div><div>Best,</div><div>Ivan</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-08-03 21:19 GMT+02:00 Yassine <span dir="ltr"><<a href="mailto:yassine912@gmail.com" target="_blank">yassine912@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I have a question about functor applicate.<br>
<br>
I know that:<br>
pure (+1) <*> Just 2<br>
<br>
<br>
produce: Just 3<br>
because pure (+1) produce Just (+1) and then Just (+1) <*> Just 2<br>
produce Just (2+1)<br>
<br>
<br>
but in more complex case like:<br>
newtype Parser a = P (String -> [(a,String)])<br>
<br>
parse :: Parser a -> String -> [(a,String)]<br>
parse (P p) inp = p inp<br>
<br>
<br>
item :: Parser Char<br>
item = P (\inp -> case inp of<br>
[] -> []<br>
(x:xs) -> [(x,xs)])<br>
<br>
instance Functor Parser where<br>
fmap g p = P (\inp -> case p inp of<br>
[] -> []<br>
[(v, out)] -> [(g v, out)])<br>
<br>
instance Applicative Parser where<br>
pure v = P (\inp -> [(v, inp)])<br>
pg <*> px = P (\inp -> case parse pg inp of<br>
[] -> []<br>
[(g, out)] -> parse (fmap g px) out)<br>
<br>
<br>
When I do:<br>
parse (pure (\x y -> (x,y)) <*> item <*> item) "abc"<br>
<br>
The answer is:<br>
[(('a','b'),"c")]<br>
<br>
But I don't understand what exactly happens.<br>
First:<br>
pure (\x y -> (x,y)) => P (\inp -> [(\x y -> (x,y), inp)])<br>
<br>
Then:<br>
P (\inp -> [(\x y -> (x,y), inp)]) <*> item => ???<br>
<br>
Can someone explain what's happens step by step please.<br>
<br>
Thank you.<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>