]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/oCic2NCic.ml
Avoid application to 0 arguments.
[helm.git] / helm / software / components / ng_kernel / oCic2NCic.ml
1 let convert_term = Obj.magic;;
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 NReference.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 (* we are lambda-lifting also variables that do not occur *)
65 (* ctx does not distinguish successive blocks of cofix, since there may be no
66  *   lambda separating them *)
67 let convert_term uri t = 
68   let rec aux octx (ctx : ctx list) n_fix uri = function
69     | Cic.CoFix (k, fl) ->
70         let buri = 
71           UriManager.uri_of_string 
72            (UriManager.buri_of_uri uri^"/"^
73             UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
74         in
75         let bctx, fixpoints_tys, tys, _ = 
76           List.fold_right 
77             (fun (name,ty,_) (ctx, fixpoints, tys, idx) -> 
78               let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
79               let r = NReference.reference_of_ouri buri(NReference.CoFix idx) in
80               ctx @ [Fix (r,name,ty)], fixpoints_ty @ fixpoints,ty::tys,idx+1)
81             fl ([], [], [], 0)
82         in
83         let bctx = bctx @ ctx in
84         let n_fl = List.length fl in
85         let boctx,_ =
86          List.fold_left
87           (fun (types,len) (n,ty,_) ->
88              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
89               len+1)) (octx,0) fl
90         in
91         let fl, fixpoints =
92           List.fold_right2 
93             (fun (name,_,bo) ty  (l,fixpoints) -> 
94                let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
95                (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
96                fixpoints_bo @ fixpoints)
97             fl tys ([],fixpoints_tys)
98         in
99         let obj = 
100           NUri.nuri_of_ouri buri,0,[],[],
101             NCic.Fixpoint (false, fl, (`Generated, `Definition)) 
102         in
103         splat_args ctx 
104          (NCic.Const (NReference.reference_of_ouri buri (NReference.CoFix k)))
105          n_fix,
106         fixpoints @ [obj]
107     | Cic.Fix (k, fl) ->
108         let buri = 
109           UriManager.uri_of_string 
110            (UriManager.buri_of_uri uri^"/"^
111             UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
112         in
113         let rno = ref 0 in
114         let bctx, fixpoints_tys, tys, _ = 
115           List.fold_right 
116             (fun (name,recno,ty,_) (ctx, fixpoints, tys, idx) -> 
117               let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
118               if idx = k then rno := recno;
119               let r = 
120                 NReference.reference_of_ouri buri (NReference.Fix (idx,recno)) 
121               in
122               ctx @ [Fix (r,name,ty)], fixpoints_ty@fixpoints,ty::tys,idx+1)
123             fl ([], [], [], 0)
124         in
125         let bctx = bctx @ ctx in
126         let n_fl = List.length fl in
127         let boctx,_ =
128          List.fold_left
129           (fun (types,len) (n,_,ty,_) ->
130              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
131               len+1)) (octx,0) fl
132         in
133         let fl, fixpoints =
134           List.fold_right2 
135             (fun (name,rno,_,bo) ty (l,fixpoints) -> 
136                let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
137                let _, free, _ = context_tassonomy bctx in
138                let rno = rno + free in
139                (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
140                fixpoints_bo @ fixpoints)
141             fl tys ([],fixpoints_tys)
142         in
143         let obj = 
144           NUri.nuri_of_ouri buri,0,[],[],
145             NCic.Fixpoint (true, fl, (`Generated, `Definition)) 
146         in
147         splat_args ctx
148           (NCic.Const 
149             (NReference.reference_of_ouri buri (NReference.Fix (k,!rno))))
150           n_fix,
151         fixpoints @ [obj]
152     | Cic.Rel n ->
153         let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
154         (match List.nth ctx (n-1) with
155         | Fix (r,_,_) when n < primo_ce_dopo_fix -> 
156             splat_args_for_rel ctx (NCic.Const r), []
157         | Ce _ when n <= bound -> NCic.Rel n, []
158         | Fix _ (* BUG 3 fix nested *) 
159         | Ce _ -> NCic.Rel (n-n_fix), [])
160     | Cic.Lambda (name, (s as old_s), t) ->
161         let s, fixpoints_s = aux octx ctx n_fix uri s in
162         let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
163         let octx = Some (name, Cic.Decl old_s) :: octx in
164         let t, fixpoints_t = aux octx ctx n_fix uri t in
165         NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
166     | Cic.Prod (name, (s as old_s), t) ->
167         let s, fixpoints_s = aux octx ctx n_fix uri s in
168         let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
169         let octx = Some (name, Cic.Decl old_s) :: octx in
170         let t, fixpoints_t = aux octx ctx n_fix uri t in
171         NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
172     | Cic.LetIn (name, (s as old_s), t) ->
173         let s, fixpoints_s = aux octx ctx n_fix uri s in
174         let old_ty,_ = 
175           CicTypeChecker.type_of_aux' [] octx old_s CicUniv.oblivion_ugraph 
176         in
177         let ty, fixpoints_ty = aux octx ctx n_fix uri old_ty in
178         let ctx = Ce (cn_to_s name, NCic.Def (s, ty)) :: ctx in
179         let octx = Some (name, Cic.Def (old_s, Some old_ty)) :: octx in
180         let t, fixpoints_t = aux octx ctx n_fix uri t in
181         NCic.LetIn (cn_to_s name, ty, s, t), 
182         fixpoints_s @ fixpoints_t @ fixpoints_ty
183     | Cic.Cast (t,ty) ->
184         let t, fixpoints_t = aux octx ctx n_fix uri t in
185         let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
186         NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
187     | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
188     | Cic.Sort Cic.Set -> NCic.Sort NCic.Set,[]
189     | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
190     | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[] 
191        (* calculate depth in the univ_graph*)
192     | Cic.Appl l -> 
193         let l, fixpoints =
194           List.fold_right 
195              (fun t (l,acc) -> 
196                let t, fixpoints = aux octx ctx n_fix uri t in 
197                (t::l,fixpoints@acc))
198              l ([],[])
199         in
200         (match l with
201         | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
202         | _ -> NCic.Appl l, fixpoints)
203     | Cic.Const (curi, _) -> 
204         NCic.Const (NReference.reference_of_ouri curi NReference.Def),[]
205     | Cic.MutInd (curi, tyno, _) -> 
206         NCic.Const (NReference.reference_of_ouri curi (NReference.Ind tyno)),[]
207     | Cic.MutConstruct (curi, tyno, consno, _) -> 
208         NCic.Const (NReference.reference_of_ouri curi 
209         (NReference.Con (tyno,consno))),[]
210     | Cic.MutCase (curi, tyno, oty, t, branches) ->
211         let r = NReference.reference_of_ouri curi (NReference.Ind tyno) in
212         let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
213         let t, fixpoints_t = aux octx ctx n_fix uri t in
214         let branches, fixpoints =
215           List.fold_right 
216              (fun t (l,acc) -> 
217                let t, fixpoints = aux octx ctx n_fix uri t in 
218                (t::l,fixpoints@acc))
219              branches ([],[])
220         in
221         NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
222     | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
223   in
224    aux [] [] 0 uri t
225 ;;
226
227 let convert_obj_aux uri = function
228  | Cic.Constant (name, None, ty, _, _) ->
229      let nty, fixpoints = convert_term uri ty in
230      assert(fixpoints = []);
231      NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
232      fixpoints
233  | Cic.Constant (name, Some bo, ty, _, _) ->
234      let nbo, fixpoints_bo = convert_term uri bo in
235      let nty, fixpoints_ty = convert_term uri ty in
236      assert(fixpoints_ty = []);
237      NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
238      fixpoints_bo @ fixpoints_ty
239  | Cic.InductiveDefinition (_,_,_,_) -> assert false (*
240      let ind = let _,x,_,_ = List.hd itl in x in
241      let itl = 
242        List.map 
243          (fun name, _, ty, cl ->
244             [], name, convert_term ty, 
245               List.map (fun name, ty -> [], name, convert_term ty) cl) 
246          itl        
247      in
248      NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)) *)
249  | Cic.Variable _ 
250  | Cic.CurrentProof _ -> assert false
251 ;;
252
253 let convert_obj uri obj = 
254   let o, fixpoints = convert_obj_aux uri obj in
255   let obj = NUri.nuri_of_ouri uri,0, [], [], o in
256   fixpoints @ [obj]
257 ;;