2011-12-14 76 views
2

我正在嘗試編譯正則表達式並獲取可呈現給用戶的錯誤消息。我試圖與Text.Regex.TDFA和Text.Regex.Posix,似乎有同樣的表現:從編譯正則表達式中獲取錯誤說明

Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> fromLeft $ (makeRegexM ".[" :: Either String Regex) 
"*** Exception: parseRegex for Text.Regex.TDFA.String failed:".[" (line 1, column 3): 
unexpected end of input 
expecting "^", "]", "-" or Failed to parse bracketed string 
Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> isJust $ (makeRegexM ".[" :: Maybe Regex) 
False 
Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> isJust $ (makeRegexM "." :: Maybe Regex) 
True 

的也許單子似乎工作;這兩者都沒有。然而文檔說,它應該使用'失敗' - 據我所知,它是在任一monad中定義的。難道我做錯了什麼?

+2

什麼是正則表達式? – Toto 2011-12-14 14:24:29

回答

2

原因可能是最近更改了Either e的monad實例。在mtl-1.*,曾經有一個

instance Error e => Monad (Either e) where 
    ... 
    fail msg = Left (strMsg msg) -- I may misremember the exact names 

所以調用fail有沒有造成異常。現在,有在基地(Control.Monad.Instances)一個單子實例

instance Monad (Either e) where 
    ... 
    fail s = error s -- implicitly from the default method for fail 

讓您得到上面。

+0

我想過了。你知道有一種替代的「標準」monad,這個monad有正確定義(爲此目的),還是我必須建立自己的? – ondra 2011-12-14 14:35:07