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