]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nCicElim.ml
0c1a98ec342a9848406f925bc60056257afa1a59
[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 recno = List.length final_params 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)
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,None,None,branches) in
114  let where = List.length final_params - 1 in
115  let res =
116   CicNotationPt.LetRec (`Inductive,
117    [final_params, (rec_name,ty), bo, where], rec_name)
118  in
119   prerr_endline
120    (BoxPp.render_to_string
121      ~map_unicode_to_tex:false
122      (function x::_ -> x | _ -> assert false)
123      80 (CicNotationPres.render (fun _ -> None)
124      (TermContentPres.pp_ast res)));
125   prerr_endline "#####";
126   let cobj = ("xxx", [], None, `Joint {
127       Content.joint_id = "yyy";
128       joint_kind = `Recursive [recno];
129       joint_defs =
130        [ `Definition {
131             Content.def_name = Some srec_name;
132             def_id = "zzz";
133             def_aref = "www";
134             def_term = bo;
135             def_type = 
136               List.fold_right 
137                 (fun x t -> CicNotationPt.Binder(`Forall,x,t))
138                 final_params cty
139           }
140        ];
141     })
142   in
143   let ids_to_nrefs = Hashtbl.create 1 in
144   let boxml = Content2pres.ncontent2pres ~ids_to_nrefs cobj in
145   prerr_endline (
146    (BoxPp.render_to_string ~map_unicode_to_tex:false
147      (function x::_ -> x | _ -> assert false) 80 
148      (CicNotationPres.mpres_of_box boxml)));
149   CicNotationPt.Theorem (`Definition,srec_name,CicNotationPt.Implicit,Some res)
150 ;;
151
152 let mk_elims (uri,_,_,_,obj) =
153  let ast_of_sort s =
154    match s with
155       NCic.Prop -> `Prop,"ind"
156     | NCic.Type u ->
157        let u = NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] (NCic.Sort s) in
158        `NType u, "rect_" ^ u
159  in
160   match obj with
161      NCic.Inductive (true,leftno,itl,_) ->
162       List.map (fun s -> mk_elim uri leftno itl (ast_of_sort s))
163        (NCic.Prop::
164          List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
165    | _ -> []
166 ;;