我對以下程序有一些疑問。LetterP的類型是什麼?
import Data.List(nub)
import qualified Text.PrettyPrint.HughesPJ as PP
import Text.PrettyPrint.HughesPJ(Doc,text,int,(<>),(<+>),($+$),render)
data Prop a =
LetterP a
| AndP (Prop a) (Prop a)
deriving Eq
class PPLetter a where
ppLetter :: a -> Doc
instance PPLetter Int where
ppLetter a = text ("p"++show a)
instance PPLetter Char where
ppLetter = PP.char
instance PPLetter a => PPLetter [a] where
ppLetter = PP.hcat . (map ppLetter)
class PP a where
pp :: a -> Doc
instance PP Bool where
pp True = text "True"
pp False = text "False"
parens n ([email protected](LetterP _)) = pp term
instance PPLetter a => PP(Prop a) where
pp (LetterP a) = ppLetter a
pp (AndP x y) = PP.sep [ parens 4 x, text "/\\", parens 4 y]
instance PPLetter a => Show (Prop a) where
show x = render (pp x)
main = do
let p = LetterP 1
print p
我不能看到的
LetterP
的定義,所以我假設它是從導入的包。那是對的嗎?當我編譯它,我得到一個錯誤:
ho8.hs:19:12: parse error on input `=' Failed, modules loaded: none.
爲什麼會出現這種錯誤,以及如何解決呢?我試圖打印
LetterP
但它沒有 工作。parens n ([email protected](LetterP _)) = pp term
是什麼意思,parens
是做什麼和[email protected]
是做什麼的?爲什麼沒有定義term
?刪除
{}
後,出現以下錯誤。爲什麼?ho8.hs:43:9: Ambiguous type variable `a0' in the constraints: (PPLetter a0) arising from a use of `print' at ho8.hs:43:9-13 (Num a0) arising from the literal `1' at ho8.hs:42:25 Probable fix: add a type signature that fixes these type variable(s) In the expression: print p In the expression: do { let p = LetterP 1; print p } In an equation for `main': main = do { let p = ...; print p } Failed, modules loaded: none.
它是否適用於'(1 :: Int)' – Ingo 2011-04-16 16:39:49