1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
26 exception NotImplemented;;
29 {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
33 let res = "i" ^ string_of_int !seed in
38 let fresh_id seed ids_to_terms ids_to_father_ids =
40 let res = gen_id seed in
41 Hashtbl.add ids_to_father_ids res father ;
42 Hashtbl.add ids_to_terms res t ;
46 exception NotEnoughElements;;
47 exception NameExpected;;
49 (*CSC: cut&paste da cicPp.ml *)
50 (* get_nth l n returns the nth element of the list l if it exists or *)
51 (* raises NotEnoughElements if l has less than n elements *)
55 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
56 | (_,_) -> raise NotEnoughElements
59 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
60 ids_to_inner_types metasenv context idrefs t expectedty
62 let module D = DoubleTypeInference in
63 let module T = CicTypeChecker in
65 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
67 D.double_type_of metasenv context t expectedty
69 let rec aux computeinnertypes father context idrefs tt =
70 let fresh_id'' = fresh_id' father tt in
71 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
72 let aux' = aux computeinnertypes (Some fresh_id'') in
73 (* First of all we compute the inner type and the inner sort *)
74 (* of the term. They may be useful in what follows. *)
75 (*CSC: This is a very inefficient way of computing inner types *)
76 (*CSC: and inner sorts: very deep terms have their types/sorts *)
77 (*CSC: computed again and again. *)
78 let string_of_sort t =
79 match CicReduction.whd context t with
80 C.Sort C.Prop -> "Prop"
81 | C.Sort C.Set -> "Set"
82 | C.Sort C.Type -> "Type"
85 let ainnertypes,innertype,innersort,expected_available =
86 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
87 (*CSC: (expected type + inferred type). Just for now we use the usual *)
88 (*CSC: type-inference, but the result is very poor. As a very weak *)
89 (*CSC: patch, I apply whd to the computed type. Full beta *)
90 (*CSC: reduction would be a much better option. *)
91 let {D.synthesized = synthesized; D.expected = expected} =
92 if computeinnertypes then
93 D.CicHash.find terms_to_types tt
95 (* We are already in an inner-type and Coscoy's double *)
96 (* type inference algorithm has not been applied. *)
98 CicReduction.whd context (T.type_of_aux' metasenv context tt) ;
101 let innersort = T.type_of_aux' metasenv context synthesized in
102 let ainnertypes,expected_available =
103 if computeinnertypes then
104 let annexpected,expected_available =
107 | Some expectedty' ->
109 (aux false (Some fresh_id'') context idrefs expectedty'),
114 aux false (Some fresh_id'') context idrefs synthesized ;
115 annexpected = annexpected
116 }, expected_available
120 ainnertypes,synthesized, string_of_sort innersort, expected_available
122 let add_inner_type id =
123 match ainnertypes with
125 | Some ainnertypes -> Hashtbl.add ids_to_inner_types id ainnertypes
130 match get_nth context n with
131 (Some (C.Name s,_)) -> s
132 | _ -> raise NameExpected
134 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
135 if innersort = "Prop" && expected_available then
136 add_inner_type fresh_id'' ;
137 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
138 | C.Var (uri,exp_named_subst) ->
139 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
140 if innersort = "Prop" && expected_available then
141 add_inner_type fresh_id'' ;
142 let exp_named_subst' =
144 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
146 C.AVar (fresh_id'', uri,exp_named_subst')
148 let (_,canonical_context,_) =
149 List.find (function (m,_,_) -> n = m) metasenv
151 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
152 if innersort = "Prop" && expected_available then
153 add_inner_type fresh_id'' ;
154 C.AMeta (fresh_id'', n,
159 | _, Some t -> Some (aux' context idrefs t)
160 | Some _, None -> assert false (* due to typing rules *))
161 canonical_context l))
162 | C.Sort s -> C.ASort (fresh_id'', s)
163 | C.Implicit -> C.AImplicit (fresh_id'')
165 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
166 if innersort = "Prop" then
167 add_inner_type fresh_id'' ;
168 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
170 Hashtbl.add ids_to_inner_sorts fresh_id''
171 (string_of_sort innertype) ;
173 (fresh_id'', n, aux' context idrefs s,
174 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
175 | C.Lambda (n,s,t) ->
176 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
177 if innersort = "Prop" then
179 let father_is_lambda =
183 match Hashtbl.find ids_to_terms father' with
187 if (not father_is_lambda) || expected_available then
188 add_inner_type fresh_id''
191 (fresh_id'',n, aux' context idrefs s,
192 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
194 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
195 if innersort = "Prop" then
196 add_inner_type fresh_id'' ;
198 (fresh_id'', n, aux' context idrefs s,
199 aux' ((Some (n, C.Def s))::context) (fresh_id''::idrefs) t)
201 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
202 if innersort = "Prop" then
203 add_inner_type fresh_id'' ;
204 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
205 | C.Const (uri,exp_named_subst) ->
206 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
207 if innersort = "Prop" && expected_available then
208 add_inner_type fresh_id'' ;
209 let exp_named_subst' =
211 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
213 C.AConst (fresh_id'', uri, exp_named_subst')
214 | C.MutInd (uri,tyno,exp_named_subst) ->
215 let exp_named_subst' =
217 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
219 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
220 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
221 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
222 if innersort = "Prop" && expected_available then
223 add_inner_type fresh_id'' ;
224 let exp_named_subst' =
226 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
228 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
229 | C.MutCase (uri, tyno, outty, term, patterns) ->
230 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
231 if innersort = "Prop" then
232 add_inner_type fresh_id'' ;
233 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
234 aux' context idrefs term, List.map (aux' context idrefs) patterns)
235 | C.Fix (funno, funs) ->
237 List.map (function _ -> gen_id seed) funs in
238 let new_idrefs = List.rev fresh_idrefs @ idrefs in
240 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
242 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
243 if innersort = "Prop" then
244 add_inner_type fresh_id'' ;
245 C.AFix (fresh_id'', funno,
247 (fun id (name, indidx, ty, bo) ->
248 (id, name, indidx, aux' context idrefs ty,
249 aux' (tys@context) new_idrefs bo)
252 | C.CoFix (funno, funs) ->
254 List.map (function _ -> gen_id seed) funs in
255 let new_idrefs = List.rev fresh_idrefs @ idrefs in
257 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
259 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
260 if innersort = "Prop" then
261 add_inner_type fresh_id'' ;
262 C.ACoFix (fresh_id'', funno,
264 (fun id (name, ty, bo) ->
265 (id, name, aux' context idrefs ty,
266 aux' (tys@context) new_idrefs bo)
270 aux true None context idrefs t
273 let acic_of_cic_context metasenv context idrefs t =
274 let ids_to_terms = Hashtbl.create 503 in
275 let ids_to_father_ids = Hashtbl.create 503 in
276 let ids_to_inner_sorts = Hashtbl.create 503 in
277 let ids_to_inner_types = Hashtbl.create 503 in
279 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
280 ids_to_inner_types metasenv context idrefs t,
281 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
284 let acic_object_of_cic_object obj =
285 let module C = Cic in
286 let ids_to_terms = Hashtbl.create 503 in
287 let ids_to_father_ids = Hashtbl.create 503 in
288 let ids_to_inner_sorts = Hashtbl.create 503 in
289 let ids_to_inner_types = Hashtbl.create 503 in
290 let ids_to_conjectures = Hashtbl.create 11 in
291 let ids_to_hypotheses = Hashtbl.create 127 in
292 let hypotheses_seed = ref 0 in
293 let conjectures_seed = ref 0 in
295 let acic_term_of_cic_term_context' =
296 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
297 ids_to_inner_types in
298 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
301 C.Constant (id,Some bo,ty,params) ->
302 let abo = acic_term_of_cic_term' bo (Some ty) in
303 let aty = acic_term_of_cic_term' ty None in
305 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty, params)
306 | C.Constant (id,None,ty,params) -> raise NotImplemented
307 | C.Variable (id,bo,ty,params) -> raise NotImplemented
308 | C.CurrentProof (id,conjectures,bo,ty,params) ->
311 (function (i,canonical_context,term) as conjecture ->
312 let cid = "c" ^ string_of_int !conjectures_seed in
313 Hashtbl.add ids_to_conjectures cid conjecture ;
314 incr conjectures_seed ;
315 let idrefs',revacanonical_context =
316 let rec aux context idrefs =
320 let hid = "h" ^ string_of_int !hypotheses_seed in
321 let new_idrefs = hid::idrefs in
322 Hashtbl.add ids_to_hypotheses hid hyp ;
323 incr hypotheses_seed ;
325 (Some (n,C.Decl t)) ->
326 let final_idrefs,atl =
327 aux (hyp::context) new_idrefs tl in
329 acic_term_of_cic_term_context'
330 conjectures context idrefs t None
332 final_idrefs,(hid,Some (n,C.ADecl at))::atl
333 | (Some (n,C.Def t)) ->
334 let final_idrefs,atl =
335 aux (hyp::context) new_idrefs tl in
337 acic_term_of_cic_term_context'
338 conjectures context idrefs t None
340 final_idrefs,(hid,Some (n,C.ADef at))::atl
342 let final_idrefs,atl =
343 aux (hyp::context) new_idrefs tl
345 final_idrefs,(hid,None)::atl
347 aux [] [] (List.rev canonical_context)
350 acic_term_of_cic_term_context' conjectures
351 canonical_context idrefs' term None
353 (cid,i,(List.rev revacanonical_context),aterm)
356 acic_term_of_cic_term_context' conjectures [] [] bo (Some ty) in
357 let aty = acic_term_of_cic_term_context' conjectures [] [] ty None in
359 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
360 | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
362 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
363 ids_to_conjectures,ids_to_hypotheses