PrintAs.hs revision 9e0472be46104307b974fe5079bf5cc9e94a1a96
ca010363454de207082dfaa4b753531ce2a34551Christian MaederModule : $Header$
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederDescription : print the abstract syntax so that it can be re-parsed
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederCopyright : (c) Christian Maeder and Uni Bremen 2003
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
eca29a7be76eb73944ec19b06eda3d6a9e6e543dChristian MaederMaintainer : Christian.Maeder@dfki.de
ca010363454de207082dfaa4b753531ce2a34551Christian MaederStability : experimental
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederPortability : portable
79d11c2e3ad242ebb241f5d4a5e98a674c0b986fChristian Maederprinting data types of the abstract syntax
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maederimport Data.List (groupBy, mapAccumL)
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder-- | short cut for: if b then empty else d
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian MaedernoPrint :: Bool -> Doc -> Doc
83cc27e4ca7cf1a4bb5f4a8df17d3e6d44e6f1eaChristian MaedernoPrint b d = if b then empty else d
028f19cdb09d52bb2fd207399b6fa874540d1670Christian MaedernoNullPrint :: [a] -> Doc -> Doc
975642b989852fc24119c59cf40bc1af653608ffChristian MaedernoNullPrint = noPrint . null
83cc27e4ca7cf1a4bb5f4a8df17d3e6d44e6f1eaChristian MaedersemiDs :: Pretty a => [a] -> Doc
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaedersemiDs = fsep . punctuate semi . map pretty
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaedersemiAnnoted :: Pretty a => [Annoted a] -> Doc
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaedersemiAnnoted = vcat . map (printSemiAnno pretty True)
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian Maederinstance Pretty Variance where
83cc27e4ca7cf1a4bb5f4a8df17d3e6d44e6f1eaChristian Maeder pretty = sidDoc . mkSimpleId . show
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maederinstance Pretty a => Pretty (AnyKind a) where
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder pretty knd = case knd of
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder ClassKind ci -> pretty ci
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder FunKind v k1 k2 _ -> fsep
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder [ pretty v <> (case k1 of
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder FunKind _ _ _ _ -> parens
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maeder _ -> id) (pretty k1)
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maeder , funArrow, pretty k2]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaedervarOfTypeArg :: TypeArg -> Id
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaedervarOfTypeArg (TypeArg i _ _ _ _ _ _) = i
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maederinstance Pretty TypePattern where
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder pretty tp = case tp of
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder TypePattern name@(Id ts cs _) args _ ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder let ds = map (pretty . varOfTypeArg) args in
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder if placeCount name == length args then
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder let (ras, dts) = mapAccumL ( \ l t -> if isPlace t then
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder x : r -> (r, x)
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder _ -> error "Pretty TypePattern"
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder else (l, printTypeToken t)) ds ts
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder in fsep $ dts ++ (if null cs then [] else
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder [brackets $ sepByCommas $ map printTypeId cs])
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder else printTypeId name <+> fsep ds
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder TypePatternToken t -> printTypeToken t
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder MixfixTypePattern ts -> fsep $ map pretty ts
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder BracketTypePattern k l _ -> bracket k $ ppWithCommas l
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maeder TypePatternArg t _ -> parens $ pretty t
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maeder-- | put proper brackets around a document
53301de22afd7190981b363b57c48df86fcb50f7Christian Maederbracket :: BracketKind -> Doc -> Doc
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maederbracket b = case b of
5e605dc61ff9ec5724c319603905dc9b0dccc05fChristian Maeder Parens -> parens
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder Squares -> brackets
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder Braces -> specBraces
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder NoBrackets -> id
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder-- | print a 'Kind' plus a preceding colon (or nothing)
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederprintKind :: Kind -> Doc
d48085f765fca838c1d972d2123601997174583dChristian MaederprintKind k = noPrint (k == universe) $ printVarKind InVar (VarKind k)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder-- | print the kind of a variable with its variance and a preceding colon
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederprintVarKind :: Variance -> VarKind -> Doc
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian MaederprintVarKind e vk = case vk of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder Downset t -> less <+> pretty t
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder VarKind k -> colon <+> pretty e <> pretty k
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder MissingKind -> empty
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maederdata TypePrec = Outfix | Prefix | Lazyfix | ProdInfix | FunInfix | Absfix
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder deriving (Eq, Ord)
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederparenPrec :: TypePrec -> (TypePrec, Doc) -> Doc
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederparenPrec p1 (p2, d) = if p2 < p1 then d else parens d
da2b959c50c95309d8eb8b24174249c2847e74b5Christian MaederprintTypeToken :: Token -> Doc
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederprintTypeToken t = let
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder l = ("*", cross) : map ( \ (a, d) -> (show a, d) )
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder [ (FunArr, funArrow)
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , (PFunArr, pfun)
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder , (ContFunArr, cfun)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder , (PContFunArr, pcfun) ]
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder in case lookup (tokStr t) l of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder _ -> pretty t
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederprintTypeId :: Id -> Doc
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian MaederprintTypeId (Id ts cs _) = let (toks, pls) = splitMixToken ts in
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder fcat $ map printTypeToken toks
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder ++ (if null cs then [] else [brackets $ sepByCommas $ map printTypeId cs])
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder ++ map printTypeToken pls
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaedertoMixType :: Type -> (TypePrec, Doc)
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaedertoMixType typ = case typ of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder TypeName name _ _ -> (Outfix, printTypeId name)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder TypeToken tt -> (Outfix, printTypeToken tt)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder TypeAbs v t _ ->
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder (Absfix, sep [ lambda <+> pretty v, bullet <+> snd (toMixType t)])
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maeder ExpandedType t1 _ -> toMixType t1 -- here we print the unexpanded type
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder BracketType k l _ ->
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maeder (Outfix, bracket k $ sepByCommas $ map (snd . toMixType) l)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder KindedType t kind _ ->
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder (Lazyfix, fsep [parenPrec Lazyfix $ toMixType t, colon, pretty kind])
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder MixfixType ts -> (Prefix, fsep $ map (snd . toMixType) ts)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder TypeAppl t1 t2 -> let
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder (topTy, tyArgs) = getTypeApplAux False typ
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder aArgs = (Prefix, sep [ parenPrec ProdInfix $ toMixType t1
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder , parenPrec Prefix $ toMixType t2 ])
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder in case topTy of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder TypeName name@(Id ts cs _) _k _i ->
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maeder case map toMixType tyArgs of
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder [dArg] -> case ts of
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder [e] | name == lazyTypeId ->
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder (Lazyfix, pretty e <+> parenPrec Lazyfix dArg)
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder [e1, e2, e3] | not (isPlace e1) && isPlace e2
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder && not (isPlace e3) && null cs ->
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder (Outfix, fsep [pretty e1, snd dArg, pretty e3])
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder [dArg1, dArg2] -> case ts of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder [_, e2, _] | isInfix name && null cs ->
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder if tokStr e2 == prodS then
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder (ProdInfix, fsep
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder [ parenPrec ProdInfix dArg1
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder , cross, parenPrec ProdInfix dArg2])
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder else -- assume fun type
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder (FunInfix, fsep
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder [ parenPrec FunInfix dArg1
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder , printTypeToken e2, snd dArg2])
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder dArgs -> if isProductIdWithArgs name $ length tyArgs then
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder (ProdInfix, fsep $ punctuate (space <> cross) $
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder map (parenPrec ProdInfix) dArgs) else aArgs
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maederinstance Pretty Type where
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder pretty = snd . toMixType
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder-- no curried notation for bound variables
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maederinstance Pretty TypeScheme where
3f7009c892b16d172314abbba83d663fa0d87a65Christian Maeder pretty (TypeScheme vs t _) = let tdoc = pretty t in
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder if null vs then tdoc else
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder fsep [forallDoc, semiDs vs, bullet <+> tdoc]
53301de22afd7190981b363b57c48df86fcb50f7Christian Maederinstance Pretty Partiality where
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder pretty p = case p of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder Partial -> quMarkD
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder Total -> empty
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maederinstance Pretty Quantifier where
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder pretty q = case q of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder Universal -> forallDoc
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder Existential -> exists
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder Unique -> unique
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maederinstance Pretty TypeQual where
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder pretty q = case q of
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder OfType -> colon
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder AsType -> text asS
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder InType -> inDoc
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder Inferred -> colon
53301de22afd7190981b363b57c48df86fcb50f7Christian Maederinstance Pretty Term where
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder pretty = printTerm . rmSomeTypes
53301de22afd7190981b363b57c48df86fcb50f7Christian MaederisSimpleTerm :: Term -> Bool
53301de22afd7190981b363b57c48df86fcb50f7Christian MaederisSimpleTerm trm = case trm of
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder QualVar _ -> True
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder QualOp _ _ _ _ _ -> True
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder ResolvedMixTerm _ _ _ _ -> True
669e21946b6f90785fc3cb44e7cf4f38c3f6493dChristian Maeder ApplTerm _ _ _ -> True
669e21946b6f90785fc3cb44e7cf4f38c3f6493dChristian Maeder TupleTerm _ _ -> True
669e21946b6f90785fc3cb44e7cf4f38c3f6493dChristian Maeder TermToken _ -> True
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder BracketTerm _ _ _ -> True
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder-- | used only to produce CASL applications
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian MaederisSimpleArgTerm :: Term -> Bool
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederisSimpleArgTerm trm = case trm of
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder QualVar vd -> not (isPatVarDecl vd)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder QualOp _ _ _ _ _ -> True
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maeder ResolvedMixTerm n _ l _ -> placeCount n /= 0 || not (null l)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder TupleTerm _ _ -> True
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder BracketTerm _ _ _ -> True
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederhasRightQuant :: Term -> Bool
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian MaederhasRightQuant t = case t of
669e21946b6f90785fc3cb44e7cf4f38c3f6493dChristian Maeder QuantifiedTerm {} -> True
42c01284bba8d7c8d995c8dfb96ace57d28ed1bcTill Mossakowski LambdaTerm {} -> True
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder CaseTerm {} -> True
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder LetTerm Let _ _ _ -> True
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder ResolvedMixTerm n _ ts _ | endPlace n && placeCount n == length ts
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder -> hasRightQuant (last ts)
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder ApplTerm (ResolvedMixTerm n _ [] _) t2 _ | endPlace n ->
42c01284bba8d7c8d995c8dfb96ace57d28ed1bcTill Mossakowski TupleTerm ts _ | placeCount n == length ts -> hasRightQuant (last ts)
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder _ -> hasRightQuant t2
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder ApplTerm _ t2 _ -> hasRightQuant t2
f8a03685d9184046e88e1d76aabdab4f714db440Christian MaederzipArgs :: Id -> [Term] -> [Doc] -> [Doc]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederzipArgs n ts ds = case (ts, ds) of
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder (t : r, d : s) -> let
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder p = parenTermDoc t d
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder e = if hasRightQuant t then parens d else p
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder in if null r && null s && endPlace n then
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder [if hasRightQuant t then d else p]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder else e : zipArgs n r s
fcfed328fae6266214ee61ee7a16fd263fd3cb70Christian MaederisPatVarDecl :: VarDecl -> Bool
dcb9ff0e2c2379735acce7073196508d455e0b01Christian MaederisPatVarDecl (VarDecl v ty _ _) = case ty of
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder TypeName t _ _ -> isSimpleId v && take 2 (show t) == "_v"
975642b989852fc24119c59cf40bc1af653608ffChristian MaederparenTermDoc :: Term -> Doc -> Doc
dcb9ff0e2c2379735acce7073196508d455e0b01Christian MaederparenTermDoc trm = if isSimpleTerm trm then id else parens
13b24998210d193b38cae06485da6f06c61d7f62Christian MaederprintTermRec :: FoldRec Doc (Doc, Doc)
dcb9ff0e2c2379735acce7073196508d455e0b01Christian MaederprintTermRec = FoldRec
13b24998210d193b38cae06485da6f06c61d7f62Christian Maeder { foldQualVar = \ _ vd@(VarDecl v _ _ _) ->
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder if isPatVarDecl vd then pretty v
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder else parens $ keyword varS <+> pretty vd
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder , foldQualOp = \ _ br n t tys _ -> (if null tys then id else
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder (<> brackets (ppWithCommas tys))) $
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder parens $ fsep [pretty br, pretty n, colon, pretty $
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maeder if isPred br then unPredTypeScheme t else t]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldResolvedMixTerm =
e68f45f355ed9d4026ee9baff5aa75aa7c911cc2Christian Maeder \ (ResolvedMixTerm _ _ os _) n@(Id toks cs ps) tys ts _ ->
da2b959c50c95309d8eb8b24174249c2847e74b5Christian Maeder let pn = placeCount n in if pn == length ts || null ts then
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder let ds = zipArgs n os ts in
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder if null tys then idApplDoc n ds
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder else let (ftoks, _) = splitMixToken toks
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder fId = Id ftoks cs ps
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder (fts, rts) = splitAt (placeCount fId) $ if null ts
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder then replicate pn $ pretty placeTok else ds
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder in fsep $ (idApplDoc fId fts <> brackets (ppWithCommas tys))
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder else idApplDoc applId [idDoc n, parens $ sepByCommas ts]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldApplTerm = \ (ApplTerm o1 o2 _) t1 t2 _ ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder case (o1, o2) of
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder -- comment out the following two guards for CASL applications
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder (ResolvedMixTerm n _ [] _, TupleTerm ts@(_ : _) _)
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder | placeCount n == length ts ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder idApplDoc n (zipArgs n ts $ map printTerm ts)
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder (ResolvedMixTerm n _ [] _, _) | placeCount n == 1
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder -> idApplDoc n $ zipArgs n [o2] [t2]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder _ -> idApplDoc applId $ zipArgs applId [o1, o2] [t1, t2]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldTupleTerm = \ _ ts _ -> parens $ sepByCommas ts
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldTypedTerm = \ (TypedTerm ot _ _ _) t q typ _ -> fsep [(case ot of
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder LambdaTerm {} -> parens
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder LetTerm {} -> parens
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder CaseTerm {} -> parens
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder QuantifiedTerm {} -> parens
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder ApplTerm (ResolvedMixTerm n _ [] _) arg _ ->
13b24998210d193b38cae06485da6f06c61d7f62Christian Maeder let pn = placeCount n in case arg of
13b24998210d193b38cae06485da6f06c61d7f62Christian Maeder TupleTerm ts@(_ : _) _ | pn == length ts -> parens
13b24998210d193b38cae06485da6f06c61d7f62Christian Maeder _ | pn == 1 -> parens
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder _ -> id) t, pretty q, pretty typ]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldQuantifiedTerm = \ _ q vs t _ ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder fsep [pretty q, printGenVarDecls vs, bullet <+> t]
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder , foldLambdaTerm = \ _ ps q t _ ->
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder fsep [ lambda
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder [p] -> if show p == "()" then empty else p
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder _ -> fcat $ map parens ps
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder Partial -> bullet
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder Total -> bullet <> text exMark) <+> t]
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder , foldCaseTerm = \ _ t es _ ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder fsep [text caseS, t, text ofS,
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder cat $ punctuate (space <> bar <> space) $
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder map (printEq0 funArrow) es]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldLetTerm = \ _ br es t _ ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder let des = sep $ punctuate semi $ map (printEq0 equals) es
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder in case br of
2fc11b362b9242202bda207e7c7ecc7771f1a5e3Christian Maeder Let -> fsep [sep [text letS <+> des, text inS], t]
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder Where -> fsep [sep [t, text whereS], des]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder Program -> text programS <+> des
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldTermToken = \ _ t -> pretty t
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldMixTypeTerm = \ _ q t _ -> pretty q <+> pretty t
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldMixfixTerm = \ _ ts -> fsep ts
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldBracketTerm = \ _ k l _ -> bracket k $ sepByCommas l
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldAsPattern = \ _ (VarDecl v _ _ _) p _ ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder fsep [pretty v, text asP, p]
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder , foldProgEq = \ _ p t _ -> (p, t) }
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederprintTerm :: Term -> Doc
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederprintTerm = foldTerm printTermRec
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederrmTypeRec :: MapRec
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian MaederrmTypeRec = mapRec
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder { foldQualOp = \ t _ (PolyId i _ _) _ _ ps ->
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder if elem i $ map fst bList then
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder ResolvedMixTerm i [] [] ps else t
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder , foldTypedTerm = \ _ nt q ty ps ->
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder Inferred -> nt
59a2f25e7d71b91b4eda6fa4da753473ad629619Christian Maeder _ -> case nt of
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder TypedTerm _ oq oty _ | oty == ty || oq == InType -> nt
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder QualVar (VarDecl _ oty _ _) | oty == ty -> nt
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder _ -> TypedTerm nt q ty ps }
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian MaederrmSomeTypes :: Term -> Term
dcb9ff0e2c2379735acce7073196508d455e0b01Christian MaederrmSomeTypes = foldTerm rmTypeRec
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder-- | put parenthesis around applications
dcb9ff0e2c2379735acce7073196508d455e0b01Christian MaederparenTermRec :: MapRec
dcb9ff0e2c2379735acce7073196508d455e0b01Christian MaederparenTermRec = let
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder addParAppl t = case t of
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder ResolvedMixTerm _ _ [] _ -> t
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder QualVar _ -> t
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder QualOp _ _ _ _ _ -> t
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder TermToken _ -> t
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder BracketTerm _ _ _ -> t
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder TupleTerm _ _ -> t
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian Maeder _ -> TupleTerm [t] nullRange
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder { foldApplTerm = \ _ t1 t2 ps ->
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder ApplTerm (addParAppl t1) (addParAppl t2) ps
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder , foldResolvedMixTerm = \ _ n tys ts ps ->
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder ResolvedMixTerm n tys (map addParAppl ts) ps
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder , foldTypedTerm = \ _ t q typ ps ->
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder TypedTerm (addParAppl t) q typ ps
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder , foldMixfixTerm = \ _ ts -> MixfixTerm $ map addParAppl ts
dcb9ff0e2c2379735acce7073196508d455e0b01Christian Maeder , foldAsPattern = \ _ v p ps -> AsPattern v (addParAppl p) ps
49fc18b4bb1d4a8d3ec05e0cffd5f0475b289592Christian MaederparenTerm :: Term -> Term
975642b989852fc24119c59cf40bc1af653608ffChristian MaederparenTerm = foldTerm parenTermRec
975642b989852fc24119c59cf40bc1af653608ffChristian Maeder-- | print an equation with different symbols between 'Pattern' and 'Term'
975642b989852fc24119c59cf40bc1af653608ffChristian MaederprintEq0 :: Doc -> (Doc, Doc) -> Doc