ShowModule.hs 15.3 KB
Newer Older
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
{- |Transform a CurrySyntax module into a string representation without any
    pretty printing.
    Behaves like a derived Show instance even on parts with a specific one.

    @author Sebastian Fischer (sebf@informatik.uni-kiel.de)
    @version December 2008
    bug fixed by bbr
-}
module Curry.Syntax.ShowModule (showModule) where

import Curry.Base.Ident
import Curry.Base.Position

import Curry.Syntax.Type

showModule :: Module -> String
showModule m = showsModule m "\n"

showsModule :: Module -> ShowS
showsModule (Module mident espec decls)
  = showsString "Module "
  . showsModuleIdent mident . newline
  . showsMaybe showsExportSpec espec . newline
  . showsList (\d -> showsDecl d . newline) decls

showsPosition :: Position -> ShowS
showsPosition Position { line = l, column = c } = showsPair shows shows (l, c)
showsPosition _ = id
-- showsPosition (Position file row col)
--   = showsString "(Position "
--   . shows file . space
--   . shows row . space
--   . shows col
--   . showsString ")"

showsExportSpec :: ExportSpec -> ShowS
showsExportSpec (Exporting pos exports)
  = showsString "(Exporting "
  . showsPosition pos . space
  . showsList showsExport exports
  . showsString ")"

showsExport :: Export -> ShowS
showsExport (Export qident)
  = showsString "(Export " . showsQualIdent qident . showsString ")"
showsExport (ExportTypeWith qident ids)
  = showsString "(ExportTypeWith "
  . showsQualIdent qident . space
  . showsList showsIdent ids
  . showsString ")"
showsExport (ExportTypeAll qident)
  = showsString "(ExportTypeAll " . showsQualIdent qident . showsString ")"
showsExport (ExportModule m)
  = showsString "(ExportModule " . showsModuleIdent m . showChar ')'

showsImportSpec :: ImportSpec -> ShowS
showsImportSpec (Importing pos imports)
  = showsString "(Importing "
  . showsPosition pos . space
  . showsList showsImport imports
  . showsString ")"
showsImportSpec (Hiding pos imports)
  = showsString "(Hiding "
  . showsPosition pos . space
  . showsList showsImport imports
  . showsString ")"

showsImport :: Import -> ShowS
showsImport (Import ident)
  = showsString "(Import " . showsIdent ident . showsString ")"
showsImport (ImportTypeWith ident idents)
  = showsString "(ImportTypeWith "
  . showsIdent ident . space
  . showsList showsIdent idents
  . showsString ")"
showsImport (ImportTypeAll ident)
  = showsString "(ImportTypeAll " . showsIdent ident . showsString ")"

showsDecl :: Decl -> ShowS
showsDecl (ImportDecl pos mident quali mmident mimpspec)
  = showsString "(ImportDecl "
  . showsPosition pos . space
  . showsModuleIdent mident . space
  . shows quali . space
  . showsMaybe showsModuleIdent mmident . space
  . showsMaybe showsImportSpec mimpspec
  . showsString ")"
showsDecl (InfixDecl pos infx prec idents)
  = showsString "(InfixDecl "
  . showsPosition pos . space
  . shows infx . space
  . shows prec . space
  . showsList showsIdent idents
  . showsString ")"
showsDecl (DataDecl pos ident idents consdecls)
  = showsString "(DataDecl "
  . showsPosition pos . space
  . showsIdent ident . space
  . showsList showsIdent idents . space
  . showsList showsConsDecl consdecls
  . showsString ")"
showsDecl (NewtypeDecl pos ident idents newconsdecl)
  = showsString "(NewtypeDecl "
  . showsPosition pos . space
  . showsIdent ident . space
  . showsList showsIdent idents . space
  . showsNewConsDecl newconsdecl
  . showsString ")"
showsDecl (TypeDecl pos ident idents typ)
  = showsString "(TypeDecl "
  . showsPosition pos . space
  . showsIdent ident . space
  . showsList showsIdent idents . space
  . showsTypeExpr typ
  . showsString ")"
showsDecl (TypeSig pos idents typ)
  = showsString "(TypeSig "
  . showsPosition pos . space
  . showsList showsIdent idents . space
  . showsTypeExpr typ
  . showsString ")"
showsDecl (EvalAnnot pos idents annot)
  = showsString "(EvalAnnot "
  . showsPosition pos . space
  . showsList showsIdent idents . space
  . shows annot
  . showsString ")"
showsDecl (FunctionDecl pos ident eqs)
  = showsString "(FunctionDecl "
  . showsPosition pos . space
  . showsIdent ident . space
  . showsList showsEquation eqs
  . showsString ")"
showsDecl (ExternalDecl pos cconv mstr ident typ)
  = showsString "(ExternalDecl "
  . showsPosition pos . space
  . shows cconv . space
  . shows mstr . space
  . showsIdent ident . space
  . showsTypeExpr typ
  . showsString ")"
showsDecl (FlatExternalDecl pos idents)
  = showsString "(FlatExternalDecl "
  . showsPosition pos . space
  . showsList showsIdent idents
  . showsString ")"
showsDecl (PatternDecl pos cons rhs)
  = showsString "(PatternDecl "
  . showsPosition pos . space
  . showsConsTerm cons . space
  . showsRhs rhs
  . showsString ")"
showsDecl (ExtraVariables pos idents)
  = showsString "(ExtraVariables "
  . showsPosition pos . space
  . showsList showsIdent idents
  . showsString ")"

showsConsDecl :: ConstrDecl -> ShowS
showsConsDecl (ConstrDecl pos idents ident types)
  = showsString "(ConstrDecl "
  . showsPosition pos . space
  . showsList showsIdent idents . space
  . showsIdent ident . space
  . showsList showsTypeExpr types
  . showsString ")"
showsConsDecl (ConOpDecl pos idents ty1 ident ty2)
  = showsString "(ConOpDecl "
  . showsPosition pos . space
  . showsList showsIdent idents . space
  . showsTypeExpr ty1 . space
  . showsIdent ident . space
  . showsTypeExpr ty2
  . showsString ")"

showsNewConsDecl :: NewConstrDecl -> ShowS
showsNewConsDecl (NewConstrDecl pos idents ident typ)
  = showsString "(NewConstrDecl "
  . showsPosition pos . space
  . showsList showsIdent idents . space
  . showsIdent ident . space
  . showsTypeExpr typ
  . showsString ")"

showsTypeExpr :: TypeExpr -> ShowS
showsTypeExpr (ConstructorType qident types)
  = showsString "(ConstructorType "
  . showsQualIdent qident . space
  . showsList showsTypeExpr types
  . showsString ")"
showsTypeExpr (VariableType ident)
  = showsString "(VariableType " . showsIdent ident . showsString ")"
showsTypeExpr (TupleType types)
  = showsString "(TupleType " . showsList showsTypeExpr types . showsString ")"
showsTypeExpr (ListType typ)
  = showsString "(ListType " . showsTypeExpr typ . showsString ")"
showsTypeExpr (ArrowType dom ran)
  = showsString "(ArrowType "
  . showsTypeExpr dom . space
  . showsTypeExpr ran
  . showsString ")"
showsTypeExpr (RecordType fieldts mtyp)
  = showsString "(RecordType "
  . showsList (showsPair (showsList showsIdent) showsTypeExpr) fieldts . space
  . showsMaybe showsTypeExpr mtyp
  . showsString ")"

showsEquation :: Equation -> ShowS
showsEquation (Equation pos lhs rhs)
  = showsString "(Equation "
  . showsPosition pos . space
  . showsLhs lhs . space
  . showsRhs rhs
  . showsString ")"

showsLhs :: Lhs -> ShowS
showsLhs (FunLhs ident conss)
  = showsString "(FunLhs "
  . showsIdent ident . space
  . showsList showsConsTerm conss
  . showsString ")"
showsLhs (OpLhs cons1 ident cons2)
  = showsString "(OpLhs "
  . showsConsTerm cons1 . space
  . showsIdent ident . space
  . showsConsTerm cons2
  . showsString ")"
showsLhs (ApLhs lhs conss)
  = showsString "(ApLhs "
  . showsLhs lhs . space
  . showsList showsConsTerm conss
  . showsString ")"

showsRhs :: Rhs -> ShowS
showsRhs (SimpleRhs pos expr decls)
  = showsString "(SimpleRhs "
  . showsPosition pos . space
  . showsExpression expr . space
  . showsList showsDecl decls
  . showsString ")"
showsRhs (GuardedRhs cexps decls)
  = showsString "(GuardedRhs "
  . showsList showsCondExpr cexps . space
  . showsList showsDecl decls
  . showsString ")"

showsCondExpr :: CondExpr -> ShowS
showsCondExpr (CondExpr pos exp1 exp2)
  = showsString "(CondExpr "
  . showsPosition pos . space
  . showsExpression exp1 . space
  . showsExpression exp2
  . showsString ")"

showsLiteral :: Literal -> ShowS
showsLiteral (Char _ c) = showsString "(Char " . shows c . showsString ")"
showsLiteral (Int ident n)
  = showsString "(Int "
  . showsIdent ident . space
  . shows n
  . showsString ")"
showsLiteral (Float _ x) = showsString "(Float " . shows x . showsString ")"
showsLiteral (String _ s) = showsString "(String " . shows s . showsString ")"

showsConsTerm :: ConstrTerm -> ShowS
showsConsTerm (LiteralPattern lit)
  = showsString "(LiteralPattern "
  . showsLiteral lit
  . showsString ")"
showsConsTerm (NegativePattern ident lit)
  = showsString "(NegativePattern "
  . showsIdent ident . space
  . showsLiteral lit
  . showsString ")"
showsConsTerm (VariablePattern ident)
  = showsString "(VariablePattern "
  . showsIdent ident
  . showsString ")"
showsConsTerm (ConstructorPattern qident conss)
  = showsString "(ConstructorPattern "
  . showsQualIdent qident . space
  . showsList showsConsTerm conss
  . showsString ")"
showsConsTerm (InfixPattern cons1 qident cons2)
  = showsString "(InfixPattern "
  . showsConsTerm cons1 . space
  . showsQualIdent qident . space
  . showsConsTerm cons2
  . showsString ")"
showsConsTerm (ParenPattern cons)
  = showsString "(ParenPattern "
  . showsConsTerm cons
  . showsString ")"
showsConsTerm (TuplePattern _ conss)
  = showsString "(TuplePattern "
  . showsList showsConsTerm conss
  . showsString ")"
showsConsTerm (ListPattern _ conss)
  = showsString "(ListPattern "
  . showsList showsConsTerm conss
  . showsString ")"
showsConsTerm (AsPattern ident cons)
  = showsString "(AsPattern "
  . showsIdent ident . space
  . showsConsTerm cons
  . showsString ")"
showsConsTerm (LazyPattern _ cons)
  = showsString "(LazyPattern "
  . showsConsTerm cons
  . showsString ")"
showsConsTerm (FunctionPattern qident conss)
  = showsString "(FunctionPattern "
  . showsQualIdent qident . space
  . showsList showsConsTerm conss
  . showsString ")"
showsConsTerm (InfixFuncPattern cons1 qident cons2)
  = showsString "(InfixFuncPattern "
  . showsConsTerm cons1 . space
  . showsQualIdent qident . space
  . showsConsTerm cons2
  . showsString ")"
showsConsTerm (RecordPattern cfields mcons)
  = shows "(RecordPattern "
  . showsList (showsField showsConsTerm) cfields . space
  . showsMaybe showsConsTerm mcons
  . showsString ")"

showsExpression :: Expression -> ShowS
showsExpression (Literal lit)
  = showsString "(Literal " . showsLiteral lit . showsString ")"
showsExpression (Variable qident)
  = showsString "(Variable " . showsQualIdent qident . showsString ")"
showsExpression (Constructor qident)
  = showsString "(Constructor " . showsQualIdent qident . showsString ")"
showsExpression (Paren expr)
  = showsString "(Paren " . showsExpression expr . showsString ")"
showsExpression (Typed expr typ)
  = showsString "(Typed "
  . showsExpression expr . space
  . showsTypeExpr typ
  . showsString ")"
showsExpression (Tuple _ exps)
  = showsString "(Tuple " . showsList showsExpression exps . showsString ")"
showsExpression (List _ exps)
  = showsString "(List " . showsList showsExpression exps . showsString ")"
showsExpression (ListCompr _ expr stmts)
  = showsString "(ListCompr "
  . showsExpression expr . space
  . showsList showsStatement stmts
  . showsString ")"
showsExpression (EnumFrom expr)
  = showsString "(EnumFrom " . showsExpression expr . showsString ")"
showsExpression (EnumFromThen exp1 exp2)
  = showsString "(EnumFromThen "
  . showsExpression exp1 . space
  . showsExpression exp2
  . showsString ")"
showsExpression (EnumFromTo exp1 exp2)
  = showsString "(EnumFromTo "
  . showsExpression exp1 . space
  . showsExpression exp2
  . showsString ")"
showsExpression (EnumFromThenTo exp1 exp2 exp3)
  = showsString "(EnumFromThenTo "
  . showsExpression exp1 . space
  . showsExpression exp2 . space
  . showsExpression exp3
  . showsString ")"
showsExpression (UnaryMinus ident expr)
  = showsString "(UnaryMinus "
  . showsIdent ident . space
  . showsExpression expr
  . showsString ")"
showsExpression (Apply exp1 exp2)
  = showsString "(Apply "
  . showsExpression exp1 . space
  . showsExpression exp2
  . showsString ")"
showsExpression (InfixApply exp1 op exp2)
  = showsString "(InfixApply "
  . showsExpression exp1 . space
  . showsInfixOp op . space
  . showsExpression exp2
  . showsString ")"
showsExpression (LeftSection expr op)
  = showsString "(LeftSection "
  . showsExpression expr . space
  . showsInfixOp op
  . showsString ")"
showsExpression (RightSection op expr)
  = showsString "(RightSection "
  . showsInfixOp op . space
  . showsExpression expr
  . showsString ")"
showsExpression (Lambda _ conss expr)
  = showsString "(Lambda "
  . showsList showsConsTerm conss . space
  . showsExpression expr
  . showsString ")"
showsExpression (Let decls expr)
  = showsString "(Let "
  . showsList showsDecl decls . space
  . showsExpression expr
  . showsString ")"
showsExpression (Do stmts expr)
  = showsString "(Do "
  . showsList showsStatement stmts . space
  . showsExpression expr
  . showsString ")"
showsExpression (IfThenElse _ exp1 exp2 exp3)
  = showsString "(IfThenElse "
  . showsExpression exp1 . space
  . showsExpression exp2 . space
  . showsExpression exp3
  . showsString ")"
showsExpression (Case _ expr alts)
  = showsString "(Case "
  . showsExpression expr . space
  . showsList showsAlt alts
  . showsString ")"
showsExpression (RecordConstr efields)
  = showsString "(RecordConstr "
  . showsList (showsField showsExpression) efields
  . showsString ")"
showsExpression (RecordSelection expr ident)
  = showsString "(RecordSelection "
  . showsExpression expr . space
  . showsIdent ident
  . showsString ")"
showsExpression (RecordUpdate efields expr)
  = showsString "(RecordUpdate "
  . showsList (showsField showsExpression) efields . space
  . showsExpression expr
  . showsString ")"

showsInfixOp :: InfixOp -> ShowS
showsInfixOp (InfixOp qident)
  = showsString "(InfixOp " . showsQualIdent qident . showsString ")"
showsInfixOp (InfixConstr qident)
  = showsString "(InfixConstr " . showsQualIdent qident . showsString ")"

showsStatement :: Statement -> ShowS
showsStatement (StmtExpr _ expr)
  = showsString "(StmtExpr " . showsExpression expr . showsString ")"
showsStatement (StmtDecl decls)
  = showsString "(StmtDecl " . showsList showsDecl decls . showsString ")"
showsStatement (StmtBind _ cons expr)
  = showsString "(StmtBind "
  . showsConsTerm cons . space
  . showsExpression expr
  . showsString ")"

showsAlt :: Alt -> ShowS
showsAlt (Alt pos cons rhs)
  = showsString "(Alt "
  . showsPosition pos . space
  . showsConsTerm cons . space
  . showsRhs rhs
  . showsString ")"

showsField :: (a -> ShowS) -> Field a -> ShowS
showsField sa (Field pos ident a)
  = showsString "(Field "
  . showsPosition pos . space
  . showsIdent ident . space
  . sa a
  . showsString ")"

showsString :: String -> ShowS
showsString = (++)

space :: ShowS
space = showsString " "

newline :: ShowS
newline = showsString "\n"

showsMaybe :: (a -> ShowS) -> Maybe a -> ShowS
showsMaybe shs
  = maybe (showsString "Nothing")
          (\x -> showsString "(Just " . shs x . showsString ")")

showsList :: (a -> ShowS) -> [a] -> ShowS
showsList _ [] = showsString "[]"
showsList shs (x:xs)
  = showsString "["
  . foldl (\sys y -> sys . showsString "," . shs y) (shs x) xs
  . showsString "]"

showsPair :: (a -> ShowS) -> (b -> ShowS) -> (a,b) -> ShowS
showsPair sa sb (a,b)
  = showsString "(" . sa a . showsString "," . sb b . showsString ")"


showsIdent :: Ident -> ShowS
showsIdent (Ident _ idName n)
  = showsString "(Ident " . shows idName . space . shows n . showsString ")"

showsQualIdent :: QualIdent -> ShowS
showsQualIdent (QualIdent mident ident)
    = showsString "(QualIdent "
      . showsMaybe showsModuleIdent mident
      . space
      . showsIdent ident
      . showsString ")"

showsModuleIdent :: ModuleIdent -> ShowS
showsModuleIdent = shows . moduleName