Could not get parser ready

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Could not get parser ready

Marcus Manning
Hello,

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:

p :: Parser (Char,Char)
p = do
              x ← item
              item
              y ← item
              return (x,y)


ghci and ghc throw errors:
Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item;
return (x,y)}

<interactive>:10:65: error:
     • Couldn't match type ‘[(Char, String)]’ with ‘Char’
       Expected type: String -> [((Char, Char), String)]
         Actual type: Parser ([(Char, String)], [(Char, String)])
     • In a stmt of a 'do' block: return (x, y)
       In the expression:
         do x <- item
            item
            y <- item
            return (x, y)
       In an equation for ‘p’:
           p = do x <- item
                  item
                  y <- item
                  ....
Did the semantics of do expr changed?

[1]
https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf

Cheers,

iconfly.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Could not get parser ready

Francesco Ariis
On Sun, Nov 05, 2017 at 06:51:57PM +0100, Marcus Manning wrote:

> Hello,
>
> 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:
>
> p :: Parser (Char,Char)
> p = do
>              x ← item
>              item
>              y ← item
>              return (x,y)
>
>
> ghci and ghc throw errors:
> Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item;
> return (x,y)}

Hello Marcus,
    what is the :type of `item`?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Could not get parser ready

Tobias Brandt-3
In reply to this post by Marcus Manning

Hey,

can you show us your Parser definition? 

Cheers,
Tobias 

----- Nachricht von Marcus Manning <[hidden email]> ---------
     Datum: Sun, 5 Nov 2017 18:51:57 +0100
       Von: Marcus Manning <[hidden email]>
Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <[hidden email]>
   Betreff: [Haskell-beginners] Could not get parser ready
        An: [hidden email]

Hello,

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:

p :: Parser (Char,Char)
p = do
             x ← item
             item
             y ← item
             return (x,y)


ghci and ghc throw errors:
Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item; return (x,y)}

<interactive>:10:65: error:
    • Couldn't match type ‘[(Char, String)]’ with ‘Char’
      Expected type: String -> [((Char, Char), String)]
        Actual type: Parser ([(Char, String)], [(Char, String)])
    • In a stmt of a 'do' block: return (x, y)
      In the expression:
        do x <- item
           item
           y <- item
           return (x, y)
      In an equation for ‘p’:
          p = do x <- item
                 item
                 y <- item
                 ....
Did the semantics of do expr changed?

[1] https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf

Cheers,

iconfly.
_______________________________________________
Beginners mailing list
[hidden email].<a href="orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




----- Ende der Nachricht von Marcus Manning <[hidden email]> -----


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Could not get parser ready

Marcus Manning
type Parser a = String → [(a,String)]

item :: Parser Char 
item = λinp → case inp of 
                            [] → [] 
                            (x:xs) → [(x,xs)]
failure :: Parser a 
failure = λinp → []

return :: a → Parser a 
return v = λinp → [(v,inp)]

(+++) :: Parser a → Parser a → Parser a 
p +++ q = λinp → case p inp of 
                                 [] → q inp 
                                 [(v,out)] → [(v,out)]

parse :: Parser a → String → [(a,String)]
parse p inp = p inp

p :: Parser (Char,Char)
p = do x ← item 
           item 
           y ← item 
           return (x,y)

I assume the bind operator (==>) was overwritten by

(>>=) :: Parser a → (a → Parser b) → Parser b p 
>>= f = λinp → case parse p inp of 
                             [ ] → [ ] 
                             [ (v, out) ] → parse (f v) out

in order to manipulate the do expr to make the p function work, right?

2017-11-05 21:56 GMT+01:00 Tobias Brandt <[hidden email]>:

Hey,

can you show us your Parser definition? 

Cheers,
Tobias 

----- Nachricht von Marcus Manning <[hidden email]> ---------
     Datum: Sun, 5 Nov 2017 18:51:57 +0100
       Von: Marcus Manning <[hidden email]>
Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <[hidden email]>
   Betreff: [Haskell-beginners] Could not get parser ready
        An: [hidden email]

Hello,

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:

p :: Parser (Char,Char)
p = do
             x ← item
             item
             y ← item
             return (x,y)


ghci and ghc throw errors:
Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item; return (x,y)}

<interactive>:10:65: error:
    • Couldn't match type ‘[(Char, String)]’ with ‘Char’
      Expected type: String -> [((Char, Char), String)]
        Actual type: Parser ([(Char, String)], [(Char, String)])
    • In a stmt of a 'do' block: return (x, y)
      In the expression:
        do x <- item
           item
           y <- item
           return (x, y)
      In an equation for ‘p’:
          p = do x <- item
                 item
                 y <- item
                 ....
Did the semantics of do expr changed?

[1] https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf

Cheers,

iconfly.
_______________________________________________
Beginners mailing list
[hidden email].orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




----- Ende der Nachricht von Marcus Manning <[hidden email]> -----


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Could not get parser ready

David McBride
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.

You can either a) write your own bind that conforms to your type.

bind :: Parser a -> (a -> Parser b) -> Parser b
bind = undefined

p' :: Parser (Char, Char)
p' = item `bind` \x -> item `bind` \_ -> item `bind` \y -> Foo.return (x, y)

Or you can use the Monad return instead.  But the type is not what you expect.

p :: String -> ([(Char, String)], [(Char, String)])
p = do x <- item
       item
       y <- item
       Prelude.return (x,y)





On Mon, Nov 6, 2017 at 3:15 AM, Marcus Manning <[hidden email]> wrote:
type Parser a = String → [(a,String)]

item :: Parser Char 
item = λinp → case inp of 
                            [] → [] 
                            (x:xs) → [(x,xs)]
failure :: Parser a 
failure = λinp → []

return :: a → Parser a 
return v = λinp → [(v,inp)]

(+++) :: Parser a → Parser a → Parser a 
p +++ q = λinp → case p inp of 
                                 [] → q inp 
                                 [(v,out)] → [(v,out)]

parse :: Parser a → String → [(a,String)]
parse p inp = p inp

p :: Parser (Char,Char)
p = do x ← item 
           item 
           y ← item 
           return (x,y)

I assume the bind operator (==>) was overwritten by

(>>=) :: Parser a → (a → Parser b) → Parser b p 
>>= f = λinp → case parse p inp of 
                             [ ] → [ ] 
                             [ (v, out) ] → parse (f v) out

in order to manipulate the do expr to make the p function work, right?

2017-11-05 21:56 GMT+01:00 Tobias Brandt <[hidden email]>:

Hey,

can you show us your Parser definition? 

Cheers,
Tobias 

----- Nachricht von Marcus Manning <[hidden email]> ---------
     Datum: Sun, 5 Nov 2017 18:51:57 +0100
       Von: Marcus Manning <[hidden email]>
Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <[hidden email]>
   Betreff: [Haskell-beginners] Could not get parser ready
        An: [hidden email]

Hello,

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:

p :: Parser (Char,Char)
p = do
             x ← item
             item
             y ← item
             return (x,y)


ghci and ghc throw errors:
Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item; return (x,y)}

<interactive>:10:65: error:
    • Couldn't match type ‘[(Char, String)]’ with ‘Char’
      Expected type: String -> [((Char, Char), String)]
        Actual type: Parser ([(Char, String)], [(Char, String)])
    • In a stmt of a 'do' block: return (x, y)
      In the expression:
        do x <- item
           item
           y <- item
           return (x, y)
      In an equation for ‘p’:
          p = do x <- item
                 item
                 y <- item
                 ....
Did the semantics of do expr changed?

[1] https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf

Cheers,

iconfly.
_______________________________________________
Beginners mailing list
[hidden email].orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




----- Ende der Nachricht von Marcus Manning <[hidden email]> -----


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Could not get parser ready

Marcus Manning
p' :: Parser (Char, Char)
p' = item `bind` \x -> item `bind` \_ -> item `bind` \y -> return (x, y)

with 

bind:: Parser a -> (a -> Parser b) -> Parser b
bind p f = \ inp -> case parse p inp of 
                                [] -> []
                                [(v,out)] -> parse (f v) out

works like a charm.

Another alternatiev would to change the Parser definition to the one given in [1].

Thanks.

Cheers,

iconfly

[1] http://dev.stephendiehl.com/fun/002_parsers.html


2017-11-06 14:29 GMT+01:00 David McBride <[hidden email]>:
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.

You can either a) write your own bind that conforms to your type.

bind :: Parser a -> (a -> Parser b) -> Parser b
bind = undefined

p' :: Parser (Char, Char)
p' = item `bind` \x -> item `bind` \_ -> item `bind` \y -> Foo.return (x, y)

Or you can use the Monad return instead.  But the type is not what you expect.

p :: String -> ([(Char, String)], [(Char, String)])
p = do x <- item
       item
       y <- item
       Prelude.return (x,y)





On Mon, Nov 6, 2017 at 3:15 AM, Marcus Manning <[hidden email]> wrote:
type Parser a = String → [(a,String)]

item :: Parser Char 
item = λinp → case inp of 
                            [] → [] 
                            (x:xs) → [(x,xs)]
failure :: Parser a 
failure = λinp → []

return :: a → Parser a 
return v = λinp → [(v,inp)]

(+++) :: Parser a → Parser a → Parser a 
p +++ q = λinp → case p inp of 
                                 [] → q inp 
                                 [(v,out)] → [(v,out)]

parse :: Parser a → String → [(a,String)]
parse p inp = p inp

p :: Parser (Char,Char)
p = do x ← item 
           item 
           y ← item 
           return (x,y)

I assume the bind operator (==>) was overwritten by

(>>=) :: Parser a → (a → Parser b) → Parser b p 
>>= f = λinp → case parse p inp of 
                             [ ] → [ ] 
                             [ (v, out) ] → parse (f v) out

in order to manipulate the do expr to make the p function work, right?

2017-11-05 21:56 GMT+01:00 Tobias Brandt <[hidden email]>:

Hey,

can you show us your Parser definition? 

Cheers,
Tobias 

----- Nachricht von Marcus Manning <[hidden email]> ---------
     Datum: Sun, 5 Nov 2017 18:51:57 +0100
       Von: Marcus Manning <[hidden email]>
Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <[hidden email]>
   Betreff: [Haskell-beginners] Could not get parser ready
        An: [hidden email]

Hello,

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:

p :: Parser (Char,Char)
p = do
             x ← item
             item
             y ← item
             return (x,y)


ghci and ghc throw errors:
Prelude> let p:: Parser (Char,Char); p = do {x <- item; item; y <- item; return (x,y)}

<interactive>:10:65: error:
    • Couldn't match type ‘[(Char, String)]’ with ‘Char’
      Expected type: String -> [((Char, Char), String)]
        Actual type: Parser ([(Char, String)], [(Char, String)])
    • In a stmt of a 'do' block: return (x, y)
      In the expression:
        do x <- item
           item
           y <- item
           return (x, y)
      In an equation for ‘p’:
          p = do x <- item
                 item
                 y <- item
                 ....
Did the semantics of do expr changed?

[1] https://userpages.uni-koblenz.de/~laemmel/paradigms1011/resources/pdf/haskell.pdf

Cheers,

iconfly.
_______________________________________________
Beginners mailing list
[hidden email].orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




----- Ende der Nachricht von Marcus Manning <[hidden email]> -----


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners