]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/grafite_parser/grafiteDisambiguate.ml
Fixed a bug in deep_eq: we generated new clauses but neglected the
[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       HLog.debug 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 (terms, params) 
311 =
312     let metasenv, terms = 
313       List.fold_right 
314        (fun t (metasenv, terms) ->
315          let metasenv,t = disambiguate_term context metasenv t in
316          metasenv,t::terms) terms (metasenv, [])
317     in
318     metasenv, (terms, params)
319 ;;
320
321 let disambiguate_just disambiguate_term context metasenv =
322  function
323     `Term t ->
324       let metasenv,t = disambiguate_term context metasenv t in
325        metasenv, `Term t
326   | `Auto params ->
327       let metasenv,params = disambiguate_auto_params disambiguate_term metasenv
328        context params
329       in
330        metasenv, `Auto params
331 ;;
332       
333 let rec disambiguate_tactic 
334   lexicon_status_ref context metasenv goal (text,prefix_len,tactic) 
335 =
336   let disambiguate_term_hint = 
337     let _,_,expty = 
338       List.find (fun (x,_,_) -> Some x = goal) metasenv
339     in
340     disambiguate_term (Some expty) text prefix_len lexicon_status_ref in
341   let disambiguate_term = 
342     disambiguate_term None text prefix_len lexicon_status_ref in
343   let disambiguate_pattern = 
344     disambiguate_pattern text prefix_len lexicon_status_ref in
345   let disambiguate_reduction_kind = 
346     disambiguate_reduction_kind text prefix_len lexicon_status_ref in
347   let disambiguate_lazy_term = 
348     disambiguate_lazy_term None text prefix_len lexicon_status_ref in
349   let disambiguate_tactic metasenv tac =
350    disambiguate_tactic lexicon_status_ref context metasenv goal (text,prefix_len,tac)
351   in
352   let disambiguate_auto_params m p = 
353     disambiguate_auto_params disambiguate_term m context p
354   in
355    match tactic with
356     (* Higher  order tactics *)
357     | GrafiteAst.Progress (loc,tac) ->
358         let metasenv,tac = disambiguate_tactic metasenv tac in
359         metasenv,GrafiteAst.Progress (loc,tac)
360     | GrafiteAst.Solve (loc,tacl) ->
361         let metasenv,tacl =
362          List.fold_right
363           (fun tac (metasenv,tacl) ->
364             let metasenv,tac = disambiguate_tactic metasenv tac in
365              metasenv,tac::tacl
366           ) tacl (metasenv,[])
367         in
368          metasenv,GrafiteAst.Solve (loc,tacl)
369     | GrafiteAst.Try (loc,tac) ->
370         let metasenv,tac = disambiguate_tactic metasenv tac in
371         metasenv,GrafiteAst.Try (loc,tac)
372     | GrafiteAst.First (loc,tacl) ->
373         let metasenv,tacl =
374          List.fold_right
375           (fun tac (metasenv,tacl) ->
376             let metasenv,tac = disambiguate_tactic metasenv tac in
377              metasenv,tac::tacl
378           ) tacl (metasenv,[])
379         in
380          metasenv,GrafiteAst.First (loc,tacl)
381     | GrafiteAst.Seq (loc,tacl) ->
382         let metasenv,tacl =
383          List.fold_right
384           (fun tac (metasenv,tacl) ->
385             let metasenv,tac = disambiguate_tactic metasenv tac in
386              metasenv,tac::tacl
387           ) tacl (metasenv,[])
388         in
389          metasenv,GrafiteAst.Seq (loc,tacl)
390     | GrafiteAst.Repeat (loc,tac) ->
391         let metasenv,tac = disambiguate_tactic metasenv tac in
392         metasenv,GrafiteAst.Repeat (loc,tac)
393     | GrafiteAst.Do (loc,n,tac) ->
394         let metasenv,tac = disambiguate_tactic metasenv tac in
395         metasenv,GrafiteAst.Do (loc,n,tac)
396     | GrafiteAst.Then (loc,tac,tacl) ->
397         let metasenv,tac = disambiguate_tactic metasenv tac in
398         let metasenv,tacl =
399          List.fold_right
400           (fun tac (metasenv,tacl) ->
401             let metasenv,tac = disambiguate_tactic metasenv tac in
402              metasenv,tac::tacl
403           ) tacl (metasenv,[])
404         in
405          metasenv,GrafiteAst.Then (loc,tac,tacl)
406     (* First order tactics *)
407     | GrafiteAst.Absurd (loc, term) -> 
408         let metasenv,cic = disambiguate_term context metasenv term in
409         metasenv,GrafiteAst.Absurd (loc, cic)
410     | GrafiteAst.Apply (loc, term) ->
411         let metasenv,cic = disambiguate_term context metasenv term in
412         metasenv,GrafiteAst.Apply (loc, cic)
413     | GrafiteAst.ApplyRule (loc, term) ->
414         let metasenv,cic = disambiguate_term_hint context metasenv term in
415         metasenv,GrafiteAst.ApplyRule (loc, cic)
416     | GrafiteAst.ApplyP (loc, term) ->
417         let metasenv,cic = disambiguate_term context metasenv term in
418         metasenv,GrafiteAst.ApplyP (loc, cic)
419     | GrafiteAst.ApplyS (loc, term, params) ->
420         let metasenv, params = disambiguate_auto_params metasenv params in
421         let metasenv,cic = disambiguate_term context metasenv term in
422         metasenv,GrafiteAst.ApplyS (loc, cic, params)
423     | GrafiteAst.Assumption loc ->
424         metasenv,GrafiteAst.Assumption loc
425     | GrafiteAst.AutoBatch (loc,params) ->
426         let metasenv, params = disambiguate_auto_params metasenv params in
427         metasenv,GrafiteAst.AutoBatch (loc,params)
428     | GrafiteAst.Cases (loc, what, pattern, idents) ->
429         let metasenv,what = disambiguate_term context metasenv what in
430         let pattern = disambiguate_pattern pattern in
431         metasenv,GrafiteAst.Cases (loc, what, pattern, idents)
432     | GrafiteAst.Change (loc, pattern, with_what) -> 
433         let with_what = disambiguate_lazy_term with_what in
434         let pattern = disambiguate_pattern pattern in
435         metasenv,GrafiteAst.Change (loc, pattern, with_what)
436     | GrafiteAst.Clear (loc,id) ->
437         metasenv,GrafiteAst.Clear (loc,id)
438     | GrafiteAst.ClearBody (loc,id) ->
439        metasenv,GrafiteAst.ClearBody (loc,id)
440     | GrafiteAst.Compose (loc, t1, t2, times, spec) ->
441         let metasenv,t1 = disambiguate_term context metasenv t1 in
442         let metasenv,t2 = 
443           match t2 with
444           | None -> metasenv, None
445           | Some t2 -> 
446               let m, t2 = disambiguate_term context metasenv t2 in
447               m, Some t2
448         in
449         metasenv,   GrafiteAst.Compose (loc, t1, t2, times, spec)
450     | GrafiteAst.Constructor (loc,n) ->
451         metasenv,GrafiteAst.Constructor (loc,n)
452     | GrafiteAst.Contradiction loc ->
453         metasenv,GrafiteAst.Contradiction loc
454     | GrafiteAst.Cut (loc, ident, term) -> 
455         let metasenv,cic = disambiguate_term context metasenv term in
456         metasenv,GrafiteAst.Cut (loc, ident, cic)
457     | GrafiteAst.Decompose (loc, names) ->
458          metasenv,GrafiteAst.Decompose (loc, names)
459     | GrafiteAst.Demodulate (loc, params) ->
460         let metasenv, params = disambiguate_auto_params metasenv params in
461         metasenv,GrafiteAst.Demodulate (loc, params)
462     | GrafiteAst.Destruct (loc, Some terms) ->
463         let map term (metasenv, terms) =
464            let metasenv, term = disambiguate_term context metasenv term in
465            metasenv, term :: terms
466         in
467         let metasenv, terms = List.fold_right map terms (metasenv, []) in 
468         metasenv, GrafiteAst.Destruct(loc, Some terms)
469     | GrafiteAst.Destruct (loc, None) ->
470         metasenv,GrafiteAst.Destruct(loc,None)
471     | GrafiteAst.Exact (loc, term) -> 
472         let metasenv,cic = disambiguate_term context metasenv term in
473         metasenv,GrafiteAst.Exact (loc, cic)
474     | GrafiteAst.Elim (loc, what, Some using, pattern, specs) ->
475         let metasenv,what = disambiguate_term context metasenv what in
476         let metasenv,using = disambiguate_term context metasenv using in
477         let pattern = disambiguate_pattern pattern in
478         metasenv,GrafiteAst.Elim (loc, what, Some using, pattern, specs)
479     | GrafiteAst.Elim (loc, what, None, pattern, specs) ->
480         let metasenv,what = disambiguate_term context metasenv what in
481         let pattern = disambiguate_pattern pattern in
482         metasenv,GrafiteAst.Elim (loc, what, None, pattern, specs)
483     | GrafiteAst.ElimType (loc, what, Some using, specs) ->
484         let metasenv,what = disambiguate_term context metasenv what in
485         let metasenv,using = disambiguate_term context metasenv using in
486         metasenv,GrafiteAst.ElimType (loc, what, Some using, specs)
487     | GrafiteAst.ElimType (loc, what, None, specs) ->
488         let metasenv,what = disambiguate_term context metasenv what in
489         metasenv,GrafiteAst.ElimType (loc, what, None, specs)
490     | GrafiteAst.Exists loc ->
491         metasenv,GrafiteAst.Exists loc 
492     | GrafiteAst.Fail loc ->
493         metasenv,GrafiteAst.Fail loc
494     | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
495         let pattern = disambiguate_pattern pattern in
496         let term = disambiguate_lazy_term term in
497         let red_kind = disambiguate_reduction_kind red_kind in
498         metasenv,GrafiteAst.Fold (loc, red_kind, term, pattern)
499     | GrafiteAst.FwdSimpl (loc, hyp, names) ->
500        metasenv,GrafiteAst.FwdSimpl (loc, hyp, names)  
501     | GrafiteAst.Fourier loc ->
502        metasenv,GrafiteAst.Fourier loc
503     | GrafiteAst.Generalize (loc,pattern,ident) ->
504         let pattern = disambiguate_pattern pattern in
505         metasenv,GrafiteAst.Generalize (loc,pattern,ident)
506     | GrafiteAst.IdTac loc ->
507         metasenv,GrafiteAst.IdTac loc
508     | GrafiteAst.Intros (loc, specs) ->
509         metasenv,GrafiteAst.Intros (loc, specs)
510     | GrafiteAst.Inversion (loc, term) ->
511        let metasenv,term = disambiguate_term context metasenv term in
512         metasenv,GrafiteAst.Inversion (loc, term)
513     | GrafiteAst.LApply (loc, linear, depth, to_what, what, ident) ->
514        let f term (metasenv, to_what) =
515           let metasenv, term = disambiguate_term context metasenv term in
516           metasenv, term :: to_what
517        in
518        let metasenv, to_what = List.fold_right f to_what (metasenv, []) in 
519        let metasenv, what = disambiguate_term context metasenv what in
520        metasenv,GrafiteAst.LApply (loc, linear, depth, to_what, what, ident)
521     | GrafiteAst.Left loc ->
522        metasenv,GrafiteAst.Left loc
523     | GrafiteAst.LetIn (loc, term, name) ->
524         let metasenv,term = disambiguate_term context metasenv term in
525         metasenv,GrafiteAst.LetIn (loc,term,name)
526     | GrafiteAst.Reduce (loc, red_kind, pattern) ->
527         let pattern = disambiguate_pattern pattern in
528         let red_kind = disambiguate_reduction_kind red_kind in
529         metasenv,GrafiteAst.Reduce(loc, red_kind, pattern)
530     | GrafiteAst.Reflexivity loc ->
531         metasenv,GrafiteAst.Reflexivity loc
532     | GrafiteAst.Replace (loc, pattern, with_what) -> 
533         let pattern = disambiguate_pattern pattern in
534         let with_what = disambiguate_lazy_term with_what in
535         metasenv,GrafiteAst.Replace (loc, pattern, with_what)
536     | GrafiteAst.Rewrite (loc, dir, t, pattern, names) ->
537         let metasenv,term = disambiguate_term context metasenv t in
538         let pattern = disambiguate_pattern pattern in
539         metasenv,GrafiteAst.Rewrite (loc, dir, term, pattern, names)
540     | GrafiteAst.Right loc ->
541         metasenv,GrafiteAst.Right loc
542     | GrafiteAst.Ring loc ->
543         metasenv,GrafiteAst.Ring loc
544     | GrafiteAst.Split loc ->
545         metasenv,GrafiteAst.Split loc
546     | GrafiteAst.Symmetry loc ->
547         metasenv,GrafiteAst.Symmetry loc
548     | GrafiteAst.Transitivity (loc, term) -> 
549         let metasenv,cic = disambiguate_term context metasenv term in
550         metasenv,GrafiteAst.Transitivity (loc, cic)
551       (* Nuovi casi *)
552     | GrafiteAst.Assume (loc, id, term) -> 
553         let metasenv,cic = disambiguate_term context metasenv term in
554         metasenv,GrafiteAst.Assume (loc, id, cic)
555     | GrafiteAst.Suppose (loc, term, id, term') ->
556         let metasenv,cic = disambiguate_term context metasenv term in
557         let metasenv,cic' =
558            match term' with
559               None -> metasenv,None
560             | Some t ->
561                   let metasenv,t = disambiguate_term context metasenv t in
562                   metasenv,Some t in
563         metasenv,GrafiteAst.Suppose (loc, cic, id, cic')
564     | GrafiteAst.Bydone (loc,just) ->
565         let metasenv,just =
566          disambiguate_just disambiguate_term context metasenv just
567         in
568          metasenv,GrafiteAst.Bydone (loc, just)
569     | GrafiteAst.We_need_to_prove (loc,term,id,term') ->
570         let metasenv,cic = disambiguate_term context metasenv term in
571         let metasenv,cic' = 
572             match term' with
573               None -> metasenv,None
574             | Some t ->
575                   let metasenv,t = disambiguate_term context metasenv t in
576                   metasenv,Some t in
577         metasenv,GrafiteAst.We_need_to_prove (loc,cic,id,cic')
578     | GrafiteAst.By_just_we_proved (loc,just,term',id,term'') ->
579         let metasenv,just =
580          disambiguate_just disambiguate_term context metasenv just in
581         let metasenv,cic' = disambiguate_term context metasenv term' in
582         let metasenv,cic'' = 
583             match term'' with
584               None -> metasenv,None
585            |  Some t ->  
586                     let metasenv,t = disambiguate_term context metasenv t in
587                      metasenv,Some t in
588         metasenv,GrafiteAst.By_just_we_proved (loc,just,cic',id,cic'')
589     | GrafiteAst.We_proceed_by_cases_on (loc, term, term') ->
590         let metasenv,cic = disambiguate_term context metasenv term in
591         let metasenv,cic' = disambiguate_term context metasenv term' in
592         metasenv,GrafiteAst.We_proceed_by_cases_on (loc, cic, cic')
593     | GrafiteAst.We_proceed_by_induction_on (loc, term, term') ->
594         let metasenv,cic = disambiguate_term context metasenv term in
595         let metasenv,cic' = disambiguate_term context metasenv term' in
596         metasenv,GrafiteAst.We_proceed_by_induction_on (loc, cic, cic')
597    | GrafiteAst.Byinduction (loc, term, id) ->
598         let metasenv,cic = disambiguate_term context metasenv term in
599         metasenv,GrafiteAst.Byinduction(loc, cic, id)
600    | GrafiteAst.Thesisbecomes (loc, term) ->
601         let metasenv,cic = disambiguate_term context metasenv term in
602         metasenv,GrafiteAst.Thesisbecomes (loc, cic)
603    | GrafiteAst.ExistsElim (loc, just, id1, term1, id2, term2) ->
604         let metasenv,just =
605          disambiguate_just disambiguate_term context metasenv just in
606         let metasenv,cic' = disambiguate_term context metasenv term1 in
607         let cic''= disambiguate_lazy_term term2 in
608         metasenv,GrafiteAst.ExistsElim(loc, just, id1, cic', id2, cic'')
609    | GrafiteAst.AndElim (loc, just, id, term1, id1, term2) ->
610         let metasenv,just =
611          disambiguate_just disambiguate_term context metasenv just in
612         let metasenv,cic'= disambiguate_term context metasenv term1 in
613         let metasenv,cic''= disambiguate_term context metasenv term2 in
614         metasenv,GrafiteAst.AndElim(loc, just, id, cic', id1, cic'')   
615    | GrafiteAst.Case (loc, id, params) ->
616         let metasenv,params' =
617          List.fold_right
618           (fun (id,term) (metasenv,params) ->
619             let metasenv,cic = disambiguate_term context metasenv term in
620              metasenv,(id,cic)::params
621           ) params (metasenv,[])
622         in
623         metasenv,GrafiteAst.Case(loc, id, params')   
624    | GrafiteAst.RewritingStep (loc, term1, term2, term3, cont) ->
625         let metasenv,cic =
626          match term1 with
627             None -> metasenv,None
628           | Some (start,t) -> 
629              let metasenv,t = disambiguate_term context metasenv t in
630               metasenv,Some (start,t) in
631         let metasenv,cic'= disambiguate_term context metasenv term2 in
632         let metasenv,cic'' =
633          match term3 with
634           | `SolveWith term ->
635              let metasenv,term = disambiguate_term context metasenv term in
636              metasenv, `SolveWith term
637           | `Auto params -> 
638               let metasenv, params = disambiguate_auto_params metasenv params in
639               metasenv,`Auto params
640           | `Term t -> 
641              let metasenv,t = disambiguate_term context metasenv t in
642               metasenv,`Term t
643           | `Proof as t -> metasenv,t in
644         metasenv,GrafiteAst.RewritingStep (loc, cic, cic', cic'', cont)   
645
646 let disambiguate_obj estatus ?baseuri metasenv (text,prefix_len,obj) =
647   let uri =
648    let baseuri = 
649      match baseuri with Some x -> x | None -> raise BaseUriNotSetYet
650    in
651    let name = 
652      match obj with
653      | CicNotationPt.Inductive (_,(name,_,_,_)::_)
654      | CicNotationPt.Record (_,name,_,_) -> name ^ ".ind"
655      | CicNotationPt.Theorem (_,name,_,_,_) -> name ^ ".con"
656      | CicNotationPt.Inductive _ -> assert false
657    in
658      UriManager.uri_of_string (baseuri ^ "/" ^ name)
659   in
660 (*
661  let _try_new cic =
662   (NCicLibrary.clear_cache ();
663    NCicEnvironment.invalidate ();
664    OCic2NCic.clear ();
665    let graph = 
666      match cic with
667      | Some (Cic.CurrentProof (_,metasenv, _, ty,_,_)) ->
668          let _, ugraph = 
669            CicTypeChecker.type_of_aux' metasenv [] ty CicUniv.empty_ugraph
670          in
671            ugraph
672      | Some (Cic.Constant (_,_, ty,_,_)) ->
673          let _, ugraph = 
674                  CicTypeChecker.type_of_aux' [] [] ty CicUniv.empty_ugraph
675          in
676            ugraph
677      | _ -> CicUniv.empty_ugraph
678    in
679
680 (*
681    prerr_endline "PRIMA COERCIONS";
682    let _,l = CicUniv.do_rank graph in
683    List.iter (fun k -> 
684      prerr_endline (CicUniv.string_of_universe k ^ " = " ^ string_of_int
685      (CicUniv.get_rank k))) l;
686 *)
687
688    let graph =
689        List.fold_left 
690          (fun graph (_,_,l) ->
691            List.fold_left
692              (fun graph (uri,_,_) ->
693                 let _,g = CicTypeChecker.typecheck uri in
694                 CicUniv.merge_ugraphs ~base_ugraph:graph ~increment:(g,uri))
695              graph l)
696        graph (CoercDb.to_list (CoercDb.dump ()))
697    in
698    ignore(CicUniv.do_rank graph);
699
700
701 (*
702    prerr_endline "DOPO COERCIONS";
703    let _,l = CicUniv.do_rank graph in
704    List.iter (fun k -> 
705      prerr_endline (CicUniv.string_of_universe k ^ " = " ^ string_of_int
706      (CicUniv.get_rank k))) l;
707 *)
708
709
710    prerr_endline "INIZIO NUOVA DISAMBIGUAZIONE";
711    let time = Unix.gettimeofday () in
712        (try
713          (match 
714           NCicDisambiguate.disambiguate_obj
715            ~rdb:estatus
716            ~lookup_in_library:nlookup_in_library
717            ~description_of_alias:LexiconAst.description_of_alias
718            ~mk_choice:ncic_mk_choice
719            ~mk_implicit
720            ~uri:(OCic2NCic.nuri_of_ouri uri)
721            ~aliases:estatus#lstatus.LexiconEngine.aliases
722            ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases)
723            (text,prefix_len,obj)
724          with
725          | [_,_,_,obj],_ ->
726           let time = Unix.gettimeofday () -. time in
727 (*           NCicTypeChecker.typecheck_obj obj; *)
728           prerr_endline ("NUOVA DISAMBIGUAZIONE OK: "^ string_of_float time);
729 (*
730           let obj = 
731             let u,i,m,_,o = obj in
732             u,i,m,[],o
733           in
734 *)
735           prerr_endline (NCicPp.ppobj obj)
736          | _ ->
737           prerr_endline ("NUOVA DISAMBIGUAZIONE AMBIGUO!!!!!!!!!  "))
738        with 
739        | MultiPassDisambiguator.DisambiguationError (_,s) ->
740         prerr_endline ("ERRORE NUOVA DISAMBIGUAZIONE ("
741           ^UriManager.string_of_uri uri^
742           "):\n" ^
743          String.concat "\n" 
744           (List.map (fun _,_,x,_ -> snd (Lazy.force x)) (List.flatten s)))
745 (*        | exn -> prerr_endline (Printexc.to_string exn) *)
746        )
747   )
748  in 
749 *)
750
751
752  try
753 (*   let time = Unix.gettimeofday () in  *)
754
755
756   let (diff, metasenv, _, cic, _) =
757     singleton "third"
758       (CicDisambiguate.disambiguate_obj 
759         ~lookup_in_library 
760         ~mk_choice:cic_mk_choice
761         ~mk_implicit ~fix_instance
762         ~description_of_alias:LexiconAst.description_of_alias
763         ~aliases:estatus#lstatus.LexiconEngine.aliases
764         ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases) 
765         ~uri:(Some uri)
766         (text,prefix_len,obj)) 
767   in
768
769
770 (*
771   let time = Unix.gettimeofday () -. time in
772   prerr_endline ("VECCHIA DISAMBIGUAZIONE ("^
773     UriManager.string_of_uri uri ^"): " ^ string_of_float time);
774 *)
775 (*    try_new (Some cic);   *)
776
777
778   let estatus = LexiconEngine.set_proof_aliases estatus diff in
779    estatus, metasenv, cic
780
781  with 
782  | Sys.Break as exn -> raise exn
783  | exn ->
784 (*    try_new None; *)
785    raise exn
786 ;;
787
788 let disambiguate_nobj estatus ?baseuri (text,prefix_len,obj) =
789   let uri =
790    let baseuri = 
791      match baseuri with Some x -> x | None -> raise BaseUriNotSetYet
792    in
793    let name = 
794      match obj with
795      | CicNotationPt.Inductive (_,(name,_,_,_)::_)
796      | CicNotationPt.Record (_,name,_,_) -> name ^ ".ind"
797      | CicNotationPt.Theorem (_,name,_,_,_) -> name ^ ".con"
798      | CicNotationPt.Inductive _ -> assert false
799    in
800      UriManager.uri_of_string (baseuri ^ "/" ^ name)
801   in
802   let diff, _, _, cic =
803    singleton "third"
804     (NCicDisambiguate.disambiguate_obj
805       ~lookup_in_library:nlookup_in_library
806       ~description_of_alias:LexiconAst.description_of_alias
807       ~mk_choice:ncic_mk_choice
808       ~mk_implicit ~fix_instance
809       ~uri:(OCic2NCic.nuri_of_ouri uri)
810       ~rdb:estatus
811       ~aliases:estatus#lstatus.LexiconEngine.aliases
812       ~universe:(Some estatus#lstatus.LexiconEngine.multi_aliases) 
813       (text,prefix_len,obj)) in
814   let estatus = LexiconEngine.set_proof_aliases estatus diff in
815    estatus, cic
816 ;;
817   
818 let disambiguate_command estatus ?baseuri metasenv (text,prefix_len,cmd)=
819   match cmd with
820    | GrafiteAst.Index(loc,key,uri) ->
821        let lexicon_status_ref = ref estatus in 
822        let disambiguate_term =
823          disambiguate_term None text prefix_len lexicon_status_ref [] in
824        let disambiguate_term_option metasenv =
825          function
826              None -> metasenv,None
827            | Some t ->
828                let metasenv,t = disambiguate_term metasenv t in
829                  metasenv, Some t
830        in
831        let metasenv,key = disambiguate_term_option metasenv key in
832         !lexicon_status_ref,metasenv,GrafiteAst.Index(loc,key,uri)
833    | GrafiteAst.Select (loc,uri) -> 
834         estatus, metasenv, GrafiteAst.Select(loc,uri)
835    | GrafiteAst.Pump(loc,i) -> 
836         estatus, metasenv, GrafiteAst.Pump(loc,i)
837    | GrafiteAst.PreferCoercion (loc,t) -> 
838        let lexicon_status_ref = ref estatus in 
839        let disambiguate_term =
840          disambiguate_term None text prefix_len lexicon_status_ref [] in
841       let metasenv,t = disambiguate_term metasenv t in
842        !lexicon_status_ref, metasenv, GrafiteAst.PreferCoercion (loc,t)
843    | GrafiteAst.Coercion (loc,t,b,a,s) -> 
844        let lexicon_status_ref = ref estatus in 
845        let disambiguate_term =
846          disambiguate_term None text prefix_len lexicon_status_ref [] in
847       let metasenv,t = disambiguate_term metasenv t in
848        !lexicon_status_ref, metasenv, GrafiteAst.Coercion (loc,t,b,a,s)
849    | GrafiteAst.Inverter (loc,n,indty,params) ->
850        let lexicon_status_ref = ref estatus in
851        let disambiguate_term = 
852          disambiguate_term None text prefix_len lexicon_status_ref [] in
853        let metasenv,indty = disambiguate_term metasenv indty in
854         !lexicon_status_ref, metasenv, GrafiteAst.Inverter (loc,n,indty,params)
855    | GrafiteAst.Default _
856    | GrafiteAst.Drop _
857    | GrafiteAst.Include _
858    | GrafiteAst.Print _
859    | GrafiteAst.Qed _
860    | GrafiteAst.Set _ as cmd ->
861        estatus,metasenv,cmd
862    | GrafiteAst.Obj (loc,obj) ->
863        let estatus,metasenv,obj =
864         disambiguate_obj estatus ?baseuri metasenv (text,prefix_len,obj)in
865        estatus, metasenv, GrafiteAst.Obj (loc,obj)
866    | GrafiteAst.Relation (loc,id,a,aeq,refl,sym,trans) ->
867       let lexicon_status_ref = ref estatus in 
868       let disambiguate_term =
869        disambiguate_term None text prefix_len lexicon_status_ref [] in
870       let disambiguate_term_option metasenv =
871        function
872           None -> metasenv,None
873        | Some t ->
874           let metasenv,t = disambiguate_term metasenv t in
875            metasenv, Some t
876       in
877       let metasenv,a = disambiguate_term metasenv a in
878       let metasenv,aeq = disambiguate_term metasenv aeq in
879       let metasenv,refl = disambiguate_term_option metasenv refl in
880       let metasenv,sym = disambiguate_term_option metasenv sym in
881       let metasenv,trans = disambiguate_term_option metasenv trans in
882        !lexicon_status_ref, metasenv,
883         GrafiteAst.Relation (loc,id,a,aeq,refl,sym,trans)
884
885 let disambiguate_macro 
886   lexicon_status_ref metasenv context (text,prefix_len, macro) 
887 =
888  let disambiguate_term = disambiguate_term None text prefix_len lexicon_status_ref in
889   let disambiguate_reduction_kind = 
890     disambiguate_reduction_kind text prefix_len lexicon_status_ref in
891   match macro with
892    | GrafiteAst.WMatch (loc,term) ->
893       let metasenv,term = disambiguate_term context metasenv term in
894        metasenv,GrafiteAst.WMatch (loc,term)
895    | GrafiteAst.WInstance (loc,term) ->
896       let metasenv,term = disambiguate_term context metasenv term in
897        metasenv,GrafiteAst.WInstance (loc,term)
898    | GrafiteAst.WElim (loc,term) ->
899       let metasenv,term = disambiguate_term context metasenv term in
900        metasenv,GrafiteAst.WElim (loc,term)
901    | GrafiteAst.WHint (loc,term) ->
902       let metasenv,term = disambiguate_term context metasenv term in
903        metasenv,GrafiteAst.WHint (loc,term)
904    | GrafiteAst.Check (loc,term) ->
905       let metasenv,term = disambiguate_term context metasenv term in
906        metasenv,GrafiteAst.Check (loc,term)
907    | GrafiteAst.Eval (loc,kind,term) ->
908       let metasenv, term = disambiguate_term context metasenv term in
909       let kind = disambiguate_reduction_kind kind in
910        metasenv,GrafiteAst.Eval (loc,kind,term)
911    | GrafiteAst.AutoInteractive (loc, params) -> 
912       let metasenv, params = 
913         disambiguate_auto_params disambiguate_term metasenv context params in
914       metasenv, GrafiteAst.AutoInteractive (loc, params)
915    | GrafiteAst.Hint _
916    | GrafiteAst.WLocate _
917    | GrafiteAst.Inline _ as macro ->
918       metasenv,macro