Haskell programming error in sat function

I'm in chapter 8 of Graham Hatton's program at Haskell, and I copy the code and test it in the GHC.

See the slides here: http://www.cis.syr.edu/~sueo/cis352/chapter8.pdf in particular slide 15

Relevant code that I have copied so far:

type Parser a = String -> [(a, String)]
pih_return :: a -> Parser a
pih_return v = \inp -> [(v, inp)]
failure :: Parser a
failure = \inp -> []
item :: Parser Char
item = \inp -> case inp of
                    [] -> []
        (x:xs) -> [(x,xs)]
parse :: Parser a -> String -> [(a, String)]
parse p inp = p inp
sat :: (Char -> Bool) -> Parser Char
sat p = do x <- item
           if p x then pih_return x else failure

I changed the name of the function returnfrom the book to pih_returnso that it does not collide with the Prelude function return.

Errors are in the last function sat. I copied this directly from the book.

As you can see, it pis a function from Charto Bool(for example, isDigit) and xhas a type [(Char, String)], so the first error.

pih_return v [(v, inp)], inp String. sat, v x, Char.

, inp sat

sat :: (Char -> Bool) -> Parser Char
sat p inp = do x <- item inp
               if p (fst x) then pih_return (fst x) inp else failure inp

?

+5
4

sat , Parser , do. , newtype.

, , , , , Parser (sat) .

- , Parser .

sat Parser, - ( item) ( , sat do Parser, - ? t213 > ). , sat: pih_return (fst x) inp, , pih_return (fst x) (snd x).

+5

do , , data newtype, data newtype, . , , .

, newtype Parser. , instance, do -notation, return fail.

:

module P
where

newtype Parser a = Parser (String -> [(a, String)])
instance Monad Parser where
  return a = Parser $ \inp -> [(a, inp)]
  fail _   = Parser $ \_   -> []
  Parser f >>= k = Parser $ \inp -> 
     [(b, inp'') | (a, inp') <- f inp, let Parser g = k a, (b, inp'') <- g inp']

item :: Parser Char
item = Parser $ \inp -> case inp of
                          [] -> []
                          (x:xs) -> [(x,xs)]
parse :: Parser a -> String -> [(a, String)]
parse (Parser p) inp = p inp
sat :: (Char -> Bool) -> Parser Char
sat p = do x <- item
           if p x then return x else fail "predicate not satisfied"
+2

Monad Parser.

do ; x <- item [(Char, String)] (Char, String).

, , , :

instance Monad (Parser a) where
  return x = pih_return x
  -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
  p >>= f = \inp -> case p inf of
                      [] -> []
                      (x:xs) -> (f x) xs -- this line is probably wrong
  fail message = [] -- message is ignored
+1
source

I read the same book and come to the same problem when trying to do the exercises. I refused to use the notation "do ..." for → =. For everyone concerned, I attached my code before using the nat function. I added all my functions with a and changed → = to →> = to avoid name clashes with the prelude.

type AParser a = String -> [(a, String)]
areturn :: a -> AParser a
areturn v = \inp -> [(v, inp)]
afailure :: AParser a
afailure = \inp -> []
aitem :: AParser Char
aitem = \inp -> case inp of
                  [] -> []
                  (x:xs) -> [(x, xs)]
aparse :: AParser a -> String -> [(a, String)]
aparse p inp = p inp
(>>>=) :: AParser a -> (a -> AParser b) -> AParser b
p >>>= f = \inp -> case aparse p inp of
                    [] -> []
                    [(v, out)] -> aparse (f v) out
(+++) :: AParser a -> AParser a -> AParser a
p +++ q = \inp -> case aparse p inp of
                    [] -> aparse q inp
                    [(v, out)] -> [(v, out)]
asat :: (Char -> Bool) -> AParser Char
asat p = aitem >>>= (\x -> if p x then areturn x else afailure)
adigit :: AParser Char
adigit = asat isDigit
amany :: AParser a -> AParser [a]
amany p = amany1 p +++ areturn []
amany1 :: AParser a -> AParser [a]
amany1 p = p >>>= (\v -> (amany p) >>>= (\vs -> areturn (v:vs)))
anat :: AParser Int
anat = amany1 adigit >>>= (\xs -> areturn (read xs))
+1
source

All Articles