1 module Ref = NReference
9 | Ce of NCic.hypothesis
10 | Fix of Ref.reference * string * NCic.term
12 let splat mk_pi ctx t =
16 | Ce (name, NCic.Def (bo,ty)) -> NCic.LetIn (name, ty, bo, t)
17 | Ce (name, NCic.Decl ty) when mk_pi -> NCic.Prod (name, ty, t)
18 | Ce (name, NCic.Decl ty) -> NCic.Lambda (name, ty, t)
19 | Fix (_,name,ty) when mk_pi -> NCic.Prod (name, ty, t)
20 | Fix (_,name,ty) -> NCic.Lambda (name,ty,t))
24 let context_tassonomy ctx =
25 let rec split inner acc acc1 = function
26 | Ce _ :: tl when inner -> split inner (acc+1) (acc1+1) tl
27 | Fix _ ::tl -> split false acc (acc1+1) tl
28 | _ as l -> acc, List.length l, acc1
33 let splat_args_for_rel ctx t =
34 let bound, free, primo_ce_dopo_fix = context_tassonomy ctx in
37 let rec aux = function
40 (match List.nth ctx (n+bound) with
41 | Fix (refe, _, _) when (n+bound) < primo_ce_dopo_fix -> NCic.Const refe
42 | Fix _ | Ce _ -> NCic.Rel (n+bound)) :: aux (n-1)
44 NCic.Appl (t:: aux free)
47 let splat_args ctx t n_fix =
48 let bound, free, primo_ce_dopo_fix = context_tassonomy ctx in
51 let rec aux = function
54 (match List.nth ctx (n-1) with
55 | Ce _ when n <= bound -> NCic.Rel n
56 | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
57 splat_args_for_rel ctx (NCic.Const refe)
58 | Fix _ | Ce _ -> NCic.Rel (n - n_fix)
61 NCic.Appl (t:: aux (List.length ctx))
64 (* we are lambda-lifting also variables that do not occur *)
65 (* ctx does not distinguish successive blocks of cofix, since there may be no
66 * lambda separating them *)
67 let convert_term uri t =
68 let rec aux octx (ctx : ctx list) n_fix uri = function
69 | Cic.CoFix (k, fl) ->
71 UriManager.uri_of_string
72 (UriManager.buri_of_uri uri^"/"^
73 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
75 let bctx, fixpoints_tys, tys, _ =
77 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
78 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
79 let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
80 Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
83 let bctx = bctx @ ctx in
84 let n_fl = List.length fl in
87 (fun (types,len) (n,ty,_) ->
88 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
93 (fun (name,_,bo) ty (l,fixpoints) ->
94 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
95 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
96 fixpoints_bo @ fixpoints)
97 fl tys ([],fixpoints_tys)
100 NUri.nuri_of_ouri buri,0,[],[],
101 NCic.Fixpoint (false, fl, (`Generated, `Definition))
104 (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
109 UriManager.uri_of_string
110 (UriManager.buri_of_uri uri^"/"^
111 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
113 let bad_bctx, fixpoints_tys, tys, _ =
115 (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) ->
116 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
117 let r = (* recno is dummy here, must be lifted by the ctx len *)
118 Ref.reference_of_ouri buri (Ref.Fix (idx,recno))
120 Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
123 let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
126 | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
127 Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
128 | _ -> assert false) bad_bctx @ ctx
130 let n_fl = List.length fl in
133 (fun (types,len) (n,_,ty,_) ->
134 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
138 let fl, fixpoints,_ =
140 (fun (name,rno,_,bo) ty (l,fixpoints,idx) ->
141 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
142 let rno = rno + free in
143 if idx = k then rno_k := rno;
144 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
145 fixpoints_bo @ fixpoints,idx+1)
146 fl tys ([],fixpoints_tys,0)
149 NUri.nuri_of_ouri buri,max_int,[],[],
150 NCic.Fixpoint (true, fl, (`Generated, `Definition))
154 (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
158 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
159 (match List.nth ctx (n-1) with
160 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
161 splat_args_for_rel ctx (NCic.Const r), []
162 | Ce _ when n <= bound -> NCic.Rel n, []
163 | Fix _ (* BUG 3 fix nested *)
164 | Ce _ -> NCic.Rel (n-n_fix), [])
165 | Cic.Lambda (name, (s as old_s), t) ->
166 let s, fixpoints_s = aux octx ctx n_fix uri s in
167 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
168 let octx = Some (name, Cic.Decl old_s) :: octx in
169 let t, fixpoints_t = aux octx ctx n_fix uri t in
170 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
171 | Cic.Prod (name, (s as old_s), t) ->
172 let s, fixpoints_s = aux octx ctx n_fix uri s in
173 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
174 let octx = Some (name, Cic.Decl old_s) :: octx in
175 let t, fixpoints_t = aux octx ctx n_fix uri t in
176 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
177 | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
178 let te, fixpoints_s = aux octx ctx n_fix uri te in
179 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
180 let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
181 let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
182 let t, fixpoints_t = aux octx ctx n_fix uri t in
183 NCic.LetIn (cn_to_s name, ty, te, t),
184 fixpoints_s @ fixpoints_t @ fixpoints_ty
186 let t, fixpoints_t = aux octx ctx n_fix uri t in
187 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
188 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
189 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
190 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
191 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
192 | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[]
193 (* calculate depth in the univ_graph*)
198 let t, fixpoints = aux octx ctx n_fix uri t in
199 (t::l,fixpoints@acc))
203 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
204 | _ -> NCic.Appl l, fixpoints)
205 | Cic.Const (curi, ens) ->
206 aux_ens octx ctx n_fix uri ens
207 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
208 | Cic.Constant (_,Some _,_,_,_) ->
209 NCic.Const (Ref.reference_of_ouri curi Ref.Def)
210 | Cic.Constant (_,None,_,_,_) ->
211 NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
213 | Cic.MutInd (curi, tyno, ens) ->
214 aux_ens octx ctx n_fix uri ens
215 (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
216 | Cic.MutConstruct (curi, tyno, consno, ens) ->
217 aux_ens octx ctx n_fix uri ens
218 (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
219 | Cic.MutCase (curi, tyno, oty, t, branches) ->
220 let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
221 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
222 let t, fixpoints_t = aux octx ctx n_fix uri t in
223 let branches, fixpoints =
226 let t, fixpoints = aux octx ctx n_fix uri t in
227 (t::l,fixpoints@acc))
230 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
231 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
232 and aux_ens octx ctx n_fix uri ens he =
238 (fun (_,t) (l,objs) ->
239 let t,o = aux octx ctx n_fix uri t in
243 NCic.Appl (he::ens),objs
249 let t = CicSubstitution.lift (List.length vars) t in
252 let t = CicSubstitution.subst_vars [uri,Cic.Rel 1] t in
254 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
255 Cic.Variable (_,bo,ty,_,_) -> bo,ty
256 | _ -> assert false in
257 let id = Cic.Name (UriManager.name_of_uri uri) in
260 None,ty -> Cic.Lambda (id,ty,t)
261 | Some bo,ty -> Cic.LetIn (id,bo,ty,t)
267 let convert_obj_aux uri = function
268 | Cic.Constant (name, None, ty, vars, _) ->
269 let ty = cook vars ty in
270 let nty, fixpoints = convert_term uri ty in
271 assert(fixpoints = []);
272 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
274 | Cic.Constant (name, Some bo, ty, vars, _) ->
275 let bo = cook vars bo in
276 let ty = cook vars ty in
277 let nbo, fixpoints_bo = convert_term uri bo in
278 let nty, fixpoints_ty = convert_term uri ty in
279 assert(fixpoints_ty = []);
280 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
281 fixpoints_bo @ fixpoints_ty
282 | Cic.InductiveDefinition (itl,vars,leftno,_) ->
283 let ind = let _,x,_,_ = List.hd itl in x in
286 (fun (name, _, ty, cl) (itl,acc) ->
287 let ty = cook vars ty in
288 let ty, fix_ty = convert_term uri ty in
291 (fun (name, ty) (cl,acc) ->
292 let ty = cook vars ty in
293 let ty, fix_ty = convert_term uri ty in
294 ([], name, ty)::cl, acc @ fix_ty)
297 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
300 NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
303 | Cic.CurrentProof _ -> assert false
306 let convert_obj uri obj =
307 let o, fixpoints = convert_obj_aux uri obj in
308 let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in