| 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
 | 
module FlatCurryGoodies where
import FlatCurry.Annotated.Goodies
import FlatCurry.Annotated.Pretty  (ppExp)
import FlatCurry.Annotated.Types
import List                        (find, isPrefixOf)
import Utils
type IDAnn a = (a, Maybe VarIndex, Bool)
type TypeAnn = IDAnn TypeExpr
extendAnn :: a -> IDAnn a
extendAnn ann = (ann, Nothing, False)
tyAnn :: TypeAnn -> TypeExpr
tyAnn = fst3
cidAnn :: TypeAnn -> VarIndex
cidAnn ann = case snd3 ann of
  Nothing  -> error "FlatCurryGoodies.cidAnn: No identifier annotation found"
  Just cid -> cid
litAnn :: TypeAnn -> Bool
litAnn = trd3
mkFunType :: [TypeExpr] -> TypeExpr -> TypeExpr
mkFunType tys ty = foldr FuncType ty tys
hasName :: QName -> AFuncDecl a -> Bool
hasName qn f = qn == funcName f
prel :: String -> QName
prel f = ("Prelude", f)
qualPrel :: (String, b) -> (QName, b)
qualPrel (x, y) = (prel x, y)
isDict :: QName -> Bool
isDict (_, n) = "_Dict" `isPrefixOf` n
failedExpr :: TypeAnn -> AExpr TypeAnn
failedExpr ann = AComb ann FuncCall ((prel "failed"), ann) []
mkOr :: AExpr TypeAnn -> AExpr TypeAnn -> AExpr TypeAnn
mkOr e1 e2 | e1 == failedExpr ann = e2
           | e2 == failedExpr ann = e1
           | otherwise            = AOr ann e1 e2
  where ann = annExpr e1
boolType :: TypeExpr
boolType = TCons (prel "Bool") []
intType :: TypeExpr
intType = TCons (prel "Int") []
charType :: TypeExpr
charType = TCons (prel "Char") []
floatType :: TypeExpr
floatType = TCons (prel "Float") []
listType :: TypeExpr -> TypeExpr
listType ty = TCons (prel "[]") [ty]
unitType :: TypeExpr
unitType = TCons (prel "()") []
boolAnn :: TypeAnn
boolAnn = extendAnn boolType
intAnn :: TypeAnn
intAnn = extendAnn intType
charAnn :: TypeAnn
charAnn = extendAnn $ TCons (prel "Char") []
floatAnn :: TypeAnn
floatAnn = extendAnn floatType
listAnn :: TypeExpr -> TypeAnn
listAnn = extendAnn . listType
tplAnn :: [TypeExpr] -> TypeAnn
tplAnn tys = extendAnn $ TCons (prel "(,)") tys
condAnn :: TypeExpr -> TypeAnn
condAnn ty = extendAnn $ FuncType boolType (FuncType ty ty)
ampAnn :: TypeAnn
ampAnn = extendAnn $ FuncType boolType (FuncType boolType boolType)
unifyAnn :: TypeAnn -> TypeAnn
unifyAnn ann = extendAnn $ FuncType ty (FuncType ty boolType)
  where ty = tyAnn ann
trueExpr :: AExpr TypeAnn
trueExpr = AComb boolAnn ConsCall (prel "True", boolAnn) []
falseExpr :: AExpr TypeAnn
falseExpr = AComb boolAnn ConsCall (prel "False", boolAnn) []
nil :: AExpr TypeAnn
nil = AComb ann  ConsCall (prel "[]", ann) []
  where ann = listAnn (TVar 0)
cons :: TypeExpr -> [AExpr TypeAnn] -> AExpr TypeAnn
cons ty = AComb ann ConsCall ((prel ":"), ann)
  where ann = listAnn ty
tpl :: [TypeExpr] -> [AExpr TypeAnn] -> AExpr TypeAnn
tpl tys = AComb ann ConsCall ((prel "(,)"), ann)
  where ann = tplAnn tys
isBoolType :: TypeExpr -> Bool
isBoolType ty = ty == boolType
isOtherwise :: AExpr a -> Bool
isOtherwise e = case e of
  AComb _ FuncCall (("Prelude", "otherwise"), _) [] -> True
  _                                                 -> False
hasBoolType :: AExpr TypeAnn -> Bool
hasBoolType e = tyAnn (annExpr e) == boolType
isConj :: AExpr a -> Bool
isConj e = case e of
  AComb _ _ (qn, _) _ -> qn == prel "&&" || qn == prel "&"
  _                   -> False
isDisj :: AExpr a -> Bool
isDisj e = case e of
  AComb _ _ (qn, _) _ -> qn == prel "||"
  _                   -> False
truePat :: APattern TypeAnn
truePat = APattern boolAnn (prel "True", boolAnn) []
falsePat :: APattern TypeAnn
falsePat = APattern boolAnn (prel "False", boolAnn) []
resArgTypes :: TypeExpr -> [TypeExpr]
resArgTypes = resArgTypes' []
  where
  resArgTypes' tys ty@(TVar         _) = ty : reverse tys
  resArgTypes' tys ty@(TCons      _ _) = ty : reverse tys
  resArgTypes' tys (FuncType  ty1 ty2) = resArgTypes' (ty1 : tys) ty2
  resArgTypes' _   (ForallType    _ _) = error "FlatCurryGoodies.resArgTypes"
patVars :: APattern a -> [VarIndex]
patVars (APattern _ _ vs) = map fst vs
patVars (ALPattern   _ _) = []
getVarIdx :: AExpr a -> [VarIndex]
getVarIdx e = case e of
  AVar _ i -> [i]
  _        -> []
getTyVars :: TypeExpr -> [TVarIndex]
getTyVars (TVar           i) = [i]
getTyVars (FuncType ty1 ty2) = getTyVars ty1 ++ getTyVars ty2
getTyVars (TCons      _ tys) = concatMap getTyVars tys
getTyVars (ForallType   _ _) = []
eqPattern :: APattern a -> APattern a -> Bool
eqPattern p q = case (p, q) of
  (APattern _ (c1, _) _, APattern _ (c2, _) _) -> c1 == c2
  (ALPattern       _ l1, ALPattern       _ l2) -> l1 == l2
  _                                            -> False
findFunc :: QName -> [AFuncDecl a] -> Maybe (AFuncDecl a)
findFunc qn fs = find (hasName qn) fs
findBranch :: APattern a -> [ABranchExpr a] -> Maybe (Int, [VarIndex], AExpr a)
findBranch = findBranch' 1
  where
  findBranch' _ _ []                                 = Nothing
  findBranch' i p (ABranch q e : bs) | eqPattern p q = Just (i, patVars q, e)
                                     | otherwise     = findBranch' (i+1) p bs
addPartCallArg :: a -> CombType -> (QName, a) -> [AExpr a] -> AExpr a -> AExpr a
addPartCallArg ann ct qn es e = AComb ann ct' qn (es ++ [e])
  where ct' = case ct of
                ConsPartCall 1 -> ConsCall
                ConsPartCall n -> ConsPartCall (n - 1)
                FuncPartCall 1 -> FuncCall
                FuncPartCall n -> FuncPartCall (n - 1)
                _              -> error "FlatCurryGoodies.addPartCallArg"
isPartCall :: CombType -> Bool
isPartCall ct = case ct of
  ConsPartCall _ -> True
  FuncPartCall _ -> True
  _              -> False
combine :: QName -> QName -> AExpr TypeAnn -> [AExpr TypeAnn]
        -> [AExpr TypeAnn] -> AExpr TypeAnn
combine amp uni def es1 es2
  | null eqs  = def
  | otherwise = foldr1 (mkCall (const ampAnn) amp) eqs
  where
  eqs                = zipWith (mkCall unifyAnn uni) es1 es2
  mkCall tc qn e1 e2 = AComb boolAnn FuncCall (qn, tc (annExpr e1)) [e1, e2]
getMainBody :: AProg a -> Maybe (AExpr a)
getMainBody (AProg m _ _ fs _) = case findFunc (m, "main") fs of
  Just f | isFuncCall b -> Just b
    where b = funcBody f
  _                     -> Nothing
class ToFCY a where
  toFCY   :: a    -> AExpr TypeAnn
  fromFCY :: AExpr TypeAnn -> a
instance ToFCY Bool where
  toFCY False = falseExpr
  toFCY True  = trueExpr
  fromFCY e = case e of
    AComb _ ConsCall (("Prelude", "False"), _) [] -> False
    AComb _ ConsCall (("Prelude",  "True"), _) [] -> True
    _                                             -> error "fromFCY: no boolean"
instance ToFCY Int where
  toFCY = ALit intAnn . Intc
  fromFCY e = case e of
    ALit _ (Intc x) -> x
    _               -> error "fromFCY: no integer"
instance ToFCY Char where
  toFCY = ALit charAnn . Charc
  fromFCY e = case e of
    ALit _ (Charc c) -> c
    _                -> error "fromFCY: no character"
instance ToFCY Float where
  toFCY = ALit floatAnn . Floatc
  fromFCY e = case e of
    ALit _ (Floatc f) -> f
    _                 -> error "fromFCY: no float"
instance ToFCY a => ToFCY [a] where
  toFCY []     = nil
  toFCY (x:xs) = cons ty [x', toFCY xs]
    where x' = toFCY x
          ty = tyAnn (annExpr x')
  fromFCY e = case e of
    AComb _ ConsCall (("Prelude", "[]"), _) []       -> []
    AComb _ ConsCall (("Prelude",  ":"), _) [e1, e2] -> fromFCY e1 : fromFCY e2
    _                                                -> error "fromFCY: no list"
instance (ToFCY a, ToFCY b) => ToFCY (a, b) where
  toFCY (x, y) = tpl tys [x', y']
    where x'  = toFCY x
          y'  = toFCY y
          tys = map (tyAnn . annExpr) [x', y']
  fromFCY e = case e of
    AComb _ ConsCall (("Prelude", "(,)"), _) [e1, e2] -> (fromFCY e1, fromFCY e2)
    _                                                 -> error $ "fromFCY: no tuple: " ++ show e
 |