PrintAs.hs revision d92635f998347112e5d5803301c2abfe7832ab65
8267b99c0d7a187abe6f87ad50530dc08f5d1cdcAndy GimblettModule : $Header$
e071fb22ea9923a2a4ff41184d80ca46b55ee932Till MossakowskiCopyright : (c) Christian Maeder and Uni Bremen 2003
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettMaintainer : maeder@tzi.de
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettStability : experimental
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettPortability : portable
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblettprinting data types of the abstract syntax
04ceed96d1528b939f2e592d0656290d81d1c045Andy Gimblett-- | short cut for: if b then empty else d
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettnoPrint :: Bool -> Doc -> Doc
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettnoPrint b d = if b then empty else d
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettnoNullPrint :: [a] -> Doc -> Doc
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettnoNullPrint = noPrint . null
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettsemiDs :: Pretty a => [a] -> Doc
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettsemiDs = fsep . punctuate semi . map pretty
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettsemiAnnoted :: Pretty a => [Annoted a] -> Doc
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettsemiAnnoted = semiAnnos pretty
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblettinstance Pretty Variance where
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett pretty = sidDoc . mkSimpleId . show
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblettinstance Pretty a => Pretty (AnyKind a) where
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett pretty knd = case knd of
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett ClassKind ci -> pretty ci
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett FunKind v k1 k2 _ -> fsep [pretty v <>
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett FunKind _ _ _ _ -> parens
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett _ -> id) (pretty k1)
2f06b54890375b6cac90394b80b07bd451d728fcAndy Gimblettinstance Pretty TypePattern where
2f06b54890375b6cac90394b80b07bd451d728fcAndy Gimblett pretty tp = case tp of
2f06b54890375b6cac90394b80b07bd451d728fcAndy Gimblett TypePattern name args _ -> pretty name
2f06b54890375b6cac90394b80b07bd451d728fcAndy Gimblett <> fcat (map (parens . pretty) args)
a09bfcbcb0fba5663fca1968aa82daebf2e092c4Andy Gimblett TypePatternToken t -> pretty t
ac5ec613b786cd05f495b568ab5214c31a333e67Andy Gimblett MixfixTypePattern ts -> fsep (map (pretty) ts)
a09bfcbcb0fba5663fca1968aa82daebf2e092c4Andy Gimblett BracketTypePattern k l _ -> bracket k $ ppWithCommas l
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett TypePatternArg t _ -> parens $ pretty t
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett-- | put proper brackets around a document
f909337bf7012aca169c0b56b89efbd4a310f8daAndy Gimblettbracket :: BracketKind -> Doc -> Doc
9f93b2a8b552789cd939d599504d39732672dc84Christian Maederbracket b = case b of
f909337bf7012aca169c0b56b89efbd4a310f8daAndy Gimblett Parens -> parens
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett Squares -> brackets
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett Braces -> specBraces
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett NoBrackets -> id
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett-- | print a 'Kind' plus a preceding colon (or nothing)
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy GimblettprintKind :: Kind -> Doc
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy GimblettprintKind k = noPrint (k == universe) $ printVarKind InVar (VarKind k)
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett-- | print the kind of a variable with its variance and a preceding colon
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimblettprintVarKind :: Variance -> VarKind -> Doc
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy GimblettprintVarKind e vk = case vk of
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett space <> less <+> pretty t
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett VarKind k -> space <> colon <+>
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett pretty e <> pretty k
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett MissingKind -> empty
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblettdata TypePrec = Outfix | Prefix | ProdInfix | FunInfix deriving (Eq, Ord)
2f06b54890375b6cac90394b80b07bd451d728fcAndy GimblettparenPrec :: TypePrec -> (TypePrec, Doc) -> Doc
a09bfcbcb0fba5663fca1968aa82daebf2e092c4Andy GimblettparenPrec p1 (p2, d) = if p2 < p1 then d else parens d
020cdb5dad6b871aba61136a0e1567c00426de87Andy GimbletttoMixType :: Type -> (TypePrec, Doc)