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 ty = Cic.MutInd (curi,tyno,ens) in
98 if args = [] && irl = [] then ty
100 Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
101 let he = CicSubstitution.lift (rightno + 1) outty in
104 else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
106 Cic.Lambda (Cic.Anonymous, ty, t)
107 | n, Cic.Prod (name,so,ty) ->
109 aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
111 Cic.Lambda (name,so,ty')
112 | _,_ -> assert false
114 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
116 fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
118 try aux rightno [] context outsort
119 with Nothing_to_do -> outty
120 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
123 (* we are lambda-lifting also variables that do not occur *)
124 (* ctx does not distinguish successive blocks of cofix, since there may be no
125 * lambda separating them *)
126 let convert_term uri t =
127 let rec aux octx (ctx : ctx list) n_fix uri = function
128 | Cic.CoFix (k, fl) ->
130 UriManager.uri_of_string
131 (UriManager.buri_of_uri uri^"/"^
132 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
134 let bctx, fixpoints_tys, tys, _ =
136 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
137 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
138 let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
139 Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
142 let bctx = bctx @ ctx in
143 let n_fl = List.length fl in
146 (fun (types,len) (n,ty,_) ->
147 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
152 (fun (name,_,bo) ty (l,fixpoints) ->
153 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
154 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
155 fixpoints_bo @ fixpoints)
156 fl tys ([],fixpoints_tys)
159 NUri.nuri_of_ouri buri,0,[],[],
160 NCic.Fixpoint (false, fl, (`Generated, `Definition))
163 (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
168 UriManager.uri_of_string
169 (UriManager.buri_of_uri uri^"/"^
170 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
172 let bad_bctx, fixpoints_tys, tys, _ =
174 (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) ->
175 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
176 let r = (* recno is dummy here, must be lifted by the ctx len *)
177 Ref.reference_of_ouri buri (Ref.Fix (idx,recno))
179 Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
182 let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
185 | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
186 Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
187 | _ -> assert false) bad_bctx @ ctx
189 let n_fl = List.length fl in
192 (fun (types,len) (n,_,ty,_) ->
193 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
197 let fl, fixpoints,_ =
199 (fun (name,rno,_,bo) ty (l,fixpoints,idx) ->
200 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
201 let rno = rno + free in
202 if idx = k then rno_k := rno;
203 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
204 fixpoints_bo @ fixpoints,idx+1)
205 fl tys ([],fixpoints_tys,0)
208 NUri.nuri_of_ouri buri,max_int,[],[],
209 NCic.Fixpoint (true, fl, (`Generated, `Definition))
213 (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
217 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
218 (match List.nth ctx (n-1) with
219 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
220 splat_args_for_rel ctx (NCic.Const r), []
221 | Ce _ when n <= bound -> NCic.Rel n, []
222 | Fix _ (* BUG 3 fix nested *)
223 | Ce _ -> NCic.Rel (n-n_fix), [])
224 | Cic.Lambda (name, (s as old_s), t) ->
225 let s, fixpoints_s = aux octx ctx n_fix uri s in
226 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
227 let octx = Some (name, Cic.Decl old_s) :: octx in
228 let t, fixpoints_t = aux octx ctx n_fix uri t in
229 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
230 | Cic.Prod (name, (s as old_s), t) ->
231 let s, fixpoints_s = aux octx ctx n_fix uri s in
232 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
233 let octx = Some (name, Cic.Decl old_s) :: octx in
234 let t, fixpoints_t = aux octx ctx n_fix uri t in
235 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
236 | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
237 let te, fixpoints_s = aux octx ctx n_fix uri te in
238 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
239 let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
240 let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
241 let t, fixpoints_t = aux octx ctx n_fix uri t in
242 NCic.LetIn (cn_to_s name, ty, te, t),
243 fixpoints_s @ fixpoints_t @ fixpoints_ty
245 let t, fixpoints_t = aux octx ctx n_fix uri t in
246 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
247 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
248 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
249 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
250 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
251 | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[]
252 (* calculate depth in the univ_graph*)
257 let t, fixpoints = aux octx ctx n_fix uri t in
258 (t::l,fixpoints@acc))
262 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
263 | _ -> NCic.Appl l, fixpoints)
264 | Cic.Const (curi, ens) ->
265 aux_ens octx ctx n_fix uri ens
266 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
267 | Cic.Constant (_,Some _,_,_,_) ->
268 NCic.Const (Ref.reference_of_ouri curi Ref.Def)
269 | Cic.Constant (_,None,_,_,_) ->
270 NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
272 | Cic.MutInd (curi, tyno, ens) ->
273 aux_ens octx ctx n_fix uri ens
274 (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
275 | Cic.MutConstruct (curi, tyno, consno, ens) ->
276 aux_ens octx ctx n_fix uri ens
277 (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
278 | Cic.MutCase (curi, tyno, outty, t, branches) ->
279 let outty = fix_outty curi tyno t octx outty in
280 let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
281 let outty, fixpoints_outty = aux octx ctx n_fix uri outty in
282 let t, fixpoints_t = aux octx ctx n_fix uri t in
283 let branches, fixpoints =
286 let t, fixpoints = aux octx ctx n_fix uri t in
287 (t::l,fixpoints@acc))
290 NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
291 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
292 and aux_ens octx ctx n_fix uri ens he =
298 (fun (_,t) (l,objs) ->
299 let t,o = aux octx ctx n_fix uri t in
303 NCic.Appl (he::ens),objs
308 let cook mode vars t =
309 let t = CicSubstitution.lift (List.length vars) t in
312 let t = CicSubstitution.subst_vars [uri,Cic.Rel 1] t in
314 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
315 Cic.Variable (_,bo,ty,_,_) -> bo,ty
316 | _ -> assert false in
317 let id = Cic.Name (UriManager.name_of_uri uri) in
319 match bo,ty,mode with
320 None,ty,`Lambda -> Cic.Lambda (id,ty,t)
321 | None,ty,`Pi -> Cic.Prod (id,ty,t)
322 | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
328 let convert_obj_aux uri = function
329 | Cic.Constant (name, None, ty, vars, _) ->
330 let ty = cook `Pi vars ty in
331 let nty, fixpoints = convert_term uri ty in
332 assert(fixpoints = []);
333 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
335 | Cic.Constant (name, Some bo, ty, vars, _) ->
336 let bo = cook `Lambda vars bo in
337 let ty = cook `Pi vars ty in
338 let nbo, fixpoints_bo = convert_term uri bo in
339 let nty, fixpoints_ty = convert_term uri ty in
340 assert(fixpoints_ty = []);
341 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
342 fixpoints_bo @ fixpoints_ty
343 | Cic.InductiveDefinition (itl,vars,leftno,_) ->
344 let ind = let _,x,_,_ = List.hd itl in x in
347 (fun (name, _, ty, cl) (itl,acc) ->
348 let ty = cook `Pi vars ty in
349 let ty, fix_ty = convert_term uri ty in
352 (fun (name, ty) (cl,acc) ->
353 let ty = cook `Pi vars ty in
354 let ty, fix_ty = convert_term uri ty in
355 ([], name, ty)::cl, acc @ fix_ty)
358 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
361 NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
364 | Cic.CurrentProof _ -> assert false
367 let convert_obj uri obj =
368 let o, fixpoints = convert_obj_aux uri obj in
369 let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in