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