## lustrec / src / typing.ml @ 51768260

History | View | Annotate | Download (28.3 KB)

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

2 |
* SchedMCore - A MultiCore Scheduling Framework |

3 |
* Copyright (C) 2009-2011, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE |

4 |
* |

5 |
* This file is part of Prelude |

6 |
* |

7 |
* Prelude is free software; you can redistribute it and/or |

8 |
* modify it under the terms of the GNU Lesser General Public License |

9 |
* as published by the Free Software Foundation ; either version 2 of |

10 |
* the License, or (at your option) any later version. |

11 |
* |

12 |
* Prelude is distributed in the hope that it will be useful, but |

13 |
* WITHOUT ANY WARRANTY ; without even the implied warranty of |

14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |

15 |
* Lesser General Public License for more details. |

16 |
* |

17 |
* You should have received a copy of the GNU Lesser General Public |

18 |
* License along with this program ; if not, write to the Free Software |

19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |

20 |
* USA |

21 |
*---------------------------------------------------------------------------- *) |

22 | |

23 |
(** Main typing module. Classic inference algorithm with destructive |

24 |
unification. *) |

25 | |

26 |
let debug fmt args = () (* Format.eprintf "%a" *) |

27 |
(* Though it shares similarities with the clock calculus module, no code |

28 |
is shared. Simple environments, very limited identifier scoping, no |

29 |
identifier redefinition allowed. *) |

30 | |

31 |
open Utils |

32 |
(* Yes, opening both modules is dirty as some type names will be |

33 |
overwritten, yet this makes notations far lighter.*) |

34 |
open LustreSpec |

35 |
open Corelang |

36 |
open Types |

37 |
open Format |

38 | |

39 |
let pp_typing_env fmt env = |

40 |
Env.pp_env print_ty fmt env |

41 | |

42 |
(** [occurs tvar ty] returns true if the type variable [tvar] occurs in |

43 |
type [ty]. False otherwise. *) |

44 |
let rec occurs tvar ty = |

45 |
let ty = repr ty in |

46 |
match ty.tdesc with |

47 |
| Tvar -> ty=tvar |

48 |
| Tarrow (t1, t2) -> |

49 |
(occurs tvar t1) || (occurs tvar t2) |

50 |
| Ttuple tl -> |

51 |
List.exists (occurs tvar) tl |

52 |
| Tstruct fl -> |

53 |
List.exists (fun (f, t) -> occurs tvar t) fl |

54 |
| Tarray (_, t) |

55 |
| Tstatic (_, t) |

56 |
| Tclock t |

57 |
| Tlink t -> occurs tvar t |

58 |
| Tenum _ | Tconst _ | Tunivar | Tint | Treal | Tbool | Trat -> false |

59 | |

60 |
(** Promote monomorphic type variables to polymorphic type variables. *) |

61 |
(* Generalize by side-effects *) |

62 |
let rec generalize ty = |

63 |
match ty.tdesc with |

64 |
| Tvar -> |

65 |
(* No scopes, always generalize *) |

66 |
ty.tdesc <- Tunivar |

67 |
| Tarrow (t1,t2) -> |

68 |
generalize t1; generalize t2 |

69 |
| Ttuple tl -> |

70 |
List.iter generalize tl |

71 |
| Tstruct fl -> |

72 |
List.iter (fun (f, t) -> generalize t) fl |

73 |
| Tstatic (d, t) |

74 |
| Tarray (d, t) -> Dimension.generalize d; generalize t |

75 |
| Tclock t |

76 |
| Tlink t -> |

77 |
generalize t |

78 |
| Tenum _ | Tconst _ | Tunivar | Tint | Treal | Tbool | Trat -> () |

79 | |

80 |
(** Downgrade polymorphic type variables to monomorphic type variables *) |

81 |
let rec instantiate inst_vars inst_dim_vars ty = |

82 |
let ty = repr ty in |

83 |
match ty.tdesc with |

84 |
| Tenum _ | Tconst _ | Tvar | Tint | Treal | Tbool | Trat -> ty |

85 |
| Tarrow (t1,t2) -> |

86 |
{ty with tdesc = |

87 |
Tarrow ((instantiate inst_vars inst_dim_vars t1), (instantiate inst_vars inst_dim_vars t2))} |

88 |
| Ttuple tlist -> |

89 |
{ty with tdesc = Ttuple (List.map (instantiate inst_vars inst_dim_vars) tlist)} |

90 |
| Tstruct flist -> |

91 |
{ty with tdesc = Tstruct (List.map (fun (f, t) -> (f, instantiate inst_vars inst_dim_vars t)) flist)} |

92 |
| Tclock t -> |

93 |
{ty with tdesc = Tclock (instantiate inst_vars inst_dim_vars t)} |

94 |
| Tstatic (d, t) -> |

95 |
{ty with tdesc = Tstatic (Dimension.instantiate inst_dim_vars d, instantiate inst_vars inst_dim_vars t)} |

96 |
| Tarray (d, t) -> |

97 |
{ty with tdesc = Tarray (Dimension.instantiate inst_dim_vars d, instantiate inst_vars inst_dim_vars t)} |

98 |
| Tlink t -> |

99 |
(* should not happen *) |

100 |
{ty with tdesc = Tlink (instantiate inst_vars inst_dim_vars t)} |

101 |
| Tunivar -> |

102 |
try |

103 |
List.assoc ty.tid !inst_vars |

104 |
with Not_found -> |

105 |
let var = new_var () in |

106 |
inst_vars := (ty.tid, var)::!inst_vars; |

107 |
var |

108 | |

109 |
(* [type_coretype cty] types the type declaration [cty] *) |

110 |
let rec type_coretype type_dim cty = |

111 |
match (*get_repr_type*) cty with |

112 |
| Tydec_any -> new_var () |

113 |
| Tydec_int -> Type_predef.type_int |

114 |
| Tydec_real -> Type_predef.type_real |

115 |
| Tydec_float -> Type_predef.type_real |

116 |
| Tydec_bool -> Type_predef.type_bool |

117 |
| Tydec_clock ty -> Type_predef.type_clock (type_coretype type_dim ty) |

118 |
| Tydec_const c -> Type_predef.type_const c |

119 |
| Tydec_enum tl -> Type_predef.type_enum tl |

120 |
| Tydec_struct fl -> Type_predef.type_struct (List.map (fun (f, ty) -> (f, type_coretype type_dim ty)) fl) |

121 |
| Tydec_array (d, ty) -> |

122 |
begin |

123 |
type_dim d; |

124 |
Type_predef.type_array d (type_coretype type_dim ty) |

125 |
end |

126 | |

127 |
(* [coretype_type is the reciprocal of [type_typecore] *) |

128 |
let rec coretype_type ty = |

129 |
match (repr ty).tdesc with |

130 |
| Tvar -> Tydec_any |

131 |
| Tint -> Tydec_int |

132 |
| Treal -> Tydec_real |

133 |
| Tbool -> Tydec_bool |

134 |
| Tconst c -> Tydec_const c |

135 |
| Tclock t -> Tydec_clock (coretype_type t) |

136 |
| Tenum tl -> Tydec_enum tl |

137 |
| Tstruct fl -> Tydec_struct (List.map (fun (f, t) -> (f, coretype_type t)) fl) |

138 |
| Tarray (d, t) -> Tydec_array (d, coretype_type t) |

139 |
| Tstatic (_, t) -> coretype_type t |

140 |
| _ -> assert false |

141 | |

142 |
let get_type_definition tname = |

143 |
try |

144 |
type_coretype (fun d -> ()) (Hashtbl.find type_table (Tydec_const tname)) |

145 |
with Not_found -> raise (Error (Location.dummy_loc, Unbound_type tname)) |

146 | |

147 |
(** [unify t1 t2] unifies types [t1] and [t2]. Raises [Unify |

148 |
(t1,t2)] if the types are not unifiable.*) |

149 |
(* Standard destructive unification *) |

150 |
let rec unify t1 t2 = |

151 |
let t1 = repr t1 in |

152 |
let t2 = repr t2 in |

153 |
if t1=t2 then |

154 |
() |

155 |
else |

156 |
(* No type abbreviations resolution for now *) |

157 |
match t1.tdesc,t2.tdesc with |

158 |
(* This case is not mandory but will keep "older" types *) |

159 |
| Tvar, Tvar -> |

160 |
if t1.tid < t2.tid then |

161 |
t2.tdesc <- Tlink t1 |

162 |
else |

163 |
t1.tdesc <- Tlink t2 |

164 |
| (Tvar, _) when (not (occurs t1 t2)) -> |

165 |
t1.tdesc <- Tlink t2 |

166 |
| (_,Tvar) when (not (occurs t2 t1)) -> |

167 |
t2.tdesc <- Tlink t1 |

168 |
| Tarrow (t1,t2), Tarrow (t1',t2') -> |

169 |
begin |

170 |
unify t1 t1'; |

171 |
unify t2 t2' |

172 |
end |

173 |
| Ttuple tl, Ttuple tl' when List.length tl = List.length tl' -> |

174 |
List.iter2 unify tl tl' |

175 |
| Tstruct fl, Tstruct fl' when List.map fst fl = List.map fst fl' -> |

176 |
List.iter2 (fun (_, t) (_, t') -> unify t t') fl fl' |

177 |
| Tclock _, Tstatic _ |

178 |
| Tstatic _, Tclock _ -> raise (Unify (t1, t2)) |

179 |
| Tclock t1', _ -> unify t1' t2 |

180 |
| _, Tclock t2' -> unify t1 t2' |

181 |
| Tint, Tint | Tbool, Tbool | Trat, Trat |

182 |
| Tunivar, _ | _, Tunivar -> () |

183 |
| (Tconst t, _) -> |

184 |
let def_t = get_type_definition t in |

185 |
unify def_t t2 |

186 |
| (_, Tconst t) -> |

187 |
let def_t = get_type_definition t in |

188 |
unify t1 def_t |

189 |
| Tenum tl, Tenum tl' when tl == tl' -> () |

190 |
| Tstruct fl, Tstruct fl' when fl == fl' -> () |

191 |
| Tstatic (e1, t1'), Tstatic (e2, t2') |

192 |
| Tarray (e1, t1'), Tarray (e2, t2') -> |

193 |
begin |

194 |
unify t1' t2'; |

195 |
Dimension.eval Basic_library.eval_env (fun c -> None) e1; |

196 |
Dimension.eval Basic_library.eval_env (fun c -> None) e2; |

197 |
Dimension.unify e1 e2; |

198 |
end |

199 |
| _,_ -> raise (Unify (t1, t2)) |

200 | |

201 |
(** [semi_unify t1 t2] checks whether type [t1] is an instance of type [t2]. Raises [Unify |

202 |
(t1,t2)] if the types are not semi-unifiable.*) |

203 |
(* Standard destructive semi-unification *) |

204 |
let rec semi_unify t1 t2 = |

205 |
let t1 = repr t1 in |

206 |
let t2 = repr t2 in |

207 |
if t1=t2 then |

208 |
() |

209 |
else |

210 |
(* No type abbreviations resolution for now *) |

211 |
match t1.tdesc,t2.tdesc with |

212 |
(* This case is not mandory but will keep "older" types *) |

213 |
| Tvar, Tvar -> |

214 |
if t1.tid < t2.tid then |

215 |
t2.tdesc <- Tlink t1 |

216 |
else |

217 |
t1.tdesc <- Tlink t2 |

218 |
| (Tvar, _) -> raise (Unify (t1, t2)) |

219 |
| (_,Tvar) when (not (occurs t2 t1)) -> |

220 |
t2.tdesc <- Tlink t1 |

221 |
| Tarrow (t1,t2), Tarrow (t1',t2') -> |

222 |
begin |

223 |
semi_unify t1 t1'; |

224 |
semi_unify t2 t2' |

225 |
end |

226 |
| Ttuple tl, Ttuple tl' when List.length tl = List.length tl' -> |

227 |
List.iter2 semi_unify tl tl' |

228 |
| Tstruct fl, Tstruct fl' when List.map fst fl = List.map fst fl' -> |

229 |
List.iter2 (fun (_, t) (_, t') -> semi_unify t t') fl fl' |

230 |
| Tclock _, Tstatic _ |

231 |
| Tstatic _, Tclock _ -> raise (Unify (t1, t2)) |

232 |
| Tclock t1', _ -> semi_unify t1' t2 |

233 |
| _, Tclock t2' -> semi_unify t1 t2' |

234 |
| Tint, Tint | Tbool, Tbool | Trat, Trat |

235 |
| Tunivar, _ | _, Tunivar -> () |

236 |
| (Tconst t, _) -> |

237 |
let def_t = get_type_definition t in |

238 |
semi_unify def_t t2 |

239 |
| (_, Tconst t) -> |

240 |
let def_t = get_type_definition t in |

241 |
semi_unify t1 def_t |

242 |
| Tenum tl, Tenum tl' when tl == tl' -> () |

243 | |

244 |
| Tstatic (e1, t1'), Tstatic (e2, t2') |

245 |
| Tarray (e1, t1'), Tarray (e2, t2') -> |

246 |
begin |

247 |
semi_unify t1' t2'; |

248 |
Dimension.eval Basic_library.eval_env (fun c -> Some (Dimension.mkdim_ident Location.dummy_loc c)) e1; |

249 |
Dimension.eval Basic_library.eval_env (fun c -> Some (Dimension.mkdim_ident Location.dummy_loc c)) e2; |

250 |
Dimension.semi_unify e1 e2; |

251 |
end |

252 |
| _,_ -> raise (Unify (t1, t2)) |

253 | |

254 |
(* Expected type ty1, got type ty2 *) |

255 |
let try_unify ty1 ty2 loc = |

256 |
try |

257 |
unify ty1 ty2 |

258 |
with |

259 |
| Unify _ -> |

260 |
raise (Error (loc, Type_clash (ty1,ty2))) |

261 |
| Dimension.Unify _ -> |

262 |
raise (Error (loc, Type_clash (ty1,ty2))) |

263 | |

264 |
let try_semi_unify ty1 ty2 loc = |

265 |
try |

266 |
semi_unify ty1 ty2 |

267 |
with |

268 |
| Unify _ -> |

269 |
raise (Error (loc, Type_clash (ty1,ty2))) |

270 |
| Dimension.Unify _ -> |

271 |
raise (Error (loc, Type_clash (ty1,ty2))) |

272 | |

273 |
(* ty1 is a subtype of ty2 *) |

274 |
let rec sub_unify sub ty1 ty2 = |

275 |
match (repr ty1).tdesc, (repr ty2).tdesc with |

276 |
| Ttuple [t1] , Ttuple [t2] -> sub_unify sub t1 t2 |

277 |
| Ttuple tl1 , Ttuple tl2 -> |

278 |
if List.length tl1 <> List.length tl2 |

279 |
then raise (Unify (ty1, ty2)) |

280 |
else List.iter2 (sub_unify sub) tl1 tl2 |

281 |
| Ttuple [t1] , _ -> sub_unify sub t1 ty2 |

282 |
| _ , Ttuple [t2] -> sub_unify sub ty1 t2 |

283 |
| Tstruct tl1 , Tstruct tl2 -> |

284 |
if List.map fst tl1 <> List.map fst tl2 |

285 |
then raise (Unify (ty1, ty2)) |

286 |
else List.iter2 (fun (_, t1) (_, t2) -> sub_unify sub t1 t2) tl1 tl2 |

287 |
| Tstatic (d1, t1) , Tstatic (d2, t2) -> |

288 |
begin |

289 |
sub_unify sub t1 t2; |

290 |
Dimension.eval Basic_library.eval_env (fun c -> None) d1; |

291 |
Dimension.eval Basic_library.eval_env (fun c -> None) d2; |

292 |
Dimension.unify d1 d2 |

293 |
end |

294 |
| Tstatic (r_d, t1) , _ when sub -> sub_unify sub ty2 t1 |

295 |
| _ -> unify ty2 ty1 |

296 | |

297 |
let try_sub_unify sub ty1 ty2 loc = |

298 |
try |

299 |
sub_unify sub ty1 ty2 |

300 |
with |

301 |
| Unify _ -> |

302 |
raise (Error (loc, Type_clash (ty1,ty2))) |

303 |
| Dimension.Unify _ -> |

304 |
raise (Error (loc, Type_clash (ty1,ty2))) |

305 | |

306 |
let type_struct_field loc ftyp (label, f) = |

307 |
if Hashtbl.mem field_table label |

308 |
then let tydec = Hashtbl.find field_table label in |

309 |
let tydec_struct = get_struct_type_fields tydec in |

310 |
let ty_label = type_coretype (fun d -> ()) (List.assoc label tydec_struct) in |

311 |
begin |

312 |
try_unify ty_label (ftyp loc f) loc; |

313 |
type_coretype (fun d -> ()) tydec |

314 |
end |

315 |
else raise (Error (loc, Unbound_value ("struct field " ^ label))) |

316 | |

317 |
let rec type_const loc c = |

318 |
match c with |

319 |
| Const_int _ -> Type_predef.type_int |

320 |
| Const_real _ -> Type_predef.type_real |

321 |
| Const_float _ -> Type_predef.type_real |

322 |
| Const_array ca -> let d = Dimension.mkdim_int loc (List.length ca) in |

323 |
let ty = new_var () in |

324 |
List.iter (fun e -> try_unify ty (type_const loc e) loc) ca; |

325 |
Type_predef.type_array d ty |

326 |
| Const_tag t -> |

327 |
if Hashtbl.mem tag_table t |

328 |
then type_coretype (fun d -> ()) (Hashtbl.find tag_table t) |

329 |
else raise (Error (loc, Unbound_value ("enum tag " ^ t))) |

330 |
| Const_struct fl -> |

331 |
let ty_struct = new_var () in |

332 |
begin |

333 |
List.iter (fun f -> try_unify ty_struct (type_struct_field loc type_const f) loc) fl; |

334 |
ty_struct |

335 |
end |

336 | |

337 |
(* The following typing functions take as parameter an environment [env] |

338 |
and whether the element being typed is expected to be constant [const]. |

339 |
[env] is a pair composed of: |

340 |
- a map from ident to type, associating to each ident, i.e. |

341 |
variables, constants and (imported) nodes, its type including whether |

342 |
it is constant or not. This latter information helps in checking constant |

343 |
propagation policy in Lustre. |

344 |
- a vdecl list, in order to modify types of declared variables that are |

345 |
later discovered to be clocks during the typing process. |

346 |
*) |

347 |
let check_constant loc const_expected const_real = |

348 |
if const_expected && not const_real |

349 |
then raise (Error (loc, Not_a_constant)) |

350 | |

351 |
let rec type_standard_args env in_main const expr_list = |

352 |
let ty_list = List.map (fun e -> dynamic_type (type_expr env in_main const e)) expr_list in |

353 |
let ty_res = new_var () in |

354 |
List.iter2 (fun e ty -> try_unify ty_res ty e.expr_loc) expr_list ty_list; |

355 |
ty_res |

356 | |

357 |
(* emulates a subtyping relation between types t and (d : t), |

358 |
used during node applications and assignments *) |

359 |
and type_subtyping_arg env in_main ?(sub=true) const real_arg formal_type = |

360 |
let loc = real_arg.expr_loc in |

361 |
let const = const || (Types.get_static_value formal_type <> None) in |

362 |
let real_type = type_expr env in_main const real_arg in |

363 |
let real_type = |

364 |
if const |

365 |
then let d = |

366 |
if is_dimension_type real_type |

367 |
then dimension_of_expr real_arg |

368 |
else Dimension.mkdim_var () in |

369 |
let eval_const id = Types.get_static_value (Env.lookup_value (fst env) id) in |

370 |
Dimension.eval Basic_library.eval_env eval_const d; |

371 |
let real_static_type = Type_predef.type_static d (Types.dynamic_type real_type) in |

372 |
(match Types.get_static_value real_type with |

373 |
| None -> () |

374 |
| Some d' -> try_unify real_type real_static_type loc); |

375 |
real_static_type |

376 |
else real_type in |

377 |
(*Format.eprintf "subtyping const %B real %a:%a vs formal %a@." const Printers.pp_expr real_arg Types.print_ty real_type Types.print_ty formal_type;*) |

378 |
try_sub_unify sub real_type formal_type loc |

379 |
(* |

380 |
and type_subtyping_tuple loc real_type formal_type = |

381 |
let real_types = type_list_of_type real_type in |

382 |
let formal_types = type_list_of_type formal_type in |

383 |
if (List.length real_types) <> (List.length formal_types) |

384 |
then raise (Unify (real_type, formal_type)) |

385 |
else List.iter2 (type_subtyping loc sub) real_types formal_types |

386 | |

387 |
and type_subtyping loc sub real_type formal_type = |

388 |
match (repr real_type).tdesc, (repr formal_type).tdesc with |

389 |
| Tstatic _ , Tstatic _ when sub -> try_unify formal_type real_type loc |

390 |
| Tstatic (r_d, r_ty), _ when sub -> try_unify formal_type r_ty loc |

391 |
| _ -> try_unify formal_type real_type loc |

392 |
*) |

393 |
and type_ident env in_main loc const id = |

394 |
type_expr env in_main const (expr_of_ident id loc) |

395 | |

396 |
(* typing an application implies: |

397 |
- checking that const formal parameters match real const (maybe symbolic) arguments |

398 |
- checking type adequation between formal and real arguments |

399 |
*) |

400 |
and type_appl env in_main loc const f args = |

401 |
let tfun = type_ident env in_main loc const f in |

402 |
let tins, touts = split_arrow tfun in |

403 |
let tins = type_list_of_type tins in |

404 |
let args = expr_list_of_expr args in |

405 |
List.iter2 (type_subtyping_arg env in_main const) args tins; |

406 |
touts |

407 | |

408 |
(** [type_expr env in_main expr] types expression [expr] in environment |

409 |
[env], expecting it to be [const] or not. *) |

410 |
and type_expr env in_main const expr = |

411 |
let res = |

412 |
match expr.expr_desc with |

413 |
| Expr_const c -> |

414 |
let ty = type_const expr.expr_loc c in |

415 |
let ty = Type_predef.type_static (Dimension.mkdim_var ()) ty in |

416 |
expr.expr_type <- ty; |

417 |
ty |

418 |
| Expr_ident v -> |

419 |
let tyv = |

420 |
try |

421 |
Env.lookup_value (fst env) v |

422 |
with Not_found -> |

423 |
Format.eprintf "Failure in typing expr %a@." Printers.pp_expr expr; |

424 |
raise (Error (expr.expr_loc, Unbound_value ("identifier " ^ v))) |

425 |
in |

426 |
let ty = instantiate (ref []) (ref []) tyv in |

427 |
let ty' = |

428 |
if const |

429 |
then Type_predef.type_static (Dimension.mkdim_var ()) (new_var ()) |

430 |
else new_var () in |

431 |
try_unify ty ty' expr.expr_loc; |

432 |
expr.expr_type <- ty; |

433 |
ty |

434 |
| Expr_array elist -> |

435 |
let ty_elt = type_standard_args env in_main const elist in |

436 |
let d = Dimension.mkdim_int expr.expr_loc (List.length elist) in |

437 |
let ty = Type_predef.type_array d ty_elt in |

438 |
expr.expr_type <- ty; |

439 |
ty |

440 |
| Expr_access (e1, d) -> |

441 |
type_subtyping_arg env in_main true (expr_of_dimension d) Type_predef.type_int; |

442 |
let ty_elt = new_var () in |

443 |
let d = Dimension.mkdim_var () in |

444 |
type_subtyping_arg env in_main const e1 (Type_predef.type_array d ty_elt); |

445 |
expr.expr_type <- ty_elt; |

446 |
ty_elt |

447 |
| Expr_power (e1, d) -> |

448 |
let eval_const id = Types.get_static_value (Env.lookup_value (fst env) id) in |

449 |
type_subtyping_arg env in_main true (expr_of_dimension d) Type_predef.type_int; |

450 |
Dimension.eval Basic_library.eval_env eval_const d; |

451 |
let ty_elt = type_standard_args env in_main const [e1] in |

452 |
let ty = Type_predef.type_array d ty_elt in |

453 |
expr.expr_type <- ty; |

454 |
ty |

455 |
| Expr_tuple elist -> |

456 |
let ty = new_ty (Ttuple (List.map (type_expr env in_main const) elist)) in |

457 |
expr.expr_type <- ty; |

458 |
ty |

459 |
| Expr_ite (c, t, e) -> |

460 |
type_subtyping_arg env in_main const c Type_predef.type_bool; |

461 |
let ty = type_standard_args env in_main const [t; e] in |

462 |
expr.expr_type <- ty; |

463 |
ty |

464 |
| Expr_appl (id, args, r) -> |

465 |
(* application of non internal function is not legal in a constant |

466 |
expression *) |

467 |
(match r with |

468 |
| None -> () |

469 |
| Some (x, l) -> |

470 |
check_constant expr.expr_loc const false; |

471 |
let expr_x = expr_of_ident x expr.expr_loc in |

472 |
let typ_l = |

473 |
Type_predef.type_clock |

474 |
(type_const expr.expr_loc (Const_tag l)) in |

475 |
type_subtyping_arg env in_main ~sub:false const expr_x typ_l); |

476 |
let touts = type_appl env in_main expr.expr_loc const id args in |

477 |
expr.expr_type <- touts; |

478 |
touts |

479 |
| Expr_fby (e1,e2) |

480 |
| Expr_arrow (e1,e2) -> |

481 |
(* fby/arrow is not legal in a constant expression *) |

482 |
check_constant expr.expr_loc const false; |

483 |
let ty = type_standard_args env in_main const [e1; e2] in |

484 |
expr.expr_type <- ty; |

485 |
ty |

486 |
| Expr_pre e -> |

487 |
(* pre is not legal in a constant expression *) |

488 |
check_constant expr.expr_loc const false; |

489 |
let ty = type_standard_args env in_main const [e] in |

490 |
expr.expr_type <- ty; |

491 |
ty |

492 |
| Expr_when (e1,c,l) -> |

493 |
(* when is not legal in a constant expression *) |

494 |
check_constant expr.expr_loc const false; |

495 |
let typ_l = Type_predef.type_clock (type_const expr.expr_loc (Const_tag l)) in |

496 |
let expr_c = expr_of_ident c expr.expr_loc in |

497 |
type_subtyping_arg env in_main ~sub:false const expr_c typ_l; |

498 |
update_clock env in_main c expr.expr_loc typ_l; |

499 |
let ty = type_standard_args env in_main const [e1] in |

500 |
expr.expr_type <- ty; |

501 |
ty |

502 |
| Expr_merge (c,hl) -> |

503 |
(* merge is not legal in a constant expression *) |

504 |
check_constant expr.expr_loc const false; |

505 |
let typ_in, typ_out = type_branches env in_main expr.expr_loc const hl in |

506 |
let expr_c = expr_of_ident c expr.expr_loc in |

507 |
let typ_l = Type_predef.type_clock typ_in in |

508 |
type_subtyping_arg env in_main ~sub:false const expr_c typ_l; |

509 |
update_clock env in_main c expr.expr_loc typ_l; |

510 |
expr.expr_type <- typ_out; |

511 |
typ_out |

512 |
| Expr_uclock (e,k) | Expr_dclock (e,k) -> |

513 |
let ty = type_expr env in_main const e in |

514 |
expr.expr_type <- ty; |

515 |
ty |

516 |
| Expr_phclock (e,q) -> |

517 |
let ty = type_expr env in_main const e in |

518 |
expr.expr_type <- ty; |

519 |
ty |

520 |
in (*Format.eprintf "typing %B %a at %a = %a@." const Printers.pp_expr expr Location.pp_loc expr.expr_loc Types.print_ty res;*) res |

521 | |

522 |
and type_branches env in_main loc const hl = |

523 |
let typ_in = new_var () in |

524 |
let typ_out = new_var () in |

525 |
try |

526 |
let used_labels = |

527 |
List.fold_left (fun accu (t, h) -> |

528 |
unify typ_in (type_const loc (Const_tag t)); |

529 |
type_subtyping_arg env in_main const h typ_out; |

530 |
if List.mem t accu |

531 |
then raise (Error (loc, Already_bound t)) |

532 |
else t :: accu) [] hl in |

533 |
let type_labels = get_enum_type_tags (coretype_type typ_in) in |

534 |
if List.sort compare used_labels <> List.sort compare type_labels |

535 |
then let unbound_tag = List.find (fun t -> not (List.mem t used_labels)) type_labels in |

536 |
raise (Error (loc, Unbound_value ("branching tag " ^ unbound_tag))) |

537 |
else (typ_in, typ_out) |

538 |
with Unify (t1, t2) -> |

539 |
raise (Error (loc, Type_clash (t1,t2))) |

540 | |

541 |
and update_clock env in_main id loc typ = |

542 |
(*Log.report ~level:1 (fun fmt -> Format.fprintf fmt "update_clock %s with %a@ " id print_ty typ);*) |

543 |
try |

544 |
let vdecl = List.find (fun v -> v.var_id = id) (snd env) |

545 |
in vdecl.var_type <- typ |

546 |
with |

547 |
Not_found -> |

548 |
raise (Error (loc, Unbound_value ("clock " ^ id))) |

549 | |

550 |
(** [type_eq env eq] types equation [eq] in environment [env] *) |

551 |
let type_eq env in_main undefined_vars eq = |

552 |
(* Check undefined variables, type lhs *) |

553 |
let expr_lhs = expr_of_expr_list eq.eq_loc (List.map (fun v -> expr_of_ident v eq.eq_loc) eq.eq_lhs) in |

554 |
let ty_lhs = type_expr env in_main false expr_lhs in |

555 |
(* Check multiple variable definitions *) |

556 |
let define_var id uvars = |

557 |
try |

558 |
ignore(IMap.find id uvars); |

559 |
IMap.remove id uvars |

560 |
with Not_found -> |

561 |
raise (Error (eq.eq_loc, Already_defined id)) |

562 |
in |

563 |
let undefined_vars = |

564 |
List.fold_left (fun uvars v -> define_var v uvars) undefined_vars eq.eq_lhs in |

565 |
(* Type rhs wrt to lhs type with subtyping, i.e. a constant rhs value may be assigned |

566 |
to a (always non-constant) lhs variable *) |

567 |
type_subtyping_arg env in_main false eq.eq_rhs ty_lhs; |

568 |
undefined_vars |

569 | |

570 | |

571 |
(* [type_coreclock env ck id loc] types the type clock declaration [ck] |

572 |
in environment [env] *) |

573 |
let type_coreclock env ck id loc = |

574 |
match ck.ck_dec_desc with |

575 |
| Ckdec_any | Ckdec_pclock (_,_) -> () |

576 |
| Ckdec_bool cl -> |

577 |
let dummy_id_expr = expr_of_ident id loc in |

578 |
let when_expr = |

579 |
List.fold_left |

580 |
(fun expr (x, l) -> |

581 |
{expr_tag = new_tag (); |

582 |
expr_desc= Expr_when (expr,x,l); |

583 |
expr_type = new_var (); |

584 |
expr_clock = Clocks.new_var true; |

585 |
expr_delay = Delay.new_var (); |

586 |
expr_loc=loc; |

587 |
expr_annot = None}) |

588 |
dummy_id_expr cl |

589 |
in |

590 |
ignore (type_expr env false false when_expr) |

591 | |

592 |
let rec check_type_declaration loc cty = |

593 |
match cty with |

594 |
| Tydec_clock ty |

595 |
| Tydec_array (_, ty) -> check_type_declaration loc ty |

596 |
| Tydec_const tname -> |

597 |
if not (Hashtbl.mem type_table cty) |

598 |
then raise (Error (loc, Unbound_type tname)); |

599 |
| _ -> () |

600 | |

601 |
let type_var_decl vd_env env vdecl = |

602 |
check_type_declaration vdecl.var_loc vdecl.var_dec_type.ty_dec_desc; |

603 |
let eval_const id = Types.get_static_value (Env.lookup_value env id) in |

604 |
let type_dim d = |

605 |
begin |

606 |
type_subtyping_arg (env, vd_env) false true (expr_of_dimension d) Type_predef.type_int; |

607 |
Dimension.eval Basic_library.eval_env eval_const d; |

608 |
end in |

609 |
let ty = type_coretype type_dim vdecl.var_dec_type.ty_dec_desc in |

610 |
let ty_status = |

611 |
if vdecl.var_dec_const |

612 |
then Type_predef.type_static (Dimension.mkdim_var ()) ty |

613 |
else ty in |

614 |
let new_env = Env.add_value env vdecl.var_id ty_status in |

615 |
type_coreclock (new_env,vd_env) vdecl.var_dec_clock vdecl.var_id vdecl.var_loc; |

616 |
vdecl.var_type <- ty_status; |

617 |
new_env |

618 | |

619 |
let type_var_decl_list vd_env env l = |

620 |
List.fold_left (type_var_decl vd_env) env l |

621 | |

622 |
let type_of_vlist vars = |

623 |
let tyl = List.map (fun v -> v.var_type) vars in |

624 |
type_of_type_list tyl |

625 | |

626 |
let add_vdecl vd_env vdecl = |

627 |
if List.exists (fun v -> v.var_id = vdecl.var_id) vd_env |

628 |
then raise (Error (vdecl.var_loc, Already_bound vdecl.var_id)) |

629 |
else vdecl::vd_env |

630 | |

631 |
let check_vd_env vd_env = |

632 |
ignore (List.fold_left add_vdecl [] vd_env) |

633 | |

634 |
(** [type_node env nd loc] types node [nd] in environment env. The |

635 |
location is used for error reports. *) |

636 |
let type_node env nd loc = |

637 |
let is_main = nd.node_id = !Options.main_node in |

638 |
let vd_env_ol = nd.node_outputs@nd.node_locals in |

639 |
let vd_env = nd.node_inputs@vd_env_ol in |

640 |
check_vd_env vd_env; |

641 |
let init_env = env in |

642 |
let delta_env = type_var_decl_list vd_env init_env nd.node_inputs in |

643 |
let delta_env = type_var_decl_list vd_env delta_env nd.node_outputs in |

644 |
let delta_env = type_var_decl_list vd_env delta_env nd.node_locals in |

645 |
let new_env = Env.overwrite env delta_env in |

646 |
let undefined_vars_init = |

647 |
List.fold_left |

648 |
(fun uvs v -> IMap.add v.var_id () uvs) |

649 |
IMap.empty vd_env_ol in |

650 |
let undefined_vars = |

651 |
List.fold_left (type_eq (new_env, vd_env) is_main) undefined_vars_init nd.node_eqs |

652 |
in |

653 |
(* check that table is empty *) |

654 |
if (not (IMap.is_empty undefined_vars)) then |

655 |
raise (Error (loc, Undefined_var undefined_vars)); |

656 |
let ty_ins = type_of_vlist nd.node_inputs in |

657 |
let ty_outs = type_of_vlist nd.node_outputs in |

658 |
let ty_node = new_ty (Tarrow (ty_ins,ty_outs)) in |

659 |
generalize ty_node; |

660 |
(* TODO ? Check that no node in the hierarchy remains polymorphic ? *) |

661 |
nd.node_type <- ty_node; |

662 |
Env.add_value env nd.node_id ty_node |

663 | |

664 |
let type_imported_node env nd loc = |

665 |
let new_env = type_var_decl_list nd.nodei_inputs env nd.nodei_inputs in |

666 |
let vd_env = nd.nodei_inputs@nd.nodei_outputs in |

667 |
check_vd_env vd_env; |

668 |
ignore(type_var_decl_list vd_env new_env nd.nodei_outputs); |

669 |
let ty_ins = type_of_vlist nd.nodei_inputs in |

670 |
let ty_outs = type_of_vlist nd.nodei_outputs in |

671 |
let ty_node = new_ty (Tarrow (ty_ins,ty_outs)) in |

672 |
generalize ty_node; |

673 |
(* |

674 |
if (is_polymorphic ty_node) then |

675 |
raise (Error (loc, Poly_imported_node nd.nodei_id)); |

676 |
*) |

677 |
let new_env = Env.add_value env nd.nodei_id ty_node in |

678 |
nd.nodei_type <- ty_node; |

679 |
new_env |

680 | |

681 |
let type_imported_fun env nd loc = |

682 |
let new_env = type_var_decl_list nd.fun_inputs env nd.fun_inputs in |

683 |
let vd_env = nd.fun_inputs@nd.fun_outputs in |

684 |
check_vd_env vd_env; |

685 |
ignore(type_var_decl_list vd_env new_env nd.fun_outputs); |

686 |
let ty_ins = type_of_vlist nd.fun_inputs in |

687 |
let ty_outs = type_of_vlist nd.fun_outputs in |

688 |
let ty_node = new_ty (Tarrow (ty_ins,ty_outs)) in |

689 |
generalize ty_node; |

690 |
(* |

691 |
if (is_polymorphic ty_node) then |

692 |
raise (Error (loc, Poly_imported_node nd.fun_id)); |

693 |
*) |

694 |
let new_env = Env.add_value env nd.fun_id ty_node in |

695 |
nd.fun_type <- ty_node; |

696 |
new_env |

697 | |

698 |
let type_top_consts env clist = |

699 |
List.fold_left (fun env cdecl -> |

700 |
let ty = type_const cdecl.const_loc cdecl.const_value in |

701 |
let d = |

702 |
if is_dimension_type ty |

703 |
then dimension_of_const cdecl.const_loc cdecl.const_value |

704 |
else Dimension.mkdim_var () in |

705 |
let ty = Type_predef.type_static d ty in |

706 |
let new_env = Env.add_value env cdecl.const_id ty in |

707 |
cdecl.const_type <- ty; |

708 |
new_env) env clist |

709 | |

710 |
let type_top_decl env decl = |

711 |
match decl.top_decl_desc with |

712 |
| Node nd -> ( |

713 |
try |

714 |
type_node env nd decl.top_decl_loc |

715 |
with Error (loc, err) as exc -> ( |

716 |
if !Options.global_inline then |

717 |
Format.eprintf "Type error: failing node@.%a@.@?" |

718 |
Printers.pp_node nd |

719 |
; |

720 |
raise exc) |

721 |
) |

722 |
| ImportedNode nd -> |

723 |
type_imported_node env nd decl.top_decl_loc |

724 |
| ImportedFun nd -> |

725 |
type_imported_fun env nd decl.top_decl_loc |

726 |
| Consts clist -> |

727 |
type_top_consts env clist |

728 |
| Open _ -> env |

729 | |

730 |
let type_prog env decls = |

731 |
try |

732 |
List.fold_left type_top_decl env decls |

733 |
with Failure _ as exc -> raise exc |

734 | |

735 |
(* Once the Lustre program is fully typed, |

736 |
we must get back to the original description of dimensions, |

737 |
with constant parameters, instead of unifiable internal variables. *) |

738 | |

739 |
(* The following functions aims at 'unevaluating' dimension expressions occuring in array types, |

740 |
i.e. replacing unifiable second_order variables with the original static parameters. |

741 |
Once restored in this formulation, dimensions may be meaningfully printed. |

742 |
*) |

743 |
let uneval_vdecl_generics vdecl = |

744 |
if vdecl.var_dec_const |

745 |
then |

746 |
match get_static_value vdecl.var_type with |

747 |
| None -> (Format.eprintf "internal error: %a@." Types.print_ty vdecl.var_type; assert false) |

748 |
| Some d -> Dimension.uneval vdecl.var_id d |

749 | |

750 |
let uneval_node_generics vdecls = |

751 |
List.iter uneval_vdecl_generics vdecls |

752 | |

753 |
let uneval_top_generics decl = |

754 |
match decl.top_decl_desc with |

755 |
| Node nd -> |

756 |
uneval_node_generics (nd.node_inputs @ nd.node_outputs) |

757 |
| ImportedNode nd -> |

758 |
uneval_node_generics (nd.nodei_inputs @ nd.nodei_outputs) |

759 |
| ImportedFun nd -> |

760 |
() |

761 |
| Consts clist -> () |

762 |
| Open _ -> () |

763 | |

764 |
let uneval_prog_generics prog = |

765 |
List.iter uneval_top_generics prog |

766 | |

767 |
let check_env_compat header declared computed = |

768 |
uneval_prog_generics header; |

769 |
Env.iter declared (fun k decl_type_k -> |

770 |
let computed_t = instantiate (ref []) (ref []) (Env.lookup_value computed k) in |

771 |
(*Types.print_ty Format.std_formatter decl_type_k; |

772 |
Types.print_ty Format.std_formatter computed_t;*) |

773 |
try_semi_unify decl_type_k computed_t Location.dummy_loc |

774 |
) |

775 | |

776 |
(* Local Variables: *) |

777 |
(* compile-command:"make -C .." *) |

778 |
(* End: *) |