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