## lustrec / src / dimension.ml @ a2d97a3e

History | View | Annotate | Download (10.4 KB)

1 |
(********************************************************************) |
---|---|

2 |
(* *) |

3 |
(* The LustreC compiler toolset / The LustreC Development Team *) |

4 |
(* Copyright 2012 - -- ONERA - CNRS - INPT *) |

5 |
(* *) |

6 |
(* LustreC is free software, distributed WITHOUT ANY WARRANTY *) |

7 |
(* under the terms of the GNU Lesser General Public License *) |

8 |
(* version 2.1. *) |

9 |
(* *) |

10 |
(********************************************************************) |

11 | |

12 |
open Format |

13 | |

14 |
type dim_expr = |

15 |
{mutable dim_desc: dim_desc; |

16 |
dim_loc: Location.t; |

17 |
dim_id: int} |

18 | |

19 |
and dim_desc = |

20 |
| Dbool of bool |

21 |
| Dint of int |

22 |
| Dident of Utils.ident |

23 |
| Dappl of Utils.ident * dim_expr list |

24 |
| Dite of dim_expr * dim_expr * dim_expr |

25 |
| Dlink of dim_expr |

26 |
| Dvar |

27 |
| Dunivar |

28 | |

29 |
exception Unify of dim_expr * dim_expr |

30 |
exception InvalidDimension |

31 | |

32 |
let new_id = ref (-1) |

33 | |

34 |
let mkdim loc dim = |

35 |
incr new_id; |

36 |
{ dim_loc = loc; |

37 |
dim_id = !new_id; |

38 |
dim_desc = dim;} |

39 | |

40 |
let mkdim_var () = |

41 |
incr new_id; |

42 |
{ dim_loc = Location.dummy_loc; |

43 |
dim_id = !new_id; |

44 |
dim_desc = Dvar;} |

45 | |

46 |
let mkdim_ident loc id = |

47 |
incr new_id; |

48 |
{ dim_loc = loc; |

49 |
dim_id = !new_id; |

50 |
dim_desc = Dident id;} |

51 | |

52 |
let mkdim_bool loc b = |

53 |
incr new_id; |

54 |
{ dim_loc = loc; |

55 |
dim_id = !new_id; |

56 |
dim_desc = Dbool b;} |

57 | |

58 |
let mkdim_int loc i = |

59 |
incr new_id; |

60 |
{ dim_loc = loc; |

61 |
dim_id = !new_id; |

62 |
dim_desc = Dint i;} |

63 | |

64 |
let mkdim_appl loc f args = |

65 |
incr new_id; |

66 |
{ dim_loc = loc; |

67 |
dim_id = !new_id; |

68 |
dim_desc = Dappl (f, args);} |

69 | |

70 |
let mkdim_ite loc i t e = |

71 |
incr new_id; |

72 |
{ dim_loc = loc; |

73 |
dim_id = !new_id; |

74 |
dim_desc = Dite (i, t, e);} |

75 | |

76 |
let rec pp_dimension fmt dim = |

77 |
(*fprintf fmt "<%d>" (Obj.magic dim: int);*) |

78 |
match dim.dim_desc with |

79 |
| Dident id -> |

80 |
fprintf fmt "%s" id |

81 |
| Dint i -> |

82 |
fprintf fmt "%d" i |

83 |
| Dbool b -> |

84 |
fprintf fmt "%B" b |

85 |
| Dite (i, t, e) -> |

86 |
fprintf fmt "if %a then %a else %a" |

87 |
pp_dimension i pp_dimension t pp_dimension e |

88 |
| Dappl (f, [arg]) -> |

89 |
fprintf fmt "(%s%a)" f pp_dimension arg |

90 |
| Dappl (f, [arg1; arg2]) -> |

91 |
fprintf fmt "(%a%s%a)" pp_dimension arg1 f pp_dimension arg2 |

92 |
| Dappl (_, _) -> assert false |

93 |
| Dlink dim' -> fprintf fmt "%a" pp_dimension dim' |

94 |
| Dvar -> fprintf fmt "_%s" (Utils.name_of_dimension dim.dim_id) |

95 |
| Dunivar -> fprintf fmt "'%s" (Utils.name_of_dimension dim.dim_id) |

96 | |

97 |
let rec multi_dimension_product loc dim_list = |

98 |
match dim_list with |

99 |
| [] -> mkdim_int loc 1 |

100 |
| [d] -> d |

101 |
| d::q -> mkdim_appl loc "*" [d; multi_dimension_product loc q] |

102 | |

103 |
(* Builds a dimension expr representing 0<=d *) |

104 |
let check_bound loc d = |

105 |
mkdim_appl loc "<=" [mkdim_int loc 0; d] |

106 | |

107 |
(* Builds a dimension expr representing 0<=i<d *) |

108 |
let check_access loc d i = |

109 |
mkdim_appl loc "&&" |

110 |
[mkdim_appl loc "<=" [mkdim_int loc 0; i]; |

111 |
mkdim_appl loc "<" [i; d]] |

112 | |

113 |
let rec repr dim = |

114 |
match dim.dim_desc with |

115 |
| Dlink dim' -> repr dim' |

116 |
| _ -> dim |

117 | |

118 |
let rec is_eq_dimension d1 d2 = |

119 |
let d1 = repr d1 in |

120 |
let d2 = repr d2 in |

121 |
d1.dim_id = d2.dim_id || |

122 |
match d1.dim_desc, d2.dim_desc with |

123 |
| Dappl (f1, args1), Dappl (f2, args2) -> |

124 |
f1 = f2 && List.length args1 = List.length args2 && List.for_all2 is_eq_dimension args1 args2 |

125 |
| Dite (c1, t1, e1), Dite (c2, t2, e2) -> |

126 |
is_eq_dimension c1 c2 && is_eq_dimension t1 t2 && is_eq_dimension e1 e2 |

127 |
| Dvar, _ |

128 |
| _, Dvar |

129 |
| Dunivar, _ |

130 |
| _, Dunivar -> false |

131 |
| _ -> d1 = d2 |

132 | |

133 |
let is_dimension_const dim = |

134 |
match (repr dim).dim_desc with |

135 |
| Dint _ |

136 |
| Dbool _ -> true |

137 |
| _ -> false |

138 | |

139 |
let size_const_dimension dim = |

140 |
match (repr dim).dim_desc with |

141 |
| Dint i -> i |

142 |
| Dbool b -> if b then 1 else 0 |

143 |
| _ -> (Format.eprintf "internal error: size_const_dimension %a@." pp_dimension dim; assert false) |

144 | |

145 |
let rec is_polymorphic dim = |

146 |
match dim.dim_desc with |

147 |
| Dident _ |

148 |
| Dint _ |

149 |
| Dbool _ |

150 |
| Dvar -> false |

151 |
| Dite (i, t, e) -> |

152 |
is_polymorphic i || is_polymorphic t || is_polymorphic e |

153 |
| Dappl (_, args) -> List.exists is_polymorphic args |

154 |
| Dlink dim' -> is_polymorphic dim' |

155 |
| Dunivar -> true |

156 | |

157 |
(* Normalizes a dimension expression, i.e. canonicalize all polynomial |

158 |
sub-expressions, where unsupported operations (eg. '/') are treated |

159 |
as variables. |

160 |
*) |

161 | |

162 |
let rec factors dim = |

163 |
match dim.dim_desc with |

164 |
| Dappl (f, args) when f = "*" -> List.flatten (List.map factors args) |

165 |
| _ -> [dim] |

166 | |

167 |
let rec factors_constant fs = |

168 |
match fs with |

169 |
| [] -> 1 |

170 |
| f::q -> |

171 |
match f.dim_desc with |

172 |
| Dint i -> i * (factors_constant q) |

173 |
| _ -> factors_constant q |

174 | |

175 |
let norm_factors fs = |

176 |
let k = factors_constant fs in |

177 |
let nk = List.filter (fun d -> not (is_dimension_const d)) fs in |

178 |
(k, List.sort Pervasives.compare nk) |

179 | |

180 |
let rec terms dim = |

181 |
match dim.dim_desc with |

182 |
| Dappl (f, args) when f = "+" -> List.flatten (List.map terms args) |

183 |
| _ -> [dim] |

184 | |

185 |
let rec normalize dim = |

186 |
dim |

187 |
(* |

188 |
let rec unnormalize loc l = |

189 |
let l = List.sort (fun (k, l) (k', l') -> compare l l') (List.map (fun (k, l) -> (k, List.sort compare l)) l) in |

190 |
match l with |

191 |
| [] -> mkdim_int loc 0 |

192 |
| t::q -> |

193 |
List.fold_left (fun res (k, l) -> mkdim_appl loc "+" res (mkdim_appl loc "*" (mkdim_int loc k) l)) t q |

194 |
*) |

195 |
let copy copy_dim_vars dim = |

196 |
let rec cp dim = |

197 |
match dim.dim_desc with |

198 |
| Dbool _ |

199 |
| Dint _ -> dim |

200 |
| Dident id -> mkdim_ident dim.dim_loc id |

201 |
| Dite (c, t, e) -> mkdim_ite dim.dim_loc (cp c) (cp t) (cp e) |

202 |
| Dappl (id, args) -> mkdim_appl dim.dim_loc id (List.map cp args) |

203 |
| Dlink dim' -> cp dim' |

204 |
| Dunivar -> assert false |

205 |
| Dvar -> |

206 |
try |

207 |
List.assoc dim.dim_id !copy_dim_vars |

208 |
with Not_found -> |

209 |
let var = mkdim dim.dim_loc Dvar in |

210 |
copy_dim_vars := (dim.dim_id, var)::!copy_dim_vars; |

211 |
var |

212 |
in cp dim |

213 | |

214 |
(* Partially evaluates a 'simple' dimension expr [dim], i.e. an expr containing only int and bool |

215 |
constructs, with conditionals. [eval_const] is a typing environment for static values. [eval_op] is an evaluation env for basic operators. The argument [dim] is modified in-place. |

216 |
*) |

217 |
let rec eval eval_op eval_const dim = |

218 |
match dim.dim_desc with |

219 |
| Dbool _ |

220 |
| Dint _ -> () |

221 |
| Dident id -> |

222 |
(match eval_const id with |

223 |
| Some val_dim -> dim.dim_desc <- Dlink val_dim |

224 |
| None -> raise InvalidDimension) |

225 |
| Dite (c, t, e) -> |

226 |
begin |

227 |
eval eval_op eval_const c; |

228 |
eval eval_op eval_const t; |

229 |
eval eval_op eval_const e; |

230 |
match (repr c).dim_desc with |

231 |
| Dbool b -> dim.dim_desc <- Dlink (if b then t else e) |

232 |
| _ -> () |

233 |
end |

234 |
| Dappl (id, args) -> |

235 |
begin |

236 |
List.iter (eval eval_op eval_const) args; |

237 |
if List.for_all is_dimension_const args |

238 |
then dim.dim_desc <- Env.lookup_value eval_op id (List.map (fun d -> (repr d).dim_desc) args) |

239 |
end |

240 |
| Dlink dim' -> |

241 |
begin |

242 |
eval eval_op eval_const dim'; |

243 |
dim.dim_desc <- Dlink (repr dim') |

244 |
end |

245 |
| Dvar -> () |

246 |
| Dunivar -> assert false |

247 | |

248 |
let uneval const univar = |

249 |
let univar = repr univar in |

250 |
match univar.dim_desc with |

251 |
| Dunivar -> univar.dim_desc <- Dident const |

252 |
| _ -> assert false |

253 | |

254 |
(** [occurs dvar dim] returns true if the dimension variable [dvar] occurs in |

255 |
dimension expression [dim]. False otherwise. *) |

256 |
let rec occurs dvar dim = |

257 |
let dim = repr dim in |

258 |
match dim.dim_desc with |

259 |
| Dvar -> dim.dim_id = dvar.dim_id |

260 |
| Dident _ |

261 |
| Dint _ |

262 |
| Dbool _ |

263 |
| Dunivar -> false |

264 |
| Dite (i, t, e) -> |

265 |
occurs dvar i || occurs dvar t || occurs dvar e |

266 |
| Dappl (_, args) -> List.exists (occurs dvar) args |

267 |
| Dlink _ -> assert false |

268 | |

269 |
(* Promote monomorphic dimension variables to polymorphic variables. |

270 |
Generalize by side-effects *) |

271 |
let rec generalize dim = |

272 |
match dim.dim_desc with |

273 |
| Dvar -> dim.dim_desc <- Dunivar |

274 |
| Dident _ |

275 |
| Dint _ |

276 |
| Dbool _ |

277 |
| Dunivar -> () |

278 |
| Dite (i, t, e) -> |

279 |
generalize i; generalize t; generalize e |

280 |
| Dappl (_, args) -> List.iter generalize args |

281 |
| Dlink dim' -> generalize dim' |

282 | |

283 |
(* Instantiate polymorphic dimension variables to monomorphic variables. |

284 |
Also duplicates the whole term structure (but the constant sub-terms). |

285 |
*) |

286 |
let rec instantiate inst_dim_vars dim = |

287 |
let dim = repr dim in |

288 |
match dim.dim_desc with |

289 |
| Dvar _ |

290 |
| Dident _ |

291 |
| Dint _ |

292 |
| Dbool _ -> dim |

293 |
| Dite (i, t, e) -> |

294 |
mkdim_ite dim.dim_loc |

295 |
(instantiate inst_dim_vars i) |

296 |
(instantiate inst_dim_vars t) |

297 |
(instantiate inst_dim_vars e) |

298 |
| Dappl (f, args) -> mkdim_appl dim.dim_loc f (List.map (instantiate inst_dim_vars) args) |

299 |
| Dlink dim' -> assert false (*mkdim dim.dim_loc (Dlink (instantiate inst_dim_vars dim'))*) |

300 |
| Dunivar -> |

301 |
try |

302 |
List.assoc dim.dim_id !inst_dim_vars |

303 |
with Not_found -> |

304 |
let var = mkdim dim.dim_loc Dvar in |

305 |
inst_dim_vars := (dim.dim_id, var)::!inst_dim_vars; |

306 |
var |

307 | |

308 |
(** destructive unification of [dim1] and [dim2]. |

309 |
Raises [Unify (t1,t2)] if the types are not unifiable. |

310 |
if [semi] unification is required, |

311 |
[dim1] should furthermore be an instance of [dim2] *) |

312 |
let unify ?(semi=false) dim1 dim2 = |

313 |
let rec unif dim1 dim2 = |

314 |
let dim1 = repr dim1 in |

315 |
let dim2 = repr dim2 in |

316 |
if dim1.dim_id = dim2.dim_id then () else |

317 |
match dim1.dim_desc, dim2.dim_desc with |

318 |
| Dunivar, _ |

319 |
| _ , Dunivar -> assert false |

320 |
| Dvar , Dvar -> |

321 |
if dim1.dim_id < dim2.dim_id |

322 |
then dim2.dim_desc <- Dlink dim1 |

323 |
else dim1.dim_desc <- Dlink dim2 |

324 |
| Dvar , _ when (not semi) && not (occurs dim1 dim2) -> |

325 |
dim1.dim_desc <- Dlink dim2 |

326 |
| _ , Dvar when not (occurs dim2 dim1) -> |

327 |
dim2.dim_desc <- Dlink dim1 |

328 |
| Dite(i1, t1, e1), Dite(i2, t2, e2) -> |

329 |
begin |

330 |
unif i1 i2; |

331 |
unif t1 t2; |

332 |
unif e1 e2 |

333 |
end |

334 |
| Dappl(f1, args1), Dappl(f2, args2) when f1 = f2 && List.length args1 = List.length args2 -> |

335 |
List.iter2 unif args1 args2 |

336 |
| Dbool b1, Dbool b2 when b1 = b2 -> () |

337 |
| Dint i1 , Dint i2 when i1 = i2 -> () |

338 |
| Dident id1, Dident id2 when id1 = id2 -> () |

339 |
| _ -> raise (Unify (dim1, dim2)) |

340 |
in unif dim1 dim2 |

341 | |

342 |
let rec expr_replace_var fvar e = |

343 |
{ e with dim_desc = expr_replace_desc fvar e.dim_desc } |

344 |
and expr_replace_desc fvar e = |

345 |
let re = expr_replace_var fvar in |

346 |
match e with |

347 |
| Dvar |

348 |
| Dunivar |

349 |
| Dbool _ |

350 |
| Dint _ -> e |

351 |
| Dident v -> Dident (fvar v) |

352 |
| Dappl (id, el) -> Dappl (id, List.map re el) |

353 |
| Dite (g,t,e) -> Dite (re g, re t, re e) |

354 |
| Dlink e -> Dlink (re e) |