]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nCicElim.ml
4117c4ffa48423e9cff889e3822dc04f47b98e08
[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) =
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,CicNotationPt.Implicit `JustOne,Some res)
154 ;;
155
156 let ast_of_sort s =
157   let headrm prefix s =
158     try 
159       let len_prefix = String.length prefix in 
160       assert (String.sub s 0 len_prefix = prefix);
161       String.sub s len_prefix (String.length s - len_prefix)
162     with Invalid_argument _ -> assert false
163   in
164   match s with
165    | NCic.Prop -> `Prop,"ind"
166    | NCic.Type []  -> `NType "", "rect_Type"
167    | NCic.Type ((`Type,u) :: _) ->
168        let name = NUri.name_of_uri u in
169        `NType (headrm "Type" name), "rect_" ^ name
170    | NCic.Type ((`CProp,u) :: _) ->
171        let name = NUri.name_of_uri u in
172        `NCProp (headrm "Type" name), 
173        "rect_" ^ Str.replace_first (Str.regexp "Type") "CProp" name
174    | _ -> assert false
175 ;;
176
177 let mk_elims (uri,_,_,_,obj) =
178   match obj with
179     NCic.Inductive (true,leftno,[itl],_) ->
180       List.map (fun s -> mk_elim uri leftno itl (ast_of_sort s))
181        (NCic.Prop::
182          List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
183    | _ -> []
184 ;;
185
186 (********************* Projections **********************)
187
188 let mk_lambda =
189  function
190     [] -> assert false 
191   | [t] -> t
192   | l -> CicNotationPt.Appl l
193 ;;
194
195 let rec count_prods = function NCic.Prod (_,_,t) -> 1 + count_prods t | _ -> 0;;
196
197 let rec nth_prod projs n ty =
198  match ty with
199     NCic.Prod (_,s,_) when n=0 -> projs, s
200   | NCic.Prod (name,_,t) -> nth_prod (name::projs) (n-1) t
201   | _ -> assert false
202 ;;
203
204 (* this code should be unified with NTermCicContent.nast_of_cic0,
205    but the two contexts have different types *)
206 let rec pp rels =
207  function
208     NCic.Rel i -> List.nth rels (i - 1)
209   | NCic.Const _ as t ->
210      CicNotationPt.Ident
211       (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t,None)
212   | NCic.Sort s -> CicNotationPt.Sort (fst (ast_of_sort s))
213   | NCic.Meta _
214   | NCic.Implicit _ -> assert false
215   | NCic.Appl l -> CicNotationPt.Appl (List.map (pp rels) l)
216   | NCic.Prod (n,s,t) ->
217      let n = mk_id n in
218       CicNotationPt.Binder (`Pi, (n,Some (pp rels s)), pp (n::rels) t)
219   | NCic.Lambda (n,s,t) ->
220      let n = mk_id n in
221       CicNotationPt.Binder (`Lambda, (n,Some (pp rels s)), pp (n::rels) t)
222   | NCic.LetIn (n,s,ty,t) ->
223      let n = mk_id n in
224       CicNotationPt.LetIn ((n, Some (pp rels ty)), pp rels s, pp (n::rels) t)
225   | NCic.Match (NReference.Ref (uri,_) as r,outty,te,patterns) ->
226     let name = NUri.name_of_uri uri in
227     let case_indty = Some (name, None) in
228     let constructors, leftno =
229      let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys r in
230      let _,_,_,cl  = List.nth tys n in
231       cl,leftno
232     in
233     let rec eat_branch n rels ty pat =
234       match (ty, pat) with
235       | NCic.Prod (name, s, t), _ when n > 0 ->
236          eat_branch (pred n) rels t pat
237       | NCic.Prod (_, _, t), NCic.Lambda (name, s, t') ->
238           let cv, rhs = eat_branch 0 ((mk_id name)::rels) t t' in
239            (mk_id name, Some (pp rels s)) :: cv, rhs
240       | _, _ -> [], pp rels pat
241     in
242     let patterns =
243       try
244         List.map2
245           (fun (_, name, ty) pat ->
246             let capture_variables,rhs = eat_branch leftno rels ty pat in
247              CicNotationPt.Pattern (name, None, capture_variables), rhs
248           ) constructors patterns
249       with Invalid_argument _ -> assert false
250     in
251      CicNotationPt.Case (pp rels te, case_indty, Some (pp rels outty), patterns)
252 ;;
253
254 let mk_projection leftno tyname consname consty (projname,_,_) i =
255  let argsno = count_prods consty - leftno in
256  let rec aux names ty leftno =
257   match leftno,ty with
258    | 0,_ ->
259      let arg = mk_id "xxx" in
260      let arg_ty = mk_appl (mk_id tyname :: List.rev names) in
261      let bvar = mk_id "yyy" in
262      let underscore = CicNotationPt.Ident ("_",None),None in
263      let bvars =
264       HExtlib.mk_list underscore i @ [bvar,None] @
265        HExtlib.mk_list underscore (argsno - i -1) in
266      let branch = CicNotationPt.Pattern (consname,None,bvars), bvar in
267      let projs,outtype = nth_prod [] i ty in
268      let rels =
269       List.map
270        (fun name -> mk_appl (mk_id name :: List.rev names @ [arg])) projs
271       @ names in
272      let outtype = pp rels outtype in
273      let outtype= CicNotationPt.Binder (`Lambda, (arg, Some arg_ty), outtype) in
274       [arg, Some arg_ty], CicNotationPt.Case (arg,None,Some outtype,[branch])
275    | _,NCic.Prod (name,_,t) ->
276       let name = mk_id name in
277       let params,body = aux (name::names) t (leftno - 1) in
278        (name,None)::params, body
279    | _,_ -> assert false
280  in
281  let params,bo = aux [] consty leftno in
282  let pprojname = mk_id projname in
283  let res =
284   CicNotationPt.LetRec (`Inductive,
285    [params, (pprojname,None), bo, leftno], pprojname) in
286 (* prerr_endline
287    (BoxPp.render_to_string
288      ~map_unicode_to_tex:false
289      (function x::_ -> x | _ -> assert false)
290      80 (CicNotationPres.render (fun _ -> None)
291      (TermContentPres.pp_ast res)));*)
292   CicNotationPt.Theorem
293    (`Definition,projname,CicNotationPt.Implicit `JustOne,Some res)
294 ;;
295
296 let mk_projections (_,_,_,_,obj) =
297  match obj with
298     NCic.Inductive
299      (true,leftno,[_,tyname,_,[_,consname,consty]],(_,`Record fields))
300     ->
301      HExtlib.list_mapi (mk_projection leftno tyname consname consty) fields
302   | _ -> []
303 ;;