]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/oCic2NCic.ml
Cooked objects are no longer well typed in the uncooked old environment.
[helm.git] / helm / software / components / ng_kernel / oCic2NCic.ml
1 module Ref = NReference
2
3 let cn_to_s = function
4   | Cic.Anonymous -> "_"
5   | Cic.Name s -> s
6 ;;
7
8 type ctx = 
9   | Ce of NCic.hypothesis 
10   | Fix of Ref.reference * string * NCic.term
11
12 let splat mk_pi ctx t =
13   List.fold_left
14     (fun t c -> 
15       match c with
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))
21     t ctx
22 ;;
23
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
29     in
30       split true 0 1 ctx
31 ;;
32
33 let splat_args_for_rel ctx t = 
34   let bound, free, primo_ce_dopo_fix = context_tassonomy ctx in
35   if free = 0 then t 
36   else
37     let rec aux = function
38       | 0 -> []
39       | n -> 
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)
43     in
44     NCic.Appl (t:: aux free)
45 ;;
46
47 let splat_args ctx t n_fix = 
48   let bound, free, primo_ce_dopo_fix = context_tassonomy ctx in
49   if ctx = [] then t
50   else
51    let rec aux = function
52      | 0 -> []
53      | n -> 
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)
59         ) :: aux (n-1)
60    in
61    NCic.Appl (t:: aux (List.length ctx))
62 ;;
63
64 exception Nothing_to_do;;
65
66 let fix_outty curi tyno t context outty =
67  let leftno,rightno =
68   match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
69      Cic.InductiveDefinition (tyl,_,leftno,_) ->
70       let _,_,arity,_ = List.nth tyl tyno in
71       let rec count_prods leftno context arity =
72        match leftno, CicReduction.whd context arity with
73           0, Cic.Sort _ -> 0
74         | 0, Cic.Prod (name,so,ty) ->
75            1 + count_prods 0 (Some (name, Cic.Decl so)::context) ty
76         | n, Cic.Prod (name,so,ty) ->
77            count_prods (leftno - 1) (Some (name, Cic.Decl so)::context) ty
78         | _,_ -> assert false
79       in
80 (*prerr_endline (UriManager.string_of_uri curi);
81 prerr_endline ("LEFTNO: " ^ string_of_int leftno ^ "  " ^ CicPp.ppterm arity);*)
82        leftno, count_prods leftno [] arity
83    | _ -> assert false in
84  let ens,args =
85   let tty,_= CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
86   match CicReduction.whd context tty with
87      Cic.MutInd (_,_,ens) -> ens,[]
88    | Cic.Appl (Cic.MutInd (_,_,ens)::args) ->
89       ens,fst (HExtlib.split_nth leftno args)
90    | _ -> assert false
91  in
92   let rec aux n irl context outsort =
93    match n, CicReduction.whd context outsort with
94       0, Cic.Prod _ -> raise Nothing_to_do
95     | 0, _ ->
96        let irl = List.rev irl in
97        let ty = CicSubstitution.lift rightno (Cic.MutInd (curi,tyno,ens)) in
98        let ty =
99         if args = [] && irl = [] then ty
100         else
101          Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
102        let he = CicSubstitution.lift (rightno + 1) outty in
103        let t =
104         if irl = [] then he
105         else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
106        in
107         Cic.Lambda (Cic.Anonymous, ty, t)
108     | n, Cic.Prod (name,so,ty) ->
109        let ty' =
110         aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
111        in
112         Cic.Lambda (name,so,ty')
113     | _,_ -> assert false
114   in
115 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
116    let outsort =
117     fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
118    in
119     try aux rightno [] context outsort
120     with Nothing_to_do -> outty
121 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
122 ;;
123
124 let fix_outtype t =
125  let module C = Cic in
126  let rec aux context =
127   function
128      C.Rel _ as t -> t
129    | C.Var (uri,exp_named_subst) ->
130       let exp_named_subst' =
131        List.map (function i,t -> i, (aux context t)) exp_named_subst in
132         C.Var (uri,exp_named_subst')
133    | C.Implicit _
134    | C.Meta _ -> assert false
135    | C.Sort _ as t -> t
136    | C.Cast (v,t) -> C.Cast (aux context v, aux context t)
137    | C.Prod (n,s,t) ->
138         C.Prod (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
139    | C.Lambda (n,s,t) ->
140        C.Lambda (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
141    | C.LetIn (n,s,ty,t) ->
142       C.LetIn
143        (n, aux context s, aux context ty,
144         aux ((Some (n, C.Def(s,ty)))::context) t)
145    | C.Appl l -> C.Appl (List.map (aux context) l)
146    | C.Const (uri,exp_named_subst) ->
147       let exp_named_subst' =
148        List.map (function i,t -> i, (aux context t)) exp_named_subst
149       in
150        C.Const (uri,exp_named_subst')
151    | C.MutInd (uri,tyno,exp_named_subst) ->
152       let exp_named_subst' =
153        List.map (function i,t -> i, (aux context t)) exp_named_subst
154       in
155        C.MutInd (uri, tyno, exp_named_subst')
156    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
157       let exp_named_subst' =
158        List.map (function i,t -> i, (aux context t)) exp_named_subst
159       in
160        C.MutConstruct (uri, tyno, consno, exp_named_subst')
161    | C.MutCase (uri, tyno, outty, term, patterns) ->
162       let outty = fix_outty uri tyno term context outty in
163        C.MutCase (uri, tyno, aux context outty,
164         aux context term, List.map (aux context) patterns)
165    | C.Fix (funno, funs) ->
166       let tys,_ =
167         List.fold_left
168           (fun (types,len) (n,_,ty,_) ->
169             ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
170               len+1
171           ) ([],0) funs
172       in
173        C.Fix (funno,
174         List.map
175          (fun (name, indidx, ty, bo) ->
176            (name, indidx, aux context ty, aux (tys@context) bo)
177          ) funs
178       )
179    | C.CoFix (funno, funs) ->
180       let tys,_ =
181         List.fold_left
182           (fun (types,len) (n,ty,_) ->
183             ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
184               len+1
185           ) ([],0) funs
186       in
187        C.CoFix (funno,
188         List.map
189          (fun (name, ty, bo) ->
190            (name, aux context ty, aux (tys@context) bo)
191          ) funs
192        )
193  in
194   aux [] t
195 ;;
196
197 (* we are lambda-lifting also variables that do not occur *)
198 (* ctx does not distinguish successive blocks of cofix, since there may be no
199  *   lambda separating them *)
200 let convert_term uri t = 
201   let rec aux octx (ctx : ctx list) n_fix uri = function
202     | Cic.CoFix (k, fl) ->
203         let buri = 
204           UriManager.uri_of_string 
205            (UriManager.buri_of_uri uri^"/"^
206             UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
207         in
208         let bctx, fixpoints_tys, tys, _ = 
209           List.fold_right 
210             (fun (name,ty,_) (ctx, fixpoints, tys, idx) -> 
211               let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
212               let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
213               Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
214             fl ([], [], [], 0)
215         in
216         let bctx = bctx @ ctx in
217         let n_fl = List.length fl in
218         let boctx,_ =
219          List.fold_left
220           (fun (types,len) (n,ty,_) ->
221              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
222               len+1)) (octx,0) fl
223         in
224         let fl, fixpoints =
225           List.fold_right2 
226             (fun (name,_,bo) ty  (l,fixpoints) -> 
227                let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
228                (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
229                fixpoints_bo @ fixpoints)
230             fl tys ([],fixpoints_tys)
231         in
232         let obj = 
233           NUri.nuri_of_ouri buri,0,[],[],
234             NCic.Fixpoint (false, fl, (`Generated, `Definition)) 
235         in
236         splat_args ctx 
237          (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
238          n_fix,
239         fixpoints @ [obj]
240     | Cic.Fix (k, fl) ->
241         let buri = 
242           UriManager.uri_of_string 
243            (UriManager.buri_of_uri uri^"/"^
244             UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
245         in
246         let bad_bctx, fixpoints_tys, tys, _ = 
247           List.fold_right 
248             (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) -> 
249               let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
250               let r =  (* recno is dummy here, must be lifted by the ctx len *)
251                 Ref.reference_of_ouri buri (Ref.Fix (idx,recno)) 
252               in
253               Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
254             fl ([], [], [], 0)
255         in
256         let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
257         let bctx = 
258           List.map (function 
259             | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
260               Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
261             | _ -> assert false) bad_bctx @ ctx
262         in
263         let n_fl = List.length fl in
264         let boctx,_ =
265          List.fold_left
266           (fun (types,len) (n,_,ty,_) ->
267              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
268               len+1)) (octx,0) fl
269         in
270         let rno_k = ref 0 in
271         let fl, fixpoints,_ =
272           List.fold_right2 
273             (fun (name,rno,_,bo) ty (l,fixpoints,idx) -> 
274                let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
275                let rno = rno + free in
276                if idx = k then rno_k := rno;
277                (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
278                fixpoints_bo @ fixpoints,idx+1)
279             fl tys ([],fixpoints_tys,0)
280         in
281         let obj = 
282           NUri.nuri_of_ouri buri,max_int,[],[],
283             NCic.Fixpoint (true, fl, (`Generated, `Definition)) 
284         in
285         splat_args ctx
286           (NCic.Const 
287             (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
288           n_fix,
289         fixpoints @ [obj]
290     | Cic.Rel n ->
291         let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
292         (match List.nth ctx (n-1) with
293         | Fix (r,_,_) when n < primo_ce_dopo_fix -> 
294             splat_args_for_rel ctx (NCic.Const r), []
295         | Ce _ when n <= bound -> NCic.Rel n, []
296         | Fix _ (* BUG 3 fix nested *) 
297         | Ce _ -> NCic.Rel (n-n_fix), [])
298     | Cic.Lambda (name, (s as old_s), t) ->
299         let s, fixpoints_s = aux octx ctx n_fix uri s in
300         let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
301         let octx = Some (name, Cic.Decl old_s) :: octx in
302         let t, fixpoints_t = aux octx ctx n_fix uri t in
303         NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
304     | Cic.Prod (name, (s as old_s), t) ->
305         let s, fixpoints_s = aux octx ctx n_fix uri s in
306         let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
307         let octx = Some (name, Cic.Decl old_s) :: octx in
308         let t, fixpoints_t = aux octx ctx n_fix uri t in
309         NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
310     | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
311         let te, fixpoints_s = aux octx ctx n_fix uri te in
312         let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
313         let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
314         let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
315         let t, fixpoints_t = aux octx ctx n_fix uri t in
316         NCic.LetIn (cn_to_s name, ty, te, t), 
317         fixpoints_s @ fixpoints_t @ fixpoints_ty
318     | Cic.Cast (t,ty) ->
319         let t, fixpoints_t = aux octx ctx n_fix uri t in
320         let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
321         NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
322     | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
323     | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
324     | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[] 
325     | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[] 
326        (* calculate depth in the univ_graph*)
327     | Cic.Appl l -> 
328         let l, fixpoints =
329           List.fold_right 
330              (fun t (l,acc) -> 
331                let t, fixpoints = aux octx ctx n_fix uri t in 
332                (t::l,fixpoints@acc))
333              l ([],[])
334         in
335         (match l with
336         | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
337         | _ -> NCic.Appl l, fixpoints)
338     | Cic.Const (curi, ens) -> 
339        aux_ens curi octx ctx n_fix uri ens
340         (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
341         | Cic.Constant (_,Some _,_,_,_) ->
342                NCic.Const (Ref.reference_of_ouri curi Ref.Def)
343         | Cic.Constant (_,None,_,_,_) ->
344                NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
345         | _ -> assert false)
346     | Cic.MutInd (curi, tyno, ens) -> 
347        aux_ens curi octx ctx n_fix uri ens
348         (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
349     | Cic.MutConstruct (curi, tyno, consno, ens) -> 
350        aux_ens curi octx ctx n_fix uri ens
351         (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
352     | Cic.MutCase (curi, tyno, outty, t, branches) ->
353         let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
354         let outty, fixpoints_outty = aux octx ctx n_fix uri outty in
355         let t, fixpoints_t = aux octx ctx n_fix uri t in
356         let branches, fixpoints =
357           List.fold_right 
358              (fun t (l,acc) -> 
359                let t, fixpoints = aux octx ctx n_fix uri t in 
360                (t::l,fixpoints@acc))
361              branches ([],[])
362         in
363         NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
364     | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
365   and aux_ens curi octx ctx n_fix uri ens he =
366    match ens with
367       [] -> he,[]
368     | _::_ ->
369       let params =
370        match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
371           Cic.Constant (_,_,_,params,_)
372         | Cic.InductiveDefinition (_,params,_,_) -> params
373         | Cic.Variable _
374         | Cic.CurrentProof _ -> assert false
375       in
376       let ens,objs =
377        List.fold_right
378         (fun uri (l,objs) ->
379           let t = List.assoc uri ens in
380           let t,o = aux octx ctx n_fix uri t in
381            t::l, o@objs
382         ) params ([],[])
383       in
384        NCic.Appl (he::ens),objs
385   in
386    aux [] [] 0 uri t
387 ;;
388
389 let cook mode vars t =
390  let t = fix_outtype t in
391  let varsno = List.length vars in
392  let t = CicSubstitution.lift varsno t in
393  let rec aux n acc l =
394   let subst =
395    snd(List.fold_left (fun (i,res) uri -> i+1,(uri,Cic.Rel i)::res) (1,[]) acc)
396   in
397   match l with
398      [] -> CicSubstitution.subst_vars subst t
399    | uri::uris ->
400     let bo,ty =
401      match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
402         Cic.Variable (_,bo,ty,_,_) ->
403          HExtlib.map_option fix_outtype bo, fix_outtype ty
404       | _ -> assert false in
405     let ty = CicSubstitution.subst_vars subst ty in
406     let bo = HExtlib.map_option (CicSubstitution.subst_vars subst) bo in
407     let id = Cic.Name (UriManager.name_of_uri uri) in
408     let t = aux (n-1) (uri::acc) uris in
409      match bo,ty,mode with
410         None,ty,`Lambda -> Cic.Lambda (id,ty,t)
411       | None,ty,`Pi -> Cic.Prod (id,ty,t)
412       | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
413  in
414   aux varsno [] vars
415 ;;
416
417 let convert_obj_aux uri = function
418  | Cic.Constant (name, None, ty, vars, _) ->
419      let ty = cook `Pi vars ty in
420      let nty, fixpoints = convert_term uri ty in
421      assert(fixpoints = []);
422      NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
423      fixpoints
424  | Cic.Constant (name, Some bo, ty, vars, _) ->
425      let bo = cook `Lambda vars bo in
426      let ty = cook `Pi vars ty in
427      let nbo, fixpoints_bo = convert_term uri bo in
428      let nty, fixpoints_ty = convert_term uri ty in
429      assert(fixpoints_ty = []);
430      NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
431      fixpoints_bo @ fixpoints_ty
432  | Cic.InductiveDefinition (itl,vars,leftno,_) -> 
433      let ind = let _,x,_,_ = List.hd itl in x in
434      let itl, fix_itl = 
435        List.fold_right
436          (fun (name, _, ty, cl) (itl,acc) ->
437             let ty = cook `Pi vars ty in
438             let ty, fix_ty = convert_term uri ty in
439             let cl, fix_cl = 
440               List.fold_right
441                (fun (name, ty) (cl,acc) -> 
442                  let ty = cook `Pi vars ty in
443                  let ty, fix_ty = convert_term uri ty in
444                  ([], name, ty)::cl, acc @ fix_ty)
445                cl ([],[])
446             in
447             ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
448          itl ([],[])
449      in
450      NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
451      fix_itl
452  | Cic.Variable _ 
453  | Cic.CurrentProof _ -> assert false
454 ;;
455
456 let convert_obj uri obj = 
457   let o, fixpoints = convert_obj_aux uri obj in
458   let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in
459   fixpoints @ [obj]
460 ;;