]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nCicElim.ml
Release 0.5.9.
[helm.git] / helm / software / components / ng_tactics / nCicElim.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
13
14 let fresh_name =
15  let i = ref 0 in
16  function () ->
17   incr i;
18   "x_" ^ string_of_int !i
19 ;;
20
21 let mk_id id =
22  let id = if id = "_" then fresh_name () else id in
23   CicNotationPt.Ident (id,None)
24 ;;
25
26 (*CSC: cut&paste from nCicReduction.split_prods, but does not check that
27   the return type is a sort *)
28 let rec my_split_prods ~subst context n te =
29   match (n, NCicReduction.whd ~subst context te) with
30    | (0, _) -> context,te
31    | (n, NCic.Prod (name,so,ta)) ->
32        my_split_prods ~subst ((name,(NCic.Decl so))::context) (n - 1) ta
33    | (n, _) when n <= 0 -> context,te
34    | (_, _) -> raise (Failure "my_split_prods")
35 ;;
36
37 let mk_appl =
38  function
39     [] -> assert false
40   | [x] -> x
41   | CicNotationPt.Appl l1 :: l2 -> CicNotationPt.Appl (l1 @ l2)
42   | l -> CicNotationPt.Appl l
43 ;;
44
45 let mk_elim uri leftno it (outsort,suffix) pragma =
46  let _,ind_name,ty,cl = it in
47  let srec_name = ind_name ^ "_" ^ suffix in
48  let rec_name = mk_id srec_name in
49  let name_of_k id = mk_id ("H_" ^ id) in
50  let p_name = mk_id "Q_" in
51  let params,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
52  let params = List.rev_map (function name,_ -> mk_id name) params in
53  let args,sort = NCicReduction.split_prods ~subst:[] [] (-1) ty in
54  let args = List.rev_map (function name,_ -> mk_id name) args in
55  let rec_arg = mk_id (fresh_name ()) in
56  let p_ty =
57   List.fold_right
58    (fun name res -> CicNotationPt.Binder (`Forall,(name,None),res)) args
59    (CicNotationPt.Binder
60     (`Forall,
61      (rec_arg,Some (mk_appl (mk_id ind_name :: params @ args))),
62      CicNotationPt.Sort outsort)) in
63  let args = args @ [rec_arg] in
64  let k_names = List.map (function _,name,_ -> name_of_k name) cl in
65  let final_params =
66   List.map (function name -> name, None) params @
67   [p_name,Some p_ty] @
68   List.map (function name -> name, None) k_names @
69   List.map (function name -> name, None) args in
70  let cty = mk_appl (p_name :: args) in
71  let ty = Some cty in
72  let branches =
73   List.map
74    (function (_,name,ty) ->
75      let _,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
76      let cargs,ty= my_split_prods ~subst:[] [] (-1) ty in
77      let cargs_and_recursive_args =
78       List.rev_map
79        (function
80            _,NCic.Def _ -> assert false
81          | name,NCic.Decl ty ->
82             let context,ty = my_split_prods ~subst:[] [] (-1) ty in
83              match ty with
84               | NCic.Const nref
85               | NCic.Appl (NCic.Const nref::_)
86                  when
87                   let NReference.Ref (uri',_) = nref in
88                    NUri.eq uri uri'
89                  ->
90                   let abs = List.rev_map (fun id,_ -> mk_id id) context in
91                   let name = mk_id name in
92                    name, Some (
93                    List.fold_right
94                     (fun id res ->
95                       CicNotationPt.Binder (`Lambda,(id,None),res))
96                     abs
97                     (CicNotationPt.Appl
98                      (rec_name ::
99                       params @
100                       [p_name] @
101                       k_names @
102                       List.map (fun _ -> CicNotationPt.Implicit `JustOne)
103                        (List.tl args) @
104                       [mk_appl (name::abs)])))
105               | _ -> mk_id name,None
106        ) cargs in
107      let cargs,recursive_args = List.split cargs_and_recursive_args in
108      let recursive_args = HExtlib.filter_map (fun x -> x) recursive_args in
109       CicNotationPt.Pattern (name,None,List.map (fun x -> x,None) cargs),
110        mk_appl (name_of_k name :: cargs @ recursive_args)
111    ) cl
112  in
113  let bo = CicNotationPt.Case (rec_arg,Some (ind_name,None),None,branches) in
114  let recno = List.length final_params in
115  let where = recno - 1 in
116  let res =
117   CicNotationPt.LetRec (`Inductive,
118    [final_params, (rec_name,ty), bo, where], rec_name)
119  in
120 (*
121   prerr_endline
122    (BoxPp.render_to_string
123      ~map_unicode_to_tex:false
124      (function x::_ -> x | _ -> assert false)
125      80 (CicNotationPres.render (fun _ -> None)
126      (TermContentPres.pp_ast res)));
127   prerr_endline "#####";
128   let cobj = ("xxx", [], None, `Joint {
129       Content.joint_id = "yyy";
130       joint_kind = `Recursive [recno];
131       joint_defs =
132        [ `Definition {
133             Content.def_name = Some srec_name;
134             def_id = "zzz";
135             def_aref = "www";
136             def_term = bo;
137             def_type = 
138               List.fold_right 
139                 (fun x t -> CicNotationPt.Binder(`Forall,x,t))
140                 final_params cty
141           }
142        ];
143     })
144   in
145   let ids_to_nrefs = Hashtbl.create 1 in
146   let boxml = Content2pres.ncontent2pres ~ids_to_nrefs cobj in
147   prerr_endline (
148    (BoxPp.render_to_string ~map_unicode_to_tex:false
149      (function x::_ -> x | _ -> assert false) 80 
150      (CicNotationPres.mpres_of_box boxml)));
151 *)
152   CicNotationPt.Theorem
153    (`Definition,srec_name,
154       CicNotationPt.Implicit `JustOne,Some res,pragma)
155 ;;
156
157 let ast_of_sort s =
158   let headrm prefix s =
159     try 
160       let len_prefix = String.length prefix in 
161       assert (String.sub s 0 len_prefix = prefix);
162       String.sub s len_prefix (String.length s - len_prefix)
163     with Invalid_argument _ -> assert false
164   in
165   match s with
166    | NCic.Prop -> `Prop,"ind"
167    | NCic.Type []  -> `NType "", "rect_Type"
168    | NCic.Type ((`Type,u) :: _) ->
169        let name = NUri.name_of_uri u in
170        `NType (headrm "Type" name), "rect_" ^ name
171    | NCic.Type ((`CProp,u) :: _) ->
172        let name = NUri.name_of_uri u in
173        `NCProp (headrm "Type" name), 
174        "rect_" ^ Str.replace_first (Str.regexp "Type") "CProp" name
175    | _ -> assert false
176 ;;
177
178 let mk_elims (uri,_,_,_,obj) =
179   match obj with
180     NCic.Inductive (true,leftno,[itl],_) ->
181       List.map (fun s -> mk_elim uri leftno itl (ast_of_sort s) (`Elim s))
182        (NCic.Prop::
183          List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
184    | _ -> []
185 ;;
186
187 (********************* Projections **********************)
188
189 let mk_lambda =
190  function
191     [] -> assert false 
192   | [t] -> t
193   | l -> CicNotationPt.Appl l
194 ;;
195
196 let rec count_prods = function NCic.Prod (_,_,t) -> 1 + count_prods t | _ -> 0;;
197
198 let rec nth_prod projs n ty =
199  match ty with
200     NCic.Prod (_,s,_) when n=0 -> projs, s
201   | NCic.Prod (name,_,t) -> nth_prod (name::projs) (n-1) t
202   | _ -> assert false
203 ;;
204
205 (* this code should be unified with NTermCicContent.nast_of_cic0,
206    but the two contexts have different types *)
207 let rec pp rels =
208  function
209     NCic.Rel i -> List.nth rels (i - 1)
210   | NCic.Const _ as t ->
211      CicNotationPt.Ident
212       (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t,None)
213   | NCic.Sort s -> CicNotationPt.Sort (fst (ast_of_sort s))
214   | NCic.Meta _
215   | NCic.Implicit _ -> assert false
216   | NCic.Appl l -> CicNotationPt.Appl (List.map (pp rels) l)
217   | NCic.Prod (n,s,t) ->
218      let n = mk_id n in
219       CicNotationPt.Binder (`Pi, (n,Some (pp rels s)), pp (n::rels) t)
220   | NCic.Lambda (n,s,t) ->
221      let n = mk_id n in
222       CicNotationPt.Binder (`Lambda, (n,Some (pp rels s)), pp (n::rels) t)
223   | NCic.LetIn (n,s,ty,t) ->
224      let n = mk_id n in
225       CicNotationPt.LetIn ((n, Some (pp rels ty)), pp rels s, pp (n::rels) t)
226   | NCic.Match (NReference.Ref (uri,_) as r,outty,te,patterns) ->
227     let name = NUri.name_of_uri uri in
228     let case_indty = Some (name, None) in
229     let constructors, leftno =
230      let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys r in
231      let _,_,_,cl  = List.nth tys n in
232       cl,leftno
233     in
234     let rec eat_branch n rels ty pat =
235       match (ty, pat) with
236       | NCic.Prod (name, s, t), _ when n > 0 ->
237          eat_branch (pred n) rels t pat
238       | NCic.Prod (_, _, t), NCic.Lambda (name, s, t') ->
239           let cv, rhs = eat_branch 0 ((mk_id name)::rels) t t' in
240            (mk_id name, Some (pp rels s)) :: cv, rhs
241       | _, _ -> [], pp rels pat
242     in
243     let patterns =
244       try
245         List.map2
246           (fun (_, name, ty) pat ->
247             let capture_variables,rhs = eat_branch leftno rels ty pat in
248              CicNotationPt.Pattern (name, None, capture_variables), rhs
249           ) constructors patterns
250       with Invalid_argument _ -> assert false
251     in
252      CicNotationPt.Case (pp rels te, case_indty, Some (pp rels outty), patterns)
253 ;;
254
255 let mk_projection leftno tyname consname consty (projname,_,_) i =
256  let argsno = count_prods consty - leftno in
257  let rec aux names ty leftno =
258   match leftno,ty with
259    | 0,_ ->
260      let arg = mk_id "xxx" in
261      let arg_ty = mk_appl (mk_id tyname :: List.rev names) in
262      let bvar = mk_id "yyy" in
263      let underscore = CicNotationPt.Ident ("_",None),None in
264      let bvars =
265       HExtlib.mk_list underscore i @ [bvar,None] @
266        HExtlib.mk_list underscore (argsno - i -1) in
267      let branch = CicNotationPt.Pattern (consname,None,bvars), bvar in
268      let projs,outtype = nth_prod [] i ty in
269      let rels =
270       List.map
271        (fun name -> mk_appl (mk_id name :: List.rev names @ [arg])) projs
272       @ names in
273      let outtype = pp rels outtype in
274      let outtype= CicNotationPt.Binder (`Lambda, (arg, Some arg_ty), outtype) in
275       [arg, Some arg_ty], CicNotationPt.Case (arg,None,Some outtype,[branch])
276    | _,NCic.Prod (name,_,t) ->
277       let name = mk_id name in
278       let params,body = aux (name::names) t (leftno - 1) in
279        (name,None)::params, body
280    | _,_ -> assert false
281  in
282  let params,bo = aux [] consty leftno in
283  let pprojname = mk_id projname in
284  let res =
285   CicNotationPt.LetRec (`Inductive,
286    [params, (pprojname,None), bo, leftno], pprojname) in
287 (* prerr_endline
288    (BoxPp.render_to_string
289      ~map_unicode_to_tex:false
290      (function x::_ -> x | _ -> assert false)
291      80 (CicNotationPres.render (fun _ -> None)
292      (TermContentPres.pp_ast res)));*)
293   CicNotationPt.Theorem
294    (`Definition,projname,CicNotationPt.Implicit `JustOne,Some res,`Projection)
295 ;;
296
297 let mk_projections (_,_,_,_,obj) =
298  match obj with
299     NCic.Inductive
300      (true,leftno,[_,tyname,_,[_,consname,consty]],(_,`Record fields))
301     ->
302      HExtlib.list_mapi (mk_projection leftno tyname consname consty) fields
303   | _ -> []
304 ;;