]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nCicElim.ml
54d8d5df62cfd27e2d9a28e862943fd5e1553c10
[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_elims (uri,_,_,_,obj) =
38  match obj with
39     NCic.Inductive (true,leftno,[it],_) ->
40      let _,ind_name,ty,cl = it in
41      let rec_name = ind_name ^ "_rect" in
42      let rec_name = mk_id rec_name in
43      let name_of_k id = mk_id ("H_" ^ id) in
44      let p_name = mk_id "Q_" in
45      let params,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
46      let params = List.rev_map (function name,_ -> mk_id name) params in
47      let args,sort = NCicReduction.split_prods ~subst:[] [] (-1) ty in
48      let rec_arg = mk_id (fresh_name ()) in
49      let args = List.rev_map (function name,_ -> mk_id name) args in
50      let p_ty =
51       List.fold_right
52        (fun name res -> CicNotationPt.Binder (`Forall,(name,None),res)) args
53        (CicNotationPt.Binder
54         (`Forall,
55          (rec_arg,Some (CicNotationPt.Appl (mk_id ind_name :: params @ args))),
56          CicNotationPt.Sort (`Type (CicUniv.fresh ())))) in
57      let args = args @ [rec_arg] in
58      let k_names = List.map (function _,name,_ -> name_of_k name) cl in
59      let final_params =
60       List.map (function name -> name, None) params @
61       [p_name,Some p_ty] @
62       List.map (function name -> name, None) k_names @
63       List.map (function name -> name, None) args in
64      let ty = Some (CicNotationPt.Appl (p_name :: args)) in
65      let branches =
66       List.map
67        (function (_,name,ty) ->
68          let _,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
69          let cargs,ty= my_split_prods ~subst:[] [] (-1) ty in
70          let cargs_and_recursive_args =
71           List.rev_map
72            (function
73                name,NCic.Def _ -> assert false
74              | name,NCic.Decl (NCic.Const nref)
75              | name,NCic.Decl (NCic.Appl (NCic.Const nref::_))
76                when
77                 let NReference.Ref (uri',_) = nref in
78                  NUri.eq uri uri'
79                ->
80                 let name = mk_id name in
81                  name, Some (
82                  CicNotationPt.Appl
83                   (rec_name ::
84                    params @
85                    [p_name] @
86                    k_names @
87                    List.map (fun _ -> CicNotationPt.Implicit) (List.tl args) @
88                    [name]))
89              | name,_ -> mk_id name,None
90            ) cargs in
91          let cargs,recursive_args = List.split cargs_and_recursive_args in
92          let recursive_args = HExtlib.filter_map (fun x -> x) recursive_args in
93           CicNotationPt.Pattern (name,None,List.map (fun x -> x,None) cargs),
94            CicNotationPt.Appl (name_of_k name :: cargs @ recursive_args)
95        ) cl
96      in
97      let bo = CicNotationPt.Case (rec_arg,None,None,branches) in
98      let where = List.length final_params - 1 in
99      let res =
100       CicNotationPt.LetRec (`Inductive,
101        [final_params, (rec_name,ty), bo, where], rec_name)
102      in
103       prerr_endline (CicNotationPp.pp_term res);
104       prerr_endline "#####";
105       prerr_endline
106        (BoxPp.render_to_string
107          ~map_unicode_to_tex:false
108          (function x::_ -> x | _ -> assert false)
109          80 (CicNotationPres.render (Hashtbl.create 0)
110          (TermContentPres.pp_ast res)));
111
112       []
113   | _ -> []
114 ;;