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 let fix_outty curi tyno t context outty =
66 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
67 Cic.InductiveDefinition (tyl,_,leftno,_) ->
68 let _,_,arity,_ = List.nth tyl tyno in
69 let rec count_prods leftno context arity =
70 match leftno, CicReduction.whd context arity with
72 | 0, Cic.Prod (name,so,ty) ->
73 1 + count_prods 0 (Some (name, Cic.Decl so)::context) ty
74 | n, Cic.Prod (name,so,ty) ->
75 count_prods (leftno - 1) (Some (name, Cic.Decl so)::context) ty
78 (*prerr_endline (UriManager.string_of_uri curi);
79 prerr_endline ("LEFTNO: " ^ string_of_int leftno ^ " " ^ CicPp.ppterm arity);*)
80 leftno, count_prods leftno [] arity
81 | _ -> assert false in
83 match fst (CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
85 Cic.MutInd (_,_,ens) -> ens,[]
86 | Cic.Appl (Cic.MutInd (_,_,ens)::args) ->
87 ens,fst (HExtlib.split_nth leftno args)
90 let rec aux n irl context outty =
91 match n, CicReduction.whd context outty with
92 0, (Cic.Lambda _ as t) -> t
94 let ty = Cic.MutInd (curi,tyno,ens) in
95 let args = args @ irl in
96 let ty = if args = [] then ty else Cic.Appl (ty::args) in
97 Cic.Lambda (Cic.Anonymous, ty, CicSubstitution.lift 1 t)
98 | n, Cic.Lambda (name,so,ty) ->
100 aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
102 Cic.Lambda (name,so,ty')
103 | _,_ -> assert false
105 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
106 let outty' = aux rightno [] context outty in
107 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
108 if outty' = outty then outty else outty'
111 (* we are lambda-lifting also variables that do not occur *)
112 (* ctx does not distinguish successive blocks of cofix, since there may be no
113 * lambda separating them *)
114 let convert_term uri t =
115 let rec aux octx (ctx : ctx list) n_fix uri = function
116 | Cic.CoFix (k, fl) ->
118 UriManager.uri_of_string
119 (UriManager.buri_of_uri uri^"/"^
120 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
122 let bctx, fixpoints_tys, tys, _ =
124 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
125 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
126 let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
127 Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
130 let bctx = bctx @ ctx in
131 let n_fl = List.length fl in
134 (fun (types,len) (n,ty,_) ->
135 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
140 (fun (name,_,bo) ty (l,fixpoints) ->
141 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
142 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
143 fixpoints_bo @ fixpoints)
144 fl tys ([],fixpoints_tys)
147 NUri.nuri_of_ouri buri,0,[],[],
148 NCic.Fixpoint (false, fl, (`Generated, `Definition))
151 (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
156 UriManager.uri_of_string
157 (UriManager.buri_of_uri uri^"/"^
158 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
160 let bad_bctx, fixpoints_tys, tys, _ =
162 (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) ->
163 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
164 let r = (* recno is dummy here, must be lifted by the ctx len *)
165 Ref.reference_of_ouri buri (Ref.Fix (idx,recno))
167 Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
170 let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
173 | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
174 Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
175 | _ -> assert false) bad_bctx @ ctx
177 let n_fl = List.length fl in
180 (fun (types,len) (n,_,ty,_) ->
181 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
185 let fl, fixpoints,_ =
187 (fun (name,rno,_,bo) ty (l,fixpoints,idx) ->
188 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
189 let rno = rno + free in
190 if idx = k then rno_k := rno;
191 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
192 fixpoints_bo @ fixpoints,idx+1)
193 fl tys ([],fixpoints_tys,0)
196 NUri.nuri_of_ouri buri,max_int,[],[],
197 NCic.Fixpoint (true, fl, (`Generated, `Definition))
201 (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
205 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
206 (match List.nth ctx (n-1) with
207 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
208 splat_args_for_rel ctx (NCic.Const r), []
209 | Ce _ when n <= bound -> NCic.Rel n, []
210 | Fix _ (* BUG 3 fix nested *)
211 | Ce _ -> NCic.Rel (n-n_fix), [])
212 | Cic.Lambda (name, (s as old_s), t) ->
213 let s, fixpoints_s = aux octx ctx n_fix uri s in
214 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
215 let octx = Some (name, Cic.Decl old_s) :: octx in
216 let t, fixpoints_t = aux octx ctx n_fix uri t in
217 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
218 | Cic.Prod (name, (s as old_s), t) ->
219 let s, fixpoints_s = aux octx ctx n_fix uri s in
220 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
221 let octx = Some (name, Cic.Decl old_s) :: octx in
222 let t, fixpoints_t = aux octx ctx n_fix uri t in
223 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
224 | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
225 let te, fixpoints_s = aux octx ctx n_fix uri te in
226 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
227 let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
228 let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
229 let t, fixpoints_t = aux octx ctx n_fix uri t in
230 NCic.LetIn (cn_to_s name, ty, te, t),
231 fixpoints_s @ fixpoints_t @ fixpoints_ty
233 let t, fixpoints_t = aux octx ctx n_fix uri t in
234 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
235 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
236 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
237 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
238 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
239 | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[]
240 (* calculate depth in the univ_graph*)
245 let t, fixpoints = aux octx ctx n_fix uri t in
246 (t::l,fixpoints@acc))
250 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
251 | _ -> NCic.Appl l, fixpoints)
252 | Cic.Const (curi, ens) ->
253 aux_ens octx ctx n_fix uri ens
254 (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
255 | Cic.Constant (_,Some _,_,_,_) ->
256 NCic.Const (Ref.reference_of_ouri curi Ref.Def)
257 | Cic.Constant (_,None,_,_,_) ->
258 NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
260 | Cic.MutInd (curi, tyno, ens) ->
261 aux_ens octx ctx n_fix uri ens
262 (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
263 | Cic.MutConstruct (curi, tyno, consno, ens) ->
264 aux_ens octx ctx n_fix uri ens
265 (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
266 | Cic.MutCase (curi, tyno, outty, t, branches) ->
267 let outty = fix_outty curi tyno t octx outty in
268 let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
269 let outty, fixpoints_outty = aux octx ctx n_fix uri outty in
270 let t, fixpoints_t = aux octx ctx n_fix uri t in
271 let branches, fixpoints =
274 let t, fixpoints = aux octx ctx n_fix uri t in
275 (t::l,fixpoints@acc))
278 NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
279 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
280 and aux_ens octx ctx n_fix uri ens he =
286 (fun (_,t) (l,objs) ->
287 let t,o = aux octx ctx n_fix uri t in
291 NCic.Appl (he::ens),objs
296 let cook mode vars t =
297 let t = CicSubstitution.lift (List.length vars) t in
300 let t = CicSubstitution.subst_vars [uri,Cic.Rel 1] t in
302 match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
303 Cic.Variable (_,bo,ty,_,_) -> bo,ty
304 | _ -> assert false in
305 let id = Cic.Name (UriManager.name_of_uri uri) in
307 match bo,ty,mode with
308 None,ty,`Lambda -> Cic.Lambda (id,ty,t)
309 | None,ty,`Pi -> Cic.Prod (id,ty,t)
310 | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
316 let convert_obj_aux uri = function
317 | Cic.Constant (name, None, ty, vars, _) ->
318 let ty = cook `Pi vars ty in
319 let nty, fixpoints = convert_term uri ty in
320 assert(fixpoints = []);
321 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
323 | Cic.Constant (name, Some bo, ty, vars, _) ->
324 let bo = cook `Lambda vars bo in
325 let ty = cook `Pi vars ty in
326 let nbo, fixpoints_bo = convert_term uri bo in
327 let nty, fixpoints_ty = convert_term uri ty in
328 assert(fixpoints_ty = []);
329 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
330 fixpoints_bo @ fixpoints_ty
331 | Cic.InductiveDefinition (itl,vars,leftno,_) ->
332 let ind = let _,x,_,_ = List.hd itl in x in
335 (fun (name, _, ty, cl) (itl,acc) ->
336 let ty = cook `Pi vars ty in
337 let ty, fix_ty = convert_term uri ty in
340 (fun (name, ty) (cl,acc) ->
341 let ty = cook `Pi vars ty in
342 let ty, fix_ty = convert_term uri ty in
343 ([], name, ty)::cl, acc @ fix_ty)
346 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
349 NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
352 | Cic.CurrentProof _ -> assert false
355 let convert_obj uri obj =
356 let o, fixpoints = convert_obj_aux uri obj in
357 let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in