PrintAs.hs revision e997f1724fcc2c5bb49f44e9f45e8354e0f3a2d6
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceModule : $Header$
5d801400993c9671010d244646936d8fd435638cChristian MaederCopyright : (c) Christian Maeder and Uni Bremen 2003
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceMaintainer : maeder@tzi.de
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceStability : experimental
5d801400993c9671010d244646936d8fd435638cChristian MaederPortability : portable
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Manceprinting data types of the abstract syntax
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Manceimport Data.List (groupBy)
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance-- | short cut for: if b then empty else d
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancenoPrint :: Bool -> Doc -> Doc
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancenoPrint b d = if b then empty else d
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel MancenoNullPrint :: [a] -> Doc -> Doc
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel MancenoNullPrint = noPrint . null
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancesemiDs :: Pretty a => [a] -> Doc
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancesemiDs = fsep . punctuate semi . map pretty
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancesemiAnnoted :: Pretty a => [Annoted a] -> Doc
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancesemiAnnoted = vcat . map (printSemiAnno pretty True)
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Manceinstance Pretty Variance where
0ec1551231bc5dfdcb3f2bd68fec7457fade7bfdFelix Gabriel Mance pretty = sidDoc . mkSimpleId . show
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maederinstance Pretty a => Pretty (AnyKind a) where
968930c7674ae3b63d308bf4fa651400aa263054Christian Maeder pretty knd = case knd of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance ClassKind ci -> pretty ci
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance FunKind v k1 k2 _ -> fsep [pretty v <>
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance FunKind _ _ _ _ -> parens
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance _ -> id) (pretty k1)
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel MancevarOfTypeArg :: TypeArg -> Id
fc7bd98aabe1bc26058660085e8c77d60a97bcecChristian MaedervarOfTypeArg (TypeArg i _ _ _ _ _ _) = i
fc7bd98aabe1bc26058660085e8c77d60a97bcecChristian Maederinstance Pretty TypePattern where
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance pretty tp = case tp of
3c6b4f79cea11dd2acc2060bf1502b6ba9e905f2Felix Gabriel Mance TypePattern name args _ -> pretty name
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance <+> fsep (map (pretty . varOfTypeArg) args)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance TypePatternToken t -> pretty t
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance MixfixTypePattern ts -> fsep $ map pretty ts
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder BracketTypePattern k l _ -> bracket k $ ppWithCommas l
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance TypePatternArg t _ -> parens $ pretty t
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance-- | put proper brackets around a document
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mancebracket :: BracketKind -> Doc -> Doc
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mancebracket b = case b of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Parens -> parens
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Squares -> brackets
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance Braces -> specBraces
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance NoBrackets -> id
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder-- | print a 'Kind' plus a preceding colon (or nothing)
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel ManceprintKind :: Kind -> Doc
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceprintKind k = noPrint (k == universe) $ printVarKind InVar (VarKind k)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance-- | print the kind of a variable with its variance and a preceding colon
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceprintVarKind :: Variance -> VarKind -> Doc
852bd6145634dc2832b61c44678fe539bc1682d5Christian MaederprintVarKind e vk = case vk of
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder space <> less <+> pretty t
ea3f858eb531d981df3ed00beeadd99cf025adecChristian Maeder VarKind k -> space <> colon <+>
ea3f858eb531d981df3ed00beeadd99cf025adecChristian Maeder pretty e <> pretty k
ea3f858eb531d981df3ed00beeadd99cf025adecChristian Maeder MissingKind -> empty
3c6b4f79cea11dd2acc2060bf1502b6ba9e905f2Felix Gabriel Mancedata TypePrec = Outfix | Prefix | ProdInfix | FunInfix | Absfix
52991d9b46a98ad6a9020421a3244950b0f8a522Felix Gabriel Mance deriving (Eq, Ord)
3c6b4f79cea11dd2acc2060bf1502b6ba9e905f2Felix Gabriel ManceparenPrec :: TypePrec -> (TypePrec, Doc) -> Doc
3c6b4f79cea11dd2acc2060bf1502b6ba9e905f2Felix Gabriel ManceparenPrec p1 (p2, d) = if p2 < p1 then d else parens d
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel ManceprintTypeToken :: Token -> Doc
75aaf82c430ad2a5cf159962b1c5c09255010fb4Felix Gabriel ManceprintTypeToken t = let
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance l = ("*", cross) : map ( \ (a, d) -> (show a, d) )
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance [ (FunArr, funArrow)
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance , (PFunArr, pfun)
1b1144abf7f95a4b23405b8d5604813cfe7b036aFelix Gabriel Mance , (ContFunArr, cfun)
4c684d7a2343be7350eba088f8be42888f86a495Felix Gabriel Mance , (PContFunArr, pcfun) ]
1b1144abf7f95a4b23405b8d5604813cfe7b036aFelix Gabriel Mance in case lookup (tokStr t) l of
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel MancetoMixType :: Type -> (TypePrec, Doc)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancetoMixType typ = case typ of
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance TypeName name _ _ -> (Outfix, pretty name)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance TypeToken tt -> (Outfix, printTypeToken tt)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance TypeAbs v t _ -> (Absfix, sep [ lambda <+> pretty v
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance , bullet <+> snd (toMixType t)])
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance ExpandedType t1 _ -> toMixType t1 -- here we print the unexpanded type
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance BracketType k l _ -> (Outfix, bracket k $ sepByCommas $ map
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance (snd . toMixType) l)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance KindedType t kind _ -> (Prefix,
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance fsep [parenPrec Prefix $ toMixType t, colon, pretty kind])
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance MixfixType ts -> (Prefix, fsep $ map (snd . toMixType) ts)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance TypeAppl t1 t2 -> let
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance (topTy, tyArgs) = getTypeApplAux False typ
9cb6af1a7632f12b60f592ce5eb2ac51e6bd33bbFelix Gabriel Mance aArgs = (Prefix, sep [ parenPrec ProdInfix $ toMixType t1
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance , parenPrec Prefix $ toMixType t2 ])
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance in case topTy of
968930c7674ae3b63d308bf4fa651400aa263054Christian Maeder TypeName name@(Id ts cs _) _k _i ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance case map toMixType tyArgs of
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance [e1, e2, e3] | not (isPlace e1) && isPlace e2
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance && not (isPlace e3) && null cs ->
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance (Outfix, fsep [pretty e1, snd dArg, pretty e3])
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance [dArg1, dArg2] ->
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance [e1, e2, e3] | isPlace e1 && not (isPlace e2)
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance && isPlace e3 && null cs ->
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance if tokStr e2 == prodS then
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance (ProdInfix, fsep [
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance parenPrec ProdInfix dArg1, cross,
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance parenPrec ProdInfix dArg2])
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance else -- assume fun type
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance (FunInfix, fsep [
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance parenPrec FunInfix dArg1, printTypeToken e2, snd dArg2])
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance dArgs -> if name == productId (length tyArgs) then
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance (ProdInfix, fsep $ punctuate (space <> cross) $
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance map (parenPrec ProdInfix) dArgs)
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Manceinstance Pretty Type where
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance pretty = snd . toMixType
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance-- no curried notation for bound variables
968930c7674ae3b63d308bf4fa651400aa263054Christian Maederinstance Pretty TypeScheme where
a921ae1da1302f673204e7b63cdce01439a9bd5eFelix Gabriel Mance pretty (TypeScheme vs t _) = let tdoc = pretty t in
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance if null vs then tdoc else
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance fsep [forallDoc, semiDs vs, bullet <+> tdoc]
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Manceinstance Pretty Partiality where
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance pretty p = case p of
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance Partial -> quMarkD
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance Total -> empty
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Manceinstance Pretty Quantifier where
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance pretty q = case q of
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance Universal -> forallDoc
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance Existential -> exists
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance Unique -> unique
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Manceinstance Pretty TypeQual where
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance pretty q = case q of
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance OfType -> colon
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance AsType -> text asS
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance InType -> inDoc
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance Inferred -> colon
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Manceinstance Pretty Term where
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance pretty = printTerm . rmSomeTypes
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel ManceisSimpleTerm :: Term -> Bool
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel ManceisSimpleTerm trm = case trm of
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance QualVar _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance QualOp _ _ _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance ResolvedMixTerm _ _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance ApplTerm _ _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance TupleTerm _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance TermToken _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance BracketTerm _ _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance-- | used only to produce CASL applications
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel ManceisSimpleArgTerm :: Term -> Bool
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel ManceisSimpleArgTerm trm = case trm of
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance QualVar vd -> not (isPatVarDecl vd)
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance QualOp _ _ _ _ -> True
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance ResolvedMixTerm n l _ -> placeCount n /= 0 || not (null l)
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance TupleTerm _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance BracketTerm _ _ _ -> True
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel MancehasRightQuant :: Term -> Bool
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancehasRightQuant t = case t of
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance QuantifiedTerm {} -> True
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance LambdaTerm {} -> True
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance CaseTerm {} -> True
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance LetTerm Let _ _ _ -> True
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance ResolvedMixTerm n ts _ | endPlace n && placeCount n == length ts
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance -> hasRightQuant (last ts)
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance ApplTerm (ResolvedMixTerm n [] _) t2 _ | endPlace n ->
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance TupleTerm ts _ | placeCount n == length ts -> hasRightQuant (last ts)
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance _ -> hasRightQuant t2
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance ApplTerm _ t2 _ -> hasRightQuant t2
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel MancezipArgs :: Id -> [Term] -> [Doc] -> [Doc]
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel MancezipArgs n ts ds = case (ts, ds) of
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance (t : r, d : s) -> let
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance p = parenTermDoc t d
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance e = if hasRightQuant t then parens d else p
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance in if null r && null s && endPlace n then
44985cbd4eb61dbc348617ebdd44a774e51dac07Christian Maeder [if hasRightQuant t then d else p]
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance else e : zipArgs n r s
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel ManceisPatVarDecl :: VarDecl -> Bool
44985cbd4eb61dbc348617ebdd44a774e51dac07Christian MaederisPatVarDecl (VarDecl v ty _ _) = case ty of
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance TypeName t _ _ -> isSimpleId v && take 2 (show t) == "_v"
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel ManceparenTermDoc :: Term -> Doc -> Doc
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel ManceparenTermDoc trm = if isSimpleTerm trm then id else parens
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel ManceprintTermRec :: FoldRec Doc (Doc, Doc)
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel ManceprintTermRec = FoldRec
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel Mance { foldQualVar = \ _ vd@(VarDecl v _ _ _) ->
511be329b2e8f55d0c6b18bd92571a1776b15932Felix Gabriel Mance if isPatVarDecl vd then pretty v
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel Mance else parens $ keyword varS <+> pretty vd
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel Mance , foldQualOp = \ _ br n t _ ->
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel Mance parens $ fsep [pretty br, pretty n, colon, pretty $
83f5f3291f9b40fa688776b4da10b5fa102a5ff8Felix Gabriel Mance if isPred br then unPredTypeScheme t else t]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , foldResolvedMixTerm = \ (ResolvedMixTerm _ os _) n ts _ ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance if placeCount n == length ts || null ts then
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance idApplDoc n $ zipArgs n os ts
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance else idApplDoc applId [idDoc n, parens $ sepByCommas ts]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , foldApplTerm = \ (ApplTerm o1 o2 _) t1 t2 _ ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance case (o1, o2) of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance -- comment out the following two guards for CASL applications
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance (ResolvedMixTerm n [] _, TupleTerm ts _) | placeCount n == length ts
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance -> idApplDoc n $ zipArgs n ts $ map printTerm ts
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance (ResolvedMixTerm n [] _, _) | placeCount n == 1
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance -> idApplDoc n $ zipArgs n [o2] [t2]
968930c7674ae3b63d308bf4fa651400aa263054Christian Maeder _ -> idApplDoc applId $ zipArgs applId [o1, o2] [t1, t2]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , foldTupleTerm = \ _ ts _ -> parens $ sepByCommas ts
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , foldTypedTerm = \ (TypedTerm ot _ _ _) t q typ _ -> fsep [(case ot of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance LambdaTerm {} -> parens
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance LetTerm {} -> parens
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance CaseTerm {} -> parens
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance QuantifiedTerm {} -> parens
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance _ -> id) t, pretty q, pretty typ]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , foldQuantifiedTerm = \ _ q vs t _ ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance fsep [pretty q, printGenVarDecls vs, bullet <+> t]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , foldLambdaTerm = \ _ ps q t _ ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance _ -> fcat $ map parens ps
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance Partial -> bullet
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance Total -> bullet <> text exMark) <+> t]
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldCaseTerm = \ _ t es _ ->
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance fsep [text caseS, t, text ofS,
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance cat $ punctuate (space <> bar <> space) $
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance map (printEq0 funArrow) es]
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldLetTerm = \ _ br es t _ ->
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance let des = sep $ punctuate semi $ map (printEq0 equals) es
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance Let -> fsep [sep [text letS <+> des, text inS], t]
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance Where -> fsep [sep [t, text whereS], des]
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance Program -> text programS <+> des
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance , foldTermToken = \ _ t -> pretty t
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldMixTypeTerm = \ _ q t _ -> pretty q <+> pretty t
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldMixfixTerm = \ _ ts -> fsep ts
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldBracketTerm = \ _ k l _ -> bracket k $ sepByCommas l
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldAsPattern = \ _ (VarDecl v _ _ _) p _ ->
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance fsep [pretty v, text asP, p]
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldProgEq = \ _ p t _ -> (p, t)
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel ManceprintTerm :: Term -> Doc
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel ManceprintTerm = foldTerm printTermRec
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel MancermTypeRec :: MapRec
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel MancermTypeRec = mapRec
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance { foldQualOp = \ t _ (InstOpId i _ _) _ ps ->
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance if elem i $ map fst bList then
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance ResolvedMixTerm i [] ps else t
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance , foldTypedTerm = \ _ nt q ty ps ->
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance Inferred -> nt
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance _ -> case nt of
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance TypedTerm _ oq oty _ | oty == ty || oq == InType -> nt
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance QualVar (VarDecl _ oty _ _) | oty == ty -> nt
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance _ -> TypedTerm nt q ty ps
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel MancermSomeTypes :: Term -> Term
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel MancermSomeTypes = foldTerm rmTypeRec
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance-- | put parenthesis around applications
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel ManceparenTermRec :: MapRec
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel ManceparenTermRec = let
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance addParAppl t = case t of
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance ResolvedMixTerm _ [] _ -> t
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance QualVar _ -> t
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance QualOp _ _ _ _ -> t
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance TermToken _ -> t
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance BracketTerm _ _ _ -> t
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder TupleTerm _ _ -> t
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance _ -> TupleTerm [t] nullRange
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder { foldApplTerm = \ _ t1 t2 ps ->
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder ApplTerm (addParAppl t1) (addParAppl t2) ps
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder , foldResolvedMixTerm = \ _ n ts ps ->
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder ResolvedMixTerm n (map addParAppl ts) ps
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder , foldTypedTerm = \ _ t q typ ps ->
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder TypedTerm (addParAppl t) q typ ps
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder , foldMixfixTerm = \ _ ts -> MixfixTerm $ map addParAppl ts
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder , foldAsPattern = \ _ v p ps -> AsPattern v (addParAppl p) ps
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederparenTerm :: Term -> Term
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederparenTerm = foldTerm parenTermRec
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder-- | print an equation with different symbols between 'Pattern' and 'Term'
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintEq0 :: Doc -> (Doc, Doc) -> Doc
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintEq0 s (p, t) = sep [p, hsep [s, t]]
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintGenVarDecls :: [GenVarDecl] -> Doc
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintGenVarDecls = fsep . punctuate semi . map
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder ( \ l -> case l of
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder [x] -> pretty x
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder GenVarDecl (VarDecl _ t _ _) : _ ->
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder ppWithCommas (map ( \ (GenVarDecl (VarDecl v _ _ _)) -> v) l)
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder <> printVarDeclType t
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder GenTypeVarDecl (TypeArg _ e c _ _ _ _) : _ ->
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder ppWithCommas (map ( \ (GenTypeVarDecl v) -> varOfTypeArg v) l)
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder <> printVarKind e c
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder _ -> error "printGenVarDecls") . groupBy sameType
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaedersameType :: GenVarDecl -> GenVarDecl -> Bool
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaedersameType g1 g2 = case (g1, g2) of
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder (GenVarDecl (VarDecl _ t1 Comma _), GenVarDecl (VarDecl _ t2 _ _))
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder | t1 == t2 -> True
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder (GenTypeVarDecl (TypeArg _ e1 c1 _ _ Comma _),
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder GenTypeVarDecl (TypeArg _ e2 c2 _ _ _ _)) | e1 == e2 && c1 == c2 -> True
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintVarDeclType :: Type -> Doc
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintVarDeclType t = case t of
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder MixfixType [] -> empty
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder _ -> space <> colon <+> pretty t
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maederinstance Pretty VarDecl where
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder pretty (VarDecl v t _ _) = pretty v <> printVarDeclType t
cbb0a924599bcaea230e7dcd2892cc91c49319aeChristian Maederinstance Pretty GenVarDecl where
cbb0a924599bcaea230e7dcd2892cc91c49319aeChristian Maeder pretty gvd = case gvd of
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder GenVarDecl v -> pretty v
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder GenTypeVarDecl tv -> pretty tv
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maederinstance Pretty TypeArg where
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder pretty (TypeArg v e c _ _ _ _) =
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder pretty v <> printVarKind e c
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder-- | don't print an empty list and put parens around longer lists
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintList0 :: (Pretty a) => [a] -> Doc
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintList0 l = case l of
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder [x] -> pretty x
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder _ -> parens $ ppWithCommas l
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maederinstance Pretty InstOpId where
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder pretty (InstOpId n l _) = pretty n <> noNullPrint l
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder (brackets $ semiDs l)
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder-- | print a 'TypeScheme' as a pseudo type
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintPseudoType :: TypeScheme -> Doc
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian MaederprintPseudoType (TypeScheme l t _) = noNullPrint l (lambda
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder <+> (if null $ tail l then pretty $ head l
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder else fsep(map (parens . pretty) l))
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder <+> bullet <> space) <> pretty t
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maederinstance Pretty BasicSpec where
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder pretty (BasicSpec l) = if null l then specBraces empty else
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maeder changeGlobalAnnos addBuiltins . vcat $ map pretty l
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Manceinstance Pretty ProgEq where
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance pretty = printEq0 equals . foldEq printTermRec
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Manceinstance Pretty BasicItem where
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance pretty bi = case bi of
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance SigItems s -> pretty s
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance ProgItems l _ -> sep [keyword programS, semiAnnoted l]
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance ClassItems i l _ -> let b = semiAnnos pretty l in case i of
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance Plain -> topSigKey classS <+> b
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance Instance -> sep [keyword classS <+> keyword instanceS, b]
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance GenVarItems l _ -> topSigKey (varS ++ case l of
c704da29ad5d9d00c07e75f9109442d178dd990bFelix Gabriel Mance _ : _ : _ -> sS
a4a1b0cfce938fc38d5924b8fb6a7e140602ec5cFelix Gabriel Mance _ -> "") <+> printGenVarDecls l
cbb0a924599bcaea230e7dcd2892cc91c49319aeChristian Maeder FreeDatatype l _ ->
52991d9b46a98ad6a9020421a3244950b0f8a522Felix Gabriel Mance sep [keyword freeS <+> keyword (typeS ++ case l of
52991d9b46a98ad6a9020421a3244950b0f8a522Felix Gabriel Mance _ : _ : _ -> sS
52991d9b46a98ad6a9020421a3244950b0f8a522Felix Gabriel Mance _ -> ""), semiAnnos pretty l]
a4a1b0cfce938fc38d5924b8fb6a7e140602ec5cFelix Gabriel Mance GenItems l _ -> let gkw = keyword generatedS in
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance (if all (isDatatype . item) l then \ i -> gkw <+> rmTopKey i
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance else \ i -> sep [gkw, specBraces i])
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance $ vcat $ map (printAnnoted pretty) l
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance AxiomItems vs fs _ ->
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance sep [ if null vs then empty else
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance forallDoc <+> printGenVarDecls vs
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance _ -> let pp = addBullet . pretty in
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance vcat $ map (printAnnoted pp) (init fs)
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance ++ [printSemiAnno pp True $ last fs]]
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance Internal l _ -> sep [keyword internalS,
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance specBraces $ vcat $ map (printAnnoted pretty) l]
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel ManceisDatatype :: SigItems -> Bool
962c83276bc80dd04f4a83e47eb81524d5294a4fChristian MaederisDatatype si = case si of
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance TypeItems _ l _ -> all ((\ t -> case t of
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance Datatype _ -> True
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance _ -> False) . item) l
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceinstance Pretty OpBrand where
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance pretty b = keyword $ show b
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Manceinstance Pretty SigItems where
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance pretty si = case si of
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance TypeItems i l _ -> let b = semiAnnos pretty l in case i of
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance Plain -> topSigKey ((if all (isSimpleTypeItem . item) l
e99c3c1f572d0442872bba58f187ca520ef5d040Felix Gabriel Mance then typeS else typeS) ++ plTypes l) <+> b
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance sep [keyword typeS <+> keyword (instanceS ++ plTypes l), b]
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance OpItems b l _ -> noNullPrint l $ topSigKey (show b ++ plOps l)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance <+> let po = (prettyOpItem $ isPred b) in
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance if case item $ last l of
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance OpDecl _ _ a@(_ : _) _ -> case last a of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance UnitOpAttr {} -> True
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance OpDefn {} -> True
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance then vcat (map (printSemiAnno po True) l)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance else semiAnnos po l
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel ManceplTypes :: [Annoted TypeItem] -> String
962c83276bc80dd04f4a83e47eb81524d5294a4fChristian MaederplTypes l = case map item l of
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance _ : _ : _ -> sS
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance [TypeDecl (_ : _ : _) _ _] -> sS
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance [SubtypeDecl (_ : _ : _) _ _] -> sS