parser.mly 42.2 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
MARCHE Claude's avatar
MARCHE Claude committed
4
(*  Copyright 2010-2017   --   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

15
  let infix  s = "infix "  ^ s
16
  let prefix s = "prefix " ^ s
17
  let mixfix s = "mixfix " ^ s
18

Andrei Paskevich's avatar
Andrei Paskevich committed
19 20 21
  let qualid_last = function Qident x | Qdot (_, x) -> x

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

23
  let floc s e = Loc.extract (s,e)
24

25
  let model_label = Ident.create_label "model"
26
  let model_projected = Ident.create_label "model_projected"
27

28 29 30
  let is_model_label l = match l with
    | Lstr lab -> Ident.lab_equal lab model_label ||
                  Ident.lab_equal lab model_projected
31
    | Lpos _ -> false
32

33
  let model_lab_present labels = List.exists is_model_label labels
34

35 36
  let is_model_trace_label l = match l with
    | Lstr lab -> Strings.has_prefix "model_trace:" lab.Ident.lab_string
37
    | Lpos _ -> false
38 39

  let model_trace_lab_present labels = List.exists is_model_trace_label labels
40 41

  let add_model_trace name labels =
42
    if model_lab_present labels && not (model_trace_lab_present labels) then
43 44
      (Lstr (Ident.create_label ("model_trace:" ^ name)))::labels
    else
45
      labels
46

47
  let add_lab id l = { id with id_lab = add_model_trace id.id_str l }
48

49
  let id_anonymous loc = { id_str = "_"; id_lab = []; id_loc = loc }
50

51 52 53 54 55 56
  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}

57
  let mk_id id s e = { id_str = id; id_lab = []; id_loc = floc s e }
58

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

65 66
  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 }
67
  let mk_expr d s e = { expr_desc = d; expr_loc = floc s e }
68

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

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

86 87 88 89
  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;
90
    sp_reads   = s1.sp_reads @ s2.sp_reads;
91 92
    sp_writes  = s1.sp_writes @ s2.sp_writes;
    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 107 108
  let error_loc loc = Loc.error ~loc Error

  let () = Exn_printer.register (fun fmt exn -> match exn with
    | Error -> Format.fprintf fmt "syntax error"
    | _ -> 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

Clément Fumex's avatar
Clément Fumex committed
113
%token <string> LIDENT LIDENT_QUOTE UIDENT UIDENT_QUOTE
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 <Loc.position> POSITION
119
%token <string> QUOTE_LIDENT
120

121
(* keywords *)
122

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

129
(* program keywords *)
130

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

137
(* symbols *)
138

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

%token EOF

150
(* program symbols *)
151

152
%token AMPAMP BARBAR LEFTBRC RIGHTBRC SEMICOLON
153

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

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

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

186
%start <Pmodule.pmodule Stdlib.Mstr.t> mlw_file
187
%start <Ptree.term> term_eof
188 189
%start <Ptree.qualid> qualid_eof
%start <Ptree.qualid list> qualid_comma_list_eof
190
%start <Ptree.term list> term_comma_list_eof
191
%start <Ptree.ident list> ident_comma_list_eof
192

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

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

term_eof:
| term EOF { $1 }

200
(* Modules and scopes *)
201

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

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

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

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

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

230
(* Use and clone *)
231

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

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

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

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

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

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

Andrei Paskevich's avatar
Andrei Paskevich committed
285 286 287 288 289 290 291 292 293
(* 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
294 295 296
| 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
297

298
(* Type declarations *)
299 300

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

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

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

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

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

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

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

346
type_field:
347
| labels(lident_nq) cast
Andrei Paskevich's avatar
Andrei Paskevich committed
348 349
  { { 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
350
| field_modifiers labels(lident_nq) cast
351 352
  { { f_ident = $2; f_mutable = fst $1; f_ghost = snd $1;
      f_pty = $3; f_loc = floc $startpos $endpos } }
353

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

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

363
(* Logic declarations *)
364

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

370 371
function_decl:
| labels(lident_rich) params cast preceded(EQUAL,term)?
372 373
  { { 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
374

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

380
with_logic_decl:
381
| WITH labels(lident_rich) params cast? preceded(EQUAL,term)?
382 383
  { { 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
384

385
(* Inductive declarations *)
386 387

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

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

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

399
(* Type expressions *)
400

401 402 403 404
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
405

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

414 415
cast:
| COLON ty  { $2 }
416

417
(* Parameters and binders *)
418

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

425
params:  param*  { List.concat $1 }
426

427
binders: binder+ { List.concat $1 }
428 429 430

param:
| anon_binder
431 432 433 434 435 436 437 438
    { 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 _, []) ->
439 440
             error_param (floc $startpos $endpos)
      | _ -> error_loc (floc $startpos($2) $endpos($2)) }
441
| LEFTPAR binder_vars_rest RIGHTPAR
442
    { match $2 with [l,_] -> error_param l
443
      | _ -> error_loc (floc $startpos($3) $endpos($3)) }
444
| LEFTPAR GHOST binder_vars_rest RIGHTPAR
445
    { match $3 with [l,_] -> error_param l
446 447
      | _ -> error_loc (floc $startpos($4) $endpos($4)) }
| LEFTPAR binder_vars cast RIGHTPAR
448
    { List.map (fun (l,i) -> l, i, false, $3) $2 }
449
| LEFTPAR GHOST binder_vars cast RIGHTPAR
450
    { List.map (fun (l,i) -> l, i, true, $4) $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
451

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

483 484 485
binder_vars:
| binder_vars_head  { List.rev $1 }
| binder_vars_rest  { $1 }
486

487
binder_vars_rest:
488 489 490 491 492 493 494
| 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*
495
    { List.rev_append $1 ($2 :: $3) }
496
| anon_binder binder_var*
497
    { $1 :: $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
498

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

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

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

516 517 518 519
(* Logical terms *)

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

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

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

single_term: t = mk_term(single_term_) { t }

single_term_:
532 533
| term_arg_
    { match $1 with (* break the infix relation chain *)
534 535 536
      | Tinfix (l,o,r) -> Tinnfix (l,o,r)
      | Tbinop (l,o,r) -> Tbinnop (l,o,r)
      | d -> d }
537
| NOT single_term
538
    { Tnot $2 }
539
| OLD single_term
540
    { Tat ($2, mk_id Dexpr.old_mark $startpos($1) $endpos($1)) }
541
| single_term AT uident
542
    { Tat ($1, $3) }
543
| prefix_op single_term %prec prec_prefix_op
544
    { Tidapp (Qident $1, [$2]) }
545
| MINUS INTEGER
546
    { Tconst (Number.ConstInt (mk_int_const true $2)) }
547
| MINUS REAL
548
    { Tconst (Number.ConstReal (mk_real_const true $2)) }
549
| l = single_term ; o = bin_op ; r = single_term
550
    { Tbinop (l, o, r) }
551
| l = single_term ; o = infix_op_1 ; r = single_term
552
    { Tinfix (l, o, r) }
553
| l = single_term ; o = infix_op_234 ; r = single_term
554
    { Tidapp (Qident o, [l; r]) }
555 556 557 558 559 560
| 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
561 562 563 564 565 566 567 568 569
    { 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)
      | _ -> Tmatch (def, [pat, $6]) }
570 571
| LET labels(lident_op_id) EQUAL term IN term
    { Tlet ($2, $4, $6) }
572
| LET labels(lident_nq) mk_term(lam_defn) IN term
573 574 575
    { Tlet ($2, $3, $5) }
| LET labels(lident_op_id) mk_term(lam_defn) IN term
    { Tlet ($2, $3, $5) }
576 577 578 579
| MATCH term WITH match_cases(term) END
    { Tmatch ($2, $4) }
| quant comma_list1(quant_vars) triggers DOT term
    { Tquant ($1, List.concat $2, $3, $5) }
580
| FUN binders ARROW term
581
    { Tquant (Dterm.DTlambda, $2, [], $4) }
582 583
| EPSILON
    { Loc.errorm "Epsilon terms are currently not supported in WhyML" }
584
| label single_term %prec prec_named
585
    { Tnamed ($1, $2) }
586
| single_term cast
587 588
    { Tcast ($1, $2) }

589
lam_defn:
590
| binders EQUAL term  { Tquant (Dterm.DTlambda, $1, [], $3) }
591

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
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
608
term_block:
609
| BEGIN term END                                    { $2.term_desc }
610
| LEFTPAR term RIGHTPAR                             { $2.term_desc }
611
| BEGIN END                                         { Ttuple [] }
612 613 614
| 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
615 616 617 618 619

term_sub_:
| term_block                                        { $1 }
| uqualid DOT mk_term(term_block)                   { Tscope ($1, $3) }
| term_dot DOT lqualid_rich                         { Tidapp ($3,[$1]) }
620 621 622 623
| 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]) }
624 625 626 627 628 629
| 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]) }
630

631 632
field_list1(X):
| fl = semicolon_list1(separated_pair(lqualid, EQUAL, X)) { fl }
633

634
match_cases(X):
635 636 637 638
| cl = bar_list1(match_case(X)) { cl }

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

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

643
triggers:
644 645
| (* epsilon *)                                                         { [] }
| LEFTSQ separated_nonempty_list(BAR,comma_list1(single_term)) RIGHTSQ  { $2 }
646

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

657
quant:
658 659
| FORALL  { Dterm.DTforall }
| EXISTS  { Dterm.DTexists }
660

661
numeral:
662 663
| INTEGER { Number.ConstInt (mk_int_const false $1) }
| REAL    { Number.ConstReal (mk_real_const false $1) }
664

665
(* Program declarations *)
666

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

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

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

(* Function definitions *)
687

688
rec_defn:
689
| ghost kind labels(lident_rich) binders ret_opt spec EQUAL spec seq_expr
Andrei Paskevich's avatar
Andrei Paskevich committed
690 691 692
    { 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 }
693

694
fun_defn:
695
| binders ret_opt spec EQUAL spec seq_expr
Andrei Paskevich's avatar
Andrei Paskevich committed
696 697 698
    { let id = mk_id return_id $startpos($4) $endpos($4) in
      let e = { $6 with expr_desc = Eoptexn (id, snd $2, $6) } in
      Efun ($1, fst $2, snd $2, spec_union $3 $5, e) }
699

700
val_defn:
701 702
| params ret_opt spec
    { Eany ($1, Expr.RKnone, fst $2, snd $2, $3) }
703

704 705 706 707
const_defn:
| cast EQUAL seq_expr   { { $3 with expr_desc = Ecast ($3, $1) } }
| EQUAL seq_expr        { $2 }

708 709 710 711 712
(* Program expressions *)

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

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

Andrei Paskevich's avatar
Andrei Paskevich committed
718 719 720 721 722 723 724
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 }

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
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}::_, _::_
          when id.id_str = mixfix "[]" -> Loc.errorm ~loc
            "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
        | Eidapp (Qident id, [e1;e2]), _ when id.id_str = mixfix "[]" ->
            Eidapp (Qident {id with id_str = mixfix "[]<-"}, [e1;e2;$3])
        | 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
748
| single_expr COMMA expr_list1
749
    { mk_expr (Etuple ($1::$3)) $startpos $endpos }
750

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

single_expr: e = mk_expr(single_expr_)  { e }

single_expr_:
758
| expr_arg_
759 760
    { match $1 with (* break the infix relation chain *)
      | Einfix (l,o,r) -> Einnfix (l,o,r) | d -> d }
761
| single_expr AMPAMP single_expr
762
    { Eand ($1, $3) }
763
| single_expr BARBAR single_expr
764
    { Eor ($1, $3) }
765
| NOT single_expr
766
    { Enot $2 }
767
| prefix_op single_expr %prec prec_prefix_op
768
    { Eidapp (Qident $1, [$2]) }
769
| MINUS INTEGER
770
    { Econst (Number.ConstInt (mk_int_const true $2)) }
771
| MINUS REAL
772
    { Econst (Number.ConstReal (mk_real_const true $2)) }
773
| l = single_expr ; o = infix_op_1 ; r = single_expr
774
    { Einfix (l,o,r) }
775
| l = single_expr ; o = infix_op_234 ; r = single_expr
776
    { Eidapp (Qident o, [l;r]) }
777 778 779
| 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
780
| IF seq_expr THEN contract_expr ELSE contract_expr
781
    { Eif ($2, $4, $6) }
Andrei Paskevich's avatar
Andrei Paskevich committed
782
| IF seq_expr THEN contract_expr %prec prec_no_else
783
    { Eif ($2, $4, mk_expr (Etuple []) $startpos $endpos) }
784 785 786 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
      | Pvar (id, gh) -> Elet (id, gh, kind, def, $8)
      | Pwild -> Elet (id_anonymous pat.pat_loc, false, kind, def, $8)
      | _ -> Ematch (def, [pat, $8]) }
811 812
| LET ghost kind labels(lident_op_id) EQUAL seq_expr IN seq_expr
    { Elet ($4, $2, $3, $6, $8) }
813
| LET ghost kind labels(lident_nq) mk_expr(fun_defn) IN seq_expr
814 815 816
    { Elet ($4, $2, $3, $5, $7) }
| LET ghost kind labels(lident_op_id) mk_expr(fun_defn) IN seq_expr
    { Elet ($4, $2, $3, $5, $7) }
817
| LET REC with_list1(rec_defn) IN seq_expr
818
    { Erec ($3, $5) }
819
| FUN binders spec ARROW spec seq_expr
Andrei Paskevich's avatar
Andrei Paskevich committed
820 821 822
    { let id = mk_id return_id $startpos($4) $endpos($4) in
      let e = { $6 with expr_desc = Eoptexn (id, Ity.MaskVisible, $6) } in
      Efun ($2, None, Ity.MaskVisible, spec_union $3 $5, e) }
823 824
| ANY return spec
    { Eany ([], Expr.RKnone, Some (fst $2), snd $2, $3) }
825 826
| VAL ghost kind labels(lident_rich