]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tactics/universe.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / tactics / universe.ml
1 (* Copyright (C) 2002, 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 module Codomain = struct 
27   type t = Cic.term 
28   let compare = Pervasives.compare 
29 end
30 module S = Set.Make(Codomain)
31 module TI = Discrimination_tree.Make(Cic_indexable.CicIndexable)(S)
32 type universe = TI.t
33
34 let empty = TI.empty ;;
35
36 let iter u f = 
37   TI.iter u 
38    (fun p s -> f p (S.elements s))
39 ;;
40
41 let get_candidates univ ty = 
42   S.elements (TI.retrieve_unifiables univ ty)
43 ;;
44
45 let in_universe univ ty =
46   let candidates = get_candidates univ ty in
47     List.fold_left 
48       (fun res cand ->
49          match res with
50            | Some found -> Some found
51            | None -> 
52                let candty,_ = 
53                  CicTypeChecker.type_of_aux' [] [] cand CicUniv.oblivion_ugraph in
54                let same ,_ = 
55                  CicReduction.are_convertible [] candty ty CicUniv.oblivion_ugraph in
56                if same then Some cand else None
57       ) None candidates
58 ;;
59
60 let rec unfold context = function
61   | Cic.Prod(name,s,t) -> 
62       let t' = unfold ((Some (name,Cic.Decl s))::context) t in
63         Cic.Prod(name,s,t')        
64   | t -> ProofEngineReduction.unfold context t
65
66 let rec collapse_head_metas t = 
67   match t with
68     | Cic.Appl([]) -> assert false
69     | Cic.Appl(a::l) -> 
70         let a' = collapse_head_metas a in
71           (match a' with
72              | Cic.Meta(n,m) -> Cic.Meta(n,m)
73              | t ->     
74                  let l' = List.map collapse_head_metas l in
75                    Cic.Appl(t::l'))
76     | Cic.Rel _ 
77     | Cic.Var _  
78     | Cic.Meta _ 
79     | Cic.Sort _ 
80     | Cic.Implicit _
81     | Cic.Const _ 
82     | Cic.MutInd _
83     | Cic.MutConstruct _ -> t
84     | Cic.LetIn _
85     | Cic.Lambda _
86     | Cic.Prod _
87     | Cic.Cast _
88     | Cic.MutCase _
89     | Cic.Fix _
90     | Cic.CoFix _ -> Cic.Meta(-1,[])
91 ;;
92
93 let rec dummies_of_coercions = 
94   function
95     | Cic.Appl (c::l) when CoercDb.is_a_coercion c <> None ->
96         Cic.Meta (-1,[])
97     | Cic.Appl l -> 
98         let l' = List.map dummies_of_coercions l in Cic.Appl l'
99     | Cic.Lambda(n,s,t) ->
100         let s' = dummies_of_coercions s in
101         let t' = dummies_of_coercions t in
102           Cic.Lambda (n,s',t')
103     | Cic.Prod(n,s,t) ->
104         let s' = dummies_of_coercions s in
105         let t' = dummies_of_coercions t in
106           Cic.Prod (n,s',t')        
107     | Cic.LetIn(n,s,ty,t) ->
108         let s' = dummies_of_coercions s in
109         let ty' = dummies_of_coercions ty in
110         let t' = dummies_of_coercions t in
111           Cic.LetIn (n,s',ty',t')        
112     | Cic.MutCase _ -> Cic.Meta (-1,[])
113     | t -> t
114 ;;
115
116
117 let rec head remove_coercions t = 
118   let clean_up t =
119     if remove_coercions then dummies_of_coercions t
120     else t in
121   let rec aux = function
122   | Cic.Prod(_,_,t) -> 
123       CicSubstitution.subst (Cic.Meta (-1,[])) (aux t)
124   | t -> t
125   in collapse_head_metas (clean_up (aux t))
126 ;;
127
128
129 let index univ key term =
130   (* flexible terms are not indexed *)
131   if key = Cic.Meta(-1,[]) then univ
132   else
133     ((*prerr_endline("ADD: "^CicPp.ppterm key^" |-> "^CicPp.ppterm term);*)
134      TI.index univ key term)
135 ;;
136
137 let keys context ty =
138   try
139     [head true ty; head true (unfold context ty)]
140   with ProofEngineTypes.Fail _ -> [head true ty]
141
142 let key term = head false term;;
143
144 let index_term_and_unfolded_term univ context t ty =
145   let key = head true ty in
146   let univ = index univ key t in
147   try  
148     let key = head true (unfold context ty) in
149     index univ key t
150   with ProofEngineTypes.Fail _ -> univ
151 ;;
152
153 let index_local_term univ context t ty =
154   let key = head true ty in
155   let univ = index univ key t in
156   let key1 = head false ty in
157   let univ =
158     if key<>key1 then index univ key1 t else univ in
159   try  
160     let key = head true (unfold context ty) in
161     index univ key t
162   with ProofEngineTypes.Fail _ -> univ
163 ;;
164
165
166 let index_list univ context terms_and_types =
167   List.fold_left  
168     (fun acc (term,ty) -> 
169        index_term_and_unfolded_term acc context term ty)
170     univ terms_and_types
171
172 ;;
173
174 let remove univ context term ty =
175   let key = head true ty in
176   let univ = TI.remove_index univ key term in
177   try  
178     let key = head true (unfold context ty) in
179     TI.remove_index univ key term
180   with ProofEngineTypes.Fail _ -> univ
181
182 let remove_uri univ uri =
183   let term = CicUtil.term_of_uri uri in
184   let ty,_ = CicTypeChecker.type_of_aux' [] [] term CicUniv.oblivion_ugraph in
185     remove univ [] term ty
186
187