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 < 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) ->
53 let bctx, fixpoints_tys, tys, _ =
55 (fun (name,ty,_) (ctx, fixpoints, tys, idx) ->
56 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
57 let r = NReference.reference_of_ouri uri(NReference.CoFix idx) in
58 ctx @ [Fix (r,name,ty)], fixpoints_ty @ fixpoints,ty::tys,idx+1)
62 UriManager.uri_of_string
63 (UriManager.string_of_uri uri^string_of_int (List.length ctx)^".con")
65 let n_fl = List.length fl in
68 (fun (types,len) (n,ty,_) ->
69 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
74 (fun (name,_,bo) ty (l,fixpoints) ->
75 let bo, fixpoints_bo = aux boctx bctx (n_fix + n_fl) buri bo in
76 (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
77 fixpoints_bo @ fixpoints)
78 fl tys ([],fixpoints_tys)
81 NUri.nuri_of_ouri uri,0,[],[],
82 NCic.Fixpoint (false, fl, (`Generated, `Definition))
85 (NCic.Const (NReference.reference_of_ouri uri (NReference.CoFix k))),
89 let bctx, fixpoints_tys, tys, _ =
91 (fun (name,recno,ty,_) (ctx, fixpoints, tys, idx) ->
92 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
93 if idx = k then rno := recno;
95 NReference.reference_of_ouri uri (NReference.Fix (idx,recno))
97 ctx @ [Fix (r,name,ty)], fixpoints_ty@fixpoints,ty::tys,idx+1)
101 UriManager.uri_of_string
102 (UriManager.string_of_uri uri^string_of_int (List.length ctx)^".con")
104 let n_fl = List.length fl in
107 (fun (types,len) (n,_,ty,_) ->
108 (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
113 (fun (name,rno,_,bo) ty (l,fixpoints) ->
114 let bo, fixpoints_bo = aux boctx bctx (n_fix + n_fl) buri bo in
115 let rno = rno + List.length ctx - n_fix in
116 (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
117 fixpoints_bo @ fixpoints)
118 fl tys ([],fixpoints_tys)
121 NUri.nuri_of_ouri uri,0,[],[],
122 NCic.Fixpoint (true, fl, (`Generated, `Definition))
126 (NReference.reference_of_ouri uri (NReference.Fix (k,!rno)))),
129 let _, _, primo_ce_dopo_fix = context_tassonomy ctx in
130 (match List.nth ctx n with
131 | Fix (r,_,_) when n < primo_ce_dopo_fix ->
132 splat_args ctx (NCic.Const r), []
133 | Fix (_,_,_) | Ce _ -> NCic.Rel (n-n_fix), [])
134 | Cic.Lambda (name, (s as old_s), t) ->
135 let s, fixpoints_s = aux octx ctx n_fix uri s in
136 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
137 let octx = Some (name, Cic.Decl old_s) :: octx in
138 let t, fixpoints_t = aux octx ctx n_fix uri t in
139 NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
140 | Cic.Prod (name, (s as old_s), t) ->
141 let s, fixpoints_s = aux octx ctx n_fix uri s in
142 let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
143 let octx = Some (name, Cic.Decl old_s) :: octx in
144 let t, fixpoints_t = aux octx ctx n_fix uri t in
145 NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
146 | Cic.LetIn (name, (s as old_s), t) ->
147 let s, fixpoints_s = aux octx ctx n_fix uri s in
149 CicTypeChecker.type_of_aux' [] octx old_s CicUniv.oblivion_ugraph
151 let ty, fixpoints_ty = aux octx ctx n_fix uri old_ty in
152 let ctx = Ce (cn_to_s name, NCic.Def (s, ty)) :: ctx in
153 let octx = Some (name, Cic.Def (old_s, Some old_ty)) :: octx in
154 let t, fixpoints_t = aux octx ctx n_fix uri t in
155 NCic.LetIn (cn_to_s name, ty, s, t),
156 fixpoints_s @ fixpoints_t @ fixpoints_ty
158 let t, fixpoints_t = aux octx ctx n_fix uri t in
159 let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
160 NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
161 | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
162 | Cic.Sort Cic.Set -> NCic.Sort NCic.Set,[]
163 | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
164 | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[]
165 (* calculate depth in the univ_graph*)
170 let t, fixpoints = aux octx ctx n_fix uri t in
171 (t::l,fixpoints@acc))
175 | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
176 | _ -> NCic.Appl l, fixpoints)
177 | Cic.Const (curi, _) ->
178 NCic.Const (NReference.reference_of_ouri curi NReference.Def),[]
179 | Cic.MutInd (curi, tyno, _) ->
180 NCic.Const (NReference.reference_of_ouri curi (NReference.Ind tyno)),[]
181 | Cic.MutConstruct (curi, tyno, consno, _) ->
182 NCic.Const (NReference.reference_of_ouri curi
183 (NReference.Con (tyno,consno))),[]
184 | Cic.MutCase (curi, tyno, oty, t, branches) ->
185 let r = NReference.reference_of_ouri curi (NReference.Ind tyno) in
186 let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
187 let t, fixpoints_t = aux octx ctx n_fix uri t in
188 let branches, fixpoints =
191 let t, fixpoints = aux octx ctx n_fix uri t in
192 (t::l,fixpoints@acc))
195 NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
196 | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
201 let convert_obj_aux uri = function
202 | Cic.Constant (name, None, ty, _, _) ->
203 let nty, fixpoints = convert_term uri ty in
204 assert(fixpoints = []);
205 NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
207 | Cic.Constant (name, Some bo, ty, _, _) ->
208 let nbo, fixpoints_bo = convert_term uri bo in
209 let nty, fixpoints_ty = convert_term uri ty in
210 assert(fixpoints_ty = []);
211 NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
212 fixpoints_bo @ fixpoints_ty
213 | Cic.InductiveDefinition (_,_,_,_) -> assert false (*
214 let ind = let _,x,_,_ = List.hd itl in x in
217 (fun name, _, ty, cl ->
218 [], name, convert_term ty,
219 List.map (fun name, ty -> [], name, convert_term ty) cl)
222 NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)) *)
224 | Cic.CurrentProof _ -> assert false
227 let convert_obj uri obj =
228 let o, fixpoints = convert_obj_aux uri obj in
229 let obj = NUri.nuri_of_ouri uri,0, [], [], o in