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
--- ----------------------------------------------------------------------------
--- This module provides some additional goodies for annotated FlatCurry.
---
--- @author  Jan Tikovsky
--- @version October 2017
--- ----------------------------------------------------------------------------
module FlatCurryGoodies where

import FlatCurry.Annotated.Goodies
import FlatCurry.Annotated.Pretty  (ppExp)
import FlatCurry.Annotated.Types
import List                        (find, isPrefixOf)

import Utils

--- ----------------------------------------------------------------------------
--- Extended annotations for concolic testing
--- ----------------------------------------------------------------------------

--- Extended FlatCurry annotation for concolic testing providing
---   * case identifiers and
---   * literal flags
type IDAnn a = (a, Maybe VarIndex, Bool)

--- Concolic testing annotation with type information
type TypeAnn = IDAnn TypeExpr

--- Extend given annotation to default `IDAnn` annotation
extendAnn :: a -> IDAnn a
extendAnn ann = (ann, Nothing, False)

--- Select type annotation
tyAnn :: TypeAnn -> TypeExpr
tyAnn = fst3

--- Select case identifier annotation
cidAnn :: TypeAnn -> VarIndex
cidAnn ann = case snd3 ann of
  Nothing  -> error "FlatCurryGoodies.cidAnn: No identifier annotation found"
  Just cid -> cid

--- Select literal annotation
litAnn :: TypeAnn -> Bool
litAnn = trd3

--- ----------------------------------------------------------------------------

--- Generate a functional FlatCurry type expression from a list of argument types
--- and a result type
mkFunType :: [TypeExpr] -> TypeExpr -> TypeExpr
mkFunType tys ty = foldr FuncType ty tys

hasName :: QName -> AFuncDecl a -> Bool
hasName qn f = qn == funcName f

--- Qualification of Prelude functions
prel :: String -> QName
prel f = ("Prelude", f)

--- Qualify first component of a tuple with "Prelude"
qualPrel :: (String, b) -> (QName, b)
qualPrel (x, y) = (prel x, y)

--- Check whether the qualified name represents a dictionary
isDict :: QName -> Bool
isDict (_, n) = "_Dict" `isPrefixOf` n

--- Annotated FlatCurry expression representing `failed`
failedExpr :: TypeAnn -> AExpr TypeAnn
failedExpr ann = AComb ann FuncCall ((prel "failed"), ann) []

--- smart constructor for a nondeterministic choice in FlatCurry
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

--- ----------------------------------------------------------------------------
--- Representation of some basic types in FlatCurry
--- ----------------------------------------------------------------------------

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]

--- Extended annotation for `()`
unitType :: TypeExpr
unitType = TCons (prel "()") []

--- ----------------------------------------------------------------------------
--- Representation of extended annotations
--- ----------------------------------------------------------------------------

--- Extended annotation for `Bool`
boolAnn :: TypeAnn
boolAnn = extendAnn boolType

--- Extended annotation for `Int`
intAnn :: TypeAnn
intAnn = extendAnn intType

--- Extended annotation for `Char`
charAnn :: TypeAnn
charAnn = extendAnn $ TCons (prel "Char") []

--- Extended annotation for `Float`
floatAnn :: TypeAnn
floatAnn = extendAnn floatType

--- Extended annotation for the list type constructor
listAnn :: TypeExpr -> TypeAnn
listAnn = extendAnn . listType

--- Extended annotation for the tuple type constructor
tplAnn :: [TypeExpr] -> TypeAnn
tplAnn tys = extendAnn $ TCons (prel "(,)") tys

--- Extended annotations of Prelude functions

--- Extended annotation for type `Bool -> a -> a`
condAnn :: TypeExpr -> TypeAnn
condAnn ty = extendAnn $ FuncType boolType (FuncType ty ty)

--- Extended annotation for type `Bool -> Bool -> Bool`
ampAnn :: TypeAnn
ampAnn = extendAnn $ FuncType boolType (FuncType boolType boolType)

--- Extended annotation for type `a -> a -> Bool`
unifyAnn :: TypeAnn -> TypeAnn
unifyAnn ann = extendAnn $ FuncType ty (FuncType ty boolType)
  where ty = tyAnn ann

--- ----------------------------------------------------------------------------
--- FlatCurry expressions
--- ----------------------------------------------------------------------------

--- Annotated FlatCurry expression representing `True`
trueExpr :: AExpr TypeAnn
trueExpr = AComb boolAnn ConsCall (prel "True", boolAnn) []

--- Annotated FlatCurry expression representing `False`
falseExpr :: AExpr TypeAnn
falseExpr = AComb boolAnn ConsCall (prel "False", boolAnn) []

--- Annotated FlatCurry expression representing `[]`
nil :: AExpr TypeAnn
nil = AComb ann  ConsCall (prel "[]", ann) []
  where ann = listAnn (TVar 0)

--- Annotated FlatCurry expression representing `:`
cons :: TypeExpr -> [AExpr TypeAnn] -> AExpr TypeAnn
cons ty = AComb ann ConsCall ((prel ":"), ann)
  where ann = listAnn ty

--- FlatCurry expression representing `(,)`
tpl :: [TypeExpr] -> [AExpr TypeAnn] -> AExpr TypeAnn
tpl tys = AComb ann ConsCall ((prel "(,)"), ann)
  where ann = tplAnn tys

--- Check if the given FlatCurry type is a boolean type
isBoolType :: TypeExpr -> Bool
isBoolType ty = ty == boolType

--- Check if the given FlatCurry expression is `otherwise`
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

--- Check if the given FlatCurry expression is a boolean conjunction
isConj :: AExpr a -> Bool
isConj e = case e of
  AComb _ _ (qn, _) _ -> qn == prel "&&" || qn == prel "&"
  _                   -> False

--- Check if the given FlatCurry expression is a boolean disjunction
isDisj :: AExpr a -> Bool
isDisj e = case e of
  AComb _ _ (qn, _) _ -> qn == prel "||"
  _                   -> False

--- ----------------------------------------------------------------------------
--- FlatCurry pattern
--- ----------------------------------------------------------------------------

--- FlatCurry `True` pattern
truePat :: APattern TypeAnn
truePat = APattern boolAnn (prel "True", boolAnn) []

--- FlatCurry `False` pattern
falsePat :: APattern TypeAnn
falsePat = APattern boolAnn (prel "False", boolAnn) []

--- Get the result type followed by the argument types in given order
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"

--- Select the pattern variables of a given pattern
patVars :: APattern a -> [VarIndex]
patVars (APattern _ _ vs) = map fst vs
patVars (ALPattern   _ _) = []

--- Get the variable index of a FlatCurry expression
getVarIdx :: AExpr a -> [VarIndex]
getVarIdx e = case e of
  AVar _ i -> [i]
  _        -> []

--- Get all type variables of a given FlatCurry type expression
getTyVars :: TypeExpr -> [TVarIndex]
getTyVars (TVar           i) = [i]
getTyVars (FuncType ty1 ty2) = getTyVars ty1 ++ getTyVars ty2
getTyVars (TCons      _ tys) = concatMap getTyVars tys
getTyVars (ForallType   _ _) = []

--- Check if two pattern are equal
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

--- Find the function declaration for given qualified name
findFunc :: QName -> [AFuncDecl a] -> Maybe (AFuncDecl a)
findFunc qn fs = find (hasName qn) fs

--- Find the matching branch for a given pattern
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

--- Add an argument to a partial call
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"

--- Check if given combination type is a partial call
isPartCall :: CombType -> Bool
isPartCall ct = case ct of
  ConsPartCall _ -> True
  FuncPartCall _ -> True
  _              -> False

--- Combine given expressions lists with `=:=` or `=:<=` and resulting
--- unifications with `&`
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]

--- Get the rhs expression of the main function of the given FlatCurry program
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

--- Generate a call of the main function of the given module
-- mainCall :: String -> TypeExpr -> AExpr TypeExpr
-- mainCall m ty = AComb (resultType ty) FuncCall ((m, "main"), ty) []

--- Conversion of Curry types into their FlatCurry representation
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