2011-04-13 34 views
0
module REPL(REPL(..), repl) where 
import qualified Control.Exception as E 
import System.Console.Readline(readline, addHistory) 

data REPL s = REPL { 
    repl_init :: IO (String, s),  -- prompt and initial state 
    repl_eval :: s -> String -> IO (Bool, s),  -- quit flag and new state 
    repl_exit :: s -> IO() 
    } 

repl :: REPL s -> IO() 
repl p = do 
    (prompt, state) <- repl_init p 
    let loop s = (do 
     mline <- readline prompt 
     case mline of 
     Nothing -> loop s 
     Just line -> do 
      (quit, s') <- repl_eval p s line 
      if quit then 
      repl_exit p s' 
      else do 
      addHistory line 
      loop s' 
     ) E.catch undefined (\(e :: E.SomeException) -> putStrLn "Handled exception!" 
     ) 
    loop state 

輸出:怎麼辦時加載錯誤無法比擬預期的類型`IO(也許字符串)」

REPL.hs:21:5: 
    Couldn't match expected type `IO (Maybe String)' 
      against inferred type `t -> Maybe String' 
    In a stmt of a 'do' expression: mline <- readline prompt 
    In the expression: 
     (do { mline <- readline prompt; 
       case mline of { 
       Nothing -> loop s 
       Just line 
        -> do { (quit, s') <- repl_eval p s line; 
          .... } } }) 
      E.catch 
      undefined 
      (\ (e :: E.SomeException) -> putStrLn "Handled exception!") 
    In the definition of `loop': 
     loop s = (do { mline <- readline prompt; 
         case mline of { 
         Nothing -> loop s 
         Just line -> do { ... } } }) 
        E.catch 
        undefined 
        (\ (e :: E.SomeException) -> putStrLn "Handled exception!") 
+1

我認爲E.catch應該在反引號之內,並且在它必須去之後還有'undefined'。之後,它爲我工作。 – yatima2975 2011-04-13 12:30:19

回答

2

由於以前的問題,你已經去掉了反引號,`,在你的E.catch聲明。我對這個問題的回答沒有使用反引號,因爲我沒有調用catch中綴。您可以使用捕捉修復後無反引號:

let loop s = E.catch (do ...) (\(e :: E.SomeException) -> ...) 

,或者您可以使用捕捉綴與反引號:

let loop s = (do ...) `E.catch` (\(e :: E.SomeException) -> ...) 

此外,undefined被我只是強迫一個例外的例子的緣故在GHCi REPL上 - 它代替了代碼中的(do ..)聲明。

+0

今晚我會試試,稍後再回復。 – Jo0o0 2011-04-14 02:21:06

+0

優秀!!!它現在可以運行!萬分感謝 – Jo0o0 2011-04-14 11:38:37

+0

很高興聽到它! – 2011-04-14 14:40:14

相關問題