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 rec aux computeinnertypes father context 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 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
73 (*CSC: (expected type + inferred type). Just for now we use the usual *)
74 (*CSC: type-inference, but the result is very poort. As a very weak *)
75 (*CSC: patch, I apply whd to the computed type. Full beta *)
76 (*CSC: reduction would be a much better option. *)
78 CicReduction.whd context (T.type_of_aux' metasenv context tt)
80 let innersort = T.type_of_aux' metasenv context innertype in
82 if computeinnertypes then
83 Some (aux false (Some fresh_id'') context innertype)
87 ainnertype, innertype, string_of_sort innersort
89 let add_inner_type id =
92 | Some ainnertype -> Hashtbl.add ids_to_inner_types id ainnertype
97 match get_nth context n with
98 (Some (C.Name s,_)) -> s
99 | _ -> raise NameExpected
101 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
102 C.ARel (fresh_id'', n, id)
104 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
105 C.AVar (fresh_id'', uri)
107 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
108 C.AMeta (fresh_id'', n,
110 (function None -> None | Some t -> Some (aux' context t)) l))
111 | C.Sort s -> C.ASort (fresh_id'', s)
112 | C.Implicit -> C.AImplicit (fresh_id'')
114 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
115 if innersort = "Prop" then
116 add_inner_type fresh_id'' ;
117 C.ACast (fresh_id'', aux' context v, aux' context t)
119 Hashtbl.add ids_to_inner_sorts fresh_id''
120 (string_of_sort innertype) ;
122 (fresh_id'', n, aux' context s,
123 aux' ((Some (n, C.Decl s))::context) t)
124 | C.Lambda (n,s,t) ->
125 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
126 if innersort = "Prop" then
128 let father_is_lambda =
132 match Hashtbl.find ids_to_terms father' with
136 if not father_is_lambda then
137 add_inner_type fresh_id''
140 (fresh_id'',n, aux' context s,
141 aux' ((Some (n, C.Decl s)::context)) t)
143 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
145 (fresh_id'', n, aux' context s,
146 aux' ((Some (n, C.Def s))::context) t)
148 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
149 if innersort = "Prop" then
150 add_inner_type fresh_id'' ;
151 C.AAppl (fresh_id'', List.map (aux' context) l)
152 | C.Const (uri,cn) ->
153 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
154 C.AConst (fresh_id'', uri, cn)
155 | C.Abst _ -> raise NotImplemented
156 | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
157 | C.MutConstruct (uri,cn,tyno,consno) ->
158 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
159 C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
160 | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
161 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
162 if innersort = "Prop" then
163 add_inner_type fresh_id'' ;
164 C.AMutCase (fresh_id'', uri, cn, tyno, aux' context outty,
165 aux' context term, List.map (aux' context) patterns)
166 | C.Fix (funno, funs) ->
168 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
170 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
171 if innersort = "Prop" then
172 add_inner_type fresh_id'' ;
173 C.AFix (fresh_id'', funno,
175 (fun (name, indidx, ty, bo) ->
176 (name, indidx, aux' context ty, aux' (tys@context) bo)
179 | C.CoFix (funno, funs) ->
181 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs in
182 Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
183 if innersort = "Prop" then
184 add_inner_type fresh_id'' ;
185 C.ACoFix (fresh_id'', funno,
187 (fun (name, ty, bo) ->
188 (name, aux' context ty, aux' (tys@context) bo)
192 aux true None context t
195 let acic_of_cic_context metasenv context t =
196 let ids_to_terms = Hashtbl.create 503 in
197 let ids_to_father_ids = Hashtbl.create 503 in
198 let ids_to_inner_sorts = Hashtbl.create 503 in
199 let ids_to_inner_types = Hashtbl.create 503 in
201 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
202 ids_to_inner_types metasenv context t,
203 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
206 exception Found of (Cic.name * Cic.context_entry) list;;
208 exception NotImplemented;;
210 let acic_object_of_cic_object obj =
211 let module C = Cic in
212 let ids_to_terms = Hashtbl.create 503 in
213 let ids_to_father_ids = Hashtbl.create 503 in
214 let ids_to_inner_sorts = Hashtbl.create 503 in
215 let ids_to_inner_types = Hashtbl.create 503 in
217 let acic_term_of_cic_term_context' =
218 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
219 ids_to_inner_types in
220 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] in
223 C.Definition (id,bo,ty,params) ->
224 let abo = acic_term_of_cic_term' bo in
225 let aty = acic_term_of_cic_term' ty
227 C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
228 | C.Axiom (id,ty,params) -> raise NotImplemented
229 | C.Variable (id,bo,ty) -> raise NotImplemented
230 | C.CurrentProof (id,conjectures,bo,ty) ->
233 (function (i,canonical_context,term) ->
234 let acanonical_context =
238 | (Some (n,C.Decl t))::tl ->
240 acic_term_of_cic_term_context' conjectures tl t
242 Some (n,C.ADecl at)::(aux tl)
243 | (Some (n,C.Def t))::tl ->
245 acic_term_of_cic_term_context' conjectures tl t
247 Some (n,C.ADef at)::(aux tl)
248 | None::tl -> None::(aux tl)
250 aux canonical_context
253 acic_term_of_cic_term_context' conjectures canonical_context term
255 (i, acanonical_context,aterm)
257 let abo = acic_term_of_cic_term_context' conjectures [] bo in
258 let aty = acic_term_of_cic_term_context' conjectures [] ty in
259 C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
260 | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
262 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types