1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 open Disambiguate_types
28 exception Invalid_choice
29 exception No_choices of domain_item
30 exception NoWellTypedInterpretation
32 (** raised when an environment is not enough informative to decide *)
36 let debug_print = if debug then prerr_endline else ignore
38 let descr_of_domain_item = function
41 | Num i -> string_of_int i
43 let symbol_choices = Hashtbl.create 1023
44 let num_choices = ref []
46 let add_symbol_choice symbol codomain_item =
49 Hashtbl.find symbol_choices symbol
52 Hashtbl.replace symbol_choices symbol (codomain_item :: current_choices)
53 let add_num_choice choice = num_choices := choice :: !num_choices
56 | Ok of Cic.term * Cic.metasenv
60 let refine metasenv context term =
61 let metasenv, term = CicMkImplicit.expand_implicits metasenv context term in
63 let term', _, _, metasenv' = CicRefine.type_of_aux' metasenv context term in
66 | CicRefine.MutCaseFixAndCofixRefineNotImplemented ->
67 (* TODO remove this case as soon as refine is fully implemented *)
69 debug_print (Printf.sprintf "TYPE CHECKER %s" (CicPp.ppterm term));
70 let term' = CicTypeChecker.type_of_aux' metasenv context term in
74 | CicRefine.Uncertain _ ->
75 debug_print ("%%% UNCERTAIN!!! " ^ CicPp.ppterm term) ;
78 debug_print ("%%% PRUNED!!! " ^ CicPp.ppterm term) ;
85 let indtyuri_of_uri uri =
86 let index_sharp = String.index uri '#' in
87 let index_num = index_sharp + 3 in
88 (UriManager.uri_of_string (String.sub uri 0 index_sharp),
89 int_of_string(String.sub uri index_num (String.length uri - index_num)) - 1)
91 let indconuri_of_uri uri =
92 let index_sharp = String.index uri '#' in
93 let index_div = String.rindex uri '/' in
94 let index_con = index_div + 1 in
95 (UriManager.uri_of_string (String.sub uri 0 index_sharp),
97 (String.sub uri (index_sharp + 3) (index_div - index_sharp - 3)) - 1,
99 (String.sub uri index_con (String.length uri - index_con)))
101 (* TODO move it to Cic *)
102 let term_of_uri uri =
105 (* TODO explicit substitutions? *)
106 let len = String.length uri in
107 if String.sub uri (len - 4) 4 = ".con" then
108 Cic.Const (uri_of_string uri, [])
109 else if String.sub uri (len - 4) 4 = ".var" then
110 Cic.Var (uri_of_string uri, [])
114 let uri',typeno = indtyuri_of_uri uri in
115 Cic.MutInd (uri', typeno, [])
117 | UriManager.IllFormedUri _ | Failure _ | Invalid_argument _ ->
118 (* Constructor of an Inductive Type *)
119 let uri',typeno,consno = indconuri_of_uri uri in
120 Cic.MutConstruct (uri', typeno, consno, []))
122 | Invalid_argument _ -> raise (UriManager.IllFormedUri uri)
124 let resolve (env: environment) (item: domain_item) ?(num = "") ?(args = []) () =
125 snd (Environment.find item env) env num args
127 let find_in_environment name context =
128 let rec aux acc = function
129 | [] -> raise Not_found
130 | Cic.Name hd :: tl when hd = name -> acc
131 | _ :: tl -> aux (acc + 1) tl
135 let interpretate ~context ~env ast =
136 let rec aux loc context = function
137 | Ast.LocatedTerm (loc, term) -> aux loc context term
138 | Ast.Appl terms -> Cic.Appl (List.map (aux loc context) terms)
139 | Ast.Appl_symbol (symb, i, args) ->
140 let cic_args = List.map (aux loc context) args in
141 resolve env (Symbol (symb, i)) ~args:cic_args ()
142 | Ast.Binder (binder_kind, var, typ, body) ->
143 let cic_type = aux_option loc context typ in
144 let cic_body = aux loc (var :: context) body in
145 (match binder_kind with
146 | `Lambda -> Cic.Lambda (var, cic_type, cic_body)
147 | `Pi | `Forall -> Cic.Prod (var, cic_type, cic_body)
149 resolve env (Symbol ("exists", 0))
150 ~args:[ cic_type; Cic.Lambda (var, cic_type, cic_body) ] ())
151 | Ast.Case (term, indty_ident, outtype, branches) ->
152 let cic_term = aux loc context term in
153 let cic_outtype = aux_option loc context outtype in
154 let do_branch (pat, term) =
155 let rec do_branch' context = function
156 | [] -> aux loc context term
158 let cic_body = do_branch' (Cic.Name hd :: context) tl in
159 Cic.Lambda (Cic.Name hd, Cic.Implicit, cic_body)
162 | _ :: tl -> (* ignoring constructor *) do_branch' context tl
165 let (indtype_uri, indtype_no) =
166 match resolve env (Id indty_ident) () with
167 | Cic.MutInd (uri, tyno, _) -> uri, tyno
168 | Cic.Implicit -> raise Try_again
169 | _ -> Parser.fail loc ("Not an inductive type: " ^ indty_ident)
171 Cic.MutCase (indtype_uri, indtype_no, cic_outtype, cic_term,
172 (List.map do_branch branches))
173 | Ast.LetIn (var, def, body) ->
174 let cic_def = aux loc context def in
175 let name = Cic.Name var in
176 let cic_body = aux loc (name :: context) body in
177 Cic.LetIn (name, cic_def, cic_body)
178 | Ast.LetRec (kind, defs, body) ->
180 List.fold_left (fun acc (var, _, _, _) -> Cic.Name var :: acc)
183 let cic_body = aux loc context' body in
186 (fun (var, body, typ, decr_idx) ->
187 let cic_body = aux loc context' body in
188 let cic_type = aux_option loc context typ in
189 (var, decr_idx, cic_type, cic_body))
192 let counter = ref 0 in
193 let build_term funs =
194 (* this is the body of the fold_right function below. Rationale: Fix
195 * and CoFix cases differs only in an additional index in the
196 * indcutiveFun list, see Cic.term *)
199 (fun (var, _, _, _) cic ->
201 Cic.LetIn (Cic.Name var, Cic.Fix (!counter, funs), cic))
204 List.map (fun (name, _, typ, body) -> (name, typ, body)) funs
206 (fun (var, _, _, _) cic ->
207 Cic.LetIn (Cic.Name var, Cic.CoFix (!counter, funs), cic))
209 List.fold_right (build_term inductiveFuns) inductiveFuns cic_body
210 | Ast.Ident (name, subst) ->
211 (* TODO hanlde explicit substitutions *)
213 let index = find_in_environment name context in
215 Parser.fail loc "Explicit substitutions not allowed here";
217 with Not_found -> resolve env (Id name) ())
218 | Ast.Num (num, i) -> resolve env (Num i) ~num ()
219 | Ast.Meta (index, subst) ->
222 (function None -> None | Some term -> Some (aux loc context term))
225 Cic.Meta (index, cic_subst)
226 | Ast.Sort `Prop -> Cic.Sort Cic.Prop
227 | Ast.Sort `Set -> Cic.Sort Cic.Set
228 | Ast.Sort `Type -> Cic.Sort Cic.Type
229 | Ast.Sort `CProp -> Cic.Sort Cic.CProp
230 and aux_option loc context = function
231 | None -> Cic.Implicit
232 | Some term -> aux loc context term
235 | Ast.LocatedTerm (loc, term) -> aux loc context term
238 let domain_of_term ~context ast =
239 let rec aux loc context = function
240 | Ast.LocatedTerm (_, term) -> aux loc context term
242 List.fold_left (fun dom term -> Domain.union dom (aux loc context term))
244 | Ast.Appl_symbol (symb, i, args) ->
245 List.fold_left (fun dom term -> Domain.union dom (aux loc context term))
246 (Domain.singleton (Symbol (symb, i))) args
247 | Ast.Binder (_, var, typ, body) ->
248 let type_dom = aux_option loc context typ in
249 let body_dom = aux loc (var :: context) body in
250 Domain.union type_dom body_dom
251 | Ast.Case (term, indty_ident, outtype, branches) ->
252 let term_dom = aux loc context term in
253 let outtype_dom = aux_option loc context outtype in
254 let do_branch (pat, term) =
258 (List.fold_left (fun acc var -> (Cic.Name var) :: acc)
264 List.fold_left (fun dom branch -> Domain.union dom (do_branch branch))
265 Domain.empty branches
267 Domain.add (Id indty_ident)
268 (Domain.union outtype_dom (Domain.union term_dom branches_dom))
269 | Ast.LetIn (var, body, where) ->
270 let body_dom = aux loc context body in
271 let where_dom = aux loc (Cic.Name var :: context) where in
272 Domain.union body_dom where_dom
273 | Ast.LetRec (kind, defs, where) ->
275 List.fold_left (fun acc (var, _, _, _) -> Cic.Name var :: acc)
278 let where_dom = aux loc context' where in
281 (fun dom (_, body, typ, _) ->
282 Domain.union (aux loc context' body) (aux_option loc context typ))
285 Domain.union where_dom defs_dom
286 | Ast.Ident (name, subst) ->
287 (* TODO hanlde explicit substitutions *)
289 let index = find_in_environment name context in
291 Parser.fail loc "Explicit substitutions not allowed here";
293 with Not_found -> Domain.singleton (Id name))
294 | Ast.Num (num, i) -> Domain.singleton (Num i)
295 | Ast.Meta (index, local_context) ->
297 (fun dom term -> Domain.union dom (aux_option loc context term))
298 Domain.empty local_context
299 | Ast.Sort _ -> Domain.empty
300 and aux_option loc context = function
301 | None -> Domain.empty
302 | Some t -> aux loc context t
305 | Ast.LocatedTerm (loc, term) -> aux loc context term
308 module Make (C: Callbacks) =
310 let choices_of_id mqi_handle id =
311 let query = MQueryGenerator.locate id in
312 let result = MQueryInterpreter.execute mqi_handle query in
316 MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri
318 C.output_html (`Msg (`T "Locate query:"));
319 MQueryUtil.text_of_query
320 (fun s -> C.output_html ~append_NL:false (`Msg (`T s)))
322 C.output_html (`Msg (`T "Result:"));
323 MQueryUtil.text_of_result
324 (fun s -> C.output_html (`Msg (`T s))) "" result;
328 [UriManager.string_of_uri (C.input_or_locate_uri
329 ~title:("URI matching \"" ^ id ^ "\" unknown."))]
332 C.interactive_user_uri_choice ~selection_mode:`MULTIPLE
333 ~ok:"Try every selection." ~enable_button_for_non_vars:true
334 ~title:"Ambiguous input." ~id
335 ~msg: ("Ambiguous input \"" ^ id ^
336 "\". Please, choose one or more interpretations:")
340 (fun uri -> (uri, let term = term_of_uri uri in fun _ _ _ -> term))
343 let disambiguate_term mqi_handle context metasenv term ~aliases:current_env
345 let current_dom = (* TODO temporary, remove ASAP *)
346 Environment.fold (fun item _ dom -> Domain.add item dom)
347 current_env Domain.empty
349 debug_print "NEW DISAMBIGUATE INPUT";
350 let disambiguate_context = (* cic context -> disambiguate context *)
352 (function None -> Cic.Anonymous | Some (name, _) -> name)
355 let term_dom = domain_of_term ~context:disambiguate_context term in
356 debug_print (sprintf "DISAMBIGUATION DOMAIN: %s"
357 (string_of_domain term_dom));
358 let todo_dom = Domain.diff term_dom current_dom in
359 (* (2) lookup function for any item (Id/Symbol/Num) *)
361 let id_choices = Hashtbl.create 1023 in
367 Hashtbl.find id_choices id
369 let choices = choices_of_id mqi_handle id in
370 Hashtbl.add id_choices id choices;
372 | Symbol (symb, _) ->
373 (try Hashtbl.find symbol_choices symb with Not_found -> [])
374 | Num instance -> !num_choices
376 if choices = [] then raise (No_choices item);
379 (* (3) test an interpretation filling with meta uninterpreted identifiers
381 let test_env current_env todo_dom =
385 Environment.add item ("Implicit", fun _ _ _ -> Cic.Implicit) env)
390 interpretate ~context:disambiguate_context ~env:filled_env term
393 (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppterm cic_term));
394 refine metasenv context cic_term
396 | Try_again -> Uncertain
397 | Invalid_choice -> Ko
399 (* (4) build all possible interpretations *)
400 let rec aux current_env todo_dom =
401 if Domain.is_empty todo_dom then
402 match test_env current_env Domain.empty with
403 | Ok (term, metasenv) -> [ current_env, term, metasenv ]
404 | Ko | Uncertain -> []
406 let item = Domain.choose todo_dom in
407 let remaining_dom = Domain.remove item todo_dom in
408 debug_print (sprintf "CHOOSED ITEM: %s" (string_of_domain_item item));
409 let choices = lookup_choices item in
410 let rec filter = function
412 | codomain_item :: tl ->
413 let new_env = Environment.add item codomain_item current_env in
414 (match test_env new_env remaining_dom with
415 | Ok (term, metasenv) ->
416 (if Domain.is_empty remaining_dom then
417 [ new_env, term, metasenv ]
419 aux new_env remaining_dom)
422 (if Domain.is_empty remaining_dom then
425 aux new_env remaining_dom)
431 let (choosed_env, choosed_term, choosed_metasenv) =
432 match aux current_env todo_dom with
433 | [] -> raise NoWellTypedInterpretation
435 debug_print "UNA SOLA SCELTA";
438 debug_print (sprintf "PIU' SCELTE (%d)" (List.length l));
445 fst (Environment.find domain_item env)
447 (descr_of_domain_item domain_item, description))
448 (Domain.elements term_dom))
451 let choosed = C.interactive_interpretation_choice choices in
454 (choosed_env, choosed_metasenv, choosed_term)