1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
module CurryInterface.Pretty where

import Prelude hiding ( empty )
import Data.List      ( intersperse )
import Data.Maybe     ( isNothing )
import Text.Pretty

import CurryInterface.Types

--- Options to influence the pretty printing of Curry interfaces.
data Options = Options
  { optModule       :: String  -- name of current module
  , optInstances    :: [IDecl] -- locally defined class instances
  , optQualify      :: Bool    -- show identifiers with module qualifier?
  , optWithString   :: Bool    -- show type `[Char]` as `String`?
  , optWithArity    :: Bool    -- show arity of operations?
  , optWithHiding   :: Bool    -- show `hiding` information?
  , optWithInstance :: Bool    -- show detailed `instance` information?
  , optWithImports  :: Bool    -- show also information about imported entities?
  , optIndent       :: Int     -- the number of columns for indention
  , optHelp         :: Bool    -- show help (used in main tool)
  }

--- The default options for pretty printing: show everything
defaultOptions :: Options
defaultOptions = Options "" [] True True True True True True 2 False

--- pretty-print a Curry interface
ppInterface :: Options -> Interface -> Doc
ppInterface options (Interface mident decls1 decls2) =
  text "interface" <+> ppModuleIdent options mident <+> text "where" <+>
  lbrace <> linebreak <>
  ((vsep . punctuate semi) (pdecls1 ++ pdecls2)) <$$> rbrace
 where
  pdecls1 = filter (not . isEmpty) (map (ppImportDecl options) decls1)
  pdecls2 = filter (not . isEmpty) (map (ppDecl options) decls2)

--- pretty-print a ModuleIdent
ppModuleIdent :: Options -> ModuleIdent -> Doc
ppModuleIdent _ (ModuleIdent ids) =
    hcat (punctuate dot (map text ids))

--- pretty-print an import declaration
ppImportDecl :: Options -> IImportDecl -> Doc
ppImportDecl options (IImportDecl mident) =
    text "import" <+> ppModuleIdent options mident

--- pretty-print a declaration
ppDecl :: Options -> IDecl -> Doc
ppDecl opts (IInfixDecl inf prec qualId) =
  let ppid = ppQualIdent opts 0 qualId
  in ppInfix opts inf <+> ppPrecedence opts prec <+>
     if isOperator (idName (qidIdent qualId)) then ppid else bquotes ppid

ppDecl opts (HidingDataDecl qualId mkind tvars)
  | optWithHiding opts
  = text "hiding data" <+> ppWithOptionalKind opts qualId mkind <+>
    ppTypeVariables opts tvars
  | otherwise = empty

ppDecl opts (IDataDecl qualId mkind tvars constrs pragmas) =
  (if optWithInstance opts
     then ppdata
     else ppdata <$$>
          ppDeriving (filter (isInstanceOf qualId) (optInstances opts)))
 where
  ppdatalhs = text "data" <+> ppWithOptionalKind opts qualId mkind <+>
              ppTypeVariables opts tvars

  ppdata = nest (optIndent opts)
    (case constrs of
       []   -> ppdatalhs
       c:cs -> if optWithHiding opts -- show all details --> with line breaks
                 then ppdatalhs <$$> equals <+> ppConstructors opts constrs
                 else fillSep (ppdatalhs : (equals <+> ppConstructor opts c) :
                               map (bar <+>) (map (ppConstructor opts) cs))) <>
    ppHiddenPragma opts pragmas

  ppDeriving []          = empty
  ppDeriving insts@(_:_) = hang 11 $
    text " deriving" <+>
    parensIf (length insts > 1)
      ((fillSep . punctuate (text ", ")) (map classOf insts))
   where
    classOf idecl = case idecl of
      IInstanceDecl _ qid _ _ _ -> ppQualIdent opts 0 qid
      _                         -> empty -- should not occur

ppDecl opts (INewtypeDecl qualId mkind tvars newconstr pragmas) =
  text "newtype" <+> ppWithOptionalKind opts qualId mkind <+>
  ppTypeVariables opts tvars <+>
  equals <+> ppNewConstructor opts newconstr <> ppHiddenPragma opts pragmas
ppDecl opts (ITypeDecl qualId mkind tvars texp) =
  text "type" <+> ppWithOptionalKind opts qualId mkind <+>
  ppTypeVariables opts tvars <+>
  equals <+> ppType opts 0 texp
ppDecl opts (IFunctionDecl qualId prag ari qualTExp) =
  nest (optIndent opts) $ fillSep
    [ ppQualIdent opts 1 qualId
    , ppMaybe (\x -> space <> ppMethodPragma opts x) prag
    , if optWithArity opts then ppArity opts ari else empty
    , doubleColon, ppQualType opts qualTExp ]
ppDecl opts (HidingClassDecl ctx qualId mkind id)
  | optWithHiding opts
  = text "hiding class" <+> ppContext opts ctx <+>
    ppWithOptionalKind opts qualId mkind <+> ppTypeVariable opts id
  | otherwise = empty
ppDecl opts (IClassDecl ctx qualId mkind id mDecls pragmas) =
  text "class" <+> ppContext opts ctx <+>
  ppWithOptionalKind opts qualId mkind <+> ppTypeVariable opts id <+>
  ppMethodDecls opts mDecls <+> ppHiddenPragma opts pragmas
ppDecl opts (IInstanceDecl ctx qualId itype mImpls mIdent)
  | optWithInstance opts && (optWithImports opts || isNothing mIdent)
  = text "instance" <+> ppContext opts ctx <+> ppQualIdent opts 0 qualId <+>
    ppInstance opts itype <+>
    ppImplementations opts mImpls <>
    ppMaybe (\x -> space <> ppModulePragma opts x) mIdent
  | otherwise = empty

--- pretty-print an arity
ppArity :: Options -> Arity -> Doc
ppArity _ = int

--- pretty-print a precedence
ppPrecedence :: Options -> Precedence -> Doc
ppPrecedence _ = int

--- pretty-print an infix declaration
ppInfix :: Options -> Infix -> Doc
ppInfix _ InfixL = text "infixl"
ppInfix _ InfixR = text "infixr"
ppInfix _ Infix  = text "infix"

--- Pretty-print an Ident. If the second argument is non-zero,
--- operators (i.e., strings with special characters) will be
--- enclosed in parentheses.
ppIdent :: Options -> Int -> Ident -> Doc
ppIdent _ p (Ident id) =
  parensIf (p >= 1 && isOperator id) (text id)

--- pretty-print a QualIdent
ppQualIdent :: Options -> Int -> QualIdent -> Doc
ppQualIdent opts p (QualIdent Nothing id) = ppIdent opts p id
ppQualIdent opts p (QualIdent (Just mident) id)
  | optQualify opts = ppModuleIdent opts mident <> dot <> ppIdent opts p id
  | otherwise       = ppIdent opts p id

--- pretty-print a QualIdent with an optional kind expression
ppWithOptionalKind :: Options -> QualIdent -> Maybe KindExpr -> Doc
ppWithOptionalKind opts qualId Nothing = ppQualIdent opts 0 qualId
ppWithOptionalKind opts qualId (Just k) =
  parens (ppQualIdent opts 0 qualId <+> doubleColon <+> ppKindExpr opts 0 k)

--- pretty-print a type variable
ppTypeVariable :: Options -> Ident -> Doc
ppTypeVariable opts tvar = ppIdent opts 0 tvar

--- pretty-print a list of type variables
ppTypeVariables :: Options -> [Ident] -> Doc
ppTypeVariables opts tvars = hsep (map (ppTypeVariable opts) tvars)

--- pretty-print a newtype constructor declaration
ppNewConstructor :: Options -> NewConstrDecl -> Doc
ppNewConstructor opts (NewConstrDecl id t) =
  ppIdent opts 0 id <+> ppType opts 0 t
ppNewConstructor opts (NewRecordDecl id1 (id2, t)) =
  ppIdent opts 0 id1 <+>
  lbrace <+> (ppIdent opts 0 id2 <+> doubleColon <+> ppType opts 0 t) <+> rbrace

--- pretty-print a constructor declaration
ppConstructor :: Options -> ConstrDecl -> Doc
ppConstructor opts (ConstrDecl id texps) =
  hsep (ppIdent opts 0 id : map (ppType opts 0) texps)
ppConstructor opts (ConOpDecl t1 id t2) =
  ppType opts 0 t1 <+> ppIdent opts 0 id <+> ppType opts 0 t2
ppConstructor opts (RecordDecl id fields) =
  ppIdent opts 0 id <+> ppFields opts fields

--- pretty-print a list of constructor declarations
ppConstructors :: Options -> [ConstrDecl] -> Doc
ppConstructors opts constrs =
  compose (\d1 d2 -> d1 $$ bar <+> d2) (map (ppConstructor opts) constrs)

--- pretty-print a field declaration
ppField :: Options -> FieldDecl -> Doc
ppField opts (FieldDecl ids t) =
  (hcat . punctuate dot) (map (ppIdent opts 0) ids) <+>
  doubleColon <+> ppType opts 0 t

--- pretty-print a list of field declarations
ppFields :: Options -> [FieldDecl] -> Doc
ppFields opts fields =
  lbrace <+>
  ((hcat . punctuate (text ", ")) (map (ppField opts) fields)) <+>
  rbrace

--- pretty-print a module pragma
ppModulePragma :: Options -> ModuleIdent -> Doc
ppModulePragma opts mid = lpragma <+> text "MODULE" <+> ppModuleIdent opts mid <+> rpragma

--- pretty-print a hidden pragma
ppHiddenPragma :: Options -> [Ident] -> Doc
ppHiddenPragma opts pragmas = case pragmas of
    [] -> empty
    _  -> space <> lpragma <+> text "HIDING" <+>
          (hsep . punctuate comma) (map (ppIdent opts 0) pragmas) <+> rpragma

--- pretty-print a method pragma
ppMethodPragma :: Options -> Ident -> Doc
ppMethodPragma opts id =
  lpragma <+> text "METHOD" <+> ppIdent opts 0 id <+> rpragma

--- pretty-print a type declaration
ppType :: Options -> Int -> TypeExpr -> Doc
ppType opts _ (ConstructorType qualId) = ppQualIdent opts 1 qualId
ppType opts _ (VariableType i) = ppIdent opts 0 i
ppType opts _ (TupleType texps) =
  parens ((hcat . punctuate (text ", ")) (map (ppType opts 0) texps))
ppType opts _ (ListType texps)
  | optWithString opts &&
    (optModule opts == "Prelude" && texps == [localCharType] ||
     texps == [preludeCharType])
  = text "String"
  | otherwise
  = brackets ((hcat . punctuate (text ", ")) (map (ppType opts 0) texps))
ppType opts p (ArrowType texp1 texp2) =
  parensIf (p >= 1) (ppType opts (if isArrowType texp1 then 1 else 0) texp1 </>
                     rarrow <+> ppType opts 0 texp2)
 where
  isArrowType te = case te of ArrowType _ _ -> True
                              _             -> False
ppType opts _ (ParenType texp) = parens (ppType opts 0 texp)
ppType _ _ (ForallType _ _) = text "FORALLTYPE"
ppType opts p texp@(ApplyType texp1 texp2) = parensIf (p > 0) $
  maybe (ppType opts 1 texp1 <+> ppType opts 1 texp2)
        (\qid -> (ppQualIdent opts 1 qid <+>
                  hsep (map (ppType opts 0) (argsOfApply texp))))
        (funOfApply texp)
 where
  argsOfApply te = case te of
    ApplyType (ConstructorType _) ta -> [ta]
    ApplyType t1         t2          -> argsOfApply t1 ++ [t2]
    _                                -> [] -- should not occur

--- pretty-print a QualType
ppQualType :: Options -> QualTypeExpr -> Doc
ppQualType opts (QualTypeExpr ctx texp) =
  ppContext opts ctx <+> ppType opts 0 texp

--- pretty-print a constraint
ppConstraint :: Options -> Constraint -> Doc
ppConstraint opts (Constraint qualId texp) =
  ppQualIdent opts 0 qualId <+> ppType opts 0 texp

--- pretty-print a context
ppContext :: Options -> Context -> Doc
ppContext opts ctx = case ctx of
  []       -> empty
  [constr] -> ppConstraint opts constr <+> text "=>"
  _        -> parens ((hcat . punctuate (text ", "))
                        (map (ppConstraint opts) ctx)) <+> doubleArrow

--- pretty-print a method declaration
ppMethodDecl :: Options -> IMethodDecl -> Doc
ppMethodDecl opts (IMethodDecl id mari qualTExp) =
  nest (optIndent opts) $ fillSep
    [ ppIdent opts 1 id
    , if optWithArity opts then ppMaybe (ppArity opts) mari else empty
    , doubleColon, ppQualType opts qualTExp]

--- pretty-print a list of method declarations
ppMethodDecls :: Options -> [IMethodDecl] -> Doc
ppMethodDecls opts mDecls = case mDecls of
  [] -> lbrace <$$> rbrace
  _  -> lbrace <$$>
        (nest (optIndent opts) . indent (optIndent opts))
           ((vsep . punctuate semi) (map (ppMethodDecl opts) mDecls)) <$$>
        rbrace

--- pretty-print an instance
ppInstance :: Options -> InstanceType -> Doc
ppInstance opts it = ppType opts 1 it

--- pretty-print a method implementation
ppImplementation :: Options -> IMethodImpl -> Doc
ppImplementation opts (id, ari) =
  ppIdent opts 1 id <+>
  (if optWithArity opts then ppArity opts ari else empty)

--- pretty-print a list of method implementations
ppImplementations :: Options -> [IMethodImpl] -> Doc
ppImplementations opts mImpls = case mImpls of
  [] -> lbrace <$$> rbrace
  _  -> lbrace <$$>
        (nest (optIndent opts) . indent (optIndent opts))
           ((vsep . punctuate (text "; "))
              (map (ppImplementation opts) mImpls)) <$$> rbrace

--- pretty-print a kind expression
ppKindExpr :: Options -> Int -> KindExpr -> Doc
ppKindExpr _       _ Star = text "*"
ppKindExpr opts p (ArrowKind k1 k2) =
  parensIf (p >= 1) (ppKindExpr opts 1 k1 <+> rarrow <+> ppKindExpr opts 0 k2)

--- HELPER FUNCTIONS
--- pretty-print Just as normal, Nothing as empty
ppMaybe :: (a -> Doc) -> Maybe a -> Doc
ppMaybe _ Nothing = empty
ppMaybe p (Just x) = p x

--- Check if string is an operator
isOperator :: String -> Bool
isOperator = all (flip elem allowed)
 where
  allowed = "!#$%&*+./<=>?@\\^|-~:"

--- pretty-print "{-#"
lpragma :: Doc
lpragma = text "{-#"

--- pretty-print "-#}"
rpragma :: Doc
rpragma = text "-#}"

------------------------------------------------------------------------------
-- Auxiliaries:

--- `Prelude.Char` type.
preludeCharType :: TypeExpr
preludeCharType =
  ConstructorType (QualIdent (Just (ModuleIdent ["Prelude"])) (Ident "Char"))

--- Local `Char` type.
localCharType :: TypeExpr
localCharType = ConstructorType (QualIdent Nothing (Ident "Char"))

isInstanceOf :: QualIdent -> IDecl -> Bool
isInstanceOf qtc idecl = case idecl of
  IInstanceDecl _ _ te _ _  -> case te of
                                 ConstructorType qc -> qc == qtc
                                 ParenType pt -> funOfApply pt == Just qtc
                                 _            -> funOfApply te == Just qtc
  _                         -> False

funOfApply :: TypeExpr -> Maybe QualIdent
funOfApply te = case te of ApplyType (ConstructorType qc) _ -> Just qc
                           ApplyType t1                   _ -> funOfApply t1
                           _                                -> Nothing

------------------------------------------------------------------------------