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
|
module FlatCurry.Typed.Pretty where
import Prelude hiding ( empty )
import Text.Pretty
import FlatCurry.Typed.Types
ppProg :: TProg -> Doc
ppProg (TProg m is ts fs os) = compose (<$+$>)
[ ppHeader m ts fs
, ppImports is
, ppOpDecls os
, ppTypeDecls ts
, ppFuncDecls fs
]
ppHeader :: String -> [TypeDecl] -> [TFuncDecl] -> Doc
m ts fs = indent $
sep [text "module" <+> text m, ppExports ts fs, text "where"]
ppExports :: [TypeDecl] -> [TFuncDecl] -> Doc
ppExports ts fs = tupledSpaced (map ppTypeExport ts ++ ppFuncExports fs)
ppTypeExport :: TypeDecl -> Doc
ppTypeExport (Type qn vis _ cs)
| vis == Private = empty
| all isPublicCons cs = ppPrefixOp qn <+> text "(..)"
| otherwise = ppPrefixOp qn <+> tupled (ppConsExports cs)
where isPublicCons (Cons _ _ v _) = v == Public
ppTypeExport (TypeSyn qn vis _ _ )
| vis == Private = empty
| otherwise = ppPrefixOp qn
ppTypeExport (TypeNew qn vis _ (NewCons _ vis' _))
| vis == Private || vis' == Private = empty
| otherwise = ppPrefixOp qn <+> text "(..)"
ppConsExports :: [ConsDecl] -> [Doc]
ppConsExports cs = [ ppPrefixOp qn | Cons qn _ Public _ <- cs]
ppFuncExports :: [TFuncDecl] -> [Doc]
ppFuncExports fs = [ ppPrefixOp qn | TFunc qn _ Public _ _ <- fs]
ppImports :: [String] -> Doc
ppImports = vsep . map ppImport
ppImport :: String -> Doc
ppImport m = indent $ text "import" <+> text m
ppOpDecls :: [OpDecl] -> Doc
ppOpDecls = vsep . map ppOpDecl
ppOpDecl :: OpDecl -> Doc
ppOpDecl (Op qn fix n) = indent $ ppFixity fix <+> int n <+> ppInfixOp qn
ppFixity :: Fixity -> Doc
ppFixity InfixOp = text "infix"
ppFixity InfixlOp = text "infixl"
ppFixity InfixrOp = text "infixr"
ppTypeDecls :: [TypeDecl] -> Doc
ppTypeDecls = compose (<$+$>) . map ppTypeDecl
ppTypeDecl :: TypeDecl -> Doc
ppTypeDecl (Type qn _ vs cs) = indent $ text "data" <+> ppQName qn
<+> hsep (map (ppTVarIndex . fst) vs) <$$> ppConsDecls cs
ppTypeDecl (TypeSyn qn _ vs ty) = indent $ text "type" <+> ppQName qn
<+> hsep (map (ppTVarIndex . fst) vs) </> equals <+> ppTypeExp ty
ppTypeDecl (TypeNew qn _ vs c) = indent $ text "newtype" <+> ppQName qn
<+> hsep (empty : map (ppTVarIndex . fst) vs) $$ ppNewConsDecl c
ppConsDecls :: [ConsDecl] -> Doc
ppConsDecls cs = vsep $
zipWith (<+>) (equals : repeat bar) (map ppConsDecl cs)
ppConsDecl :: ConsDecl -> Doc
ppConsDecl (Cons qn _ _ tys) = hsep $ ppPrefixOp qn : map (ppTypeExpr 2) tys
ppNewConsDecl :: NewConsDecl -> Doc
ppNewConsDecl (NewCons qn _ ty) = hsep [ppPrefixOp qn, ppTypeExpr 2 ty]
ppTypeExp :: TypeExpr -> Doc
ppTypeExp = ppTypeExpr 0
ppTypeExpr :: Int -> TypeExpr -> Doc
ppTypeExpr _ (TVar v) = ppTVarIndex v
ppTypeExpr p (FuncType ty1 ty2) = parensIf (p > 0) $
ppTypeExpr 1 ty1 </> rarrow <+> ppTypeExp ty2
ppTypeExpr p (TCons qn tys)
| isListId qn && length tys == 1 = brackets (ppTypeExp (head tys))
| isTupleId qn = tupled (map ppTypeExp tys)
| otherwise = parensIf (p > 1 && not (null tys)) $ sep
(ppPrefixOp qn : map (ppTypeExpr 2) tys)
ppTypeExpr _ (ForallType _ _) = error $ "FlatCurry.Typed.Pretty.ppTypeExp: " ++
"ForallType in TypedFlatCurry"
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 :: [TFuncDecl] -> Doc
ppFuncDecls = compose (<$+$>) . map ppFuncDecl
ppFuncDecl :: TFuncDecl -> Doc
ppFuncDecl (TFunc qn _ _ ty r)
= indent (sep [ppPrefixOp qn, text "::", ppTypeExp ty])
<$$> indent (ppPrefixOp qn <+> ppRule r)
ppRule :: TRule -> Doc
ppRule (TRule vs e)
| null vs = equals <+> ppExp e
| otherwise = hsep (map ppTVarEIndex vs) </> equals <+> ppExp e
ppRule (TExternal _ e) = text "external" <+> dquotes (text e)
ppExp :: TExpr -> Doc
ppExp = ppExpr 0
ppExpr :: Int -> TExpr -> Doc
ppExpr _ (TVarE _ v) = ppVarEIndex v
ppExpr _ (TLit _ l) = ppLiteral l
ppExpr p (TComb _ _ qn es) = ppComb p qn es
ppExpr p (TFree vs e)
| null vs = ppExpr p e
| otherwise = parensIf (p > 0) $ sep
[ text "let"
<+> encloseSep empty empty comma
(map ppTVarEIndex vs)
<+> text "free"
, text "in" </> ppExp e
]
ppExpr p (TLet ds e) = parensIf (p > 0) $ sep
[text "let" <+> ppDecls ds, text "in" <+> ppExp e]
ppExpr p (TOr e1 e2) = parensIf (p > 0)
$ ppExpr 1 e1 <+> text "?" <+> ppExpr 1 e2
ppExpr p (TCase ct e bs) = parensIf (p > 0) $ indent
$ ppCaseType ct <+> ppExpr 1 e <+> text "of"
<$$> vsep (map ppBranch bs)
ppExpr p (TTyped e ty) = parensIf (p > 0)
$ ppExp e <+> text "::" <+> ppTypeExp ty
ppTVarEIndex :: (VarIndex, _) -> Doc
ppTVarEIndex (i, _) | i < 0 = text $ 'x' : show (negate i)
| otherwise = text $ 'v' : show i
ppVarEIndex :: VarIndex -> Doc
ppVarEIndex 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 (showEscape c)
showEscape :: Char -> String
showEscape c
| o < 10 = "'\\00" ++ show o ++ "'"
| o < 32 = "'\\0" ++ show o ++ "'"
| o == 127 = "'\\127'"
| otherwise = show c
where o = ord c
ppComb :: Int -> QName -> [TExpr] -> Doc
ppComb p qn es | isListId qn && null es = text "[]"
| isTupleId qn = tupled (map ppExp es)
| otherwise = case es of
[] -> ppPrefixOp qn
[e1,e2]
| isInfixOp qn -> parensIf (p > 0)
$ sep [ppExpr 1 e1, ppInfixOp qn, ppExpr 1 e2]
_ -> parensIf (p > 0)
$ sep (ppPrefixOp qn : map (ppExpr 1) es)
ppDecls :: [((VarIndex, _), TExpr)] -> Doc
ppDecls = semiBracesSpaced . map ppDecl
ppDecl :: ((VarIndex, _), TExpr) -> Doc
ppDecl (v, e) = ppTVarEIndex v <+> equals <+> ppExp e
ppCaseType :: CaseType -> Doc
ppCaseType Rigid = text "case"
ppCaseType Flex = text "fcase"
ppBranch :: TBranchExpr -> Doc
ppBranch (TBranch p e) = ppPattern p <+> rarrow <+> indent (ppExp e)
ppPattern :: TPattern -> Doc
ppPattern (TPattern _ c vs)
| isListId c && null vs = text "[]"
| isTupleId c = tupled (map ppTVarEIndex vs)
| otherwise = case vs of
[v1,v2] | isInfixOp c -> ppTVarEIndex v1 <+> ppInfixOp c <+> ppTVarEIndex v2
_ -> hsep (ppPrefixOp c : map ppTVarEIndex vs)
ppPattern (TLPattern _ l) = ppLiteral l
ppPrefixOp :: QName -> Doc
ppPrefixOp qn = parensIf (isInfixOp qn) (ppQName qn)
ppInfixOp :: QName -> Doc
ppInfixOp qn = if isInfixOp qn then ppQName qn else bquotes (ppQName qn)
ppQName :: QName -> Doc
ppQName (m, i)
| m == "Prelude" && i `elem` builtin = text i
| otherwise = text $ m ++ '.' : i
where builtin = [ "[]", "?", ":", "+", "-", "*", "<"
, ">", "<=", ">=", "==", "/=", "&>", "&" ]
isInfixOp :: QName -> Bool
isInfixOp = all (`elem` "~!@#$%^&*+-=<>:?./|\\") . snd
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 :: Doc -> Doc
indent = nest 2
|