<div dir="ltr"><span style="font-size:12.8px">p' :: Parser (Char, Char)</span><br style="font-size:12.8px"><span style="font-size:12.8px">p' = item `bind` \x -> item `bind` \_ -> item `bind` \y -> return (x, y)</span><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">with </span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">bind:: Parser a -> (a -> Parser b) -> Parser b</span></div><div><span style="font-size:12.8px">bind p f = \ inp -> case parse p inp of </span></div><div><span style="font-size:12.8px">                                [] -> []</span></div><div><span style="font-size:12.8px">                                [(v,out)] -> parse (f v) out</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">works like a charm.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Another alternatiev would to change the Parser definition to the one given in [1].</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Thanks.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Cheers,</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">iconfly</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">[1] <a href="http://dev.stephendiehl.com/fun/002_parsers.html">http://dev.stephendiehl.com/fun/002_parsers.html</a></span><br><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">2017-11-06 14:29 GMT+01:00 David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div>The problem is that in p you are using do notation for bind, which uses the Monad instance for bind (>>=) for ((->) String), because Parser is a type alias for (String -> [(a, String)].  But then you are using your own return which is not of the same type as Monad's return would be.  The way you have it now your return considers the loose `a` as the Monad parameter, but do notation considers it [(a, String)] instead.<br><br></div>You can either a) write your own bind that conforms to your type.<br><br>bind :: Parser a -> (a -> Parser b) -> Parser b<br>bind = undefined<br><br>p' :: Parser (Char, Char)<br>p' = item `bind` \x -> item `bind` \_ -> item `bind` \y -> Foo.return (x, y)<br><br></div>Or you can use the Monad return instead.  But the type is not what you expect.<br><br>p :: String -> ([(Char, String)], [(Char, String)])<span class="gmail-"><br>p = do x <- item<br>       item<br>       y <- item<br></span>       Prelude.return (x,y)<br></div><div><br><br><div><div><br><br></div></div></div></div><div class="gmail-HOEnZb"><div class="gmail-h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 6, 2017 at 3:15 AM, Marcus Manning <span dir="ltr"><<a href="mailto:iconsize@gmail.com" target="_blank">iconsize@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">type Parser a = String → [(a,String)]<div><br></div><div>item :: Parser Char </div><div>item = λinp → case inp of </div><div>                            [] → [] </div><div>                            (x:xs) → [(x,xs)]</div><div>failure :: Parser a </div><div>failure = λinp → []</div><div><br></div><div>return :: a → Parser a </div><div>return v = λinp → [(v,inp)]</div><div><br></div><div>(+++) :: Parser a → Parser a → Parser a </div><div>p +++ q = λinp → case p inp of </div><div>                                 [] → q inp </div><div>                                 [(v,out)] → [(v,out)]</div><div><br></div><div>parse :: Parser a → String → [(a,String)]</div><div>parse p inp = p inp</div><span><div><br></div><div>p :: Parser (Char,Char)</div><div>p = do
x ← item </div><div>           item </div><div>           y ← item </div><div>           return (x,y)</div></span><div><div><br></div><div>It is described in pages 189-216 in [1].</div><div><br></div><div>[1] <a href="https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf" target="_blank">https://userpages.uni-kobl<wbr>enz.de/~laemmel/paradigms1011/<wbr>resources/pdf/haskell.pdf</a></div></div><div><br></div><div>I assume the bind operator (==>) was overwritten by</div><div><br></div><div>(>>=) :: Parser a → (a → Parser b) → Parser b
p </div><div>>>= f = λinp → case parse p inp of </div><div>                             [ ] → [ ] </div><div>                             [ (v, out) ] → parse (f v) out<br></div><div><br></div><div>in order to manipulate the do expr to make the p function work, right?</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="gmail-m_-1446843925754177561h5">2017-11-05 21:56 GMT+01:00 Tobias Brandt <span dir="ltr"><<a href="mailto:to_br@uni-bremen.de" target="_blank">to_br@uni-bremen.de</a>></span>:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="gmail-m_-1446843925754177561h5"><u></u>





<div style="font-family:Arial;font-size:14px">
<p>Hey,<br>
<br>
can you show us your Parser definition? <br>
<br>
Cheers,<br>
Tobias <br>
<br>
----- Nachricht von Marcus Manning <<a href="mailto:iconsize@gmail.com" target="_blank">iconsize@gmail.com</a>> ---------<br>
     Datum: Sun, 5 Nov 2017 18:51:57 +0100<br>
       Von: Marcus Manning <<a href="mailto:iconsize@gmail.com" target="_blank">iconsize@gmail.com</a>><br>
Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
   Betreff: [Haskell-beginners] Could not get parser ready<br>
        An: <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a></p>
<blockquote style="border-left:2px solid blue;margin-left:2px;padding-left:12px" type="cite">
<p></p><div><div class="gmail-m_-1446843925754177561m_-7154231356269832074h5">Hello,<br>
<br>
I follow the instructions of script [1] in order to set up a parser functionality. But I' get into problems at page 202 with the code:<br>
<br>
p :: Parser (Char,Char)<br>
p = do<br>
             x ← item<br>
             item<br>
             y ← item<br>
             return (x,y)<br>
<br>
<br>
ghci and ghc throw errors:<br>
Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item; return (x,y)}<br>
<br>
<interactive>:10:65: error:<br>
    • Couldn't match type ‘[(Char, String)]’ with ‘Char’<br>
      Expected type: String -> [((Char, Char), String)]<br>
        Actual type: Parser ([(Char, String)], [(Char, String)])<br>
    • In a stmt of a 'do' block: return (x, y)<br>
      In the expression:<br>
        do x <- item<br>
           item<br>
           y <- item<br>
           return (x, y)<br>
      In an equation for ‘p’:<br>
          p = do x <- item<br>
                 item<br>
                 y <- item<br>
                 ....<br>
Did the semantics of do expr changed?<br>
<br>
[1] <a href="https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf" target="_blank">https://userpages.uni-koblenz.<wbr>de/~laemmel/paradigms1011/reso<wbr>urces/pdf/haskell.pdf</a><br>
<br>
Cheers,<br>
<br>
iconfly.<br></div></div>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell" target="_blank">Beginners@haskell</a>.<a>orghttp://ma<wbr>il.haskell.org/cgi-bin/mailman<wbr>/listinfo/beginners</a><p></p>
</blockquote>
<p><br>
<br>
<br>
----- Ende der Nachricht von Marcus Manning <<a href="mailto:iconsize@gmail.com" target="_blank">iconsize@gmail.com</a>> -----<br>
<br></p>
</div>
<br></div></div><span>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">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-bi<wbr>n/mailman/listinfo/beginners</a><br>
<br></span></blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">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-bi<wbr>n/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</div></div><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></blockquote></div><br></div></div></div>