1
|
(* This file is part of the Kind 2 model checker.
|
2
|
|
3
|
Copyright (c) 2015 by the Board of Trustees of the University of Iowa
|
4
|
|
5
|
Licensed under the Apache License, Version 2.0 (the "License"); you
|
6
|
may not use this file except in compliance with the License. You
|
7
|
may obtain a copy of the License at
|
8
|
|
9
|
http://www.apache.org/licenses/LICENSE-2.0
|
10
|
|
11
|
Unless required by applicable law or agreed to in writing, software
|
12
|
distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
14
|
implied. See the License for the specific language governing
|
15
|
permissions and limitations under the License.
|
16
|
|
17
|
*)
|
18
|
|
19
|
(** Minimally simplified Lustre abstract syntax tree
|
20
|
|
21
|
The types in this module closely represent the abstract syntax of
|
22
|
Lustre. No type checking or simplification is performed when
|
23
|
constructing the abstract syntax tree, this is done when producing
|
24
|
the intermediate Lustre representation in {!LustreDeclarations}.
|
25
|
|
26
|
Some values are reserved for future use and will cause the
|
27
|
translation to intermediate Lustre to fail.
|
28
|
|
29
|
A Lustre file is parsed into a {!declaration} list, where a
|
30
|
declaration is either
|
31
|
|
32
|
- a type definition [type t = t'],
|
33
|
- a constant definition [const c = v], or
|
34
|
- a node declaration.
|
35
|
|
36
|
Almost all types are annotated with the position in the input file
|
37
|
for better error reporting in the translation.
|
38
|
|
39
|
@author Christoph Sticksel *)
|
40
|
|
41
|
open KindLib
|
42
|
|
43
|
module SI : Set.S with type elt = KindIdent.t
|
44
|
|
45
|
(** Error while parsing *)
|
46
|
exception Parser_error
|
47
|
|
48
|
(** {1 Types} *)
|
49
|
|
50
|
(** An identifier *)
|
51
|
type ident = string
|
52
|
|
53
|
(** A single index *)
|
54
|
type index = string
|
55
|
|
56
|
(** A clock expression *)
|
57
|
type clock_expr =
|
58
|
| ClockTrue
|
59
|
| ClockPos of ident
|
60
|
| ClockNeg of ident
|
61
|
| ClockConstr of ident * ident
|
62
|
|
63
|
(** A Lustre expression *)
|
64
|
type expr =
|
65
|
Ident of position * ident
|
66
|
| ModeRef of position * ident list
|
67
|
| RecordProject of position * expr * index
|
68
|
| TupleProject of position * expr * expr
|
69
|
| StructUpdate of position * expr * label_or_index list * expr
|
70
|
| True of position
|
71
|
| False of position
|
72
|
| Num of position * string
|
73
|
| Dec of position * string
|
74
|
| ToInt of position * expr
|
75
|
| ToReal of position * expr
|
76
|
| ExprList of position * expr list
|
77
|
| TupleExpr of position * expr list
|
78
|
| ArrayExpr of position * expr list
|
79
|
| ArrayConstr of position * expr * expr
|
80
|
| ArraySlice of position * expr * (expr * expr)
|
81
|
| ArrayConcat of position * expr * expr
|
82
|
| RecordExpr of position * ident * (ident * expr) list
|
83
|
| Not of position * expr
|
84
|
| And of position * expr * expr
|
85
|
| Or of position * expr * expr
|
86
|
| Xor of position * expr * expr
|
87
|
| Impl of position * expr * expr
|
88
|
| Forall of position * typed_ident list * expr
|
89
|
| Exists of position * typed_ident list * expr
|
90
|
| OneHot of position * expr list
|
91
|
| Uminus of position * expr
|
92
|
| Mod of position * expr * expr
|
93
|
| Minus of position * expr * expr
|
94
|
| Plus of position * expr * expr
|
95
|
| Div of position * expr * expr
|
96
|
| Times of position * expr * expr
|
97
|
| IntDiv of position * expr * expr
|
98
|
| Ite of position * expr * expr * expr
|
99
|
| With of position * expr * expr * expr
|
100
|
| Eq of position * expr * expr
|
101
|
| Neq of position * expr * expr
|
102
|
| Lte of position * expr * expr
|
103
|
| Lt of position * expr * expr
|
104
|
| Gte of position * expr * expr
|
105
|
| Gt of position * expr * expr
|
106
|
| When of position * expr * clock_expr
|
107
|
| Current of position * expr
|
108
|
| Condact of position * expr * expr * ident * expr list * expr list
|
109
|
| Activate of position * ident * expr * expr * expr list
|
110
|
| Merge of position * ident * (ident * expr) list
|
111
|
| RestartEvery of position * ident * expr list * expr
|
112
|
| Pre of position * expr
|
113
|
| Last of position * ident
|
114
|
| Fby of position * expr * int * expr
|
115
|
| Arrow of position * expr * expr
|
116
|
| Call of position * ident * expr list
|
117
|
| CallParam of position * ident * lustre_type list * expr list
|
118
|
|
119
|
(** A Lustre type *)
|
120
|
and lustre_type =
|
121
|
Bool of position
|
122
|
| Int of position
|
123
|
| IntRange of position * expr * expr
|
124
|
| Real of position
|
125
|
| UserType of position * ident
|
126
|
| TupleType of position * lustre_type list
|
127
|
| RecordType of position * typed_ident list
|
128
|
| ArrayType of position * (lustre_type * expr)
|
129
|
| EnumType of position * ident option * ident list
|
130
|
|
131
|
(** An identifier with a type *)
|
132
|
and typed_ident = position * ident * lustre_type
|
133
|
|
134
|
(** A record field or an array or tuple index *)
|
135
|
and label_or_index =
|
136
|
| Label of position * index
|
137
|
| Index of position * expr
|
138
|
|
139
|
(** {1 Declarations} *)
|
140
|
|
141
|
(** A declaration of an alias or free type *)
|
142
|
type type_decl =
|
143
|
| AliasType of position * ident * lustre_type
|
144
|
| FreeType of position * ident
|
145
|
|
146
|
(** An identifier with a type and a clock as used for the type of variables *)
|
147
|
type clocked_typed_decl = position * ident * lustre_type * clock_expr
|
148
|
|
149
|
(** An identifier, possibly flagged as constant, with a type and a
|
150
|
clock as used for the type of variables *)
|
151
|
type const_clocked_typed_decl = position * ident * lustre_type * clock_expr * bool
|
152
|
|
153
|
(** A constant declaration *)
|
154
|
type const_decl =
|
155
|
| FreeConst of position * ident * lustre_type
|
156
|
| UntypedConst of position * ident * expr
|
157
|
| TypedConst of position * ident * expr * lustre_type
|
158
|
|
159
|
(** A type parameter of a node *)
|
160
|
type node_param =
|
161
|
| TypeParam of ident
|
162
|
|
163
|
(** A local constant or variable declaration of a node *)
|
164
|
type node_local_decl =
|
165
|
| NodeConstDecl of position * const_decl
|
166
|
| NodeVarDecl of position * clocked_typed_decl
|
167
|
|
168
|
(** Structural assignment on the left-hand side of an equation *)
|
169
|
type struct_item =
|
170
|
| SingleIdent of position * ident
|
171
|
| TupleStructItem of position * struct_item list
|
172
|
| TupleSelection of position * ident * expr
|
173
|
| FieldSelection of position * ident * ident
|
174
|
| ArraySliceStructItem of position * ident * (expr * expr) list
|
175
|
| ArrayDef of position * ident * ident list
|
176
|
|
177
|
(** The left-hand side of an equation *)
|
178
|
type eq_lhs =
|
179
|
| StructDef of position * struct_item list
|
180
|
|
181
|
type transition_to =
|
182
|
| TransRestart of position * (position * ident)
|
183
|
| TransResume of position * (position * ident)
|
184
|
|
185
|
type transition_branch =
|
186
|
| Target of transition_to
|
187
|
| TransIf of position * expr *
|
188
|
transition_branch * transition_branch option
|
189
|
|
190
|
type automaton_transition = position * transition_branch
|
191
|
|
192
|
type auto_returns = Given of ident list | Inferred
|
193
|
|
194
|
(** An equation or assertion in the node body *)
|
195
|
type node_equation =
|
196
|
| Assert of position * expr
|
197
|
| Equation of position * eq_lhs * expr
|
198
|
| Automaton of position * ident option * state list * auto_returns
|
199
|
|
200
|
and state =
|
201
|
| State of position * ident * bool *
|
202
|
node_local_decl list *
|
203
|
node_equation list *
|
204
|
automaton_transition option *
|
205
|
automaton_transition option
|
206
|
|
207
|
(** An item in a node declaration *)
|
208
|
type node_item =
|
209
|
| Body of node_equation
|
210
|
| AnnotMain of bool
|
211
|
| AnnotProperty of position * string option * expr
|
212
|
|
213
|
(* A contract ghost constant. *)
|
214
|
type contract_ghost_const = const_decl
|
215
|
|
216
|
(* A contract ghost variable. *)
|
217
|
type contract_ghost_var = const_decl
|
218
|
|
219
|
(* A contract assume. *)
|
220
|
type contract_assume = position * string option * expr
|
221
|
|
222
|
(* A contract guarantee. *)
|
223
|
type contract_guarantee = position * string option * expr
|
224
|
|
225
|
(* A contract requirement. *)
|
226
|
type contract_require = position * string option * expr
|
227
|
|
228
|
(* A contract ensure. *)
|
229
|
type contract_ensure = position * string option * expr
|
230
|
|
231
|
(* A contract mode. *)
|
232
|
type contract_mode =
|
233
|
position * ident * (contract_require list) * (contract_ensure list)
|
234
|
|
235
|
(* A contract call. *)
|
236
|
type contract_call = position * ident * expr list * expr list
|
237
|
|
238
|
(* Equations that can appear in a contract node. *)
|
239
|
type contract_node_equation =
|
240
|
| GhostConst of contract_ghost_const
|
241
|
| GhostVar of contract_ghost_var
|
242
|
| Assume of contract_assume
|
243
|
| Guarantee of contract_guarantee
|
244
|
| Mode of contract_mode
|
245
|
| ContractCall of contract_call
|
246
|
|
247
|
(* A contract is some ghost consts / var, and assumes guarantees and modes. *)
|
248
|
type contract = contract_node_equation list
|
249
|
|
250
|
(* contract_ghost_const list * contract_ghost_var list * ( *)
|
251
|
(* contract_assume list * contract_guarantee list * *)
|
252
|
(* contract_mode list * contract_call list *)
|
253
|
(* ) list *)
|
254
|
|
255
|
(** Declaration of a node or function as a tuple of
|
256
|
|
257
|
- its identifier,
|
258
|
- a flag, true if the node / function is extern
|
259
|
- its type parameters,
|
260
|
- the list of its inputs,
|
261
|
- the list of its outputs,
|
262
|
- the list of its local constant and variable declarations,
|
263
|
- its equations, assertions and annotiations, and
|
264
|
- its optional contract specification *)
|
265
|
type node_decl =
|
266
|
ident
|
267
|
* bool
|
268
|
* node_param list
|
269
|
* const_clocked_typed_decl list
|
270
|
* clocked_typed_decl list
|
271
|
* node_local_decl list
|
272
|
* node_item list
|
273
|
* contract option
|
274
|
|
275
|
(** A contract node declaration as a tuple of
|
276
|
|
277
|
- its identifier,
|
278
|
- its type parameters,
|
279
|
- its inputs,
|
280
|
- its outputs,
|
281
|
- its body as a [contract]. *)
|
282
|
type contract_node_decl =
|
283
|
ident
|
284
|
* node_param list
|
285
|
* const_clocked_typed_decl list
|
286
|
* clocked_typed_decl list
|
287
|
* contract
|
288
|
|
289
|
|
290
|
(** An instance of a parametric node as a tuple of the identifier for
|
291
|
the instance, the identifier of the parametric node and the list of
|
292
|
type parameters *)
|
293
|
type node_param_inst = ident * ident * lustre_type list
|
294
|
|
295
|
(** A declaration of a type, a constant, a node, a function or an
|
296
|
instance of a parametric node *)
|
297
|
type declaration =
|
298
|
| TypeDecl of position * type_decl
|
299
|
| ConstDecl of position * const_decl
|
300
|
| NodeDecl of position * node_decl
|
301
|
| FuncDecl of position * node_decl
|
302
|
| ContractNodeDecl of position * contract_node_decl
|
303
|
| NodeParamInst of position * node_param_inst
|
304
|
|
305
|
(** A Lustre program as a list of declarations *)
|
306
|
type t = declaration list
|
307
|
|
308
|
(** {1 Pretty-printers} *)
|
309
|
val pp_print_node_param_list : Format.formatter -> node_param list -> unit
|
310
|
val pp_print_ident : Format.formatter -> ident -> unit
|
311
|
val pp_print_expr : Format.formatter -> expr -> unit
|
312
|
val pp_print_array_slice : Format.formatter -> expr * expr -> unit
|
313
|
val pp_print_field_assign : Format.formatter -> ident * expr -> unit
|
314
|
val pp_print_clock_expr : Format.formatter -> clock_expr -> unit
|
315
|
val pp_print_lustre_type : Format.formatter -> lustre_type -> unit
|
316
|
val pp_print_typed_ident : Format.formatter -> typed_ident -> unit
|
317
|
val pp_print_clocked_typed_ident :
|
318
|
Format.formatter -> position * ident * lustre_type * clock_expr -> unit
|
319
|
val pp_print_const_clocked_typed_ident :
|
320
|
Format.formatter -> position * ident * lustre_type * clock_expr * bool -> unit
|
321
|
val pp_print_type_decl : Format.formatter -> type_decl -> unit
|
322
|
val pp_print_var_decl :
|
323
|
Format.formatter -> position * ident * lustre_type * clock_expr -> unit
|
324
|
val pp_print_const_decl : Format.formatter -> const_decl -> unit
|
325
|
val pp_print_node_local_decl_var :
|
326
|
Format.formatter -> node_local_decl -> unit
|
327
|
val pp_print_node_local_decl_const :
|
328
|
Format.formatter -> node_local_decl -> unit
|
329
|
val pp_print_node_local_decl :
|
330
|
Format.formatter -> node_local_decl list -> unit
|
331
|
val pp_print_struct_item : Format.formatter -> struct_item -> unit
|
332
|
val pp_print_node_item : Format.formatter -> node_item -> unit
|
333
|
val pp_print_declaration : Format.formatter -> declaration -> unit
|
334
|
val pp_print_program : Format.formatter -> t -> unit
|
335
|
|
336
|
val pp_print_contract_item : Format.formatter -> contract_node_equation -> unit
|
337
|
val pp_print_contract_node : Format.formatter -> contract_node_decl -> unit
|
338
|
|
339
|
|
340
|
(** {1 Helpers} *)
|
341
|
|
342
|
(** Returns the position of an expression *)
|
343
|
val pos_of_expr : expr -> KindLib.position
|
344
|
|
345
|
(** Returns true if the expression has unguareded pre's *)
|
346
|
val has_unguarded_pre : expr -> bool
|
347
|
|
348
|
(** Returns true if the expression has a `pre` or a `->`. *)
|
349
|
val has_pre_or_arrow : expr -> KindLib.position option
|
350
|
|
351
|
(** Returns true iff a contract mentions a `pre` or a `->`.
|
352
|
|
353
|
Does not (cannot) check contract calls recursively, checks only inputs and
|
354
|
outputs. *)
|
355
|
val contract_has_pre_or_arrow : contract -> KindLib.position option
|
356
|
|
357
|
(** Checks whether a node local declaration has a `pre` or a `->`. *)
|
358
|
val node_local_decl_has_pre_or_arrow : node_local_decl -> KindLib.position option
|
359
|
|
360
|
(** Checks whether a node equation has a `pre` or a `->`. *)
|
361
|
val node_item_has_pre_or_arrow : node_item -> KindLib.position option
|
362
|
|
363
|
(** [replace_lasts allowed prefix acc e] replaces [last x] expressions in AST
|
364
|
[e] by abstract identifiers prefixed with [prefix]. Only identifiers that
|
365
|
appear in the list [allowed] are allowed to appear under a last. It returns
|
366
|
the new AST expression and a set of identifers for which the last
|
367
|
application was replaced. *)
|
368
|
val replace_lasts : string list -> string -> SI.t -> expr -> expr * SI.t
|
369
|
|
370
|
|
371
|
(*
|
372
|
Local Variables:
|
373
|
compile-command: "make -k -C .."
|
374
|
indent-tabs-mode: nil
|
375
|
End:
|
376
|
*)
|