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 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 (* we are lambda-lifting also variables that do not occur *)
48 (* ctx does not distinguish successive blocks of cofix, since there may be no
49 * lambda separating them *)
50 let convert_term uri t =
51 let rec aux octx (ctx : ctx list) n_fix uri = function
52 | Cic.CoFix (k, fl) ->
54 UriManager.uri_of_string
55 (UriManager.buri_of_uri uri^"/"^
56 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
58 let bctx, fixpoints_tys, tys, _ =
60 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
61 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
62 let r = NReference.reference_of_ouri buri(NReference.CoFix idx) in
63 ctx @ [Fix (r,name,ty)], fixpoints_ty @ fixpoints,ty::tys,idx+1)
66 let bctx = bctx @ ctx in
67 let n_fl = List.length fl in
70 (fun (types,len) (n,ty,_) ->
71 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
76 (fun (name,_,bo) ty (l,fixpoints) ->
77 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
78 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
79 fixpoints_bo @ fixpoints)
80 fl tys ([],fixpoints_tys)
83 NUri.nuri_of_ouri buri,0,[],[],
84 NCic.Fixpoint (false, fl, (`Generated, `Definition))
87 (NCic.Const (NReference.reference_of_ouri buri (NReference.CoFix k))),
91 UriManager.uri_of_string
92 (UriManager.buri_of_uri uri^"/"^
93 UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
96 let bctx, fixpoints_tys, tys, _ =
98 (fun (name,recno,ty,_) (ctx, fixpoints, tys, idx) ->
99 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
100 if idx = k then rno := recno;
102 NReference.reference_of_ouri buri (NReference.Fix (idx,recno))
104 ctx @ [Fix (r,name,ty)], fixpoints_ty@fixpoints,ty::tys,idx+1)
107 let bctx = bctx @ ctx in
108 let n_fl = List.length fl in
111 (fun (types,len) (n,_,ty,_) ->
112 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
117 (fun (name,rno,_,bo) ty (l,fixpoints) ->
118 let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
119 let _, free, _ = context_tassonomy bctx in
120 let rno = rno + free in
121 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
122 fixpoints_bo @ fixpoints)
123 fl tys ([],fixpoints_tys)
126 NUri.nuri_of_ouri buri,0,[],[],
127 NCic.Fixpoint (true, fl, (`Generated, `Definition))
131 (NReference.reference_of_ouri buri (NReference.Fix (k,!rno)))),
134 let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
135 (match List.nth ctx (n-1) with
136 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
137 splat_args ctx (NCic.Const r), []
138 | Ce _ when n <= bound -> NCic.Rel n, []
139 | Fix _ (* BUG 3 fix nested *)
140 | Ce _ -> NCic.Rel (n-n_fix), [])
141 | Cic.Lambda (name, (s as old_s), t) ->
142 let s, fixpoints_s = aux octx ctx n_fix uri s in
143 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
144 let octx = Some (name, Cic.Decl old_s) :: octx in
145 let t, fixpoints_t = aux octx ctx n_fix uri t in
146 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
147 | Cic.Prod (name, (s as old_s), t) ->
148 let s, fixpoints_s = aux octx ctx n_fix uri s in
149 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
150 let octx = Some (name, Cic.Decl old_s) :: octx in
151 let t, fixpoints_t = aux octx ctx n_fix uri t in
152 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
153 | Cic.LetIn (name, (s as old_s), t) ->
154 let s, fixpoints_s = aux octx ctx n_fix uri s in
156 CicTypeChecker.type_of_aux' [] octx old_s CicUniv.oblivion_ugraph
158 let ty, fixpoints_ty = aux octx ctx n_fix uri old_ty in
159 let ctx = Ce (cn_to_s name, NCic.Def (s, ty)) :: ctx in
160 let octx = Some (name, Cic.Def (old_s, Some old_ty)) :: octx in
161 let t, fixpoints_t = aux octx ctx n_fix uri t in
162 NCic.LetIn (cn_to_s name, ty, s, t),
163 fixpoints_s @ fixpoints_t @ fixpoints_ty
165 let t, fixpoints_t = aux octx ctx n_fix uri t in
166 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
167 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
168 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
169 | Cic.Sort Cic.Set -> NCic.Sort NCic.Set,[]
170 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
171 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
172 (* calculate depth in the univ_graph*)
177 let t, fixpoints = aux octx ctx n_fix uri t in
178 (t::l,fixpoints@acc))
182 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
183 | _ -> NCic.Appl l, fixpoints)
184 | Cic.Const (curi, _) ->
185 NCic.Const (NReference.reference_of_ouri curi NReference.Def),[]
186 | Cic.MutInd (curi, tyno, _) ->
187 NCic.Const (NReference.reference_of_ouri curi (NReference.Ind tyno)),[]
188 | Cic.MutConstruct (curi, tyno, consno, _) ->
189 NCic.Const (NReference.reference_of_ouri curi
190 (NReference.Con (tyno,consno))),[]
191 | Cic.MutCase (curi, tyno, oty, t, branches) ->
192 let r = NReference.reference_of_ouri curi (NReference.Ind tyno) in
193 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
194 let t, fixpoints_t = aux octx ctx n_fix uri t in
195 let branches, fixpoints =
198 let t, fixpoints = aux octx ctx n_fix uri t in
199 (t::l,fixpoints@acc))
202 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
203 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
208 let convert_obj_aux uri = function
209 | Cic.Constant (name, None, ty, _, _) ->
210 let nty, fixpoints = convert_term uri ty in
211 assert(fixpoints = []);
212 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
214 | Cic.Constant (name, Some bo, ty, _, _) ->
215 let nbo, fixpoints_bo = convert_term uri bo in
216 let nty, fixpoints_ty = convert_term uri ty in
217 assert(fixpoints_ty = []);
218 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
219 fixpoints_bo @ fixpoints_ty
220 | Cic.InductiveDefinition (_,_,_,_) -> assert false (*
221 let ind = let _,x,_,_ = List.hd itl in x in
224 (fun name, _, ty, cl ->
225 [], name, convert_term ty,
226 List.map (fun name, ty -> [], name, convert_term ty) cl)
229 NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)) *)
231 | Cic.CurrentProof _ -> assert false
234 let convert_obj uri obj =
235 let o, fixpoints = convert_obj_aux uri obj in
236 let obj = NUri.nuri_of_ouri uri,0, [], [], o in