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 ctx @ [Fix (r,name,ty)], 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 ctx @ [Fix (r,name,ty)], 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, (s as old_s), t) ->
173 let s, fixpoints_s = aux octx ctx n_fix uri s in
175 CicTypeChecker.type_of_aux' [] octx old_s CicUniv.oblivion_ugraph
177 let ty, fixpoints_ty = aux octx ctx n_fix uri old_ty in
178 let ctx = Ce (cn_to_s name, NCic.Def (s, ty)) :: ctx in
179 let octx = Some (name, Cic.Def (old_s, Some old_ty)) :: octx in
180 let t, fixpoints_t = aux octx ctx n_fix uri t in
181 NCic.LetIn (cn_to_s name, ty, s, t),
182 fixpoints_s @ fixpoints_t @ fixpoints_ty
184 let t, fixpoints_t = aux octx ctx n_fix uri t in
185 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
186 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
187 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
188 | Cic.Sort Cic.Set -> NCic.Sort NCic.Set,[]
189 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
190 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
191 (* calculate depth in the univ_graph*)
196 let t, fixpoints = aux octx ctx n_fix uri t in
197 (t::l,fixpoints@acc))
201 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
202 | _ -> NCic.Appl l, fixpoints)
203 | Cic.Const (curi, _) ->
204 NCic.Const (NReference.reference_of_ouri curi NReference.Def),[]
205 | Cic.MutInd (curi, tyno, _) ->
206 NCic.Const (NReference.reference_of_ouri curi (NReference.Ind tyno)),[]
207 | Cic.MutConstruct (curi, tyno, consno, _) ->
208 NCic.Const (NReference.reference_of_ouri curi
209 (NReference.Con (tyno,consno))),[]
210 | Cic.MutCase (curi, tyno, oty, t, branches) ->
211 let r = NReference.reference_of_ouri curi (NReference.Ind tyno) in
212 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
213 let t, fixpoints_t = aux octx ctx n_fix uri t in
214 let branches, fixpoints =
217 let t, fixpoints = aux octx ctx n_fix uri t in
218 (t::l,fixpoints@acc))
221 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
222 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
227 let convert_obj_aux uri = function
228 | Cic.Constant (name, None, ty, _, _) ->
229 let nty, fixpoints = convert_term uri ty in
230 assert(fixpoints = []);
231 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
233 | Cic.Constant (name, Some bo, ty, _, _) ->
234 let nbo, fixpoints_bo = convert_term uri bo in
235 let nty, fixpoints_ty = convert_term uri ty in
236 assert(fixpoints_ty = []);
237 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
238 fixpoints_bo @ fixpoints_ty
239 | Cic.InductiveDefinition (_,_,_,_) -> assert false (*
240 let ind = let _,x,_,_ = List.hd itl in x in
243 (fun name, _, ty, cl ->
244 [], name, convert_term ty,
245 List.map (fun name, ty -> [], name, convert_term ty) cl)
248 NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)) *)
250 | Cic.CurrentProof _ -> assert false
253 let convert_obj uri obj =
254 let o, fixpoints = convert_obj_aux uri obj in
255 let obj = NUri.nuri_of_ouri uri,0, [], [], o in