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