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 exception Nothing_to_do;;
66 let fix_outty curi tyno t context outty =
68 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
69 Cic.InductiveDefinition (tyl,_,leftno,_) ->
70 let _,_,arity,_ = List.nth tyl tyno in
71 let rec count_prods leftno context arity =
72 match leftno, CicReduction.whd context arity with
74 | 0, Cic.Prod (name,so,ty) ->
75 1 + count_prods 0 (Some (name, Cic.Decl so)::context) ty
76 | n, Cic.Prod (name,so,ty) ->
77 count_prods (leftno - 1) (Some (name, Cic.Decl so)::context) ty
80 (*prerr_endline (UriManager.string_of_uri curi);
81 prerr_endline ("LEFTNO: " ^ string_of_int leftno ^ " " ^ CicPp.ppterm arity);*)
82 leftno, count_prods leftno [] arity
83 | _ -> assert false in
85 let tty,_= CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
86 match CicReduction.whd context tty with
87 Cic.MutInd (_,_,ens) -> ens,[]
88 | Cic.Appl (Cic.MutInd (_,_,ens)::args) ->
89 ens,fst (HExtlib.split_nth leftno args)
92 let rec aux n irl context outsort =
93 match n, CicReduction.whd context outsort with
94 0, Cic.Prod _ -> raise Nothing_to_do
96 let irl = List.rev irl in
97 let ty = CicSubstitution.lift rightno (Cic.MutInd (curi,tyno,ens)) in
99 if args = [] && irl = [] then ty
101 Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
102 let he = CicSubstitution.lift (rightno + 1) outty in
105 else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
107 Cic.Lambda (Cic.Anonymous, ty, t)
108 | n, Cic.Prod (name,so,ty) ->
110 aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
112 Cic.Lambda (name,so,ty')
113 | _,_ -> assert false
115 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
117 fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
119 try aux rightno [] context outsort
120 with Nothing_to_do -> outty
121 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
125 let module C = Cic in
126 let rec aux context =
129 | C.Var (uri,exp_named_subst) ->
130 let exp_named_subst' =
131 List.map (function i,t -> i, (aux context t)) exp_named_subst in
132 C.Var (uri,exp_named_subst')
134 | C.Meta _ -> assert false
136 | C.Cast (v,t) -> C.Cast (aux context v, aux context t)
138 C.Prod (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
139 | C.Lambda (n,s,t) ->
140 C.Lambda (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
141 | C.LetIn (n,s,ty,t) ->
143 (n, aux context s, aux context ty,
144 aux ((Some (n, C.Def(s,ty)))::context) t)
145 | C.Appl l -> C.Appl (List.map (aux context) l)
146 | C.Const (uri,exp_named_subst) ->
147 let exp_named_subst' =
148 List.map (function i,t -> i, (aux context t)) exp_named_subst
150 C.Const (uri,exp_named_subst')
151 | C.MutInd (uri,tyno,exp_named_subst) ->
152 let exp_named_subst' =
153 List.map (function i,t -> i, (aux context t)) exp_named_subst
155 C.MutInd (uri, tyno, exp_named_subst')
156 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
157 let exp_named_subst' =
158 List.map (function i,t -> i, (aux context t)) exp_named_subst
160 C.MutConstruct (uri, tyno, consno, exp_named_subst')
161 | C.MutCase (uri, tyno, outty, term, patterns) ->
162 let outty = fix_outty uri tyno term context outty in
163 C.MutCase (uri, tyno, aux context outty,
164 aux context term, List.map (aux context) patterns)
165 | C.Fix (funno, funs) ->
168 (fun (types,len) (n,_,ty,_) ->
169 ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
175 (fun (name, indidx, ty, bo) ->
176 (name, indidx, aux context ty, aux (tys@context) bo)
179 | C.CoFix (funno, funs) ->
182 (fun (types,len) (n,ty,_) ->
183 ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
189 (fun (name, ty, bo) ->
190 (name, aux context ty, aux (tys@context) bo)
197 (* we are lambda-lifting also variables that do not occur *)
198 (* ctx does not distinguish successive blocks of cofix, since there may be no
199 * lambda separating them *)
200 let convert_term uri t =
201 let rec aux octx (ctx : ctx list) n_fix uri = function
202 | Cic.CoFix (k, fl) ->
204 UriManager.uri_of_string
205 (UriManager.buri_of_uri uri^"/"^
206 UriManager.name_of_uri uri ^ "___" ^
207 string_of_int (List.length ctx)^".con")
209 let bctx, fixpoints_tys, tys, _ =
211 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
212 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
213 let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
214 Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
217 let bctx = bctx @ ctx in
218 let n_fl = List.length fl in
221 (fun (types,len) (n,ty,_) ->
222 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
227 (fun (name,_,bo) ty (l,fixpoints) ->
228 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
229 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
230 fixpoints_bo @ fixpoints)
231 fl tys ([],fixpoints_tys)
234 NUri.nuri_of_ouri buri,0,[],[],
235 NCic.Fixpoint (false, fl, (`Generated, `Definition))
238 (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
243 UriManager.uri_of_string
244 (UriManager.buri_of_uri uri^"/"^
245 UriManager.name_of_uri uri ^ "___" ^
246 string_of_int (List.length ctx)^".con")
248 let bad_bctx, fixpoints_tys, tys, _ =
250 (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) ->
251 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
252 let r = (* recno is dummy here, must be lifted by the ctx len *)
253 Ref.reference_of_ouri buri (Ref.Fix (idx,recno))
255 Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
258 let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
261 | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
262 Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
263 | _ -> assert false) bad_bctx @ ctx
265 let n_fl = List.length fl in
268 (fun (types,len) (n,_,ty,_) ->
269 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
273 let fl, fixpoints,_ =
275 (fun (name,rno,_,bo) ty (l,fixpoints,idx) ->
276 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
277 let rno = rno + free in
278 if idx = k then rno_k := rno;
279 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
280 fixpoints_bo @ fixpoints,idx+1)
281 fl tys ([],fixpoints_tys,0)
284 NUri.nuri_of_ouri buri,max_int,[],[],
285 NCic.Fixpoint (true, fl, (`Generated, `Definition))
289 (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
293 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
294 (match List.nth ctx (n-1) with
295 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
296 splat_args_for_rel ctx (NCic.Const r), []
297 | Ce _ when n <= bound -> NCic.Rel n, []
298 | Fix _ (* BUG 3 fix nested *)
299 | Ce _ -> NCic.Rel (n-n_fix), [])
300 | Cic.Lambda (name, (s as old_s), t) ->
301 let s, fixpoints_s = aux octx ctx n_fix uri s in
302 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
303 let octx = Some (name, Cic.Decl old_s) :: octx in
304 let t, fixpoints_t = aux octx ctx n_fix uri t in
305 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
306 | Cic.Prod (name, (s as old_s), t) ->
307 let s, fixpoints_s = aux octx ctx n_fix uri s in
308 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
309 let octx = Some (name, Cic.Decl old_s) :: octx in
310 let t, fixpoints_t = aux octx ctx n_fix uri t in
311 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
312 | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
313 let te, fixpoints_s = aux octx ctx n_fix uri te in
314 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
315 let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
316 let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
317 let t, fixpoints_t = aux octx ctx n_fix uri t in
318 NCic.LetIn (cn_to_s name, ty, te, t),
319 fixpoints_s @ fixpoints_t @ fixpoints_ty
321 let t, fixpoints_t = aux octx ctx n_fix uri t in
322 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
323 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
324 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
325 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
326 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
327 | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[]
328 (* calculate depth in the univ_graph*)
333 let t, fixpoints = aux octx ctx n_fix uri t in
334 (t::l,fixpoints@acc))
338 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
339 | _ -> NCic.Appl l, fixpoints)
340 | Cic.Const (curi, ens) ->
341 aux_ens curi octx ctx n_fix uri ens
342 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
343 | Cic.Constant (_,Some _,_,_,_) ->
344 NCic.Const (Ref.reference_of_ouri curi Ref.Def)
345 | Cic.Constant (_,None,_,_,_) ->
346 NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
348 | Cic.MutInd (curi, tyno, ens) ->
349 aux_ens curi octx ctx n_fix uri ens
350 (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
351 | Cic.MutConstruct (curi, tyno, consno, ens) ->
352 aux_ens curi octx ctx n_fix uri ens
353 (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
354 | Cic.MutCase (curi, tyno, outty, t, branches) ->
355 let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
356 let outty, fixpoints_outty = aux octx ctx n_fix uri outty in
357 let t, fixpoints_t = aux octx ctx n_fix uri t in
358 let branches, fixpoints =
361 let t, fixpoints = aux octx ctx n_fix uri t in
362 (t::l,fixpoints@acc))
365 NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
366 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
367 and aux_ens curi octx ctx n_fix uri ens he =
372 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
373 Cic.Constant (_,_,_,params,_)
374 | Cic.InductiveDefinition (_,params,_,_) -> params
376 | Cic.CurrentProof _ -> assert false
381 let t = List.assoc uri ens in
382 let t,o = aux octx ctx n_fix uri t in
386 NCic.Appl (he::ens),objs
391 let cook mode vars t =
392 let t = fix_outtype t in
393 let varsno = List.length vars in
394 let t = CicSubstitution.lift varsno t in
395 let rec aux n acc l =
397 snd(List.fold_left (fun (i,res) uri -> i+1,(uri,Cic.Rel i)::res) (1,[]) acc)
400 [] -> CicSubstitution.subst_vars subst t
403 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
404 Cic.Variable (_,bo,ty,_,_) ->
405 HExtlib.map_option fix_outtype bo, fix_outtype ty
406 | _ -> assert false in
407 let ty = CicSubstitution.subst_vars subst ty in
408 let bo = HExtlib.map_option (CicSubstitution.subst_vars subst) bo in
409 let id = Cic.Name (UriManager.name_of_uri uri) in
410 let t = aux (n-1) (uri::acc) uris in
411 match bo,ty,mode with
412 None,ty,`Lambda -> Cic.Lambda (id,ty,t)
413 | None,ty,`Pi -> Cic.Prod (id,ty,t)
414 | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
419 let convert_obj_aux uri = function
420 | Cic.Constant (name, None, ty, vars, _) ->
421 let ty = cook `Pi vars ty in
422 let nty, fixpoints = convert_term uri ty in
423 assert(fixpoints = []);
424 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
426 | Cic.Constant (name, Some bo, ty, vars, _) ->
427 let bo = cook `Lambda vars bo in
428 let ty = cook `Pi vars ty in
429 let nbo, fixpoints_bo = convert_term uri bo in
430 let nty, fixpoints_ty = convert_term uri ty in
431 assert(fixpoints_ty = []);
432 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
433 fixpoints_bo @ fixpoints_ty
434 | Cic.InductiveDefinition (itl,vars,leftno,_) ->
435 let ind = let _,x,_,_ = List.hd itl in x in
438 (fun (name, _, ty, cl) (itl,acc) ->
439 let ty = cook `Pi vars ty in
440 let ty, fix_ty = convert_term uri ty in
443 (fun (name, ty) (cl,acc) ->
444 let ty = cook `Pi vars ty in
445 let ty, fix_ty = convert_term uri ty in
446 ([], name, ty)::cl, acc @ fix_ty)
449 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
452 NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
455 | Cic.CurrentProof _ -> assert false
458 let convert_obj uri obj =
459 let o, fixpoints = convert_obj_aux uri obj in
460 let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in