]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nCicElim.ml
Generation of inductive principle for Type[0].
[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_elims (uri,_,_,_,obj) =
45  match obj with
46     NCic.Inductive (true,leftno,[it],_) ->
47      let _,ind_name,ty,cl = it in
48      let srec_name = ind_name ^ "_rect" in
49      let rec_name = mk_id srec_name in
50      let name_of_k id = mk_id ("H_" ^ id) in
51      let p_name = mk_id "Q_" in
52      let params,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
53      let params = List.rev_map (function name,_ -> mk_id name) params in
54      let args,sort = NCicReduction.split_prods ~subst:[] [] (-1) ty in
55      let args = List.rev_map (function name,_ -> mk_id name) args in
56      let rec_arg = mk_id (fresh_name ()) in
57      let p_ty =
58       List.fold_right
59        (fun name res -> CicNotationPt.Binder (`Forall,(name,None),res)) args
60        (CicNotationPt.Binder
61         (`Forall,
62          (rec_arg,Some (mk_appl (mk_id ind_name :: params @ args))),
63          CicNotationPt.Sort (`Type (CicUniv.fresh ())))) in
64      let args = args @ [rec_arg] in
65      let k_names = List.map (function _,name,_ -> name_of_k name) cl in
66      let final_params =
67       List.map (function name -> name, None) params @
68       [p_name,Some p_ty] @
69       List.map (function name -> name, None) k_names @
70       List.map (function name -> name, None) args in
71      let recno = List.length final_params in
72      let cty = mk_appl (p_name :: args) in
73      let ty = Some cty in
74      let branches =
75       List.map
76        (function (_,name,ty) ->
77          let _,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
78          let cargs,ty= my_split_prods ~subst:[] [] (-1) ty in
79          let cargs_and_recursive_args =
80           List.rev_map
81            (function
82                _,NCic.Def _ -> assert false
83              | name,NCic.Decl ty ->
84                 let context,ty = my_split_prods ~subst:[] [] (-1) ty in
85                  match ty with
86                   | NCic.Const nref
87                   | NCic.Appl (NCic.Const nref::_)
88                      when
89                       let NReference.Ref (uri',_) = nref in
90                        NUri.eq uri uri'
91                      ->
92                       let abs = List.rev_map (fun id,_ -> mk_id id) context in
93                       let name = mk_id name in
94                        name, Some (
95                        List.fold_right
96                         (fun id res ->
97                           CicNotationPt.Binder (`Lambda,(id,None),res))
98                         abs
99                         (CicNotationPt.Appl
100                          (rec_name ::
101                           params @
102                           [p_name] @
103                           k_names @
104                           List.map (fun _ -> CicNotationPt.Implicit)
105                            (List.tl args) @
106                           [mk_appl (name::abs)])))
107                   | _ -> mk_id name,None
108            ) cargs in
109          let cargs,recursive_args = List.split cargs_and_recursive_args in
110          let recursive_args = HExtlib.filter_map (fun x -> x) recursive_args in
111           CicNotationPt.Pattern (name,None,List.map (fun x -> x,None) cargs),
112            CicNotationPt.Appl (name_of_k name :: cargs @ recursive_args)
113        ) cl
114      in
115      let bo = CicNotationPt.Case (rec_arg,None,None,branches) in
116      let where = List.length final_params - 1 in
117      let res =
118       CicNotationPt.LetRec (`Inductive,
119        [final_params, (rec_name,ty), bo, where], rec_name)
120      in
121       prerr_endline (CicNotationPp.pp_term res);
122       prerr_endline "#####";
123       prerr_endline
124        (BoxPp.render_to_string
125          ~map_unicode_to_tex:false
126          (function x::_ -> x | _ -> assert false)
127          80 (CicNotationPres.render (fun _ -> None)
128          (TermContentPres.pp_ast res)));
129       prerr_endline "#####";
130       let cobj = ("xxx", [], None, `Joint {
131           Content.joint_id = "yyy";
132           joint_kind = `Recursive [recno];
133           joint_defs =
134            [ `Definition {
135                 Content.def_name = Some srec_name;
136                 def_id = "zzz";
137                 def_aref = "www";
138                 def_term = bo;
139                 def_type = 
140                   List.fold_right 
141                     (fun x t -> CicNotationPt.Binder(`Forall,x,t))
142                     final_params cty
143               }
144            ];
145         })
146       in
147       let ids_to_nrefs = Hashtbl.create 1 in
148       let boxml = Content2pres.ncontent2pres ~ids_to_nrefs cobj in
149       prerr_endline (
150        (BoxPp.render_to_string ~map_unicode_to_tex:false
151          (function x::_ -> x | _ -> assert false) 80 
152          (CicNotationPres.mpres_of_box boxml)));
153       [CicNotationPt.Theorem (`Definition,srec_name,CicNotationPt.Implicit,Some res)]
154   | _ -> []
155 ;;