]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/oCic2NCic.ml
fix_outty fixed in order to perform eta-expansion when the term is not
[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 ty = Cic.MutInd (curi,tyno,ens) in
97        let ty =
98         if args = [] && irl = [] then ty
99         else
100          Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
101        let he = CicSubstitution.lift (rightno + 1) outty in
102        let t =
103         if irl = [] then he
104         else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
105        in
106         Cic.Lambda (Cic.Anonymous, ty, t)
107     | n, Cic.Prod (name,so,ty) ->
108        let ty' =
109         aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
110        in
111         Cic.Lambda (name,so,ty')
112     | _,_ -> assert false
113   in
114 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
115    let outsort =
116     fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
117    in
118     try aux rightno [] context outsort
119     with Nothing_to_do -> outty
120 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
121 ;;
122
123 (* we are lambda-lifting also variables that do not occur *)
124 (* ctx does not distinguish successive blocks of cofix, since there may be no
125  *   lambda separating them *)
126 let convert_term uri t = 
127   let rec aux octx (ctx : ctx list) n_fix uri = function
128     | Cic.CoFix (k, fl) ->
129         let buri = 
130           UriManager.uri_of_string 
131            (UriManager.buri_of_uri uri^"/"^
132             UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
133         in
134         let bctx, fixpoints_tys, tys, _ = 
135           List.fold_right 
136             (fun (name,ty,_) (ctx, fixpoints, tys, idx) -> 
137               let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
138               let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
139               Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
140             fl ([], [], [], 0)
141         in
142         let bctx = bctx @ ctx in
143         let n_fl = List.length fl in
144         let boctx,_ =
145          List.fold_left
146           (fun (types,len) (n,ty,_) ->
147              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
148               len+1)) (octx,0) fl
149         in
150         let fl, fixpoints =
151           List.fold_right2 
152             (fun (name,_,bo) ty  (l,fixpoints) -> 
153                let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
154                (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
155                fixpoints_bo @ fixpoints)
156             fl tys ([],fixpoints_tys)
157         in
158         let obj = 
159           NUri.nuri_of_ouri buri,0,[],[],
160             NCic.Fixpoint (false, fl, (`Generated, `Definition)) 
161         in
162         splat_args ctx 
163          (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
164          n_fix,
165         fixpoints @ [obj]
166     | Cic.Fix (k, fl) ->
167         let buri = 
168           UriManager.uri_of_string 
169            (UriManager.buri_of_uri uri^"/"^
170             UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
171         in
172         let bad_bctx, fixpoints_tys, tys, _ = 
173           List.fold_right 
174             (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) -> 
175               let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
176               let r =  (* recno is dummy here, must be lifted by the ctx len *)
177                 Ref.reference_of_ouri buri (Ref.Fix (idx,recno)) 
178               in
179               Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
180             fl ([], [], [], 0)
181         in
182         let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
183         let bctx = 
184           List.map (function 
185             | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
186               Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
187             | _ -> assert false) bad_bctx @ ctx
188         in
189         let n_fl = List.length fl in
190         let boctx,_ =
191          List.fold_left
192           (fun (types,len) (n,_,ty,_) ->
193              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
194               len+1)) (octx,0) fl
195         in
196         let rno_k = ref 0 in
197         let fl, fixpoints,_ =
198           List.fold_right2 
199             (fun (name,rno,_,bo) ty (l,fixpoints,idx) -> 
200                let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
201                let rno = rno + free in
202                if idx = k then rno_k := rno;
203                (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
204                fixpoints_bo @ fixpoints,idx+1)
205             fl tys ([],fixpoints_tys,0)
206         in
207         let obj = 
208           NUri.nuri_of_ouri buri,max_int,[],[],
209             NCic.Fixpoint (true, fl, (`Generated, `Definition)) 
210         in
211         splat_args ctx
212           (NCic.Const 
213             (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
214           n_fix,
215         fixpoints @ [obj]
216     | Cic.Rel n ->
217         let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
218         (match List.nth ctx (n-1) with
219         | Fix (r,_,_) when n < primo_ce_dopo_fix -> 
220             splat_args_for_rel ctx (NCic.Const r), []
221         | Ce _ when n <= bound -> NCic.Rel n, []
222         | Fix _ (* BUG 3 fix nested *) 
223         | Ce _ -> NCic.Rel (n-n_fix), [])
224     | Cic.Lambda (name, (s as old_s), t) ->
225         let s, fixpoints_s = aux octx ctx n_fix uri s in
226         let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
227         let octx = Some (name, Cic.Decl old_s) :: octx in
228         let t, fixpoints_t = aux octx ctx n_fix uri t in
229         NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
230     | Cic.Prod (name, (s as old_s), t) ->
231         let s, fixpoints_s = aux octx ctx n_fix uri s in
232         let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
233         let octx = Some (name, Cic.Decl old_s) :: octx in
234         let t, fixpoints_t = aux octx ctx n_fix uri t in
235         NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
236     | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
237         let te, fixpoints_s = aux octx ctx n_fix uri te in
238         let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
239         let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
240         let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
241         let t, fixpoints_t = aux octx ctx n_fix uri t in
242         NCic.LetIn (cn_to_s name, ty, te, t), 
243         fixpoints_s @ fixpoints_t @ fixpoints_ty
244     | Cic.Cast (t,ty) ->
245         let t, fixpoints_t = aux octx ctx n_fix uri t in
246         let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
247         NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
248     | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
249     | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
250     | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[] 
251     | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[] 
252        (* calculate depth in the univ_graph*)
253     | Cic.Appl l -> 
254         let l, fixpoints =
255           List.fold_right 
256              (fun t (l,acc) -> 
257                let t, fixpoints = aux octx ctx n_fix uri t in 
258                (t::l,fixpoints@acc))
259              l ([],[])
260         in
261         (match l with
262         | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
263         | _ -> NCic.Appl l, fixpoints)
264     | Cic.Const (curi, ens) -> 
265        aux_ens octx ctx n_fix uri ens
266         (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
267         | Cic.Constant (_,Some _,_,_,_) ->
268                NCic.Const (Ref.reference_of_ouri curi Ref.Def)
269         | Cic.Constant (_,None,_,_,_) ->
270                NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
271         | _ -> assert false)
272     | Cic.MutInd (curi, tyno, ens) -> 
273        aux_ens octx ctx n_fix uri ens
274         (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
275     | Cic.MutConstruct (curi, tyno, consno, ens) -> 
276        aux_ens octx ctx n_fix uri ens
277         (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
278     | Cic.MutCase (curi, tyno, outty, t, branches) ->
279         let outty = fix_outty curi tyno t octx outty in
280         let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
281         let outty, fixpoints_outty = aux octx ctx n_fix uri outty in
282         let t, fixpoints_t = aux octx ctx n_fix uri t in
283         let branches, fixpoints =
284           List.fold_right 
285              (fun t (l,acc) -> 
286                let t, fixpoints = aux octx ctx n_fix uri t in 
287                (t::l,fixpoints@acc))
288              branches ([],[])
289         in
290         NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
291     | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
292   and aux_ens octx ctx n_fix uri ens he =
293    match ens with
294       [] -> he,[]
295     | _::_ ->
296       let ens,objs =
297        List.fold_right
298         (fun (_,t) (l,objs) ->
299           let t,o = aux octx ctx n_fix uri t in
300            t::l, o@objs
301         ) ens ([],[])
302       in
303        NCic.Appl (he::ens),objs
304   in
305    aux [] [] 0 uri t
306 ;;
307
308 let cook mode vars t =
309  let t = CicSubstitution.lift (List.length vars) t in
310  snd (List.fold_right
311   (fun uri (n,t) ->
312     let t = CicSubstitution.subst_vars [uri,Cic.Rel 1] t in
313     let bo,ty =
314      match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
315         Cic.Variable (_,bo,ty,_,_) -> bo,ty
316       | _ -> assert false in
317     let id = Cic.Name (UriManager.name_of_uri uri) in
318     let t =
319      match bo,ty,mode with
320         None,ty,`Lambda -> Cic.Lambda (id,ty,t)
321       | None,ty,`Pi -> Cic.Prod (id,ty,t)
322       | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
323     in
324      n+1,t
325   ) vars (1,t))
326 ;;
327
328 let convert_obj_aux uri = function
329  | Cic.Constant (name, None, ty, vars, _) ->
330      let ty = cook `Pi vars ty in
331      let nty, fixpoints = convert_term uri ty in
332      assert(fixpoints = []);
333      NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
334      fixpoints
335  | Cic.Constant (name, Some bo, ty, vars, _) ->
336      let bo = cook `Lambda vars bo in
337      let ty = cook `Pi vars ty in
338      let nbo, fixpoints_bo = convert_term uri bo in
339      let nty, fixpoints_ty = convert_term uri ty in
340      assert(fixpoints_ty = []);
341      NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
342      fixpoints_bo @ fixpoints_ty
343  | Cic.InductiveDefinition (itl,vars,leftno,_) -> 
344      let ind = let _,x,_,_ = List.hd itl in x in
345      let itl, fix_itl = 
346        List.fold_right
347          (fun (name, _, ty, cl) (itl,acc) ->
348             let ty = cook `Pi vars ty in
349             let ty, fix_ty = convert_term uri ty in
350             let cl, fix_cl = 
351               List.fold_right
352                (fun (name, ty) (cl,acc) -> 
353                  let ty = cook `Pi vars ty in
354                  let ty, fix_ty = convert_term uri ty in
355                  ([], name, ty)::cl, acc @ fix_ty)
356                cl ([],[])
357             in
358             ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
359          itl ([],[])
360      in
361      NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
362      fix_itl
363  | Cic.Variable _ 
364  | Cic.CurrentProof _ -> assert false
365 ;;
366
367 let convert_obj uri obj = 
368   let o, fixpoints = convert_obj_aux uri obj in
369   let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in
370   fixpoints @ [obj]
371 ;;