]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/freshNamesGenerator.ml
- the result of a refinement is now cleared from dummy dependent types
[helm.git] / helm / ocaml / cic_unification / freshNamesGenerator.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* mk_fresh_name context name typ                      *)
27 (* returns an identifier which is fresh in the context *)
28 (* and that resembles [name] as much as possible.      *)
29 (* [typ] will be the type of the variable              *)
30 let mk_fresh_name metasenv context name ~typ =
31  let module C = Cic in
32   let basename =
33    match name with
34       C.Anonymous ->
35        (*CSC: great space for improvements here *)
36        (try
37          (match CicTypeChecker.type_of_aux' metasenv context typ with
38              C.Sort C.Prop
39            | C.Sort C.CProp -> "H"
40            | C.Sort C.Set -> "x"
41            | _ -> "H"
42          )
43         with CicTypeChecker.TypeCheckerFailure _ -> "H"
44        )
45     | C.Name name ->
46        Str.global_replace (Str.regexp "[0-9]*$") "" name
47   in
48    let already_used name =
49     List.exists (function Some (C.Name n,_) -> n=name | _ -> false) context
50    in
51     if not (already_used basename) then
52      C.Name basename
53     else
54      let rec try_next n =
55       let name' = basename ^ string_of_int n in
56        if already_used name' then
57         try_next (n+1)
58        else
59         C.Name name'
60      in
61       try_next 1
62 ;;
63
64 (* clean_dummy_dependent_types term                             *)
65 (* returns a copy of [term] where every dummy dependent product *)
66 (* have been replaced with a non-dependent product and where    *)
67 (* dummy let-ins have been removed.                             *)
68 let clean_dummy_dependent_types t =
69  let module C = Cic in
70   let rec aux k =
71    function
72       C.Rel m as t -> t,[k - m]
73     | C.Var (uri,exp_named_subst) ->
74        let exp_named_subst',rels = 
75         List.fold_right
76          (fun (uri,t) (exp_named_subst,rels) ->
77            let t',rels' = aux k t in
78             (uri,t')::exp_named_subst, rels' @ rels
79          ) exp_named_subst ([],[])
80        in
81         C.Var (uri,exp_named_subst'),rels
82     | C.Meta (i,l) ->
83        let l',rels =
84         List.fold_right
85          (fun t (l,rels) ->
86            let t',rels' =
87             match t with
88                None -> None,[]
89              | Some t ->
90                 let t',rels' = aux k t in
91                  Some t', rels'
92            in
93             t'::l, rels' @ rels
94          ) l ([],[])
95        in
96         C.Meta(i,l'),rels
97     | C.Sort _ as t -> t,[]
98     | C.Implicit as t -> t,[]
99     | C.Cast (te,ty) ->
100        let te',rels1 = aux k te in
101        let ty',rels2 = aux k ty in
102         C.Cast (te', ty'), rels1@rels2
103     | C.Prod (n,s,t) ->
104        let s',rels1 = aux k s in
105        let t',rels2 = aux (k+1) t in
106         let n' =
107          match n with
108             C.Anonymous ->
109              if List.mem k rels2 then assert false else C.Anonymous
110           | C.Name _ as n ->
111              if List.mem k rels2 then n else C.Anonymous
112         in
113          C.Prod (n', s', t'), rels1@rels2
114     | C.Lambda (n,s,t) ->
115        let s',rels1 = aux k s in
116        let t',rels2 = aux (k+1) t in
117         C.Lambda (n, s', t'), rels1@rels2
118     | C.LetIn (n,s,t) ->
119        let s',rels1 = aux k s in
120        let t',rels2 = aux (k+1) t in
121        let rels = rels1 @ rels2 in
122         if List.mem k rels2 then
123          C.LetIn (n, s', t'), rels
124         else
125          (* (C.Rel 1) is just a dummy term; any term would fit *)
126          CicSubstitution.subst (C.Rel 1) t', rels
127     | C.Appl l ->
128        let l',rels =
129         List.fold_right
130          (fun t (exp_named_subst,rels) ->
131            let t',rels' = aux k t in
132             t'::exp_named_subst, rels' @ rels
133          ) l ([],[])
134        in
135         C.Appl l', rels
136     | C.Const (uri,exp_named_subst) ->
137        let exp_named_subst',rels = 
138         List.fold_right
139          (fun (uri,t) (exp_named_subst,rels) ->
140            let t',rels' = aux k t in
141             (uri,t')::exp_named_subst, rels' @ rels
142          ) exp_named_subst ([],[])
143        in
144         C.Const (uri,exp_named_subst'),rels
145     | C.MutInd (uri,tyno,exp_named_subst) ->
146        let exp_named_subst',rels = 
147         List.fold_right
148          (fun (uri,t) (exp_named_subst,rels) ->
149            let t',rels' = aux k t in
150             (uri,t')::exp_named_subst, rels' @ rels
151          ) exp_named_subst ([],[])
152        in
153         C.MutInd (uri,tyno,exp_named_subst'),rels
154     | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
155        let exp_named_subst',rels = 
156         List.fold_right
157          (fun (uri,t) (exp_named_subst,rels) ->
158            let t',rels' = aux k t in
159             (uri,t')::exp_named_subst, rels' @ rels
160          ) exp_named_subst ([],[])
161        in
162         C.MutConstruct (uri,tyno,consno,exp_named_subst'),rels
163     | C.MutCase (sp,i,outty,t,pl) ->
164        let outty',rels1 = aux k outty in
165        let t',rels2 = aux k t in
166        let pl',rels3 =
167         List.fold_right
168          (fun t (exp_named_subst,rels) ->
169            let t',rels' = aux k t in
170             t'::exp_named_subst, rels' @ rels
171          ) pl ([],[])
172        in
173         C.MutCase (sp, i, outty', t', pl'), rels1 @ rels2 @rels3
174     | C.Fix (i, fl) ->
175        let len = List.length fl in
176        let fl',rels =
177         List.fold_right
178          (fun (name,i,ty,bo) (fl,rels) ->
179            let ty',rels1 = aux k ty in
180            let bo',rels2 = aux (k + len) bo in
181             (name,i,ty',bo')::fl, rels1 @ rels2 @ rels
182          ) fl ([],[])
183        in
184         C.Fix (i, fl'),rels
185     | C.CoFix (i, fl) ->
186        let len = List.length fl in
187        let fl',rels =
188         List.fold_right
189          (fun (name,ty,bo) (fl,rels) ->
190            let ty',rels1 = aux k ty in
191            let bo',rels2 = aux (k + len) bo in
192             (name,ty',bo')::fl, rels1 @ rels2 @ rels
193          ) fl ([],[])
194        in
195         C.CoFix (i, fl'),rels
196   in
197    fst (aux 0 t)
198 ;;