]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_parser/grafiteDisambiguate.ml
7511e02aaad65670504ea7a23cd0902990da0148
[helm.git] / matita / components / grafite_parser / grafiteDisambiguate.ml
1 (*
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 (* $Id$ *)
27
28 class type g_status =
29   object
30    inherit LexiconTypes.g_status
31   end
32
33 class status =
34  object (self)
35   inherit LexiconTypes.status
36   method set_grafite_disambiguate_status
37    : 'status. #g_status as 'status -> 'self
38       = fun o -> (self#set_lexicon_engine_status o)
39  end
40
41 exception BaseUriNotSetYet
42
43 let singleton msg = function
44   | [x], _ -> x
45   | l, _   ->
46       let debug = 
47          Printf.sprintf "GrafiteDisambiguate.singleton (%s): %u interpretations"
48          msg (List.length l)
49       in
50       prerr_endline debug; assert false
51
52 let __Implicit = "__Implicit__"
53 let __Closed_Implicit = "__Closed_Implicit__"
54
55 let ncic_mk_choice status = function
56   | GrafiteAst.Symbol_alias (name, _, dsc) ->
57      if name = __Implicit then
58        dsc, `Sym_interp (fun _ -> NCic.Implicit `Term)
59      else if name = __Closed_Implicit then 
60        dsc, `Sym_interp (fun _ -> NCic.Implicit `Closed)
61      else
62        DisambiguateChoices.lookup_symbol_by_dsc status
63         ~mk_implicit:(function 
64            | true -> NCic.Implicit `Closed
65            | false -> NCic.Implicit `Term)
66         ~mk_appl:(function 
67            (NCic.Appl l)::tl -> NCic.Appl (l@tl) | l -> NCic.Appl l)
68         ~term_of_nref:(fun nref -> NCic.Const nref)
69        name dsc
70   | GrafiteAst.Number_alias (_, dsc) -> 
71      let desc,f = DisambiguateChoices.nlookup_num_by_dsc dsc in
72       desc, `Num_interp
73        (fun num -> match f with `Num_interp f -> f num | _ -> assert false)
74   | GrafiteAst.Ident_alias (name, uri) -> 
75      uri, `Sym_interp 
76       (fun l->assert(l = []);
77         let nref = NReference.reference_of_string uri in
78          NCic.Const nref)
79 ;;
80
81
82 let mk_implicit b =
83   match b with
84   | false -> 
85       GrafiteAst.Symbol_alias (__Implicit,-1,"Fake Implicit")
86   | true -> 
87       GrafiteAst.Symbol_alias (__Closed_Implicit,-1,"Fake Closed Implicit")
88 ;;
89
90 let nlookup_in_library 
91   interactive_user_uri_choice input_or_locate_uri item 
92 =
93   match item with
94   | DisambiguateTypes.Id id -> 
95      (try
96        let references = NCicLibrary.resolve id in
97         List.map
98          (fun u -> GrafiteAst.Ident_alias (id,NReference.string_of_reference u)
99          ) references
100       with
101        NCicEnvironment.ObjectNotFound _ -> [])
102   | _ -> []
103 ;;
104
105 let fix_instance item l =
106  match item with
107     DisambiguateTypes.Symbol (_,n) ->
108      List.map
109       (function
110           GrafiteAst.Symbol_alias (s,_,d) -> GrafiteAst.Symbol_alias (s,n,d)
111         | _ -> assert false
112       ) l
113   | DisambiguateTypes.Num n ->
114      List.map
115       (function
116           GrafiteAst.Number_alias (_,d) -> GrafiteAst.Number_alias (n,d)
117         | _ -> assert false
118       ) l
119   | DisambiguateTypes.Id _ -> l
120 ;;
121
122
123 let disambiguate_nterm expty estatus context metasenv subst thing
124 =
125   let diff, metasenv, subst, cic =
126     singleton "first"
127       (NCicDisambiguate.disambiguate_term
128         ~rdb:estatus
129         ~aliases:estatus#lstatus.LexiconTypes.aliases
130         ~expty 
131         ~universe:(Some estatus#lstatus.LexiconTypes.multi_aliases)
132         ~lookup_in_library:nlookup_in_library
133         ~mk_choice:(ncic_mk_choice estatus)
134         ~mk_implicit ~fix_instance
135         ~description_of_alias:GrafiteAst.description_of_alias
136         ~context ~metasenv ~subst thing)
137   in
138   let estatus =
139     LexiconEngine.set_proof_aliases estatus ~implicit_aliases:true
140      GrafiteAst.WithPreferences diff
141   in
142    metasenv, subst, estatus, cic
143 ;;
144
145
146 type pattern = 
147   NotationPt.term Disambiguate.disambiguator_input option * 
148   (string * NCic.term) list * NCic.term option
149
150 let disambiguate_npattern (text, prefix_len, (wanted, hyp_paths, goal_path)) =
151   let interp path = NCicDisambiguate.disambiguate_path path in
152   let goal_path = HExtlib.map_option interp goal_path in
153   let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
154   let wanted = 
155     match wanted with None -> None | Some x -> Some (text,prefix_len,x)
156   in
157    (wanted, hyp_paths, goal_path)
158 ;;
159
160 let disambiguate_reduction_kind text prefix_len lexicon_status_ref = function
161   | `Unfold (Some t) -> assert false (* MATITA 1.0 *)
162   | `Normalize
163   | `Simpl
164   | `Unfold None
165   | `Whd as kind -> kind
166 ;;
167
168 let disambiguate_auto_params 
169   disambiguate_term metasenv context (oterms, params) 
170 =
171   match oterms with 
172     | None -> metasenv, (None, params)
173     | Some terms ->
174         let metasenv, terms = 
175           List.fold_right 
176             (fun t (metasenv, terms) ->
177                let metasenv,t = disambiguate_term context metasenv t in
178                  metasenv,t::terms) terms (metasenv, [])
179         in
180           metasenv, (Some terms, params)
181 ;;
182
183 let disambiguate_just disambiguate_term context metasenv =
184  function
185     `Term t ->
186       let metasenv,t = disambiguate_term context metasenv t in
187        metasenv, `Term t
188   | `Auto params ->
189       let metasenv,params = disambiguate_auto_params disambiguate_term metasenv
190        context params
191       in
192        metasenv, `Auto params
193 ;;
194       
195 let disambiguate_nobj estatus ?baseuri (text,prefix_len,obj) =
196   let uri =
197    let baseuri = 
198      match baseuri with Some x -> x | None -> raise BaseUriNotSetYet
199    in
200    let name = 
201      match obj with
202      | NotationPt.Inductive (_,(name,_,_,_)::_)
203      | NotationPt.Record (_,name,_,_) -> name ^ ".ind"
204      | NotationPt.Theorem (_,name,_,_,_) -> name ^ ".con"
205      | NotationPt.Inductive _ -> assert false
206    in
207      NUri.uri_of_string (baseuri ^ "/" ^ name)
208   in
209   let diff, _, _, cic =
210    singleton "third"
211     (NCicDisambiguate.disambiguate_obj
212       ~lookup_in_library:nlookup_in_library
213       ~description_of_alias:GrafiteAst.description_of_alias
214       ~mk_choice:(ncic_mk_choice estatus)
215       ~mk_implicit ~fix_instance
216       ~uri
217       ~rdb:estatus
218       ~aliases:estatus#lstatus.LexiconTypes.aliases
219       ~universe:(Some estatus#lstatus.LexiconTypes.multi_aliases) 
220       (text,prefix_len,obj)) in
221   let estatus =
222    LexiconEngine.set_proof_aliases estatus ~implicit_aliases:true
223     GrafiteAst.WithPreferences diff
224   in
225    estatus, cic
226 ;;