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
49 let rec aux = function
52 (match List.nth ctx (n-1) with
53 | Ce _ when n <= bound -> NCic.Rel n
54 | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
55 splat_args_for_rel ctx (NCic.Const refe)
56 | Fix _ | Ce _ -> NCic.Rel (n - n_fix)
59 NCic.Appl (t:: aux (List.length ctx))
62 (* we are lambda-lifting also variables that do not occur *)
63 (* ctx does not distinguish successive blocks of cofix, since there may be no
64 * lambda separating them *)
65 let convert_term uri t =
66 let rec aux octx (ctx : ctx list) n_fix uri = function
67 | Cic.CoFix (k, fl) ->
69 UriManager.uri_of_string
70 (UriManager.buri_of_uri uri^"/"^
71 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
73 let bctx, fixpoints_tys, tys, _ =
75 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
76 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
77 let r = NReference.reference_of_ouri buri(NReference.CoFix idx) in
78 ctx @ [Fix (r,name,ty)], fixpoints_ty @ fixpoints,ty::tys,idx+1)
81 let bctx = bctx @ ctx in
82 let n_fl = List.length fl in
85 (fun (types,len) (n,ty,_) ->
86 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
91 (fun (name,_,bo) ty (l,fixpoints) ->
92 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
93 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
94 fixpoints_bo @ fixpoints)
95 fl tys ([],fixpoints_tys)
98 NUri.nuri_of_ouri buri,0,[],[],
99 NCic.Fixpoint (false, fl, (`Generated, `Definition))
102 (NCic.Const (NReference.reference_of_ouri buri (NReference.CoFix k)))
107 UriManager.uri_of_string
108 (UriManager.buri_of_uri uri^"/"^
109 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
112 let bctx, fixpoints_tys, tys, _ =
114 (fun (name,recno,ty,_) (ctx, fixpoints, tys, idx) ->
115 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
116 if idx = k then rno := recno;
118 NReference.reference_of_ouri buri (NReference.Fix (idx,recno))
120 ctx @ [Fix (r,name,ty)], fixpoints_ty@fixpoints,ty::tys,idx+1)
123 let bctx = bctx @ ctx in
124 let n_fl = List.length fl in
127 (fun (types,len) (n,_,ty,_) ->
128 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
133 (fun (name,rno,_,bo) ty (l,fixpoints) ->
134 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
135 let _, free, _ = context_tassonomy bctx in
136 let rno = rno + free in
137 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
138 fixpoints_bo @ fixpoints)
139 fl tys ([],fixpoints_tys)
142 NUri.nuri_of_ouri buri,0,[],[],
143 NCic.Fixpoint (true, fl, (`Generated, `Definition))
147 (NReference.reference_of_ouri buri (NReference.Fix (k,!rno))))
151 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
152 (match List.nth ctx (n-1) with
153 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
154 splat_args_for_rel ctx (NCic.Const r), []
155 | Ce _ when n <= bound -> NCic.Rel n, []
156 | Fix _ (* BUG 3 fix nested *)
157 | Ce _ -> NCic.Rel (n-n_fix), [])
158 | Cic.Lambda (name, (s as old_s), t) ->
159 let s, fixpoints_s = aux octx ctx n_fix uri s in
160 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
161 let octx = Some (name, Cic.Decl old_s) :: octx in
162 let t, fixpoints_t = aux octx ctx n_fix uri t in
163 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
164 | Cic.Prod (name, (s as old_s), t) ->
165 let s, fixpoints_s = aux octx ctx n_fix uri s in
166 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
167 let octx = Some (name, Cic.Decl old_s) :: octx in
168 let t, fixpoints_t = aux octx ctx n_fix uri t in
169 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
170 | Cic.LetIn (name, (s as old_s), t) ->
171 let s, fixpoints_s = aux octx ctx n_fix uri s in
173 CicTypeChecker.type_of_aux' [] octx old_s CicUniv.oblivion_ugraph
175 let ty, fixpoints_ty = aux octx ctx n_fix uri old_ty in
176 let ctx = Ce (cn_to_s name, NCic.Def (s, ty)) :: ctx in
177 let octx = Some (name, Cic.Def (old_s, Some old_ty)) :: octx in
178 let t, fixpoints_t = aux octx ctx n_fix uri t in
179 NCic.LetIn (cn_to_s name, ty, s, t),
180 fixpoints_s @ fixpoints_t @ fixpoints_ty
182 let t, fixpoints_t = aux octx ctx n_fix uri t in
183 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
184 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
185 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
186 | Cic.Sort Cic.Set -> NCic.Sort NCic.Set,[]
187 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
188 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
189 (* calculate depth in the univ_graph*)
194 let t, fixpoints = aux octx ctx n_fix uri t in
195 (t::l,fixpoints@acc))
199 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
200 | _ -> NCic.Appl l, fixpoints)
201 | Cic.Const (curi, _) ->
202 NCic.Const (NReference.reference_of_ouri curi NReference.Def),[]
203 | Cic.MutInd (curi, tyno, _) ->
204 NCic.Const (NReference.reference_of_ouri curi (NReference.Ind tyno)),[]
205 | Cic.MutConstruct (curi, tyno, consno, _) ->
206 NCic.Const (NReference.reference_of_ouri curi
207 (NReference.Con (tyno,consno))),[]
208 | Cic.MutCase (curi, tyno, oty, t, branches) ->
209 let r = NReference.reference_of_ouri curi (NReference.Ind tyno) in
210 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
211 let t, fixpoints_t = aux octx ctx n_fix uri t in
212 let branches, fixpoints =
215 let t, fixpoints = aux octx ctx n_fix uri t in
216 (t::l,fixpoints@acc))
219 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
220 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
225 let convert_obj_aux uri = function
226 | Cic.Constant (name, None, ty, _, _) ->
227 let nty, fixpoints = convert_term uri ty in
228 assert(fixpoints = []);
229 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
231 | Cic.Constant (name, Some bo, ty, _, _) ->
232 let nbo, fixpoints_bo = convert_term uri bo in
233 let nty, fixpoints_ty = convert_term uri ty in
234 assert(fixpoints_ty = []);
235 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
236 fixpoints_bo @ fixpoints_ty
237 | Cic.InductiveDefinition (_,_,_,_) -> assert false (*
238 let ind = let _,x,_,_ = List.hd itl in x in
241 (fun name, _, ty, cl ->
242 [], name, convert_term ty,
243 List.map (fun name, ty -> [], name, convert_term ty) cl)
246 NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)) *)
248 | Cic.CurrentProof _ -> assert false
251 let convert_obj uri obj =
252 let o, fixpoints = convert_obj_aux uri obj in
253 let obj = NUri.nuri_of_ouri uri,0, [], [], o in