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
|
module ICurry.Extended.I2E(
i2eProg,
extractDepInfoFromI
) where
import Maybe
import List
import ICurry.Extended.Types
import ICurry.Types
data DepInfo =
DepInfo
{ typeDInfo :: [IEQName]
, consDInfo :: [IEQName]
, funcDInfo :: [IEQName]
} deriving (Show)
type DepInfos = [(String, DepInfo)]
i2eQN :: (DepInfo -> [IEQName]) -> DepInfos -> IQName -> IEQName
i2eQN accessor di qn@(mn,_) =
fromJust $ (lookup mn di
>>= lookup qn . accessor
>>= \x -> return (qn,x))
`mplus`
Just (error $ "Cannot find module: " ++ show qn)
specialTypes :: [IEQName]
specialTypes = [
(("Prelude","Apply"), -1)]
i2eQNType :: DepInfos -> IQName -> IEQName
i2eQNType di qn = case lookup qn specialTypes of
Just x -> (qn,x)
Nothing -> i2eQN typeDInfo di qn
i2eQNCons :: DepInfos -> IQName -> IEQName
i2eQNCons = i2eQN consDInfo
specialFunctions :: [IEQName]
specialFunctions =
[ (("Prelude","Int"), -1)
, (("Prelude","Bool"), -1)
, (("Prelude","Float"), -1)]
i2eQNFunc :: DepInfos -> IQName -> IEQName
i2eQNFunc di qn = case lookup qn specialFunctions of
Just x -> (qn,x)
Nothing -> i2eQN funcDInfo di qn
extractDepInfoFromI :: IProg -> (String, DepInfo)
(IProg n _ dts fns) = (n, DepInfo ti ci fi)
where
(ti,ci) = extractTypeInfosFromI dts
fi' = map (\(x, n) -> (x,n+1+maxType)) $ extractFuncInfosFromI fns
maxType = maximum $ -1 : map snd ti
fi = ti ++ fi'
extractTypeInfosFromI :: [IDataType] -> ([IEQName], [IEQName])
= extractTypeInfosFromI' 0
where
extractTypeInfosFromI' :: Int -> [IDataType] -> ([IEQName], [IEQName])
_ [] = ([],[])
extractTypeInfosFromI' nt (t:ts) =
let
(ti, ci) = extractTypeInfoFromI nt t
(ti', ci') = extractTypeInfosFromI' (nt + 1) ts
in
(ti:ti', ci ++ ci')
nt (n, k, _, cs) = ((n,nt), ci)
where
ci = zipWith (\i (IConstructor c _ _) -> (c,i)) [0..] cs
extractFuncInfosFromI :: [IFunction] -> [IEQName]
= zipWith efi [0..]
where
efi :: Int -> IFunction -> IEQName
efi i (IFunction n _ _) = (n, i)
i2eProg :: DepInfos -> IProg -> IEProg
i2eProg di' prog@(IProg n is dts fns) =
IEProg n is (map (i2eDType di) dts) (map (i2eFunc di . fixDeepNests) fns)
where
di = extractDepInfoFromI prog:di'
i2eDType :: DepInfos -> IDataType -> IEDataType
i2eDType di (n, k, vs, cs) = (i2eQNType di n, i2eVis k, vs, map (i2eCons di) cs)
i2eCons :: DepInfos -> IConstructor -> IEConstructor
i2eCons di (IConstructor n k te) =
IEConstructor (i2eQNCons di n) (i2eVis k) (map (i2eTExp di) te)
i2eTExp :: DepInfos -> ITExpr -> IETExpr
i2eTExp _ (ITVar v) = IETVar v
i2eTExp di (ITFunc e1 e2) = IETFunc (i2eTExp di e1) (i2eTExp di e2)
i2eTExp di (ITCons c es) = IETCons (i2eQNType di c) (map (i2eTExp di) es)
i2eVis :: IVisibility -> IEVisibility
i2eVis ICurry.Types.Public = ICurry.Extended.Types.Public
i2eVis ICurry.Types.Private = ICurry.Extended.Types.Private
i2eFunc :: DepInfos -> IFunction -> IEFunction
i2eFunc di (IFunction n k b) = IEFunction (i2eQNFunc di n)
(i2eVis k)
(i2eFuncBody di b)
i2eFuncBody :: DepInfos -> IFuncBody -> IEFuncBody
i2eFuncBody _ (IExternal n en) = IEExternal n en
i2eFuncBody di (IFuncBody vs b) = IEFuncBody vs $ i2eBlock di b
i2eAssign :: DepInfos -> IAssign -> IEAssign
i2eAssign di (v, e) = (v, i2eExp di e)
i2eBlock :: DepInfos -> IBlock -> IEBlock
i2eBlock di (ISimpleBlock as e) =
IESimpleBlock (map (i2eAssign di) as) $ i2eExp di e
i2eBlock di (ICaseConsBlock as v bs) =
IECaseConsBlock (map (i2eAssign di) as) v $ map (i2eCBranch di) bs
i2eBlock di (ICaseLitBlock as v bs) =
IECaseLitBlock (map (i2eAssign di) as) v $ map (i2eLBranch di) bs
i2eCBranch :: DepInfos -> IConsBranch -> IEConsBranch
i2eCBranch di (IConsBranch c vs b) =
IEConsBranch (i2eQNCons di c) vs $ i2eBlock di b
i2eLBranch :: DepInfos -> ILitBranch -> IELitBranch
i2eLBranch di (ILitBranch l b) = IELitBranch l $ i2eBlock di b
i2eExp :: DepInfos -> IExpr -> IEExpr
i2eExp _ (IVar v) = IEVar v
i2eExp _ (ILit l) = IELit l
i2eExp di (IFCall n es) = IEFCall (i2eQNFunc di n) $ map (i2eExp di) es
i2eExp di (ICCall n es) = IECCall (i2eQNCons di n) $ map (i2eExp di) es
i2eExp di (IOr es) = IEOr $ map (i2eExp di) es
maxDepth = 4
fixDeepNests :: IFunction -> IFunction
fixDeepNests (IFunction n k b) = IFunction n k $ fdnBody b
where
fdnBody :: IFuncBody -> IFuncBody
fdnBody e@(IExternal _ _) = e
fdnBody (IFuncBody vs b) = IFuncBody vs $ fdnBlock 0 vs b
fdnBlock :: Int -> [IVarIndex] -> IBlock -> IBlock
fdnBlock i vs (ISimpleBlock as e) =
let as' = fdnAssigns i (vs ++ map fst as) as
(as'',e') = fdnExp i (vs ++ map fst as ++ map fst as') e
in ISimpleBlock (as'++as'') e'
fdnBlock i vs (ICaseLitBlock as v bs) =
let as' = fdnAssigns i (vs ++ map fst as) as
in ICaseLitBlock as' v $ fdnLBs i (vs ++ map fst as') bs
fdnBlock i vs (ICaseConsBlock as v bs) =
let as' = fdnAssigns i (vs ++ map fst as) as
in ICaseConsBlock as' v $ fdnCBs i (vs ++ map fst as') bs
fdnAssigns :: Int -> [IVarIndex] -> [IAssign] -> [IAssign]
fdnAssigns i vs as = uncurry (++) $ chain (fdnAssign i) vs as
fdnAssign :: Int -> [IVarIndex] -> IAssign -> ([IAssign], IAssign)
fdnAssign i vs (v,e) = let (as,e') = fdnExp i vs e in (as, (v, e'))
fdnLBs :: Int -> [IVarIndex] -> [ILitBranch] -> [ILitBranch]
fdnLBs i vs = map (fdnLB i vs)
fdnCBs :: Int -> [IVarIndex] -> [IConsBranch] -> [IConsBranch]
fdnCBs i vs = map (fdnCB i vs)
fdnLB :: Int -> [IVarIndex] -> ILitBranch -> ILitBranch
fdnLB i vs (ILitBranch c b) = ILitBranch c $ fdnBlock (i+1) vs b
fdnCB :: Int -> [IVarIndex] -> IConsBranch -> IConsBranch
fdnCB i vs (IConsBranch c lvs b) =
IConsBranch c lvs $ fdnBlock (i+1) (vs ++ lvs) b
fdnExp :: Int -> [IVarIndex] -> IExpr -> ([IAssign], IExpr)
fdnExp _ _ e@(IVar v) = ([], e)
fdnExp _ _ e@(ILit l) = ([], e)
fdnExp i vs (IFCall n es) = fdnExps i vs (IFCall n) es
fdnExp i vs (ICCall n es) = fdnExps i vs (ICCall n) es
fdnExp i vs (IOr es) = fdnExps i vs IOr es
fdnExps :: Int
-> [IVarIndex]
-> ([IExpr] -> IExpr)
-> [IExpr]
-> ([IAssign], IExpr)
fdnExps i vs c es
| i <= maxDepth =
let
(as', es') = mapFdnExps (i+1) vs es
in
(as', c es')
| otherwise =
let
v = nextFree vs
(as',es') = mapFdnExps 0 (v:vs) es
in
(as' ++ [(v,c es')], IVar v)
chain :: ([a] -> b -> ([(a,c)],d)) -> [a] -> [b] -> ([(a,c)],[d])
chain f _ [] = ([], [])
chain f vs (x:xs) =
let
(as' , x' ) = f vs x
(as'', xs') = chain f (vs ++ map fst as') xs
in
(as' ++ as'', x':xs')
mapFdnExps :: Int -> [IVarIndex] -> [IExpr] -> ([IAssign], [IExpr])
mapFdnExps i = chain (fdnExp i)
nextFree :: [IVarIndex] -> IVarIndex
nextFree vs = 1 + maximum (-1:vs)
|