PrintAs.hs revision 2f6227e9ec96ca827cc40078916f18d54a075136
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian MaederModule : $Header$
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian MaederDescription : print the abstract syntax so that it can be re-parsed
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederCopyright : (c) Christian Maeder and Uni Bremen 2003
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian MaederLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian MaederMaintainer : Christian.Maeder@dfki.de
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaederStability : experimental
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederPortability : portable
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maederprinting data types of the abstract syntax
3c5cc698b0c061209ff83eb8de027daef5ae922aChristian Maederimport Data.List (groupBy, mapAccumL)
3c5cc698b0c061209ff83eb8de027daef5ae922aChristian Maeder-- | short cut for: if b then empty else d
3c5cc698b0c061209ff83eb8de027daef5ae922aChristian MaedernoPrint :: Bool -> Doc -> Doc
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaedernoPrint b d = if b then empty else d
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaedernoNullPrint :: [a] -> Doc -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedernoNullPrint = noPrint . null
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersemiDs :: Pretty a => [a] -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersemiDs = fsep . punctuate semi . map pretty
c18e9c3c6d5039618f1f2c05526ece84c7794ea3Christian MaedersemiAnnoted :: Pretty a => [Annoted a] -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersemiAnnoted = vcat . map (printSemiAnno pretty True)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty Variance where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty = sidDoc . mkSimpleId . show
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty a => Pretty (AnyKind a) where
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder pretty knd = case knd of
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder ClassKind ci -> pretty ci
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder FunKind v k1 k2 _ -> fsep
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [ pretty v <> (case k1 of
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder FunKind _ _ _ _ -> parens
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder _ -> id) (pretty k1)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , funArrow, pretty k2]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedervarOfTypeArg :: TypeArg -> Id
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedervarOfTypeArg (TypeArg i _ _ _ _ _ _) = i
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty TypePattern where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty tp = case tp of
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder TypePattern name@(Id ts cs _) args _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let ds = map (pretty . varOfTypeArg) args in
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder if placeCount name == length args then
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian Maeder let (ras, dts) = mapAccumL ( \ l t -> if isPlace t then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder x : r -> (r, x)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> error "Pretty TypePattern"
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else (l, printTypeToken t)) ds ts
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in fsep $ dts ++ (if null cs then [] else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [brackets $ sepByCommas $ map printTypeId cs])
92aa1b88f02d2a413da60dba78acd34312e6f29aChristian Maeder else printTypeId name <+> fsep ds
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TypePatternToken t -> printTypeToken t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder MixfixTypePattern ts -> fsep $ map pretty ts
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder BracketTypePattern k l _ -> bracket k $ ppWithCommas l
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder TypePatternArg t _ -> parens $ pretty t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | put proper brackets around a document
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederbracket :: BracketKind -> Doc -> Doc
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maederbracket b = case b of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Parens -> parens
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder Squares -> brackets
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Braces -> specBraces
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder NoBrackets -> id
d48085f765fca838c1d972d2123601997174583dChristian Maeder-- | print a 'Kind' plus a preceding colon (or nothing)
d48085f765fca838c1d972d2123601997174583dChristian MaederprintKind :: Kind -> Doc
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian MaederprintKind k = noPrint (k == universe) $ printVarKind InVar (VarKind k)
d48085f765fca838c1d972d2123601997174583dChristian Maeder-- | print the kind of a variable with its variance and a preceding colon
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian MaederprintVarKind :: Variance -> VarKind -> Doc
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaederprintVarKind e vk = case vk of
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder Downset t -> space <> less <+> pretty t
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder VarKind k -> space <> colon <+> pretty e <> pretty k
d48085f765fca838c1d972d2123601997174583dChristian Maeder MissingKind -> empty
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maederdata TypePrec = Outfix | Prefix | Lazyfix | ProdInfix | FunInfix | Absfix
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder deriving (Eq, Ord)
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian MaederparenPrec :: TypePrec -> (TypePrec, Doc) -> Doc
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian MaederparenPrec p1 (p2, d) = if p2 < p1 then d else parens d
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaederprintTypeToken :: Token -> Doc
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian MaederprintTypeToken t = let
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder l = ("*", cross) : map ( \ (a, d) -> (show a, d) )
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder [ (FunArr, funArrow)
d48085f765fca838c1d972d2123601997174583dChristian Maeder , (PFunArr, pfun)
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder , (ContFunArr, cfun)
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder , (PContFunArr, pcfun) ]
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder in case lookup (tokStr t) l of
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder _ -> pretty t
d48085f765fca838c1d972d2123601997174583dChristian MaederprintTypeId :: Id -> Doc
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaederprintTypeId (Id ts cs _) = let (toks, pls) = splitMixToken ts in
d48085f765fca838c1d972d2123601997174583dChristian Maeder fcat $ map printTypeToken toks
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder ++ (if null cs then [] else [brackets $ sepByCommas $ map printTypeId cs])
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder ++ map printTypeToken pls
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian MaedertoMixType :: Type -> (TypePrec, Doc)
d48085f765fca838c1d972d2123601997174583dChristian MaedertoMixType typ = case typ of
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder TypeName name _ _ -> (Outfix, printTypeId name)
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder TypeToken tt -> (Outfix, printTypeToken tt)
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder TypeAbs v t _ ->
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder (Absfix, sep [ lambda <+> pretty v, bullet <+> snd (toMixType t)])
d48085f765fca838c1d972d2123601997174583dChristian Maeder ExpandedType t1 _ -> toMixType t1 -- here we print the unexpanded type
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder BracketType k l _ ->
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder (Outfix, bracket k $ sepByCommas $ map (snd . toMixType) l)
d48085f765fca838c1d972d2123601997174583dChristian Maeder KindedType t kind _ ->
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder (Lazyfix, fsep [parenPrec Lazyfix $ toMixType t, colon, pretty kind])
d48085f765fca838c1d972d2123601997174583dChristian Maeder MixfixType ts -> (Prefix, fsep $ map (snd . toMixType) ts)
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder TypeAppl t1 t2 -> let
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder (topTy, tyArgs) = getTypeApplAux False typ
d48085f765fca838c1d972d2123601997174583dChristian Maeder aArgs = (Prefix, sep [ parenPrec ProdInfix $ toMixType t1
d48085f765fca838c1d972d2123601997174583dChristian Maeder , parenPrec Prefix $ toMixType t2 ])
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in case topTy of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TypeName name@(Id ts cs _) _k _i ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder case map toMixType tyArgs of
2986838ec286d67e7c199e7ea81e7364ca36ad25Christian Maeder [dArg] -> case ts of
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder [e] | name == lazyTypeId ->
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder (Lazyfix, pretty e <+> parenPrec Lazyfix dArg)
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder [e1, e2, e3] | not (isPlace e1) && isPlace e2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder && not (isPlace e3) && null cs ->
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder (Outfix, fsep [pretty e1, snd dArg, pretty e3])
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder [dArg1, dArg2] -> case ts of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [_, e2, _] | isInfix name && null cs ->
d48085f765fca838c1d972d2123601997174583dChristian Maeder if tokStr e2 == prodS then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (ProdInfix, fsep
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder [ parenPrec ProdInfix dArg1
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder , cross, parenPrec ProdInfix dArg2])
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder else -- assume fun type
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (FunInfix, fsep
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [ parenPrec FunInfix dArg1
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder , printTypeToken e2, snd dArg2])
48c4688439e0aade4faeebf25ca8b16d661e47afChristian Maeder dArgs -> if isProductIdWithArgs name $ length tyArgs then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (ProdInfix, fsep $ punctuate (space <> cross) $
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (parenPrec ProdInfix) dArgs) else aArgs
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maederinstance Pretty Type where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty = snd . toMixType
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- no curried notation for bound variables
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty TypeScheme where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty (TypeScheme vs t _) = let tdoc = pretty t in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if null vs then tdoc else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder fsep [forallDoc, semiDs vs, bullet <+> tdoc]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty Partiality where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty p = case p of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Partial -> quMarkD
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Total -> empty
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty Quantifier where
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder pretty q = case q of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Universal -> forallDoc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Existential -> exists
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Unique -> unique
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty TypeQual where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty q = case q of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder OfType -> colon
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder AsType -> text asS
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder InType -> inDoc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Inferred -> colon
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty Term where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty = printTerm . rmSomeTypes
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisSimpleTerm :: Term -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisSimpleTerm trm = case trm of
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder QualVar _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder QualOp _ _ _ _ _ -> True
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder ResolvedMixTerm _ _ _ _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ApplTerm _ _ _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TupleTerm _ _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TermToken _ -> True
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder BracketTerm _ _ _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | used only to produce CASL applications
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisSimpleArgTerm :: Term -> Bool
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian MaederisSimpleArgTerm trm = case trm of
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder QualVar vd -> not (isPatVarDecl vd)
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder QualOp _ _ _ _ _ -> True
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder ResolvedMixTerm n _ l _ -> placeCount n /= 0 || not (null l)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TupleTerm _ _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder BracketTerm _ _ _ -> True
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian MaederhasRightQuant :: Term -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederhasRightQuant t = case t of
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder QuantifiedTerm {} -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder LambdaTerm {} -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder CaseTerm {} -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder LetTerm Let _ _ _ -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ResolvedMixTerm n _ ts _ | endPlace n && placeCount n == length ts
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder -> hasRightQuant (last ts)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ApplTerm (ResolvedMixTerm n _ [] _) t2 _ | endPlace n ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TupleTerm ts _ | placeCount n == length ts -> hasRightQuant (last ts)
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder _ -> hasRightQuant t2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ApplTerm _ t2 _ -> hasRightQuant t2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederzipArgs :: Id -> [Term] -> [Doc] -> [Doc]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederzipArgs n ts ds = case (ts, ds) of
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder (t : r, d : s) -> let
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder p = parenTermDoc t d
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder e = if hasRightQuant t then parens d else p
966e627a1c06b302a06d59d08b8ab45905f3509cChristian Maeder in if null r && null s && endPlace n then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [if hasRightQuant t then d else p]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else e : zipArgs n r s
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederisPatVarDecl :: VarDecl -> Bool
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederisPatVarDecl (VarDecl v ty _ _) = case ty of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TypeName t _ _ -> isSimpleId v && take 2 (show t) == "_v"
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederparenTermDoc :: Term -> Doc -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederparenTermDoc trm = if isSimpleTerm trm then id else parens
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintTermRec :: FoldRec Doc (Doc, Doc)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintTermRec = FoldRec
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder { foldQualVar = \ _ vd@(VarDecl v _ _ _) ->
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder if isPatVarDecl vd then pretty v
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder else parens $ keyword varS <+> pretty vd
68485f7bfab1b4c6f963ce6837cba5fb148ed625Christian Maeder , foldQualOp = \ _ br n t tys _ -> (if null tys then id else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (<> brackets (ppWithCommas tys))) $
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder parens $ fsep [pretty br, pretty n, colon, pretty $
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if isPred br then unPredTypeScheme t else t]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldResolvedMixTerm =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder \ (ResolvedMixTerm _ _ os _) n@(Id toks cs ps) tys ts _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let pn = placeCount n in if pn == length ts || null ts then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let ds = zipArgs n os ts in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if null tys then idApplDoc n ds
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else let (ftoks, _) = splitMixToken toks
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder fId = Id ftoks cs ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (fts, rts) = splitAt (placeCount fId) $ if null ts
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder then replicate pn $ pretty placeTok else ds
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian Maeder in fsep $ (idApplDoc fId fts <> brackets (ppWithCommas tys))
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else idApplDoc applId [idDoc n, parens $ sepByCommas ts]
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder , foldApplTerm = \ (ApplTerm o1 o2 _) t1 t2 _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder case (o1, o2) of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder -- comment out the following two guards for CASL applications
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder (ResolvedMixTerm n _ [] _, TupleTerm ts@(_ : _) _)
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder | placeCount n == length ts ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder idApplDoc n (zipArgs n ts $ map printTerm ts)
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder (ResolvedMixTerm n _ [] _, _) | placeCount n == 1
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder -> idApplDoc n $ zipArgs n [o2] [t2]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> idApplDoc applId $ zipArgs applId [o1, o2] [t1, t2]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldTupleTerm = \ _ ts _ -> parens $ sepByCommas ts
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldTypedTerm = \ (TypedTerm ot _ _ _) t q typ _ -> fsep [(case ot of
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder LambdaTerm {} -> parens
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder LetTerm {} -> parens
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder CaseTerm {} -> parens
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder QuantifiedTerm {} -> parens
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder _ -> id) t, pretty q, pretty typ]
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian Maeder , foldQuantifiedTerm = \ _ q vs t _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder fsep [pretty q, printGenVarDecls vs, bullet <+> t]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldLambdaTerm = \ _ ps q t _ ->
9c5b1136299d9052e4e995614a3a36a051a2682fChristian Maeder fsep [ lambda
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder [p] -> if show p == "()" then empty else p
da245da15da78363c896e44ea97a14ab1f83eb50Christian Maeder _ -> fcat $ map parens ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Partial -> bullet
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Total -> bullet <> text exMark) <+> t]
d703a61b7fa9f85d92ac8f768d7c290b7c0a41c5Christian Maeder , foldCaseTerm = \ _ t es _ ->
abd8dd44106c507dd2cb64359b63d7d56fa0a9c8Christian Maeder fsep [text caseS, t, text ofS,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder cat $ punctuate (space <> bar <> space) $
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (printEq0 funArrow) es]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldLetTerm = \ _ br es t _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let des = sep $ punctuate semi $ map (printEq0 equals) es
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in case br of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Let -> fsep [sep [text letS <+> des, text inS], t]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Where -> fsep [sep [t, text whereS], des]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Program -> text programS <+> des
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldTermToken = \ _ t -> pretty t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldMixTypeTerm = \ _ q t _ -> pretty q <+> pretty t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldMixfixTerm = \ _ ts -> fsep ts
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldBracketTerm = \ _ k l _ -> bracket k $ sepByCommas l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldAsPattern = \ _ (VarDecl v _ _ _) p _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder fsep [pretty v, text asP, p]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldProgEq = \ _ p t _ -> (p, t) }
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederprintTerm :: Term -> Doc
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederprintTerm = foldTerm printTermRec
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederrmTypeRec :: MapRec
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederrmTypeRec = mapRec
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder { foldQualOp = \ t _ i _ _ ps ->
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder if elem i $ map fst bList then
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder ResolvedMixTerm i [] [] ps else t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldTypedTerm = \ _ nt q ty ps ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Inferred -> nt
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder _ -> case nt of
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder TypedTerm _ oq oty _ | oty == ty || oq == InType -> nt
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder QualVar (VarDecl _ oty _ _) | oty == ty -> nt
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> TypedTerm nt q ty ps }
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederrmSomeTypes :: Term -> Term
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederrmSomeTypes = foldTerm rmTypeRec
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | put parenthesis around applications
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederparenTermRec :: MapRec
eab576044505ba1fbc64610323053490fbd9e82cChristian MaederparenTermRec = let
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder addParAppl t = case t of
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder ResolvedMixTerm _ _ [] _ -> t
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder QualVar _ -> t
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian Maeder QualOp _ _ _ _ _ -> t
48c4688439e0aade4faeebf25ca8b16d661e47afChristian Maeder TermToken _ -> t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder BracketTerm _ _ _ -> t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TupleTerm _ _ -> t
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder _ -> TupleTerm [t] nullRange
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder { foldApplTerm = \ _ t1 t2 ps ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ApplTerm (addParAppl t1) (addParAppl t2) ps
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder , foldResolvedMixTerm = \ _ n tys ts ps ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ResolvedMixTerm n tys (map addParAppl ts) ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldTypedTerm = \ _ t q typ ps ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder TypedTerm (addParAppl t) q typ ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , foldMixfixTerm = \ _ ts -> MixfixTerm $ map addParAppl ts
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder , foldAsPattern = \ _ v p ps -> AsPattern v (addParAppl p) ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederparenTerm :: Term -> Term
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederparenTerm = foldTerm parenTermRec
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | print an equation with different symbols between 'Pattern' and 'Term'
ce3928e71520030ad0275b72050a8f4377f9313cChristian MaederprintEq0 :: Doc -> (Doc, Doc) -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintEq0 s (p, t) = sep [p, hsep [s, t]]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintGenVarDecls :: [GenVarDecl] -> Doc
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian MaederprintGenVarDecls = fsep . punctuate semi . map
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ( \ l -> case l of
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder [x] -> pretty x
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder GenVarDecl (VarDecl _ t _ _) : _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ppWithCommas (map ( \ (GenVarDecl (VarDecl v _ _ _)) -> v) l)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder <> printVarDeclType t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder GenTypeVarDecl (TypeArg _ e c _ _ _ _) : _ ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ppWithCommas (map ( \ (GenTypeVarDecl v) -> varOfTypeArg v) l)
48c4688439e0aade4faeebf25ca8b16d661e47afChristian Maeder <> printVarKind e c
48c4688439e0aade4faeebf25ca8b16d661e47afChristian Maeder _ -> error "printGenVarDecls") . groupBy sameType
48c4688439e0aade4faeebf25ca8b16d661e47afChristian MaedersameType :: GenVarDecl -> GenVarDecl -> Bool
48c4688439e0aade4faeebf25ca8b16d661e47afChristian MaedersameType g1 g2 = case (g1, g2) of
aae7a026a323021c5364aef85a0e03d586e5a5c3Christian Maeder (GenVarDecl (VarDecl _ t1 Comma _), GenVarDecl (VarDecl _ t2 _ _))
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | t1 == t2 -> True
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (GenTypeVarDecl (TypeArg _ e1 c1 _ _ Comma _),
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder GenTypeVarDecl (TypeArg _ e2 c2 _ _ _ _)) | e1 == e2 && c1 == c2 -> True
ce3928e71520030ad0275b72050a8f4377f9313cChristian MaederprintVarDeclType :: Type -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintVarDeclType t = case t of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder MixfixType [] -> empty
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> space <> colon <+> pretty t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty VarDecl where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty (VarDecl v t _ _) = pretty v <> printVarDeclType t
48c4688439e0aade4faeebf25ca8b16d661e47afChristian Maederinstance Pretty GenVarDecl where
48c4688439e0aade4faeebf25ca8b16d661e47afChristian Maeder pretty gvd = case gvd of
aae7a026a323021c5364aef85a0e03d586e5a5c3Christian Maeder GenVarDecl v -> pretty v
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder GenTypeVarDecl tv -> pretty tv
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty TypeArg where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty (TypeArg v e c _ _ _ _) =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty v <> printVarKind e c
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | don't print an empty list and put parens around longer lists
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintList0 :: (Pretty a) => [a] -> Doc
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederprintList0 l = case l of
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian Maeder [x] -> pretty x
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> parens $ ppWithCommas l
aae7a026a323021c5364aef85a0e03d586e5a5c3Christian Maederinstance Pretty BasicSpec where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty (BasicSpec l) = if null l then specBraces empty else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder changeGlobalAnnos addBuiltins . vcat $ map pretty l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty ProgEq where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty = printEq0 equals . foldEq printTermRec
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty BasicItem where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty bi = case bi of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder SigItems s -> pretty s
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ProgItems l _ -> sep [keyword programS, semiAnnoted l]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ClassItems i l _ -> let
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder b = semiAnnos pretty l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder p = plClass l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Plain -> topSigKey (classS ++ if p then "es" else "") <+> b
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Instance -> sep [keyword classS <+>
413db961f13e112716509b6d61d7a7bbf50c98b2Christian Maeder keyword (instanceS ++ if p then sS else ""), b]
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder GenVarItems l _ -> topSigKey (varS ++ pluralS l) <+> printGenVarDecls l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder FreeDatatype l _ -> sep
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [ keyword freeS <+> keyword (typeS ++ pluralS l)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , semiAnnos pretty l]
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder GenItems l _ -> let gkw = keyword generatedS in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (if all (isDatatype . item) l then \ i -> gkw <+> rmTopKey i
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else \ i -> sep [gkw, specBraces i])
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder $ vcat $ map (printAnnoted pretty) l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder AxiomItems vs fs _ -> sep
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [ if null vs then empty else forallDoc <+> printGenVarDecls vs
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> let pp = addBullet . pretty in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder vcat $ map (printAnnoted pp) (init fs)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ++ [printSemiAnno pp True $ last fs]]
f875f7eebac7f69bf9da98c93479a542d0a8056fChristian Maeder Internal l _ -> sep
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [ keyword internalS
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , specBraces $ vcat $ map (printAnnoted pretty) l]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederplClass :: [Annoted ClassItem] -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederplClass l = case map item l of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ : _ : _ -> True
ce3928e71520030ad0275b72050a8f4377f9313cChristian Maeder [ClassItem (ClassDecl (_ : _ : _) _ _) _ _] -> True
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaederpluralS :: [a] -> String
413db961f13e112716509b6d61d7a7bbf50c98b2Christian MaederpluralS l = case l of
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder _ : _ : _ -> sS