开发者

need help with haskell expression (Not in scope compiler error)

Hi I have the following code

expression :: String →  Maybe Expr
expression s =   case parse expr s' of
      Just (a,"") →  Just a
      _           →  Nothing
    where s' = filter (λx →  x ≠ ' ') s

expr, term, factor, num, sin', cos' :: Parser Expr
num    = dbl' +++ int'

expr   = chain term '+' Add

ter开发者_运维知识库m   = chain factor '*' Mul



func = sin' +++ cos'

var' = do
       char 'x'
       return (Var "x")

int' = do n ←  int
          return (Num (fromIntegral n))

dbl' = do n ←  int
   char '.'
   n' ←  oneOrMore number
   let c = ((show n) ⊕ "." ⊕ n')
   return (Dbl (read c))

sin' = do char 's'
   char 'i'
          char 'n'
   e ←  factor
          return (Sin e)

And the compiler says that the following is not in scope "chain", "int" "number"

Why are the compiler complaining on these commands, isn't chain, int and number well known names?

EDIT

If you where to use the following parser, how would you solve the problem?

module Parsing
 ( Parser,parse,
  success,failure,sat,pmap,char,digit,
  (+++),(<:>),(>*>),(>->),(<-<),
  oneOrMore,zeroOrMore
 )

where

import Data.Maybe
import Data.Char

------------------



-------------------
-- Basic Parsers, dependent on internal structure --
-- success and fail
failure    = P $ \s -> Nothing
success a  = P $ \s -> Just (a,s)

-- Parse any single character
item  =  P $ \s -> case s of
               []     -> Nothing
               (c:cs) -> Just (c,cs)

-- (+++)  parse either using p or else using q
infixr 5 +++
(+++) :: Parser a -> Parser a -> Parser a

p +++ q  = P $ \s -> listToMaybe [ x | Just x <- [parse p s, parse q s]]

-- (p >*> f) parse using p to produce a.
-- Then parse using f a

infixl 1 >*>

(>*>) :: Parser a -> (a -> Parser b) -> Parser b

p >*> f  = P $ \s ->
            case parse p s of
                    Just(a,s') -> parse (f a) s'
                    _          -> Nothing

-----------------------------------------------


-- pmap modifies the result of a parser
pmap :: (a -> b) -> Parser a -> Parser b
pmap f p = p >*> success . f

p >-> q = p >*> \_ -> q  -- equivalent to monadic op: >>
p <-< q = p >*> \a -> q >-> success a


(<:>):: Parser a -> Parser [a] -> Parser [a]
p <:> q = p >*> \a -> pmap (a:) q


(What version of parsec are you using?)

There's no chain, int, or number provided by Parsec 2.x or Parsec 3.x, though these would be simple to write.

chain term op cons = sepBy1 expr (char op) >>= return . foldr1 cons
int = many1 digit >>= return . read
number = digit

(Untested, and I'm just guessing at the intent of your code.)


Cute little parser combinator library there. Is this homework or something?

punva grez bc pbaf = cznc (sbyqe1 pbaf) $ grez <:> mrebBeZber (pune bc >-> grez)
vag = cznc ernq $ barBeZber qvtvg


It seems that you are using some sort of parsing library like Parsec, or the parser module from "programming in Haskell." You need to import the one you are using.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜