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,0,[],[],
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, _) ->
206 NCic.Const (Ref.reference_of_ouri curi Ref.Def),[]
207 | Cic.MutInd (curi, tyno, _) ->
208 NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)),[]
209 | Cic.MutConstruct (curi, tyno, consno, _) ->
210 NCic.Const (Ref.reference_of_ouri curi
211 (Ref.Con (tyno,consno))),[]
212 | Cic.MutCase (curi, tyno, oty, t, branches) ->
213 let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
214 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
215 let t, fixpoints_t = aux octx ctx n_fix uri t in
216 let branches, fixpoints =
219 let t, fixpoints = aux octx ctx n_fix uri t in
220 (t::l,fixpoints@acc))
223 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
224 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
229 let convert_obj_aux uri = function
230 | Cic.Constant (name, None, ty, _, _) ->
231 let nty, fixpoints = convert_term uri ty in
232 assert(fixpoints = []);
233 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
235 | Cic.Constant (name, Some bo, ty, _, _) ->
236 let nbo, fixpoints_bo = convert_term uri bo in
237 let nty, fixpoints_ty = convert_term uri ty in
238 assert(fixpoints_ty = []);
239 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
240 fixpoints_bo @ fixpoints_ty
241 | Cic.InductiveDefinition (itl,_,leftno,_) ->
242 let ind = let _,x,_,_ = List.hd itl in x in
245 (fun (name, _, ty, cl) (itl,acc) ->
246 let ty, fix_ty = convert_term uri ty in
249 (fun (name, ty) (cl,acc) ->
250 let ty, fix_ty = convert_term uri ty in
251 ([], name, ty)::cl, acc @ fix_ty)
254 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
257 NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)),
260 | Cic.CurrentProof _ -> assert false
263 let convert_obj uri obj =
264 let o, fixpoints = convert_obj_aux uri obj in
265 let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in