<div dir="ltr">Exactly, It is left-associative and it will apply "pure g", which does nothing in terms of parsing but put g into the parsing result, and then apply item.<br><div><br></div><div>Another way to think about it, is to forget about the list and think only in terms of types.</div><div>With a value of type Parser (a -> (b, c)) and Parser a you can use <*> to get a value of type Parser (b, c).</div><div>Then, if you execute such a parser you will have a list of nested tuples as you requested, i.e. [(b, c), String].</div><div><br></div><div>Try your first example with <span style="font-size:12.8px">g = (\x -> (x,0)) and pure g <*> item. Play with different expressions and the applicative operator.</span></div><div><br></div><div>Best,</div><div>Ivan</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-08-05 0:29 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">Thanks for you nice answer but I still have some difficulties.<br>
<br>
When you do:<br>
g = (\x y -> (x,y))<br>
first_item = pure g <*> item<br>
<br>
Because <*> is left associative, we do first the application of g on<br>
item before apply item, is it correct ?<br>
<br>
Furthermore, item return a list containing a tuple with the first<br>
character and the remaining of the string. So how can I get a list<br>
with a tuple containing another tuple with the parsed characters<br>
(inner tuple) and the remaining of the string (in the outer tuple).<br>
<div class="HOEnZb"><div class="h5"><br>
2017-08-04 23:30 GMT+02:00 Ivan Llopard <<a href="mailto:ivanllopard@gmail.com">ivanllopard@gmail.com</a>>:<br>
> Hi Yassine,<br>
><br>
> I prefer to explain you with an abstract view of these definitions.<br>
> Unrolling this stuff in your mind (or paper) can be complex and, IMO, it<br>
> might be useless as it does not give you any specific hints to build even<br>
> more complex ones.<br>
><br>
> You can view Parser a as an object with a certain structure (or form, or<br>
> definition if you prefer). However, you do not want to know how complex its<br>
> structure or definition is. You are certain about one thing, it holds some<br>
> value of type a.<br>
> Let's say that such value is "hidden" by Parser. The same idea applies to<br>
> Maybe a.<br>
><br>
> Then, you want to work with that value no matter the structure of Parser. As<br>
> you already know, fmap allows you to do that. You can go from Parser a to<br>
> Parser b with a function from a to b.<br>
> The applicative allows you to go further. If you have a hidden function<br>
> (e.g. Parser (a->b)) and a hidden parameter (Parser a). Then you want to<br>
> apply that hidden function to the hidden parameter in order to obtain a<br>
> Parser b.<br>
> That is what the expression parserF <*> ParserA would do if parserF hides a<br>
> function and parserA its parameter.<br>
><br>
> Now, you need to know more about the meaning of Parser a. It is an object<br>
> that reads the input and produce a result (or token) accordingly.<br>
> The returned value of the parser is a list of (result, remaining_input).<br>
> The interesting part is the result value, which is of type a. You want to<br>
> play around with it. Again, fmap is an easy way. Going from Parser a to<br>
> Parser b via fmap does not change the parsing action of Parser a, you will<br>
> have a Parser b but the behavior remains the same. You just played with the<br>
> result of the first parser.<br>
><br>
> The functor instance tells that more precisely (fixed):<br>
><br>
> instance Functor Parser where<br>
> fmap g (P p) = P (\inp -> case p inp of<br>
> []              -> []<br>
>  [(v, out)]      -> [(g v, out)])<br>
><br>
> Now look at the definition of the applicative<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>
> First of all, the function "parser" applies the parser, i.e., it parses the<br>
> input and returns the list [(g, out)]. Here, we have two applications of<br>
> "parser".<br>
> The first one applies parser pg, "case parse pg inp". Obviously, pg hides a<br>
> function "g".<br>
> Now you preserve the same behavior of px but you fmap on it function "g".<br>
> That is, the parser "fmap g px" will do the same as px but its result is<br>
> changed by g.<br>
> And finally, you apply such parser.<br>
><br>
> Let us take the first two terms of your example<br>
><br>
> first_item = pure (\x y -> (x,y)) <*> item<br>
><br>
> then g = \x y -> (x,y)<br>
> which is a higher order function. In the expression, g is applied partially<br>
> over the result of item. You know that item returns the first char of the<br>
> input, 'a'.<br>
> first_item is then a parser that hides a function of the form h = \y -><br>
> ('a', y).<br>
><br>
> Because it hides a function, you can use the applicative again<br>
><br>
> first_term <*> item<br>
><br>
> and h will be applied to the result of item again. Because we already<br>
> applied item once, the remaining input is "bc". Then, item will give you 'b'<br>
> as a result.<br>
> Now h 'b' = ('a, 'b'), which is the result of your final parser plus the<br>
> remaining input "c". Applying the final parser, you obtain<br>
><br>
> [('a', 'b'), "c")]<br>
><br>
> I hope this will help you !<br>
><br>
> Best,<br>
> Ivan<br>
><br>
> 2017-08-03 21:19 GMT+02:00 Yassine <<a href="mailto:yassine912@gmail.com">yassine912@gmail.com</a>>:<br>
>><br>
>> 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>
><br>
><br>
><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>
><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>
</div></div></blockquote></div><br></div>