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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
module CurryInterface.Pretty where
import Prelude hiding ( empty )
import Data.List ( intersperse )
import Data.Maybe ( isNothing )
import Text.Pretty
import CurryInterface.Types
data Options = Options
{ optModule :: String
, optInstances :: [IDecl]
, optQualify :: Bool
, optWithString :: Bool
, optWithArity :: Bool
, optWithHiding :: Bool
, optWithInstance :: Bool
, optWithImports :: Bool
, optWithKinds :: Bool
, optIndent :: Int
, optHelp :: Bool
}
defaultOptions :: Options
defaultOptions = Options "" [] True True True True True True True 2 False
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)
ppModuleIdent :: Options -> ModuleIdent -> Doc
ppModuleIdent _ (ModuleIdent ids) =
hcat (punctuate dot (map text ids))
ppImportDecl :: Options -> IImportDecl -> Doc
ppImportDecl options (IImportDecl mident) =
text "import" <+> ppModuleIdent options mident
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
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
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 ids fdeps)
| optWithHiding opts
= text "hiding class" <+> ppContext opts ctx <+>
ppWithOptionalKind opts qualId mkind <+>
hsep (map (ppTypeVariable opts) ids) <+> ppFunDeps opts fdeps
| otherwise = empty
ppDecl opts (IClassDecl ctx qualId mkind ids fdeps mDecls pragmas) =
text "class" <+> ppContext opts ctx <+>
ppWithOptionalKind opts qualId mkind <+>
hsep (map (ppTypeVariable opts) ids) <+> ppFunDeps opts fdeps <+>
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
ppFunDeps :: Options -> [FunDep] -> Doc
ppFunDeps opts fdeps
| null fdeps = empty
| otherwise = text "|" <+> sep (punctuate comma (map (ppFunDep opts) fdeps))
where
ppFunDep :: Options -> FunDep -> Doc
ppFunDep opts' (FunDep lhs rhs) =
sep (map (ppIdent opts' 0) lhs) <+> rarrow <+>
sep (map (ppIdent opts' 0) rhs)
ppArity :: Options -> Arity -> Doc
ppArity _ = int
ppPrecedence :: Options -> Precedence -> Doc
ppPrecedence _ = int
ppInfix :: Options -> Infix -> Doc
ppInfix _ InfixL = text "infixl"
ppInfix _ InfixR = text "infixr"
ppInfix _ Infix = text "infix"
ppIdent :: Options -> Int -> Ident -> Doc
ppIdent _ p (Ident id) =
parensIf (p >= 1 && isOperator id) (text id)
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
ppWithOptionalKind :: Options -> QualIdent -> Maybe KindExpr -> Doc
ppWithOptionalKind opts qualId Nothing = ppQualIdent opts 0 qualId
ppWithOptionalKind opts qualId (Just k)
| optWithKinds opts
= parens (ppQualIdent opts 0 qualId <+> doubleColon <+> ppKindExpr opts 0 k)
| otherwise
= ppQualIdent opts 0 qualId
ppTypeVariable :: Options -> Ident -> Doc
ppTypeVariable opts tvar = ppIdent opts 0 tvar
ppTypeVariables :: Options -> [Ident] -> Doc
ppTypeVariables opts tvars = hsep (map (ppTypeVariable opts) tvars)
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
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
ppConstructors :: Options -> [ConstrDecl] -> Doc
ppConstructors opts constrs =
compose (\d1 d2 -> d1 $$ bar <+> d2) (map (ppConstructor opts) constrs)
ppField :: Options -> FieldDecl -> Doc
ppField opts (FieldDecl ids t) =
(hcat . punctuate dot) (map (ppIdent opts 0) ids) <+>
doubleColon <+> ppType opts 0 t
ppFields :: Options -> [FieldDecl] -> Doc
ppFields opts fields =
lbrace <+>
((hcat . punctuate (text ", ")) (map (ppField opts) fields)) <+>
rbrace
ppModulePragma :: Options -> ModuleIdent -> Doc
ppModulePragma opts mid = lpragma <+> text "MODULE" <+> ppModuleIdent opts mid <+> rpragma
ppHiddenPragma :: Options -> [Ident] -> Doc
ppHiddenPragma opts pragmas = case pragmas of
[] -> empty
_ -> space <> lpragma <+> text "HIDING" <+>
(hsep . punctuate comma) (map (ppIdent opts 0) pragmas) <+> rpragma
ppMethodPragma :: Options -> Ident -> Doc
ppMethodPragma opts id =
lpragma <+> text "METHOD" <+> ppIdent opts 0 id <+> rpragma
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]
_ -> []
ppQualType :: Options -> QualTypeExpr -> Doc
ppQualType opts (QualTypeExpr ctx texp) =
ppContext opts ctx <+> ppType opts 0 texp
ppConstraint :: Options -> Constraint -> Doc
ppConstraint opts (Constraint qualId ts) =
ppQualIdent opts 0 qualId <+> hsep (map (ppType opts 0) ts)
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
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]
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
ppInstance :: Options -> InstanceType -> Doc
ppInstance opts it = hsep $ map (ppType opts 1) it
ppImplementation :: Options -> IMethodImpl -> Doc
ppImplementation opts (id, ari) =
ppIdent opts 1 id <+>
(if optWithArity opts then ppArity opts ari else empty)
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
ppKindExpr :: Options -> Int -> KindExpr -> Doc
ppKindExpr opts p (ArrowKind k1 k2) =
parensIf (p >= 1) (ppKindExpr opts 1 k1 <+> rarrow <+> ppKindExpr opts 0 k2)
ppKindExpr _ _ Star = text "*"
ppKindExpr _ _ ConstraintKind = text "Constraint"
ppMaybe :: (a -> Doc) -> Maybe a -> Doc
ppMaybe _ Nothing = empty
ppMaybe p (Just x) = p x
isOperator :: String -> Bool
isOperator = all (flip elem allowed)
where
allowed = "!#$%&*+./<=>?@\\^|-~:"
lpragma :: Doc
lpragma = text "{-#"
rpragma :: Doc
rpragma = text "-#}"
preludeCharType :: TypeExpr
preludeCharType =
ConstructorType (QualIdent (Just (ModuleIdent ["Prelude"])) (Ident "Char"))
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
|