]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaDisambiguator.ml
snapshot, notably:
[helm.git] / helm / matita / matitaDisambiguator.ml
1 (* Copyright (C) 2004, 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 MatitaTypes
27
28 class parserr () =
29   object
30     method parseTerm = CicTextualParser2.parse_term
31     method parseTactical = CicTextualParser2.parse_tactical
32   end
33
34 let parserr () = new parserr ()
35 let parserr_instance = MatitaMisc.singleton parserr
36
37 class disambiguator () =
38   let _chooseUris = ref mono_uris_callback in
39   let _chooseInterp = ref mono_interp_callback in
40   let disambiguate_term =
41     let module Callbacks =
42       struct
43         let interactive_user_uri_choice
44           ~selection_mode ?ok ?(enable_button_for_non_vars = true) ~title ~msg
45           ~id uris
46         =
47           !_chooseUris ~selection_mode ~title ~msg
48             ~nonvars_button:enable_button_for_non_vars uris
49
50         let interactive_interpretation_choice interp = !_chooseInterp interp
51
52         let input_or_locate_uri ~(title:string) ?id =
53           (* Zack: I try to avoid using this callback. I therefore assume that
54           * the presence of an identifier that can't be resolved via "locate"
55           * query is a syntax error *)
56           let msg = match id with Some id -> id | _ -> "_" in
57           raise (Unbound_identifier msg)
58       end
59     in
60     let module Disambiguator = Disambiguate.Make (Callbacks) in
61     Disambiguator.disambiguate_term 
62   in
63   object (self)
64     val mutable _env = DisambiguateTypes.Environment.empty
65     method env = _env
66     method setEnv e = _env <- e
67
68     method chooseUris = !_chooseUris
69     method setChooseUris f = _chooseUris := f
70
71     method chooseInterp = !_chooseInterp
72     method setChooseInterp f = _chooseInterp := f
73
74     val parserr = parserr_instance ()
75     method parserr = parserr
76
77     val dbd = MatitaMisc.dbd_instance ()
78
79     method disambiguateTermAst ?(context = []) ?(metasenv = []) ?env termAst =
80       let (save_state, env) =
81         match env with
82         | Some env -> (false, env)
83         | None -> (true, _env)
84       in
85       match disambiguate_term ~initial_ugraph:CicUniv.empty_ugraph 
86         ~dbd context metasenv termAst ~aliases:env with
87       | [ (env, metasenv, term,ugraph) as x ] ->
88           if save_state then self#setEnv env;
89           x
90       | _ -> assert false
91
92     method disambiguateTermAsts ?(metasenv = []) ?env asts =
93       let ast = CicAst.pack asts in
94       let (env, metasenv, term, ugraph) =
95         self#disambiguateTermAst ~context:[] ~metasenv ?env ast
96       in
97       (env, metasenv, CicUtil.unpack term, ugraph)
98
99     method disambiguateTerm ?context ?metasenv ?env stream =
100       self#disambiguateTermAst ?context ?metasenv ?env
101         (parserr#parseTerm stream)
102   end
103
104 let disambiguator () = new disambiguator ()
105 let instance = MatitaMisc.singleton disambiguator 
106