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
|
module FlatCurry.Pretty where
import Pretty
import FlatCurry.Types
data Options = Options
{ indentWidth :: Int
, qualMode :: QualMode
, currentModule :: String
}
data QualMode = QualNone | QualImportsButPrelude | QualImports | QualAll
defaultOptions :: Options
defaultOptions = Options
{ indentWidth = 2
, qualMode = QualImportsButPrelude
, currentModule = ""
}
ppProg :: Options -> Prog -> Doc
ppProg o (Prog m is ts fs os) = vsepBlank
[ ppHeader o' m ts fs
, ppImports o' is
, ppOpDecls o' os
, ppTypeDecls o' ts
, ppFuncDecls o' fs
]
where o' = o { currentModule = m }
ppHeader :: Options -> String -> [TypeDecl] -> [FuncDecl] -> Doc
o m ts fs = indent o $
sep [text "module" <+> text m, ppExports o ts fs, text "where"]
ppExports :: Options -> [TypeDecl] -> [FuncDecl] -> Doc
ppExports o ts fs = tupledSpaced (map (ppTypeExport o) ts ++ ppFuncExports o fs)
ppTypeExport :: Options -> TypeDecl -> Doc
ppTypeExport o (Type qn vis _ cs)
| vis == Private = empty
| null cs = ppPrefixQOp o qn
| all isPublicCons cs = ppPrefixQOp o qn <+> text "(..)"
| otherwise = ppPrefixQOp o qn <+> tupled (ppConsExports o cs)
where isPublicCons (Cons _ _ v _) = v == Public
ppTypeExport o (TypeSyn qn vis _ _ )
| vis == Private = empty
| otherwise = ppPrefixQOp o qn
ppConsExports :: Options -> [ConsDecl] -> [Doc]
ppConsExports o cs = [ ppPrefixQOp o qn | Cons qn _ Public _ <- cs]
ppFuncExports :: Options -> [FuncDecl] -> [Doc]
ppFuncExports o fs = [ ppPrefixQOp o qn | Func qn _ Public _ _ <- fs]
ppImports :: Options -> [String] -> Doc
ppImports o = vsep . map (ppImport o)
ppImport :: Options -> String -> Doc
ppImport o m = indent o $ text "import" <+> text m
ppOpDecls :: Options -> [OpDecl] -> Doc
ppOpDecls o = vsep . map (ppOpDecl o)
ppOpDecl :: Options -> OpDecl -> Doc
ppOpDecl o (Op qn fix n) = indent o $ ppFixity fix <+> int n <+> ppInfixQOp o qn
ppFixity :: Fixity -> Doc
ppFixity InfixOp = text "infix"
ppFixity InfixlOp = text "infixl"
ppFixity InfixrOp = text "infixr"
ppTypeDecls :: Options -> [TypeDecl] -> Doc
ppTypeDecls o = vsepBlank . map (ppTypeDecl o)
ppTypeDecl :: Options -> TypeDecl -> Doc
ppTypeDecl o (Type qn _ vs cs) = indent o $ (text "data" <+> ppName qn
<+> hsep (empty : map ppTVarIndex vs)) $$ ppConsDecls o cs
ppTypeDecl o (TypeSyn qn _ vs ty) = indent o $ text "type" <+> ppName qn
<+> hsep (empty : map ppTVarIndex vs) </> equals <+> ppTypeExp o ty
ppConsDecls :: Options -> [ConsDecl] -> Doc
ppConsDecls o cs = vsep $ zipWith (<+>) (equals : repeat bar)
(map (ppConsDecl o) cs)
ppConsDecl :: Options -> ConsDecl -> Doc
ppConsDecl o (Cons qn _ _ tys) = hsep $ ppPrefixOp qn : map (ppTypeExpr o 2) tys
ppTypeExp :: Options -> TypeExpr -> Doc
ppTypeExp o = ppTypeExpr o 0
ppTypeExpr :: Options -> Int -> TypeExpr -> Doc
ppTypeExpr _ _ (TVar v) = ppTVarIndex v
ppTypeExpr o p (FuncType ty1 ty2) = parensIf (p > 0) $
ppTypeExpr o 1 ty1 </> rarrow <+> ppTypeExp o ty2
ppTypeExpr o p (TCons qn tys)
| isListId qn && length tys == 1 = brackets (ppTypeExp o (head tys))
| isTupleId qn = tupled (map (ppTypeExp o) tys)
| otherwise = parensIf (p > 1 && not (null tys)) $ sep
(ppPrefixQOp o qn : map (ppTypeExpr o 2) tys)
ppTVarIndex :: TVarIndex -> Doc
ppTVarIndex i = text $ vars !! i
where vars = [ chr c : if n == 0 then [] else show n
| n <- [0 ..], c <- [ord 'a' .. ord 'z']
]
ppFuncDecls :: Options -> [FuncDecl] -> Doc
ppFuncDecls o = vsepBlank . map (ppFuncDecl o)
ppFuncDecl :: Options -> FuncDecl -> Doc
ppFuncDecl o (Func qn _ _ ty r)
= indent o (sep [ppPrefixOp qn, text "::", ppTypeExp o ty])
$$ indent o (ppPrefixOp qn <+> ppRule o r)
ppRule :: Options -> Rule -> Doc
ppRule o (Rule vs e)
| null vs = equals <+> ppExp o e
| otherwise = hsep (map ppVarIndex vs) </> equals <+> ppExp o e
ppRule _ (External e) = text "external" <+> dquotes (text e)
ppExp :: Options -> Expr -> Doc
ppExp o = ppExpr o 0
ppExpr :: Options -> Int -> Expr -> Doc
ppExpr _ _ (Var v) = ppVarIndex v
ppExpr _ _ (Lit l) = ppLiteral l
ppExpr o p (Comb _ qn es) = ppComb o p qn es
ppExpr o p (Free vs e)
| null vs = ppExpr o p e
| otherwise = parensIf (p > 0) $ sep
[ text "let"
<+> sep (punctuate comma (map ppVarIndex vs))
<+> text "free"
, text "in" </> ppExp o e
]
ppExpr o p (Let ds e) = parensIf (p > 0) $ sep
[ text "let" <+> ppDecls o ds
, text "in" <+> ppExp o e
]
ppExpr o p (Or e1 e2) = parensIf (p > 0)
$ ppExpr o 1 e1 <+> text "?" <+> ppExpr o 1 e2
ppExpr o p (Case ct e bs) = parensIf (p > 0) $ indent o
$ ppCaseType ct <+> ppExpr o 1 e <+> text "of"
$$ vsep (map (ppBranch o) bs)
ppExpr o p (Typed e ty) = parensIf (p > 0)
$ ppExp o e <+> text "::" <+> ppTypeExp o ty
ppVarIndex :: VarIndex -> Doc
ppVarIndex i | i < 0 = text $ 'x' : show (negate i)
| otherwise = text $ 'v' : show i
ppLiteral :: Literal -> Doc
ppLiteral (Intc i) = int i
ppLiteral (Floatc f) = float f
ppLiteral (Charc c) = text (show c)
ppComb :: Options -> Int -> QName -> [Expr] -> Doc
ppComb o p qn es | isListId qn && null es = text "[]"
| isTupleId qn = tupled (map (ppExp o) es)
| otherwise = case es of
[] -> ppPrefixQOp o qn
[e1,e2]
| isInfixOp qn -> parensIf (p > 0)
$ fillSep [ppExpr o 1 e1, ppInfixQOp o qn, ppExpr o 1 e2]
_ -> parensIf (p > 0)
$ fillSep (ppPrefixQOp o qn : map (ppExpr o 1) es)
ppDecls :: Options -> [(VarIndex, Expr)] -> Doc
ppDecls o = align . vsep . map (ppDecl o)
ppDecl :: Options -> (VarIndex, Expr) -> Doc
ppDecl o (v, e) = ppVarIndex v <+> equals <+> ppExp o e
ppCaseType :: CaseType -> Doc
ppCaseType Rigid = text "case"
ppCaseType Flex = text "fcase"
ppBranch :: Options -> BranchExpr -> Doc
ppBranch o (Branch p e) = ppPattern o p <+> rarrow <+> indent o (ppExp o e)
ppPattern :: Options -> Pattern -> Doc
ppPattern o (Pattern c vs)
| isListId c && null vs = text "[]"
| isTupleId c = tupled (map ppVarIndex vs)
| otherwise = case vs of
[v1,v2] | isInfixOp c -> ppVarIndex v1 <+> ppInfixQOp o c <+> ppVarIndex v2
_ -> hsep (ppPrefixQOp o c : map ppVarIndex vs)
ppPattern _ (LPattern l) = ppLiteral l
ppPrefixQOp :: Options -> QName -> Doc
ppPrefixQOp o qn = parensIf (isInfixOp qn) (ppQName o qn)
ppPrefixOp :: QName -> Doc
ppPrefixOp qn = parensIf (isInfixOp qn) (ppName qn)
ppInfixQOp :: Options -> QName -> Doc
ppInfixQOp o qn = if isInfixOp qn then ppQName o qn else bquotes (ppQName o qn)
ppQName :: Options -> QName -> Doc
ppQName o qn@(m, i)
| null m = text i
| isConsId qn || isListId qn || isTupleId qn = text i
| q == QualNone = text i
| q == QualImportsButPrelude
&& (m == m' || m == "Prelude") = text i
| q == QualImports && m == m' = text i
| otherwise = text $ m ++ '.' : i
where
q = qualMode o
m' = currentModule o
ppName :: QName -> Doc
ppName (_, i) = text i
isInfixOp :: QName -> Bool
isInfixOp = all (`elem` "~!@#$%^&*+-=<>:?./|\\") . snd
isConsId :: QName -> Bool
isConsId (m, i) = m `elem` ["Prelude", ""] && i == ":"
isListId :: QName -> Bool
isListId (m, i) = m `elem` ["Prelude", ""] && i == "[]"
isTupleId :: QName -> Bool
isTupleId (m, i) = m `elem` ["Prelude", ""] && i == mkTuple (length i)
where mkTuple n = '(' : replicate (n - 2) ',' ++ ")"
indent :: Options -> Doc -> Doc
indent o d = nest (indentWidth o) d
|