parser.mly 42.4 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
4
(*  Copyright 2010-2018   --   Inria - CNRS - Paris-Sud University  *)
5 6 7 8
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
9
(*                                                                  *)
10
(********************************************************************)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
11 12 13 14

%{
  open Ptree

Andrei Paskevich's avatar
Andrei Paskevich committed
15 16 17
  let qualid_last = function Qident x | Qdot (_, x) -> x

  let use_as q = function Some x -> x | None -> qualid_last q
18

19
  let floc s e = Loc.extract (s,e)
20

21 22 23
  let debug_auto_model =
    Debug.register_flag ~desc:"When set, model labels are not added during parsing"
      "no_auto_model"
24

25 26
  let add_lab id l =
    { id with id_lab = l }
27

28 29 30 31 32 33 34 35
  let add_model_trace_label id =
    if Debug.test_flag debug_auto_model then id else
    let is_model_trace_label l =
      match l with
      | Lpos _ -> false
      | Lstr lab -> Ident.is_model_trace_label lab
    in
    if List.exists is_model_trace_label id.id_lab then id else
36
      let l =
37 38
        (Lstr (Ident.create_model_trace_label id.id_str))
        ::(Lstr Ident.model_label) :: id.id_lab in
39
      { id with id_lab = l }
40

41 42 43
  let add_model_labels (b : binder) =
    match b with
    | (loc, Some id, ghost, ty) ->
44
      (loc, Some (add_model_trace_label id), ghost, ty)
45 46
    | _ -> b

47
  let id_anonymous loc = { id_str = "_"; id_lab = []; id_loc = loc }
48

49 50 51 52 53 54
  let mk_int_const neg lit =
    Number.{ ic_negative = neg ; ic_abs = lit}

  let mk_real_const neg lit =
    Number.{ rc_negative = neg ; rc_abs = lit}

55
  let mk_id id s e = { id_str = id; id_lab = []; id_loc = floc s e }
56

57 58 59 60 61
  let get_op s e = Qident (mk_id (Ident.mixfix "[]") s e)
  let set_op s e = Qident (mk_id (Ident.mixfix "[<-]") s e)
  let sub_op s e = Qident (mk_id (Ident.mixfix "[_.._]") s e)
  let above_op s e = Qident (mk_id (Ident.mixfix "[_..]") s e)
  let below_op s e = Qident (mk_id (Ident.mixfix "[.._]") s e)
62

63 64
  let mk_pat  d s e = { pat_desc  = d; pat_loc  = floc s e }
  let mk_term d s e = { term_desc = d; term_loc = floc s e }
65
  let mk_expr d s e = { expr_desc = d; expr_loc = floc s e }
66

67 68 69
  let variant_union v1 v2 = match v1, v2 with
    | _, [] -> v1
    | [], _ -> v2
70
    | _, ({term_loc = loc},_)::_ -> Loc.errorm ~loc
71 72 73 74 75 76
        "multiple `variant' clauses are not allowed"

  let empty_spec = {
    sp_pre     = [];
    sp_post    = [];
    sp_xpost   = [];
77
    sp_reads   = [];
78
    sp_writes  = [];
79
    sp_alias   = [];
80
    sp_variant = [];
81 82
    sp_checkrw = false;
    sp_diverge = false;
83
  }
84

85 86 87 88
  let spec_union s1 s2 = {
    sp_pre     = s1.sp_pre @ s2.sp_pre;
    sp_post    = s1.sp_post @ s2.sp_post;
    sp_xpost   = s1.sp_xpost @ s2.sp_xpost;
89
    sp_reads   = s1.sp_reads @ s2.sp_reads;
90
    sp_writes  = s1.sp_writes @ s2.sp_writes;
91
    sp_alias   = s1.sp_alias @ s2.sp_alias;
92
    sp_variant = variant_union s1.sp_variant s2.sp_variant;
93 94
    sp_checkrw = s1.sp_checkrw || s2.sp_checkrw;
    sp_diverge = s1.sp_diverge || s2.sp_diverge;
95
  }
96

Andrei Paskevich's avatar
Andrei Paskevich committed
97 98 99
  let break_id    = "'Break"
  let continue_id = "'Continue"
  let return_id   = "'Return"
100

101 102
  let error_param loc =
    Loc.errorm ~loc "cannot determine the type of the parameter"
103

104 105 106
  let error_loc loc = Loc.error ~loc Error

  let () = Exn_printer.register (fun fmt exn -> match exn with
107
    | Error -> Format.fprintf fmt "syntax error %s" (Parser_messages.message 1)
108
    | _ -> raise exn)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
109 110
%}

111
(* Tokens *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
112

113
%token <string> LIDENT CORE_LIDENT UIDENT CORE_UIDENT
114
%token <Number.integer_literal> INTEGER
115
%token <string> OP1 OP2 OP3 OP4 OPPREF
116
%token <Number.real_literal> REAL
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
117
%token <string> STRING
118
%token <string> ATTRIBUTE
119
%token <Loc.position> POSITION
120
%token <string> QUOTE_LIDENT
121

122
(* keywords *)
123

Martin Clochard's avatar
Martin Clochard committed
124
%token AS AXIOM BY CLONE COINDUCTIVE CONSTANT
Clément Fumex's avatar
Clément Fumex committed
125
%token ELSE END EPSILON EXISTS EXPORT FALSE FLOAT FORALL FUNCTION
Andrei Paskevich's avatar
Andrei Paskevich committed
126
%token GOAL IF IMPORT IN INDUCTIVE LEMMA
127
%token LET MATCH META NOT PREDICATE RANGE SCOPE
Martin Clochard's avatar
Martin Clochard committed
128
%token SO THEN THEORY TRUE TYPE USE WITH
129

130
(* program keywords *)
131

132
%token ABSTRACT ABSURD ALIAS ANY ASSERT ASSUME AT BEGIN BREAK CHECK
Andrei Paskevich's avatar
Andrei Paskevich committed
133
%token CONTINUE DIVERGES DO DONE DOWNTO ENSURES EXCEPTION FOR
134
%token FUN GHOST INVARIANT LABEL MODULE MUTABLE OLD
135 136
%token PRIVATE PURE RAISE RAISES READS REC REQUIRES
%token RETURN RETURNS TO TRY VAL VARIANT WHILE WRITES
137

138
(* symbols *)
139

Andrei Paskevich's avatar
Andrei Paskevich committed
140
%token AND ARROW
141
%token BAR
142
%token COLON COMMA
143
%token DOT DOTDOT EQUAL LT GT LTGT MINUS
144
%token LEFTPAR LEFTSQ
145
%token LARROW LRARROW OR
146
%token RIGHTPAR RIGHTSQ
Andrei Paskevich's avatar
Andrei Paskevich committed
147
%token UNDERSCORE
148 149 150

%token EOF

151
(* program symbols *)
152

153
%token AMPAMP BARBAR LEFTBRC RIGHTBRC SEMICOLON
154

155
(* Precedences *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
156

157 158
%nonassoc below_SEMI
%nonassoc SEMICOLON
159
%nonassoc LET VAL EXCEPTION
160
%nonassoc prec_no_else
161
%nonassoc DOT ELSE RETURN
Andrei Paskevich's avatar
Andrei Paskevich committed
162
%nonassoc prec_no_spec
163
%nonassoc REQUIRES ENSURES RETURNS RAISES READS WRITES ALIAS DIVERGES VARIANT
164 165 166 167 168
%nonassoc below_LARROW
%nonassoc LARROW
%nonassoc below_COMMA
%nonassoc COMMA
%nonassoc GHOST
169
%nonassoc prec_named
170
%nonassoc COLON (* weaker than -> because of t: a -> b *)
171
%right ARROW LRARROW BY SO
172 173
%right OR BARBAR
%right AND AMPAMP
Andrei Paskevich's avatar
Andrei Paskevich committed
174
%nonassoc NOT
175
%right EQUAL LTGT LT GT OP1
176
%nonassoc AT OLD
177
%left OP2 MINUS
178
%left OP3
179
%left OP4
180
%nonassoc prec_prefix_op
181
%nonassoc INTEGER REAL (* stronger than MINUS *)
182 183
%nonassoc LEFTSQ
%nonassoc OPPREF
184

185
(* Entry points *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
186

Guillaume Melquiond's avatar
Guillaume Melquiond committed
187
%start <Pmodule.pmodule Wstdlib.Mstr.t> mlw_file
188
%start <Ptree.term> term_eof
189 190
%start <Ptree.qualid> qualid_eof
%start <Ptree.qualid list> qualid_comma_list_eof
191
%start <Ptree.term list> term_comma_list_eof
192
%start <Ptree.ident list> ident_comma_list_eof
193

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
194 195
%%

196 197 198 199 200
(* parsing of a single term *)

term_eof:
| term EOF { $1 }

201
(* Modules and scopes *)
202

203
mlw_file:
204 205 206 207 208
| mlw_module* EOF
    { Typing.close_file () }
| module_decl+ EOF
    { let loc = floc $startpos($2) $endpos($2) in
      Typing.close_module loc; Typing.close_file () }
209

210
mlw_module:
211 212
| module_head module_decl* END
    { Typing.close_module (floc $startpos($3) $endpos($3)) }
213

214
module_head:
215 216
| THEORY labels(uident_nq)  { Typing.open_module $2 }
| MODULE labels(uident_nq)  { Typing.open_module $2 }
217

Andrei Paskevich's avatar
Andrei Paskevich committed
218 219
scope_head:
| SCOPE boption(IMPORT) uident
220
    { Typing.open_scope (floc $startpos $endpos) $3; $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
221

222
module_decl:
Andrei Paskevich's avatar
Andrei Paskevich committed
223
| scope_head module_decl* END
224
    { Typing.close_scope (floc $startpos($1) $endpos($1)) ~import:$1 }
225 226
| IMPORT uqualid
    { Typing.import_scope (floc $startpos $endpos) $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
227 228 229
| d = pure_decl | d = prog_decl | d = meta_decl
    { Typing.add_decl (floc $startpos $endpos) d }
| use_clone { () }
230

231
(* Use and clone *)
232

233
use_clone:
Andrei Paskevich's avatar
Andrei Paskevich committed
234 235 236 237 238 239
| USE EXPORT tqualid
    { Typing.add_decl (floc $startpos $endpos) (Duse $3) }
| CLONE EXPORT tqualid clone_subst
    { Typing.add_decl (floc $startpos $endpos) (Dclone ($3, $4)) }
| USE boption(IMPORT) tqualid option(preceded(AS, uident))
    { let loc = floc $startpos $endpos in
240
      Typing.open_scope loc (use_as $3 $4);
Andrei Paskevich's avatar
Andrei Paskevich committed
241
      Typing.add_decl loc (Duse $3);
242
      Typing.close_scope loc ~import:$2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
243 244
| CLONE boption(IMPORT) tqualid option(preceded(AS, uident)) clone_subst
    { let loc = floc $startpos $endpos in
245
      Typing.open_scope loc (use_as $3 $4);
Andrei Paskevich's avatar
Andrei Paskevich committed
246
      Typing.add_decl loc (Dclone ($3, $5));
247
      Typing.close_scope loc ~import:$2 }
248

249
clone_subst:
Andrei Paskevich's avatar
Andrei Paskevich committed
250 251 252 253
| (* epsilon *)                         { [] }
| WITH comma_list1(single_clone_subst)  { $2 }

single_clone_subst:
Andrei Paskevich's avatar
Andrei Paskevich committed
254
| TYPE qualid ty_var* EQUAL ty  { CStsym  ($2,$3,$5) }
255
| TYPE qualid                   { CStsym  ($2, [], PTtyapp ($2, [])) }
Andrei Paskevich's avatar
Andrei Paskevich committed
256
| CONSTANT  qualid EQUAL qualid { CSfsym  ($2,$4) }
257
| CONSTANT  qualid              { CSfsym  ($2,$2) }
Andrei Paskevich's avatar
Andrei Paskevich committed
258
| FUNCTION  qualid EQUAL qualid { CSfsym  ($2,$4) }
259
| FUNCTION  qualid              { CSfsym  ($2,$2) }
Andrei Paskevich's avatar
Andrei Paskevich committed
260
| PREDICATE qualid EQUAL qualid { CSpsym  ($2,$4) }
261
| PREDICATE qualid              { CSpsym  ($2,$2) }
Andrei Paskevich's avatar
Andrei Paskevich committed
262
| VAL       qualid EQUAL qualid { CSvsym  ($2,$4) }
263
| VAL       qualid              { CSvsym  ($2,$2) }
264 265
| EXCEPTION qualid EQUAL qualid { CSxsym  ($2,$4) }
| EXCEPTION qualid              { CSxsym  ($2,$2) }
266
| AXIOM     qualid              { CSaxiom ($2) }
Andrei Paskevich's avatar
Andrei Paskevich committed
267 268
| LEMMA     qualid              { CSlemma ($2) }
| GOAL      qualid              { CSgoal  ($2) }
269

Andrei Paskevich's avatar
Andrei Paskevich committed
270
(* Meta declarations *)
271

Andrei Paskevich's avatar
Andrei Paskevich committed
272 273
meta_decl:
| META sident comma_list1(meta_arg)  { Dmeta ($2, $3) }
274 275

meta_arg:
276 277 278 279
| TYPE      ty      { Mty $2 }
| CONSTANT  qualid  { Mfs $2 }
| FUNCTION  qualid  { Mfs $2 }
| PREDICATE qualid  { Mps $2 }
280 281 282
| AXIOM     qualid  { Max $2 }
| LEMMA     qualid  { Mlm $2 }
| GOAL      qualid  { Mgl $2 }
283
| STRING            { Mstr $1 }
284
| INTEGER           { Mint (Number.to_small_integer $1) }
285

Andrei Paskevich's avatar
Andrei Paskevich committed
286 287 288 289 290 291 292 293 294
(* Theory declarations *)

pure_decl:
| TYPE with_list1(type_decl)                { Dtype $2 }
| CONSTANT  constant_decl                   { Dlogic [$2] }
| FUNCTION  function_decl  with_logic_decl* { Dlogic ($2::$3) }
| PREDICATE predicate_decl with_logic_decl* { Dlogic ($2::$3) }
| INDUCTIVE   with_list1(inductive_decl)    { Dind (Decl.Ind, $2) }
| COINDUCTIVE with_list1(inductive_decl)    { Dind (Decl.Coind, $2) }
Clément Fumex's avatar
Clément Fumex committed
295 296 297
| AXIOM labels(ident_nq) COLON term         { Dprop (Decl.Paxiom, $2, $4) }
| LEMMA labels(ident_nq) COLON term         { Dprop (Decl.Plemma, $2, $4) }
| GOAL  labels(ident_nq) COLON term         { Dprop (Decl.Pgoal, $2, $4) }
Andrei Paskevich's avatar
Andrei Paskevich committed
298

299
(* Type declarations *)
300 301

type_decl:
302
| labels(lident_nq) ty_var* typedefn invariant* type_witness
303
  { let (vis, mut), def = $3 in
304
    { td_ident = $1; td_params = $2;
305
      td_vis = vis; td_mut = mut;
306
      td_inv = $4; td_wit = $5; td_def = def;
307
      td_loc = floc $startpos $endpos } }
308

309 310 311 312
type_witness:
| (* epsilon *)                           { [] }
| BY LEFTBRC field_list1(expr) RIGHTBRC   { $3 }

313
ty_var:
314
| labels(quote_lident) { $1 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
315 316

typedefn:
317
| (* epsilon *)
318
    { (Abstract, false), TDrecord [] }
319 320
| EQUAL vis_mut bar_list1(type_case)
    { $2, TDalgebraic $3 }
321
| EQUAL vis_mut LEFTBRC loption(semicolon_list1(type_field)) RIGHTBRC
322 323 324
    { $2, TDrecord $4 }
| EQUAL vis_mut ty
    { $2, TDalias $3 }
Clément Fumex's avatar
Clément Fumex committed
325
(* FIXME: allow negative bounds *)
326
| EQUAL LT RANGE int_constant int_constant GT
327
    { (Public, false), TDrange ($4, $5) }
Clément Fumex's avatar
Clément Fumex committed
328
| EQUAL LT FLOAT INTEGER INTEGER GT
329
    { (Public, false),
330 331 332
      TDfloat (Number.to_small_integer $4, Number.to_small_integer $5) }

int_constant:
333 334
| INTEGER       { Number.compute_int_literal $1 }
| MINUS INTEGER { BigInt.minus (Number.compute_int_literal $2) }
335 336 337 338 339 340 341

vis_mut:
| (* epsilon *)     { Public, false }
| MUTABLE           { Public, true  }
| abstract          { $1, false }
| abstract MUTABLE  { $1, true }
| MUTABLE abstract  { $2, true }
342 343

abstract:
344 345
| PRIVATE           { Private }
| ABSTRACT          { Abstract }
346

347
type_field:
348
| labels(lident_nq) cast
Andrei Paskevich's avatar
Andrei Paskevich committed
349 350
  { { f_ident = $1; f_mutable = false; f_ghost = false;
      f_pty = $2; f_loc = floc $startpos $endpos } }
Clément Fumex's avatar
Clément Fumex committed
351
| field_modifiers labels(lident_nq) cast
352 353
  { { f_ident = $2; f_mutable = fst $1; f_ghost = snd $1;
      f_pty = $3; f_loc = floc $startpos $endpos } }
354

355 356 357 358 359 360
field_modifiers:
| MUTABLE       { true,  false }
| GHOST         { false, true  }
| GHOST MUTABLE { true,  true  }
| MUTABLE GHOST { true,  true  }

361
type_case:
Clément Fumex's avatar
Clément Fumex committed
362
| labels(uident_nq) params { floc $startpos $endpos, $1, $2 }
363

364
(* Logic declarations *)
365

366 367
constant_decl:
| labels(lident_rich) cast preceded(EQUAL,term)?
368 369
  { { ld_ident = add_model_trace_label $1;
      ld_params = []; ld_type = Some $2;
370
      ld_def = $3; ld_loc = floc $startpos $endpos } }
371

372 373
function_decl:
| labels(lident_rich) params cast preceded(EQUAL,term)?
374 375
  { { ld_ident = $1; ld_params = $2; ld_type = Some $3;
      ld_def = $4; ld_loc = floc $startpos $endpos } }
Andrei Paskevich's avatar
Andrei Paskevich committed
376

377 378
predicate_decl:
| labels(lident_rich) params preceded(EQUAL,term)?
379 380
  { { ld_ident = $1; ld_params = $2; ld_type = None;
      ld_def = $3; ld_loc = floc $startpos $endpos } }
381

382
with_logic_decl:
383
| WITH labels(lident_rich) params cast? preceded(EQUAL,term)?
384 385
  { { ld_ident = $2; ld_params = $3; ld_type = $4;
      ld_def = $5; ld_loc = floc $startpos $endpos } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
386

387
(* Inductive declarations *)
388 389

inductive_decl:
390
| labels(lident_rich) params ind_defn
391 392
  { { in_ident = $1; in_params = $2;
      in_def = $3; in_loc = floc $startpos $endpos } }
393

394 395 396
ind_defn:
| (* epsilon *)             { [] }
| EQUAL bar_list1(ind_case) { $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
397

398
ind_case:
Clément Fumex's avatar
Clément Fumex committed
399
| labels(ident_nq) COLON term  { floc $startpos $endpos, $1, $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
400

401
(* Type expressions *)
402

403 404 405 406
ty:
| ty_arg          { $1 }
| lqualid ty_arg+ { PTtyapp ($1, $2) }
| ty ARROW ty     { PTarrow ($1, $3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
407

408 409
ty_arg:
| lqualid                           { PTtyapp ($1, []) }
410
| quote_lident                      { PTtyvar $1 }
411 412 413
| LEFTPAR comma_list2(ty) RIGHTPAR  { PTtuple $2 }
| LEFTPAR RIGHTPAR                  { PTtuple [] }
| LEFTPAR ty RIGHTPAR               { PTparen $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
414
| LEFTBRC ty RIGHTBRC               { PTpure $2 }
415

416 417
cast:
| COLON ty  { $2 }
418

419
(* Parameters and binders *)
420

421 422
(* [param] and [binder] below must have the same grammar
   and raise [Error] in the same cases. Interpretaion of
423 424
   single-standing untyped [Qident]'s is different: [param]
   treats them as type expressions, [binder], as parameter
425 426
   names, whose type must be inferred. *)

427
params:  param*  { List.concat $1 }
428

429
binders: binder+ { List.concat $1 }
430 431 432

param:
| anon_binder
433 434 435 436 437 438 439 440
    { error_param (floc $startpos $endpos) }
| ty_arg
    { [floc $startpos $endpos, None, false, $1] }
| LEFTPAR GHOST ty RIGHTPAR
    { [floc $startpos $endpos, None, true, $3] }
| ty_arg label label*
    { match $1 with
      | PTtyapp (Qident _, []) ->
441 442
             error_param (floc $startpos $endpos)
      | _ -> error_loc (floc $startpos($2) $endpos($2)) }
443
| LEFTPAR binder_vars_rest RIGHTPAR
444
    { match $2 with [l,_] -> error_param l
445
      | _ -> error_loc (floc $startpos($3) $endpos($3)) }
446
| LEFTPAR GHOST binder_vars_rest RIGHTPAR
447
    { match $3 with [l,_] -> error_param l
448 449
      | _ -> error_loc (floc $startpos($4) $endpos($4)) }
| LEFTPAR binder_vars cast RIGHTPAR
450
    { List.map (fun (l,i) -> l, i, false, $3) $2 }
451
| LEFTPAR GHOST binder_vars cast RIGHTPAR
452
    { List.map (fun (l,i) -> l, i, true, $4) $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
453

454 455
binder:
| anon_binder
456
    { let l,i = $1 in [l, i, false, None] }
457 458 459 460
| ty_arg
    { match $1 with
      | PTtyapp (Qident id, [])
      | PTparen (PTtyapp (Qident id, [])) ->
461
          [floc $startpos $endpos, Some id, false, None]
462
      | _ -> [floc $startpos $endpos, None, false, Some $1] }
463 464 465
| LEFTPAR GHOST ty RIGHTPAR
    { match $3 with
      | PTtyapp (Qident id, []) ->
466 467
             [floc $startpos $endpos, Some id, true, None]
      | _ -> [floc $startpos $endpos, None, true, Some $3] }
468 469 470
| ty_arg label label*
    { match $1 with
      | PTtyapp (Qident id, []) ->
471 472 473
             let id = add_lab id ($2::$3) in
             [floc $startpos $endpos, Some id, false, None]
      | _ -> error_loc (floc $startpos($2) $endpos($2)) }
474
| LEFTPAR binder_vars_rest RIGHTPAR
475
    { match $2 with [l,i] -> [l, i, false, None]
476
      | _ -> error_loc (floc $startpos($3) $endpos($3)) }
477
| LEFTPAR GHOST binder_vars_rest RIGHTPAR
478
    { match $3 with [l,i] -> [l, i, true, None]
479 480
      | _ -> error_loc (floc $startpos($4) $endpos($4)) }
| LEFTPAR binder_vars cast RIGHTPAR
481
    { List.map (fun (l,i) -> l, i, false, Some $3) $2 }
482
| LEFTPAR GHOST binder_vars cast RIGHTPAR
483
    { List.map (fun (l,i) -> l, i, true, Some $4) $3 }
484

485 486 487
binder_vars:
| binder_vars_head  { List.rev $1 }
| binder_vars_rest  { $1 }
488

489
binder_vars_rest:
490 491 492 493 494 495 496
| binder_vars_head label label* binder_var*
    { List.rev_append (match $1 with
        | (l, Some id) :: bl ->
            let l3 = floc $startpos($3) $endpos($3) in
            (Loc.join l l3, Some (add_lab id ($2::$3))) :: bl
        | _ -> assert false) $4 }
| binder_vars_head anon_binder binder_var*
497
    { List.rev_append $1 ($2 :: $3) }
498
| anon_binder binder_var*
499
    { $1 :: $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
500

501
binder_vars_head:
502
| ty {
503 504
    let of_id id = id.id_loc, Some id in
    let push acc = function
505
      | PTtyapp (Qident id, []) -> of_id id :: acc
506
      | _ -> Loc.error ~loc:(floc $startpos $endpos) Error in
507
    match $1 with
508
      | PTtyapp (Qident id, l) -> List.fold_left push [of_id id] l
509
      | _ -> Loc.error ~loc:(floc $startpos $endpos) Error }
510

511
binder_var:
Clément Fumex's avatar
Clément Fumex committed
512 513
| labels(lident_nq) { floc $startpos $endpos, Some $1 }
| anon_binder       { $1 }
514 515

anon_binder:
Clément Fumex's avatar
Clément Fumex committed
516
| UNDERSCORE        { floc $startpos $endpos, None }
517

518 519 520 521
(* Logical terms *)

mk_term(X): d = X { mk_term d $startpos $endpos }

522 523 524 525
term:
| single_term %prec below_COMMA   { $1 }
| single_term COMMA term_
    { mk_term (Ttuple ($1::$3)) $startpos $endpos }
526 527

term_:
528 529 530 531 532 533
| single_term %prec below_COMMA   { [$1] }
| single_term COMMA term_         { $1::$3 }

single_term: t = mk_term(single_term_) { t }

single_term_:
534 535
| term_arg_
    { match $1 with (* break the infix relation chain *)
536 537 538
      | Tinfix (l,o,r) -> Tinnfix (l,o,r)
      | Tbinop (l,o,r) -> Tbinnop (l,o,r)
      | d -> d }
539
| NOT single_term
540
    { Tnot $2 }
541
| OLD single_term
542
    { Tat ($2, mk_id Dexpr.old_mark $startpos($1) $endpos($1)) }
543
| single_term AT uident
544
    { Tat ($1, $3) }
545
| prefix_op single_term %prec prec_prefix_op
546
    { Tidapp (Qident $1, [$2]) }
547
| MINUS INTEGER
548
    { Tconst (Number.ConstInt (mk_int_const true $2)) }
549
| MINUS REAL
550
    { Tconst (Number.ConstReal (mk_real_const true $2)) }
551
| l = single_term ; o = bin_op ; r = single_term
552
    { Tbinop (l, o, r) }
553
| l = single_term ; o = infix_op_1 ; r = single_term
554
    { Tinfix (l, o, r) }
555
| l = single_term ; o = infix_op_234 ; r = single_term
556
    { Tidapp (Qident o, [l; r]) }
557 558 559 560 561 562
| term_arg located(term_arg)+ (* FIXME/TODO: "term term_arg" *)
    { let join f (a,_,e) = mk_term (Tapply (f,a)) $startpos e in
      (List.fold_left join $1 $2).term_desc }
| IF term THEN term ELSE term
    { Tif ($2, $4, $6) }
| LET pattern EQUAL term IN term
563 564 565 566 567 568 569 570
    { let cast ty = { $4 with term_desc = Tcast ($4, ty) } in
      let pat, def = match $2.pat_desc with
        | Ptuple [] -> { $2 with pat_desc = Pwild }, cast (PTtuple [])
        | Pcast ({pat_desc = (Pvar (_,false)|Pwild)} as p, ty) -> p, cast ty
        | _ -> $2, $4 in
      match pat.pat_desc with
      | Pvar (id,false) -> Tlet (id, def, $6)
      | Pwild -> Tlet (id_anonymous pat.pat_loc, def, $6)
571
      | _ -> Tcase (def, [pat, $6]) }
572 573
| LET labels(lident_op_id) EQUAL term IN term
    { Tlet ($2, $4, $6) }
574
| LET labels(lident_nq) mk_term(lam_defn) IN term
575 576 577
    { Tlet ($2, $3, $5) }
| LET labels(lident_op_id) mk_term(lam_defn) IN term
    { Tlet ($2, $3, $5) }
578
| MATCH term WITH match_cases(term) END
579
    { Tcase ($2, $4) }
580
| quant comma_list1(quant_vars) triggers DOT term
581 582
    { let l = List.map add_model_labels (List.concat $2) in
      Tquant ($1, l, $3, $5) }
583
| FUN binders ARROW term
584
    { Tquant (Dterm.DTlambda, $2, [], $4) }
585 586
| EPSILON
    { Loc.errorm "Epsilon terms are currently not supported in WhyML" }
587
| label single_term %prec prec_named
588
    { Tnamed ($1, $2) }
589
| single_term cast
590 591
    { Tcast ($1, $2) }

592
lam_defn:
593
| binders EQUAL term  { Tquant (Dterm.DTlambda, $1, [], $3) }
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
term_arg: mk_term(term_arg_) { $1 }
term_dot: mk_term(term_dot_) { $1 }

term_arg_:
| qualid                    { Tident $1 }
| numeral                   { Tconst $1 }
| TRUE                      { Ttrue }
| FALSE                     { Tfalse }
| o = oppref ; a = term_arg { Tidapp (Qident o, [a]) }
| term_sub_                 { $1 }

term_dot_:
| lqualid                   { Tident $1 }
| o = oppref ; a = term_dot { Tidapp (Qident o, [a]) }
| term_sub_                 { $1 }

Andrei Paskevich's avatar
M.()  
Andrei Paskevich committed
611
term_block:
612
| BEGIN term END                                    { $2.term_desc }
613
| LEFTPAR term RIGHTPAR                             { $2.term_desc }
614
| BEGIN END                                         { Ttuple [] }
615 616 617
| LEFTPAR RIGHTPAR                                  { Ttuple [] }
| LEFTBRC field_list1(term) RIGHTBRC                { Trecord $2 }
| LEFTBRC term_arg WITH field_list1(term) RIGHTBRC  { Tupdate ($2,$4) }
Andrei Paskevich's avatar
M.()  
Andrei Paskevich committed
618 619 620 621 622

term_sub_:
| term_block                                        { $1 }
| uqualid DOT mk_term(term_block)                   { Tscope ($1, $3) }
| term_dot DOT lqualid_rich                         { Tidapp ($3,[$1]) }
623 624 625 626
| term_arg LEFTSQ term RIGHTSQ
    { Tidapp (get_op $startpos($2) $endpos($2), [$1;$3]) }
| term_arg LEFTSQ term LARROW term RIGHTSQ
    { Tidapp (set_op $startpos($2) $endpos($2), [$1;$3;$5]) }
627 628 629 630 631 632
| term_arg LEFTSQ term DOTDOT term RIGHTSQ
    { Tidapp (sub_op $startpos($2) $endpos($2), [$1;$3;$5]) }
| term_arg LEFTSQ term DOTDOT RIGHTSQ
    { Tidapp (above_op $startpos($2) $endpos($2), [$1;$3]) }
| term_arg LEFTSQ DOTDOT term RIGHTSQ
    { Tidapp (below_op $startpos($2) $endpos($2), [$1;$4]) }
633

634 635
field_list1(X):
| fl = semicolon_list1(separated_pair(lqualid, EQUAL, X)) { fl }
636

637
match_cases(X):
638 639 640 641
| cl = bar_list1(match_case(X)) { cl }

match_case(X):
| mc = separated_pair(pattern, ARROW, X) { mc }
642

643 644
quant_vars:
| binder_var+ cast? { List.map (fun (l,i) -> l, i, false, $2) $1 }
645

646
triggers:
647 648
| (* epsilon *)                                                         { [] }
| LEFTSQ separated_nonempty_list(BAR,comma_list1(single_term)) RIGHTSQ  { $2 }
649

650
%inline bin_op:
651 652 653 654 655 656
| ARROW   { Dterm.DTimplies }
| LRARROW { Dterm.DTiff }
| OR      { Dterm.DTor }
| BARBAR  { Dterm.DTor_asym }
| AND     { Dterm.DTand }
| AMPAMP  { Dterm.DTand_asym }
657 658
| BY      { Dterm.DTby }
| SO      { Dterm.DTso }
659

660
quant:
661 662
| FORALL  { Dterm.DTforall }
| EXISTS  { Dterm.DTexists }
663

664
numeral:
665 666
| INTEGER { Number.ConstInt (mk_int_const false $1) }
| REAL    { Number.ConstReal (mk_real_const false $1) }
667

668
(* Program declarations *)
669

Andrei Paskevich's avatar
Andrei Paskevich committed
670
prog_decl:
Guillaume Melquiond's avatar
Guillaume Melquiond committed
671
| VAL ghost kind labels(lident_rich) mk_expr(val_defn) { Dlet (add_model_trace_label $4, $2, $3, $5) }
672
| LET ghost kind labels(lident_rich) mk_expr(fun_defn) { Dlet ($4, $2, $3, $5) }
673
| LET ghost kind labels(lident_rich) const_defn        { Dlet ($4, $2, $3, $5) }
674
| LET REC with_list1(rec_defn)                         { Drec $3 }
675 676
| EXCEPTION labels(uident_nq)         { Dexn ($2, PTtuple [], Ity.MaskVisible) }
| EXCEPTION labels(uident_nq) return  { Dexn ($2, fst $3, snd $3) }
677 678 679 680 681 682 683 684 685 686 687

ghost:
| (* epsilon *) { false }
| GHOST         { true }

kind:
| (* epsilon *) { Expr.RKnone }
| FUNCTION      { Expr.RKfunc }
| CONSTANT      { Expr.RKfunc }
| PREDICATE     { Expr.RKpred }
| LEMMA         { Expr.RKlemma }
688 689

(* Function definitions *)
690

691
rec_defn:
692
| ghost kind labels(lident_rich) binders ret_opt spec EQUAL spec seq_expr
Andrei Paskevich's avatar
Andrei Paskevich committed
693 694 695
    { let id = mk_id return_id $startpos($7) $endpos($7) in
      let e = { $9 with expr_desc = Eoptexn (id, snd $5, $9) } in
      $3, $1, $2, $4, fst $5, snd $5, spec_union $6 $8, e }
696

697
fun_defn:
698
| binders ret_opt spec EQUAL spec seq_expr
Andrei Paskevich's avatar
Andrei Paskevich committed
699 700
    { let id = mk_id return_id $startpos($4) $endpos($4) in
      let e = { $6 with expr_desc = Eoptexn (id, snd $2, $6) } in
Guillaume Melquiond's avatar
Guillaume Melquiond committed
701
      Efun (List.map add_model_labels $1, fst $2, snd $2, spec_union $3 $5, e) }
702

703
val_defn:
704 705
| params ret_opt spec
    { Eany ($1, Expr.RKnone, fst $2, snd $2, $3) }
706

707 708 709 710
const_defn:
| cast EQUAL seq_expr   { { $3 with expr_desc = Ecast ($3, $1) } }
| EQUAL seq_expr        { $2 }

711 712 713 714 715
(* Program expressions *)

mk_expr(X): d = X { mk_expr d $startpos $endpos }

seq_expr:
Andrei Paskevich's avatar
Andrei Paskevich committed
716 717 718
| contract_expr %prec below_SEMI  { $1 }
| contract_expr SEMICOLON         { $1 }
| contract_expr SEMICOLON seq_expr
719
    { mk_expr (Esequence ($1, $3)) $startpos $endpos }
720

Andrei Paskevich's avatar
Andrei Paskevich committed
721 722 723 724 725 726 727
contract_expr:
| assign_expr %prec prec_no_spec  { $1 }
| assign_expr single_spec spec
    { let d = Efun ([], None, Ity.MaskVisible, spec_union $2 $3, $1) in
      let d = Enamed (Lstr Vc.wb_label, mk_expr d $startpos $endpos) in
      mk_expr d $startpos $endpos }

728 729 730 731 732 733 734
assign_expr:
| expr %prec below_LARROW         { $1 }
| expr LARROW expr
    { let loc = floc $startpos $endpos in
      let rec down ll rl = match ll, rl with
        | {expr_desc = Eidapp (q, [e1])}::ll, e2::rl -> (e1,q,e2) :: down ll rl
        | {expr_desc = Eidapp (Qident id, [_;_]); expr_loc = loc}::_, _::_
Guillaume Melquiond's avatar
Guillaume Melquiond committed
735
          when id.id_str = Ident.mixfix "[]" -> Loc.errorm ~loc
736 737 738 739 740 741
            "Parallel array assignments are not allowed"
        | {expr_loc = loc}::_, _::_ -> Loc.errorm ~loc
            "Invalid left expression in an assignment"
        | [], [] -> []
        | _ -> Loc.errorm ~loc "Invalid parallel assignment" in
      let d = match $1.expr_desc, $3.expr_desc with
Guillaume Melquiond's avatar
Guillaume Melquiond committed
742 743
        | Eidapp (Qident id, [e1;e2]), _ when id.id_str = Ident.mixfix "[]" ->
            Eidapp (Qident {id with id_str = Ident.mixfix "[]<-"}, [e1;e2;$3])
744 745 746 747 748 749 750
        | Etuple ll, Etuple rl -> Eassign (down ll rl)
        | Etuple _, _ -> Loc.errorm ~loc "Invalid parallel assignment"
        | _, _ -> Eassign (down [$1] [$3]) in
      { expr_desc = d; expr_loc = loc } }

expr:
| single_expr %prec below_COMMA   { $1 }
Andrei Paskevich's avatar
Andrei Paskevich committed
751
| single_expr COMMA expr_list1
752
    { mk_expr (Etuple ($1::$3)) $startpos $endpos }
753

Andrei Paskevich's avatar
Andrei Paskevich committed
754
expr_list1:
755
| single_expr %prec below_COMMA   { [$1] }
Andrei Paskevich's avatar
Andrei Paskevich committed
756
| single_expr COMMA expr_list1    { $1::$3 }
757 758 759 760

single_expr: e = mk_expr(single_expr_)  { e }

single_expr_:
761
| expr_arg_
762 763
    { match $1 with (* break the infix relation chain *)
      | Einfix (l,o,r) -> Einnfix (l,o,r) | d -> d }
764
| single_expr AMPAMP single_expr
765
    { Eand ($1, $3) }
766
| single_expr BARBAR single_expr
767
    { Eor ($1, $3) }
768
| NOT single_expr
769
    { Enot $2 }
770
| prefix_op single_expr %prec prec_prefix_op
771
    { Eidapp (Qident $1, [$2]) }
772
| MINUS INTEGER
773
    { Econst (Number.ConstInt (mk_int_const true $2)) }
774
| MINUS REAL
775
    { Econst (Number.ConstReal (mk_real_const true $2)) }
776
| l = single_expr ; o = infix_op_1 ; r = single_expr
777
    { Einfix (l,o,r) }
778
| l = single_expr ; o = infix_op_234 ; r = single_expr
779
    { Eidapp (Qident o, [l;r]) }
780 781 782
| expr_arg located(expr_arg)+ (* FIXME/TODO: "expr expr_arg" *)
    { let join f (a,_,e) = mk_expr (Eapply (f,a)) $startpos e in
      (List.fold_left join $1 $2).expr_desc }
Andrei Paskevich's avatar
Andrei Paskevich committed
783
| IF seq_expr THEN contract_expr ELSE contract_expr
784
    { Eif ($2, $4, $6) }
Andrei Paskevich's avatar
Andrei Paskevich committed
785
| IF seq_expr THEN contract_expr %prec prec_no_else
786
    { Eif ($2, $4, mk_expr (Etuple []) $startpos $endpos) }
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
| LET ghost kind let_pattern EQUAL seq_expr IN seq_expr
    { let re_pat pat d = { pat with pat_desc = d } in
      let rec ghostify pat = match pat.pat_desc with
        (* let_pattern marks the opening variable with Ptuple [_] *)
        | Ptuple [{pat_desc = Pvar (id,_)}] -> re_pat pat (Pvar (id,$2))
        | Ptuple (p::pl) -> re_pat pat (Ptuple (ghostify p :: pl))
        | Pas (p,id,gh) -> re_pat pat (Pas (ghostify p, id, gh))
        | Por (p1,p2) -> re_pat pat (Por (ghostify p1, p2))
        | Pcast (p,t) -> re_pat pat (Pcast (ghostify p, t))
        | _ when $2 -> Loc.errorm ~loc:(floc $startpos($2) $endpos($2))
            "illegal ghost qualifier" (* $4 does not start with a Pvar *)
        | _ -> pat in
      let pat = ghostify $4 in
      let kind = match pat.pat_desc with
        | _ when $3 = Expr.RKnone -> $3
        | Pvar (_,_) | Pcast ({pat_desc = Pvar (_,_)},_) -> $3
        | _ -> Loc.errorm ~loc:(floc $startpos($3) $endpos($3))
            "illegal kind qualifier" in
      let cast ty = { $6 with expr_desc = Ecast ($6, ty) } in
      let pat, def = match pat.pat_desc with
        | Ptuple [] -> re_pat pat Pwild, cast (PTtuple [])
        | Pcast ({pat_desc = (Pvar _|Pwild)} as pat, ty) -> pat, cast ty
        | _ -> pat, $6 in
      match pat.pat_desc with
Guillaume Melquiond's avatar
Guillaume Melquiond committed
811
      | Pvar (id, gh) ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
812
        let id = add_model_trace_label id in
Guillaume Melquiond's avatar
Guillaume Melquiond committed
813
        Elet (id, gh, kind, def, $8)
814
      | Pwild -> Elet (id_anonymous pat.pat_loc, false, kind, def, $8)