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