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_env' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
51 ids_to_inner_types metasenv env t
53 let module T = CicTypeChecker in
55 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
56 let rec aux computeinnertypes father bs tt =
57 let fresh_id'' = fresh_id' father tt in
58 let aux' = aux true (Some fresh_id'') in
59 (* First of all we compute the inner type and the inner sort *)
60 (* of the term. They may be useful in what follows. *)
61 (*CSC: This is a very inefficient way of computing inner types *)
62 (*CSC: and inner sorts: very deep terms have their types/sorts *)
63 (*CSC: computed again and again. *)
66 C.Sort C.Prop -> "Prop"
67 | C.Sort C.Set -> "Set"
68 | C.Sort C.Type -> "Type"
71 let ainnertype,innertype,innersort =
72 let cicenv = List.map (function (_,ty) -> ty) bs in
73 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
74 (*CSC: (expected type + inferred type). Just for now we use the usual *)
75 (*CSC: type-inference, but the result is very poort. As a very weak *)
76 (*CSC: patch, I apply whd to the computed type. Full beta *)
77 (*CSC: reduction would be a much better option. *)
79 CicReduction.whd cicenv (T.type_of_aux' metasenv cicenv tt)
81 let innersort = T.type_of_aux' metasenv cicenv innertype in
83 if computeinnertypes then
84 Some (aux false (Some fresh_id'') bs innertype)
88 ainnertype, innertype, string_of_sort innersort
90 let add_inner_type id =
93 | Some ainnertype -> Hashtbl.add ids_to_inner_types id ainnertype
98 match get_nth bs n with
100 | _ -> raise NameExpected
102 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
103 C.ARel (fresh_id'', n, id)
105 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
106 C.AVar (fresh_id'', uri)
108 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
109 C.AMeta (fresh_id'', n)
110 | C.Sort s -> C.ASort (fresh_id'', s)
111 | C.Implicit -> C.AImplicit (fresh_id'')
113 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
114 if innersort = "Prop" then
115 add_inner_type fresh_id'' ;
116 C.ACast (fresh_id'', aux' bs v, aux' bs t)
118 Hashtbl.add ids_to_inner_sorts fresh_id''
119 (string_of_sort innertype) ;
120 C.AProd (fresh_id'', n, aux' bs s, aux' ((n, C.Decl s)::bs) t)
121 | C.Lambda (n,s,t) ->
122 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
123 if innersort = "Prop" then
125 let father_is_lambda =
129 match Hashtbl.find ids_to_terms father' with
133 if not father_is_lambda then
134 add_inner_type fresh_id''
136 C.ALambda (fresh_id'',n, aux' bs s, aux' ((n, C.Decl s)::bs) t)
138 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
139 C.ALetIn (fresh_id'', n, aux' bs s, aux' ((n, C.Def s)::bs) t)
141 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
142 if innersort = "Prop" then
143 add_inner_type fresh_id'' ;
144 C.AAppl (fresh_id'', List.map (aux' bs) l)
145 | C.Const (uri,cn) ->
146 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
147 C.AConst (fresh_id'', uri, cn)
148 | C.Abst _ -> raise NotImplemented
149 | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
150 | C.MutConstruct (uri,cn,tyno,consno) ->
151 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
152 C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
153 | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
154 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
155 if innersort = "Prop" then
156 add_inner_type fresh_id'' ;
157 C.AMutCase (fresh_id'', uri, cn, tyno, aux' bs outty,
158 aux' bs term, List.map (aux' bs) patterns)
159 | C.Fix (funno, funs) ->
161 List.map (fun (name,_,ty,_) -> C.Name name, C.Decl ty) funs
163 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
164 if innersort = "Prop" then
165 add_inner_type fresh_id'' ;
166 C.AFix (fresh_id'', funno,
168 (fun (name, indidx, ty, bo) ->
169 (name, indidx, aux' bs ty, aux' (names@bs) bo)
172 | C.CoFix (funno, funs) ->
174 List.map (fun (name,ty,_) -> C.Name name, C.Decl ty) funs in
175 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
176 if innersort = "Prop" then
177 add_inner_type fresh_id'' ;
178 C.ACoFix (fresh_id'', funno,
180 (fun (name, ty, bo) ->
181 (name, aux' bs ty, aux' (names@bs) bo)
188 let acic_of_cic_env metasenv env t =
189 let ids_to_terms = Hashtbl.create 503 in
190 let ids_to_father_ids = Hashtbl.create 503 in
191 let ids_to_inner_sorts = Hashtbl.create 503 in
192 let ids_to_inner_types = Hashtbl.create 503 in
194 acic_of_cic_env' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
195 ids_to_inner_types metasenv env t,
196 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
199 exception Found of (Cic.name * Cic.context_entry) list;;
201 (* get_context_of_meta meta term *)
202 (* returns the context of the occurrence of [meta] in [term]. *)
203 (* Warning: if [meta] occurs not linearly in [term], the context *)
204 (* of one "random" occurrence is returned. *)
205 let get_context_of_meta meta term =
206 let module C = Cic in
211 | C.Meta i when meta = i -> raise (Found ctx)
215 | C.Cast (te,ty) -> aux ctx te ; aux ctx ty
216 | C.Prod (n,s,t) -> aux ctx s ; aux ((n, C.Decl s)::ctx) t
217 | C.Lambda (n,s,t) -> aux ctx s ; aux ((n, C.Decl s)::ctx) t
218 | C.LetIn (n,s,t) -> aux ctx s ; aux ((n, C.Def s)::ctx) t
219 | C.Appl l -> List.iter (aux ctx) l
221 | C.Abst _ -> assert false
223 | C.MutConstruct _ -> ()
224 | C.MutCase (_,_,_,outt,t,pl) ->
225 aux ctx outt ; aux ctx t; List.iter (aux ctx) pl
227 let counter = ref 0 in
230 (function (name,_,ty,bo) ->
231 let res = (C.Name name, C.Def (C.Fix (!counter,ifl))) in
237 List.iter (function (_,_,ty,bo) -> aux ctx ty ; aux ctx' bo) ifl
239 let counter = ref 0 in
242 (function (name,ty,bo) ->
243 let res = (C.Name name, C.Def (C.CoFix (!counter,ifl))) in
249 List.iter (function (_,ty,bo) -> aux ctx ty ; aux ctx' bo) ifl
253 assert false (* No occurrences found. *)
255 Found context -> context
258 exception NotImplemented;;
260 let acic_object_of_cic_object obj =
261 let module C = Cic in
262 let ids_to_terms = Hashtbl.create 503 in
263 let ids_to_father_ids = Hashtbl.create 503 in
264 let ids_to_inner_sorts = Hashtbl.create 503 in
265 let ids_to_inner_types = Hashtbl.create 503 in
267 let acic_term_of_cic_term_env' =
268 acic_of_cic_env' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
269 ids_to_inner_types in
270 let acic_term_of_cic_term' = acic_term_of_cic_term_env' [] [] in
273 C.Definition (id,bo,ty,params) ->
274 let abo = acic_term_of_cic_term' bo in
275 let aty = acic_term_of_cic_term' ty
277 C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
278 | C.Axiom (id,ty,params) -> raise NotImplemented
279 | C.Variable (id,bo,ty) -> raise NotImplemented
280 | C.CurrentProof (id,conjectures,bo,ty) ->
283 (function (i,term) ->
284 let context = get_context_of_meta i bo in
285 let aterm = acic_term_of_cic_term_env' conjectures context term in
288 let abo = acic_term_of_cic_term_env' conjectures [] bo in
289 let aty = acic_term_of_cic_term_env' conjectures [] ty in
290 C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
291 | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
293 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types