| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
|
| 11 |
DECLARE GLOBAL PLUGIN |
| 12 |
|
| 13 |
{ |
| 14 |
|
| 15 |
open Names |
| 16 |
open Constr |
| 17 |
open Libnames |
| 18 |
open Glob_term |
| 19 |
open Constrexpr |
| 20 |
open Constrexpr_ops |
| 21 |
open Util |
| 22 |
|
| 23 |
open Procq |
| 24 |
open Procq.Prim |
| 25 |
open Procq.Constr |
| 26 |
|
| 27 |
(* TODO: avoid this redefinition without an extra dep to Notation_ops *) |
| 28 |
let ldots_var = Id.of_string ".." |
| 29 |
|
| 30 |
let binder_of_name expl { CAst.loc = loc; v = na } = |
| 31 |
CLocalAssum ([CAst.make ?loc na], None, Default expl, |
| 32 |
CAst.make ?loc @@ CHole (Some (GBinderType na))) |
| 33 |
|
| 34 |
let binders_of_names l = |
| 35 |
List.map (binder_of_name Explicit) l |
| 36 |
|
| 37 |
let pat_of_name CAst.{loc;v} = match v with |
| 38 |
| Anonymous -> CAst.make ?loc @@ CPatAtom None |
| 39 |
| Name id -> CAst.make ?loc @@ CPatAtom (Some (qualid_of_ident id)) |
| 40 |
|
| 41 |
(* Hack to parse "(x:=t)" as an explicit argument without conflicts with the *) |
| 42 |
(* admissible notation "(x t)" *) |
| 43 |
let test_lpar_id_coloneq = |
| 44 |
let open Procq.Lookahead in |
| 45 |
to_entry "test_lpar_id_coloneq" begin |
| 46 |
lk_kw "(" >> lk_ident >> lk_kw ":=" |
| 47 |
end |
| 48 |
|
| 49 |
(* Hack to parse "(n:=t)" as an explicit argument without conflicts with the *) |
| 50 |
(* syntax "(n t)" *) |
| 51 |
let test_lpar_nat_coloneq = |
| 52 |
let open Procq.Lookahead in |
| 53 |
to_entry "test_lpar_id_coloneq" begin |
| 54 |
lk_kw "(" >> lk_nat >> lk_kw ":=" |
| 55 |
end |
| 56 |
|
| 57 |
let ensure_fixannot = |
| 58 |
let open Procq.Lookahead in |
| 59 |
to_entry "check_fixannot" begin |
| 60 |
lk_kw "{" >> lk_kws ["wf"; "struct"; "measure"] |
| 61 |
end |
| 62 |
|
| 63 |
let test_name_colon = |
| 64 |
let open Procq.Lookahead in |
| 65 |
to_entry "test_name_colon" begin |
| 66 |
lk_name >> lk_kw ":" |
| 67 |
end |
| 68 |
|
| 69 |
let aliasvar = function { CAst.v = CPatAlias (_, na) } -> Some na | _ -> None |
| 70 |
|
| 71 |
let test_array_opening = |
| 72 |
let open Procq.Lookahead in |
| 73 |
to_entry "test_array_opening" begin |
| 74 |
lk_kw "[" >> lk_kw "|" >> check_no_space |
| 75 |
end |
| 76 |
|
| 77 |
let test_array_closing = |
| 78 |
let open Procq.Lookahead in |
| 79 |
to_entry "test_array_closing" begin |
| 80 |
lk_kw "|" >> lk_kw "]" >> check_no_space |
| 81 |
end |
| 82 |
|
| 83 |
let test_old_sort_qvar = |
| 84 |
let open Procq.Lookahead in |
| 85 |
to_entry "test_old_sort_qvar" begin |
| 86 |
lk_ident >> lk_list lk_field >> lk_kw "|" |
| 87 |
end |
| 88 |
|
| 89 |
let test_sort_qvar = |
| 90 |
let open Procq.Lookahead in |
| 91 |
to_entry "test_sort_qvar" begin |
| 92 |
lk_ident >> lk_list lk_field >> lk_kw ";" |
| 93 |
end |
| 94 |
|
| 95 |
let warn_old_sort_syntax = |
| 96 |
CWarnings.create ~name:"deprecated-sort-poly-syntax" ~category:Deprecation.Version.v9_1 |
| 97 |
Pp.(fun () -> fmt "Separating sorts from universes with \"|\" is deprecated.@ Use \";\" instead.") |
| 98 |
|
| 99 |
type univ_level_or_quality = |
| 100 |
| SProp | Prop | Set | Type | Anon of Loc.t | Global of Libnames.qualid |
| 101 |
|
| 102 |
let force_univ_level ?loc = function |
| 103 |
| SProp -> UNamed CSProp |
| 104 |
| Prop -> UNamed CProp |
| 105 |
| Set -> UNamed CSet |
| 106 |
| Type -> UAnonymous {rigid=UnivRigid} |
| 107 |
| Anon _ -> UAnonymous {rigid=UnivFlexible false} |
| 108 |
| Global qid -> UNamed (CType qid) |
| 109 |
|
| 110 |
let force_quality ?loc = function |
| 111 |
| SProp -> CQConstant QSProp |
| 112 |
| Prop -> CQConstant QProp |
| 113 |
| Set -> CErrors.user_err ?loc Pp.(str "Universe levels cannot be Set.") |
| 114 |
| Type -> CQConstant QType |
| 115 |
| Anon loc -> CQualVar (CQAnon (Some loc)) |
| 116 |
| Global qid -> CQualVar (CQVar qid) |
| 117 |
|
| 118 |
(* XXX use registered ref? but currently constrexpr doesn't have a node for registered refs |
| 119 |
and we can't do [Rocqlib.lib_ref] at parsing time, it's only available in the interp phase. *) |
| 120 |
let sigref loc = Libnames.qualid_of_string ~loc "Corelib.Init.Specif.sig" |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
GRAMMAR EXTEND Gram |
| 125 |
GLOBAL: binder_constr lconstr constr term |
| 126 |
universe_name sort sort_quality_or_set sort_quality_var |
| 127 |
global constr_pattern cpattern Constr.ident |
| 128 |
closed_binder open_binders binder binders binders_fixannot |
| 129 |
record_declaration typeclass_constraint pattern arg type_cstr |
| 130 |
one_closed_binder one_open_binder; |
| 131 |
Constr.ident: |
| 132 |
[ [ id = Prim.ident -> { id } ] ] |
| 133 |
; |
| 134 |
Prim.name: TOP |
| 135 |
[ [ "_" -> { CAst.make ~loc Anonymous } ] ] |
| 136 |
; |
| 137 |
global: |
| 138 |
[ [ r = Prim.reference -> { r } ] ] |
| 139 |
; |
| 140 |
constr_pattern: |
| 141 |
[ [ c = constr -> { c } ] ] |
| 142 |
; |
| 143 |
cpattern: |
| 144 |
[ [ c = lconstr -> { c } ] ] |
| 145 |
; |
| 146 |
sort: |
| 147 |
[ [ "Set" -> { None, UNamed [CSet, 0] } |
| 148 |
| "Prop" -> { None, UNamed [CProp, 0] } |
| 149 |
| "SProp" -> { None, UNamed [CSProp, 0] } |
| 150 |
| "Type" -> { None, UAnonymous {rigid=UnivRigid} } |
| 151 |
| "Type"; "@{"; "_"; "}" -> { None, UAnonymous {rigid=UnivFlexible false} } |
| 152 |
| "Type"; "@{"; test_old_sort_qvar; q = reference; pipe_loc = [ "|" -> { loc } ]; u = universe; "}" -> { |
| 153 |
warn_old_sort_syntax ~loc:pipe_loc (); |
| 154 |
Some (CQVar q), u |
| 155 |
} |
| 156 |
| "Type"; "@{"; test_sort_qvar; q = reference; ";"; u = universe; "}" -> { |
| 157 |
Some (CQVar q), u |
| 158 |
} |
| 159 |
| "Type"; "@{"; u = universe; "}" -> { None, u } ] ] |
| 160 |
; |
| 161 |
sort_quality_or_set: |
| 162 |
[ [ "Prop" -> { UnivGen.QualityOrSet.prop } |
| 163 |
| "SProp" -> { UnivGen.QualityOrSet.sprop } |
| 164 |
| "Set" -> { UnivGen.QualityOrSet.Set } |
| 165 |
| "Type" -> { UnivGen.QualityOrSet.qtype } ] ] |
| 166 |
; |
| 167 |
sort_quality_var: |
| 168 |
[ [ "Prop" -> { CQConstant Sorts.Quality.QProp } |
| 169 |
| "SProp" -> { CQConstant Sorts.Quality.QSProp } |
| 170 |
| "Type" -> { CQConstant Sorts.Quality.QType } |
| 171 |
| v = reference -> { CQualVar (CQVar v) } ] ] |
| 172 |
; |
| 173 |
universe_increment: |
| 174 |
[ [ "+"; n = natural -> { n } |
| 175 |
| -> { 0 } ] ] |
| 176 |
; |
| 177 |
universe_name: |
| 178 |
[ [ id = global -> { CType id } |
| 179 |
| "Set" -> { CSet } |
| 180 |
| "Prop" -> { CProp } ] ] |
| 181 |
; |
| 182 |
universe_expr: |
| 183 |
[ [ id = universe_name; n = universe_increment -> { (id,n) } |
| 184 |
| n = natural -> { CSet, n } ] ] |
| 185 |
; |
| 186 |
universe: |
| 187 |
[ [ IDENT "max"; "("; ids = LIST1 universe_expr SEP ","; ")" -> { UNamed ids } |
| 188 |
| "_" -> { UAnonymous { rigid = UnivFlexible false } } |
| 189 |
| u = universe_expr -> { UNamed [u] } ] ] |
| 190 |
; |
| 191 |
lconstr: |
| 192 |
[ [ c = term LEVEL "200" -> { c } ] ] |
| 193 |
; |
| 194 |
constr: |
| 195 |
[ [ c = term LEVEL "8" -> { c } |
| 196 |
| "@"; f=global; i = univ_annot -> { CAst.make ~loc @@ CAppExpl((f,i),[]) } ] ] |
| 197 |
; |
| 198 |
term: |
| 199 |
[ "200" RIGHTA [ ] |
| 200 |
| "100" RIGHTA |
| 201 |
[ c1 = term; "<:"; c2 = term LEVEL "200" -> |
| 202 |
{ CAst.make ~loc @@ CCast(c1, Some VMcast, c2) } |
| 203 |
| c1 = term; "<<:"; c2 = term LEVEL "200" -> |
| 204 |
{ CAst.make ~loc @@ CCast(c1, Some NATIVEcast, c2) } |
| 205 |
| c1 = term; ":>"; c2 = term LEVEL "200" -> |
| 206 |
{ CAst.make ~loc @@ CCast(c1, None, c2) } |
| 207 |
| c1 = term; ":"; c2 = term LEVEL "200" -> |
| 208 |
{ CAst.make ~loc @@ CCast(c1, Some DEFAULTcast, c2) } ] |
| 209 |
| "99" RIGHTA [ ] |
| 210 |
| "90" RIGHTA [ ] |
| 211 |
| "10" LEFTA |
| 212 |
[ f = term; args = LIST1 arg -> { CAst.make ~loc @@ CApp(f,args) } |
| 213 |
| "@"; f = global; i = univ_annot; args = LIST0 NEXT -> { CAst.make ~loc @@ CAppExpl((f,i),args) } |
| 214 |
| "@"; lid = pattern_ident; args = LIST1 identref -> |
| 215 |
{ let { CAst.loc = locid; v = id } = lid in |
| 216 |
let args = List.map (fun x -> CAst.make @@ CRef (qualid_of_ident ?loc:x.CAst.loc x.CAst.v, None), None) args in |
| 217 |
CAst.make ~loc @@ CApp(CAst.make ?loc:locid @@ CPatVar id,args) } |
| 218 |
| c = binder_constr -> { c } ] |
| 219 |
| "9" |
| 220 |
[ ".."; c = term LEVEL "0"; ".." -> |
| 221 |
{ CAst.make ~loc @@ CAppExpl ((qualid_of_ident ~loc ldots_var, None),[c]) } ] |
| 222 |
| "8" [ ] |
| 223 |
| "1" LEFTA |
| 224 |
[ c = term; ".("; f = global; i = univ_annot; args = LIST0 arg; ")" -> |
| 225 |
{ CAst.make ~loc @@ CProj(false, (f,i), args, c) } |
| 226 |
| c = term; ".("; "@"; f = global; i = univ_annot; |
| 227 |
args = LIST0 (term LEVEL "9"); ")" -> |
| 228 |
{ CAst.make ~loc @@ CProj(true, (f,i), List.map (fun a -> (a,None)) args, c) } |
| 229 |
| c = term; "%"; key = IDENT -> |
| 230 |
{ CAst.make ~loc @@ CDelimiters (DelimUnboundedScope,key,c) } |
| 231 |
| c = term; "%_"; key = IDENT -> |
| 232 |
{ CAst.make ~loc @@ CDelimiters (DelimOnlyTmpScope,key,c) } ] |
| 233 |
| "0" |
| 234 |
[ c = atomic_constr -> { c } |
| 235 |
| c = term_match -> { c } |
| 236 |
| id = reference; i = univ_annot -> |
| 237 |
{ CAst.make ~loc @@ CRef (id, i) } |
| 238 |
| n = NUMBER-> { CAst.make ~loc @@ CPrim (Number (NumTok.SPlus,n)) } |
| 239 |
| s = string -> { CAst.make ~loc @@ CPrim (String s) } |
| 240 |
| "("; c = term LEVEL "200"; ")" -> |
| 241 |
{ (* Preserve parentheses around numbers so that constrintern does not |
| 242 |
collapse -(3) into the number -3. *) |
| 243 |
(match c.CAst.v with |
| 244 |
| CPrim (Number (NumTok.SPlus,n)) -> |
| 245 |
CAst.make ~loc @@ CNotation(None,(InConstrEntry,"( _ )"),([c],[],[],[])) |
| 246 |
| _ -> c) } |
| 247 |
| "{|"; c = record_declaration; bar_cbrace -> { c } |
| 248 |
| "`{"; c = term LEVEL "200"; "}" -> |
| 249 |
{ CAst.make ~loc @@ CGeneralization (MaxImplicit, c) } |
| 250 |
| test_array_opening; "["; "|"; ls = array_elems; "|"; def = lconstr; ty = type_cstr; test_array_closing; "|"; "]"; u = univ_annot -> |
| 251 |
{ let t = Array.make (List.length ls) def in |
| 252 |
List.iteri (fun i e -> t.(i) <- e) ls; |
| 253 |
CAst.make ~loc @@ CArray(u, t, def, ty) |
| 254 |
} |
| 255 |
| "`("; c = term LEVEL "200"; ")" -> |
| 256 |
{ CAst.make ~loc @@ CGeneralization (Explicit, c) } ] ] |
| 257 |
; |
| 258 |
array_elems: |
| 259 |
[ [ fs = LIST0 lconstr SEP ";" -> { fs } ]] |
| 260 |
; |
| 261 |
record_declaration: |
| 262 |
[ [ fs = fields_def -> { CAst.make ~loc @@ CRecord fs } ] ] |
| 263 |
; |
| 264 |
fields_def: |
| 265 |
[ RIGHTA [ f = field_def; ";"; fs = fields_def -> { f :: fs } |
| 266 |
| f = field_def -> { [f] } |
| 267 |
| -> { [] } ] ] |
| 268 |
; |
| 269 |
field_def: |
| 270 |
[ [ id = global; bl = binders; ":="; c = lconstr -> |
| 271 |
{ (id, mkLambdaCN ~loc bl c) } ] ] |
| 272 |
; |
| 273 |
binder_constr: |
| 274 |
[ [ "forall"; bl = open_binders; ","; c = term LEVEL "200" -> |
| 275 |
{ mkProdCN ~loc bl c } |
| 276 |
| "fun"; bl = open_binders; "=>"; c = term LEVEL "200" -> |
| 277 |
{ mkLambdaCN ~loc bl c } |
| 278 |
| "let"; id=name; bl = binders; ty = let_type_cstr; ":="; |
| 279 |
c1 = term LEVEL "200"; "in"; c2 = term LEVEL "200" -> |
| 280 |
{ let ty,c1 = match ty, c1 with |
| 281 |
| (_,None), { CAst.v = CCast(c, Some DEFAULTcast, t) } -> (Loc.tag ?loc:(constr_loc t) @@ Some t), c (* Tolerance, see G_vernac.def_body *) |
| 282 |
| _, _ -> ty, c1 in |
| 283 |
CAst.make ~loc @@ CLetIn(id,mkLambdaCN ?loc:(constr_loc c1) bl c1, |
| 284 |
Option.map (mkProdCN ?loc:(fst ty) bl) (snd ty), c2) } |
| 285 |
| "let"; "fix"; fx = fix_decl; "in"; c = term LEVEL "200" -> |
| 286 |
{ let {CAst.loc=locf;CAst.v=({CAst.loc=li;CAst.v=id} as lid,_,_,_,_,_ as dcl)} = fx in |
| 287 |
let fix = CAst.make ?loc:locf @@ CFix (lid,[dcl]) in |
| 288 |
CAst.make ~loc @@ CLetIn( CAst.make ?loc:li @@ Name id,fix,None,c) } |
| 289 |
| "let"; "cofix"; fx = cofix_body; "in"; c = term LEVEL "200" -> |
| 290 |
{ let {CAst.loc=locf;CAst.v=({CAst.loc=li;CAst.v=id} as lid,_,_,_,_ as dcl)} = fx in |
| 291 |
let cofix = CAst.make ?loc:locf @@ CCoFix (lid,[dcl]) in |
| 292 |
CAst.make ~loc @@ CLetIn( CAst.make ?loc:li @@ Name id,cofix,None,c) } |
| 293 |
| "let"; lb = ["("; l=LIST0 name SEP ","; ")" -> { l } | "()" -> { [] } ]; |
| 294 |
po = as_return_type; ":="; c1 = term LEVEL "200"; "in"; |
| 295 |
c2 = term LEVEL "200" -> |
| 296 |
{ CAst.make ~loc @@ CLetTuple (lb,po,c1,c2) } |
| 297 |
| "let"; "'"; p = pattern LEVEL "200"; t = OPT [ "in"; t = pattern LEVEL "200" -> { t } ]; |
| 298 |
":="; c1 = term LEVEL "200"; rt = OPT case_type; |
| 299 |
"in"; c2 = term LEVEL "200" -> |
| 300 |
{ CAst.make ~loc @@ |
| 301 |
CCases (LetPatternStyle, rt, [c1, aliasvar p, t], [CAst.make ~loc ([[p]], c2)]) } |
| 302 |
| "if"; c = term LEVEL "200"; po = as_return_type; |
| 303 |
"then"; b1 = term LEVEL "200"; |
| 304 |
"else"; b2 = term LEVEL "200" -> |
| 305 |
{ CAst.make ~loc @@ CIf (c, po, b1, b2) } |
| 306 |
| "fix"; c = fix_decls -> { let (id,dcls) = c in CAst.make ~loc @@ CFix (id,dcls) } |
| 307 |
| "cofix"; c = cofix_decls -> { let (id,dcls) = c in CAst.make ~loc @@ CCoFix (id,dcls) } ] ] |
| 308 |
; |
| 309 |
arg: |
| 310 |
[ [ test_lpar_id_coloneq; "("; id = identref; ":="; c = lconstr; ")" -> { (c,Some (CAst.make ?loc:id.CAst.loc @@ ExplByName id.CAst.v)) } |
| 311 |
| test_lpar_nat_coloneq; "("; n = natural; ":="; c = lconstr; ")" -> { (c,Some (CAst.make ~loc @@ ExplByPos n)) } |
| 312 |
| c=term LEVEL "9" -> { (c,None) } ] ] |
| 313 |
; |
| 314 |
atomic_constr: |
| 315 |
[ [ s = sort -> { CAst.make ~loc @@ CSort s } |
| 316 |
| "_" -> { CAst.make ~loc @@ CHole (None) } |
| 317 |
| "?"; "["; id = identref; "]" -> { CAst.make ~loc @@ CHole (Some (GNamedHole (false, id.CAst.v))) } |
| 318 |
| "?"; "["; id = pattern_ident; "]" -> { CAst.make ~loc @@ CHole (Some (GNamedHole (true, id.CAst.v))) } |
| 319 |
| id = pattern_ident; inst = evar_instance -> { CAst.make ~loc @@ CEvar(id,inst) } ] ] |
| 320 |
; |
| 321 |
inst: |
| 322 |
[ [ id = identref; ":="; c = lconstr -> { (id,c) } ] ] |
| 323 |
; |
| 324 |
evar_instance: |
| 325 |
[ [ "@{"; l = LIST1 inst SEP ";"; "}" -> { l } |
| 326 |
| -> { [] } ] ] |
| 327 |
; |
| 328 |
univ_annot: |
| 329 |
[ [ "@{"; l = LIST0 univ_level_or_quality; l' = OPT [ is_pipe = [ "|" -> { Some loc } | ";" -> { None } ]; l' = LIST0 univ_level_or_quality -> { is_pipe, l' } ]; "}" -> |
| 330 |
{ |
| 331 |
let l, l' = match l' with |
| 332 |
| None -> [], List.map (force_univ_level ~loc) l |
| 333 |
| Some (is_pipe, l') -> |
| 334 |
let () = Option.iter (fun loc -> warn_old_sort_syntax ~loc ()) is_pipe in |
| 335 |
List.map (force_quality ~loc) l, List.map (force_univ_level ~loc) l' |
| 336 |
in |
| 337 |
Some (l,l') |
| 338 |
} |
| 339 |
| -> { None } ] ] |
| 340 |
; |
| 341 |
univ_level_or_quality: |
| 342 |
[ [ NUMBER "0" -> { Set } |
| 343 |
| "Set" -> { Set } |
| 344 |
| "SProp" -> { SProp } |
| 345 |
| "Prop" -> { Prop } |
| 346 |
| "Type" -> { Type } |
| 347 |
| "_" -> { Anon loc } |
| 348 |
| id = global -> { Global id } ] ] |
| 349 |
; |
| 350 |
fix_decls: |
| 351 |
[ [ dcl = fix_decl -> { let (id,_,_,_,_,_) = dcl.CAst.v in (id,[dcl.CAst.v]) } |
| 352 |
| dcl = fix_decl; "with"; dcls = LIST1 fix_decl SEP "with"; "for"; id = identref -> |
| 353 |
{ (id,List.map (fun x -> x.CAst.v) (dcl::dcls)) } ] ] |
| 354 |
; |
| 355 |
cofix_decls: |
| 356 |
[ [ dcl = cofix_body -> { let (id,_,_,_,_) = dcl.CAst.v in (id,[dcl.CAst.v]) } |
| 357 |
| dcl = cofix_body; "with"; dcls = LIST1 cofix_body SEP "with"; "for"; id = identref -> |
| 358 |
{ (id,List.map (fun x -> x.CAst.v) (dcl::dcls)) } ] ] |
| 359 |
; |
| 360 |
fix_decl: |
| 361 |
[ [ id = identref; bl = binders_fixannot; ty = type_cstr; ":="; |
| 362 |
c = term LEVEL "200" -> |
| 363 |
{ CAst.make ~loc (id,None,snd bl,fst bl,ty,c) } ] ] |
| 364 |
; |
| 365 |
cofix_body: |
| 366 |
[ [ id = identref; bl = binders; ty = type_cstr; ":="; |
| 367 |
c = term LEVEL "200" -> |
| 368 |
{ CAst.make ~loc (id,None,bl,ty,c) } ] ] |
| 369 |
; |
| 370 |
term_match: |
| 371 |
[ [ "match"; ci = LIST1 case_item SEP ","; ty = OPT case_type; "with"; |
| 372 |
br = branches; "end" -> { CAst.make ~loc @@ CCases(RegularStyle,ty,ci,br) } ] ] |
| 373 |
; |
| 374 |
case_item: |
| 375 |
[ [ c = term LEVEL "100"; |
| 376 |
ona = OPT ["as"; id = name -> { id } ]; |
| 377 |
ty = OPT ["in"; t = pattern LEVEL "200" -> { t } ] -> |
| 378 |
{ (c,ona,ty) } ] ] |
| 379 |
; |
| 380 |
case_type: |
| 381 |
[ [ "return"; ty = term LEVEL "100" -> { ty } ] ] |
| 382 |
; |
| 383 |
as_return_type: |
| 384 |
[ [ a = OPT [ na = OPT ["as"; na = name -> { na } ]; |
| 385 |
ty = case_type -> { (na,ty) } ] -> |
| 386 |
{ match a with |
| 387 |
| None -> None, None |
| 388 |
| Some (na,t) -> (na, Some t) } ] ] |
| 389 |
; |
| 390 |
branches: |
| 391 |
[ [ OPT"|"; br = LIST0 eqn SEP "|" -> { br } ] ] |
| 392 |
; |
| 393 |
mult_pattern: |
| 394 |
[ [ pl = LIST1 pattern LEVEL "200" SEP "," -> { pl } ] ] |
| 395 |
; |
| 396 |
eqn: |
| 397 |
[ [ pll = LIST1 mult_pattern SEP "|"; |
| 398 |
"=>"; rhs = lconstr -> { (CAst.make ~loc (pll,rhs)) } ] ] |
| 399 |
; |
| 400 |
record_pattern: |
| 401 |
[ [ id = global; ":="; pat = pattern LEVEL "200" -> { (id, pat) } ] ] |
| 402 |
; |
| 403 |
record_patterns: |
| 404 |
[ RIGHTA [ p = record_pattern; ";"; ps = record_patterns -> { p :: ps } |
| 405 |
| p = record_pattern-> { [p] } |
| 406 |
| -> { [] } ] ] |
| 407 |
; |
| 408 |
pattern: |
| 409 |
[ "200" RIGHTA [ ] |
| 410 |
| "100" RIGHTA |
| 411 |
[ p = pattern; ":"; ty = term LEVEL "200" -> |
| 412 |
{ CAst.make ~loc @@ CPatCast (p, ty) } ] |
| 413 |
| "99" RIGHTA [ ] |
| 414 |
| "90" RIGHTA [ ] |
| 415 |
| "10" LEFTA |
| 416 |
[ p = pattern; "as"; na = name -> |
| 417 |
{ CAst.make ~loc @@ CPatAlias (p, na) } |
| 418 |
| p = pattern; lp = LIST1 NEXT -> { mkAppPattern ~loc p lp } |
| 419 |
| "@"; r = Prim.reference; lp = LIST0 NEXT -> |
| 420 |
{ CAst.make ~loc @@ CPatCstr (r, Some lp, []) } ] |
| 421 |
| "1" LEFTA |
| 422 |
[ c = pattern; "%"; key = IDENT -> |
| 423 |
{ CAst.make ~loc @@ CPatDelimiters (DelimUnboundedScope,key,c) } |
| 424 |
| c = pattern; "%_"; key = IDENT -> |
| 425 |
{ CAst.make ~loc @@ CPatDelimiters (DelimOnlyTmpScope,key,c) } ] |
| 426 |
| "0" |
| 427 |
[ r = Prim.reference -> { CAst.make ~loc @@ CPatAtom (Some r) } |
| 428 |
| "{|"; pat = record_patterns; bar_cbrace -> { CAst.make ~loc @@ CPatRecord pat } |
| 429 |
| "_" -> { CAst.make ~loc @@ CPatAtom None } |
| 430 |
| "("; p = pattern LEVEL "200"; ")" -> |
| 431 |
{ (* Preserve parentheses around numbers so that constrintern does not |
| 432 |
collapse -(3) into the number -3. *) |
| 433 |
match p.CAst.v with |
| 434 |
| CPatPrim (Number (NumTok.SPlus,n)) -> |
| 435 |
CAst.make ~loc @@ CPatNotation(None,(InConstrEntry,"( _ )"),([p],[],[]),[]) |
| 436 |
| _ -> p } |
| 437 |
| "("; p = pattern LEVEL "200"; "|" ; pl = LIST1 pattern LEVEL "200" SEP "|"; ")" -> |
| 438 |
{ CAst.make ~loc @@ CPatOr (p::pl) } |
| 439 |
| n = NUMBER-> { CAst.make ~loc @@ CPatPrim (Number (NumTok.SPlus,n)) } |
| 440 |
| s = string -> { CAst.make ~loc @@ CPatPrim (String s) } ] ] |
| 441 |
; |
| 442 |
fixannot: |
| 443 |
[ [ "{"; IDENT "struct"; id = identref; "}" -> { CAst.make ~loc @@ CStructRec id } |
| 444 |
| "{"; IDENT "wf"; rel = constr; id = identref; "}" -> { CAst.make ~loc @@ CWfRec(id,rel) } |
| 445 |
| "{"; IDENT "measure"; m = constr; id = OPT identref; rel = OPT constr; "}" -> |
| 446 |
{ CAst.make ~loc @@ CMeasureRec (id,m,rel) } ] ] |
| 447 |
; |
| 448 |
binders_fixannot: |
| 449 |
[ RIGHTA [ ensure_fixannot; f = fixannot -> { [], Some f } |
| 450 |
| b = binder; bl = binders_fixannot -> { b @ fst bl, snd bl } |
| 451 |
| -> { [], None } ] ] |
| 452 |
; |
| 453 |
open_binders: |
| 454 |
(* Same as binders but parentheses around a closed binder are optional if |
| 455 |
the latter is unique *) |
| 456 |
[ [ id = name; idl = LIST0 name; ":"; c = lconstr -> |
| 457 |
{ [CLocalAssum (id::idl,None,Default Explicit,c)] } |
| 458 |
(* binders factorized with open binder *) |
| 459 |
| id = name; idl = LIST0 name; bl = binders -> |
| 460 |
{ binders_of_names (id::idl) @ bl } |
| 461 |
| id1 = name; ".."; id2 = name -> |
| 462 |
{ [CLocalAssum ([id1;(CAst.make ~loc (Name ldots_var));id2], |
| 463 |
None, Default Explicit, CAst.make ~loc @@ CHole (None))] } |
| 464 |
| bl = closed_binder; bl' = binders -> |
| 465 |
{ bl@bl' } ] ] |
| 466 |
; |
| 467 |
binders: |
| 468 |
[ [ l = LIST0 binder -> { List.flatten l } ] ] |
| 469 |
; |
| 470 |
binder: |
| 471 |
[ [ id = name -> { [CLocalAssum ([id], None, Default Explicit, CAst.make ~loc @@ CHole (None))] } |
| 472 |
| bl = closed_binder -> { bl } ] ] |
| 473 |
; |
| 474 |
closed_binder: |
| 475 |
[ [ "("; id = name; idl = LIST1 name; ":"; c = lconstr; ")" -> |
| 476 |
{ [CLocalAssum (id::idl,None,Default Explicit,c)] } |
| 477 |
| "("; id = name; ":"; c = lconstr; ")" -> |
| 478 |
{ [CLocalAssum ([id],None,Default Explicit,c)] } |
| 479 |
| "("; id = name; ":="; c = lconstr; ")" -> |
| 480 |
{ match c.CAst.v with |
| 481 |
| CCast(c, Some DEFAULTcast, t) -> [CLocalDef (id,None,c,Some t)] |
| 482 |
| _ -> [CLocalDef (id,None,c,None)] } |
| 483 |
| "("; id = name; ":"; t = lconstr; ":="; c = lconstr; ")" -> |
| 484 |
{ [CLocalDef (id,None,c,Some t)] } |
| 485 |
| "("; id=name; ":"; t=lconstr; "|"; c=lconstr; ")" -> { |
| 486 |
let lambda = mkLambdaC ([id], default_binder_kind, t, c) in |
| 487 |
let typ = CAst.make ~loc @@ CAppExpl ((sigref loc,None), [CAst.make @@ CHole None; lambda]) in |
| 488 |
[CLocalAssum ([id], None, default_binder_kind, typ)] } |
| 489 |
| "{"; id = name; "}" -> |
| 490 |
{ [CLocalAssum ([id], None, Default MaxImplicit, CAst.make ~loc @@ CHole (None))] } |
| 491 |
| "{"; id = name; idl = LIST1 name; ":"; c = lconstr; "}" -> |
| 492 |
{ [CLocalAssum (id::idl,None,Default MaxImplicit,c)] } |
| 493 |
| "{"; id = name; ":"; c = lconstr; "}" -> |
| 494 |
{ [CLocalAssum ([id],None,Default MaxImplicit,c)] } |
| 495 |
| "{"; id = name; idl = LIST1 name; "}" -> |
| 496 |
{ List.map (fun id -> CLocalAssum ([id], None, Default MaxImplicit, CAst.make ~loc @@ CHole (None))) (id::idl) } |
| 497 |
| "["; id = name; "]" -> |
| 498 |
{ [CLocalAssum ([id], None, Default NonMaxImplicit, CAst.make ~loc @@ CHole (None))] } |
| 499 |
| "["; id = name; idl = LIST1 name; ":"; c = lconstr; "]" -> |
| 500 |
{ [CLocalAssum (id::idl,None,Default NonMaxImplicit,c)] } |
| 501 |
| "["; id = name; ":"; c = lconstr; "]" -> |
| 502 |
{ [CLocalAssum ([id],None, Default NonMaxImplicit,c)] } |
| 503 |
| "["; id = name; idl = LIST1 name; "]" -> |
| 504 |
{ List.map (fun id -> CLocalAssum ([id], None, Default NonMaxImplicit, CAst.make ~loc @@ CHole (None))) (id::idl) } |
| 505 |
| "`("; tc = LIST1 typeclass_constraint SEP "," ; ")" -> |
| 506 |
{ List.map (fun (n, b, t) -> CLocalAssum ([n], None, Generalized (Explicit, b), t)) tc } |
| 507 |
| "`{"; tc = LIST1 typeclass_constraint SEP "," ; "}" -> |
| 508 |
{ List.map (fun (n, b, t) -> CLocalAssum ([n], None, Generalized (MaxImplicit, b), t)) tc } |
| 509 |
| "`["; tc = LIST1 typeclass_constraint SEP "," ; "]" -> |
| 510 |
{ List.map (fun (n, b, t) -> CLocalAssum ([n], None, Generalized (NonMaxImplicit, b), t)) tc } |
| 511 |
| "'"; p = pattern LEVEL "0" -> { [CLocalPattern p] } ] ] |
| 512 |
; |
| 513 |
one_open_binder: |
| 514 |
[ [ na = name -> { (pat_of_name na, Explicit) } |
| 515 |
| na = name; ":"; t = lconstr -> { (CAst.make ~loc @@ CPatCast (pat_of_name na, t), Explicit) } |
| 516 |
| b = one_closed_binder -> { b } ] ] |
| 517 |
; |
| 518 |
one_closed_binder: |
| 519 |
[ [ "("; na = name; ":"; t = lconstr; ")" -> { (CAst.make ~loc @@ CPatCast (pat_of_name na, t), Explicit) } |
| 520 |
| "{"; na = name; "}" -> { (pat_of_name na, MaxImplicit) } |
| 521 |
| "{"; na = name; ":"; t = lconstr; "}" -> { (CAst.make ~loc @@ CPatCast (pat_of_name na, t), MaxImplicit) } |
| 522 |
| "["; na = name; "]" -> { (pat_of_name na, NonMaxImplicit) } |
| 523 |
| "["; na = name; ":"; t = lconstr; "]" -> { (CAst.make ~loc @@ CPatCast (pat_of_name na, t), NonMaxImplicit) } |
| 524 |
| "'"; p = pattern LEVEL "0" -> { (p, Explicit) } ] ] |
| 525 |
; |
| 526 |
typeclass_constraint: |
| 527 |
[ [ "!" ; c = term LEVEL "200" -> { (CAst.make ~loc Anonymous), true, c } |
| 528 |
| "{"; id = name; "}"; ":" ; expl = [ "!" -> { true } | -> { false } ] ; c = term LEVEL "200" -> |
| 529 |
{ id, expl, c } |
| 530 |
| test_name_colon; iid = name; ":" ; expl = [ "!" -> { true } | -> { false } ] ; c = term LEVEL "200" -> |
| 531 |
{ iid, expl, c } |
| 532 |
| c = term LEVEL "200" -> |
| 533 |
{ (CAst.make ~loc Anonymous), false, c } ] ] |
| 534 |
; |
| 535 |
type_cstr: |
| 536 |
[ [ ":"; c = lconstr -> { c } |
| 537 |
| -> { CAst.make ~loc @@ CHole (None) } ] ] |
| 538 |
; |
| 539 |
let_type_cstr: |
| 540 |
[ [ c = OPT [":"; c = lconstr -> { c } ] -> { Loc.tag ~loc c } ] ] |
| 541 |
; |
| 542 |
END |