]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/grafite_parser/grafiteDisambiguate.ml
test branch
[helm.git] / helm / ocaml / grafite_parser / grafiteDisambiguate.ml
diff --git a/helm/ocaml/grafite_parser/grafiteDisambiguate.ml b/helm/ocaml/grafite_parser/grafiteDisambiguate.ml
new file mode 100644 (file)
index 0000000..3d6f893
--- /dev/null
@@ -0,0 +1,288 @@
+(* Copyright (C) 2005, HELM Team.
+ * 
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ * 
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA  02111-1307, USA.
+ * 
+ * For details, see the HELM World-Wide-Web page,
+ * http://helm.cs.unibo.it/
+ *)
+
+(* $Id$ *)
+
+exception BaseUriNotSetYet
+
+let singleton = function
+  | [x], _ -> x
+  | _ -> assert false
+
+  (** @param term not meaningful when context is given *)
+let disambiguate_term lexicon_status_ref context metasenv term =
+  let lexicon_status = !lexicon_status_ref in
+  let (diff, metasenv, cic, _) =
+    singleton
+      (GrafiteDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
+        ~aliases:lexicon_status.LexiconEngine.aliases
+        ~universe:(Some lexicon_status.LexiconEngine.multi_aliases)
+        ~context ~metasenv term)
+  in
+  let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
+  lexicon_status_ref := lexicon_status;
+  metasenv,cic
+  
+  (** disambiguate_lazy_term (circa): term -> (unit -> status) * lazy_term
+   * rationale: lazy_term will be invoked in different context to obtain a term,
+   * each invocation will disambiguate the term and can add aliases. Once all
+   * disambiguations have been performed, the first returned function can be
+   * used to obtain the resulting aliases *)
+let disambiguate_lazy_term lexicon_status_ref term =
+  (fun context metasenv ugraph ->
+    let lexicon_status = !lexicon_status_ref in
+    let (diff, metasenv, cic, ugraph) =
+      singleton
+        (GrafiteDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
+          ~initial_ugraph:ugraph ~aliases:lexicon_status.LexiconEngine.aliases
+          ~universe:(Some lexicon_status.LexiconEngine.multi_aliases)
+          ~context ~metasenv
+          term) in
+    let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
+    lexicon_status_ref := lexicon_status;
+    cic, metasenv, ugraph)
+
+let disambiguate_pattern lexicon_status_ref (wanted, hyp_paths, goal_path) =
+  let interp path = Disambiguate.interpretate_path [] path in
+  let goal_path = HExtlib.map_option interp goal_path in
+  let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
+  let wanted =
+   match wanted with
+      None -> None
+    | Some wanted ->
+       let wanted = disambiguate_lazy_term lexicon_status_ref wanted in
+       Some wanted
+  in
+  (wanted, hyp_paths, goal_path)
+
+let disambiguate_reduction_kind lexicon_status_ref = function
+  | `Unfold (Some t) ->
+      let t = disambiguate_lazy_term lexicon_status_ref t in
+      `Unfold (Some t)
+  | `Normalize
+  | `Reduce
+  | `Simpl
+  | `Unfold None
+  | `Whd as kind -> kind
+  
+let disambiguate_tactic lexicon_status_ref context metasenv tactic =
+  let disambiguate_term = disambiguate_term lexicon_status_ref in
+  let disambiguate_pattern = disambiguate_pattern lexicon_status_ref in
+  let disambiguate_reduction_kind = disambiguate_reduction_kind lexicon_status_ref in
+  let disambiguate_lazy_term = disambiguate_lazy_term lexicon_status_ref in
+   match tactic with
+    | GrafiteAst.Absurd (loc, term) -> 
+        let metasenv,cic = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Absurd (loc, cic)
+    | GrafiteAst.Apply (loc, term) ->
+        let metasenv,cic = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Apply (loc, cic)
+    | GrafiteAst.Assumption loc ->
+        metasenv,GrafiteAst.Assumption loc
+    | GrafiteAst.Auto (loc,depth,width,paramodulation,full) ->
+        metasenv,GrafiteAst.Auto (loc,depth,width,paramodulation,full)
+    | GrafiteAst.Change (loc, pattern, with_what) -> 
+        let with_what = disambiguate_lazy_term with_what in
+        let pattern = disambiguate_pattern pattern in
+        metasenv,GrafiteAst.Change (loc, pattern, with_what)
+    | GrafiteAst.Clear (loc,id) ->
+        metasenv,GrafiteAst.Clear (loc,id)
+    | GrafiteAst.ClearBody (loc,id) ->
+       metasenv,GrafiteAst.ClearBody (loc,id)
+    | GrafiteAst.Compare (loc,term) ->
+        let metasenv,term = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Compare (loc,term)
+    | GrafiteAst.Constructor (loc,n) ->
+        metasenv,GrafiteAst.Constructor (loc,n)
+    | GrafiteAst.Contradiction loc ->
+        metasenv,GrafiteAst.Contradiction loc
+    | GrafiteAst.Cut (loc, ident, term) -> 
+        let metasenv,cic = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Cut (loc, ident, cic)
+    | GrafiteAst.DecideEquality loc ->
+        metasenv,GrafiteAst.DecideEquality loc
+    | GrafiteAst.Decompose (loc, types, what, names) ->
+        let disambiguate (metasenv,types) = function
+           | GrafiteAst.Type _   -> assert false
+           | GrafiteAst.Ident id ->
+              (match
+                disambiguate_term context metasenv
+                 (CicNotationPt.Ident(id, None))
+               with
+                | metasenv,Cic.MutInd (uri, tyno, _) ->
+                    metasenv,(GrafiteAst.Type (uri, tyno) :: types)
+                | _ ->
+                  raise (GrafiteDisambiguator.DisambiguationError
+                   (0,[[None,lazy "Decompose works only on inductive types"]])))
+        in
+        let metasenv,types =
+         List.fold_left disambiguate (metasenv,[]) types
+        in
+         metasenv,GrafiteAst.Decompose (loc, types, what, names)
+    | GrafiteAst.Discriminate (loc,term) ->
+        let metasenv,term = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Discriminate(loc,term)
+    | GrafiteAst.Exact (loc, term) -> 
+        let metasenv,cic = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Exact (loc, cic)
+    | GrafiteAst.Elim (loc, what, Some using, depth, idents) ->
+        let metasenv,what = disambiguate_term context metasenv what in
+        let metasenv,using = disambiguate_term context metasenv using in
+        metasenv,GrafiteAst.Elim (loc, what, Some using, depth, idents)
+    | GrafiteAst.Elim (loc, what, None, depth, idents) ->
+        let metasenv,what = disambiguate_term context metasenv what in
+        metasenv,GrafiteAst.Elim (loc, what, None, depth, idents)
+    | GrafiteAst.ElimType (loc, what, Some using, depth, idents) ->
+        let metasenv,what = disambiguate_term context metasenv what in
+        let metasenv,using = disambiguate_term context metasenv using in
+        metasenv,GrafiteAst.ElimType (loc, what, Some using, depth, idents)
+    | GrafiteAst.ElimType (loc, what, None, depth, idents) ->
+        let metasenv,what = disambiguate_term context metasenv what in
+        metasenv,GrafiteAst.ElimType (loc, what, None, depth, idents)
+    | GrafiteAst.Exists loc ->
+        metasenv,GrafiteAst.Exists loc 
+    | GrafiteAst.Fail loc ->
+        metasenv,GrafiteAst.Fail loc
+    | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
+        let pattern = disambiguate_pattern pattern in
+        let term = disambiguate_lazy_term term in
+        let red_kind = disambiguate_reduction_kind red_kind in
+        metasenv,GrafiteAst.Fold (loc, red_kind, term, pattern)
+    | GrafiteAst.FwdSimpl (loc, hyp, names) ->
+       metasenv,GrafiteAst.FwdSimpl (loc, hyp, names)  
+    | GrafiteAst.Fourier loc ->
+       metasenv,GrafiteAst.Fourier loc
+    | GrafiteAst.Generalize (loc,pattern,ident) ->
+        let pattern = disambiguate_pattern pattern in
+        metasenv,GrafiteAst.Generalize (loc,pattern,ident)
+    | GrafiteAst.Goal (loc, g) ->
+        metasenv,GrafiteAst.Goal (loc, g)
+    | GrafiteAst.IdTac loc ->
+        metasenv,GrafiteAst.IdTac loc
+    | GrafiteAst.Injection (loc, term) ->
+        let metasenv,term = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Injection (loc,term)
+    | GrafiteAst.Intros (loc, num, names) ->
+        metasenv,GrafiteAst.Intros (loc, num, names)
+    | GrafiteAst.Inversion (loc, term) ->
+       let metasenv,term = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Inversion (loc, term)
+    | GrafiteAst.LApply (loc, depth, to_what, what, ident) ->
+       let f term to_what =
+          let metasenv,term = disambiguate_term context metasenv term in
+          term :: to_what
+       in
+       let to_what = List.fold_right f to_what [] in 
+       let metasenv,what = disambiguate_term context metasenv what in
+       metasenv,GrafiteAst.LApply (loc, depth, to_what, what, ident)
+    | GrafiteAst.Left loc ->
+       metasenv,GrafiteAst.Left loc
+    | GrafiteAst.LetIn (loc, term, name) ->
+        let metasenv,term = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.LetIn (loc,term,name)
+    | GrafiteAst.Reduce (loc, red_kind, pattern) ->
+        let pattern = disambiguate_pattern pattern in
+        let red_kind = disambiguate_reduction_kind red_kind in
+        metasenv,GrafiteAst.Reduce(loc, red_kind, pattern)
+    | GrafiteAst.Reflexivity loc ->
+        metasenv,GrafiteAst.Reflexivity loc
+    | GrafiteAst.Replace (loc, pattern, with_what) -> 
+        let pattern = disambiguate_pattern pattern in
+        let with_what = disambiguate_lazy_term with_what in
+        metasenv,GrafiteAst.Replace (loc, pattern, with_what)
+    | GrafiteAst.Rewrite (loc, dir, t, pattern) ->
+        let metasenv,term = disambiguate_term context metasenv t in
+        let pattern = disambiguate_pattern pattern in
+        metasenv,GrafiteAst.Rewrite (loc, dir, term, pattern)
+    | GrafiteAst.Right loc ->
+        metasenv,GrafiteAst.Right loc
+    | GrafiteAst.Ring loc ->
+        metasenv,GrafiteAst.Ring loc
+    | GrafiteAst.Split loc ->
+        metasenv,GrafiteAst.Split loc
+    | GrafiteAst.Symmetry loc ->
+        metasenv,GrafiteAst.Symmetry loc
+    | GrafiteAst.Transitivity (loc, term) -> 
+        let metasenv,cic = disambiguate_term context metasenv term in
+        metasenv,GrafiteAst.Transitivity (loc, cic)
+
+let disambiguate_obj lexicon_status ~baseuri metasenv obj =
+  let uri =
+   match obj with
+    | CicNotationPt.Inductive (_,(name,_,_,_)::_)
+    | CicNotationPt.Record (_,name,_,_) ->
+       (match baseuri with
+         | Some baseuri ->
+            Some (UriManager.uri_of_string (baseuri ^ "/" ^ name ^ ".ind"))
+         | None -> raise BaseUriNotSetYet)
+    | CicNotationPt.Inductive _ -> assert false
+    | CicNotationPt.Theorem _ -> None in
+  let (diff, metasenv, cic, _) =
+    singleton
+      (GrafiteDisambiguator.disambiguate_obj ~dbd:(LibraryDb.instance ())
+        ~aliases:lexicon_status.LexiconEngine.aliases
+        ~universe:(Some lexicon_status.LexiconEngine.multi_aliases) ~uri obj) in
+  let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
+  lexicon_status, metasenv, cic
+  
+let disambiguate_command lexicon_status ~baseuri metasenv =
+ function
+  | GrafiteAst.Coercion _
+  | GrafiteAst.Default _
+  | GrafiteAst.Drop _
+  | GrafiteAst.Include _
+  | GrafiteAst.Qed _
+  | GrafiteAst.Set _ as cmd ->
+      lexicon_status,metasenv,cmd
+  | GrafiteAst.Obj (loc,obj) ->
+      let lexicon_status,metasenv,obj =
+       disambiguate_obj lexicon_status ~baseuri metasenv obj in
+      lexicon_status, metasenv, GrafiteAst.Obj (loc,obj)
+
+let disambiguate_macro lexicon_status_ref metasenv context macro =
+ let disambiguate_term = disambiguate_term lexicon_status_ref in
+  match macro with
+   | GrafiteAst.WMatch (loc,term) ->
+      let metasenv,term = disambiguate_term context metasenv term in
+       metasenv,GrafiteAst.WMatch (loc,term)
+   | GrafiteAst.WInstance (loc,term) ->
+      let metasenv,term = disambiguate_term context metasenv term in
+       metasenv,GrafiteAst.WInstance (loc,term)
+   | GrafiteAst.WElim (loc,term) ->
+      let metasenv,term = disambiguate_term context metasenv term in
+       metasenv,GrafiteAst.WElim (loc,term)
+   | GrafiteAst.WHint (loc,term) ->
+      let metasenv,term = disambiguate_term context metasenv term in
+       metasenv,GrafiteAst.WHint (loc,term)
+   | GrafiteAst.Check (loc,term) ->
+      let metasenv,term = disambiguate_term context metasenv term in
+       metasenv,GrafiteAst.Check (loc,term)
+   | GrafiteAst.Hint _
+   | GrafiteAst.WLocate _ as macro ->
+      metasenv,macro
+   | GrafiteAst.Quit _
+   | GrafiteAst.Print _
+   | GrafiteAst.Search_pat _
+   | GrafiteAst.Search_term _ -> assert false