1 let convert_term = Obj.magic;;
9 | Ce of NCic.hypothesis
10 | Fix of NReference.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 = NReference.reference_of_ouri buri(NReference.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 (NReference.reference_of_ouri buri (NReference.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")
114 let bctx, fixpoints_tys, tys, _ =
116 (fun (name,recno,ty,_) (ctx, fixpoints, tys, idx) ->
117 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
118 if idx = k then rno := recno;
120 NReference.reference_of_ouri buri (NReference.Fix (idx,recno))
122 Fix (r,name,ty) :: ctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
125 let bctx = bctx @ ctx in
126 let n_fl = List.length fl in
129 (fun (types,len) (n,_,ty,_) ->
130 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
135 (fun (name,rno,_,bo) ty (l,fixpoints) ->
136 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
137 let _, free, _ = context_tassonomy bctx in
138 let rno = rno + free in
139 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
140 fixpoints_bo @ fixpoints)
141 fl tys ([],fixpoints_tys)
144 NUri.nuri_of_ouri buri,0,[],[],
145 NCic.Fixpoint (true, fl, (`Generated, `Definition))
149 (NReference.reference_of_ouri buri (NReference.Fix (k,!rno))))
153 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
154 (match List.nth ctx (n-1) with
155 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
156 splat_args_for_rel ctx (NCic.Const r), []
157 | Ce _ when n <= bound -> NCic.Rel n, []
158 | Fix _ (* BUG 3 fix nested *)
159 | Ce _ -> NCic.Rel (n-n_fix), [])
160 | Cic.Lambda (name, (s as old_s), t) ->
161 let s, fixpoints_s = aux octx ctx n_fix uri s in
162 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
163 let octx = Some (name, Cic.Decl old_s) :: octx in
164 let t, fixpoints_t = aux octx ctx n_fix uri t in
165 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
166 | Cic.Prod (name, (s as old_s), t) ->
167 let s, fixpoints_s = aux octx ctx n_fix uri s in
168 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
169 let octx = Some (name, Cic.Decl old_s) :: octx in
170 let t, fixpoints_t = aux octx ctx n_fix uri t in
171 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
172 | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
173 let te, fixpoints_s = aux octx ctx n_fix uri te in
174 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
175 let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
176 let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
177 let t, fixpoints_t = aux octx ctx n_fix uri t in
178 NCic.LetIn (cn_to_s name, ty, te, t),
179 fixpoints_s @ fixpoints_t @ fixpoints_ty
181 let t, fixpoints_t = aux octx ctx n_fix uri t in
182 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
183 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
184 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
185 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
186 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
187 | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[]
188 (* calculate depth in the univ_graph*)
193 let t, fixpoints = aux octx ctx n_fix uri t in
194 (t::l,fixpoints@acc))
198 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
199 | _ -> NCic.Appl l, fixpoints)
200 | Cic.Const (curi, _) ->
201 NCic.Const (NReference.reference_of_ouri curi NReference.Def),[]
202 | Cic.MutInd (curi, tyno, _) ->
203 NCic.Const (NReference.reference_of_ouri curi (NReference.Ind tyno)),[]
204 | Cic.MutConstruct (curi, tyno, consno, _) ->
205 NCic.Const (NReference.reference_of_ouri curi
206 (NReference.Con (tyno,consno))),[]
207 | Cic.MutCase (curi, tyno, oty, t, branches) ->
208 let r = NReference.reference_of_ouri curi (NReference.Ind tyno) in
209 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
210 let t, fixpoints_t = aux octx ctx n_fix uri t in
211 let branches, fixpoints =
214 let t, fixpoints = aux octx ctx n_fix uri t in
215 (t::l,fixpoints@acc))
218 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
219 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
224 let convert_obj_aux uri = function
225 | Cic.Constant (name, None, ty, _, _) ->
226 let nty, fixpoints = convert_term uri ty in
227 assert(fixpoints = []);
228 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
230 | Cic.Constant (name, Some bo, ty, _, _) ->
231 let nbo, fixpoints_bo = convert_term uri bo in
232 let nty, fixpoints_ty = convert_term uri ty in
233 assert(fixpoints_ty = []);
234 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
235 fixpoints_bo @ fixpoints_ty
236 | Cic.InductiveDefinition (itl,_,leftno,_) ->
237 let ind = let _,x,_,_ = List.hd itl in x in
240 (fun (name, _, ty, cl) (itl,acc) ->
241 let ty, fix_ty = convert_term uri ty in
244 (fun (name, ty) (cl,acc) ->
245 let ty, fix_ty = convert_term uri ty in
246 ([], name, ty)::cl, acc @ fix_ty)
249 ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
252 NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)),
255 | Cic.CurrentProof _ -> assert false
258 let convert_obj uri obj =
259 let o, fixpoints = convert_obj_aux uri obj in
260 let obj = NUri.nuri_of_ouri uri,0, [], [], o in