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