1 (* *)(* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
27 oopen CicNotationEnvoopen CicNotationPt
28 eexception Parse_error of Token.flocation * stringeexception Level_not_found of int
29 llet grammar = Grammar.gcreate CicNotationLexer.notation_lexer
30 llet min_precedence = 0llet max_precedence = 100llet default_precedence = 50
31 llet level1_pattern = Grammar.Entry.create grammar "level1_pattern"llet level2_pattern = Grammar.Entry.create grammar "level2_pattern"llet level3_term = Grammar.Entry.create grammar "level3_term"llet l2_pattern = Grammar.Entry.create grammar "l2_pattern"llet notation = Grammar.Entry.create grammar "notation"(* level1 <-> level 2 *)
32 llet interpretation = Grammar.Entry.create grammar "interpretation"(* level2 <-> level 3 *)
33 llet phrase = Grammar.Entry.create grammar "phrase"
34 llet return_term loc term = ()
36 let (x, y) = loc_of_floc floc in
37 failwith (sprintf "Error at characters %d - %d: %s" x y msg)
38 llet int_of_string s =
39 try Pervasives.int_of_string s with
41 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
42 (** {2 Grammar extension} *)
44 llet symbol s = Gramext.Stoken ("SYMBOL", s)llet ident s = Gramext.Stoken ("IDENT", s)llet number s = Gramext.Stoken ("NUMBER", s)llet term = Gramext.Sself
45 llet g_symbol_of_literal =
48 | `Keyword s -> ident s
49 | `Number s -> number s
52 | Binding of string * value_type
53 | Env of (string * value_type) list
54 llet make_action action bindings =
55 let rec aux (vl : env_type) =
57 [] -> Gramext.action (fun (loc : location) -> action vl loc)
58 | NoBinding :: tl -> Gramext.action (fun _ -> aux vl tl)
59 | Binding (name, TermType) :: tl ->
61 (fun (v : term) -> aux ((name, (TermType, TermValue v)) :: vl) tl)
62 | Binding (name, StringType) :: tl ->
65 aux ((name, (StringType, StringValue v)) :: vl) tl)
66 | Binding (name, NumType) :: tl ->
68 (fun (v : string) -> aux ((name, (NumType, NumValue v)) :: vl) tl)
69 | Binding (name, OptType t) :: tl ->
71 (fun (v : 'a option) ->
72 aux ((name, (OptType t, OptValue v)) :: vl) tl)
73 | Binding (name, ListType t) :: tl ->
76 aux ((name, (ListType t, ListValue v)) :: vl) tl)
77 | Env _ :: tl -> Gramext.action (fun (v : env_type) -> aux (v @ vl) tl)
79 aux [] (List.rev bindings)
84 | NoBinding :: tl -> aux acc tl
85 | Env names :: tl -> aux (List.rev names @ acc) tl
86 | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
90 (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
91 let extract_term_production pattern =
94 AttributedTerm (_, t) -> aux t
95 | Literal l -> aux_literal l
96 | Layout l -> aux_layout l
97 | Magic m -> aux_magic m
98 | Variable v -> aux_variable v
99 | t -> prerr_endline (CicNotationPp.pp_term t); assert false
102 `Symbol s -> [NoBinding, symbol s]
103 | `Keyword s -> [NoBinding, ident s]
104 | `Number s -> [NoBinding, number s]
107 Sub (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUB"] @ aux p2
108 | Sup (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUP"] @ aux p2
109 | Below (p1, p2) -> aux p1 @ [NoBinding, symbol "\\BELOW"] @ aux p2
110 | Above (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ABOVE"] @ aux p2
111 | Frac (p1, p2) -> aux p1 @ [NoBinding, symbol "\\FRAC"] @ aux p2
112 | Atop (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ATOP"] @ aux p2
113 | Over (p1, p2) -> aux p1 @ [NoBinding, symbol "\\OVER"] @ aux p2
115 [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"] @
117 | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
119 | Box (_, pl) -> List.flatten (List.map aux pl)
120 and aux_magic magic =
123 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
124 let action (env_opt : env_type option) (loc : location) =
126 Some env -> List.map opt_binding_some env
127 | None -> List.map opt_binding_of_name p_names
129 [Env (List.map opt_declaration p_names),
131 [[Gramext.Sopt (Gramext.srules [p_atoms, p_action])],
132 Gramext.action action]]
133 | List0 (p, _) | List1 (p, _) ->
134 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
135 let env0 = List.map list_binding_of_name p_names in
136 let grow_env_entry env n v =
139 n', (ty, ListValue vl) as entry ->
140 if n' = n then n', (ty, ListValue (v :: vl)) else entry
144 let grow_env env_i env =
145 List.fold_left (fun env (n, (_, v)) -> grow_env_entry env n v) env
148 let action (env_list : env_type list) (loc : location) =
149 List.fold_right grow_env env_list env0
153 List0 (_, None) -> Gramext.Slist0 s
154 | List1 (_, None) -> Gramext.Slist1 s
155 | List0 (_, Some l) -> Gramext.Slist0sep (s, g_symbol_of_literal l)
156 | List1 (_, Some l) -> Gramext.Slist1sep (s, g_symbol_of_literal l)
159 [Env (List.map list_declaration p_names),
161 [[g_symbol (Gramext.srules [p_atoms, p_action])],
162 Gramext.action action]]
166 NumVar s -> [Binding (s, NumType), number ""]
167 | TermVar s -> [Binding (s, TermType), term]
168 | IdentVar s -> [Binding (s, StringType), ident ""]
169 | Ascription (p, s) -> assert false
170 | FreshVar _ -> assert false
171 and inner_pattern p =
172 let (p_bindings, p_atoms) = List.split (aux p) in
173 let p_names = flatten_opt p_bindings in
176 (fun (env : env_type) (loc : location) -> env)
179 p_bindings, p_atoms, p_names, action
183 let level_of_int precedence =
184 if precedence < min_precedence || precedence > max_precedence then
185 raise (Level_not_found precedence);
186 string_of_int precedence
188 type rule_id = Token.t Gramext.g_symbol list
190 let extend level1_pattern ?(precedence = default_precedence) =
191 fun ?associativity action ->
192 let (p_bindings, p_atoms) =
193 List.split (extract_term_production level1_pattern)
195 let level = level_of_int precedence in
196 let p_names = flatten_opt p_bindings in
199 [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e),
200 Some (Gramext.Level level),
201 [None, associativity,
204 (fun (env : env_type) (loc : location) -> action env loc)
209 let delete atoms = Grammar.delete_rule l2_pattern atoms
216 | l -> Layout (Box (H, l))
218 let fold_binder binder pt_names body =
219 let fold_cluster binder terms ty body =
220 List.fold_right (fun term body -> Binder (binder, (term, ty), body)) terms
223 List.fold_right (fun (names, ty) body -> fold_cluster binder names ty body)
226 let return_term loc term = AttributedTerm (`Loc loc, term)
229 let mk_level_list first last =
232 i when i < first -> acc
233 | i -> aux ((Some (string_of_int i), None, []) :: acc) (i - 1)
238 [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e), None,
239 mk_level_list min_precedence max_precedence]
243 (let _ = (level1_pattern : 'level1_pattern Grammar.Entry.e)
244 and _ = (level2_pattern : 'level2_pattern Grammar.Entry.e)
245 and _ = (level3_term : 'level3_term Grammar.Entry.e)
246 and _ = (l2_pattern : 'l2_pattern Grammar.Entry.e)
247 and _ = (notation : 'notation Grammar.Entry.e)
248 and _ = (interpretation : 'interpretation Grammar.Entry.e)
249 and _ = (phrase : 'phrase Grammar.Entry.e) in
250 let grammar_entry_create s =
251 Grammar.Entry.create (Grammar.of_entry level1_pattern) s
253 let l1_pattern : 'l1_pattern Grammar.Entry.e =
254 grammar_entry_create "l1_pattern"
255 and literal : 'literal Grammar.Entry.e = grammar_entry_create "literal"
256 and sep : 'sep Grammar.Entry.e = grammar_entry_create "sep"
257 and l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e =
258 grammar_entry_create "l1_magic_pattern"
259 and l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e =
260 grammar_entry_create "l1_pattern_variable"
261 and l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e =
262 grammar_entry_create "l1_simple_pattern"
263 and sort : 'sort Grammar.Entry.e = grammar_entry_create "sort"
264 and explicit_subst : 'explicit_subst Grammar.Entry.e =
265 grammar_entry_create "explicit_subst"
266 and meta_subst : 'meta_subst Grammar.Entry.e =
267 grammar_entry_create "meta_subst"
268 and meta_substs : 'meta_substs Grammar.Entry.e =
269 grammar_entry_create "meta_substs"
270 and possibly_typed_name : 'possibly_typed_name Grammar.Entry.e =
271 grammar_entry_create "possibly_typed_name"
272 and match_pattern : 'match_pattern Grammar.Entry.e =
273 grammar_entry_create "match_pattern"
274 and binder : 'binder Grammar.Entry.e = grammar_entry_create "binder"
275 and bound_name : 'bound_name Grammar.Entry.e =
276 grammar_entry_create "bound_name"
277 and bound_names : 'bound_names Grammar.Entry.e =
278 grammar_entry_create "bound_names"
279 and induction_kind : 'induction_kind Grammar.Entry.e =
280 grammar_entry_create "induction_kind"
281 and let_defs : 'let_defs Grammar.Entry.e =
282 grammar_entry_create "let_defs"
283 and l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e =
284 grammar_entry_create "l2_pattern_variable"
285 and l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e =
286 grammar_entry_create "l2_magic_pattern"
287 and argument : 'argument Grammar.Entry.e =
288 grammar_entry_create "argument"
289 and associativity : 'associativity Grammar.Entry.e =
290 grammar_entry_create "associativity"
291 and precedence : 'precedence Grammar.Entry.e =
292 grammar_entry_create "precedence"
294 [Grammar.Entry.obj (level1_pattern : 'level1_pattern Grammar.Entry.e),
299 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
301 (fun (p : 'l1_simple_pattern)
302 (loc : Lexing.position * Lexing.position) ->
303 (p : 'level1_pattern))]];
304 Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e), None,
309 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e)))],
311 (fun (p : 'l1_simple_pattern list)
312 (loc : Lexing.position * Lexing.position) ->
313 (p : 'l1_pattern))]];
314 Grammar.Entry.obj (literal : 'literal Grammar.Entry.e), None,
316 [[Gramext.Stoken ("NUMBER", "")],
318 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
319 (`Number n : 'literal));
320 [Gramext.Stoken ("KEYWORD", "")],
322 (fun (k : string) (loc : Lexing.position * Lexing.position) ->
323 (`Keyword k : 'literal));
324 [Gramext.Stoken ("SYMBOL", "")],
326 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
327 (`Symbol s : 'literal))]];
328 Grammar.Entry.obj (sep : 'sep Grammar.Entry.e), None,
330 [[Gramext.Stoken ("SYMBOL", "\\SEP");
332 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
334 (fun (sep : 'literal) _ (loc : Lexing.position * Lexing.position) ->
337 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e),
340 [[Gramext.Stoken ("SYMBOL", "\\OPT");
343 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
345 (fun (p : 'l1_simple_pattern) _
346 (loc : Lexing.position * Lexing.position) ->
347 (Opt p : 'l1_magic_pattern));
348 [Gramext.Stoken ("SYMBOL", "\\LIST1");
351 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
353 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
355 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
356 (loc : Lexing.position * Lexing.position) ->
357 (List1 (p, sep) : 'l1_magic_pattern));
358 [Gramext.Stoken ("SYMBOL", "\\LIST0");
361 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
363 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
365 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
366 (loc : Lexing.position * Lexing.position) ->
367 (List0 (p, sep) : 'l1_magic_pattern))]];
369 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e),
372 [[Gramext.Stoken ("SYMBOL", "\\IDENT"); Gramext.Stoken ("IDENT", "")],
374 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
375 (IdentVar id : 'l1_pattern_variable));
376 [Gramext.Stoken ("SYMBOL", "\\NUM"); Gramext.Stoken ("IDENT", "")],
378 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
379 (NumVar id : 'l1_pattern_variable));
380 [Gramext.Stoken ("SYMBOL", "\\TERM"); Gramext.Stoken ("IDENT", "")],
382 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
383 (TermVar id : 'l1_pattern_variable))]];
385 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e),
387 [Some "layout", Some Gramext.LeftA,
388 [[Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\AS");
389 Gramext.Stoken ("IDENT", "")],
391 (fun (id : string) _ (p : 'l1_simple_pattern)
392 (loc : Lexing.position * Lexing.position) ->
393 (return_term loc (Variable (Ascription (p, id))) :
394 'l1_simple_pattern));
395 [Gramext.Stoken ("DELIM", "\\[");
397 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
398 Gramext.Stoken ("DELIM", "\\]")],
400 (fun _ (p : 'l1_pattern) _
401 (loc : Lexing.position * Lexing.position) ->
402 (return_term loc (boxify p) : 'l1_simple_pattern));
403 [Gramext.Stoken ("SYMBOL", "\\BREAK")],
405 (fun _ (loc : Lexing.position * Lexing.position) ->
406 (return_term loc (Layout Break) : 'l1_simple_pattern));
407 [Gramext.Stoken ("SYMBOL", "\\VBOX"); Gramext.Stoken ("DELIM", "\\[");
409 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
410 Gramext.Stoken ("DELIM", "\\]")],
412 (fun _ (p : 'l1_pattern) _ _
413 (loc : Lexing.position * Lexing.position) ->
414 (return_term loc (Layout (Box (V, p))) : 'l1_simple_pattern));
415 [Gramext.Stoken ("SYMBOL", "\\HBOX"); Gramext.Stoken ("DELIM", "\\[");
417 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
418 Gramext.Stoken ("DELIM", "\\]")],
420 (fun _ (p : 'l1_pattern) _ _
421 (loc : Lexing.position * Lexing.position) ->
422 (return_term loc (Layout (Box (H, p))) : 'l1_simple_pattern));
423 [Gramext.Stoken ("SYMBOL", "\\ROOT"); Gramext.Sself;
424 Gramext.Stoken ("SYMBOL", "\\OF"); Gramext.Sself],
426 (fun (arg : 'l1_simple_pattern) _ (index : 'l1_simple_pattern) _
427 (loc : Lexing.position * Lexing.position) ->
428 (return_term loc (Layout (Root (arg, index))) :
429 'l1_simple_pattern));
430 [Gramext.Stoken ("SYMBOL", "\\SQRT"); Gramext.Sself],
432 (fun (p : 'l1_simple_pattern) _
433 (loc : Lexing.position * Lexing.position) ->
434 (return_term loc (Layout (Sqrt p)) : 'l1_simple_pattern));
435 [Gramext.Stoken ("SYMBOL", "\\FRAC"); Gramext.Sself; Gramext.Sself],
437 (fun (p2 : 'l1_simple_pattern) (p1 : 'l1_simple_pattern) _
438 (loc : Lexing.position * Lexing.position) ->
439 (return_term loc (Layout (Frac (p1, p2))) : 'l1_simple_pattern));
440 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\ATOP"); Gramext.Sself],
442 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
443 (loc : Lexing.position * Lexing.position) ->
444 (return_term loc (Layout (Atop (p1, p2))) : 'l1_simple_pattern));
445 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\OVER"); Gramext.Sself],
447 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
448 (loc : Lexing.position * Lexing.position) ->
449 (return_term loc (Layout (Over (p1, p2))) : 'l1_simple_pattern));
450 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\ABOVE"); Gramext.Sself],
452 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
453 (loc : Lexing.position * Lexing.position) ->
454 (return_term loc (Layout (Above (p1, p2))) :
455 'l1_simple_pattern));
456 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\BELOW"); Gramext.Sself],
458 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
459 (loc : Lexing.position * Lexing.position) ->
460 (return_term loc (Layout (Below (p1, p2))) :
461 'l1_simple_pattern));
462 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\SUP"); Gramext.Sself],
464 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
465 (loc : Lexing.position * Lexing.position) ->
466 (return_term loc (Layout (Sup (p1, p2))) : 'l1_simple_pattern));
467 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\SUB"); Gramext.Sself],
469 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
470 (loc : Lexing.position * Lexing.position) ->
471 (return_term loc (Layout (Sub (p1, p2))) : 'l1_simple_pattern))];
472 Some "simple", Some Gramext.NonA,
474 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
476 (fun (l : 'literal) (loc : Lexing.position * Lexing.position) ->
477 (return_term loc (Literal l) : 'l1_simple_pattern));
480 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e))],
482 (fun (v : 'l1_pattern_variable)
483 (loc : Lexing.position * Lexing.position) ->
484 (return_term loc (Variable v) : 'l1_simple_pattern));
487 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e))],
489 (fun (m : 'l1_magic_pattern)
490 (loc : Lexing.position * Lexing.position) ->
491 (return_term loc (Magic m) : 'l1_simple_pattern));
492 [Gramext.Stoken ("IDENT", "")],
494 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
495 (return_term loc (Ident (i, None)) : 'l1_simple_pattern))]];
496 Grammar.Entry.obj (level2_pattern : 'level2_pattern Grammar.Entry.e),
500 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e))],
502 (fun (p : 'l2_pattern) (loc : Lexing.position * Lexing.position) ->
503 (p : 'level2_pattern))]];
504 Grammar.Entry.obj (sort : 'sort Grammar.Entry.e), None,
506 [[Gramext.Stoken ("SYMBOL", "\\TYPE")],
508 (fun _ (loc : Lexing.position * Lexing.position) ->
510 [Gramext.Stoken ("SYMBOL", "\\SET")],
512 (fun _ (loc : Lexing.position * Lexing.position) -> (`Set : 'sort));
513 [Gramext.Stoken ("SYMBOL", "\\PROP")],
515 (fun _ (loc : Lexing.position * Lexing.position) ->
517 Grammar.Entry.obj (explicit_subst : 'explicit_subst Grammar.Entry.e),
520 [[Gramext.Stoken ("SYMBOL", "\\subst"); Gramext.Stoken ("SYMBOL", "[");
523 [[Gramext.Stoken ("IDENT", "");
524 Gramext.Stoken ("SYMBOL", "≔");
527 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
529 (fun (t : 'l2_pattern) _ (i : string)
530 (loc : Lexing.position * Lexing.position) ->
532 Gramext.Stoken ("SYMBOL", ";"));
533 Gramext.Stoken ("SYMBOL", "]")],
535 (fun _ (substs : 'e__1 list) _ _
536 (loc : Lexing.position * Lexing.position) ->
537 (substs : 'explicit_subst))]];
538 Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e), None,
541 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e))],
543 (fun (p : 'l2_pattern) (loc : Lexing.position * Lexing.position) ->
544 (Some p : 'meta_subst));
545 [Gramext.Stoken ("SYMBOL", "_")],
547 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
548 (None : 'meta_subst))]];
549 Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e), None,
551 [[Gramext.Stoken ("SYMBOL", "[");
554 (Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e)));
555 Gramext.Stoken ("SYMBOL", "]")],
557 (fun _ (substs : 'meta_subst list) _
558 (loc : Lexing.position * Lexing.position) ->
559 (substs : 'meta_substs))]];
561 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e),
565 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e))],
567 (fun (id : 'bound_name) (loc : Lexing.position * Lexing.position) ->
568 (id, None : 'possibly_typed_name));
569 [Gramext.Stoken ("SYMBOL", "(");
571 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e));
572 Gramext.Stoken ("SYMBOL", ":");
574 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
575 Gramext.Stoken ("SYMBOL", ")")],
577 (fun _ (typ : 'l2_pattern) _ (id : 'bound_name) _
578 (loc : Lexing.position * Lexing.position) ->
579 (id, Some typ : 'possibly_typed_name))]];
580 Grammar.Entry.obj (match_pattern : 'match_pattern Grammar.Entry.e),
583 [[Gramext.Stoken ("SYMBOL", "("); Gramext.Stoken ("IDENT", "");
587 (possibly_typed_name :
588 'possibly_typed_name Grammar.Entry.e)));
589 Gramext.Stoken ("SYMBOL", ")")],
591 (fun _ (vars : 'possibly_typed_name list) (id : string) _
592 (loc : Lexing.position * Lexing.position) ->
593 (id, vars : 'match_pattern));
594 [Gramext.Stoken ("IDENT", "")],
596 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
597 (id, [] : 'match_pattern))]];
598 Grammar.Entry.obj (binder : 'binder Grammar.Entry.e), None,
600 [[Gramext.Stoken ("SYMBOL", "λ")],
602 (fun _ (loc : Lexing.position * Lexing.position) ->
603 (`Lambda : 'binder));
604 [Gramext.Stoken ("SYMBOL", "∀")],
606 (fun _ (loc : Lexing.position * Lexing.position) ->
607 (`Forall : 'binder));
608 [Gramext.Stoken ("SYMBOL", "∃")],
610 (fun _ (loc : Lexing.position * Lexing.position) ->
611 (`Exists : 'binder));
612 [Gramext.Stoken ("SYMBOL", "Π")],
614 (fun _ (loc : Lexing.position * Lexing.position) ->
616 Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e), None,
618 [[Gramext.Stoken ("SYMBOL", "\\FRESH"); Gramext.Stoken ("IDENT", "")],
620 (fun (i : string) _ (loc : Lexing.position * Lexing.position) ->
621 (Variable (FreshVar i) : 'bound_name));
622 [Gramext.Stoken ("IDENT", "")],
624 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
625 (Ident (i, None) : 'bound_name))]];
626 Grammar.Entry.obj (bound_names : 'bound_names Grammar.Entry.e), None,
630 [[Gramext.Stoken ("SYMBOL", "(");
634 (bound_name : 'bound_name Grammar.Entry.e)),
635 Gramext.Stoken ("SYMBOL", ","));
638 [[Gramext.Stoken ("SYMBOL", ":");
641 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
643 (fun (p : 'l2_pattern) _
644 (loc : Lexing.position * Lexing.position) ->
646 Gramext.Stoken ("SYMBOL", ")")],
648 (fun _ (ty : 'e__3 option) (vars : 'bound_name list) _
649 (loc : Lexing.position * Lexing.position) ->
650 (vars, ty : 'e__4))])],
652 (fun (clusters : 'e__4 list)
653 (loc : Lexing.position * Lexing.position) ->
654 (clusters : 'bound_names));
657 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e)),
658 Gramext.Stoken ("SYMBOL", ","));
661 [[Gramext.Stoken ("SYMBOL", ":");
664 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
666 (fun (p : 'l2_pattern) _
667 (loc : Lexing.position * Lexing.position) ->
670 (fun (ty : 'e__2 option) (vars : 'bound_name list)
671 (loc : Lexing.position * Lexing.position) ->
672 ([vars, ty] : 'bound_names))]];
673 Grammar.Entry.obj (induction_kind : 'induction_kind Grammar.Entry.e),
676 [[Gramext.Stoken ("IDENT", "corec")],
678 (fun _ (loc : Lexing.position * Lexing.position) ->
679 (`CoInductive : 'induction_kind));
680 [Gramext.Stoken ("IDENT", "rec")],
682 (fun _ (loc : Lexing.position * Lexing.position) ->
683 (`Inductive : 'induction_kind))]];
684 Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e), None,
690 (bound_name : 'bound_name Grammar.Entry.e));
693 (bound_names : 'bound_names Grammar.Entry.e));
696 [[Gramext.Stoken ("IDENT", "on");
699 (bound_name : 'bound_name Grammar.Entry.e))],
701 (fun (id : 'bound_name) _
702 (loc : Lexing.position * Lexing.position) ->
706 [[Gramext.Stoken ("SYMBOL", ":");
709 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
711 (fun (p : 'l2_pattern) _
712 (loc : Lexing.position * Lexing.position) ->
714 Gramext.Stoken ("SYMBOL", "≝");
717 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
719 (fun (body : 'l2_pattern) _ (ty : 'e__6 option)
720 (index_name : 'e__5 option) (args : 'bound_names)
722 (loc : Lexing.position * Lexing.position) ->
723 (let body = fold_binder `Lambda args body in
727 | Some ty -> Some (fold_binder `Pi args ty)
729 let rec position_of name p =
732 | n :: _ when n = name -> Some p, p
733 | _ :: tl -> position_of name (p + 1) tl
735 let rec find_arg name n =
739 (sprintf "Argument %s not found"
740 (CicNotationPp.pp_term name))
742 match position_of name 0 l with
743 None, len -> find_arg name (n + len) tl
744 | Some where, len -> n + where
747 match index_name with
749 | Some name -> find_arg name 0 args
751 (name, ty), body, index :
753 Gramext.Stoken ("IDENT", "and"))],
755 (fun (defs : 'e__7 list)
756 (loc : Lexing.position * Lexing.position) ->
757 (defs : 'let_defs))]];
759 (l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e),
762 [[Gramext.Stoken ("SYMBOL", "\\FRESH"); Gramext.Stoken ("IDENT", "")],
764 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
765 (FreshVar id : 'l2_pattern_variable));
766 [Gramext.Stoken ("SYMBOL", "\\IDENT"); Gramext.Stoken ("IDENT", "")],
768 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
769 (IdentVar id : 'l2_pattern_variable));
770 [Gramext.Stoken ("SYMBOL", "\\NUM"); Gramext.Stoken ("IDENT", "")],
772 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
773 (NumVar id : 'l2_pattern_variable))]];
775 (l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e),
778 [[Gramext.Stoken ("SYMBOL", "\\DEFAULT");
779 Gramext.Stoken ("DELIM", "\\[");
781 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
782 Gramext.Stoken ("DELIM", "\\]"); Gramext.Stoken ("DELIM", "\\[");
784 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
785 Gramext.Stoken ("DELIM", "\\]")],
787 (fun _ (none : 'l2_pattern) _ _ (some : 'l2_pattern) _ _
788 (loc : Lexing.position * Lexing.position) ->
789 (Default (some, none) : 'l2_magic_pattern));
790 [Gramext.Stoken ("SYMBOL", "\\FOLD");
792 [[Gramext.Stoken ("IDENT", "right")],
794 (fun _ (loc : Lexing.position * Lexing.position) ->
796 [Gramext.Stoken ("IDENT", "left")],
798 (fun _ (loc : Lexing.position * Lexing.position) ->
800 Gramext.Stoken ("DELIM", "\\[");
802 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
803 Gramext.Stoken ("DELIM", "\\]");
804 Gramext.Stoken ("SYMBOL", "\\LAMBDA"); Gramext.Stoken ("IDENT", "");
805 Gramext.Stoken ("DELIM", "\\[");
807 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
808 Gramext.Stoken ("DELIM", "\\]")],
810 (fun _ (recursive : 'l2_pattern) _ (id : string) _ _
811 (base : 'l2_pattern) _ (kind : 'e__8) _
812 (loc : Lexing.position * Lexing.position) ->
813 (Fold (kind, base, [id], recursive) : 'l2_magic_pattern))]];
814 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
815 Some (Gramext.Level "10"),
816 [Some "10", Some Gramext.NonA,
817 [[Gramext.Stoken ("IDENT", "let");
820 (induction_kind : 'induction_kind Grammar.Entry.e));
822 (Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e));
823 Gramext.Stoken ("IDENT", "in"); Gramext.Sself],
825 (fun (body : 'l2_pattern) _ (defs : 'let_defs) (k : 'induction_kind)
826 _ (loc : Lexing.position * Lexing.position) ->
827 (return_term loc (LetRec (k, defs, body)) : 'l2_pattern));
828 [Gramext.Stoken ("IDENT", "let");
831 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e));
832 Gramext.Stoken ("SYMBOL", "≝"); Gramext.Sself;
833 Gramext.Stoken ("", "in"); Gramext.Sself],
835 (fun (p2 : 'l2_pattern) _ (p1 : 'l2_pattern) _
836 (var : 'possibly_typed_name) _
837 (loc : Lexing.position * Lexing.position) ->
838 (return_term loc (LetIn (var, p1, p2)) : 'l2_pattern))]];
839 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
840 Some (Gramext.Level "20"),
841 [Some "20", Some Gramext.RightA,
843 (Grammar.Entry.obj (binder : 'binder Grammar.Entry.e));
845 (Grammar.Entry.obj (bound_names : 'bound_names Grammar.Entry.e));
846 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
848 (fun (body : 'l2_pattern) _ (names : 'bound_names) (b : 'binder)
849 (loc : Lexing.position * Lexing.position) ->
850 (return_term loc (fold_binder b names body) : 'l2_pattern))]];
851 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
852 Some (Gramext.Level "70"),
853 [Some "70", Some Gramext.LeftA,
854 [[Gramext.Sself; Gramext.Sself],
856 (fun (p2 : 'l2_pattern) (p1 : 'l2_pattern)
857 (loc : Lexing.position * Lexing.position) ->
860 Appl (hd :: tl) | AttributedTerm (_, Appl (hd :: tl)) ->
864 return_term loc (Appl (aux p1 @ [p2])) :
866 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
867 Some (Gramext.Level "90"),
868 [Some "90", Some Gramext.NonA,
871 (l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e))],
873 (fun (m : 'l2_magic_pattern)
874 (loc : Lexing.position * Lexing.position) ->
875 (return_term loc (Magic m) : 'l2_pattern));
878 (l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e))],
880 (fun (v : 'l2_pattern_variable)
881 (loc : Lexing.position * Lexing.position) ->
882 (return_term loc (Variable v) : 'l2_pattern));
883 [Gramext.Stoken ("SYMBOL", "("); Gramext.Sself;
884 Gramext.Stoken ("SYMBOL", ")")],
886 (fun _ (p : 'l2_pattern) _
887 (loc : Lexing.position * Lexing.position) ->
889 [Gramext.Stoken ("SYMBOL", "("); Gramext.Sself;
890 Gramext.Stoken ("SYMBOL", ":"); Gramext.Sself;
891 Gramext.Stoken ("SYMBOL", ")")],
893 (fun _ (p2 : 'l2_pattern) _ (p1 : 'l2_pattern) _
894 (loc : Lexing.position * Lexing.position) ->
895 (return_term loc (Appl [Symbol ("cast", 0); p1; p2]) :
899 [[Gramext.Stoken ("SYMBOL", "[");
902 (l2_pattern : 'l2_pattern Grammar.Entry.e));
903 Gramext.Stoken ("SYMBOL", "]")],
905 (fun _ (ty : 'l2_pattern) _
906 (loc : Lexing.position * Lexing.position) ->
908 Gramext.Stoken ("IDENT", "match"); Gramext.Sself;
911 [[Gramext.Stoken ("SYMBOL", ":"); Gramext.Stoken ("IDENT", "")],
914 (loc : Lexing.position * Lexing.position) ->
916 Gramext.Stoken ("IDENT", "with"); Gramext.Stoken ("SYMBOL", "[");
921 (match_pattern : 'match_pattern Grammar.Entry.e));
922 Gramext.Stoken ("SYMBOL", "⇒");
925 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
927 (fun (rhs : 'l2_pattern) _ (lhs : 'match_pattern)
928 (loc : Lexing.position * Lexing.position) ->
929 (lhs, rhs : 'e__11))],
930 Gramext.Stoken ("SYMBOL", "|"));
931 Gramext.Stoken ("SYMBOL", "]")],
933 (fun _ (patterns : 'e__11 list) _ _ (indty_ident : 'e__10 option)
934 (t : 'l2_pattern) _ (outtyp : 'e__9 option)
935 (loc : Lexing.position * Lexing.position) ->
936 (return_term loc (Case (t, indty_ident, outtyp, patterns)) :
938 [Gramext.Snterm (Grammar.Entry.obj (sort : 'sort Grammar.Entry.e))],
940 (fun (s : 'sort) (loc : Lexing.position * Lexing.position) ->
941 (return_term loc (Sort s) : 'l2_pattern));
942 [Gramext.Stoken ("META", "");
944 (Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e))],
946 (fun (s : 'meta_substs) (m : string)
947 (loc : Lexing.position * Lexing.position) ->
948 (return_term loc (Meta (int_of_string m, s)) : 'l2_pattern));
949 [Gramext.Stoken ("META", "")],
951 (fun (m : string) (loc : Lexing.position * Lexing.position) ->
952 (return_term loc (Meta (int_of_string m, [])) : 'l2_pattern));
953 [Gramext.Stoken ("IMPLICIT", "")],
955 (fun _ (loc : Lexing.position * Lexing.position) ->
956 (return_term loc Implicit : 'l2_pattern));
957 [Gramext.Stoken ("NUMBER", "")],
959 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
960 (return_term loc (Num (n, 0)) : 'l2_pattern));
961 [Gramext.Stoken ("URI", "")],
963 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
964 (return_term loc (Uri (u, None)) : 'l2_pattern));
965 [Gramext.Stoken ("IDENT", "");
968 (explicit_subst : 'explicit_subst Grammar.Entry.e))],
970 (fun (s : 'explicit_subst) (id : string)
971 (loc : Lexing.position * Lexing.position) ->
972 (return_term loc (Ident (id, Some s)) : 'l2_pattern));
973 [Gramext.Stoken ("IDENT", "")],
975 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
976 (return_term loc (Ident (id, None)) : 'l2_pattern))]];
977 Grammar.Entry.obj (argument : 'argument Grammar.Entry.e), None,
979 [[Gramext.Stoken ("SYMBOL", "η"); Gramext.Stoken ("IDENT", "");
980 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
982 (fun (a : 'argument) _ (id : string) _
983 (loc : Lexing.position * Lexing.position) ->
984 (EtaArg (Some id, a) : 'argument));
985 [Gramext.Stoken ("SYMBOL", "η"); Gramext.Stoken ("SYMBOL", ".");
988 (fun (a : 'argument) _ _
989 (loc : Lexing.position * Lexing.position) ->
990 (EtaArg (None, a) : 'argument));
991 [Gramext.Stoken ("IDENT", "")],
993 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
994 (IdentArg id : 'argument))]];
995 Grammar.Entry.obj (level3_term : 'level3_term Grammar.Entry.e), None,
997 [[Gramext.Stoken ("SYMBOL", "("); Gramext.Slist1 Gramext.Sself;
998 Gramext.Stoken ("SYMBOL", ")")],
1000 (fun _ (terms : 'level3_term list) _
1001 (loc : Lexing.position * Lexing.position) ->
1005 | terms -> ApplPattern terms :
1008 (Grammar.Entry.obj (argument : 'argument Grammar.Entry.e))],
1010 (fun (a : 'argument) (loc : Lexing.position * Lexing.position) ->
1011 (ArgPattern a : 'level3_term));
1012 [Gramext.Stoken ("URI", "")],
1014 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
1015 (UriPattern u : 'level3_term))]];
1016 Grammar.Entry.obj (associativity : 'associativity Grammar.Entry.e),
1019 [[Gramext.Stoken ("IDENT", "non");
1020 Gramext.Stoken ("IDENT", "associative")],
1022 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1023 (Gramext.NonA : 'associativity));
1024 [Gramext.Stoken ("IDENT", "right");
1025 Gramext.Stoken ("IDENT", "associative")],
1027 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1028 (Gramext.RightA : 'associativity));
1029 [Gramext.Stoken ("IDENT", "left");
1030 Gramext.Stoken ("IDENT", "associative")],
1032 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1033 (Gramext.LeftA : 'associativity))]];
1034 Grammar.Entry.obj (precedence : 'precedence Grammar.Entry.e), None,
1036 [[Gramext.Stoken ("IDENT", "with");
1037 Gramext.Stoken ("IDENT", "precedence");
1038 Gramext.Stoken ("NUMBER", "")],
1040 (fun (n : string) _ _ (loc : Lexing.position * Lexing.position) ->
1041 (int_of_string n : 'precedence))]];
1042 Grammar.Entry.obj (notation : 'notation Grammar.Entry.e), None,
1044 [[Gramext.Stoken ("IDENT", "notation");
1047 (level1_pattern : 'level1_pattern Grammar.Entry.e));
1051 (associativity : 'associativity Grammar.Entry.e)));
1054 (Grammar.Entry.obj (precedence : 'precedence Grammar.Entry.e)));
1055 Gramext.Stoken ("IDENT", "for");
1058 (level2_pattern : 'level2_pattern Grammar.Entry.e))],
1060 (fun (p2 : 'level2_pattern) _ (prec : 'precedence option)
1061 (assoc : 'associativity option) (p1 : 'level1_pattern) _
1062 (loc : Lexing.position * Lexing.position) ->
1063 (p1, assoc, prec, p2 : 'notation))]];
1064 Grammar.Entry.obj (interpretation : 'interpretation Grammar.Entry.e),
1067 [[Gramext.Stoken ("IDENT", "interpretation");
1068 Gramext.Stoken ("SYMBOL", "");
1071 (Grammar.Entry.obj (argument : 'argument Grammar.Entry.e)));
1072 Gramext.Stoken ("IDENT", "as");
1074 (Grammar.Entry.obj (level3_term : 'level3_term Grammar.Entry.e))],
1076 (fun (t : 'level3_term) _ (args : 'argument list) (s : string) _
1077 (loc : Lexing.position * Lexing.position) ->
1078 (() : 'interpretation))]];
1079 Grammar.Entry.obj (phrase : 'phrase Grammar.Entry.e), None,
1082 (Grammar.Entry.obj (notation : 'notation Grammar.Entry.e));
1083 Gramext.Stoken ("SYMBOL", ".")],
1085 (fun _ (l1, assoc, prec, l2 : 'notation)
1086 (loc : Lexing.position * Lexing.position) ->
1087 (Notation (l1, assoc, prec, l2) : 'phrase));
1088 [Gramext.Stoken ("IDENT", "print");
1091 (level2_pattern : 'level2_pattern Grammar.Entry.e));
1092 Gramext.Stoken ("SYMBOL", ".")],
1094 (fun _ (p2 : 'level2_pattern) _
1095 (loc : Lexing.position * Lexing.position) ->
1096 (Print p2 : 'phrase))]]])
1098 (** {2 API implementation} *)
1100 let exc_located_wrapper f =
1102 Stdpp.Exc_located (floc, Stream.Error msg) ->
1103 raise (Parse_error (floc, msg))
1104 | Stdpp.Exc_located (floc, exn) ->
1105 raise (Parse_error (floc, Printexc.to_string exn))
1107 let parse_syntax_pattern stream =
1108 exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream)
1109 let parse_ast_pattern stream =
1110 exc_located_wrapper (fun () -> Grammar.Entry.parse level2_pattern stream)
1111 let parse_interpretation stream =
1112 exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream)
1113 let parse_phrase stream =
1114 exc_located_wrapper (fun () -> Grammar.Entry.parse phrase stream)
1116 (** {2 Debugging} *)
1118 let print_l2_pattern () =
1119 Grammar.print_entry Format.std_formatter (Grammar.Entry.obj l2_pattern);
1120 Format.pp_print_flush Format.std_formatter ();
1123 (* vim:set encoding=utf8 foldmethod=marker: *)