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
|
module Analysis.TermDomain
( TermDomain(..), AType, DType2, DType5, litAsCons )
where
import Data.List ( intercalate )
import Test.Prop
import FlatCurry.Types
class (Read a, Show a, Eq 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
pre :: String -> QName
pre n = ("Prelude",n)
aTrue, aFalse, aFalseTrue, aNothing, aJustTrue, aJustFalse :: DType
aTrue = dCons 2 (pre "True") []
aFalse = dCons 2 (pre "False") []
aFalseTrue = DCons [(pre "False", []), (pre "True", [])]
aNothing = dCons 2 (pre "Nothing") []
aJustTrue = dCons 2 (pre "Just") [aTrue]
aJustFalse = dCons 2 (pre "Just") [aFalse]
cutDType'test1, cutDType'test2, cutDType'test3 :: Prop
cutDType'test1 = cutDType 0 aTrue -=- anyDType
cutDType'test2 = cutDType 1 aTrue -=- aTrue
cutDType'test3 = cutDType 1 aJustTrue -=- DCons [(pre "Just",[DAny])]
lub'test1, lub'test2, join'test1, join'test2 :: Prop
lub'test1 = lubDType aTrue aTrue -=- aTrue
lub'test2 = lubDType aTrue aFalse -=- aFalseTrue
join'test1 = joinDType aJustTrue aJustFalse -=- emptyDType
join'test2 = joinDType aJustTrue aJustTrue -=- aJustTrue
|