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
30 List.filter (function Ce (_, NCic.Decl _) | Fix _ -> true | _ -> false)
33 acc, List.length l, List.length only_decl, acc1
38 let splat_args_for_rel ctx t =
39 let bound, free, _, primo_ce_dopo_fix = context_tassonomy ctx in
42 let rec aux = function
45 match List.nth ctx (n+bound) with
46 | Fix (refe, _, _) when (n+bound) < primo_ce_dopo_fix ->
47 NCic.Const refe :: aux (n-1)
48 | Fix _ | Ce (_, NCic.Decl _) -> NCic.Rel (n+bound)::aux (n-1)
49 | Ce (_, NCic.Def _) -> aux (n-1)
51 NCic.Appl (t:: aux free)
54 let splat_args ctx t n_fix =
55 let bound, free, _, primo_ce_dopo_fix = context_tassonomy ctx in
58 let rec aux = function
61 (match List.nth ctx (n-1) with
62 | Ce (_, NCic.Decl _) when n <= bound -> NCic.Rel n:: aux (n-1)
63 | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
64 splat_args_for_rel ctx (NCic.Const refe):: aux (n-1)
65 | Fix _ | Ce (_, NCic.Decl _) -> NCic.Rel (n - n_fix):: aux (n-1)
66 | Ce (_, NCic.Def _) -> aux (n - 1)
69 NCic.Appl (t:: aux (List.length ctx))
72 exception Nothing_to_do;;
74 let fix_outty curi tyno t context outty =
76 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
77 Cic.InductiveDefinition (tyl,_,leftno,_) ->
78 let _,_,arity,_ = List.nth tyl tyno in
79 let rec count_prods leftno context arity =
80 match leftno, CicReduction.whd context arity with
82 | 0, Cic.Prod (name,so,ty) ->
83 1 + count_prods 0 (Some (name, Cic.Decl so)::context) ty
84 | n, Cic.Prod (name,so,ty) ->
85 count_prods (leftno - 1) (Some (name, Cic.Decl so)::context) ty
88 (*prerr_endline (UriManager.string_of_uri curi);
89 prerr_endline ("LEFTNO: " ^ string_of_int leftno ^ " " ^ CicPp.ppterm arity);*)
90 leftno, count_prods leftno [] arity
91 | _ -> assert false in
93 let tty,_= CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
94 match CicReduction.whd context tty with
95 Cic.MutInd (_,_,ens) -> ens,[]
96 | Cic.Appl (Cic.MutInd (_,_,ens)::args) ->
97 ens,fst (HExtlib.split_nth leftno args)
100 let rec aux n irl context outsort =
101 match n, CicReduction.whd context outsort with
102 0, Cic.Prod _ -> raise Nothing_to_do
104 let irl = List.rev irl in
105 let ty = CicSubstitution.lift rightno (Cic.MutInd (curi,tyno,ens)) in
107 if args = [] && irl = [] then ty
109 Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
110 let he = CicSubstitution.lift (rightno + 1) outty in
113 else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
115 Cic.Lambda (Cic.Anonymous, ty, t)
116 | n, Cic.Prod (name,so,ty) ->
118 aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
120 Cic.Lambda (name,so,ty')
121 | _,_ -> assert false
123 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
125 fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
127 try aux rightno [] context outsort
128 with Nothing_to_do -> outty
129 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
133 let module C = Cic in
134 let rec aux context =
137 | C.Var (uri,exp_named_subst) ->
138 let exp_named_subst' =
139 List.map (function i,t -> i, (aux context t)) exp_named_subst in
140 C.Var (uri,exp_named_subst')
142 | C.Meta _ -> assert false
144 | C.Cast (v,t) -> C.Cast (aux context v, aux context t)
146 C.Prod (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
147 | C.Lambda (n,s,t) ->
148 C.Lambda (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
149 | C.LetIn (n,s,ty,t) ->
151 (n, aux context s, aux context ty,
152 aux ((Some (n, C.Def(s,ty)))::context) t)
153 | C.Appl l -> C.Appl (List.map (aux context) l)
154 | C.Const (uri,exp_named_subst) ->
155 let exp_named_subst' =
156 List.map (function i,t -> i, (aux context t)) exp_named_subst
158 C.Const (uri,exp_named_subst')
159 | C.MutInd (uri,tyno,exp_named_subst) ->
160 let exp_named_subst' =
161 List.map (function i,t -> i, (aux context t)) exp_named_subst
163 C.MutInd (uri, tyno, exp_named_subst')
164 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
165 let exp_named_subst' =
166 List.map (function i,t -> i, (aux context t)) exp_named_subst
168 C.MutConstruct (uri, tyno, consno, exp_named_subst')
169 | C.MutCase (uri, tyno, outty, term, patterns) ->
170 let outty = fix_outty uri tyno term context outty in
171 C.MutCase (uri, tyno, aux context outty,
172 aux context term, List.map (aux context) patterns)
173 | C.Fix (funno, funs) ->
176 (fun (types,len) (n,_,ty,_) ->
177 ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
183 (fun (name, indidx, ty, bo) ->
184 (name, indidx, aux context ty, aux (tys@context) bo)
187 | C.CoFix (funno, funs) ->
190 (fun (types,len) (n,ty,_) ->
191 ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
197 (fun (name, ty, bo) ->
198 (name, aux context ty, aux (tys@context) bo)
205 let get_fresh,reset_seed =
209 string_of_int !seed),
210 (function () -> seed := 0)
213 (* we are lambda-lifting also variables that do not occur *)
214 (* ctx does not distinguish successive blocks of cofix, since there may be no
215 * lambda separating them *)
216 let convert_term uri t =
217 let rec aux octx (ctx : ctx list) n_fix uri = function
218 | Cic.CoFix (k, fl) ->
220 UriManager.uri_of_string
221 (UriManager.buri_of_uri uri^"/"^
222 UriManager.name_of_uri uri ^ "___" ^ get_fresh () ^ ".con")
224 let bctx, fixpoints_tys, tys, _ =
226 (fun (name,ty,_) (bctx, fixpoints, tys, idx) ->
227 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
228 let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
229 Fix (r,name,ty) :: bctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
232 let bctx = bctx @ ctx in
233 let n_fl = List.length fl in
236 (fun (types,len) (n,ty,_) ->
237 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
242 (fun (name,_,bo) ty (l,fixpoints) ->
243 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
244 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
245 fixpoints_bo @ fixpoints)
246 fl tys ([],fixpoints_tys)
249 NUri.nuri_of_ouri buri,0,[],[],
250 NCic.Fixpoint (false, fl, (`Generated, `Definition))
253 (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
258 UriManager.uri_of_string
259 (UriManager.buri_of_uri uri^"/"^
260 UriManager.name_of_uri uri ^ "___" ^ get_fresh () ^ ".con")
262 let bad_bctx, fixpoints_tys, tys, _ =
264 (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) ->
265 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
266 let r = (* recno is dummy here, must be lifted by the ctx len *)
267 Ref.reference_of_ouri buri (Ref.Fix (idx,recno))
269 Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
272 let _, _, free_decls, _ = context_tassonomy (bad_bctx @ ctx) in
275 | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
276 Fix (Ref.reference_of_ouri buri
277 (Ref.Fix (idx,recno+free_decls)),name,ty)
278 | _ -> assert false) bad_bctx @ ctx
280 let n_fl = List.length fl in
283 (fun (types,len) (n,_,ty,_) ->
284 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
288 let fl, fixpoints,_ =
290 (fun (name,rno,_,bo) ty (l,fixpoints,idx) ->
291 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
292 let rno = rno + free_decls in
293 if idx = k then rno_k := rno;
294 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
295 fixpoints_bo @ fixpoints,idx+1)
296 fl tys ([],fixpoints_tys,0)
299 NUri.nuri_of_ouri buri,max_int,[],[],
300 NCic.Fixpoint (true, fl, (`Generated, `Definition))
304 (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
308 let bound, _, _, primo_ce_dopo_fix = context_tassonomy ctx in
309 (match List.nth ctx (n-1) with
310 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
311 splat_args_for_rel ctx (NCic.Const r), []
312 | Ce _ when n <= bound -> NCic.Rel n, []
313 | Fix _ (* BUG 3 fix nested *)
314 | Ce _ -> NCic.Rel (n-n_fix), [])
315 | Cic.Lambda (name, (s as old_s), t) ->
316 let s, fixpoints_s = aux octx ctx n_fix uri s in
317 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
318 let octx = Some (name, Cic.Decl old_s) :: octx in
319 let t, fixpoints_t = aux octx ctx n_fix uri t in
320 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
321 | Cic.Prod (name, (s as old_s), t) ->
322 let s, fixpoints_s = aux octx ctx n_fix uri s in
323 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
324 let octx = Some (name, Cic.Decl old_s) :: octx in
325 let t, fixpoints_t = aux octx ctx n_fix uri t in
326 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
327 | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
328 let te, fixpoints_s = aux octx ctx n_fix uri te in
329 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
330 let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
331 let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
332 let t, fixpoints_t = aux octx ctx n_fix uri t in
333 NCic.LetIn (cn_to_s name, ty, te, t),
334 fixpoints_s @ fixpoints_t @ fixpoints_ty
336 let t, fixpoints_t = aux octx ctx n_fix uri t in
337 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
338 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
339 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
340 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
341 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
342 | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[]
343 (* calculate depth in the univ_graph*)
348 let t, fixpoints = aux octx ctx n_fix uri t in
349 (t::l,fixpoints@acc))
353 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
354 | _ -> NCic.Appl l, fixpoints)
355 | Cic.Const (curi, ens) ->
356 aux_ens curi octx ctx n_fix uri ens
357 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
358 | Cic.Constant (_,Some _,_,_,_) ->
359 NCic.Const (Ref.reference_of_ouri curi Ref.Def)
360 | Cic.Constant (_,None,_,_,_) ->
361 NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
363 | Cic.MutInd (curi, tyno, ens) ->
364 aux_ens curi octx ctx n_fix uri ens
365 (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
366 | Cic.MutConstruct (curi, tyno, consno, ens) ->
367 aux_ens curi octx ctx n_fix uri ens
368 (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
369 | Cic.Var (curi, ens) ->
370 (match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
371 Cic.Variable (_,Some bo,_,_,_) ->
372 aux octx ctx n_fix uri (CicSubstitution.subst_vars ens bo)
374 | Cic.MutCase (curi, tyno, outty, t, branches) ->
375 let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
376 let outty, fixpoints_outty = aux octx ctx n_fix uri outty in
377 let t, fixpoints_t = aux octx ctx n_fix uri t in
378 let branches, fixpoints =
381 let t, fixpoints = aux octx ctx n_fix uri t in
382 (t::l,fixpoints@acc))
385 NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
386 | Cic.Implicit _ | Cic.Meta _ -> assert false
387 and aux_ens curi octx ctx n_fix uri ens he =
392 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
393 Cic.Constant (_,_,_,params,_)
394 | Cic.InductiveDefinition (_,params,_,_) -> params
396 | Cic.CurrentProof _ -> assert false
400 (fun luri (l,objs) ->
401 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph luri) with
402 Cic.Variable (_,Some _,_,_,_) -> l, objs
403 | Cic.Variable (_,None,_,_,_) ->
404 let t = List.assoc luri ens in
405 let t,o = aux octx ctx n_fix uri t in
410 NCic.Appl (he::ens),objs
415 let cook mode vars t =
416 let t = fix_outtype t in
417 let varsno = List.length vars in
418 let t = CicSubstitution.lift varsno t in
419 let rec aux n acc l =
421 snd(List.fold_left (fun (i,res) uri -> i+1,(uri,Cic.Rel i)::res) (1,[]) acc)
424 [] -> CicSubstitution.subst_vars subst t
427 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
428 Cic.Variable (_,bo,ty,_,_) ->
429 HExtlib.map_option fix_outtype bo, fix_outtype ty
430 | _ -> assert false in
431 let ty = CicSubstitution.subst_vars subst ty in
432 let bo = HExtlib.map_option (CicSubstitution.subst_vars subst) bo in
433 let id = Cic.Name (UriManager.name_of_uri uri) in
434 let t = aux (n-1) (uri::acc) uris in
435 match bo,ty,mode with
436 None,ty,`Lambda -> Cic.Lambda (id,ty,t)
437 | None,ty,`Pi -> Cic.Prod (id,ty,t)
438 | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
443 let convert_obj_aux uri = function
444 | Cic.Constant (name, None, ty, vars, _) ->
445 let ty = cook `Pi vars ty in
446 let nty, fixpoints = convert_term uri ty in
447 assert(fixpoints = []);
448 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
450 | Cic.Constant (name, Some bo, ty, vars, _) ->
451 let bo = cook `Lambda vars bo in
452 let ty = cook `Pi vars ty in
453 let nbo, fixpoints_bo = convert_term uri bo in
454 let nty, fixpoints_ty = convert_term uri ty in
455 assert(fixpoints_ty = []);
456 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
457 fixpoints_bo @ fixpoints_ty
458 | Cic.InductiveDefinition (itl,vars,leftno,_) ->
459 let ind = let _,x,_,_ = List.hd itl in x in
462 (fun (name, _, ty, cl) (itl,acc) ->
463 let ty = cook `Pi vars ty in
464 let ty, fix_ty = convert_term uri ty in
467 (fun (name, ty) (cl,acc) ->
468 let ty = cook `Pi vars ty in
469 let ty, fix_ty = convert_term uri ty in
470 ([], name, ty)::cl, acc @ fix_ty)
473 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
476 NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
479 | Cic.CurrentProof _ -> assert false
482 let convert_obj uri obj =
484 let o, fixpoints = convert_obj_aux uri obj in
485 let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in