]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/oCic2NCic.ml
many fixed in translation functions
[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 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 < 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 (* 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, _ = 
54           List.fold_right 
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)
59             fl (ctx, [], [], 0)
60         in
61         let buri = 
62           UriManager.uri_of_string 
63            (UriManager.string_of_uri uri^string_of_int (List.length ctx)^".con")
64         in
65         let n_fl = List.length fl in
66         let boctx,_ =
67          List.fold_left
68           (fun (types,len) (n,ty,_) ->
69              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
70               len+1)) (octx,0) fl
71         in
72         let fl, fixpoints =
73           List.fold_right2 
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)
79         in
80         let obj = 
81           NUri.nuri_of_ouri uri,0,[],[],
82             NCic.Fixpoint (false, fl, (`Generated, `Definition)) 
83         in
84         splat_args ctx 
85          (NCic.Const (NReference.reference_of_ouri uri (NReference.CoFix k))),
86         obj::fixpoints
87     | Cic.Fix (k, fl) ->
88         let rno = ref 0 in
89         let bctx, fixpoints_tys, tys, _ = 
90           List.fold_right 
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;
94               let r = 
95                 NReference.reference_of_ouri uri (NReference.Fix (idx,recno)) 
96               in
97               ctx @ [Fix (r,name,ty)], fixpoints_ty@fixpoints,ty::tys,idx+1)
98             fl (ctx, [], [], 0)
99         in
100         let buri = 
101           UriManager.uri_of_string 
102            (UriManager.string_of_uri uri^string_of_int (List.length ctx)^".con")
103         in
104         let n_fl = List.length fl in
105         let boctx,_ =
106          List.fold_left
107           (fun (types,len) (n,_,ty,_) ->
108              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
109               len+1)) (octx,0) fl
110         in
111         let fl, fixpoints =
112           List.fold_right2 
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)
119         in
120         let obj = 
121           NUri.nuri_of_ouri uri,0,[],[],
122             NCic.Fixpoint (true, fl, (`Generated, `Definition)) 
123         in
124         splat_args ctx
125           (NCic.Const 
126             (NReference.reference_of_ouri uri (NReference.Fix (k,!rno)))),
127         obj::fixpoints
128     | Cic.Rel n ->
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
148         let old_ty,_ = 
149           CicTypeChecker.type_of_aux' [] octx old_s CicUniv.oblivion_ugraph 
150         in
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
157     | Cic.Cast (t,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*)
166     | Cic.Appl l -> 
167         let l, fixpoints =
168           List.fold_right 
169              (fun t (l,acc) -> 
170                let t, fixpoints = aux octx ctx n_fix uri t in 
171                (t::l,fixpoints@acc))
172              l ([],[])
173         in
174         (match l with
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 =
189           List.fold_right 
190              (fun t (l,acc) -> 
191                let t, fixpoints = aux octx ctx n_fix uri t in 
192                (t::l,fixpoints@acc))
193              branches ([],[])
194         in
195         NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
196     | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
197   in
198    aux [] [] 0 uri t
199 ;;
200
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)),
206      fixpoints
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
215      let itl = 
216        List.map 
217          (fun name, _, ty, cl ->
218             [], name, convert_term ty, 
219               List.map (fun name, ty -> [], name, convert_term ty) cl) 
220          itl        
221      in
222      NCic.Inductive (ind, leftno, itl, (`Provided, `Regular)) *)
223  | Cic.Variable _ 
224  | Cic.CurrentProof _ -> assert false
225 ;;
226
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
230   obj, fixpoints
231 ;;