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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
module Analysis.TermDomain
( TermDomain(..), AType, DType2, DType5, litAsCons )
where
import Data.List ( intercalate )
import System.IO
import Test.Prop
import FlatCurry.Types
import RW.Base
class (Read a, Show a, Eq a, ReadWrite a) => TermDomain a where
emptyType :: a
isEmptyType :: a -> Bool
anyType :: a
isAnyType :: a -> Bool
aCons :: QName -> [a] -> a
aLit :: Literal -> a
consOfType :: a -> [QName]
argTypesOfCons :: QName -> Int -> a -> [a]
lubType :: a -> a -> a
joinType :: a -> a -> a
showType :: a -> String
litAsCons :: Literal -> QName
litAsCons l = ("", showLit l)
where
showLit (Intc i) = show i
showLit (Floatc x) = show x
showLit (Charc c) = show c
data AType = ACons [QName] | AAny
deriving (Eq, Show, Read)
isEmptyAType :: AType -> Bool
isEmptyAType AAny = False
isEmptyAType (ACons cs) = null cs
isAnyAType :: AType -> Bool
isAnyAType AAny = True
isAnyAType (ACons _) = False
lubAType :: AType -> AType -> AType
lubAType AAny _ = AAny
lubAType (ACons _) AAny = AAny
lubAType (ACons c) (ACons d) = ACons (union c d)
where
union [] ys = ys
union xs@(_:_) [] = xs
union (x:xs) (y:ys) | x==y = x : union xs ys
| x<y = x : union xs (y:ys)
| otherwise = y : union (x:xs) ys
joinAType :: AType -> AType -> AType
joinAType AAny av = av
joinAType (ACons c) AAny = ACons c
joinAType (ACons c) (ACons d) = ACons (intersect c d)
where
intersect [] _ = []
intersect (_:_) [] = []
intersect (x:xs) (y:ys) | x==y = x : intersect xs ys
| x<y = intersect xs (y:ys)
| otherwise = intersect (x:xs) ys
showAType :: AType -> String
showAType AAny = "_"
showAType (ACons cs) = "{" ++ intercalate "," (map snd cs) ++ "}"
instance TermDomain AType where
emptyType = ACons []
isEmptyType = isEmptyAType
anyType = AAny
isAnyType = isAnyAType
aCons qc _ = ACons [qc]
aLit l = aCons (litAsCons l) []
consOfType AAny = []
consOfType (ACons cs) = cs
argTypesOfCons _ ar _ = take ar (repeat anyType)
lubType = lubAType
joinType = joinAType
showType = showAType
data DType = DCons [(QName, [DType])] | DAny
deriving (Eq, Show, Read)
cutDType :: Int -> DType -> DType
cutDType _ DAny = DAny
cutDType d (DCons cs)
| d==0 = DAny
| otherwise = DCons (map (\ (c,args) -> (c, map (cutDType (d-1)) args)) cs)
emptyDType :: DType
emptyDType = DCons []
isEmptyDType :: DType -> Bool
isEmptyDType DAny = False
isEmptyDType (DCons cs) = null cs
anyDType :: DType
anyDType = DAny
isAnyDType :: DType -> Bool
isAnyDType DAny = True
isAnyDType (DCons _) = False
dCons :: Int -> QName -> [DType] -> DType
dCons depthk qc ts = cutDType depthk (DCons [(qc,ts)])
consOfDType :: DType -> [QName]
consOfDType DAny = []
consOfDType (DCons cs) = map fst cs
argDTypesOfCons :: QName -> Int -> DType -> [DType]
argDTypesOfCons _ ar DAny = take ar (repeat anyDType)
argDTypesOfCons qn ar (DCons cs) =
maybe (take ar (repeat emptyDType))
id
(lookup qn cs)
lubDType :: DType -> DType -> DType
lubDType DAny _ = DAny
lubDType (DCons _) DAny = DAny
lubDType (DCons cs) (DCons ds) = DCons (union cs ds)
where
union [] ys = ys
union xs@(_:_) [] = xs
union ((c,cts):xs) ((d,dts):ys)
| c==d = (c, map (uncurry lubDType) (zip cts dts)) : union xs ys
| c<d = (c,cts) : union xs ((d,dts):ys)
| otherwise = (d,dts) : union ((c,cts):xs) ys
joinDType :: DType -> DType -> DType
joinDType DAny av = av
joinDType (DCons cs) DAny = DCons cs
joinDType (DCons cs) (DCons ds) = DCons (intersect cs ds)
where
intersect [] _ = []
intersect (_:_) [] = []
intersect ((c,cts):xs) ((d,dts):ys)
| c==d = let cdts = map (uncurry joinDType) (zip cts dts)
in if any (== emptyDType) cdts then intersect xs ys
else (c,cdts) : intersect xs ys
| c<d = intersect xs ((d,dts):ys)
| otherwise = intersect ((c,cts):xs) ys
showDType :: DType -> String
showDType = showDT False
where
showDT _ DAny = "_"
showDT brkt (DCons cs) = case cs of
[] -> "{}"
[(c,[])] -> snd c
[(c,[t1,t2])] -> if any isAlphaNum (snd c)
then showStd c [t1,t2]
else bracketIf brkt $
showDT True t1 ++ snd c ++ showDT True t2
[(c,ts)] -> showStd c ts
_ ->
"{" ++ intercalate ", " (map (\c -> showDT False (DCons [c])) cs) ++ "}"
where
showStd c ts =
bracketIf brkt (intercalate " " (snd c : map (showDT True) ts))
bracketIf b s = if b then "(" ++ s ++ ")" else s
data DType2 = DT2 DType
deriving (Eq, Show, Read)
instance TermDomain DType2 where
emptyType = DT2 emptyDType
isEmptyType (DT2 t) = isEmptyDType t
anyType = DT2 anyDType
isAnyType (DT2 t) = isAnyDType t
aCons qc ts = DT2 (dCons 2 qc (map (\ (DT2 t) -> t) ts))
aLit l = DT2 (dCons 2 (litAsCons l) [])
consOfType (DT2 t) = consOfDType t
argTypesOfCons qn i (DT2 t) = map DT2 (argDTypesOfCons qn i t)
lubType (DT2 t1) (DT2 t2) = DT2 (lubDType t1 t2)
joinType (DT2 t1) (DT2 t2) = DT2 (joinDType t1 t2)
showType (DT2 dt) = showDType dt
data DType5 = DT5 DType
deriving (Eq, Show, Read)
instance TermDomain DType5 where
emptyType = DT5 emptyDType
isEmptyType (DT5 t) = isEmptyDType t
anyType = DT5 anyDType
isAnyType (DT5 t) = isAnyDType t
aCons qc ts = DT5 (dCons 5 qc (map (\ (DT5 t) -> t) ts))
aLit l = DT5 (dCons 5 (litAsCons l) [])
consOfType (DT5 t) = consOfDType t
argTypesOfCons qn i (DT5 t) = map DT5 (argDTypesOfCons qn i t)
lubType (DT5 t1) (DT5 t2) = DT5 (lubDType t1 t2)
joinType (DT5 t1) (DT5 t2) = DT5 (joinDType t1 t2)
showType (DT5 dt) = showDType dt
instance ReadWrite AType where
readRW strs ('0' : r0) = (ACons a',r1)
where
(a',r1) = readRW strs r0
readRW strs ('1' : r0) = (AAny,r0)
showRW params strs0 (ACons a') = (strs1,showChar '0' . show1)
where
(strs1,show1) = showRW params strs0 a'
showRW params strs0 AAny = (strs0,showChar '1')
writeRW params h (ACons a') strs =
hPutChar h '0' >> writeRW params h a' strs
writeRW params h AAny strs = hPutChar h '1' >> return strs
typeOf _ = monoRWType "AType"
instance ReadWrite DType where
readRW strs ('0' : r0) = (DCons a',r1)
where
(a',r1) = readRW strs r0
readRW strs ('1' : r0) = (DAny,r0)
showRW params strs0 (DCons a') = (strs1,showChar '0' . show1)
where
(strs1,show1) = showRW params strs0 a'
showRW params strs0 DAny = (strs0,showChar '1')
writeRW params h (DCons a') strs =
hPutChar h '0' >> writeRW params h a' strs
writeRW params h DAny strs = hPutChar h '1' >> return strs
typeOf _ = monoRWType "DType"
instance ReadWrite DType2 where
readRW strs r0 = (DT2 a',r1)
where
(a',r1) = readRW strs r0
showRW params strs0 (DT2 a') = (strs1,show1)
where
(strs1,show1) = showRW params strs0 a'
writeRW params h (DT2 a') strs = writeRW params h a' strs
typeOf _ = monoRWType "DType2"
instance ReadWrite DType5 where
readRW strs r0 = (DT5 a',r1)
where
(a',r1) = readRW strs r0
showRW params strs0 (DT5 a') = (strs1,show1)
where
(strs1,show1) = showRW params strs0 a'
writeRW params h (DT5 a') strs = writeRW params h a' strs
typeOf _ = monoRWType "DType5"
|