2011-04-16 29 views
0

例如,在這樣的代碼:在Haskell中做什麼顯示和渲染?

module Main(letters) where 

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 
+1

一個提問時,請更詳細一點。 – fuz 2011-04-16 16:09:10

回答

4
  1. The Show class,及其方法,show,將值轉換爲字符串。

  2. render功能是Text.PrettyPrint模塊的一部分,該模塊將值轉換爲精美打印的字符串(例如,使用佈局,縮進和嵌套)。

他們一起用於pretty printing