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