-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmlvec.ml
377 lines (362 loc) · 14.1 KB
/
dmlvec.ml
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
(*********************************************************************)
(* *)
(* Dependent ML *)
(* *)
(* (c) Hongwei Xi *)
(* July 2000 *)
(* *)
(* University of Cincinnati *)
(* *)
(* Distributed by permission only. *)
(* *)
(*********************************************************************)
(* dmlvec: solve the index contrants *)
open Dmlsyn
module G = Dmlglo
module C = Dmlcop
module D = Dmldeb
module A = Array
let bool_ind = function
Ifun("<", _) -> true
| Ifun("<=", _) -> true
| Ifun(">", _) -> true
| Ifun(">=", _) -> true
| Ifun("=", _) -> true
| Ifun("<>", _) -> true
| Ifun("&&", _) -> true
| Ifun("||", _) -> true
| _ -> false
let rec ipeq res = function
(Ifun ("<", [ind1; ind2]), true) -> IPlt (ind1, ind2) :: res
| (Ifun ("<", [ind1; ind2]), false) -> IPgte (ind1, ind2) :: res
| (Ifun ("<=", [ind1; ind2]), true) -> IPlte (ind1, ind2) :: res
| (Ifun ("<=", [ind1; ind2]), false) -> IPgt (ind1, ind2) :: res
| (Ifun (">", [ind1; ind2]), true) -> IPgt (ind1, ind2) :: res
| (Ifun (">", [ind1; ind2]), false) -> IPlte (ind1, ind2) :: res
| (Ifun (">=", [ind1; ind2]), true) -> IPgte (ind1, ind2) :: res
| (Ifun (">=", [ind1; ind2]), false) -> IPlt (ind1, ind2) :: res
| (Ifun ("<>", [ind1; ind2]), true) -> IPneq (ind1, ind2) :: res
| (Ifun ("<>", [ind1; ind2]), false) -> IPeq (ind1, ind2) :: res
| (Ifun ("=", [ind1; ind2]), true) -> IPeq (ind1, ind2) :: res
| (Ifun ("=", [ind1; ind2]), false) -> IPneq (ind1, ind2) :: res
| (Ifun ("&&", [ind1; ind2]), true) ->
let res = ipeq res (ind1, true) in ipeq res (ind2, true)
| (Ifun ("&&", [ind1; ind2]), false) ->
IPdisj [IPeq (ind1, Iint 0); IPeq (ind1, Iint 0)] :: res
| (Ifun ("||", [ind1; ind2]), true) ->
IPdisj [IPeq (ind1, Iint 1); IPeq (ind1, Iint 1)] :: res
| (Ifun ("||", [ind1; ind2]), false) ->
let res = ipeq res (ind1, false) in ipeq res (ind2, false)
| (ind, _) -> G.abort ("unknown index function: " ^ (D.ind_pr ind))
let bool_eq iplist (ind, ind') =
match bool_ind ind, bool_ind ind' with
(true, false) -> begin
match ind' with
Iint 1 -> ipeq iplist (ind, true)
| Iint 0 -> ipeq iplist (ind, false)
| _ -> G.abort ("iplist_of_ictx: IPeq: not a boolean: " ^
(D.ind_pr ind'))
end
| (false, true) -> begin
match ind with
Iint 1 -> ipeq iplist (ind', true)
| Iint 0 -> ipeq iplist (ind', false)
| _ -> G.abort ("iplist_of_ictx: IPeq: not a boolean: " ^
(D.ind_pr ind))
end
| (true, true) -> G.sorry ("bool_eq: " ^
(D.ind_pr ind) ^ " = " ^ (D.ind_pr ind'))
| (false, false) -> IPeq (ind, ind') :: iplist
let rec iplist_of_ictx iplist = function
[] -> iplist
| IDvar ubiv :: rest ->
let iplist = C.iplist_of_isort (I_u ubiv) iplist (ubiv.ubiv_sort)
in iplist_of_ictx iplist rest
| IDprop ip :: rest -> begin
match ip with
IPeq (ind, ind') ->
let iplist = bool_eq iplist (ind, ind')
in iplist_of_ictx iplist rest
| IPneq (ind, ind') ->
if bool_ind ind' then
let iplist =
match ind with
Iint 0 -> ipeq iplist (ind', true)
| Iint 1 -> ipeq iplist (ind', false)
| _ -> G.abort ("iplist_of_ictx: IPneq: not a boolean: " ^
(D.ind_pr ind'))
in iplist_of_ictx iplist rest
else iplist_of_ictx (ip :: iplist) rest
| _ -> iplist_of_ictx (ip :: iplist) rest
end
let rec neg_ip (res: iprop list) = function
IPlt (ind, ind') -> IPgte (ind, ind') :: res
| IPlte (ind, ind') -> IPgt (ind, ind') :: res
| IPgt (ind, ind') -> IPlte (ind, ind') :: res
| IPgte (ind, ind') -> IPlt (ind, ind') :: res
| IPneq (ind, ind') -> IPeq (ind, ind') :: res
| IPeq (ind, ind') -> IPneq (ind, ind') :: res
| IPdisj iplist -> neg_ip_list res iplist
| IPconj iplist -> IPdisj (neg_ip_list [] iplist) :: res
| ip -> G.abort ("neg_ip: " ^ (D.ip_pr ip))
and neg_ip_list res = function
[] -> res
| ip :: iplist ->
let res = neg_ip res ip in neg_ip_list res iplist
let rec neg_iplist (cond: iprop list) (res: iprop list list) = function
[] -> res
| ip :: rest -> begin
match ip with
IPeq (ind, ind') ->
if bool_ind ind then
let rest =
match ind' with
Iint 1 -> ipeq rest (ind, false)
| Iint 0 -> ipeq rest (ind, true)
| _ -> G.abort ("neg_iplist: IPeq: not a boolean: " ^ (D.ind_pr ind'))
in neg_iplist cond res rest
else neg_iplist cond ((IPneq (ind, ind') :: cond) :: res) rest
| IPconj iplist -> neg_iplist cond res (iplist @ rest)
| IPdisj iplist -> neg_iplist cond ((neg_ip_list cond iplist) :: res) rest
| IPuni (ictx, iplist) ->
let newcond = iplist_of_ictx cond ictx in
let newres = neg_iplist newcond [] iplist
in neg_iplist cond (newres @ res) rest
| ip -> neg_iplist cond ((neg_ip cond ip) :: res) rest
end
let ivs_of_iplist iplist =
let counter = ref 0 in
let rec aux_ind ivs ips = function
Ivar _ -> G.abort ("ivs_of_iplist: aux_ind: Ivar")
| I_u ubiv as ind -> begin
match ubiv.ubiv_num with
0 -> (counter := !counter + 1;
ubiv.ubiv_num <- !counter;
(ubiv :: ivs, ips, ind))
| _ -> (ivs, ips, ind)
end
| I_e ebiv -> begin
match ebiv.ebiv_link with
Inolink -> G.abort ("ivs_of_iplist: aux_ind: unlinked ebiv: " ^
(D.ebiv_pr ebiv))
| Ilinkto ind -> aux_ind ivs ips ind
end
| Iint _ as ind -> (ivs, ips, ind)
| Ifun ("abs", [ind1]) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let ind2 = Ifun ("uminus", [ind1]) in
let ubiv = C.new_ubiv ISint in
let _ = (counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = I_u ubiv in
let ip1 = IPgte (ind, Iint 0) in
let ip2 = IPdisj [IPeq (ind, ind1);IPeq (ind, ind2)]
in (ubiv :: ivs, ip1 :: ip2 :: ips, ind)
| Ifun ("/", [ind1; ind2]) as ind ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in begin
match ind2 with
Iint i -> begin
match i > 0 with
true ->
let ubiv = C.new_ubiv ISint in
let _ =
(counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = Ifun("*", [I_u ubiv; Iint i]) in
let ip1 = IPgte (ind1, ind) in
let ip2 = IPlt (ind1, Ifun("+", [ind; Iint i]))
in (ubiv :: ivs, ip1 :: ip2 :: ips, I_u ubiv)
| false ->
G.abort ("ivs_of_iplist: aux_ind: division by a negative: " ^
D.ind_pr ind)
end
| _ ->
G.abort ("ivs_of_iplist: aux_ind: division by a nonconstant:" ^
D.ind_pr ind)
end
| Ifun ("mod", [ind1; ind2]) as ind ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in begin
match ind2 with
Iint i -> begin
match i > 0 with
true ->
let ubiv = C.new_ubiv ISint in
let _ =
(counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = Ifun("*", [I_u ubiv; Iint i]) in
let ip1 = IPgte (ind1, ind) in
let ip2 = IPlt (ind1, Ifun("+", [ind; Iint i]))
in (ubiv :: ivs, ip1 :: ip2 :: ips, Ifun ("-", [ind1; ind]))
| false -> G.abort ("ivs_of_iplist: aux_ind: modulo a negative")
end
| _ -> G.abort ("ivs_of_iplist: aux_ind: modulo a nonconstant :" ^
D.ind_pr ind)
end
| Ifun ("max", [ind1; ind2]) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2 in
let ubiv = C.new_ubiv ISint in
let _ = (counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = I_u ubiv in
let ip1 = IPlte (ind1, ind) in
let ip2 = IPlte (ind2, ind) in
let ip3 = IPdisj[IPgte (ind1, ind); IPgte (ind2, ind)]
in (ubiv :: ivs, ip1 :: ip2 :: ip3 :: ips, ind)
| Ifun ("min", [ind1; ind2]) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2 in
let ubiv = C.new_ubiv ISint in
let _ = (counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = I_u ubiv in
let ip1 = IPgte (ind1, ind) in
let ip2 = IPgte (ind2, ind) in
let ip3 = IPdisj[IPlte (ind1, ind); IPlte (ind2, ind)]
in (ubiv :: ivs, ip1 :: ip2 :: ip3 :: ips, ind)
| Ifun ("=", [ind1; ind2]) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2 in
let ubiv = C.new_ubiv ISint in
let _ = (counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = I_u ubiv in
let ip1 =
IPdisj[IPgt (ind, Iint 0); IPlt (ind1, ind2); IPgt (ind1, ind2)] in
let ip2 = IPdisj[IPlt (ind, Iint 1); IPlte (ind1, ind2)] in
let ip3 = IPdisj[IPlt (ind, Iint 1); IPgte (ind1, ind2)]
in (ubiv :: ivs, ip1 :: ip2 :: ip3 :: IPlte (ind, Iint 1) :: IPgte (ind, Iint 0) :: ips, ind)
| Ifun ("<>", [ind1; ind2]) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2 in
let ubiv = C.new_ubiv ISint in
let _ = (counter := !counter + 1; ubiv.ubiv_num <- !counter) in
let ind = I_u ubiv in
let ip1 = IPdisj[IPgt (ind, Iint 0); IPlte (ind1, ind2)] in
let ip2 = IPdisj[IPgt (ind, Iint 0); IPgte (ind1, ind2)] in
let ip3 =
IPdisj[IPlt (ind, Iint 1); IPlt (ind1, ind2); IPgt (ind1, ind2)]
in (ubiv :: ivs, ip1 :: ip2 :: ip3 :: IPlte (ind, Iint 1) :: IPgte (ind, Iint 0) :: ips, ind)
| Ifun (name, indlist) as ind ->
let (ivs, ips, indlist) = aux_ind_list ivs ips [] indlist
in (ivs, ips, Ifun (name, indlist))
and aux_ind_list ivs ips (res: index list) = function
[] -> (ivs, ips, List.rev res)
| ind :: rest ->
let (ivs, ips, ind) = aux_ind ivs ips ind
in aux_ind_list ivs ips (ind :: res) rest in
let rec aux_ip ivs ips = function
IPlt (ind1, ind2) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in (ivs, ips, IPlt (ind1, ind2))
| IPlte (ind1, ind2) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in (ivs, ips, IPlte (ind1, ind2))
| IPgt (ind1, ind2) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in (ivs, ips, IPgt (ind1, ind2))
| IPgte (ind1, ind2) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in (ivs, ips, IPgte (ind1, ind2))
| IPeq (ind1, ind2) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in (ivs, ips, IPeq (ind1, ind2))
| IPneq (ind1, ind2) ->
let (ivs, ips, ind1) = aux_ind ivs ips ind1 in
let (ivs, ips, ind2) = aux_ind ivs ips ind2
in (ivs, ips, IPneq (ind1, ind2))
| IPconj iplist ->
let (ivs, ips, iplist) = aux_ip_list ivs ips [] iplist
in (ivs, ips, IPconj iplist)
| IPdisj iplist ->
let (ivs, ips, iplist) = aux_ip_list ivs ips [] iplist
in (ivs, ips, IPdisj iplist)
| IPuni _ -> G.abort ("ivs_of_iplist: aux_ip: IPuni")
and aux_ip_list ivs ips res = function
[] -> (ivs, ips, List.rev res)
| ip :: rest ->
let (ivs, ips, ip) = aux_ip ivs ips ip
in aux_ip_list ivs ips (ip :: res) rest in
let (ivs, ips, iplist) = aux_ip_list [] [] [] iplist
in (ivs, ips @ iplist)
let rec untag_ivs = function
[] -> ()
| ubiv :: rest -> ubiv.ubiv_num <- 0; untag_ivs rest
let ivec_of_ip n ip =
let rec aux_ind vec coef = function
I_u ubiv -> let i = ubiv.ubiv_num in vec.(i) <- vec.(i) + coef
| I_e ebiv -> begin
match ebiv.ebiv_link with
Inolink -> G.abort ("vec_of_ip: aux_ind: I_e: unlinked ebiv")
| Ilinkto ind -> aux_ind vec coef ind
end
| Iint i -> vec.(0) <- vec.(0) + i * coef
| Ifun ("uminus", [ind]) -> aux_ind vec (-coef) ind
| Ifun ("+", [ind1; ind2]) ->
aux_ind vec coef ind1; aux_ind vec coef ind2
| Ifun ("-", [ind1; ind2]) ->
aux_ind vec coef ind1; aux_ind vec (-coef) ind2
| Ifun ("*", [Iint i; ind]) -> aux_ind vec (i * coef) ind
| Ifun ("*", [ind; Iint i]) -> aux_ind vec (i * coef) ind
| ind -> G.abort ("vec_of_ip: aux_ind: " ^ (D.ind_pr ind)) in
let rec aux_ip = function
IPeq (ind1, ind2) ->
let vec = A.make (n + 1) 0
in begin
aux_ind vec 1 ind1; aux_ind vec (-1) ind2; IVeq vec
end
| IPneq (ind1, ind2) ->
let ivec1 = aux_ip (IPlt (ind1, ind2)) in
let ivec2 = aux_ip (IPgt (ind1, ind2))
in IVdisj [ivec1; ivec2]
| IPlt (ind1, ind2) ->
let vec = A.make (n + 1) 0
in begin
aux_ind vec (-1) ind1; aux_ind vec 1 ind2;
vec.(0) <- vec.(0) - 1; IVgte vec
end
| IPlte (ind1, ind2) ->
let vec = A.make (n + 1) 0
in aux_ind vec (-1) ind1; aux_ind vec 1 ind2; IVgte vec
| IPgt (ind1, ind2) ->
let vec = A.make (n + 1) 0
in begin
aux_ind vec 1 ind1; aux_ind vec (-1) ind2;
vec.(0) <- vec.(0) - 1; IVgte vec
end
| IPgte (ind1, ind2) ->
let vec = A.make (n + 1) 0
in aux_ind vec 1 ind1; aux_ind vec (-1) ind2; IVgte vec
| IPconj iplist -> IVconj (List.map aux_ip iplist)
| IPdisj iplist -> IVdisj (List.map aux_ip iplist)
| ip -> G.abort ("vec_of_ip: aux_ip: " ^ (D.ip_pr ip))
in aux_ip ip
let rec iveclist_of_iplist n res = function
[] -> res
| IPneq (ind1, ind2) :: rest ->
let ivec1 = ivec_of_ip n (IPlt (ind1, ind2)) in
let ivec2 = ivec_of_ip n (IPgt (ind1, ind2))
in iveclist_of_iplist n (IVdisj [ivec1; ivec2] :: res) rest
| IPconj ips :: rest -> iveclist_of_iplist n res (ips @ rest)
| IPdisj ips :: rest ->
let ivecs = List.map (ivec_of_ip n) ips
in iveclist_of_iplist n (IVdisj ivecs :: res) rest
| ip :: rest ->
let ivec = ivec_of_ip n ip
in iveclist_of_iplist n (ivec :: res) rest
let main iplist =
let iplistlist = neg_iplist [] [] iplist in
(*
let _ = D.show_iplistlist iplistlist in
*)
let aux iplist =
let (ivs, ips) = ivs_of_iplist iplist in
let n = List.length ivs in
let iveclist = iveclist_of_iplist n [] ips in
let _ = untag_ivs ivs
in iveclist
in List.map aux iplistlist