]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/grafite_parser/grafiteDisambiguate.ml
7510c1cc59f1bb61b7b310fb1c33561d6527a3ce
[helm.git] / helm / software / 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 exception BaseUriNotSetYet
29
30 type tactic = 
31  (CicNotationPt.term, CicNotationPt.term, 
32   CicNotationPt.term GrafiteAst.reduction, string) 
33    GrafiteAst.tactic
34    
35 type lazy_tactic = 
36   (Cic.term, Cic.lazy_term, Cic.lazy_term GrafiteAst.reduction, string) 
37     GrafiteAst.tactic
38
39 let singleton msg = function
40   | [x], _ -> x
41   | l, _   ->
42       let debug = 
43          Printf.sprintf "GrafiteDisambiguate.singleton (%s): %u interpretations"
44          msg (List.length l)
45       in
46       prerr_endline debug; assert false
47
48 let __Implicit = "__Implicit__"
49 let __Closed_Implicit = "__Closed_Implicit__"
50
51 let cic_mk_choice = function
52   | LexiconAst.Symbol_alias (name, _, dsc) ->
53      if name = __Implicit then
54        dsc, `Sym_interp (fun _ -> Cic.Implicit None)
55      else if name = __Closed_Implicit then 
56        dsc, `Sym_interp (fun _ -> Cic.Implicit (Some `Closed))
57      else
58        DisambiguateChoices.cic_lookup_symbol_by_dsc name dsc
59   | LexiconAst.Number_alias (_, dsc) ->
60      DisambiguateChoices.lookup_num_by_dsc dsc
61   | LexiconAst.Ident_alias (name, uri) -> 
62      uri, `Sym_interp 
63       (fun l->assert(l = []);CicUtil.term_of_uri (UriManager.uri_of_string uri))
64 ;;
65
66 let ncic_mk_choice = function
67   | LexiconAst.Symbol_alias (name, _, dsc) ->
68      if name = __Implicit then
69        dsc, `Sym_interp (fun _ -> NCic.Implicit `Term)
70      else if name = __Closed_Implicit then 
71        dsc, `Sym_interp (fun _ -> NCic.Implicit `Closed)
72      else
73        DisambiguateChoices.lookup_symbol_by_dsc 
74         ~mk_implicit:(function 
75            | true -> NCic.Implicit `Closed
76            | false -> NCic.Implicit `Term)
77         ~mk_appl:(function 
78            (NCic.Appl l)::tl -> NCic.Appl (l@tl) | l -> NCic.Appl l)
79         ~term_of_uri:(fun uri ->
80            fst (OCic2NCic.convert_term uri (CicUtil.term_of_uri uri)))
81         ~term_of_nref:(fun nref -> NCic.Const nref)
82        name dsc
83   | LexiconAst.Number_alias (_, dsc) -> 
84      (try
85        let desc,f = DisambiguateChoices.nlookup_num_by_dsc dsc in
86         desc, `Num_interp
87          (fun num -> match f with `Num_interp f -> f num | _ -> assert false)
88       with
89        DisambiguateChoices.Choice_not_found _ ->
90         let desc,f = DisambiguateChoices.lookup_num_by_dsc dsc in
91         desc, `Num_interp
92          (fun num -> 
93             fst (OCic2NCic.convert_term 
94               (UriManager.uri_of_string "cic:/xxx/x.con") 
95               (match f with `Num_interp f -> f num | _ -> assert false))))
96   | LexiconAst.Ident_alias (name, uri) -> 
97      uri, `Sym_interp 
98       (fun l->assert(l = []);
99         try
100          let nref = NReference.reference_of_string uri in
101           NCic.Const nref
102         with
103          NReference.IllFormedReference _ ->
104           let uri = UriManager.uri_of_string uri in
105            fst (OCic2NCic.convert_term uri (CicUtil.term_of_uri uri)))
106 ;;
107
108
109 let mk_implicit b =
110   match b with
111   | false -> 
112       LexiconAst.Symbol_alias (__Implicit,-1,"Fake Implicit")
113   | true -> 
114       LexiconAst.Symbol_alias (__Closed_Implicit,-1,"Fake Closed Implicit")
115 ;;
116
117 let lookup_in_library 
118   interactive_user_uri_choice input_or_locate_uri item 
119 =
120   let mk_ident_alias id u =
121     LexiconAst.Ident_alias (id,UriManager.string_of_uri u)
122   in
123   let mk_num_alias instance = 
124     List.map 
125      (fun dsc,_ -> LexiconAst.Number_alias (instance,dsc)) 
126      (DisambiguateChoices.lookup_num_choices())
127   in
128   let mk_symbol_alias symb ino (dsc, _,_) =
129      LexiconAst.Symbol_alias (symb,ino,dsc)
130   in
131   let dbd = LibraryDb.instance () in
132   let choices_of_id id =
133     let uris = Whelp.locate ~dbd id in
134      match uris with
135       | [] ->
136          (match 
137            (input_or_locate_uri 
138              ~title:("URI matching \"" ^ id ^ "\" unknown.") 
139              ?id:(Some id) ()) 
140          with
141          | None -> []
142          | Some uri -> [uri])
143       | [uri] -> [uri]
144       | _ ->
145           interactive_user_uri_choice ~selection_mode:`MULTIPLE
146            ?ok:(Some "Try selected.") 
147            ?enable_button_for_non_vars:(Some true)
148            ~title:"Ambiguous input."
149            ~msg: ("Ambiguous input \"" ^ id ^
150               "\". Please, choose one or more interpretations:")
151            ~id
152            uris
153   in
154   match item with
155   | DisambiguateTypes.Id id -> 
156       let uris = choices_of_id id in
157       List.map (mk_ident_alias id) uris
158   | DisambiguateTypes.Symbol (symb, ino) ->
159    (try
160      List.map (mk_symbol_alias symb ino) 
161       (TermAcicContent.lookup_interpretations symb)
162     with
163      TermAcicContent.Interpretation_not_found -> [])
164   | DisambiguateTypes.Num instance -> mk_num_alias instance
165 ;;
166
167 let nlookup_in_library 
168   interactive_user_uri_choice input_or_locate_uri item 
169 =
170   match item with
171   | DisambiguateTypes.Id id -> 
172      (try
173        let references = NCicLibrary.resolve id in
174         List.map
175          (fun u -> LexiconAst.Ident_alias (id,NReference.string_of_reference u)
176          ) references @
177         lookup_in_library interactive_user_uri_choice input_or_locate_uri item
178       with
179        NCicEnvironment.ObjectNotFound _ ->
180         lookup_in_library interactive_user_uri_choice input_or_locate_uri item)
181   | _ -> lookup_in_library interactive_user_uri_choice input_or_locate_uri item 
182 ;;
183
184 let fix_instance item l =
185  match item with
186     DisambiguateTypes.Symbol (_,n) ->
187      List.map
188       (function
189           LexiconAst.Symbol_alias (s,_,d) -> LexiconAst.Symbol_alias (s,n,d)
190         | _ -> assert false
191       ) l
192   | DisambiguateTypes.Num n ->
193      List.map
194       (function
195           LexiconAst.Number_alias (_,d) -> LexiconAst.Number_alias (n,d)
196         | _ -> assert false
197       ) l
198   | DisambiguateTypes.Id _ -> l
199 ;;
200
201
202   (** @param term not meaningful when context is given *)
203 let disambiguate_term expty text prefix_len lexicon_status_ref context metasenv
204 term =
205   let lexicon_status = !lexicon_status_ref in
206   let (diff, metasenv, subst, cic, _) =
207     singleton "first"
208       (CicDisambiguate.disambiguate_term
209         ~aliases:lexicon_status#lstatus.LexiconEngine.aliases
210         ~expty ~universe:(Some lexicon_status#lstatus.LexiconEngine.multi_aliases)
211         ~lookup_in_library
212         ~mk_choice:cic_mk_choice
213         ~mk_implicit ~fix_instance
214         ~description_of_alias:LexiconAst.description_of_alias
215         ~context ~metasenv ~subst:[] (text,prefix_len,term))
216   in
217   let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
218   lexicon_status_ref := lexicon_status;
219   metasenv,(*subst,*) cic
220 ;;
221
222 let disambiguate_nterm expty estatus context metasenv subst thing
223 =
224   let diff, metasenv, subst, cic =
225     singleton "first"
226       (NCicDisambiguate.disambiguate_term
227         ~rdb:estatus
228         ~aliases:estatus#lstatus.LexiconEngine.aliases
229         ~expty 
230         ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases)
231         ~lookup_in_library:nlookup_in_library
232         ~mk_choice:ncic_mk_choice
233         ~mk_implicit ~fix_instance
234         ~description_of_alias:LexiconAst.description_of_alias
235         ~context ~metasenv ~subst thing)
236   in
237   let estatus = LexiconEngine.set_proof_aliases estatus diff in
238    metasenv, subst, estatus, cic
239 ;;
240
241
242   (** disambiguate_lazy_term (circa): term -> (unit -> status) * lazy_term
243    * rationale: lazy_term will be invoked in different context to obtain a term,
244    * each invocation will disambiguate the term and can add aliases. Once all
245    * disambiguations have been performed, the first returned function can be
246    * used to obtain the resulting aliases *)
247 let disambiguate_lazy_term expty text prefix_len lexicon_status_ref term =
248   (fun context metasenv ugraph ->
249     let lexicon_status = !lexicon_status_ref in
250     let (diff, metasenv, _, cic, ugraph) =
251       singleton "second"
252         (CicDisambiguate.disambiguate_term 
253           ~lookup_in_library
254           ~mk_choice:cic_mk_choice
255           ~mk_implicit ~fix_instance
256           ~description_of_alias:LexiconAst.description_of_alias
257           ~initial_ugraph:ugraph ~aliases:lexicon_status#lstatus.LexiconEngine.aliases
258           ~universe:(Some lexicon_status#lstatus.LexiconEngine.multi_aliases)
259           ~context ~metasenv ~subst:[] 
260           (text,prefix_len,term) ~expty) in
261     let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
262     lexicon_status_ref := lexicon_status;
263     cic, metasenv, ugraph)
264 ;;
265
266 let disambiguate_pattern 
267   text prefix_len lexicon_status_ref (wanted, hyp_paths, goal_path) 
268 =
269   let interp path =CicDisambiguate.interpretate_path [] path in
270   let goal_path = HExtlib.map_option interp goal_path in
271   let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
272   let wanted =
273    match wanted with
274       None -> None
275     | Some wanted ->
276        let wanted = 
277          disambiguate_lazy_term None text prefix_len lexicon_status_ref wanted 
278        in
279        Some wanted
280   in
281   (wanted, hyp_paths, goal_path)
282 ;;
283
284 type pattern = 
285   CicNotationPt.term Disambiguate.disambiguator_input option * 
286   (string * NCic.term) list * NCic.term option
287
288 let disambiguate_npattern (text, prefix_len, (wanted, hyp_paths, goal_path)) =
289   let interp path = NCicDisambiguate.disambiguate_path path in
290   let goal_path = HExtlib.map_option interp goal_path in
291   let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
292   let wanted = 
293     match wanted with None -> None | Some x -> Some (text,prefix_len,x)
294   in
295    (wanted, hyp_paths, goal_path)
296 ;;
297
298 let disambiguate_reduction_kind text prefix_len lexicon_status_ref = function
299   | `Unfold (Some t) ->
300       let t = 
301          disambiguate_lazy_term None text prefix_len lexicon_status_ref t in
302       `Unfold (Some t)
303   | `Normalize
304   | `Simpl
305   | `Unfold None
306   | `Whd as kind -> kind
307 ;;
308
309 let disambiguate_auto_params 
310   disambiguate_term metasenv context (oterms, params) 
311 =
312   match oterms with 
313     | None -> metasenv, (None, params)
314     | Some terms ->
315         let metasenv, terms = 
316           List.fold_right 
317             (fun t (metasenv, terms) ->
318                let metasenv,t = disambiguate_term context metasenv t in
319                  metasenv,t::terms) terms (metasenv, [])
320         in
321           metasenv, (Some terms, params)
322 ;;
323
324 let disambiguate_just disambiguate_term context metasenv =
325  function
326     `Term t ->
327       let metasenv,t = disambiguate_term context metasenv t in
328        metasenv, `Term t
329   | `Auto params ->
330       let metasenv,params = disambiguate_auto_params disambiguate_term metasenv
331        context params
332       in
333        metasenv, `Auto params
334 ;;
335       
336 let rec disambiguate_tactic 
337   lexicon_status_ref context metasenv goal (text,prefix_len,tactic) 
338 =
339   let disambiguate_term_hint = 
340     let _,_,expty = 
341       List.find (fun (x,_,_) -> Some x = goal) metasenv
342     in
343     disambiguate_term (Some expty) text prefix_len lexicon_status_ref in
344   let disambiguate_term = 
345     disambiguate_term None text prefix_len lexicon_status_ref in
346   let disambiguate_pattern = 
347     disambiguate_pattern text prefix_len lexicon_status_ref in
348   let disambiguate_reduction_kind = 
349     disambiguate_reduction_kind text prefix_len lexicon_status_ref in
350   let disambiguate_lazy_term = 
351     disambiguate_lazy_term None text prefix_len lexicon_status_ref in
352   let disambiguate_tactic metasenv tac =
353    disambiguate_tactic lexicon_status_ref context metasenv goal (text,prefix_len,tac)
354   in
355   let disambiguate_auto_params m p = 
356     disambiguate_auto_params disambiguate_term m context p
357   in
358    match tactic with
359     (* Higher  order tactics *)
360     | GrafiteAst.Progress (loc,tac) ->
361         let metasenv,tac = disambiguate_tactic metasenv tac in
362         metasenv,GrafiteAst.Progress (loc,tac)
363     | GrafiteAst.Solve (loc,tacl) ->
364         let metasenv,tacl =
365          List.fold_right
366           (fun tac (metasenv,tacl) ->
367             let metasenv,tac = disambiguate_tactic metasenv tac in
368              metasenv,tac::tacl
369           ) tacl (metasenv,[])
370         in
371          metasenv,GrafiteAst.Solve (loc,tacl)
372     | GrafiteAst.Try (loc,tac) ->
373         let metasenv,tac = disambiguate_tactic metasenv tac in
374         metasenv,GrafiteAst.Try (loc,tac)
375     | GrafiteAst.First (loc,tacl) ->
376         let metasenv,tacl =
377          List.fold_right
378           (fun tac (metasenv,tacl) ->
379             let metasenv,tac = disambiguate_tactic metasenv tac in
380              metasenv,tac::tacl
381           ) tacl (metasenv,[])
382         in
383          metasenv,GrafiteAst.First (loc,tacl)
384     | GrafiteAst.Seq (loc,tacl) ->
385         let metasenv,tacl =
386          List.fold_right
387           (fun tac (metasenv,tacl) ->
388             let metasenv,tac = disambiguate_tactic metasenv tac in
389              metasenv,tac::tacl
390           ) tacl (metasenv,[])
391         in
392          metasenv,GrafiteAst.Seq (loc,tacl)
393     | GrafiteAst.Repeat (loc,tac) ->
394         let metasenv,tac = disambiguate_tactic metasenv tac in
395         metasenv,GrafiteAst.Repeat (loc,tac)
396     | GrafiteAst.Do (loc,n,tac) ->
397         let metasenv,tac = disambiguate_tactic metasenv tac in
398         metasenv,GrafiteAst.Do (loc,n,tac)
399     | GrafiteAst.Then (loc,tac,tacl) ->
400         let metasenv,tac = disambiguate_tactic metasenv tac in
401         let metasenv,tacl =
402          List.fold_right
403           (fun tac (metasenv,tacl) ->
404             let metasenv,tac = disambiguate_tactic metasenv tac in
405              metasenv,tac::tacl
406           ) tacl (metasenv,[])
407         in
408          metasenv,GrafiteAst.Then (loc,tac,tacl)
409     (* First order tactics *)
410     | GrafiteAst.Absurd (loc, term) -> 
411         let metasenv,cic = disambiguate_term context metasenv term in
412         metasenv,GrafiteAst.Absurd (loc, cic)
413     | GrafiteAst.Apply (loc, term) ->
414         let metasenv,cic = disambiguate_term context metasenv term in
415         metasenv,GrafiteAst.Apply (loc, cic)
416     | GrafiteAst.ApplyRule (loc, term) ->
417         let metasenv,cic = disambiguate_term_hint context metasenv term in
418         metasenv,GrafiteAst.ApplyRule (loc, cic)
419     | GrafiteAst.ApplyP (loc, term) ->
420         let metasenv,cic = disambiguate_term context metasenv term in
421         metasenv,GrafiteAst.ApplyP (loc, cic)
422     | GrafiteAst.ApplyS (loc, term, params) ->
423         let metasenv, params = disambiguate_auto_params metasenv params in
424         let metasenv,cic = disambiguate_term context metasenv term in
425         metasenv,GrafiteAst.ApplyS (loc, cic, params)
426     | GrafiteAst.Assumption loc ->
427         metasenv,GrafiteAst.Assumption loc
428     | GrafiteAst.AutoBatch (loc,params) ->
429         let metasenv, params = disambiguate_auto_params metasenv params in
430         metasenv,GrafiteAst.AutoBatch (loc,params)
431     | GrafiteAst.Cases (loc, what, pattern, idents) ->
432         let metasenv,what = disambiguate_term context metasenv what in
433         let pattern = disambiguate_pattern pattern in
434         metasenv,GrafiteAst.Cases (loc, what, pattern, idents)
435     | GrafiteAst.Change (loc, pattern, with_what) -> 
436         let with_what = disambiguate_lazy_term with_what in
437         let pattern = disambiguate_pattern pattern in
438         metasenv,GrafiteAst.Change (loc, pattern, with_what)
439     | GrafiteAst.Clear (loc,id) ->
440         metasenv,GrafiteAst.Clear (loc,id)
441     | GrafiteAst.ClearBody (loc,id) ->
442        metasenv,GrafiteAst.ClearBody (loc,id)
443     | GrafiteAst.Compose (loc, t1, t2, times, spec) ->
444         let metasenv,t1 = disambiguate_term context metasenv t1 in
445         let metasenv,t2 = 
446           match t2 with
447           | None -> metasenv, None
448           | Some t2 -> 
449               let m, t2 = disambiguate_term context metasenv t2 in
450               m, Some t2
451         in
452         metasenv,   GrafiteAst.Compose (loc, t1, t2, times, spec)
453     | GrafiteAst.Constructor (loc,n) ->
454         metasenv,GrafiteAst.Constructor (loc,n)
455     | GrafiteAst.Contradiction loc ->
456         metasenv,GrafiteAst.Contradiction loc
457     | GrafiteAst.Cut (loc, ident, term) -> 
458         let metasenv,cic = disambiguate_term context metasenv term in
459         metasenv,GrafiteAst.Cut (loc, ident, cic)
460     | GrafiteAst.Decompose (loc, names) ->
461          metasenv,GrafiteAst.Decompose (loc, names)
462     | GrafiteAst.Demodulate (loc, params) ->
463         let metasenv, params = disambiguate_auto_params metasenv params in
464         metasenv,GrafiteAst.Demodulate (loc, params)
465     | GrafiteAst.Destruct (loc, Some terms) ->
466         let map term (metasenv, terms) =
467            let metasenv, term = disambiguate_term context metasenv term in
468            metasenv, term :: terms
469         in
470         let metasenv, terms = List.fold_right map terms (metasenv, []) in 
471         metasenv, GrafiteAst.Destruct(loc, Some terms)
472     | GrafiteAst.Destruct (loc, None) ->
473         metasenv,GrafiteAst.Destruct(loc,None)
474     | GrafiteAst.Exact (loc, term) -> 
475         let metasenv,cic = disambiguate_term context metasenv term in
476         metasenv,GrafiteAst.Exact (loc, cic)
477     | GrafiteAst.Elim (loc, what, Some using, pattern, specs) ->
478         let metasenv,what = disambiguate_term context metasenv what in
479         let metasenv,using = disambiguate_term context metasenv using in
480         let pattern = disambiguate_pattern pattern in
481         metasenv,GrafiteAst.Elim (loc, what, Some using, pattern, specs)
482     | GrafiteAst.Elim (loc, what, None, pattern, specs) ->
483         let metasenv,what = disambiguate_term context metasenv what in
484         let pattern = disambiguate_pattern pattern in
485         metasenv,GrafiteAst.Elim (loc, what, None, pattern, specs)
486     | GrafiteAst.ElimType (loc, what, Some using, specs) ->
487         let metasenv,what = disambiguate_term context metasenv what in
488         let metasenv,using = disambiguate_term context metasenv using in
489         metasenv,GrafiteAst.ElimType (loc, what, Some using, specs)
490     | GrafiteAst.ElimType (loc, what, None, specs) ->
491         let metasenv,what = disambiguate_term context metasenv what in
492         metasenv,GrafiteAst.ElimType (loc, what, None, specs)
493     | GrafiteAst.Exists loc ->
494         metasenv,GrafiteAst.Exists loc 
495     | GrafiteAst.Fail loc ->
496         metasenv,GrafiteAst.Fail loc
497     | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
498         let pattern = disambiguate_pattern pattern in
499         let term = disambiguate_lazy_term term in
500         let red_kind = disambiguate_reduction_kind red_kind in
501         metasenv,GrafiteAst.Fold (loc, red_kind, term, pattern)
502     | GrafiteAst.FwdSimpl (loc, hyp, names) ->
503        metasenv,GrafiteAst.FwdSimpl (loc, hyp, names)  
504     | GrafiteAst.Fourier loc ->
505        metasenv,GrafiteAst.Fourier loc
506     | GrafiteAst.Generalize (loc,pattern,ident) ->
507         let pattern = disambiguate_pattern pattern in
508         metasenv,GrafiteAst.Generalize (loc,pattern,ident)
509     | GrafiteAst.IdTac loc ->
510         metasenv,GrafiteAst.IdTac loc
511     | GrafiteAst.Intros (loc, specs) ->
512         metasenv,GrafiteAst.Intros (loc, specs)
513     | GrafiteAst.Inversion (loc, term) ->
514        let metasenv,term = disambiguate_term context metasenv term in
515         metasenv,GrafiteAst.Inversion (loc, term)
516     | GrafiteAst.LApply (loc, linear, depth, to_what, what, ident) ->
517        let f term (metasenv, to_what) =
518           let metasenv, term = disambiguate_term context metasenv term in
519           metasenv, term :: to_what
520        in
521        let metasenv, to_what = List.fold_right f to_what (metasenv, []) in 
522        let metasenv, what = disambiguate_term context metasenv what in
523        metasenv,GrafiteAst.LApply (loc, linear, depth, to_what, what, ident)
524     | GrafiteAst.Left loc ->
525        metasenv,GrafiteAst.Left loc
526     | GrafiteAst.LetIn (loc, term, name) ->
527         let metasenv,term = disambiguate_term context metasenv term in
528         metasenv,GrafiteAst.LetIn (loc,term,name)
529     | GrafiteAst.Reduce (loc, red_kind, pattern) ->
530         let pattern = disambiguate_pattern pattern in
531         let red_kind = disambiguate_reduction_kind red_kind in
532         metasenv,GrafiteAst.Reduce(loc, red_kind, pattern)
533     | GrafiteAst.Reflexivity loc ->
534         metasenv,GrafiteAst.Reflexivity loc
535     | GrafiteAst.Replace (loc, pattern, with_what) -> 
536         let pattern = disambiguate_pattern pattern in
537         let with_what = disambiguate_lazy_term with_what in
538         metasenv,GrafiteAst.Replace (loc, pattern, with_what)
539     | GrafiteAst.Rewrite (loc, dir, t, pattern, names) ->
540         let metasenv,term = disambiguate_term context metasenv t in
541         let pattern = disambiguate_pattern pattern in
542         metasenv,GrafiteAst.Rewrite (loc, dir, term, pattern, names)
543     | GrafiteAst.Right loc ->
544         metasenv,GrafiteAst.Right loc
545     | GrafiteAst.Ring loc ->
546         metasenv,GrafiteAst.Ring loc
547     | GrafiteAst.Split loc ->
548         metasenv,GrafiteAst.Split loc
549     | GrafiteAst.Symmetry loc ->
550         metasenv,GrafiteAst.Symmetry loc
551     | GrafiteAst.Transitivity (loc, term) -> 
552         let metasenv,cic = disambiguate_term context metasenv term in
553         metasenv,GrafiteAst.Transitivity (loc, cic)
554       (* Nuovi casi *)
555     | GrafiteAst.Assume (loc, id, term) -> 
556         let metasenv,cic = disambiguate_term context metasenv term in
557         metasenv,GrafiteAst.Assume (loc, id, cic)
558     | GrafiteAst.Suppose (loc, term, id, term') ->
559         let metasenv,cic = disambiguate_term context metasenv term in
560         let metasenv,cic' =
561            match term' with
562               None -> metasenv,None
563             | Some t ->
564                   let metasenv,t = disambiguate_term context metasenv t in
565                   metasenv,Some t in
566         metasenv,GrafiteAst.Suppose (loc, cic, id, cic')
567     | GrafiteAst.Bydone (loc,just) ->
568         let metasenv,just =
569          disambiguate_just disambiguate_term context metasenv just
570         in
571          metasenv,GrafiteAst.Bydone (loc, just)
572     | GrafiteAst.We_need_to_prove (loc,term,id,term') ->
573         let metasenv,cic = disambiguate_term context metasenv term in
574         let metasenv,cic' = 
575             match term' with
576               None -> metasenv,None
577             | Some t ->
578                   let metasenv,t = disambiguate_term context metasenv t in
579                   metasenv,Some t in
580         metasenv,GrafiteAst.We_need_to_prove (loc,cic,id,cic')
581     | GrafiteAst.By_just_we_proved (loc,just,term',id,term'') ->
582         let metasenv,just =
583          disambiguate_just disambiguate_term context metasenv just in
584         let metasenv,cic' = disambiguate_term context metasenv term' in
585         let metasenv,cic'' = 
586             match term'' with
587               None -> metasenv,None
588            |  Some t ->  
589                     let metasenv,t = disambiguate_term context metasenv t in
590                      metasenv,Some t in
591         metasenv,GrafiteAst.By_just_we_proved (loc,just,cic',id,cic'')
592     | GrafiteAst.We_proceed_by_cases_on (loc, term, term') ->
593         let metasenv,cic = disambiguate_term context metasenv term in
594         let metasenv,cic' = disambiguate_term context metasenv term' in
595         metasenv,GrafiteAst.We_proceed_by_cases_on (loc, cic, cic')
596     | GrafiteAst.We_proceed_by_induction_on (loc, term, term') ->
597         let metasenv,cic = disambiguate_term context metasenv term in
598         let metasenv,cic' = disambiguate_term context metasenv term' in
599         metasenv,GrafiteAst.We_proceed_by_induction_on (loc, cic, cic')
600    | GrafiteAst.Byinduction (loc, term, id) ->
601         let metasenv,cic = disambiguate_term context metasenv term in
602         metasenv,GrafiteAst.Byinduction(loc, cic, id)
603    | GrafiteAst.Thesisbecomes (loc, term) ->
604         let metasenv,cic = disambiguate_term context metasenv term in
605         metasenv,GrafiteAst.Thesisbecomes (loc, cic)
606    | GrafiteAst.ExistsElim (loc, just, id1, term1, id2, term2) ->
607         let metasenv,just =
608          disambiguate_just disambiguate_term context metasenv just in
609         let metasenv,cic' = disambiguate_term context metasenv term1 in
610         let cic''= disambiguate_lazy_term term2 in
611         metasenv,GrafiteAst.ExistsElim(loc, just, id1, cic', id2, cic'')
612    | GrafiteAst.AndElim (loc, just, id, term1, id1, term2) ->
613         let metasenv,just =
614          disambiguate_just disambiguate_term context metasenv just in
615         let metasenv,cic'= disambiguate_term context metasenv term1 in
616         let metasenv,cic''= disambiguate_term context metasenv term2 in
617         metasenv,GrafiteAst.AndElim(loc, just, id, cic', id1, cic'')   
618    | GrafiteAst.Case (loc, id, params) ->
619         let metasenv,params' =
620          List.fold_right
621           (fun (id,term) (metasenv,params) ->
622             let metasenv,cic = disambiguate_term context metasenv term in
623              metasenv,(id,cic)::params
624           ) params (metasenv,[])
625         in
626         metasenv,GrafiteAst.Case(loc, id, params')   
627    | GrafiteAst.RewritingStep (loc, term1, term2, term3, cont) ->
628         let metasenv,cic =
629          match term1 with
630             None -> metasenv,None
631           | Some (start,t) -> 
632              let metasenv,t = disambiguate_term context metasenv t in
633               metasenv,Some (start,t) in
634         let metasenv,cic'= disambiguate_term context metasenv term2 in
635         let metasenv,cic'' =
636          match term3 with
637           | `SolveWith term ->
638              let metasenv,term = disambiguate_term context metasenv term in
639              metasenv, `SolveWith term
640           | `Auto params -> 
641               let metasenv, params = disambiguate_auto_params metasenv params in
642               metasenv,`Auto params
643           | `Term t -> 
644              let metasenv,t = disambiguate_term context metasenv t in
645               metasenv,`Term t
646           | `Proof as t -> metasenv,t in
647         metasenv,GrafiteAst.RewritingStep (loc, cic, cic', cic'', cont)   
648
649 let disambiguate_obj estatus ?baseuri metasenv (text,prefix_len,obj) =
650   let uri =
651    let baseuri = 
652      match baseuri with Some x -> x | None -> raise BaseUriNotSetYet
653    in
654    let name = 
655      match obj with
656      | CicNotationPt.Inductive (_,(name,_,_,_)::_)
657      | CicNotationPt.Record (_,name,_,_) -> name ^ ".ind"
658      | CicNotationPt.Theorem (_,name,_,_,_) -> name ^ ".con"
659      | CicNotationPt.Inductive _ -> assert false
660    in
661      UriManager.uri_of_string (baseuri ^ "/" ^ name)
662   in
663 (*
664  let _try_new cic =
665   (NCicLibrary.clear_cache ();
666    NCicEnvironment.invalidate ();
667    OCic2NCic.clear ();
668    let graph = 
669      match cic with
670      | Some (Cic.CurrentProof (_,metasenv, _, ty,_,_)) ->
671          let _, ugraph = 
672            CicTypeChecker.type_of_aux' metasenv [] ty CicUniv.empty_ugraph
673          in
674            ugraph
675      | Some (Cic.Constant (_,_, ty,_,_)) ->
676          let _, ugraph = 
677                  CicTypeChecker.type_of_aux' [] [] ty CicUniv.empty_ugraph
678          in
679            ugraph
680      | _ -> CicUniv.empty_ugraph
681    in
682
683 (*
684    prerr_endline "PRIMA COERCIONS";
685    let _,l = CicUniv.do_rank graph in
686    List.iter (fun k -> 
687      prerr_endline (CicUniv.string_of_universe k ^ " = " ^ string_of_int
688      (CicUniv.get_rank k))) l;
689 *)
690
691    let graph =
692        List.fold_left 
693          (fun graph (_,_,l) ->
694            List.fold_left
695              (fun graph (uri,_,_) ->
696                 let _,g = CicTypeChecker.typecheck uri in
697                 CicUniv.merge_ugraphs ~base_ugraph:graph ~increment:(g,uri))
698              graph l)
699        graph (CoercDb.to_list (CoercDb.dump ()))
700    in
701    ignore(CicUniv.do_rank graph);
702
703
704 (*
705    prerr_endline "DOPO COERCIONS";
706    let _,l = CicUniv.do_rank graph in
707    List.iter (fun k -> 
708      prerr_endline (CicUniv.string_of_universe k ^ " = " ^ string_of_int
709      (CicUniv.get_rank k))) l;
710 *)
711
712
713    prerr_endline "INIZIO NUOVA DISAMBIGUAZIONE";
714    let time = Unix.gettimeofday () in
715        (try
716          (match 
717           NCicDisambiguate.disambiguate_obj
718            ~rdb:estatus
719            ~lookup_in_library:nlookup_in_library
720            ~description_of_alias:LexiconAst.description_of_alias
721            ~mk_choice:ncic_mk_choice
722            ~mk_implicit
723            ~uri:(OCic2NCic.nuri_of_ouri uri)
724            ~aliases:estatus#lstatus.LexiconEngine.aliases
725            ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases)
726            (text,prefix_len,obj)
727          with
728          | [_,_,_,obj],_ ->
729           let time = Unix.gettimeofday () -. time in
730 (*           NCicTypeChecker.typecheck_obj obj; *)
731           prerr_endline ("NUOVA DISAMBIGUAZIONE OK: "^ string_of_float time);
732 (*
733           let obj = 
734             let u,i,m,_,o = obj in
735             u,i,m,[],o
736           in
737 *)
738           prerr_endline (NCicPp.ppobj obj)
739          | _ ->
740           prerr_endline ("NUOVA DISAMBIGUAZIONE AMBIGUO!!!!!!!!!  "))
741        with 
742        | MultiPassDisambiguator.DisambiguationError (_,s) ->
743         prerr_endline ("ERRORE NUOVA DISAMBIGUAZIONE ("
744           ^UriManager.string_of_uri uri^
745           "):\n" ^
746          String.concat "\n" 
747           (List.map (fun _,_,x,_ -> snd (Lazy.force x)) (List.flatten s)))
748 (*        | exn -> prerr_endline (Printexc.to_string exn) *)
749        )
750   )
751  in 
752 *)
753
754
755  try
756 (*   let time = Unix.gettimeofday () in  *)
757
758
759   let (diff, metasenv, _, cic, _) =
760     singleton "third"
761       (CicDisambiguate.disambiguate_obj 
762         ~lookup_in_library 
763         ~mk_choice:cic_mk_choice
764         ~mk_implicit ~fix_instance
765         ~description_of_alias:LexiconAst.description_of_alias
766         ~aliases:estatus#lstatus.LexiconEngine.aliases
767         ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases) 
768         ~uri:(Some uri)
769         (text,prefix_len,obj)) 
770   in
771
772
773 (*
774   let time = Unix.gettimeofday () -. time in
775   prerr_endline ("VECCHIA DISAMBIGUAZIONE ("^
776     UriManager.string_of_uri uri ^"): " ^ string_of_float time);
777 *)
778 (*    try_new (Some cic);   *)
779
780
781   let estatus = LexiconEngine.set_proof_aliases estatus diff in
782    estatus, metasenv, cic
783
784  with 
785  | Sys.Break as exn -> raise exn
786  | exn ->
787 (*    try_new None; *)
788    raise exn
789 ;;
790
791 let disambiguate_nobj estatus ?baseuri (text,prefix_len,obj) =
792   let uri =
793    let baseuri = 
794      match baseuri with Some x -> x | None -> raise BaseUriNotSetYet
795    in
796    let name = 
797      match obj with
798      | CicNotationPt.Inductive (_,(name,_,_,_)::_)
799      | CicNotationPt.Record (_,name,_,_) -> name ^ ".ind"
800      | CicNotationPt.Theorem (_,name,_,_,_) -> name ^ ".con"
801      | CicNotationPt.Inductive _ -> assert false
802    in
803      UriManager.uri_of_string (baseuri ^ "/" ^ name)
804   in
805   let diff, _, _, cic =
806    singleton "third"
807     (NCicDisambiguate.disambiguate_obj
808       ~lookup_in_library:nlookup_in_library
809       ~description_of_alias:LexiconAst.description_of_alias
810       ~mk_choice:ncic_mk_choice
811       ~mk_implicit ~fix_instance
812       ~uri:(OCic2NCic.nuri_of_ouri uri)
813       ~rdb:estatus
814       ~aliases:estatus#lstatus.LexiconEngine.aliases
815       ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases) 
816       (text,prefix_len,obj)) in
817   let estatus = LexiconEngine.set_proof_aliases estatus diff in
818    estatus, cic
819 ;;
820   
821 let disambiguate_command estatus ?baseuri metasenv (text,prefix_len,cmd)=
822   match cmd with
823    | GrafiteAst.Index(loc,key,uri) ->
824        let lexicon_status_ref = ref estatus in 
825        let disambiguate_term =
826          disambiguate_term None text prefix_len lexicon_status_ref [] in
827        let disambiguate_term_option metasenv =
828          function
829              None -> metasenv,None
830            | Some t ->
831                let metasenv,t = disambiguate_term metasenv t in
832                  metasenv, Some t
833        in
834        let metasenv,key = disambiguate_term_option metasenv key in
835         !lexicon_status_ref,metasenv,GrafiteAst.Index(loc,key,uri)
836    | GrafiteAst.Select (loc,uri) -> 
837         estatus, metasenv, GrafiteAst.Select(loc,uri)
838    | GrafiteAst.Pump(loc,i) -> 
839         estatus, metasenv, GrafiteAst.Pump(loc,i)
840    | GrafiteAst.PreferCoercion (loc,t) -> 
841        let lexicon_status_ref = ref estatus in 
842        let disambiguate_term =
843          disambiguate_term None text prefix_len lexicon_status_ref [] in
844       let metasenv,t = disambiguate_term metasenv t in
845        !lexicon_status_ref, metasenv, GrafiteAst.PreferCoercion (loc,t)
846    | GrafiteAst.Coercion (loc,t,b,a,s) -> 
847        let lexicon_status_ref = ref estatus in 
848        let disambiguate_term =
849          disambiguate_term None text prefix_len lexicon_status_ref [] in
850       let metasenv,t = disambiguate_term metasenv t in
851        !lexicon_status_ref, metasenv, GrafiteAst.Coercion (loc,t,b,a,s)
852    | GrafiteAst.Inverter (loc,n,indty,params) ->
853        let lexicon_status_ref = ref estatus in
854        let disambiguate_term = 
855          disambiguate_term None text prefix_len lexicon_status_ref [] in
856        let metasenv,indty = disambiguate_term metasenv indty in
857         !lexicon_status_ref, metasenv, GrafiteAst.Inverter (loc,n,indty,params)
858    | GrafiteAst.Default _
859    | GrafiteAst.Drop _
860    | GrafiteAst.Include _
861    | GrafiteAst.Print _
862    | GrafiteAst.Qed _
863    | GrafiteAst.Set _ as cmd ->
864        estatus,metasenv,cmd
865    | GrafiteAst.Obj (loc,obj) ->
866        let estatus,metasenv,obj =
867         disambiguate_obj estatus ?baseuri metasenv (text,prefix_len,obj)in
868        estatus, metasenv, GrafiteAst.Obj (loc,obj)
869    | GrafiteAst.Relation (loc,id,a,aeq,refl,sym,trans) ->
870       let lexicon_status_ref = ref estatus in 
871       let disambiguate_term =
872        disambiguate_term None text prefix_len lexicon_status_ref [] in
873       let disambiguate_term_option metasenv =
874        function
875           None -> metasenv,None
876        | Some t ->
877           let metasenv,t = disambiguate_term metasenv t in
878            metasenv, Some t
879       in
880       let metasenv,a = disambiguate_term metasenv a in
881       let metasenv,aeq = disambiguate_term metasenv aeq in
882       let metasenv,refl = disambiguate_term_option metasenv refl in
883       let metasenv,sym = disambiguate_term_option metasenv sym in
884       let metasenv,trans = disambiguate_term_option metasenv trans in
885        !lexicon_status_ref, metasenv,
886         GrafiteAst.Relation (loc,id,a,aeq,refl,sym,trans)
887
888 let disambiguate_macro 
889   lexicon_status_ref metasenv context (text,prefix_len, macro) 
890 =
891  let disambiguate_term = disambiguate_term None text prefix_len lexicon_status_ref in
892   let disambiguate_reduction_kind = 
893     disambiguate_reduction_kind text prefix_len lexicon_status_ref in
894   match macro with
895    | GrafiteAst.WMatch (loc,term) ->
896       let metasenv,term = disambiguate_term context metasenv term in
897        metasenv,GrafiteAst.WMatch (loc,term)
898    | GrafiteAst.WInstance (loc,term) ->
899       let metasenv,term = disambiguate_term context metasenv term in
900        metasenv,GrafiteAst.WInstance (loc,term)
901    | GrafiteAst.WElim (loc,term) ->
902       let metasenv,term = disambiguate_term context metasenv term in
903        metasenv,GrafiteAst.WElim (loc,term)
904    | GrafiteAst.WHint (loc,term) ->
905       let metasenv,term = disambiguate_term context metasenv term in
906        metasenv,GrafiteAst.WHint (loc,term)
907    | GrafiteAst.Check (loc,term) ->
908       let metasenv,term = disambiguate_term context metasenv term in
909        metasenv,GrafiteAst.Check (loc,term)
910    | GrafiteAst.Eval (loc,kind,term) ->
911       let metasenv, term = disambiguate_term context metasenv term in
912       let kind = disambiguate_reduction_kind kind in
913        metasenv,GrafiteAst.Eval (loc,kind,term)
914    | GrafiteAst.AutoInteractive (loc, params) -> 
915       let metasenv, params = 
916         disambiguate_auto_params disambiguate_term metasenv context params in
917       metasenv, GrafiteAst.AutoInteractive (loc, params)
918    | GrafiteAst.Hint _
919    | GrafiteAst.WLocate _
920    | GrafiteAst.Inline _ as macro ->
921       metasenv,macro