]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteParser.ml
merged cic_notation with disambiguation: good luck!
[helm.git] / helm / ocaml / cic_notation / grafiteParser.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 let grammar = CicNotationParser.level2_ast_grammar
29
30 let term = CicNotationParser.term
31 let statement = Grammar.Entry.create grammar "statement"
32
33 EXTEND
34   GLOBAL: term statement;
35   arg: [
36    [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
37       SYMBOL ":"; ty = term; RPAREN -> names,ty
38    | name = IDENT -> [name],CicNotationPt.Implicit
39    ]
40   ];
41   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
42   tactic_term: [ [ t = term -> t ] ];
43   ident_list0: [
44     [ SYMBOL "["; idents = LIST0 IDENT SEP SYMBOL ";"; SYMBOL "]" -> idents ]
45   ];
46   tactic_term_list1: [
47     [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
48   ];
49   reduction_kind: [
50     [ IDENT "reduce" -> `Reduce
51     | IDENT "simplify" -> `Simpl
52     | IDENT "whd" -> `Whd 
53     | IDENT "normalize" -> `Normalize ]
54   ];
55   sequent_pattern_spec: [
56    [ hyp_paths =
57       LIST0
58        [ id = IDENT ;
59          path = OPT [SYMBOL ":" ; path = term -> path ] ->
60          (id,match path with Some p -> p | None -> CicNotationPt.UserInput) ]
61        SEP SYMBOL ";";
62      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
63       let goal_path =
64        match goal_path with
65           None -> CicNotationPt.UserInput
66         | Some goal_path -> goal_path
67       in
68        hyp_paths,goal_path
69    ]
70   ];
71   pattern_spec: [
72     [ res = OPT [
73        "in";
74        wanted_and_sps =
75         [ "match" ; wanted = term ;
76           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
77            Some wanted,sps
78         | sps = sequent_pattern_spec ->
79            None,Some sps
80         ] ->
81          let wanted,hyp_paths,goal_path =
82           match wanted_and_sps with
83              wanted,None -> wanted, [], CicNotationPt.UserInput
84            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
85          in
86           wanted, hyp_paths, goal_path ] ->
87       match res with
88          None -> None,[],CicNotationPt.UserInput
89        | Some ps -> ps]
90   ];
91   direction: [
92     [ SYMBOL ">" -> `LeftToRight
93     | SYMBOL "<" -> `RightToLeft ]
94   ];
95   int: [ [ num = NUMBER -> int_of_string num ] ];
96   tactic: [
97     [ IDENT "absurd"; t = tactic_term ->
98         GrafiteAst.Absurd (loc, t)
99     | IDENT "apply"; t = tactic_term ->
100         GrafiteAst.Apply (loc, t)
101     | IDENT "assumption" ->
102         GrafiteAst.Assumption loc
103     | IDENT "auto";
104       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
105       width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ] -> 
106           GrafiteAst.Auto (loc,depth,width)
107     | IDENT "clear"; id = IDENT ->
108         GrafiteAst.Clear (loc,id)
109     | IDENT "clearbody"; id = IDENT ->
110         GrafiteAst.ClearBody (loc,id)
111     | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
112         GrafiteAst.Change (loc, what, t)
113     | IDENT "compare"; t = tactic_term ->
114         GrafiteAst.Compare (loc,t)
115     | IDENT "constructor"; n = int ->
116         GrafiteAst.Constructor (loc, n)
117     | IDENT "contradiction" ->
118         GrafiteAst.Contradiction loc
119     | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
120         GrafiteAst.Cut (loc, ident, t)
121     | IDENT "decide"; IDENT "equality" ->
122         GrafiteAst.DecideEquality loc
123     | IDENT "decompose"; where = tactic_term ->
124         GrafiteAst.Decompose (loc, where)
125     | IDENT "discriminate"; t = tactic_term ->
126         GrafiteAst.Discriminate (loc, t)
127     | IDENT "elim"; what = tactic_term;
128       using = OPT [ "using"; using = tactic_term -> using ];  
129       OPT "names"; num = OPT [num = int -> num];
130       idents = OPT ident_list0 ->
131         let idents = match idents with None -> [] | Some idents -> idents in
132         GrafiteAst.Elim (loc, what, using, num, idents)
133     | IDENT "elimType"; what = tactic_term;
134       using = OPT [ "using"; using = tactic_term -> using ];  
135       OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
136         let idents = match idents with None -> [] | Some idents -> idents in
137         GrafiteAst.ElimType (loc, what, using, num, idents)
138     | IDENT "exact"; t = tactic_term ->
139         GrafiteAst.Exact (loc, t)
140     | IDENT "exists" ->
141         GrafiteAst.Exists loc
142     | IDENT "fail" -> GrafiteAst.Fail loc
143     | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
144         let (pt,_,_) = p in
145           if pt <> None then
146             raise (CicNotationParser.Parse_error
147               (loc, "the pattern cannot specify the term to replace, only its"
148               ^ " paths in the hypotheses and in the conclusion"))
149         else
150          GrafiteAst.Fold (loc, kind, t, p)
151     | IDENT "fourier" ->
152         GrafiteAst.Fourier loc
153     | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
154         let idents = match idents with None -> [] | Some idents -> idents in
155         GrafiteAst.FwdSimpl (loc, hyp, idents)
156     | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
157        GrafiteAst.Generalize (loc,p,id)
158     | IDENT "goal"; n = int ->
159         GrafiteAst.Goal (loc, n)
160     | IDENT "id" -> GrafiteAst.IdTac loc
161     | IDENT "injection"; t = tactic_term ->
162         GrafiteAst.Injection (loc, t)
163     | IDENT "intro"; ident = OPT IDENT ->
164         let idents = match ident with None -> [] | Some id -> [id] in
165         GrafiteAst.Intros (loc, Some 1, idents)
166     | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
167         let idents = match idents with None -> [] | Some idents -> idents in
168         GrafiteAst.Intros (loc, num, idents)
169     | IDENT "lapply"; 
170       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
171       what = tactic_term; 
172       to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
173       ident = OPT [ "using" ; ident = IDENT -> ident ] ->
174         let to_what = match to_what with None -> [] | Some to_what -> to_what in
175         GrafiteAst.LApply (loc, depth, to_what, what, ident)
176     | IDENT "left" -> GrafiteAst.Left loc
177     | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
178         GrafiteAst.LetIn (loc, t, where)
179     | kind = reduction_kind; p = pattern_spec ->
180         GrafiteAst.Reduce (loc, kind, p)
181     | IDENT "reflexivity" ->
182         GrafiteAst.Reflexivity loc
183     | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
184         GrafiteAst.Replace (loc, p, t)
185     | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
186        let (pt,_,_) = p in
187         if pt <> None then
188          raise
189           (CicNotationParser.Parse_error
190             (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
191         else
192          GrafiteAst.Rewrite (loc, d, t, p)
193     | IDENT "right" ->
194         GrafiteAst.Right loc
195     | IDENT "ring" ->
196         GrafiteAst.Ring loc
197     | IDENT "split" ->
198         GrafiteAst.Split loc
199     | IDENT "symmetry" ->
200         GrafiteAst.Symmetry loc
201     | IDENT "transitivity"; t = tactic_term ->
202         GrafiteAst.Transitivity (loc, t)
203     ]
204   ];
205   tactical:
206     [ "sequence" LEFTA
207       [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
208          match tacticals with
209             [] -> assert false
210           | [tac] -> tac
211           | l -> GrafiteAst.Seq (loc, l)
212       ]
213     | "then" NONA
214       [ tac = tactical; SYMBOL ";";
215         SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
216           (GrafiteAst.Then (loc, tac, tacs))
217       ]
218     | "loops" RIGHTA
219       [ IDENT "do"; count = int; tac = tactical ->
220           GrafiteAst.Do (loc, count, tac)
221       | IDENT "repeat"; tac = tactical ->
222           GrafiteAst.Repeat (loc, tac)
223       ]
224     | "simple" NONA
225       [ IDENT "first";
226         SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
227           GrafiteAst.First (loc, tacs)
228       | IDENT "try"; tac = NEXT ->
229           GrafiteAst.Try (loc, tac)
230       | IDENT "solve";
231         SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
232           GrafiteAst.Solve (loc, tacs)
233       | LPAREN; tac = tactical; RPAREN -> tac
234       | tac = tactic -> GrafiteAst.Tactic (loc, tac)
235       ]
236     ];
237   theorem_flavour: [
238     [ [ IDENT "definition"  ] -> `Definition
239     | [ IDENT "fact"        ] -> `Fact
240     | [ IDENT "lemma"       ] -> `Lemma
241     | [ IDENT "remark"      ] -> `Remark
242     | [ IDENT "theorem"     ] -> `Theorem
243     ]
244   ];
245   inductive_spec: [ [
246     fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
247     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
248     fst_constructors = LIST0 constructor SEP SYMBOL "|";
249     tl = OPT [ "with";
250       types = LIST1 [
251         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
252        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
253           (name, true, typ, constructors) ] SEP "with" -> types
254     ] ->
255       let params =
256         List.fold_right
257           (fun (names, typ) acc ->
258             (List.map (fun name -> (name, typ)) names) @ acc)
259           params []
260       in
261       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
262       let tl_ind_types = match tl with None -> [] | Some types -> types in
263       let ind_types = fst_ind_type :: tl_ind_types in
264       (params, ind_types)
265   ] ];
266   
267   record_spec: [ [
268     name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
269      SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
270      fields = LIST0 [ 
271        name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty) 
272      ] SEP SYMBOL ";"; SYMBOL "}" -> 
273       let params =
274         List.fold_right
275           (fun (names, typ) acc ->
276             (List.map (fun name -> (name, typ)) names) @ acc)
277           params []
278       in
279       (params,name,typ,fields)
280   ] ];
281   
282   macro: [
283     [ [ IDENT "quit"  ] -> GrafiteAst.Quit loc
284 (*     | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
285     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
286 (*     | [ IDENT "undo"   ]; steps = OPT NUMBER ->
287         GrafiteAst.Undo (loc, int_opt steps)
288     | [ IDENT "redo"   ]; steps = OPT NUMBER ->
289         GrafiteAst.Redo (loc, int_opt steps) *)
290     | [ IDENT "check"   ]; t = term ->
291         GrafiteAst.Check (loc, t)
292     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
293     | [ IDENT "whelp"; "match" ] ; t = term -> 
294         GrafiteAst.WMatch (loc,t)
295     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
296         GrafiteAst.WInstance (loc,t)
297     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
298         GrafiteAst.WLocate (loc,id)
299     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
300         GrafiteAst.WElim (loc, t)
301     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
302         GrafiteAst.WHint (loc,t)
303     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
304     ]
305   ];
306   alias_spec: [
307     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
308       let alpha = "[a-zA-Z]" in
309       let num = "[0-9]+" in
310       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
311       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
312       let rex = Str.regexp ("^"^ident^"$") in
313       if Str.string_match rex id 0 then
314         if (try ignore (UriManager.uri_of_string uri); true
315             with UriManager.IllFormedUri _ -> false)
316         then
317           GrafiteAst.Ident_alias (id, uri)
318         else 
319           raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
320       else
321         raise (CicNotationParser.Parse_error (loc,
322           sprintf "Not a valid identifier: %s" id))
323     | IDENT "symbol"; symbol = QSTRING;
324       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
325       SYMBOL "="; dsc = QSTRING ->
326         let instance =
327           match instance with Some i -> i | None -> 0
328         in
329         GrafiteAst.Symbol_alias (symbol, instance, dsc)
330     | IDENT "num";
331       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
332       SYMBOL "="; dsc = QSTRING ->
333         let instance =
334           match instance with Some i -> i | None -> 0
335         in
336         GrafiteAst.Number_alias (instance, dsc)
337     ]
338   ];
339   argument: [
340     [ id = IDENT -> CicNotationPt.IdentArg (0, id)
341     | l = LIST1 [ SYMBOL <:unicode<eta>> (* η *) -> () ] SEP SYMBOL ".";
342       SYMBOL "."; id = IDENT ->
343         CicNotationPt.IdentArg (List.length l, id)
344     ]
345   ];
346   associativity: [
347     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
348     | IDENT "right"; IDENT "associative" -> Gramext.RightA
349     | IDENT "non"; IDENT "associative" -> Gramext.NonA
350     ]
351   ];
352   precedence: [
353     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
354   ];
355   notation: [
356     [ s = QSTRING;
357       assoc = OPT associativity; prec = OPT precedence;
358       IDENT "for";
359       p2 = 
360         [ blob = UNPARSED_AST ->
361             CicNotationParser.parse_level2_ast (Stream.of_string blob)
362         | blob = UNPARSED_META ->
363             CicNotationParser.parse_level2_meta (Stream.of_string blob) ]
364       ->
365         (CicNotationParser.parse_level1_pattern (Stream.of_string s), assoc, prec, p2)
366     ]
367   ];
368   level3_term: [
369     [ u = URI -> CicNotationPt.UriPattern (UriManager.uri_of_string u)
370     | id = IDENT -> CicNotationPt.VarPattern id
371     | SYMBOL "_" -> CicNotationPt.ImplicitPattern
372     | LPAREN; terms = LIST1 SELF; RPAREN ->
373         (match terms with
374         | [] -> assert false
375         | [term] -> term
376         | terms -> CicNotationPt.ApplPattern terms)
377     ]
378   ];
379   interpretation: [
380     [ s = CSYMBOL; args = LIST1 argument; SYMBOL "="; t = level3_term ->
381         (s, args, t)
382     ]
383   ];
384   command: [ [
385       IDENT "set"; n = QSTRING; v = QSTRING ->
386         GrafiteAst.Set (loc, n, v)
387     | IDENT "drop" -> GrafiteAst.Drop loc
388     | IDENT "qed" -> GrafiteAst.Qed loc
389     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
390       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
391         GrafiteAst.Obj (loc, 
392           GrafiteAst.Theorem 
393             (`Variant,name,typ,Some (CicNotationPt.Ident (newname, None))))
394     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
395       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
396         GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
397     | flavour = theorem_flavour; name = IDENT;
398       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
399         GrafiteAst.Obj (loc,
400           GrafiteAst.Theorem (flavour, name, CicNotationPt.Implicit, body))
401     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
402         defs = CicNotationParser.let_defs -> 
403           let name,ty = 
404             match defs with
405             | ((CicNotationPt.Ident (name, None), Some ty),_,_) :: _ -> name,ty
406             | ((CicNotationPt.Ident (name, None), None),_,_) :: _ ->
407                 name, CicNotationPt.Implicit
408             | _ -> assert false 
409           in
410           let body = CicNotationPt.Ident (name,None) in
411           GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
412             Some (CicNotationPt.LetRec (ind_kind, defs, body))))
413     | [ IDENT "inductive" ]; spec = inductive_spec ->
414         let (params, ind_types) = spec in
415         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
416     | [ IDENT "coinductive" ]; spec = inductive_spec ->
417         let (params, ind_types) = spec in
418         let ind_types = (* set inductive flags to false (coinductive) *)
419           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
420             ind_types
421         in
422         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
423     | IDENT "coercion" ; name = IDENT -> 
424         GrafiteAst.Coercion (loc, CicNotationPt.Ident (name,Some []))
425     | IDENT "coercion" ; name = URI -> 
426         GrafiteAst.Coercion (loc, CicNotationPt.Uri (name,Some []))
427     | IDENT "alias" ; spec = alias_spec ->
428         GrafiteAst.Alias (loc, spec)
429     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
430         GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
431     | IDENT "include" ; path = QSTRING ->
432         GrafiteAst.Include (loc,path)
433     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
434        let uris = List.map UriManager.uri_of_string uris in
435         GrafiteAst.Default (loc,what,uris)
436     | IDENT "notation"; (l1, assoc, prec, l2) = notation ->
437         GrafiteAst.Notation (loc, l1, assoc, prec, l2)
438     | IDENT "interpretation"; id = QSTRING;
439       (symbol, args, l3) = interpretation ->
440         GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
441
442     | IDENT "dump" -> GrafiteAst.Dump loc
443     | IDENT "render"; u = URI ->
444         GrafiteAst.Render (loc, UriManager.uri_of_string u)
445   ]];
446   executable: [
447     [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
448     | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
449     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
450     ]
451   ];
452   comment: [
453     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
454        GrafiteAst.Code (loc, ex)
455     | str = NOTE -> 
456        GrafiteAst.Note (loc, str)
457     ]
458   ];
459   statement: [
460     [ ex = executable -> GrafiteAst.Executable (loc,ex)
461     | com = comment -> GrafiteAst.Comment (loc, com)
462     | EOI -> raise End_of_file
463     ]
464   ];
465 END
466
467 let exc_located_wrapper f =
468   try
469     f ()
470   with
471   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
472   | Stdpp.Exc_located (floc, Stream.Error msg) ->
473       raise (CicNotationParser.Parse_error (floc, msg))
474   | Stdpp.Exc_located (floc, exn) ->
475       raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
476
477 let parse_statement stream =
478   exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))
479