]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/disambiguate.ml
ff794fd5b2d03052bbea92803443159b059dda2e
[helm.git] / helm / gTopLevel / disambiguate.ml
1 (* Copyright (C) 2000-2002, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 06/01/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 (* CSC: IMPERATIVE AND NOT VERY CLEAN, TO GET THE LAST ISSUED QUERY *)
37 let get_last_query = 
38  let query = ref "" in
39   MQueryGenerator.set_confirm_query
40    (function q -> query := MQueryUtil.text_of_query q ; true) ;
41   function result ->
42    !query ^ " <h1>Result:</h1> " ^ MQueryUtil.text_of_result result "<br>"
43 ;;
44
45 (** This module provides a functor to disambiguate the input **)
46 (** given a set of user-interface call-backs                 **)
47
48 module type Callbacks =
49   sig
50     val output_html : string -> unit
51     val interactive_user_uri_choice :
52       selection_mode:[`SINGLE | `EXTENDED] ->
53       ?ok:string ->
54       ?enable_button_for_non_vars:bool ->
55       title:string -> msg:string -> id:string -> string list -> string list
56     val interactive_interpretation_choice :
57       (string * string) list list -> int
58     val input_or_locate_uri : title:string -> UriManager.uri
59   end
60 ;;
61
62 type domain_and_interpretation =
63  string list * (string -> CicTextualParser0.uri option)
64 ;;
65
66 module Make(C:Callbacks) =
67   struct
68
69    let locate_one_id id =
70     let result = MQueryGenerator.locate id in
71     let uris =
72      List.map
73       (function uri,_ ->
74         Misc.wrong_xpointer_format_from_wrong_xpointer_format' uri
75       ) result in
76     let html=
77      " <h1>Locate Query: </h1><pre>" ^ get_last_query result ^ "</pre>"
78     in
79      C.output_html html ;
80      let uris' =
81       match uris with
82          [] ->
83           [UriManager.string_of_uri
84            (C.input_or_locate_uri
85              ~title:("URI matching \"" ^ id ^ "\" unknown."))]
86        | [uri] -> [uri]
87        | _ ->
88          C.interactive_user_uri_choice
89           ~selection_mode:`EXTENDED
90           ~ok:"Try every selection."
91           ~enable_button_for_non_vars:true
92           ~title:"Ambiguous input."
93           ~msg:
94             ("Ambiguous input \"" ^ id ^
95              "\". Please, choose one or more interpretations:")
96           ~id
97           uris
98      in
99       List.map Misc.cic_textual_parser_uri_of_string uris'
100
101
102    exception ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
103
104    type test_result =
105       Ok of Cic.term * Cic.metasenv
106     | Ko
107     | Uncertain
108
109    let disambiguate_input context metasenv dom mk_metasenv_and_expr ~id_to_uris=
110     let known_ids,resolve_id = id_to_uris in
111     let dom' =
112      let rec filter =
113       function
114          [] -> []
115        | he::tl ->
116           if List.mem he known_ids then filter tl else he::(filter tl)
117      in
118       filter dom
119     in
120      (* for each id in dom' we get the list of uris associated to it *)
121      let list_of_uris = List.map locate_one_id dom' in
122      let tests_no =
123       List.fold_left (fun i uris -> i * List.length uris) 1 list_of_uris
124      in
125       if tests_no > 1 then
126        C.output_html
127         ("<h1>Disambiguation phase started: " ^
128           string_of_int tests_no ^ " cases will be tried.") ;
129      (* and now we compute the list of all possible assignments from *)
130      (* id to uris that generate well-typed terms                    *)
131      let resolve_ids =
132       (* function to test if a partial interpretation is so far correct *)
133       let test resolve_id residual_dom =
134        (* We put implicits in place of every identifier that is not *)
135        (* resolved by resolve_id                                    *)
136        let resolve_id'' =
137         let resolve_id' =
138          function id ->
139           match resolve_id id with
140              None -> None
141            | Some uri -> Some (CicTextualParser0.Uri uri)
142         in
143          List.fold_left
144           (fun f id ->
145             function id' ->
146              if id = id' then Some (CicTextualParser0.Implicit) else f id'
147           ) resolve_id' residual_dom
148        in
149         (* and we try to refine the term *)
150         let saved_status = !CicTextualParser0.metasenv in
151         let metasenv',expr = mk_metasenv_and_expr resolve_id'' in
152 (*CSC: Bug here: we do not try to typecheck also the metasenv' *)
153         (* The parser is imperative ==> we must restore the old status ;-(( *)
154         CicTextualParser0.metasenv := saved_status ;
155          try
156           let term,_,_,metasenv'' =
157            CicRefine.type_of_aux' metasenv' context expr
158           in
159            Ok (term,metasenv'')
160          with
161             CicRefine.MutCaseFixAndCofixRefineNotImplemented ->
162              (try
163                let term = CicTypeChecker.type_of_aux' metasenv' context expr in
164                 Ok (term,metasenv')
165               with _ -> Ko
166              )
167           | CicRefine.Uncertain _ ->
168 prerr_endline ("%%% UNCERTAIN!!! " ^ CicPp.ppterm expr) ;
169              Uncertain
170           | _ ->
171 prerr_endline ("%%% PRUNED!!! " ^ CicPp.ppterm expr) ;
172             Ko
173       in
174       let rec aux resolve_id ids list_of_uris =
175        match ids,list_of_uris with
176           [],[] ->
177            (match test resolve_id [] with
178                Ok (term,metasenv) -> [resolve_id,term,metasenv]
179              | Ko | Uncertain -> [])
180         | id::idtl,uris::uristl ->
181            let rec filter =
182             function
183                [] -> []
184              | uri::uritl ->
185                 let resolve_id' =
186                  function id' -> if id = id' then Some uri else resolve_id id'
187                 in
188                  (match test resolve_id' idtl with
189                      Ok (term,metasenv) ->
190                       (* the next three ``if''s are used to avoid the base   *)
191                       (* case where the term would be refined a second time. *)
192                       (if uristl = [] then
193                         [resolve_id',term,metasenv]
194                        else
195                         (aux resolve_id' idtl uristl)
196                       ) @ (filter uritl)
197                    | Uncertain ->
198                       (if uristl = [] then []
199                        else
200                         (aux resolve_id' idtl uristl)
201                       ) @ (filter uritl)
202                    | Ko ->
203                       filter uritl
204                  )
205            in
206             filter uris
207         | _,_ -> assert false
208       in
209        aux resolve_id dom' list_of_uris
210      in
211       List.iter
212        (function (resolve,term,newmetasenv) ->
213          (* If metasen <> newmetasenv is a normal condition, we should *)
214          (* be prepared to apply the returned substitution to the      *)
215          (* whole current proof.                                       *)
216          if metasenv <> newmetasenv then
217           begin
218            prerr_endline
219             ("+++++ ASSERTION FAILED: " ^
220              "a refine operation should not modify the metasenv") ;
221            (* an assert would raise an exception that could be caught *)
222            exit 1
223           end
224        ) resolve_ids ;
225       let resolve_id',term,metasenv' =
226        match resolve_ids with
227           [] -> raise ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
228         | [resolve_id] -> resolve_id
229         | _ ->
230           let choices =
231            List.map
232             (function (resolve,_,_) ->
233               List.map
234                (function id ->
235                  id,
236                   match resolve id with
237                      None -> assert false
238                    | Some uri ->
239                       match uri with
240                          CicTextualParser0.ConUri uri
241                        | CicTextualParser0.VarUri uri ->
242                           UriManager.string_of_uri uri
243                        | CicTextualParser0.IndTyUri (uri,tyno) ->
244                           UriManager.string_of_uri uri ^ "#xpointer(1/" ^
245                            string_of_int (tyno+1) ^ ")"
246                        | CicTextualParser0.IndConUri (uri,tyno,consno) ->
247                           UriManager.string_of_uri uri ^ "#xpointer(1/" ^
248                            string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^                           ")"
249                ) dom
250             ) resolve_ids
251           in
252            let index = C.interactive_interpretation_choice choices in
253             List.nth resolve_ids index
254       in
255        (known_ids @ dom', resolve_id'), metasenv',term
256 end
257 ;;