Source file unifyClock.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
open Lxm
open Lv6errors
open Lic
(** DEBUG FLAG *)
let dbg = (Lv6Verbose.get_flag "clock-check2")
let ci2str = LicDump.string_of_clock2
type subst1 = (Lv6Id.t * Lv6Id.t) list
(**
A dedicated kind of substitution tailored to deal with clock variables
(Lic.ClockVar). A few fact motivating why a general unification algorithm
is not necessary.
- ClockVar can only be created at val_exp leaves.
- each leave returns a unique ClockVar
- By construction, clocks of the form "On(id,clk)" do not
contain ClockVar (I should probably craft a new Lic.clock type
that materialize this fact).
- When unifying two Lic.clocks, as far as ClockVar are concerned, there is
therefore only two cases:
(1) c1,c2= ClockVar i, ClockVar j
(2) c1,c2= ClockVar i, clk (where clk does not contain ClockVar _ )
We do that by constructing subst2 only via the function [add_link2] (case 1) and
[add_subst2] (case 2).
And since [add_subst2] is linear (cf the code), this unification algorithm
is also linear.
*)
module IntMap = Map.Make(struct type t = int let compare = compare end)
module IntSet = Set.Make(struct type t = int let compare = compare end)
type cv_info =
| Equiv of IntSet.t
| Clk of clock
type cv_tbl = cv_info IntMap.t
type subst2 = { cpt : int; cv_tbl : cv_tbl }
type subst = subst1 * subst2
let (subst2_to_string : subst2 -> string) =
fun s2 ->
(String.concat " ; "
(IntMap.fold
(fun i cvi acc ->
(match cvi with
| Clk c -> "CV"^(string_of_int i)^"->"^(ci2str c)
| Equiv is -> "CV" ^ (string_of_int i) ^ " in {" ^
(String.concat ","
(IntSet.fold (fun i acc -> ("CV"^(string_of_int i))::acc) is [])) ^
"}"
)::acc
)
s2.cv_tbl
[]
))
let (subst_to_string : subst -> string) =
fun (s1,s2) ->
let v2s = Lv6Id.to_string in
let s2str = subst2_to_string s2 in
let s1str =
(String.concat ", " (List.map (fun (v1,v2) -> (v2s v1) ^ "->" ^ (v2s v2)) s1))
in
if s2str = "" then s2str else if s1str = "" then s2str else
(s1str ^" ; " ^ s2str)
let (empty_subst2:subst2) = { cpt = 0 ; cv_tbl = IntMap.empty }
let (empty_subst:subst) = [], empty_subst2
let (_add_subst1 : Lv6Id.t -> Lv6Id.t -> subst1 -> subst1) =
fun id1 id2 s ->
if List.mem_assoc id1 s then s else (id1,id2)::s
let rec (add_link2 : int -> int -> subst2 -> subst2) =
fun i j s2 ->
if j > i then add_link2 j i s2 else
let tbl = s2.cv_tbl in
let cvi_i = IntMap.find i tbl
and cvi_j = IntMap.find j tbl in
let tbl =
match cvi_i, cvi_j with
| Equiv li, Equiv lj ->
let l = IntSet.union li lj in
let tbl = IntSet.fold (fun i tbl -> IntMap.add i (Equiv l) tbl) l tbl in
tbl
| Equiv l, Clk c
| Clk c, Equiv l ->
IntSet.fold (fun i tbl -> IntMap.add i (Clk c) tbl) l tbl
| Clk c1, Clk c2 ->
assert (c1=c2); tbl
in
{ s2 with cv_tbl = tbl }
let (add_subst2 : int -> Lic.clock -> subst2 -> subst2) =
fun i c s2 ->
let tbl =
match IntMap.find i s2.cv_tbl with
| Equiv l ->
IntSet.fold (fun i tbl -> IntMap.add i (Clk c) tbl) l s2.cv_tbl
| Clk c2 ->
IntMap.add i (Clk c2) s2.cv_tbl
in
{ s2 with cv_tbl = tbl }
let (find_subst1 : Lv6Id.t -> subst1 -> Lv6Id.t option) =
fun id s ->
try Some (List.assoc id s) with Not_found -> None
let (find_subst2 : int -> subst2 -> Lic.clock option) =
fun i s2 ->
try
match IntMap.find i s2.cv_tbl with
| Equiv _l -> None
| Clk c -> Some c
with Not_found ->
print_string (" *** Don't know anything about CV" ^ (string_of_int i) ^ "\n");
print_string (" in the table : " ^ subst2_to_string s2);
flush stdout;
assert false
let (new_clock_var : subst -> subst * Lic.clock) =
fun (s1, s2) ->
let clk = ClockVar s2.cpt in
let tbl = IntMap.add s2.cpt (Equiv (IntSet.singleton s2.cpt)) s2.cv_tbl in
let s2 = { cpt = s2.cpt+1; cv_tbl = tbl } in
(s1, s2), clk
let rec (apply_subst:subst -> Lic.clock -> Lic.clock) =
fun (s1,s2) c ->
match c with
| BaseLic -> BaseLic
| On((cc,cv,ct),clk) ->
let cv = match find_subst1 cv s1 with Some cv2 -> cv2 | None -> cv in
let clk = apply_subst (s1,s2) clk in
On((cc,cv,ct), clk)
| ClockVar i ->
match find_subst2 i s2 with
| Some clk -> apply_subst (s1,s2) clk
| None -> c
let rec (apply_subst2:subst -> Lic.clock -> Lic.clock) =
fun (s1,s2) c ->
match c with
| BaseLic -> BaseLic
| On(v,clk) ->
let clk = apply_subst2 (s1,s2) clk in
On(v, clk)
| ClockVar i ->
match find_subst2 i s2 with
| Some clk -> apply_subst2 (s1,s2) clk
| None -> c
let rec (apply_subst_val_exp : subst -> Lic.val_exp -> Lic.val_exp) =
fun s ve ->
let ve_core =
match ve.ve_core with
| CallByPosLic (by_pos_op, vel) ->
let vel = List.map (apply_subst_val_exp s) vel in
CallByPosLic (by_pos_op, vel)
| CallByNameLic(by_name_op, fl) ->
let fl = List.map
(fun (fn,ve) -> (fn, apply_subst_val_exp s ve)) fl
in
CallByNameLic(by_name_op, fl)
| Merge(ce,cl) ->
let cl = List.map
(fun (fn,ve) -> (fn, apply_subst_val_exp s ve)) cl
in
Merge(ce, cl)
in
let new_clk = List.map (apply_subst s) ve.ve_clk in
let ve = { ve with ve_core = ve_core ; ve_clk = new_clk } in
ve
let is_clock_var = function
| On(_) | BaseLic -> false | ClockVar _ -> true
let (f : subst -> Lxm.t -> Lic.clock -> Lic.clock -> subst) =
fun (s1,s2) lxm arg par ->
let arg = apply_subst (s1,s2) arg in
let par = apply_subst (s1,s2) par in
let rec aux (s1,s2) (c1,c2) =
match (c1,c2) with
| On(_v,_clk), BaseLic
| BaseLic, On(_v,_clk) -> (s1,s2)
| BaseLic, BaseLic -> (s1,s2)
| On((cc1,cv1,_), clk1), On((cc2,cv2,_), clk2) ->
if (cc1,cv1) = (cc2,cv2) then aux (s1,s2) (clk1, clk2) else (s1,s2)
| ClockVar i, ClockVar j ->
if i=j then s1,s2 else s1,(add_link2 i j s2)
| ClockVar i, ci
| ci, ClockVar i -> s1, add_subst2 i ci s2
in
let s = aux (s1,s2) (arg,par) in
let compatible =
let npar = apply_subst s par
and narg = apply_subst s arg in
narg = npar || is_clock_var narg || is_clock_var npar
in
if s <> (s1,s2) then
Lv6Verbose.exe
~flag:dbg
(fun () ->
print_string (
"# UnifyClock.f ("^(Lxm.position lxm)^"), unifying \n#\t -" ^ (ci2str arg) ^
" \n#\t -" ^ (ci2str par) ^ "\n# produces s = " ^ (subst_to_string s)^
"\n# out of s = " ^ (subst_to_string (s1,s2))^
"\n"
);
flush stdout;
);
if compatible then s else
let msg =
("\n*** clock error: The two following clocks are not compatible:\n***\t" ^
(ci2str arg) ^ " (provided clock)\n***\t" ^
(ci2str par) ^ " (expected clock)\n")
in
raise(Compile_error(lxm, msg))
let (list : Lxm.t list -> Lic.clock list -> subst -> subst) =
fun lxms cl s ->
if cl = [] then s
else (
assert(lxms<>[]);
snd(
List.fold_left2
(fun (clk,s) lxm clk2 ->
let s = f s lxm clk clk2 in
let clk = apply_subst s clk in
(clk,s)
)
(List.hd cl, s)
(List.tl lxms)
(List.tl cl)
))
let rec (const_to_val_eff: Lxm.t -> bool -> subst -> const -> subst * val_exp) =
fun lxm expand_const s const ->
let mk_by_pos_op_arg by_pos_op_eff arg =
let _s,clk = match const with
| Tuple_const_eff cl ->
let f (s,clks) _c =
let s, clk = new_clock_var s in
(s,clk::clks)
in
List.fold_left f (s,[]) cl
| _c ->
let s, clk = new_clock_var s in
s, [clk]
in
{ ve_core = CallByPosLic(flagit by_pos_op_eff lxm, arg) ;
ve_typ = types_of_const const ;
ve_clk = clk;
ve_src = lxm
}
in
let mk_by_pos_op by_pos_op_eff = mk_by_pos_op_arg by_pos_op_eff [] in
match const with
| Bool_const_eff _
| Int_const_eff _
| Real_const_eff _ -> s, mk_by_pos_op (CONST const)
| Enum_const_eff (l, _)
| Extern_const_eff (l, _) -> s, mk_by_pos_op (CONST_REF l)
| Abstract_const_eff (l, _teff, c, _is_exported) ->
if expand_const
then const_to_val_eff lxm expand_const s c
else s, mk_by_pos_op (CONST_REF l)
| Array_const_eff (ct, _) ->
let s, vel =
List.fold_left
(fun (s,vel) c ->
let s,ve = const_to_val_eff lxm expand_const s c in
(s,ve::vel)
)
(s,[])
ct
in
let vel = List.rev vel in
s, mk_by_pos_op_arg ARRAY vel
| Tuple_const_eff cl ->
let s, vel =
List.fold_left
(fun (s,vel) c ->
let s, ve = const_to_val_eff lxm expand_const s c in
(s, ve::vel)
)
(s,[])
cl
in
let vel = List.rev vel in
s, mk_by_pos_op_arg TUPLE vel
| Struct_const_eff (fl, stype) ->
let sname = match stype with
| Struct_type_eff(sname, _) -> sname
| _ -> assert false
in
let name_op_flg = flagit (STRUCT(sname)) lxm in
let s, fl =
List.fold_left
(fun (s,fl) (id,const) ->
let s, ve = const_to_val_eff lxm expand_const s const in
s, (flagit id lxm, ve)::fl
)
(s,[])
fl
in
let fl = List.rev fl in
s, { ve_core = (CallByNameLic(name_op_flg, fl));
ve_typ = [stype] ;
ve_clk = [BaseLic];
ve_src = lxm
}