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) =
58 prerr_endline "aux: make_action";
59 Gramext.action (fun (loc : location) -> action vl loc)
61 prerr_endline "aux: none"; Gramext.action (fun _ -> aux vl tl)
62 | Binding (name, TermType) :: tl ->
63 prerr_endline "aux: term";
65 (fun (v : term) -> aux ((name, (TermType, TermValue v)) :: vl) tl)
66 | Binding (name, StringType) :: tl ->
67 prerr_endline "aux: string";
70 aux ((name, (StringType, StringValue v)) :: vl) tl)
71 | Binding (name, NumType) :: tl ->
72 prerr_endline "aux: num";
74 (fun (v : string) -> aux ((name, (NumType, NumValue v)) :: vl) tl)
75 | Binding (name, OptType t) :: tl ->
76 prerr_endline "aux: opt";
78 (fun (v : 'a option) ->
79 aux ((name, (OptType t, OptValue v)) :: vl) tl)
80 | Binding (name, ListType t) :: tl ->
81 prerr_endline "aux: list";
84 aux ((name, (ListType t, ListValue v)) :: vl) tl)
86 prerr_endline "aux: env";
87 Gramext.action (fun (v : env_type) -> aux (v @ vl) tl)
89 aux [] (List.rev bindings)
94 | NoBinding :: tl -> aux acc tl
95 | Env names :: tl -> aux (List.rev names @ acc) tl
96 | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
100 (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
101 let extract_term_production pattern =
104 AttributedTerm (_, t) -> aux t
105 | Literal l -> aux_literal l
106 | Layout l -> aux_layout l
107 | Magic m -> aux_magic m
108 | Variable v -> aux_variable v
109 | t -> prerr_endline (CicNotationPp.pp_term t); assert false
112 `Symbol s -> [NoBinding, symbol s]
113 | `Keyword s -> [NoBinding, ident s]
114 | `Number s -> [NoBinding, number s]
117 Sub (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUB"] @ aux p2
118 | Sup (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUP"] @ aux p2
119 | Below (p1, p2) -> aux p1 @ [NoBinding, symbol "\\BELOW"] @ aux p2
120 | Above (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ABOVE"] @ aux p2
121 | Frac (p1, p2) -> aux p1 @ [NoBinding, symbol "\\FRAC"] @ aux p2
122 | Atop (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ATOP"] @ aux p2
123 | Over (p1, p2) -> aux p1 @ [NoBinding, symbol "\\OVER"] @ aux p2
125 [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"] @
127 | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
129 | Box (_, pl) -> List.flatten (List.map aux pl)
130 and aux_magic magic =
133 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
134 let action (env_opt : env_type option) (loc : location) =
136 Some env -> List.map opt_binding_some env
137 | None -> List.map opt_binding_of_name p_names
139 [Env (List.map opt_declaration p_names),
141 [[Gramext.Sopt (Gramext.srules [p_atoms, p_action])],
142 Gramext.action action]]
143 | List0 (p, _) | List1 (p, _) ->
144 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
145 let env0 = List.map list_binding_of_name p_names in
146 let grow_env_entry env n v =
147 prerr_endline "grow_env_entry";
150 n', (ty, ListValue vl) as entry ->
151 if n' = n then n', (ty, ListValue (v :: vl)) else entry
155 let grow_env env_i env =
156 prerr_endline "grow_env";
157 List.fold_left (fun env (n, (_, v)) -> grow_env_entry env n v) env
160 let action (env_list : env_type list) (loc : location) =
161 prerr_endline "list action"; List.fold_right grow_env env_list env0
165 List0 (_, None) -> Gramext.Slist0 s
166 | List1 (_, None) -> Gramext.Slist1 s
167 | List0 (_, Some l) -> Gramext.Slist0sep (s, g_symbol_of_literal l)
168 | List1 (_, Some l) -> Gramext.Slist1sep (s, g_symbol_of_literal l)
171 [Env (List.map list_declaration p_names),
173 [[g_symbol (Gramext.srules [p_atoms, p_action])],
174 Gramext.action action]]
178 NumVar s -> [Binding (s, NumType), number ""]
179 | TermVar s -> [Binding (s, TermType), term]
180 | IdentVar s -> [Binding (s, StringType), ident ""]
181 | Ascription (p, s) -> assert false
182 | FreshVar _ -> assert false
183 and inner_pattern p =
184 let (p_bindings, p_atoms) = List.split (aux p) in
185 let p_names = flatten_opt p_bindings in
188 ("inner names: " ^ String.concat " " (List.map fst p_names))
192 (fun (env : env_type) (loc : location) ->
193 prerr_endline "inner action"; env)
196 p_bindings, p_atoms, p_names, action
200 let level_of_int precedence =
201 if precedence < min_precedence || precedence > max_precedence then
202 raise (Level_not_found precedence);
203 string_of_int precedence
205 type rule_id = Token.t Gramext.g_symbol list
207 let extend level1_pattern ?(precedence = default_precedence) =
208 fun ?associativity action ->
209 let (p_bindings, p_atoms) =
210 List.split (extract_term_production level1_pattern)
212 let level = level_of_int precedence in
213 let p_names = flatten_opt p_bindings in
215 prerr_endline (string_of_int (List.length p_bindings));
217 [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e),
218 Some (Gramext.Level level),
219 [None, associativity,
222 (fun (env : env_type) (loc : location) -> action env loc)
227 let delete atoms = Grammar.delete_rule l2_pattern atoms
234 | l -> Layout (Box (H, l))
236 let fold_binder binder pt_names body =
237 let fold_cluster binder terms ty body =
238 List.fold_right (fun term body -> Binder (binder, (term, ty), body)) terms
241 List.fold_right (fun (names, ty) body -> fold_cluster binder names ty body)
244 let return_term loc term = AttributedTerm (`Loc loc, term)
247 let mk_level_list first last =
250 i when i < first -> acc
251 | i -> aux ((Some (string_of_int i), None, []) :: acc) (i - 1)
256 [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e), None,
257 mk_level_list min_precedence max_precedence]
261 (let _ = (level1_pattern : 'level1_pattern Grammar.Entry.e)
262 and _ = (level2_pattern : 'level2_pattern Grammar.Entry.e)
263 and _ = (level3_term : 'level3_term Grammar.Entry.e)
264 and _ = (l2_pattern : 'l2_pattern Grammar.Entry.e)
265 and _ = (notation : 'notation Grammar.Entry.e)
266 and _ = (interpretation : 'interpretation Grammar.Entry.e)
267 and _ = (phrase : 'phrase Grammar.Entry.e) in
268 let grammar_entry_create s =
269 Grammar.Entry.create (Grammar.of_entry level1_pattern) s
271 let l1_pattern : 'l1_pattern Grammar.Entry.e =
272 grammar_entry_create "l1_pattern"
273 and literal : 'literal Grammar.Entry.e = grammar_entry_create "literal"
274 and sep : 'sep Grammar.Entry.e = grammar_entry_create "sep"
275 and l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e =
276 grammar_entry_create "l1_magic_pattern"
277 and l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e =
278 grammar_entry_create "l1_pattern_variable"
279 and l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e =
280 grammar_entry_create "l1_simple_pattern"
281 and sort : 'sort Grammar.Entry.e = grammar_entry_create "sort"
282 and explicit_subst : 'explicit_subst Grammar.Entry.e =
283 grammar_entry_create "explicit_subst"
284 and meta_subst : 'meta_subst Grammar.Entry.e =
285 grammar_entry_create "meta_subst"
286 and meta_substs : 'meta_substs Grammar.Entry.e =
287 grammar_entry_create "meta_substs"
288 and possibly_typed_name : 'possibly_typed_name Grammar.Entry.e =
289 grammar_entry_create "possibly_typed_name"
290 and match_pattern : 'match_pattern Grammar.Entry.e =
291 grammar_entry_create "match_pattern"
292 and binder : 'binder Grammar.Entry.e = grammar_entry_create "binder"
293 and bound_name : 'bound_name Grammar.Entry.e =
294 grammar_entry_create "bound_name"
295 and bound_names : 'bound_names Grammar.Entry.e =
296 grammar_entry_create "bound_names"
297 and induction_kind : 'induction_kind Grammar.Entry.e =
298 grammar_entry_create "induction_kind"
299 and let_defs : 'let_defs Grammar.Entry.e =
300 grammar_entry_create "let_defs"
301 and l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e =
302 grammar_entry_create "l2_pattern_variable"
303 and l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e =
304 grammar_entry_create "l2_magic_pattern"
305 and argument : 'argument Grammar.Entry.e =
306 grammar_entry_create "argument"
307 and associativity : 'associativity Grammar.Entry.e =
308 grammar_entry_create "associativity"
309 and precedence : 'precedence Grammar.Entry.e =
310 grammar_entry_create "precedence"
312 [Grammar.Entry.obj (level1_pattern : 'level1_pattern Grammar.Entry.e),
317 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
319 (fun (p : 'l1_simple_pattern)
320 (loc : Lexing.position * Lexing.position) ->
321 (p : 'level1_pattern))]];
322 Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e), None,
327 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e)))],
329 (fun (p : 'l1_simple_pattern list)
330 (loc : Lexing.position * Lexing.position) ->
331 (p : 'l1_pattern))]];
332 Grammar.Entry.obj (literal : 'literal Grammar.Entry.e), None,
334 [[Gramext.Stoken ("NUMBER", "")],
336 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
337 (`Number n : 'literal));
338 [Gramext.Stoken ("KEYWORD", "")],
340 (fun (k : string) (loc : Lexing.position * Lexing.position) ->
341 (`Keyword k : 'literal));
342 [Gramext.Stoken ("SYMBOL", "")],
344 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
345 (`Symbol s : 'literal))]];
346 Grammar.Entry.obj (sep : 'sep Grammar.Entry.e), None,
348 [[Gramext.Stoken ("SYMBOL", "\\SEP");
350 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
352 (fun (sep : 'literal) _ (loc : Lexing.position * Lexing.position) ->
355 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e),
358 [[Gramext.Stoken ("SYMBOL", "\\OPT");
361 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
363 (fun (p : 'l1_simple_pattern) _
364 (loc : Lexing.position * Lexing.position) ->
365 (Opt p : 'l1_magic_pattern));
366 [Gramext.Stoken ("SYMBOL", "\\LIST1");
369 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
371 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
373 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
374 (loc : Lexing.position * Lexing.position) ->
375 (List1 (p, sep) : 'l1_magic_pattern));
376 [Gramext.Stoken ("SYMBOL", "\\LIST0");
379 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
381 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
383 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
384 (loc : Lexing.position * Lexing.position) ->
385 (List0 (p, sep) : 'l1_magic_pattern))]];
387 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e),
390 [[Gramext.Stoken ("SYMBOL", "\\IDENT"); Gramext.Stoken ("IDENT", "")],
392 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
393 (IdentVar id : 'l1_pattern_variable));
394 [Gramext.Stoken ("SYMBOL", "\\NUM"); Gramext.Stoken ("IDENT", "")],
396 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
397 (NumVar id : 'l1_pattern_variable));
398 [Gramext.Stoken ("SYMBOL", "\\TERM"); Gramext.Stoken ("IDENT", "")],
400 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
401 (TermVar id : 'l1_pattern_variable))]];
403 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e),
405 [Some "layout", Some Gramext.LeftA,
406 [[Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\AS");
407 Gramext.Stoken ("IDENT", "")],
409 (fun (id : string) _ (p : 'l1_simple_pattern)
410 (loc : Lexing.position * Lexing.position) ->
411 (return_term loc (Variable (Ascription (p, id))) :
412 'l1_simple_pattern));
413 [Gramext.Stoken ("DELIM", "\\[");
415 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
416 Gramext.Stoken ("DELIM", "\\]")],
418 (fun _ (p : 'l1_pattern) _
419 (loc : Lexing.position * Lexing.position) ->
420 (return_term loc (boxify p) : 'l1_simple_pattern));
421 [Gramext.Stoken ("SYMBOL", "\\BREAK")],
423 (fun _ (loc : Lexing.position * Lexing.position) ->
424 (return_term loc (Layout Break) : 'l1_simple_pattern));
425 [Gramext.Stoken ("SYMBOL", "\\VBOX"); Gramext.Stoken ("DELIM", "\\[");
427 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
428 Gramext.Stoken ("DELIM", "\\]")],
430 (fun _ (p : 'l1_pattern) _ _
431 (loc : Lexing.position * Lexing.position) ->
432 (return_term loc (Layout (Box (V, p))) : 'l1_simple_pattern));
433 [Gramext.Stoken ("SYMBOL", "\\HBOX"); Gramext.Stoken ("DELIM", "\\[");
435 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
436 Gramext.Stoken ("DELIM", "\\]")],
438 (fun _ (p : 'l1_pattern) _ _
439 (loc : Lexing.position * Lexing.position) ->
440 (return_term loc (Layout (Box (H, p))) : 'l1_simple_pattern));
441 [Gramext.Stoken ("SYMBOL", "\\ROOT"); Gramext.Sself;
442 Gramext.Stoken ("SYMBOL", "\\OF"); Gramext.Sself],
444 (fun (arg : 'l1_simple_pattern) _ (index : 'l1_simple_pattern) _
445 (loc : Lexing.position * Lexing.position) ->
446 (return_term loc (Layout (Root (arg, index))) :
447 'l1_simple_pattern));
448 [Gramext.Stoken ("SYMBOL", "\\SQRT"); Gramext.Sself],
450 (fun (p : 'l1_simple_pattern) _
451 (loc : Lexing.position * Lexing.position) ->
452 (return_term loc (Layout (Sqrt p)) : 'l1_simple_pattern));
453 [Gramext.Stoken ("SYMBOL", "\\FRAC"); Gramext.Sself; Gramext.Sself],
455 (fun (p2 : 'l1_simple_pattern) (p1 : 'l1_simple_pattern) _
456 (loc : Lexing.position * Lexing.position) ->
457 (return_term loc (Layout (Frac (p1, p2))) : 'l1_simple_pattern));
458 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\ATOP"); Gramext.Sself],
460 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
461 (loc : Lexing.position * Lexing.position) ->
462 (return_term loc (Layout (Atop (p1, p2))) : 'l1_simple_pattern));
463 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\OVER"); Gramext.Sself],
465 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
466 (loc : Lexing.position * Lexing.position) ->
467 (return_term loc (Layout (Over (p1, p2))) : 'l1_simple_pattern));
468 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\ABOVE"); Gramext.Sself],
470 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
471 (loc : Lexing.position * Lexing.position) ->
472 (return_term loc (Layout (Above (p1, p2))) :
473 'l1_simple_pattern));
474 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\BELOW"); Gramext.Sself],
476 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
477 (loc : Lexing.position * Lexing.position) ->
478 (return_term loc (Layout (Below (p1, p2))) :
479 'l1_simple_pattern));
480 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\SUP"); Gramext.Sself],
482 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
483 (loc : Lexing.position * Lexing.position) ->
484 (return_term loc (Layout (Sup (p1, p2))) : 'l1_simple_pattern));
485 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\SUB"); Gramext.Sself],
487 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
488 (loc : Lexing.position * Lexing.position) ->
489 (return_term loc (Layout (Sub (p1, p2))) : 'l1_simple_pattern))];
490 Some "simple", Some Gramext.NonA,
492 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
494 (fun (l : 'literal) (loc : Lexing.position * Lexing.position) ->
495 (return_term loc (Literal l) : 'l1_simple_pattern));
498 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e))],
500 (fun (v : 'l1_pattern_variable)
501 (loc : Lexing.position * Lexing.position) ->
502 (return_term loc (Variable v) : 'l1_simple_pattern));
505 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e))],
507 (fun (m : 'l1_magic_pattern)
508 (loc : Lexing.position * Lexing.position) ->
509 (return_term loc (Magic m) : 'l1_simple_pattern));
510 [Gramext.Stoken ("IDENT", "")],
512 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
513 (return_term loc (Ident (i, None)) : 'l1_simple_pattern))]];
514 Grammar.Entry.obj (level2_pattern : 'level2_pattern Grammar.Entry.e),
518 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e))],
520 (fun (p : 'l2_pattern) (loc : Lexing.position * Lexing.position) ->
521 (p : 'level2_pattern))]];
522 Grammar.Entry.obj (sort : 'sort Grammar.Entry.e), None,
524 [[Gramext.Stoken ("SYMBOL", "\\TYPE")],
526 (fun _ (loc : Lexing.position * Lexing.position) ->
528 [Gramext.Stoken ("SYMBOL", "\\SET")],
530 (fun _ (loc : Lexing.position * Lexing.position) -> (`Set : 'sort));
531 [Gramext.Stoken ("SYMBOL", "\\PROP")],
533 (fun _ (loc : Lexing.position * Lexing.position) ->
535 Grammar.Entry.obj (explicit_subst : 'explicit_subst Grammar.Entry.e),
538 [[Gramext.Stoken ("SYMBOL", "\\subst"); Gramext.Stoken ("SYMBOL", "[");
541 [[Gramext.Stoken ("IDENT", "");
542 Gramext.Stoken ("SYMBOL", "≔");
545 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
547 (fun (t : 'l2_pattern) _ (i : string)
548 (loc : Lexing.position * Lexing.position) ->
550 Gramext.Stoken ("SYMBOL", ";"));
551 Gramext.Stoken ("SYMBOL", "]")],
553 (fun _ (substs : 'e__1 list) _ _
554 (loc : Lexing.position * Lexing.position) ->
555 (substs : 'explicit_subst))]];
556 Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e), None,
559 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e))],
561 (fun (p : 'l2_pattern) (loc : Lexing.position * Lexing.position) ->
562 (Some p : 'meta_subst));
563 [Gramext.Stoken ("SYMBOL", "_")],
565 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
566 (None : 'meta_subst))]];
567 Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e), None,
569 [[Gramext.Stoken ("SYMBOL", "[");
572 (Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e)));
573 Gramext.Stoken ("SYMBOL", "]")],
575 (fun _ (substs : 'meta_subst list) _
576 (loc : Lexing.position * Lexing.position) ->
577 (substs : 'meta_substs))]];
579 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e),
583 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e))],
585 (fun (id : 'bound_name) (loc : Lexing.position * Lexing.position) ->
586 (id, None : 'possibly_typed_name));
587 [Gramext.Stoken ("SYMBOL", "(");
589 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e));
590 Gramext.Stoken ("SYMBOL", ":");
592 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
593 Gramext.Stoken ("SYMBOL", ")")],
595 (fun _ (typ : 'l2_pattern) _ (id : 'bound_name) _
596 (loc : Lexing.position * Lexing.position) ->
597 (id, Some typ : 'possibly_typed_name))]];
598 Grammar.Entry.obj (match_pattern : 'match_pattern Grammar.Entry.e),
601 [[Gramext.Stoken ("SYMBOL", "("); Gramext.Stoken ("IDENT", "");
605 (possibly_typed_name :
606 'possibly_typed_name Grammar.Entry.e)));
607 Gramext.Stoken ("SYMBOL", ")")],
609 (fun _ (vars : 'possibly_typed_name list) (id : string) _
610 (loc : Lexing.position * Lexing.position) ->
611 (id, vars : 'match_pattern));
612 [Gramext.Stoken ("IDENT", "")],
614 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
615 (id, [] : 'match_pattern))]];
616 Grammar.Entry.obj (binder : 'binder Grammar.Entry.e), None,
618 [[Gramext.Stoken ("SYMBOL", "λ")],
620 (fun _ (loc : Lexing.position * Lexing.position) ->
621 (`Lambda : 'binder));
622 [Gramext.Stoken ("SYMBOL", "∀")],
624 (fun _ (loc : Lexing.position * Lexing.position) ->
625 (`Forall : 'binder));
626 [Gramext.Stoken ("SYMBOL", "∃")],
628 (fun _ (loc : Lexing.position * Lexing.position) ->
629 (`Exists : 'binder));
630 [Gramext.Stoken ("SYMBOL", "Π")],
632 (fun _ (loc : Lexing.position * Lexing.position) ->
634 Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e), None,
636 [[Gramext.Stoken ("SYMBOL", "\\FRESH"); Gramext.Stoken ("IDENT", "")],
638 (fun (i : string) _ (loc : Lexing.position * Lexing.position) ->
639 (Variable (FreshVar i) : 'bound_name));
640 [Gramext.Stoken ("IDENT", "")],
642 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
643 (Ident (i, None) : 'bound_name))]];
644 Grammar.Entry.obj (bound_names : 'bound_names Grammar.Entry.e), None,
648 [[Gramext.Stoken ("SYMBOL", "(");
652 (bound_name : 'bound_name Grammar.Entry.e)),
653 Gramext.Stoken ("SYMBOL", ","));
656 [[Gramext.Stoken ("SYMBOL", ":");
659 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
661 (fun (p : 'l2_pattern) _
662 (loc : Lexing.position * Lexing.position) ->
664 Gramext.Stoken ("SYMBOL", ")")],
666 (fun _ (ty : 'e__3 option) (vars : 'bound_name list) _
667 (loc : Lexing.position * Lexing.position) ->
668 (vars, ty : 'e__4))])],
670 (fun (clusters : 'e__4 list)
671 (loc : Lexing.position * Lexing.position) ->
672 (clusters : 'bound_names));
675 (Grammar.Entry.obj (bound_name : 'bound_name Grammar.Entry.e)),
676 Gramext.Stoken ("SYMBOL", ","));
679 [[Gramext.Stoken ("SYMBOL", ":");
682 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
684 (fun (p : 'l2_pattern) _
685 (loc : Lexing.position * Lexing.position) ->
688 (fun (ty : 'e__2 option) (vars : 'bound_name list)
689 (loc : Lexing.position * Lexing.position) ->
690 ([vars, ty] : 'bound_names))]];
691 Grammar.Entry.obj (induction_kind : 'induction_kind Grammar.Entry.e),
694 [[Gramext.Stoken ("IDENT", "corec")],
696 (fun _ (loc : Lexing.position * Lexing.position) ->
697 (`CoInductive : 'induction_kind));
698 [Gramext.Stoken ("IDENT", "rec")],
700 (fun _ (loc : Lexing.position * Lexing.position) ->
701 (`Inductive : 'induction_kind))]];
702 Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e), None,
708 (bound_name : 'bound_name Grammar.Entry.e));
711 (bound_names : 'bound_names Grammar.Entry.e));
714 [[Gramext.Stoken ("IDENT", "on");
717 (bound_name : 'bound_name Grammar.Entry.e))],
719 (fun (id : 'bound_name) _
720 (loc : Lexing.position * Lexing.position) ->
724 [[Gramext.Stoken ("SYMBOL", ":");
727 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
729 (fun (p : 'l2_pattern) _
730 (loc : Lexing.position * Lexing.position) ->
732 Gramext.Stoken ("SYMBOL", "≝");
735 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
737 (fun (body : 'l2_pattern) _ (ty : 'e__6 option)
738 (index_name : 'e__5 option) (args : 'bound_names)
740 (loc : Lexing.position * Lexing.position) ->
741 (let body = fold_binder `Lambda args body in
745 | Some ty -> Some (fold_binder `Pi args ty)
747 let rec position_of name p =
750 | n :: _ when n = name -> Some p, p
751 | _ :: tl -> position_of name (p + 1) tl
753 let rec find_arg name n =
757 (sprintf "Argument %s not found"
758 (CicNotationPp.pp_term name))
760 match position_of name 0 l with
761 None, len -> find_arg name (n + len) tl
762 | Some where, len -> n + where
765 match index_name with
767 | Some name -> find_arg name 0 args
769 (name, ty), body, index :
771 Gramext.Stoken ("IDENT", "and"))],
773 (fun (defs : 'e__7 list)
774 (loc : Lexing.position * Lexing.position) ->
775 (defs : 'let_defs))]];
777 (l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e),
780 [[Gramext.Stoken ("SYMBOL", "\\FRESH"); Gramext.Stoken ("IDENT", "")],
782 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
783 (FreshVar id : 'l2_pattern_variable));
784 [Gramext.Stoken ("SYMBOL", "\\IDENT"); Gramext.Stoken ("IDENT", "")],
786 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
787 (IdentVar id : 'l2_pattern_variable));
788 [Gramext.Stoken ("SYMBOL", "\\NUM"); Gramext.Stoken ("IDENT", "")],
790 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
791 (NumVar id : 'l2_pattern_variable))]];
793 (l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e),
796 [[Gramext.Stoken ("SYMBOL", "\\DEFAULT");
797 Gramext.Stoken ("DELIM", "\\[");
799 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
800 Gramext.Stoken ("DELIM", "\\]"); Gramext.Stoken ("DELIM", "\\[");
802 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
803 Gramext.Stoken ("DELIM", "\\]")],
805 (fun _ (none : 'l2_pattern) _ _ (some : 'l2_pattern) _ _
806 (loc : Lexing.position * Lexing.position) ->
807 (Default (some, none) : 'l2_magic_pattern));
808 [Gramext.Stoken ("SYMBOL", "\\FOLD");
810 [[Gramext.Stoken ("IDENT", "right")],
812 (fun _ (loc : Lexing.position * Lexing.position) ->
814 [Gramext.Stoken ("IDENT", "left")],
816 (fun _ (loc : Lexing.position * Lexing.position) ->
818 Gramext.Stoken ("DELIM", "\\[");
820 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
821 Gramext.Stoken ("DELIM", "\\]");
822 Gramext.Stoken ("SYMBOL", "\\LAMBDA"); Gramext.Stoken ("IDENT", "");
823 Gramext.Stoken ("DELIM", "\\[");
825 (Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e));
826 Gramext.Stoken ("DELIM", "\\]")],
828 (fun _ (recursive : 'l2_pattern) _ (id : string) _ _
829 (base : 'l2_pattern) _ (kind : 'e__8) _
830 (loc : Lexing.position * Lexing.position) ->
831 (Fold (kind, base, [id], recursive) : 'l2_magic_pattern))]];
832 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
833 Some (Gramext.Level "10"),
834 [Some "10", Some Gramext.NonA,
835 [[Gramext.Stoken ("IDENT", "let");
838 (induction_kind : 'induction_kind Grammar.Entry.e));
840 (Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e));
841 Gramext.Stoken ("IDENT", "in"); Gramext.Sself],
843 (fun (body : 'l2_pattern) _ (defs : 'let_defs) (k : 'induction_kind)
844 _ (loc : Lexing.position * Lexing.position) ->
845 (return_term loc (LetRec (k, defs, body)) : 'l2_pattern));
846 [Gramext.Stoken ("IDENT", "let");
849 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e));
850 Gramext.Stoken ("SYMBOL", "≝"); Gramext.Sself;
851 Gramext.Stoken ("", "in"); Gramext.Sself],
853 (fun (p2 : 'l2_pattern) _ (p1 : 'l2_pattern) _
854 (var : 'possibly_typed_name) _
855 (loc : Lexing.position * Lexing.position) ->
856 (return_term loc (LetIn (var, p1, p2)) : 'l2_pattern))]];
857 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
858 Some (Gramext.Level "20"),
859 [Some "20", Some Gramext.RightA,
861 (Grammar.Entry.obj (binder : 'binder Grammar.Entry.e));
863 (Grammar.Entry.obj (bound_names : 'bound_names Grammar.Entry.e));
864 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
866 (fun (body : 'l2_pattern) _ (names : 'bound_names) (b : 'binder)
867 (loc : Lexing.position * Lexing.position) ->
868 (return_term loc (fold_binder b names body) : 'l2_pattern))]];
869 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
870 Some (Gramext.Level "70"),
871 [Some "70", Some Gramext.LeftA,
872 [[Gramext.Sself; Gramext.Sself],
874 (fun (p2 : 'l2_pattern) (p1 : 'l2_pattern)
875 (loc : Lexing.position * Lexing.position) ->
878 Appl (hd :: tl) | AttributedTerm (_, Appl (hd :: tl)) ->
882 return_term loc (Appl (aux p1 @ [p2])) :
884 Grammar.Entry.obj (l2_pattern : 'l2_pattern Grammar.Entry.e),
885 Some (Gramext.Level "90"),
886 [Some "90", Some Gramext.NonA,
889 (l2_magic_pattern : 'l2_magic_pattern Grammar.Entry.e))],
891 (fun (m : 'l2_magic_pattern)
892 (loc : Lexing.position * Lexing.position) ->
893 (return_term loc (Magic m) : 'l2_pattern));
896 (l2_pattern_variable : 'l2_pattern_variable Grammar.Entry.e))],
898 (fun (v : 'l2_pattern_variable)
899 (loc : Lexing.position * Lexing.position) ->
900 (return_term loc (Variable v) : 'l2_pattern));
901 [Gramext.Stoken ("SYMBOL", "("); Gramext.Sself;
902 Gramext.Stoken ("SYMBOL", ")")],
904 (fun _ (p : 'l2_pattern) _
905 (loc : Lexing.position * Lexing.position) ->
907 [Gramext.Stoken ("SYMBOL", "("); Gramext.Sself;
908 Gramext.Stoken ("SYMBOL", ":"); Gramext.Sself;
909 Gramext.Stoken ("SYMBOL", ")")],
911 (fun _ (p2 : 'l2_pattern) _ (p1 : 'l2_pattern) _
912 (loc : Lexing.position * Lexing.position) ->
913 (return_term loc (Appl [Symbol ("cast", 0); p1; p2]) :
917 [[Gramext.Stoken ("SYMBOL", "[");
920 (l2_pattern : 'l2_pattern Grammar.Entry.e));
921 Gramext.Stoken ("SYMBOL", "]")],
923 (fun _ (ty : 'l2_pattern) _
924 (loc : Lexing.position * Lexing.position) ->
926 Gramext.Stoken ("IDENT", "match"); Gramext.Sself;
929 [[Gramext.Stoken ("SYMBOL", ":"); Gramext.Stoken ("IDENT", "")],
932 (loc : Lexing.position * Lexing.position) ->
934 Gramext.Stoken ("IDENT", "with"); Gramext.Stoken ("SYMBOL", "[");
939 (match_pattern : 'match_pattern Grammar.Entry.e));
940 Gramext.Stoken ("SYMBOL", "⇒");
943 (l2_pattern : 'l2_pattern Grammar.Entry.e))],
945 (fun (rhs : 'l2_pattern) _ (lhs : 'match_pattern)
946 (loc : Lexing.position * Lexing.position) ->
947 (lhs, rhs : 'e__11))],
948 Gramext.Stoken ("SYMBOL", "|"));
949 Gramext.Stoken ("SYMBOL", "]")],
951 (fun _ (patterns : 'e__11 list) _ _ (indty_ident : 'e__10 option)
952 (t : 'l2_pattern) _ (outtyp : 'e__9 option)
953 (loc : Lexing.position * Lexing.position) ->
954 (return_term loc (Case (t, indty_ident, outtyp, patterns)) :
956 [Gramext.Snterm (Grammar.Entry.obj (sort : 'sort Grammar.Entry.e))],
958 (fun (s : 'sort) (loc : Lexing.position * Lexing.position) ->
959 (return_term loc (Sort s) : 'l2_pattern));
960 [Gramext.Stoken ("META", "");
962 (Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e))],
964 (fun (s : 'meta_substs) (m : string)
965 (loc : Lexing.position * Lexing.position) ->
966 (return_term loc (Meta (int_of_string m, s)) : 'l2_pattern));
967 [Gramext.Stoken ("META", "")],
969 (fun (m : string) (loc : Lexing.position * Lexing.position) ->
970 (return_term loc (Meta (int_of_string m, [])) : 'l2_pattern));
971 [Gramext.Stoken ("IMPLICIT", "")],
973 (fun _ (loc : Lexing.position * Lexing.position) ->
974 (return_term loc Implicit : 'l2_pattern));
975 [Gramext.Stoken ("NUMBER", "")],
977 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
978 (prerr_endline "number"; return_term loc (Num (n, 0)) :
980 [Gramext.Stoken ("URI", "")],
982 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
983 (return_term loc (Uri (u, None)) : 'l2_pattern));
984 [Gramext.Stoken ("IDENT", "");
987 (explicit_subst : 'explicit_subst Grammar.Entry.e))],
989 (fun (s : 'explicit_subst) (id : string)
990 (loc : Lexing.position * Lexing.position) ->
991 (return_term loc (Ident (id, Some s)) : 'l2_pattern));
992 [Gramext.Stoken ("IDENT", "")],
994 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
995 (return_term loc (Ident (id, None)) : 'l2_pattern))]];
996 Grammar.Entry.obj (argument : 'argument Grammar.Entry.e), None,
998 [[Gramext.Stoken ("SYMBOL", "η"); Gramext.Stoken ("IDENT", "");
999 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
1001 (fun (a : 'argument) _ (id : string) _
1002 (loc : Lexing.position * Lexing.position) ->
1003 (EtaArg (Some id, a) : 'argument));
1004 [Gramext.Stoken ("SYMBOL", "η"); Gramext.Stoken ("SYMBOL", ".");
1007 (fun (a : 'argument) _ _
1008 (loc : Lexing.position * Lexing.position) ->
1009 (EtaArg (None, a) : 'argument));
1010 [Gramext.Stoken ("IDENT", "")],
1012 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
1013 (IdentArg id : 'argument))]];
1014 Grammar.Entry.obj (level3_term : 'level3_term Grammar.Entry.e), None,
1016 [[Gramext.Stoken ("SYMBOL", "("); Gramext.Slist1 Gramext.Sself;
1017 Gramext.Stoken ("SYMBOL", ")")],
1019 (fun _ (terms : 'level3_term list) _
1020 (loc : Lexing.position * Lexing.position) ->
1024 | terms -> ApplPattern terms :
1027 (Grammar.Entry.obj (argument : 'argument Grammar.Entry.e))],
1029 (fun (a : 'argument) (loc : Lexing.position * Lexing.position) ->
1030 (ArgPattern a : 'level3_term));
1031 [Gramext.Stoken ("URI", "")],
1033 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
1034 (UriPattern u : 'level3_term))]];
1035 Grammar.Entry.obj (associativity : 'associativity Grammar.Entry.e),
1038 [[Gramext.Stoken ("IDENT", "non");
1039 Gramext.Stoken ("IDENT", "associative")],
1041 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1042 (Gramext.NonA : 'associativity));
1043 [Gramext.Stoken ("IDENT", "right");
1044 Gramext.Stoken ("IDENT", "associative")],
1046 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1047 (Gramext.RightA : 'associativity));
1048 [Gramext.Stoken ("IDENT", "left");
1049 Gramext.Stoken ("IDENT", "associative")],
1051 (fun _ _ (loc : Lexing.position * Lexing.position) ->
1052 (Gramext.LeftA : 'associativity))]];
1053 Grammar.Entry.obj (precedence : 'precedence Grammar.Entry.e), None,
1055 [[Gramext.Stoken ("IDENT", "with");
1056 Gramext.Stoken ("IDENT", "precedence");
1057 Gramext.Stoken ("NUMBER", "")],
1059 (fun (n : string) _ _ (loc : Lexing.position * Lexing.position) ->
1060 (int_of_string n : 'precedence))]];
1061 Grammar.Entry.obj (notation : 'notation Grammar.Entry.e), None,
1063 [[Gramext.Stoken ("IDENT", "notation");
1066 (level1_pattern : 'level1_pattern Grammar.Entry.e));
1070 (associativity : 'associativity Grammar.Entry.e)));
1073 (Grammar.Entry.obj (precedence : 'precedence Grammar.Entry.e)));
1074 Gramext.Stoken ("IDENT", "for");
1077 (level2_pattern : 'level2_pattern Grammar.Entry.e))],
1079 (fun (p2 : 'level2_pattern) _ (prec : 'precedence option)
1080 (assoc : 'associativity option) (p1 : 'level1_pattern) _
1081 (loc : Lexing.position * Lexing.position) ->
1082 (p1, assoc, prec, p2 : 'notation))]];
1083 Grammar.Entry.obj (interpretation : 'interpretation Grammar.Entry.e),
1086 [[Gramext.Stoken ("IDENT", "interpretation");
1087 Gramext.Stoken ("SYMBOL", "");
1090 (Grammar.Entry.obj (argument : 'argument Grammar.Entry.e)));
1091 Gramext.Stoken ("IDENT", "as");
1093 (Grammar.Entry.obj (level3_term : 'level3_term Grammar.Entry.e))],
1095 (fun (t : 'level3_term) _ (args : 'argument list) (s : string) _
1096 (loc : Lexing.position * Lexing.position) ->
1097 (() : 'interpretation))]];
1098 Grammar.Entry.obj (phrase : 'phrase Grammar.Entry.e), None,
1101 (Grammar.Entry.obj (notation : 'notation Grammar.Entry.e));
1102 Gramext.Stoken ("SYMBOL", ".")],
1104 (fun _ (l1, assoc, prec, l2 : 'notation)
1105 (loc : Lexing.position * Lexing.position) ->
1106 (Notation (l1, assoc, prec, l2) : 'phrase));
1107 [Gramext.Stoken ("IDENT", "print");
1110 (level2_pattern : 'level2_pattern Grammar.Entry.e));
1111 Gramext.Stoken ("SYMBOL", ".")],
1113 (fun _ (p2 : 'level2_pattern) _
1114 (loc : Lexing.position * Lexing.position) ->
1115 (Print p2 : 'phrase))]]])
1117 (** {2 API implementation} *)
1119 let exc_located_wrapper f =
1121 Stdpp.Exc_located (floc, Stream.Error msg) ->
1122 raise (Parse_error (floc, msg))
1123 | Stdpp.Exc_located (floc, exn) ->
1124 raise (Parse_error (floc, Printexc.to_string exn))
1126 let parse_syntax_pattern stream =
1127 exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream)
1128 let parse_ast_pattern stream =
1129 exc_located_wrapper (fun () -> Grammar.Entry.parse level2_pattern stream)
1130 let parse_interpretation stream =
1131 exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream)
1132 let parse_phrase stream =
1133 exc_located_wrapper (fun () -> Grammar.Entry.parse phrase stream)
1135 (** {2 Debugging} *)
1137 let print_l2_pattern () =
1138 Grammar.print_entry Format.std_formatter (Grammar.Entry.obj l2_pattern);
1139 Format.pp_print_flush Format.std_formatter ();
1142 (* vim:set encoding=utf8 foldmethod=marker: *)