fstar.mly 23.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 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 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
%{

(*
 Known (intentional) ambiguitites: 6 s/r conflicts in total; resolved by shifting
   3 s/r conflicts on BAR
      match  with | P -> match with | Q -> _ | R -> _
      function ...
      try e with | ... 

   1 s/r conflict on SEMICOLON
       fun x -> e1 ; e2
	 is parsed as
		(fun x -> e1; e2)
	 rather than
		(fun x -> e1); e2

   1 s/r conflict on DOT
      A.B ^ .C 

   1 s/r conflict on LBRACE
	
      Consider: 
		  let f (x: y:int & z:vector y{z=z /\ y=0}) = 0
	  
	  This is parsed as:
        let f (x: (y:int & z:vector y{z=z /\ y=0})) = 0
	  rather than:
	    let f (x: (y:int & z:vector y){z=z /\ y=0}) = 0

	  Analogous ambiguities with -> and * as well.
*)











%} 

%token <bytes> BYTEARRAY
%token <bytes> STRING 
%token <string> IDENT 
%token <string> IDENT_LESS
%token <string> NAME
%token <string> TVAR
%token <string> DIV_MOD_OP 
%token <string> TILDE
%token <string> CUSTOM_OP


/* bool indicates if INT8 was 'bad' max_int+1, e.g. '128'  */
%token <sbyte * bool> INT8 
%token <int16 * bool> INT16
%token <int32 * bool> INT32 INT32_DOT_DOT
%token <int64 * bool> INT64
%token <string * bool> INT

%token <byte> UINT8
%token <uint16> UINT16
%token <uint32> UINT32
%token <uint64> UINT64
%token <uint64> UNATIVEINT
%token <int64> NATIVEINT
%token <single> IEEE32
%token <double> IEEE64
%token <char> CHAR
%token <decimal> DECIMAL 
%token <bytes> BIGINT BIGNUM
%token <bool> LET 
%token <string * bool> LQUOTE RQUOTE 
%token FORALL EXISTS ASSUME QUERY DEFINE LOGIC OPAQUE PRAGMALIGHT PRAGMA_SET_OPTIONS PRAGMA_RESET_OPTIONS
%token BAR_BAR LEQ GEQ LESS LESSLESS TYP_APP_LESS TYP_APP_GREATER LESSGREATER SUBTYPE SUBKIND BANG
%token AND AS ASSERT BEGIN ELSE END DOT_DOT
%token EXCEPTION FALSE FOR FUN FUNCTION IF IN FINALLY RESERVED MODULE DEFAULT
%token LAZY MATCH OF 
%token OPEN OR REC THEN TO TRUE TRY TYPE EFFECT VAL 
%token WHEN WITH HASH AMP AMP_AMP QUOTE LPAREN RPAREN LPAREN_RPAREN STAR COMMA RARROW
%token IFF IMPLIES CONJUNCTION DISJUNCTION 
%token DOT COLON COLON_COLON ATSIGN HAT COLON_EQUALS SEMICOLON 
%token SEMICOLON_SEMICOLON EQUALS EQUALS_EQUALS PERCENT_LBRACK LBRACK LBRACK_BAR LBRACE BACKSLASH BANG_LBRACE
%token BAR_RBRACK UNDERSCORE LENS_PAREN_LEFT LENS_PAREN_RIGHT
%token BAR RBRACK RBRACE MINUS DOLLAR  
%token PUBLIC PRIVATE LBRACE_COLON_PATTERN PIPE_LEFT PIPE_RIGHT 
%token NEW_EFFECT SUB_EFFECT SQUIGGLY_RARROW TOTAL KIND
%token PRINT REQUIRES ENSURES
%token PLUS_OP MINUS_OP BAR_FOCUS
 
/* These are artificial */
%token <string> LEX_FAILURE
%token COMMENT WHITESPACE HASH_LINE HASH_LIGHT HASH_IF HASH_ELSE HASH_ENDIF INACTIVECODE LINE_COMMENT STRING_TEXT EOF

%nonassoc THEN
%nonassoc ELSE

%start inputFragment
%type <inputFragment> inputFragment

%%
inputFragment:
  | file
	  { Inl $1 }
  | decl decls EOF
	  { Inr ($1::$2) }

file: 
  | maybe_pragma_light moduleList
      { $2 }


moduleList:
  | modul moduleList
      { $1::$2 }
  | EOF
      { [] }
      
modul:    
  | MODULE qname decls endopt
        { Module($2, $3) }

endopt:
  | END  {} 
  |      {}

maybe_pragma_light:
  |     {}
  | PRAGMALIGHT STRING
        {}

pragma: 
  | PRAGMA_SET_OPTIONS STRING
	{ 
	   let s = string_of_bytes $2 in
	   if s = "--no_fs_typ_app"
	   then fs_typ_app := false;
	   SetOptions s
	}

  | PRAGMA_RESET_OPTIONS
	{ ResetOptions }

decls:
  |   { [] }
  | SEMICOLON_SEMICOLON term 
      { [mk_decl (Main $2) (rhs2 parseState 1 2)] }
  | decl decls
      { ($1::$2) }

decl:
  | decl2 { mk_decl $1 (rhs parseState 1) }

decl2:
  | OPEN qname
      { Open $2 }
  | kind_abbrev 
      { $1 }
  | tycon 
      { $1 }
  | LET recopt letbinding letbindings
      { 
		let r, focus = $2 in
		let lbs = focusLetBindings ((focus, $3)::$4) (rhs2 parseState 1 4) in
		ToplevelLet(r, lbs)
	  }
  | qualifiers VAL ident COLON typ
      { Val($1, $3, $5) }
  | assumeTag name COLON formula
      { Assume($1, $2, $4) }
  | EXCEPTION name of_typ
      { Exception($2, $3) }
  | qualifiers NEW_EFFECT new_effect
	  { NewEffect ($1, $3) }
  | SUB_EFFECT sub_effect
	  { SubEffect $2 }
  | pragma
	  { Pragma $1 }

tycon:
  | qualifiers TYPE tyconDefinition tyconDefinitions 
      { Tycon ($1, $3::$4) }

  | qualifiers EFFECT tyconDefinition 
	  { Tycon(Effect::$1, [$3]) }

kind_abbrev: 
  | KIND name binders EQUALS kind
      { KindAbbrev($2, $3, $5) }

new_effect: 
  | name binders EQUALS simpleTerm
	  {
		 RedefineEffect($1, $2, $4)
	  }
  | LBRACE name binders COLON kind WITH effect_decl more_effect_decls RBRACE
      {  
		 DefineEffect($2, $3, $5, $7::$8)
	  }

effect_decl: 
  | ident EQUALS simpleTerm
	{
		mk_decl (Tycon ([], [TyconAbbrev($1, [], None, $3)])) (rhs2 parseState 1 3)
	}

more_effect_decls:
  |									   	    { []     }
  | SEMICOLON effect_decl more_effect_decls { $2::$3 }


sub_effect:
  | qname SQUIGGLY_RARROW qname EQUALS simpleTerm
      {
        { 
          msource=$1;
          mdest=$3;
          lift_op=$5
        }
      }

qualifier:
  | LOGIC        { Logic }
  | ASSUME       { Assumption }
  | OPAQUE       { Opaque }
  | DEFAULT      { DefaultEffect None }
  | TOTAL        { TotalEffect }
  | PRIVATE      { Private }

qualifiers:
  |                      { [] }
  | qualifier qualifiers { $1::$2 }

assumeTag:
  | ASSUME { [Assumption] }

tyconDefinition:
  | eitherName typars ascribeKindOpt tyconDefn
      { $4 $1 $2 $3 }

tyconDefinitions:
  |   { [] }
  | AND tyconDefinition tyconDefinitions
      { $2::$3 }

ident_opt:
  |       { None }
  | ident { Some $1 }

maybeFocus:
  |  { false }
  | SQUIGGLY_RARROW { true }

recopt:
  | maybeFocus REC      { true, $1 }
  |                     { false, false }

letbindings:
  | AND maybeFocus letbinding letbindings 
      { ($2, $3)::$4 }
  |   { [] }

letbinding:
  | pattern ascribeTypOpt EQUALS term 
      { 
        match $2 with 
          | None -> ($1, $4)
          | Some t -> (mk_pattern (PatAscribed($1, t)) (rhs2 parseState 1 2), $4)
      }
      
pattern:
  | tuplePattern { $1 }


tuplePattern:
  | listPattern patternListComma 
      {
        match $2 with 
          | [] -> $1
          | _ -> mk_pattern (PatTuple($1::$2, false)) (rhs2 parseState 1 2)
      }


patternListComma:
  |   { [] }
  | COMMA listPattern patternListComma { $2::$3 }

listPattern:
  | appPattern consPattern
      { 
        match $2 with 
          | None -> $1
          | Some tl -> mk_pattern (consPat (rhs parseState 2) $1 tl) (rhs2 parseState 1 2) 
      }

consPattern:
  |  { None }
  |  COLON_COLON appPattern consPattern 
      { 
        match $3 with 
          | None -> Some $2
          | Some tl -> Some (mk_pattern (consPat (rhs parseState 1) $2 tl) (rhs2 parseState 1 3))
      }

appPattern:
  | atomicPattern atomicPatterns
      { 
        let hd = $1 in 
        let r = rhs parseState 1 in 
        match $2 with 
          | [] -> hd
          | _ -> mk_pattern (PatApp(hd, $2)) (union_ranges r (rhs parseState 1))
      }

compositePattern:
  | atomicPattern atomicPattern atomicPatterns
      {
        let args = $2::$3 in 
          mk_pattern (PatApp($1, args)) (rhs2 parseState 1 3)
      }
  | tuplePattern { $1 }

atomicPatterns:
  |    { [] }
  | atomicPattern atomicPatterns { $1::$2 }

atomicPattern: 
  | atomicPattern2 { mk_pattern $1 (rhs parseState 1) }

atomicPattern2:
  | nonTvarPattern2 { $1 }
  | tvar  { PatTvar ($1, false) }

nonTvarPattern:
  | nonTvarPattern2 { mk_pattern $1 (rhs parseState 1) }

nonTvarPattern2:
  | UNDERSCORE { PatWild }
  | constant { PatConst $1 }
  | HASH ident { PatVar($2, true) }
  | ident { PatVar($1, false) }
  | qname { PatName $1 }
  | LBRACK patternListSemiColon RBRACK { PatList $2 }
  | LPAREN ascriptionOrPattern RPAREN { $2 } 
  | LBRACE recordPattern RBRACE { PatRecord $2 }
  | LENS_PAREN_LEFT listPattern COMMA listPattern patternListComma LENS_PAREN_RIGHT 
      { 
        let args = $2::$4::$5 in
        PatTuple(args, true)
      }

ascriptionOrPattern:
  |  nonTvarPattern COLON typ refineOpt
      { 
		match $4 with
		  | None -> PatAscribed($1, $3) 
			  | Some phi ->
				let t = match $1.pat with 
				  | PatVar(x, _) -> 
					mk_term (Refine(mk_binder (Annotated(x, $3)) (rhs2 parseState 1 3) Type None, phi)) (rhs2 parseState 1 4) Type 
				  | _ -> errorR(Error("Not a valid refinement type", lhs(parseState))); $3 in
				PatAscribed($1, t)
      }
  |  pattern                { $1.pat }

patternListSemiColon:
  |   { [] }
  | appPattern patternListSemiColonRest { $1::$2 }

patternListSemiColonRest:
  |  { [] }
  | SEMICOLON appPattern patternListSemiColonRest { $2::$3 }


recordPattern:
  | lid EQUALS pattern moreFieldPatterns { ($1,$3)::$4 }
      
moreFieldPatterns:
  | { [] }
  | SEMICOLON lid EQUALS pattern moreFieldPatterns { ($2,$4)::$5 }

binder:
  | ident { mk_binder (Variable($1)) (rhs parseState 1) Type None  }
  | tvar  { mk_binder (TVariable($1)) (rhs parseState 1) Kind None  }
  | LPAREN aqual_opt ident COLON typ refineOpt RPAREN
	 { mkRefinedBinder $3 $5 $6 (rhs2 parseState 1 7) $2 }

typars:
  | tvarinsts              { $1 }
  | binders                { $1 }

tvarinsts: 
  | TYP_APP_LESS tvars TYP_APP_GREATER    { List.map (fun tv -> mk_binder (TVariable(tv)) tv.idRange Kind None) $2 }

aqual_opt:
  | HASH   { Some Implicit }
  | EQUALS { Some Equality }
  |        { None }

binders: 
  | binder binders { $1::$2 }
  |                { []     }

tyconDefn: 
  |               { (fun id binders kopt -> TyconAbstract(id, binders, kopt)) }
  | EQUALS typ    { (fun id binders kopt -> TyconAbbrev(id, binders, kopt, $2)) }
  | EQUALS LBRACE recordFieldDecl recordFields RBRACE { (fun id binders kopt -> TyconRecord(id, binders, kopt, $3::$4)) }
  | EQUALS constructors { (fun id binders kopt -> TyconVariant(id, binders, kopt, $2)) }


recordFields:
  | SEMICOLON recordFieldDecl recordFields
      { $2::$3 }
  | SEMICOLON { [] }
  |   { [] }


constructors:
  |   { [] }
  | constructors constructorDecl
      { $1@[$2] }

recordFieldDecl:
  |  ident COLON tmTuple
      { ($1, $3) }

constructorDecl:
  | BAR name COLON typ
      { ($2,Some $4,false) }
  | BAR name of_typ
      { ($2,$3,true) }

of_typ:
  |        {  None }
  | OF typ { Some $2 }

eitherQname: 
  | eitherpath { lid_of_ids $1 }

eitherpath: 
  | ident { [$1] }
  | name maybeMorePath { $1::$2 }

maybeMorePath:
  |		  { [] }
  | DOT eitherpath  { $2 }

lid:
  | idpath { lid_of_ids $1 }

qname:
  | namepath { lid_of_ids $1 }

eitherName:
  | ident { $1 } 
  | name  { $1 }

ident:
  | IDENT 
      { mk_ident($1, rhs parseState 1)}

name:
  | NAME 
      { mk_ident($1, rhs parseState 1) }

tvars:
  | TVAR                { [mk_ident($1, rhs parseState 1)] }
  | TVAR COMMA tvars    { mk_ident($1, rhs parseState 1) ::$3 }

tvar:
  | TVAR 
      { mk_ident($1, rhs parseState 1) }

namepath:
  | name 
      { [$1] }
  | name DOT namepath
      { $1::$3 }

idpath:
  | ident 
      { [$1] }
  | name DOT idpath
      { $1::$3 }
      
ascribeTypOpt:
  |              { None }
  | COLON product { Some $2 }

ascribeKindOpt: 
  |               { None }
  | COLON  kind { Some $2 }

kind:
  | product { {$1 with level=Kind} }
      
typ:
  | simpleTerm  { $1 }

  | FORALL binders DOT qpat noSeqTerm
      {
        mk_term (QForall($2,$4,$5)) (rhs2 parseState 1 5) Formula
      }

  | EXISTS binders DOT qpat noSeqTerm
      {
        mk_term (QExists($2,$4,$5)) (rhs2 parseState 1 5) Formula
      }
  
term:
  | noSeqTerm 
      { $1 }

  | noSeqTerm SEMICOLON term 
      { mk_term (Seq($1, $3)) (rhs2 parseState 1 3) Expr }


noSeqTerm:
  | typ  { $1 }

  | REQUIRES label_opt typ 
     {
        let pos = rhs2 parseState 1 4 in
        let f = $3 in
        mk_term (Requires(f, $2)) pos Type
     }

	 
  | ENSURES label_opt typ 
     {
        let pos = rhs2 parseState 1 4 in
        let f = $3 in
        mk_term (Ensures(f, $2)) pos Type 
     }

  | IF noSeqTerm THEN noSeqTerm ELSE noSeqTerm
      {
        mk_term (If($2, $4, $6)) (rhs2 parseState 1 6) Expr
      } 

  | IF noSeqTerm THEN noSeqTerm
      {
	    let e = mk_term (Const Const_unit) (rhs2 parseState 4 4) Expr in
        mk_term (If($2, $4, e)) (rhs2 parseState 1 4) Expr
      } 

  | TRY term WITH firstPatternBranch patternBranches 
      {
		 let branches = focusBranches ($4::$5) (rhs2 parseState 1 5) in
         mk_term (TryWith($2, branches)) (rhs2 parseState 1 5) Expr
      }

  | MATCH term WITH firstPatternBranch patternBranches 
      {	
		let branches = focusBranches ($4::$5) (rhs2 parseState 1 5) in
	    mk_term (Match($2, branches)) (rhs2 parseState 1 5) Expr
      }

  | LET recopt letbinding letbindings IN term
      {
		let r, focus = $2 in 
		let lbs = focusLetBindings ((focus,$3)::$4) (rhs2 parseState 2 4) in
        mk_term (Let(r, lbs, $6)) (rhs2 parseState 1 6) Expr
      }

  | FUNCTION firstPatternBranch patternBranches 
      { 
	    let branches = focusBranches ($2::$3) (rhs2 parseState 1 3) in
        mk_function branches (lhs parseState) (rhs2 parseState 1 3)
      }

  | ASSUME atomicTerm 
      { mkExplicitApp (mk_term (Var assume_lid) (rhs parseState 1) Expr) [$2]
          (rhs2 parseState 1 2)
      }

label_opt:
  |                        { None }
  | DOLLAR STRING          { Some (string_of_bytes $2) }

qpat: 
  |   { [] }
  | LBRACE_COLON_PATTERN appTerm morePats RBRACE
      { $2::$3 }

morePats:
  |  { [] }
  | SEMICOLON appTerm morePats  { $2::$3 }

simpleTerm:
  | tmIff { $1 }

  | FUN atomicPattern atomicPatterns funArrow term
      {
        $4 (Abs($2::$3, $5)) (rhs2 parseState 1 5) 
      }

patternBranches:
  |   { [] }
  | patternBranches patternBranch
      { $1@[$2] }

maybeBar:
  |     {()}
  | BAR {()}

maybeFocusArrow:
  | RARROW          { false }
  | SQUIGGLY_RARROW { true }

firstPatternBranch: /* shift/reduce conflict on BAR ... expected for nested matches */
  | maybeBar disjunctivePattern maybeWhen maybeFocusArrow term 
      { 
        let pat = match $2 with 
          | [p] -> p 
          | ps -> mk_pattern (PatOr ps) (rhs2 parseState 1 2) in
        ($4, (pat, $3, $5))
      }

patternBranch: /* shift/reduce conflict on BAR ... expected for nested matches */
  | BAR disjunctivePattern maybeWhen maybeFocusArrow term 
      { 
        let pat = match $2 with 
          | [p] -> p 
          | ps -> mk_pattern (PatOr ps) (rhs2 parseState 1 2) in
        ($4, (pat, $3, $5)) 
      }

disjunctivePattern:
  | pattern     { [$1] }
  | pattern BAR disjunctivePattern { $1::$3 }

maybeWhen:
  |             { None }
  | WHEN appTerm { Some $2 }       

funArrow:
  | RARROW { fun t r -> mk_term t r Un }
 
tmIff:
  | tmImplies IFF tmIff
      {
        mk_term (Op("<==>", [$1; $3])) (rhs2 parseState 1 3) Formula
      }

  | tmImplies
      { $1 }

tmImplies:
  | tmDisjunction IMPLIES tmImplies
      {
        mk_term (Op("==>", [$1; $3])) (rhs2 parseState 1 3) Formula
      }

  | tmDisjunction
      { $1 }

tmDisjunction:
  | tmDisjunction DISJUNCTION tmConjunction
      { mk_term (Op("\\/", [$1;$3])) (rhs2 parseState 1 3) Formula }
      
  | tmConjunction
      { $1 }

tmConjunction:
  | tmConjunction CONJUNCTION tmTuple
      { mk_term (Op("/\\", [$1;$3])) (rhs2 parseState 1 3) Formula }

  | tmTuple
      { $1 }

tmTuple:
  | tupleN
      {
        match $1 with 
          | [x] -> x
          | components -> mkTuple components (rhs2 parseState 1 1)
      }

tmEq:
  | tmEq COLON_EQUALS tmOr
      {
        mk_term (Op(":=", [$1; $3])) (rhs2 parseState 1 3) Un
      }

  | tmOr
      { $1 }

tmOr:
  | tmOr BAR_BAR tmAnd
      { mk_term (Op("||", [$1; $3])) (rhs2 parseState 1 3) Un}

  | tmAnd
      { $1 }

tmAnd:
  | tmAnd AMP_AMP cmpTerm
      { mk_term (Op("&&", [$1;$3])) (rhs2 parseState 1 3) Un}

  | cmpTerm
      { $1 }
      
cmpTerm:
  | cmpTerm comparisonOp tmCons
      { mk_term (Op($2, [$1;$3])) (rhs2 parseState 1 3) Expr }
  | tmCons 
      { $1 }

comparisonOp:
  | CUSTOM_OP { $1 }
  | EQUALS    { "=" }
 
tmCons:
  | product COLON_COLON tmCons
      { consTerm (rhs parseState 2) $1 $3 }

  | product
      { $1 }

product: 
  | productDomain RARROW product
	 {
		let aq, tm = $1 in 
		let b = match extract_named_refinement tm with 
			| None -> mk_binder (NoName tm) (rhs parseState 1) Un aq
			| Some (x, t, f) -> mkRefinedBinder x t f (rhs2 parseState 1 1) aq in
        mk_term (Product([b], $3)) (rhs2 parseState 1 3)  Un
	 
	 }

  | dtupleTerm 
	  { $1 }

productDomain:
  | aqual dtupleTerm { (Some $1, $2) }
  | dtupleTerm      { (None, $1)    }

dtupleTerm:
  | arithTerm AMP dtupleTerm
      { 
		let x, t, f = match extract_named_refinement $1 with 
			| Some (x, t, f) -> x, t, f
			| _ -> raise (Error("Missing binder for the first component of a dependent tuple", rhs2 parseState 1 2)) in
	    let dom = mkRefinedBinder x t f (rhs2 parseState 1 2) None in
		let tail = $3 in
		let dom, res = match tail.tm with 
			| Sum(dom', res) -> dom::dom', res
			| _ -> [dom], tail in 
	    mk_term (Sum(dom, res)) (rhs2 parseState 1 6) Type
	  }

  | arithTerm
	 { $1 }
  
arithTerm:
  | plusOp 
	  { $1 }

plusOp:
  | minusOp PLUS_OP plusOp
      { mk_term (Op("+", [$1;$3])) (rhs2 parseState 1 3) Un}

  | minusOp
	  { $1 }

minusOp:
  | minusOp MINUS_OP starDivModTerm
      { mk_term (Op("-", [$1;$3])) (rhs2 parseState 1 3) Un}

  | starDivModTerm 
      { $1 }


starDivModTerm:
  | refinementTerm STAR starDivModTerm 
      { 
        mk_term (Op("*", [$1;$3])) (rhs2 parseState 1 3) Un
      }

  | unaryTerm DIV_MOD_OP starDivModTerm
      { mk_term (Op($2, [$1;$3])) (rhs2 parseState 1 3) Un}

  | refinementTerm
      { 
        $1 
      }

refinementTerm:
  | ident COLON appTerm 
      {
        mk_term (NamedTyp($1, $3)) (rhs2 parseState 1 3) Type 
      }
	  
  | ident COLON appTerm LBRACE formula RBRACE 
      {
        mk_term (Refine(mk_binder (Annotated($1, $3)) (rhs2 parseState 1 3) Type None, $5)) 
        (rhs2 parseState 1 6) Type
      }
	  
  | LBRACE recordExp RBRACE { $2 }

  | unaryTerm { $1 }
  
aqual:
  | HASH      { Implicit }
  | EQUALS    { Equality }

refineOpt:
  |                       { None }
  | LBRACE formula RBRACE { Some $2 }



unaryTerm: 
  | PLUS_OP atomicTerm
      { mk_term (Op("+", [$2])) (rhs2 parseState 1 3) Expr }

  | MINUS_OP atomicTerm
      { mk_term (Op("-", [$2])) (rhs2 parseState 1 3) Expr }

  | TILDE atomicTerm
      { mk_term (Op($1, [$2])) (rhs2 parseState 1 3) Formula }

  | appTerm { $1 }

appTerm:
  | atomicTerm hashAtomicTerms
      {
        mkApp $1 $2 (rhs2 parseState 1 2)
      }

formula:
  | noSeqTerm
      { {$1 with level=Formula} }

atomicTerm:
  | UNDERSCORE { mk_term Wild (rhs parseState 1) Un }
  | ASSERT   { mk_term (Var assert_lid) (rhs parseState 1) Expr }
  | tvar     { mk_term (Tvar($1)) (rhs parseState 1) Type }
  | constant { mk_term (Const $1) (rhs parseState 1) Expr }
  | LENS_PAREN_LEFT tupleN LENS_PAREN_RIGHT 
      { 
        match $2 with 
          | [x] -> x
          | components -> mkDTuple components (rhs2 parseState 1 1)
      }
  | projectionLHS maybeFieldProjections 
      {  
        List.fold_left (fun e f -> 
                          mk_term (Project(e, lid_of_ids [f])) (rhs2 parseState 1 3) Expr )
          $1 $2
      }
  | BANG atomicTerm
      { mk_term (Op("!", [$2])) (rhs2 parseState 1 2) Expr }
  | BEGIN term END 
      { $2 }

recdFieldTypes:
  |  { [] }
  | recdFieldType moreRecdFieldTypes { $1::$2 }

moreRecdFieldTypes:
  |  { [] }
  | SEMICOLON recdFieldType moreRecdFieldTypes { $2::$3 }

recdFieldType:
  | ident COLON typ { ($1, $3) }

maybeFieldProjections:
  |    { [] }
  | maybeFieldProjections DOT ident 
      { $1@[$3] }

targs:
  | atomicTerm { [$1] }
  | atomicTerm COMMA targs { $1::$3 }

maybeInsts:
  |    { (fun x -> x) }
  | TYP_APP_LESS targs TYP_APP_GREATER 
      {
        (fun (x:term) -> mkFsTypApp x $2
          (union_ranges x.range (rhs2 parseState 1 3)))
      }

projectionLHS:
  | eitherQname maybeInsts
      { 
        let t = if is_name $1 then Name $1 else Var $1 in
        $2 (mk_term t (rhs parseState 1) Un) 
      }
  | LPAREN term maybeWithSort RPAREN 
      { mk_term (Paren($3 $2 (rhs2 parseState 2 3))) (rhs2 parseState 1 4) ($2.level) }
  | LBRACK_BAR semiColonTermList BAR_RBRACK
      {
        let l = mkConsList (rhs2 parseState 1 3) $2 in 
        mkExplicitApp (mk_term (Var (array_mk_array_lid)) (rhs2 parseState 1 3) Expr) 
              [l] (rhs2 parseState 1 3)
      }
  | LBRACK semiColonTermList RBRACK
      { 
        mkConsList (rhs2 parseState 1 3) $2
      }
  | PERCENT_LBRACK semiColonTermList RBRACK
	  {
		mkLexList (rhs2 parseState 1 3) $2
	  }

  | BANG_LBRACE commaTermList RBRACE
	  {
		mkRefSet (rhs2 parseState 1 3) $2
	  }
	  
commaTermList:
  |  { [] }
  | appTerm moreCommaTerms
      { $1::$2 }

moreCommaTerms:
  |   { [] }
  | COMMA appTerm moreCommaTerms
      { $2::$3 }


semiColonTermList:
  |  { [] }
  | noSeqTerm moreSemiColonTerms
      { $1::$2 }

moreSemiColonTerms:
  |   { [] }
  | SEMICOLON { [] }
  | SEMICOLON noSeqTerm moreSemiColonTerms 
      { $2::$3 }

recordExp: 
  | appTerm recordExpRest 
      { $2 $1 (lhs parseState) }

recordExpRest:
  | WITH recordFieldAssignment recordFieldAssignments
      {
        (fun e r -> mk_term (Record(Some e, $2::$3)) (union_ranges r (rhs2 parseState 1 3)) Expr)
      }

  | EQUALS simpleTerm recordFieldAssignments
      {
        (fun e r -> match e.tm with 
          | Var l -> mk_term (Record(None, (l,$2)::$3)) (union_ranges r (rhs2 parseState 1 3)) Expr
          | _ -> errorR(Error("Record field names must be constant identifiers", lhs(parseState)));
            mk_term (Record(None, $3)) (rhs2 parseState 1 3) Expr)
      }

recordFieldAssignment:
  | lid EQUALS simpleTerm
      { ($1,$3) }

recordFieldAssignments:
  |           { [] }
  | SEMICOLON { [] }
  | SEMICOLON recordFieldAssignment recordFieldAssignments
     { $2::$3 }

maybeWithSort:
  |     { fun x r -> x }
  | hasSort simpleTerm
        { fun x r -> mk_term (Ascribed(x,{$2 with level=$1})) (union_ranges r (rhs2 parseState 1 2)) $1 }
hasSort:
  | SUBTYPE { Expr }
  | SUBKIND { Type }

maybeHash: 
  |      { Nothing }
  | HASH { Hash }

hashAtomicTerms:
  |        { [] }
  | maybeHash atomicTerm hashAtomicTerms { ($2, $1)::$3 }

atomicTerms: 
  |        { [] }
  | atomicTerm atomicTerms { $1::$2 }

consTerm:
  |       { None }
  | COLON_COLON tmTuple consTerm 
      { 
        match $3 with 
          | None -> Some $2
          | Some tl -> Some (consTerm (rhs2 parseState 2 3) $2 tl)
      }

tupleN:
  | tmEq                       { [$1] }
  | tmEq COMMA tupleN       { $1::$3 }

constant:  
  | LPAREN_RPAREN { Const_unit }
  | INT 
	 {
	    if snd $1 then 
          errorR(Error("This number is outside the allowable range for representable integer constants", lhs(parseState)));
        Const_int (fst $1) 
	 }
  | INT32 
      { 
        if snd $1 then 
          errorR(Error("This number is outside the allowable range for 32-bit signed integers", lhs(parseState)));
        Const_int32 (fst $1) 
      } 
  | UINT8 { Const_uint8 $1 } 
  
  | CHAR { Const_char $1 } 
  | STRING { Const_string ($1,lhs(parseState)) } 
  | BYTEARRAY { Const_bytearray ($1,lhs(parseState)) }
  | TRUE { Const_bool true }
  | FALSE { Const_bool false } 
  | IEEE64 { Const_float $1 } 
  | INT64 
      { 
        if snd $1 then 
          errorR(Error("This number is outside the allowable range for 64-bit signed integers", lhs(parseState)));
        Const_int64 (fst $1) 
      }   
/*
  | UINT32 { Const_uint32 $1 } 
  | INT8 
      { 
        if snd $1 then
          errorR(Error("This number is outside the allowable range for 8-bit signed integers", lhs(parseState)));
        Const_int8 (fst $1) 
      } 
  | INT16 
      { 
        if snd $1 then 
          errorR(Error("This number is outside the allowable range for 16-bit signed integers", lhs(parseState)));
        Const_int16 (fst $1) 
      } 
  | UINT16 { Const_uint16 $1 } 
  | BIGINT { Const_bigint $1 } 
  | DECIMAL { Const_decimal $1 } 
  | BIGNUM { Const_bignum $1 } 
  | UINT64 { Const_uint64 $1 } 
  | NATIVEINT { Const_nativeint $1 } 
  | UNATIVEINT { Const_unativeint $1 } 
  | IEEE32 { Const_float32 $1 } 
*/