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;;
28 let fresh_id seed ids_to_terms ids_to_father_ids =
30 let res = "i" ^ string_of_int !seed in
32 Hashtbl.add ids_to_father_ids res father ;
33 Hashtbl.add ids_to_terms res t ;
37 exception NotEnoughElements;;
38 exception NameExpected;;
40 (*CSC: cut&paste da cicPp.ml *)
41 (* get_nth l n returns the nth element of the list l if it exists or *)
42 (* raises NotEnoughElements if l has less than n elements *)
46 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
47 | (_,_) -> raise NotEnoughElements
50 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
51 ids_to_inner_types metasenv context t
53 let module T = CicTypeChecker in
55 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
56 let terms_to_types = DoubleTypeInference.double_type_of metasenv context t in
57 let rec aux computeinnertypes father context tt =
58 let fresh_id'' = fresh_id' father tt in
59 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
60 let aux' = aux computeinnertypes (Some fresh_id'') in
61 (* First of all we compute the inner type and the inner sort *)
62 (* of the term. They may be useful in what follows. *)
63 (*CSC: This is a very inefficient way of computing inner types *)
64 (*CSC: and inner sorts: very deep terms have their types/sorts *)
65 (*CSC: computed again and again. *)
68 C.Sort C.Prop -> "Prop"
69 | C.Sort C.Set -> "Set"
70 | C.Sort C.Type -> "Type"
73 let ainnertype,innertype,innersort =
74 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
75 (*CSC: (expected type + inferred type). Just for now we use the usual *)
76 (*CSC: type-inference, but the result is very poor. As a very weak *)
77 (*CSC: patch, I apply whd to the computed type. Full beta *)
78 (*CSC: reduction would be a much better option. *)
80 if computeinnertypes then
81 let {DoubleTypeInference.synthesized = synthesized} =
82 DoubleTypeInference.CicHash.find terms_to_types tt
86 (* We are already in an inner-type and Coscoy's double *)
87 (* type inference algorithm has not been applied. *)
88 CicReduction.whd context (T.type_of_aux' metasenv context tt)
90 let innersort = T.type_of_aux' metasenv context innertype in
92 if computeinnertypes then
93 Some (aux false (Some fresh_id'') context innertype)
97 ainnertype, innertype, string_of_sort innersort
99 let add_inner_type id =
100 match ainnertype with
102 | Some ainnertype -> Hashtbl.add ids_to_inner_types id ainnertype
107 match get_nth context n with
108 (Some (C.Name s,_)) -> s
109 | _ -> raise NameExpected
111 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
112 C.ARel (fresh_id'', n, id)
114 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
115 C.AVar (fresh_id'', uri)
117 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
118 C.AMeta (fresh_id'', n,
120 (function None -> None | Some t -> Some (aux' context t)) l))
121 | C.Sort s -> C.ASort (fresh_id'', s)
122 | C.Implicit -> C.AImplicit (fresh_id'')
124 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
125 if innersort = "Prop" then
126 add_inner_type fresh_id'' ;
127 C.ACast (fresh_id'', aux' context v, aux' context t)
129 Hashtbl.add ids_to_inner_sorts fresh_id''
130 (string_of_sort innertype) ;
132 (fresh_id'', n, aux' context s,
133 aux' ((Some (n, C.Decl s))::context) t)
134 | C.Lambda (n,s,t) ->
135 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
136 if innersort = "Prop" then
138 let father_is_lambda =
142 match Hashtbl.find ids_to_terms father' with
146 if not father_is_lambda then
147 add_inner_type fresh_id''
150 (fresh_id'',n, aux' context s,
151 aux' ((Some (n, C.Decl s)::context)) t)
153 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
155 (fresh_id'', n, aux' context s,
156 aux' ((Some (n, C.Def s))::context) t)
158 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
159 if innersort = "Prop" then
160 add_inner_type fresh_id'' ;
161 C.AAppl (fresh_id'', List.map (aux' context) l)
162 | C.Const (uri,cn) ->
163 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
164 C.AConst (fresh_id'', uri, cn)
165 | C.Abst _ -> raise NotImplemented
166 | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
167 | C.MutConstruct (uri,cn,tyno,consno) ->
168 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
169 C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
170 | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
171 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
172 if innersort = "Prop" then
173 add_inner_type fresh_id'' ;
174 C.AMutCase (fresh_id'', uri, cn, tyno, aux' context outty,
175 aux' context term, List.map (aux' context) patterns)
176 | C.Fix (funno, funs) ->
178 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
180 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
181 if innersort = "Prop" then
182 add_inner_type fresh_id'' ;
183 C.AFix (fresh_id'', funno,
185 (fun (name, indidx, ty, bo) ->
186 (name, indidx, aux' context ty, aux' (tys@context) bo)
189 | C.CoFix (funno, funs) ->
191 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs in
192 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
193 if innersort = "Prop" then
194 add_inner_type fresh_id'' ;
195 C.ACoFix (fresh_id'', funno,
197 (fun (name, ty, bo) ->
198 (name, aux' context ty, aux' (tys@context) bo)
202 aux true None context t
205 let acic_of_cic_context metasenv context t =
206 let ids_to_terms = Hashtbl.create 503 in
207 let ids_to_father_ids = Hashtbl.create 503 in
208 let ids_to_inner_sorts = Hashtbl.create 503 in
209 let ids_to_inner_types = Hashtbl.create 503 in
211 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
212 ids_to_inner_types metasenv context t,
213 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
216 let acic_object_of_cic_object obj =
217 let module C = Cic in
218 let ids_to_terms = Hashtbl.create 503 in
219 let ids_to_father_ids = Hashtbl.create 503 in
220 let ids_to_inner_sorts = Hashtbl.create 503 in
221 let ids_to_inner_types = Hashtbl.create 503 in
222 let ids_to_conjectures = Hashtbl.create 11 in
223 let ids_to_hypotheses = Hashtbl.create 127 in
224 let hypotheses_seed = ref 0 in
225 let conjectures_seed = ref 0 in
227 let acic_term_of_cic_term_context' =
228 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
229 ids_to_inner_types in
230 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] in
233 C.Definition (id,bo,ty,params) ->
234 let abo = acic_term_of_cic_term' bo in
235 let aty = acic_term_of_cic_term' ty
237 C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
238 | C.Axiom (id,ty,params) -> raise NotImplemented
239 | C.Variable (id,bo,ty) -> raise NotImplemented
240 | C.CurrentProof (id,conjectures,bo,ty) ->
243 (function (i,canonical_context,term) as conjecture ->
244 let cid = "c" ^ string_of_int !conjectures_seed in
245 Hashtbl.add ids_to_conjectures cid conjecture ;
246 incr conjectures_seed ;
247 let acanonical_context =
252 let hid = "h" ^ string_of_int !hypotheses_seed in
253 Hashtbl.add ids_to_hypotheses hid hyp ;
254 incr hypotheses_seed ;
256 (Some (n,C.Decl t)) ->
258 acic_term_of_cic_term_context' conjectures tl t
260 (hid,Some (n,C.ADecl at))::(aux tl)
261 | (Some (n,C.Def t)) ->
263 acic_term_of_cic_term_context' conjectures tl t
265 (hid,Some (n,C.ADef at))::(aux tl)
266 | None -> (hid,None)::(aux tl)
268 aux canonical_context
271 acic_term_of_cic_term_context' conjectures canonical_context term
273 (cid,i,acanonical_context,aterm)
275 let abo = acic_term_of_cic_term_context' conjectures [] bo in
276 let aty = acic_term_of_cic_term_context' conjectures [] ty in
277 C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
278 | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
280 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
281 ids_to_conjectures,ids_to_hypotheses