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
|
module Verify.IOTypes
( InOutType(..), trivialInOutType, isAnyIOType, showIOT, inOutATypeFunc
, VarTypes, VarTypesMap, ioVarType, showVarTypes, showArgumentVars
, addVarType2Map, concVarTypesMap, setVarTypeInMap
, bindVarInIOTypes, simplifyVarTypes
)
where
import Data.List
import Data.Maybe ( mapMaybe )
import Analysis.TermDomain ( TermDomain(..), litAsCons )
import FlatCurry.Types
import Debug.Trace ( trace )
import Verify.Helpers
data InOutType a = IOT [([a],a)]
deriving Eq
trivialInOutType :: TermDomain a => Int -> InOutType a
trivialInOutType ar = IOT [(take ar (repeat anyType), anyType)]
isAnyIOType :: TermDomain a => InOutType a -> Bool
isAnyIOType (IOT iots) = case iots of
[(ioargs,iores)] -> all (== anyType) (iores : ioargs)
_ -> False
showIOT :: TermDomain a => InOutType a -> String
showIOT (IOT iots) = "{" ++ intercalate " || " (map showIOType iots) ++ "}"
where
showIOType (ict,oct) = "(" ++ intercalate "," (map showType ict) ++ ")" ++
" |-> " ++ showType oct
valuesOfIOT :: TermDomain a => InOutType a -> a
valuesOfIOT (IOT iotypes) = foldr lubType emptyType (map snd iotypes)
normalizeIOT :: TermDomain a => InOutType a -> InOutType a
normalizeIOT (IOT iotypes) =
IOT (joinOuts (filter ((/= emptyType) . snd) iotypes))
where
joinOuts [] = []
joinOuts ((ict,oct):iots) =
let (iots1,iots2) = partition ((== ict) . fst) iots
in (ict, foldr1 lubType (oct : map snd iots1)) : joinOuts iots2
data InOutTypeState a = InOutTypeState
{ currentOp :: QName
, varPosPat :: [(Int,(Pos,CPattern))]
, ccPattern :: [CPattern]
, resValue :: (QName -> a)
}
data CPattern = AnyP | ConsP QName [CPattern]
deriving (Eq,Show)
replacePattern :: CPattern -> Pos -> CPattern -> CPattern
replacePattern _ [] spat = spat
replacePattern AnyP (_:_) _ = error "replacePattern: AnyP"
replacePattern (ConsP qn pats) (p:ps) spat =
if p >= length pats
then error "replacePattern: illegal position"
else ConsP qn (replace (replacePattern (pats!!p) ps spat) p pats)
pattern2AType :: TermDomain a => CPattern -> a
pattern2AType AnyP = anyType
pattern2AType (ConsP qc ps) = aCons qc (map pattern2AType ps)
addNewVars :: TermDomain a => [Int] -> InOutTypeState a -> InOutTypeState a
addNewVars vs iost =
iost { varPosPat = zip vs (map (\_ -> (freshVarPos,AnyP)) vs) ++
varPosPat iost }
addVarArgsAt :: TermDomain a => Pos -> [Int] -> InOutTypeState a
-> InOutTypeState a
addVarArgsAt pos vs iost =
iost { varPosPat = zip vs (map (\p -> (pos ++ [p],AnyP)) [0..]) ++
varPosPat iost }
setVarPattern :: TermDomain a => Int -> CPattern -> InOutTypeState a
-> InOutTypeState a
setVarPattern vi vt iost =
iost { varPosPat =
map (\ (v,(p,t)) -> if v==vi then (v,(p,vt)) else (v,(p,t)))
(varPosPat iost) }
initInOutTypeState :: TermDomain a => QName -> [Int] -> (QName -> a)
-> InOutTypeState a
initInOutTypeState qf vs resval =
InOutTypeState qf
(zip vs (map (\i -> ([i],AnyP)) [0..]))
(take (length vs) (repeat AnyP))
resval
inOutATypeFunc :: TermDomain a => (QName -> a) -> FuncDecl
-> (QName,InOutType a)
inOutATypeFunc resval (Func qf ar _ _ rule) = case rule of
Rule vs exp -> if length vs /= ar
then error $ "Func " ++ show qf ++ ": inconsistent arities"
else (qf,
normalizeIOT $
inOutATypeExpr (initInOutTypeState qf vs resval) exp)
External _ -> (qf, IOT [(take ar (repeat anyType), resval qf)])
inOutATypeExpr :: TermDomain a => InOutTypeState a -> Expr -> InOutType a
inOutATypeExpr tst exp = case exp of
Var v -> maybe (varNotFound v)
(\ (_,ct) -> IOT [(cpatAsAType, pattern2AType ct)])
(lookup v (varPosPat tst))
Lit l -> IOT [(cpatAsAType, aLit l)]
Comb ct qf es -> if ct == FuncCall
then IOT [(cpatAsAType, resValue tst qf)]
else let argtypes = map (valuesOfIOT . inOutATypeExpr tst)
es
in IOT [(cpatAsAType, aCons qf argtypes)]
Let vs e -> inOutATypeExpr (addNewVars (map fst vs) tst) e
Free vs e -> inOutATypeExpr (addNewVars vs tst) e
Or e1 e2 -> combineIOTs (inOutATypeExpr tst e1)
(inOutATypeExpr tst e2)
Case _ ce bs -> case ce of
Var v -> foldr1 combineIOTs
(map (\ (Branch p e) ->
inOutATypeExpr (addVarBranchPattern v p) e)
bs)
_ -> foldr1 combineIOTs
(map (\ (Branch p e) ->
inOutATypeExpr (addBranchPattern p) e)
bs)
Typed e _ -> inOutATypeExpr tst e
where
cpatAsAType = map pattern2AType (ccPattern tst)
varNotFound v = error $ "Function " ++ snd (currentOp tst) ++
": variable " ++ show v ++ " not found"
combineIOTs (IOT iots1) (IOT iots2) = IOT (iots1 ++ iots2)
addVarBranchPattern v pat
| isFreshVarPos vpos
= addNewVars (patternArgs pat) tst
| otherwise
=
case pat of Pattern _ vs -> addVarArgsAt vpos vs tst'
LPattern _ -> tst'
where
vpos = maybe (varNotFound v) fst (lookup v (varPosPat tst))
consPattern = case pat of
Pattern qc vs -> ConsP qc (take (length vs) (repeat AnyP))
LPattern l -> ConsP (litAsCons l) []
tst' = (setVarPattern v consPattern tst)
{ ccPattern = setPatternAtPos consPattern vpos (ccPattern tst) }
addBranchPattern (Pattern _ vs) = addNewVars vs tst
addBranchPattern (LPattern _) = tst
setPatternAtPos :: CPattern -> Pos -> [CPattern] -> [CPattern]
setPatternAtPos _ [] pts = trace "setPatternAtPos: root occurrence" pts
setPatternAtPos pt (p:ps) pts = replace (replacePattern (pts!!p) ps pt) p pts
type VarTypes a = [(InOutType a, [Int])]
type VarTypesMap a = [(Int, VarTypes a)]
ioVarType :: TermDomain a => a -> VarTypes a
ioVarType atype = [(IOT [([], atype)], [])]
showVarTypes :: TermDomain a => VarTypesMap a -> String
showVarTypes = unlines . map showVarType
where
showVarType (rv, vts) =
let vstr = 'v' : show rv ++ ": "
in vstr ++
intercalate ('\n' : take (length vstr) (repeat ' '))
(map (\(iot,argvs) -> showIOT iot ++ " " ++ showArgumentVars argvs)
vts)
showArgumentVars :: [Int] -> String
showArgumentVars argvs =
"(" ++ intercalate "," (map (\v -> 'v' : show v) argvs) ++ ")"
addVarType2Map :: TermDomain a
=> Int -> VarTypes a -> VarTypesMap a -> VarTypesMap a
addVarType2Map v vts = insert
where
insert [] = [(v,vts)]
insert ((v1,vts1) : vmap) | v == v1 = (v, vts1 ++ vts) : vmap
| v1 < v = (v1,vts1) : insert vmap
| otherwise = (v,vts) : (v1,vts1) : vmap
concVarTypesMap :: TermDomain a
=> VarTypesMap a -> VarTypesMap a -> VarTypesMap a
concVarTypesMap vm1 vm2 = foldr (uncurry addVarType2Map) vm2 vm1
setVarTypeInMap :: TermDomain a
=> Int -> VarTypes a -> VarTypesMap a -> VarTypesMap a
setVarTypeInMap v vts = replace
where
replace [] = [(v,vts)]
replace ((v1,vts1) : vmap) | v == v1 = (v, vts) : vmap
| v1 < v = (v1,vts1) : replace vmap
| otherwise = (v,vts) : (v1,vts1) : vmap
bindVarInIOTypes :: TermDomain a => Int -> a -> VarTypesMap a -> VarTypesMap a
bindVarInIOTypes var vatype vtsmap =
setVarTypeInMap var (maybe [] (map bindIOResult) (lookup var vtsmap)) vtsmap
where
bindIOResult (IOT iots, vs) =
( IOT (filter ((/= emptyType) . snd)
(map (\ (ats,rt) -> (ats, joinType rt vatype)) iots))
, vs)
simplifyVarTypes :: TermDomain a => VarTypesMap a -> VarTypesMap a
simplifyVarTypes = simpDefVarTypes []
where
simpDefVarTypes defts vartypes =
let defvartypes = concatMap definitiveVarTypesFrom vartypes \\ defts
in if null defvartypes
then simpEmptyVarTypes [] vartypes
else simpDefVarTypes (defts ++ defvartypes)
(propagateDefTypes defvartypes vartypes)
definitiveVarTypesFrom :: TermDomain a => (Int, VarTypes a) -> [(Int,a)]
definitiveVarTypesFrom iot = case iot of
(v, [(IOT [(ats,rt)], vs)]) -> filter (not . isAnyType . snd)
((v,rt) : zip vs ats)
_ -> []
simpEmptyVarTypes evs vartypes =
if null emptyvars then vartypes
else simpEmptyVarTypes (emptyvars ++ evs)
(map propagateEmptyVars vartypes)
where
emptyvars = concatMap getEmptyVar vartypes \\ evs
where
getEmptyVar (v, vts) = if IOT [] `elem` map fst vts then [v] else []
propagateEmptyVars (v, vts) =
(v, map (\ (IOT iots, vs) ->
if any (`elem` emptyvars) vs then (IOT [] , vs)
else (IOT iots, vs)) vts)
propagateDefTypes :: TermDomain a
=> [(Int,a)] -> VarTypesMap a -> VarTypesMap a
propagateDefTypes [] viots = viots
propagateDefTypes ((v,vt):vts) viots =
propagateDefTypes vts (map (propagateDefType (v,vt)) viots)
propagateDefType (v,vt) (v1, vts)
| v == v1
= ( v1
, map (\ (IOT iots, vs1) ->
(IOT (filter ((/= emptyType) . snd)
(map (\ (at,rt) -> (at, joinType rt vt)) iots)), vs1))
vts )
| otherwise
= ( v1
, map (\ (IOT iots, vs1) ->
if v `elem` vs1
then
(maybe (IOT iots)
(\i -> IOT (filter (all (not . isEmptyType) . fst)
(map (\ (at,rt) ->
(replace (joinType (at!!i) vt)
i at, rt))
iots)))
(elemIndex v vs1), vs1)
else (IOT iots, vs1))
vts )
|