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
100 prerr_endline (CicNotationPp.pp_term t);
104 `Symbol s -> [NoBinding, symbol s]
105 | `Keyword s -> [NoBinding, ident s]
106 | `Number s -> [NoBinding, number s]
109 Sub (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUB"] @ aux p2
110 | Sup (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUP"] @ aux p2
111 | Below (p1, p2) -> aux p1 @ [NoBinding, symbol "\\BELOW"] @ aux p2
112 | Above (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ABOVE"] @ aux p2
113 | Frac (p1, p2) -> aux p1 @ [NoBinding, symbol "\\FRAC"] @ aux p2
114 | Atop (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ATOP"] @ aux p2
115 | Over (p1, p2) -> aux p1 @ [NoBinding, symbol "\\OVER"] @ aux p2
117 [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"] @
119 | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
121 | Box (_, pl) -> List.flatten (List.map aux pl)
122 and aux_magic magic =
125 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
126 let action (env_opt : env_type option) (loc : location) =
128 Some env -> List.map opt_binding_some env
129 | None -> List.map opt_binding_of_name p_names
131 [Env (List.map opt_declaration p_names),
133 [[Gramext.Sopt (Gramext.srules [p_atoms, p_action])],
134 Gramext.action action]]
135 | List0 (p, _) | List1 (p, _) ->
136 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
137 let env0 = List.map list_binding_of_name p_names in
138 let grow_env_entry env n v =
141 n', (ty, ListValue vl) as entry ->
142 if n' = n then n', (ty, ListValue (v :: vl)) else entry
146 let grow_env env_i env =
147 List.fold_left (fun env (n, (_, v)) -> grow_env_entry env n v) env
150 let action (env_list : env_type list) (loc : location) =
151 List.fold_right grow_env env_list env0
155 List0 (_, None) -> Gramext.Slist0 s
156 | List1 (_, None) -> Gramext.Slist1 s
157 | List0 (_, Some l) -> Gramext.Slist0sep (s, g_symbol_of_literal l)
158 | List1 (_, Some l) -> Gramext.Slist1sep (s, g_symbol_of_literal l)
161 [Env (List.map list_declaration p_names),
163 [[g_symbol (Gramext.srules [p_atoms, p_action])],
164 Gramext.action action]]
168 NumVar s -> [Binding (s, NumType), number ""]
169 | TermVar s -> [Binding (s, TermType), term]
170 | IdentVar s -> [Binding (s, StringType), ident ""]
171 | Ascription (p, s) -> assert false
172 | FreshVar _ -> assert false
173 and inner_pattern p =
174 let (p_bindings, p_atoms) = List.split (aux p) in
175 let p_names = flatten_opt p_bindings in
178 (fun (env : env_type) (loc : location) -> env)
181 p_bindings, p_atoms, p_names, action
185 let level_of_int precedence =
186 if precedence < min_precedence || precedence > max_precedence then
187 raise (Level_not_found precedence);
188 string_of_int precedence
190 type rule_id = Token.t Gramext.g_symbol list
192 let extend level1_pattern ?(precedence = default_precedence) =
193 fun ?associativity action ->
194 let (p_bindings, p_atoms) =
195 List.split (extract_term_production level1_pattern)
197 let level = level_of_int precedence in
198 let p_names = flatten_opt p_bindings in
201 [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e),
202 Some (Gramext.Level level),
203 [None, associativity,
206 (fun (env : env_type) (loc : location) -> action env loc)
211 let delete atoms = Grammar.delete_rule l2_pattern atoms
218 | l -> Layout (Box (H, l))
220 let fold_binder binder pt_names body =
221 let fold_cluster binder terms ty body =
222 List.fold_right (fun term body -> Binder (binder, (term, ty), body)) terms
225 List.fold_right (fun (names, ty) body -> fold_cluster binder names ty body)
228 let return_term loc term = AttributedTerm (`Loc loc, term)
231 let mk_level_list first last =
234 i when i < first -> acc
235 | i -> aux ((Some (string_of_int i), None, []) :: acc) (i - 1)
240 [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e), None,
241 mk_level_list min_precedence max_precedence]
245 (let _ = (level1_pattern : 'level1_pattern Grammar.Entry.e)
246 and _ = (level2_pattern : 'level2_pattern Grammar.Entry.e)
247 and _ = (level3_term : 'level3_term Grammar.Entry.e)
248 and _ = (l2_pattern : 'l2_pattern Grammar.Entry.e)
249 and _ = (notation : 'notation Grammar.Entry.e)
250 and _ = (interpretation : 'interpretation Grammar.Entry.e)
251 and _ = (phrase : 'phrase Grammar.Entry.e) in
252 let grammar_entry_create s =
253 Grammar.Entry.create (Grammar.of_entry level1_pattern) s
255 let l1_pattern : 'l1_pattern Grammar.Entry.e =
256 grammar_entry_create "l1_pattern"
257 and literal : 'literal Grammar.Entry.e = grammar_entry_create "literal"
258 and sep : 'sep Grammar.Entry.e = grammar_entry_create "sep"
259 and l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e =
260 grammar_entry_create "l1_magic_pattern"
261 and l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e =
262 grammar_entry_create "l1_pattern_variable"
263 and l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e =
264 grammar_entry_create "l1_simple_pattern"
265 and sort : 'sort Grammar.Entry.e = grammar_entry_create "sort"
266 and explicit_subst : 'explicit_subst Grammar.Entry.e =
267 grammar_entry_create "explicit_subst"
268 and meta_subst : 'meta_subst Grammar.Entry.e =
269 grammar_entry_create "meta_subst"
270 and meta_substs : 'meta_substs Grammar.Entry.e =
271 grammar_entry_create "meta_substs"
272 and possibly_typed_name : 'possibly_typed_name Grammar.Entry.e =
273 grammar_entry_create "possibly_typed_name"
274 and match_pattern : 'match_pattern Grammar.Entry.e =
275 grammar_entry_create "match_pattern"
276 and binder : 'binder Grammar.Entry.e = grammar_entry_create "binder"
277 and bound_name : 'bound_name Grammar.Entry.e =
278 grammar_entry_create "bound_name"
279 and bound_names : 'bound_names Grammar.Entry.e =
280 grammar_entry_create "bound_names"
281 and induction_kind : 'induction_kind Grammar.Entry.e =
282 grammar_entry_create "induction_kind"
283 and let_defs : 'let_defs Grammar.Entry.e =
284 grammar_entry_create "let_defs"
285 and l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e =
286 grammar_entry_create "l2_pattern_variable"
287 and l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e =
288 grammar_entry_create "l2_magic_pattern"
289 and argument : 'argument Grammar.Entry.e =
290 grammar_entry_create "argument"
291 and associativity : 'associativity Grammar.Entry.e =
292 grammar_entry_create "associativity"
293 and precedence : 'precedence Grammar.Entry.e =
294 grammar_entry_create "precedence"
296 [Grammar.Entry.obj (level1_pattern : 'level1_pattern Grammar.Entry.e),
301 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
303 (fun (p : 'l1_simple_pattern)
304 (loc : Lexing.position * Lexing.position) ->
305 (p : 'level1_pattern))]];
306 Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e), None,
311 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e)))],
313 (fun (p : 'l1_simple_pattern list)
314 (loc : Lexing.position * Lexing.position) ->
315 (p : 'l1_pattern))]];
316 Grammar.Entry.obj (literal : 'literal Grammar.Entry.e), None,
318 [[Gramext.Stoken ("NUMBER", "")],
320 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
321 (`Number n : 'literal));
322 [Gramext.Stoken ("KEYWORD", "")],
324 (fun (k : string) (loc : Lexing.position * Lexing.position) ->
325 (`Keyword k : 'literal));
326 [Gramext.Stoken ("SYMBOL", "")],
328 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
329 (`Symbol s : 'literal))]];
330 Grammar.Entry.obj (sep : 'sep Grammar.Entry.e), None,
332 [[Gramext.Stoken ("SYMBOL", "\\SEP");
334 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
336 (fun (sep : 'literal) _ (loc : Lexing.position * Lexing.position) ->
339 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e),
342 [[Gramext.Stoken ("SYMBOL", "\\OPT");
345 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
347 (fun (p : 'l1_simple_pattern) _
348 (loc : Lexing.position * Lexing.position) ->
349 (Opt p : 'l1_magic_pattern));
350 [Gramext.Stoken ("SYMBOL", "\\LIST1");
353 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
355 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
357 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
358 (loc : Lexing.position * Lexing.position) ->
359 (List1 (p, sep) : 'l1_magic_pattern));
360 [Gramext.Stoken ("SYMBOL", "\\LIST0");
363 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
365 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
367 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
368 (loc : Lexing.position * Lexing.position) ->
369 (List0 (p, sep) : 'l1_magic_pattern))]];
371 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e),
374 [[Gramext.Stoken ("SYMBOL", "\\IDENT"); Gramext.Stoken ("IDENT", "")],
376 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
377 (IdentVar id : 'l1_pattern_variable));
378 [Gramext.Stoken ("SYMBOL", "\\NUM"); Gramext.Stoken ("IDENT", "")],
380 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
381 (NumVar id : 'l1_pattern_variable));
382 [Gramext.Stoken ("SYMBOL", "\\TERM"); Gramext.Stoken ("IDENT", "")],
384 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
385 (TermVar id : 'l1_pattern_variable))]];
387 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e),
389 [Some "layout", Some Gramext.LeftA,
390 [[Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\AS");
391 Gramext.Stoken ("IDENT", "")],
393 (fun (id : string) _ (p : 'l1_simple_pattern)
394 (loc : Lexing.position * Lexing.position) ->
395 (return_term loc (Variable (Ascription (p, id))) :
396 'l1_simple_pattern));
397 [Gramext.Stoken ("DELIM", "\\[");
399 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
400 Gramext.Stoken ("DELIM", "\\]")],
402 (fun _ (p : 'l1_pattern) _
403 (loc : Lexing.position * Lexing.position) ->
404 (return_term loc (boxify p) : 'l1_simple_pattern));
405 [Gramext.Stoken ("SYMBOL", "\\BREAK")],
407 (fun _ (loc : Lexing.position * Lexing.position) ->
408 (return_term loc (Layout Break) : 'l1_simple_pattern));
409 [Gramext.Stoken ("SYMBOL", "\\VBOX"); Gramext.Stoken ("DELIM", "\\[");
411 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
412 Gramext.Stoken ("DELIM", "\\]")],
414 (fun _ (p : 'l1_pattern) _ _
415 (loc : Lexing.position * Lexing.position) ->
416 (return_term loc (Layout (Box (V, p))) : 'l1_simple_pattern));
417 [Gramext.Stoken ("SYMBOL", "\\HBOX"); Gramext.Stoken ("DELIM", "\\[");
419 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
420 Gramext.Stoken ("DELIM", "\\]")],
422 (fun _ (p : 'l1_pattern) _ _
423 (loc : Lexing.position * Lexing.position) ->
424 (return_term loc (Layout (Box (H, p))) : 'l1_simple_pattern));
425 [Gramext.Stoken ("SYMBOL", "\\ROOT"); Gramext.Sself;
426 Gramext.Stoken ("SYMBOL", "\\OF"); Gramext.Sself],
428 (fun (arg : 'l1_simple_pattern) _ (index : 'l1_simple_pattern) _
429 (loc : Lexing.position * Lexing.position) ->
430 (return_term loc (Layout (Root (arg, index))) :
431 'l1_simple_pattern));
432 [Gramext.Stoken ("SYMBOL", "\\SQRT"); Gramext.Sself],
434 (fun (p : 'l1_simple_pattern) _
435 (loc : Lexing.position * Lexing.position) ->
436 (return_term loc (Layout (Sqrt p)) : 'l1_simple_pattern));
437 [Gramext.Stoken ("SYMBOL", "\\FRAC"); Gramext.Sself; Gramext.Sself],
439 (fun (p2 : 'l1_simple_pattern) (p1 : 'l1_simple_pattern) _
440 (loc : Lexing.position * Lexing.position) ->
441 (return_term loc (Layout (Frac (p1, p2))) : 'l1_simple_pattern));
442 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\ATOP"); Gramext.Sself],
444 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
445 (loc : Lexing.position * Lexing.position) ->
446 (return_term loc (Layout (Atop (p1, p2))) : 'l1_simple_pattern));
447 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\OVER"); Gramext.Sself],
449 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
450 (loc : Lexing.position * Lexing.position) ->
451 (return_term loc (Layout (Over (p1, p2))) : 'l1_simple_pattern));
452 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\ABOVE"); Gramext.Sself],
454 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
455 (loc : Lexing.position * Lexing.position) ->
456 (return_term loc (Layout (Above (p1, p2))) :
457 'l1_simple_pattern));
458 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\BELOW"); Gramext.Sself],
460 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
461 (loc : Lexing.position * Lexing.position) ->
462 (return_term loc (Layout (Below (p1, p2))) :
463 'l1_simple_pattern));
464 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\SUP"); Gramext.Sself],
466 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
467 (loc : Lexing.position * Lexing.position) ->
468 (return_term loc (Layout (Sup (p1, p2))) : 'l1_simple_pattern));
469 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\SUB"); Gramext.Sself],
471 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
472 (loc : Lexing.position * Lexing.position) ->
473 (return_term loc (Layout (Sub (p1, p2))) : 'l1_simple_pattern))];
474 Some "simple", Some Gramext.NonA,
476 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
478 (fun (l : 'literal) (loc : Lexing.position * Lexing.position) ->
479 (return_term loc (Literal l) : 'l1_simple_pattern));
482 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e))],
484 (fun (v : 'l1_pattern_variable)
485 (loc : Lexing.position * Lexing.position) ->
486 (return_term loc (Variable v) : 'l1_simple_pattern));
489 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e))],
491 (fun (m : 'l1_magic_pattern)
492 (loc : Lexing.position * Lexing.position) ->
493 (return_term loc (Magic m) : 'l1_simple_pattern));
494 [Gramext.Stoken ("IDENT", "")],
496 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
497 (return_term loc (Ident (i, None)) : 'l1_simple_pattern))]];
498 Grammar.Entry.obj (level2_pattern : 'level2_pattern Grammar.Entry.e),
502 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e))],
504 (fun (p : 'l2_pattern) (loc : Lexing.position * Lexing.position) ->
505 (p : 'level2_pattern))]];
506 Grammar.Entry.obj (sort : 'sort Grammar.Entry.e), None,
508 [[Gramext.Stoken ("SYMBOL", "\\TYPE")],
510 (fun _ (loc : Lexing.position * Lexing.position) ->
512 [Gramext.Stoken ("SYMBOL", "\\SET")],
514 (fun _ (loc : Lexing.position * Lexing.position) -> (`Set : 'sort));
515 [Gramext.Stoken ("SYMBOL", "\\PROP")],
517 (fun _ (loc : Lexing.position * Lexing.position) ->
519 Grammar.Entry.obj (explicit_subst : 'explicit_subst Grammar.Entry.e),
522 [[Gramext.Stoken ("SYMBOL", "\\subst"); Gramext.Stoken ("SYMBOL", "[");
525 [[Gramext.Stoken ("IDENT", "");
526 Gramext.Stoken ("SYMBOL", "≔");
529 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
531 (fun (t : 'l2_pattern) _ (i : string)
532 (loc : Lexing.position * Lexing.position) ->
534 Gramext.Stoken ("SYMBOL", ";"));
535 Gramext.Stoken ("SYMBOL", "]")],
537 (fun _ (substs : 'e__1 list) _ _
538 (loc : Lexing.position * Lexing.position) ->
539 (substs : 'explicit_subst))]];
540 Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e), None,
543 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e))],
545 (fun (p : 'l2_pattern) (loc : Lexing.position * Lexing.position) ->
546 (Some p : 'meta_subst));
547 [Gramext.Stoken ("SYMBOL", "_")],
549 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
550 (None : 'meta_subst))]];
551 Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e), None,
553 [[Gramext.Stoken ("SYMBOL", "[");
556 (Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e)));
557 Gramext.Stoken ("SYMBOL", "]")],
559 (fun _ (substs : 'meta_subst list) _
560 (loc : Lexing.position * Lexing.position) ->
561 (substs : 'meta_substs))]];
563 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e),
567 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e))],
569 (fun (id : 'bound_name) (loc : Lexing.position * Lexing.position) ->
570 (id, None : 'possibly_typed_name));
571 [Gramext.Stoken ("SYMBOL", "(");
573 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e));
574 Gramext.Stoken ("SYMBOL", ":");
576 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
577 Gramext.Stoken ("SYMBOL", ")")],
579 (fun _ (typ : 'l2_pattern) _ (id : 'bound_name) _
580 (loc : Lexing.position * Lexing.position) ->
581 (id, Some typ : 'possibly_typed_name))]];
582 Grammar.Entry.obj (match_pattern : 'match_pattern Grammar.Entry.e),
585 [[Gramext.Stoken ("SYMBOL", "("); Gramext.Stoken ("IDENT", "");
589 (possibly_typed_name :
590 'possibly_typed_name Grammar.Entry.e)));
591 Gramext.Stoken ("SYMBOL", ")")],
593 (fun _ (vars : 'possibly_typed_name list) (id : string) _
594 (loc : Lexing.position * Lexing.position) ->
595 (id, vars : 'match_pattern));
596 [Gramext.Stoken ("IDENT", "")],
598 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
599 (id, [] : 'match_pattern))]];
600 Grammar.Entry.obj (binder : 'binder Grammar.Entry.e), None,
602 [[Gramext.Stoken ("SYMBOL", "λ")],
604 (fun _ (loc : Lexing.position * Lexing.position) ->
605 (`Lambda : 'binder));
606 [Gramext.Stoken ("SYMBOL", "∀")],
608 (fun _ (loc : Lexing.position * Lexing.position) ->
609 (`Forall : 'binder));
610 [Gramext.Stoken ("SYMBOL", "∃")],
612 (fun _ (loc : Lexing.position * Lexing.position) ->
613 (`Exists : 'binder));
614 [Gramext.Stoken ("SYMBOL", "Π")],
616 (fun _ (loc : Lexing.position * Lexing.position) ->
618 Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e), None,
620 [[Gramext.Stoken ("SYMBOL", "\\FRESH"); Gramext.Stoken ("IDENT", "")],
622 (fun (i : string) _ (loc : Lexing.position * Lexing.position) ->
623 (Variable (FreshVar i) : 'bound_name));
624 [Gramext.Stoken ("IDENT", "")],
626 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
627 (Ident (i, None) : 'bound_name))]];
628 Grammar.Entry.obj (bound_names : 'bound_names Grammar.Entry.e), None,
632 [[Gramext.Stoken ("SYMBOL", "(");
636 (bound_name : 'bound_name Grammar.Entry.e)),
637 Gramext.Stoken ("SYMBOL", ","));
640 [[Gramext.Stoken ("SYMBOL", ":");
643 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
645 (fun (p : 'l2_pattern) _
646 (loc : Lexing.position * Lexing.position) ->
648 Gramext.Stoken ("SYMBOL", ")")],
650 (fun _ (ty : 'e__3 option) (vars : 'bound_name list) _
651 (loc : Lexing.position * Lexing.position) ->
652 (vars, ty : 'e__4))])],
654 (fun (clusters : 'e__4 list)
655 (loc : Lexing.position * Lexing.position) ->
656 (clusters : 'bound_names));
659 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e)),
660 Gramext.Stoken ("SYMBOL", ","));
663 [[Gramext.Stoken ("SYMBOL", ":");
666 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
668 (fun (p : 'l2_pattern) _
669 (loc : Lexing.position * Lexing.position) ->
672 (fun (ty : 'e__2 option) (vars : 'bound_name list)
673 (loc : Lexing.position * Lexing.position) ->
674 ([vars, ty] : 'bound_names))]];
675 Grammar.Entry.obj (induction_kind : 'induction_kind Grammar.Entry.e),
678 [[Gramext.Stoken ("IDENT", "corec")],
680 (fun _ (loc : Lexing.position * Lexing.position) ->
681 (`CoInductive : 'induction_kind));
682 [Gramext.Stoken ("IDENT", "rec")],
684 (fun _ (loc : Lexing.position * Lexing.position) ->
685 (`Inductive : 'induction_kind))]];
686 Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e), None,
692 (bound_name : 'bound_name Grammar.Entry.e));
695 (bound_names : 'bound_names Grammar.Entry.e));
698 [[Gramext.Stoken ("IDENT", "on");
701 (bound_name : 'bound_name Grammar.Entry.e))],
703 (fun (id : 'bound_name) _
704 (loc : Lexing.position * Lexing.position) ->
708 [[Gramext.Stoken ("SYMBOL", ":");
711 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
713 (fun (p : 'l2_pattern) _
714 (loc : Lexing.position * Lexing.position) ->
716 Gramext.Stoken ("SYMBOL", "≝");
719 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
721 (fun (body : 'l2_pattern) _ (ty : 'e__6 option)
722 (index_name : 'e__5 option) (args : 'bound_names)
724 (loc : Lexing.position * Lexing.position) ->
725 (let body = fold_binder `Lambda args body in
729 | Some ty -> Some (fold_binder `Pi args ty)
731 let rec position_of name p =
734 | n :: _ when n = name -> Some p, p
735 | _ :: tl -> position_of name (p + 1) tl
737 let rec find_arg name n =
741 (sprintf "Argument %s not found"
742 (CicNotationPp.pp_term name))
744 match position_of name 0 l with
745 None, len -> find_arg name (n + len) tl
746 | Some where, len -> n + where
749 match index_name with
751 | Some name -> find_arg name 0 args
753 (name, ty), body, index :
755 Gramext.Stoken ("IDENT", "and"))],
757 (fun (defs : 'e__7 list)
758 (loc : Lexing.position * Lexing.position) ->
759 (defs : 'let_defs))]];
761 (l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e),
764 [[Gramext.Stoken ("SYMBOL", "\\FRESH"); Gramext.Stoken ("IDENT", "")],
766 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
767 (FreshVar id : 'l2_pattern_variable));
768 [Gramext.Stoken ("SYMBOL", "\\IDENT"); Gramext.Stoken ("IDENT", "")],
770 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
771 (IdentVar id : 'l2_pattern_variable));
772 [Gramext.Stoken ("SYMBOL", "\\NUM"); Gramext.Stoken ("IDENT", "")],
774 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
775 (NumVar id : 'l2_pattern_variable))]];
777 (l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e),
780 [[Gramext.Stoken ("SYMBOL", "\\DEFAULT");
781 Gramext.Stoken ("DELIM", "\\[");
783 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
784 Gramext.Stoken ("DELIM", "\\]"); Gramext.Stoken ("DELIM", "\\[");
786 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
787 Gramext.Stoken ("DELIM", "\\]")],
789 (fun _ (none : 'l2_pattern) _ _ (some : 'l2_pattern) _ _
790 (loc : Lexing.position * Lexing.position) ->
791 (Default (some, none) : 'l2_magic_pattern));
792 [Gramext.Stoken ("SYMBOL", "\\FOLD");
794 [[Gramext.Stoken ("IDENT", "right")],
796 (fun _ (loc : Lexing.position * Lexing.position) ->
798 [Gramext.Stoken ("IDENT", "left")],
800 (fun _ (loc : Lexing.position * Lexing.position) ->
802 Gramext.Stoken ("DELIM", "\\[");
804 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
805 Gramext.Stoken ("DELIM", "\\]");
806 Gramext.Stoken ("SYMBOL", "\\LAMBDA"); Gramext.Stoken ("IDENT", "");
807 Gramext.Stoken ("DELIM", "\\[");
809 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
810 Gramext.Stoken ("DELIM", "\\]")],
812 (fun _ (recursive : 'l2_pattern) _ (id : string) _ _
813 (base : 'l2_pattern) _ (kind : 'e__8) _
814 (loc : Lexing.position * Lexing.position) ->
815 (Fold (kind, base, [id], recursive) : 'l2_magic_pattern))]];
816 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
817 Some (Gramext.Level "10"),
818 [Some "10", Some Gramext.NonA,
819 [[Gramext.Stoken ("IDENT", "let");
822 (induction_kind : 'induction_kind Grammar.Entry.e));
824 (Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e));
825 Gramext.Stoken ("IDENT", "in"); Gramext.Sself],
827 (fun (body : 'l2_pattern) _ (defs : 'let_defs) (k : 'induction_kind)
828 _ (loc : Lexing.position * Lexing.position) ->
829 (return_term loc (LetRec (k, defs, body)) : 'l2_pattern));
830 [Gramext.Stoken ("IDENT", "let");
833 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e));
834 Gramext.Stoken ("SYMBOL", "≝"); Gramext.Sself;
835 Gramext.Stoken ("", "in"); Gramext.Sself],
837 (fun (p2 : 'l2_pattern) _ (p1 : 'l2_pattern) _
838 (var : 'possibly_typed_name) _
839 (loc : Lexing.position * Lexing.position) ->
840 (return_term loc (LetIn (var, p1, p2)) : 'l2_pattern))]];
841 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
842 Some (Gramext.Level "20"),
843 [Some "20", Some Gramext.RightA,
845 (Grammar.Entry.obj (binder : 'binder Grammar.Entry.e));
847 (Grammar.Entry.obj (bound_names : 'bound_names Grammar.Entry.e));
848 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
850 (fun (body : 'l2_pattern) _ (names : 'bound_names) (b : 'binder)
851 (loc : Lexing.position * Lexing.position) ->
852 (return_term loc (fold_binder b names body) : 'l2_pattern))]];
853 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
854 Some (Gramext.Level "70"),
855 [Some "70", Some Gramext.LeftA,
856 [[Gramext.Sself; Gramext.Sself],
858 (fun (p2 : 'l2_pattern) (p1 : 'l2_pattern)
859 (loc : Lexing.position * Lexing.position) ->
862 Appl (hd :: tl) | AttributedTerm (_, Appl (hd :: tl)) ->
866 return_term loc (Appl (aux p1 @ [p2])) :
868 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
869 Some (Gramext.Level "90"),
870 [Some "90", Some Gramext.NonA,
873 (l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e))],
875 (fun (m : 'l2_magic_pattern)
876 (loc : Lexing.position * Lexing.position) ->
877 (return_term loc (Magic m) : 'l2_pattern));
880 (l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e))],
882 (fun (v : 'l2_pattern_variable)
883 (loc : Lexing.position * Lexing.position) ->
884 (return_term loc (Variable v) : 'l2_pattern));
885 [Gramext.Stoken ("SYMBOL", "("); Gramext.Sself;
886 Gramext.Stoken ("SYMBOL", ")")],
888 (fun _ (p : 'l2_pattern) _
889 (loc : Lexing.position * Lexing.position) ->
891 [Gramext.Stoken ("SYMBOL", "("); Gramext.Sself;
892 Gramext.Stoken ("SYMBOL", ":"); Gramext.Sself;
893 Gramext.Stoken ("SYMBOL", ")")],
895 (fun _ (p2 : 'l2_pattern) _ (p1 : 'l2_pattern) _
896 (loc : Lexing.position * Lexing.position) ->
897 (return_term loc (Appl [Symbol ("cast", 0); p1; p2]) :
901 [[Gramext.Stoken ("SYMBOL", "[");
904 (l2_pattern : 'l2_pattern Grammar.Entry.e));
905 Gramext.Stoken ("SYMBOL", "]")],
907 (fun _ (ty : 'l2_pattern) _
908 (loc : Lexing.position * Lexing.position) ->
910 Gramext.Stoken ("IDENT", "match"); Gramext.Sself;
913 [[Gramext.Stoken ("SYMBOL", ":"); Gramext.Stoken ("IDENT", "")],
916 (loc : Lexing.position * Lexing.position) ->
918 Gramext.Stoken ("IDENT", "with"); Gramext.Stoken ("SYMBOL", "[");
923 (match_pattern : 'match_pattern Grammar.Entry.e));
924 Gramext.Stoken ("SYMBOL", "⇒");
927 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
929 (fun (rhs : 'l2_pattern) _ (lhs : 'match_pattern)
930 (loc : Lexing.position * Lexing.position) ->
931 (lhs, rhs : 'e__11))],
932 Gramext.Stoken ("SYMBOL", "|"));
933 Gramext.Stoken ("SYMBOL", "]")],
935 (fun _ (patterns : 'e__11 list) _ _ (indty_ident : 'e__10 option)
936 (t : 'l2_pattern) _ (outtyp : 'e__9 option)
937 (loc : Lexing.position * Lexing.position) ->
938 (return_term loc (Case (t, indty_ident, outtyp, patterns)) :
940 [Gramext.Snterm (Grammar.Entry.obj (sort : 'sort Grammar.Entry.e))],
942 (fun (s : 'sort) (loc : Lexing.position * Lexing.position) ->
943 (return_term loc (Sort s) : 'l2_pattern));
944 [Gramext.Stoken ("META", "");
946 (Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e))],
948 (fun (s : 'meta_substs) (m : string)
949 (loc : Lexing.position * Lexing.position) ->
950 (return_term loc (Meta (int_of_string m, s)) : 'l2_pattern));
951 [Gramext.Stoken ("META", "")],
953 (fun (m : string) (loc : Lexing.position * Lexing.position) ->
954 (return_term loc (Meta (int_of_string m, [])) : 'l2_pattern));
955 [Gramext.Stoken ("IMPLICIT", "")],
957 (fun _ (loc : Lexing.position * Lexing.position) ->
958 (return_term loc Implicit : 'l2_pattern));
959 [Gramext.Stoken ("NUMBER", "")],
961 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
962 (return_term loc (Num (n, 0)) : 'l2_pattern));
963 [Gramext.Stoken ("URI", "")],
965 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
966 (return_term loc (Uri (u, None)) : 'l2_pattern));
967 [Gramext.Stoken ("IDENT", "");
970 (explicit_subst : 'explicit_subst Grammar.Entry.e))],
972 (fun (s : 'explicit_subst) (id : string)
973 (loc : Lexing.position * Lexing.position) ->
974 (return_term loc (Ident (id, Some s)) : 'l2_pattern));
975 [Gramext.Stoken ("IDENT", "")],
977 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
978 (return_term loc (Ident (id, None)) : 'l2_pattern))]];
979 Grammar.Entry.obj (argument : 'argument Grammar.Entry.e), None,
981 [[Gramext.Stoken ("SYMBOL", "η"); Gramext.Stoken ("IDENT", "");
982 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
984 (fun (a : 'argument) _ (id : string) _
985 (loc : Lexing.position * Lexing.position) ->
986 (EtaArg (Some id, a) : 'argument));
987 [Gramext.Stoken ("SYMBOL", "η"); Gramext.Stoken ("SYMBOL", ".");
990 (fun (a : 'argument) _ _
991 (loc : Lexing.position * Lexing.position) ->
992 (EtaArg (None, a) : 'argument));
993 [Gramext.Stoken ("IDENT", "")],
995 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
996 (IdentArg id : 'argument))]];
997 Grammar.Entry.obj (level3_term : 'level3_term Grammar.Entry.e), None,
999 [[Gramext.Stoken ("SYMBOL", "("); Gramext.Slist1 Gramext.Sself;
1000 Gramext.Stoken ("SYMBOL", ")")],
1002 (fun _ (terms : 'level3_term list) _
1003 (loc : Lexing.position * Lexing.position) ->
1007 | terms -> ApplPattern terms :
1010 (Grammar.Entry.obj (argument : 'argument Grammar.Entry.e))],
1012 (fun (a : 'argument) (loc : Lexing.position * Lexing.position) ->
1013 (ArgPattern a : 'level3_term));
1014 [Gramext.Stoken ("URI", "")],
1016 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
1017 (UriPattern u : 'level3_term))]];
1018 Grammar.Entry.obj (associativity : 'associativity Grammar.Entry.e),
1021 [[Gramext.Stoken ("IDENT", "non");
1022 Gramext.Stoken ("IDENT", "associative")],
1024 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1025 (Gramext.NonA : 'associativity));
1026 [Gramext.Stoken ("IDENT", "right");
1027 Gramext.Stoken ("IDENT", "associative")],
1029 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1030 (Gramext.RightA : 'associativity));
1031 [Gramext.Stoken ("IDENT", "left");
1032 Gramext.Stoken ("IDENT", "associative")],
1034 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1035 (Gramext.LeftA : 'associativity))]];
1036 Grammar.Entry.obj (precedence : 'precedence Grammar.Entry.e), None,
1038 [[Gramext.Stoken ("IDENT", "with");
1039 Gramext.Stoken ("IDENT", "precedence");
1040 Gramext.Stoken ("NUMBER", "")],
1042 (fun (n : string) _ _ (loc : Lexing.position * Lexing.position) ->
1043 (int_of_string n : 'precedence))]];
1044 Grammar.Entry.obj (notation : 'notation Grammar.Entry.e), None,
1046 [[Gramext.Stoken ("IDENT", "notation");
1049 (level1_pattern : 'level1_pattern Grammar.Entry.e));
1053 (associativity : 'associativity Grammar.Entry.e)));
1056 (Grammar.Entry.obj (precedence : 'precedence Grammar.Entry.e)));
1057 Gramext.Stoken ("IDENT", "for");
1060 (level2_pattern : 'level2_pattern Grammar.Entry.e))],
1062 (fun (p2 : 'level2_pattern) _ (prec : 'precedence option)
1063 (assoc : 'associativity option) (p1 : 'level1_pattern) _
1064 (loc : Lexing.position * Lexing.position) ->
1065 (p1, assoc, prec, p2 : 'notation))]];
1066 Grammar.Entry.obj (interpretation : 'interpretation Grammar.Entry.e),
1069 [[Gramext.Stoken ("IDENT", "interpretation");
1070 Gramext.Stoken ("SYMBOL", "");
1073 (Grammar.Entry.obj (argument : 'argument Grammar.Entry.e)));
1074 Gramext.Stoken ("IDENT", "as");
1076 (Grammar.Entry.obj (level3_term : 'level3_term Grammar.Entry.e))],
1078 (fun (t : 'level3_term) _ (args : 'argument list) (s : string) _
1079 (loc : Lexing.position * Lexing.position) ->
1080 (() : 'interpretation))]];
1081 Grammar.Entry.obj (phrase : 'phrase Grammar.Entry.e), None,
1084 (Grammar.Entry.obj (notation : 'notation Grammar.Entry.e));
1085 Gramext.Stoken ("SYMBOL", ".")],
1087 (fun _ (l1, assoc, prec, l2 : 'notation)
1088 (loc : Lexing.position * Lexing.position) ->
1089 (Notation (l1, assoc, prec, l2) : 'phrase));
1090 [Gramext.Stoken ("IDENT", "print");
1093 (level2_pattern : 'level2_pattern Grammar.Entry.e));
1094 Gramext.Stoken ("SYMBOL", ".")],
1096 (fun _ (p2 : 'level2_pattern) _
1097 (loc : Lexing.position * Lexing.position) ->
1098 (Print p2 : 'phrase))]]])
1100 (** {2 API implementation} *)
1102 let exc_located_wrapper f =
1104 Stdpp.Exc_located (floc, Stream.Error msg) ->
1105 raise (Parse_error (floc, msg))
1106 | Stdpp.Exc_located (floc, exn) ->
1107 raise (Parse_error (floc, Printexc.to_string exn))
1109 let parse_syntax_pattern stream =
1110 exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream)
1111 let parse_ast_pattern stream =
1112 exc_located_wrapper (fun () -> Grammar.Entry.parse level2_pattern stream)
1113 let parse_interpretation stream =
1114 exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream)
1115 let parse_phrase stream =
1116 exc_located_wrapper (fun () -> Grammar.Entry.parse phrase stream)
1118 (** {2 Debugging} *)
1120 let print_l2_pattern () =
1121 Grammar.print_entry Format.std_formatter (Grammar.Entry.obj l2_pattern);
1122 Format.pp_print_flush Format.std_formatter ();
1125 (* vim:set encoding=utf8 foldmethod=marker: *)