]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nCicElim.ml
9db98de498d99e7e10e2f154d22e9a5f4c723484
[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   | l -> CicNotationPt.Appl l
42 ;;
43
44 let mk_elim uri leftno it (outsort,suffix) =
45  let _,ind_name,ty,cl = it in
46  let srec_name = ind_name ^ "_" ^ suffix in
47  let rec_name = mk_id srec_name in
48  let name_of_k id = mk_id ("H_" ^ id) in
49  let p_name = mk_id "Q_" in
50  let params,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
51  let params = List.rev_map (function name,_ -> mk_id name) params in
52  let args,sort = NCicReduction.split_prods ~subst:[] [] (-1) ty in
53  let args = List.rev_map (function name,_ -> mk_id name) args in
54  let rec_arg = mk_id (fresh_name ()) in
55  let p_ty =
56   List.fold_right
57    (fun name res -> CicNotationPt.Binder (`Forall,(name,None),res)) args
58    (CicNotationPt.Binder
59     (`Forall,
60      (rec_arg,Some (mk_appl (mk_id ind_name :: params @ args))),
61      CicNotationPt.Sort outsort)) in
62  let args = args @ [rec_arg] in
63  let k_names = List.map (function _,name,_ -> name_of_k name) cl in
64  let final_params =
65   List.map (function name -> name, None) params @
66   [p_name,Some p_ty] @
67   List.map (function name -> name, None) k_names @
68   List.map (function name -> name, None) args in
69  let cty = mk_appl (p_name :: args) in
70  let ty = Some cty in
71  let branches =
72   List.map
73    (function (_,name,ty) ->
74      let _,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
75      let cargs,ty= my_split_prods ~subst:[] [] (-1) ty in
76      let cargs_and_recursive_args =
77       List.rev_map
78        (function
79            _,NCic.Def _ -> assert false
80          | name,NCic.Decl ty ->
81             let context,ty = my_split_prods ~subst:[] [] (-1) ty in
82              match ty with
83               | NCic.Const nref
84               | NCic.Appl (NCic.Const nref::_)
85                  when
86                   let NReference.Ref (uri',_) = nref in
87                    NUri.eq uri uri'
88                  ->
89                   let abs = List.rev_map (fun id,_ -> mk_id id) context in
90                   let name = mk_id name in
91                    name, Some (
92                    List.fold_right
93                     (fun id res ->
94                       CicNotationPt.Binder (`Lambda,(id,None),res))
95                     abs
96                     (CicNotationPt.Appl
97                      (rec_name ::
98                       params @
99                       [p_name] @
100                       k_names @
101                       List.map (fun _ -> CicNotationPt.Implicit `JustOne)
102                        (List.tl args) @
103                       [mk_appl (name::abs)])))
104               | _ -> mk_id name,None
105        ) cargs in
106      let cargs,recursive_args = List.split cargs_and_recursive_args in
107      let recursive_args = HExtlib.filter_map (fun x -> x) recursive_args in
108       CicNotationPt.Pattern (name,None,List.map (fun x -> x,None) cargs),
109        mk_appl (name_of_k name :: cargs @ recursive_args)
110    ) cl
111  in
112  let bo = CicNotationPt.Case (rec_arg,Some (ind_name,None),None,branches) in
113  let recno = List.length final_params in
114  let where = recno - 1 in
115  let res =
116   CicNotationPt.LetRec (`Inductive,
117    [final_params, (rec_name,ty), bo, where], rec_name)
118  in
119 (*
120   prerr_endline
121    (BoxPp.render_to_string
122      ~map_unicode_to_tex:false
123      (function x::_ -> x | _ -> assert false)
124      80 (CicNotationPres.render (fun _ -> None)
125      (TermContentPres.pp_ast res)));
126   prerr_endline "#####";
127   let cobj = ("xxx", [], None, `Joint {
128       Content.joint_id = "yyy";
129       joint_kind = `Recursive [recno];
130       joint_defs =
131        [ `Definition {
132             Content.def_name = Some srec_name;
133             def_id = "zzz";
134             def_aref = "www";
135             def_term = bo;
136             def_type = 
137               List.fold_right 
138                 (fun x t -> CicNotationPt.Binder(`Forall,x,t))
139                 final_params cty
140           }
141        ];
142     })
143   in
144   let ids_to_nrefs = Hashtbl.create 1 in
145   let boxml = Content2pres.ncontent2pres ~ids_to_nrefs cobj in
146   prerr_endline (
147    (BoxPp.render_to_string ~map_unicode_to_tex:false
148      (function x::_ -> x | _ -> assert false) 80 
149      (CicNotationPres.mpres_of_box boxml)));
150 *)
151   CicNotationPt.Theorem
152    (`Definition,srec_name,CicNotationPt.Implicit `JustOne,Some res)
153 ;;
154
155 let ast_of_sort s =
156   match s with
157      NCic.Prop -> `Prop,"ind"
158    | NCic.Type u ->
159       let u = NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] (NCic.Sort s) in
160       (try
161         if String.sub u 0 4 = "Type" then
162          `NType (String.sub u 4 (String.length u - 4)), "rect_" ^ u
163         else if String.sub u 0 5 = "CProp" then
164          `NCProp (String.sub u 5 (String.length u - 5)), "rect_" ^ u
165         else
166          (prerr_endline u;
167          assert false)
168        with Failure _ -> assert false)
169 ;;
170
171 let mk_elims (uri,_,_,_,obj) =
172   match obj with
173     NCic.Inductive (true,leftno,[itl],_) ->
174       List.map (fun s -> mk_elim uri leftno itl (ast_of_sort s))
175        (NCic.Prop::
176          List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
177    | _ -> []
178 ;;
179
180 (********************* Projections **********************)
181
182 let mk_lambda =
183  function
184     [] -> assert false 
185   | [t] -> t
186   | l -> CicNotationPt.Appl l
187 ;;
188
189 let rec count_prods = function NCic.Prod (_,_,t) -> 1 + count_prods t | _ -> 0;;
190
191 let rec nth_prod projs n ty =
192  match ty with
193     NCic.Prod (_,s,_) when n=0 -> projs, s
194   | NCic.Prod (name,_,t) -> nth_prod (name::projs) (n-1) t
195   | _ -> assert false
196 ;;
197
198 (* this code should be unified with NTermCicContent.nast_of_cic0,
199    but the two contexts have different types *)
200 let rec pp rels =
201  function
202     NCic.Rel i -> List.nth rels (i - 1)
203   | NCic.Const _ as t ->
204      CicNotationPt.Ident
205       (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t,None)
206   | NCic.Sort s -> CicNotationPt.Sort (fst (ast_of_sort s))
207   | NCic.Meta _
208   | NCic.Implicit _ -> assert false
209   | NCic.Appl l -> CicNotationPt.Appl (List.map (pp rels) l)
210   | NCic.Prod (n,s,t) ->
211      let n = mk_id n in
212       CicNotationPt.Binder (`Pi, (n,Some (pp rels s)), pp (n::rels) t)
213   | NCic.Lambda (n,s,t) ->
214      let n = mk_id n in
215       CicNotationPt.Binder (`Lambda, (n,Some (pp rels s)), pp (n::rels) t)
216   | NCic.LetIn (n,s,ty,t) ->
217      let n = mk_id n in
218       CicNotationPt.LetIn ((n, Some (pp rels ty)), pp rels s, pp (n::rels) t)
219   | NCic.Match (NReference.Ref (uri,_) as r,outty,te,patterns) ->
220     let name = NUri.name_of_uri uri in
221     let case_indty = Some (name, None) in
222     let constructors, leftno =
223      let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys r in
224      let _,_,_,cl  = List.nth tys n in
225       cl,leftno
226     in
227     let rec eat_branch n rels ty pat =
228       match (ty, pat) with
229       | NCic.Prod (name, s, t), _ when n > 0 ->
230          eat_branch (pred n) rels t pat
231       | NCic.Prod (_, _, t), NCic.Lambda (name, s, t') ->
232           let cv, rhs = eat_branch 0 ((mk_id name)::rels) t t' in
233            (mk_id name, Some (pp rels s)) :: cv, rhs
234       | _, _ -> [], pp rels pat
235     in
236     let patterns =
237       try
238         List.map2
239           (fun (_, name, ty) pat ->
240             let capture_variables,rhs = eat_branch leftno rels ty pat in
241              CicNotationPt.Pattern (name, None, capture_variables), rhs
242           ) constructors patterns
243       with Invalid_argument _ -> assert false
244     in
245      CicNotationPt.Case (pp rels te, case_indty, Some (pp rels outty), patterns)
246 ;;
247
248 let mk_projection leftno tyname consname consty (projname,_,_) i =
249  let argsno = count_prods consty - leftno in
250  let rec aux names ty leftno =
251   match leftno,ty with
252    | 0,_ ->
253      let arg = mk_id "xxx" in
254      let arg_ty = mk_appl (mk_id tyname :: List.rev names) in
255      let bvar = mk_id "yyy" in
256      let underscore = CicNotationPt.Ident ("_",None),None in
257      let bvars =
258       HExtlib.mk_list underscore i @ [bvar,None] @
259        HExtlib.mk_list underscore (argsno - i -1) in
260      let branch = CicNotationPt.Pattern (consname,None,bvars), bvar in
261      let projs,outtype = nth_prod [] i ty in
262      let rels =
263       List.map
264        (fun name -> mk_appl (mk_id name :: List.rev names @ [arg])) projs
265       @ names in
266      let outtype = pp rels outtype in
267      let outtype= CicNotationPt.Binder (`Lambda, (arg, Some arg_ty), outtype) in
268       [arg, Some arg_ty], CicNotationPt.Case (arg,None,Some outtype,[branch])
269    | _,NCic.Prod (name,_,t) ->
270       let name = mk_id name in
271       let params,body = aux (name::names) t (leftno - 1) in
272        (name,None)::params, body
273    | _,_ -> assert false
274  in
275  let params,bo = aux [] consty leftno in
276  let pprojname = mk_id projname in
277  let res =
278   CicNotationPt.LetRec (`Inductive,
279    [params, (pprojname,None), bo, leftno], pprojname) in
280 (* prerr_endline
281    (BoxPp.render_to_string
282      ~map_unicode_to_tex:false
283      (function x::_ -> x | _ -> assert false)
284      80 (CicNotationPres.render (fun _ -> None)
285      (TermContentPres.pp_ast res)));*)
286   CicNotationPt.Theorem
287    (`Definition,projname,CicNotationPt.Implicit `JustOne,Some res)
288 ;;
289
290 let mk_projections (_,_,_,_,obj) =
291  match obj with
292     NCic.Inductive
293      (true,leftno,[_,tyname,_,[_,consname,consty]],(_,`Record fields))
294     ->
295      HExtlib.list_mapi (mk_projection leftno tyname consname consty) fields
296   | _ -> []
297 ;;