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}
32 let fresh_id seed ids_to_terms ids_to_father_ids =
34 let res = "i" ^ string_of_int !seed in
36 Hashtbl.add ids_to_father_ids res father ;
37 Hashtbl.add ids_to_terms res t ;
41 exception NotEnoughElements;;
42 exception NameExpected;;
44 (*CSC: cut&paste da cicPp.ml *)
45 (* get_nth l n returns the nth element of the list l if it exists or *)
46 (* raises NotEnoughElements if l has less than n elements *)
50 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
51 | (_,_) -> raise NotEnoughElements
54 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
55 ids_to_inner_types metasenv context t expectedty
57 let module D = DoubleTypeInference in
58 let module T = CicTypeChecker in
60 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
62 D.double_type_of metasenv context t expectedty
64 let rec aux computeinnertypes father context tt =
65 let fresh_id'' = fresh_id' father tt in
66 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
67 let aux' = aux computeinnertypes (Some fresh_id'') in
68 (* First of all we compute the inner type and the inner sort *)
69 (* of the term. They may be useful in what follows. *)
70 (*CSC: This is a very inefficient way of computing inner types *)
71 (*CSC: and inner sorts: very deep terms have their types/sorts *)
72 (*CSC: computed again and again. *)
75 C.Sort C.Prop -> "Prop"
76 | C.Sort C.Set -> "Set"
77 | C.Sort C.Type -> "Type"
80 let ainnertypes,innertype,innersort,expected_available =
81 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
82 (*CSC: (expected type + inferred type). Just for now we use the usual *)
83 (*CSC: type-inference, but the result is very poor. As a very weak *)
84 (*CSC: patch, I apply whd to the computed type. Full beta *)
85 (*CSC: reduction would be a much better option. *)
86 let {D.synthesized = synthesized; D.expected = expected} =
87 if computeinnertypes then
88 D.CicHash.find terms_to_types tt
90 (* We are already in an inner-type and Coscoy's double *)
91 (* type inference algorithm has not been applied. *)
93 CicReduction.whd context (T.type_of_aux' metasenv context tt) ;
96 let innersort = T.type_of_aux' metasenv context synthesized in
97 let ainnertypes,expected_available =
98 if computeinnertypes then
99 let annexpected,expected_available =
102 | Some expectedty' ->
103 prerr_endline ("###: " ^ CicPp.ppterm synthesized ^ " <==> " ^ CicPp.ppterm expectedty') ; flush stderr ;
104 Some (aux false (Some fresh_id'') context expectedty'),true
108 aux false (Some fresh_id'') context synthesized ;
109 annexpected = annexpected
110 }, expected_available
114 ainnertypes,synthesized, string_of_sort innersort, expected_available
116 let add_inner_type id =
117 match ainnertypes with
119 | Some ainnertypes -> Hashtbl.add ids_to_inner_types id ainnertypes
124 match get_nth context n with
125 (Some (C.Name s,_)) -> s
126 | _ -> raise NameExpected
128 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
129 if innersort = "Prop" && expected_available then
130 add_inner_type fresh_id'' ;
131 C.ARel (fresh_id'', n, id)
133 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
134 if innersort = "Prop" && expected_available then
135 add_inner_type fresh_id'' ;
136 C.AVar (fresh_id'', uri)
138 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
139 if innersort = "Prop" && expected_available then
140 add_inner_type fresh_id'' ;
141 C.AMeta (fresh_id'', n,
143 (function None -> None | Some t -> Some (aux' context t)) l))
144 | C.Sort s -> C.ASort (fresh_id'', s)
145 | C.Implicit -> C.AImplicit (fresh_id'')
147 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
148 if innersort = "Prop" then
149 add_inner_type fresh_id'' ;
150 C.ACast (fresh_id'', aux' context v, aux' context t)
152 Hashtbl.add ids_to_inner_sorts fresh_id''
153 (string_of_sort innertype) ;
155 (fresh_id'', n, aux' context s,
156 aux' ((Some (n, C.Decl s))::context) t)
157 | C.Lambda (n,s,t) ->
158 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
159 if innersort = "Prop" then
161 let father_is_lambda =
165 match Hashtbl.find ids_to_terms father' with
169 if (not father_is_lambda) || expected_available then
170 add_inner_type fresh_id''
173 (fresh_id'',n, aux' context s,
174 aux' ((Some (n, C.Decl s)::context)) t)
176 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
177 if innersort = "Prop" then
178 add_inner_type fresh_id'' ;
180 (fresh_id'', n, aux' context s,
181 aux' ((Some (n, C.Def s))::context) t)
183 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
184 if innersort = "Prop" then
185 add_inner_type fresh_id'' ;
186 C.AAppl (fresh_id'', List.map (aux' context) l)
187 | C.Const (uri,cn) ->
188 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
189 if innersort = "Prop" && expected_available then
190 add_inner_type fresh_id'' ;
191 C.AConst (fresh_id'', uri, cn)
192 | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
193 | C.MutConstruct (uri,cn,tyno,consno) ->
194 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
195 if innersort = "Prop" && expected_available then
196 add_inner_type fresh_id'' ;
197 C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
198 | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
199 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
200 if innersort = "Prop" then
201 add_inner_type fresh_id'' ;
202 C.AMutCase (fresh_id'', uri, cn, tyno, aux' context outty,
203 aux' context term, List.map (aux' context) patterns)
204 | C.Fix (funno, funs) ->
206 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
208 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
209 if innersort = "Prop" then
210 add_inner_type fresh_id'' ;
211 C.AFix (fresh_id'', funno,
213 (fun (name, indidx, ty, bo) ->
214 (name, indidx, aux' context ty, aux' (tys@context) bo)
217 | C.CoFix (funno, funs) ->
219 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
221 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
222 if innersort = "Prop" then
223 add_inner_type fresh_id'' ;
224 C.ACoFix (fresh_id'', funno,
226 (fun (name, ty, bo) ->
227 (name, aux' context ty, aux' (tys@context) bo)
231 aux true None context t
234 let acic_of_cic_context metasenv context t =
235 let ids_to_terms = Hashtbl.create 503 in
236 let ids_to_father_ids = Hashtbl.create 503 in
237 let ids_to_inner_sorts = Hashtbl.create 503 in
238 let ids_to_inner_types = Hashtbl.create 503 in
240 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
241 ids_to_inner_types metasenv context t,
242 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
245 let acic_object_of_cic_object obj =
246 let module C = Cic in
247 let ids_to_terms = Hashtbl.create 503 in
248 let ids_to_father_ids = Hashtbl.create 503 in
249 let ids_to_inner_sorts = Hashtbl.create 503 in
250 let ids_to_inner_types = Hashtbl.create 503 in
251 let ids_to_conjectures = Hashtbl.create 11 in
252 let ids_to_hypotheses = Hashtbl.create 127 in
253 let hypotheses_seed = ref 0 in
254 let conjectures_seed = ref 0 in
256 let acic_term_of_cic_term_context' =
257 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
258 ids_to_inner_types in
259 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] in
262 C.Definition (id,bo,ty,params) ->
263 let abo = acic_term_of_cic_term' bo (Some ty) in
264 let aty = acic_term_of_cic_term' ty None in
265 C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
266 | C.Axiom (id,ty,params) -> raise NotImplemented
267 | C.Variable (id,bo,ty) -> raise NotImplemented
268 | C.CurrentProof (id,conjectures,bo,ty) ->
271 (function (i,canonical_context,term) as conjecture ->
272 let cid = "c" ^ string_of_int !conjectures_seed in
273 Hashtbl.add ids_to_conjectures cid conjecture ;
274 incr conjectures_seed ;
275 let acanonical_context =
280 let hid = "h" ^ string_of_int !hypotheses_seed in
281 Hashtbl.add ids_to_hypotheses hid hyp ;
282 incr hypotheses_seed ;
284 (Some (n,C.Decl t)) ->
286 acic_term_of_cic_term_context' conjectures tl t None
288 (hid,Some (n,C.ADecl at))::(aux tl)
289 | (Some (n,C.Def t)) ->
291 acic_term_of_cic_term_context' conjectures tl t None
293 (hid,Some (n,C.ADef at))::(aux tl)
294 | None -> (hid,None)::(aux tl)
296 aux canonical_context
299 acic_term_of_cic_term_context' conjectures canonical_context
302 (cid,i,acanonical_context,aterm)
304 let abo = acic_term_of_cic_term_context' conjectures [] bo (Some ty) in
305 let aty = acic_term_of_cic_term_context' conjectures [] ty None in
306 C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
307 | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
309 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
310 ids_to_conjectures,ids_to_hypotheses