]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/autoCache.ml
Theorems from the library and from the context are indexed also in "unfolded"
[helm.git] / components / tactics / autoCache.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.DiscriminationTreeIndexing(S)
32 type universe = TI.t
33 (* (proof,ty) list *)
34 type cache_key = Cic.term
35 type cache_elem = 
36   | Failed_in of int
37   | Succeded of Cic.term
38   | UnderInspection
39   | Notfound
40 type cache = (universe * ((cache_key * cache_elem) list));;
41
42 let cache_empty = (TI.empty,[]);;
43 let get_candidates (univ,_) ty = 
44   S.elements (TI.retrieve_unifiables univ ty)
45 ;;
46
47 let rec unfold context = function
48   | Cic.Prod(name,s,t) -> 
49       let t' = unfold ((Some (name,Cic.Decl s))::context) t in
50         Cic.Prod(name,s,t')     
51   | t -> ProofEngineReduction.unfold context t
52   
53 let rec head = function 
54   | Cic.Prod(_,_,t) -> CicSubstitution.subst (Cic.Meta (-1,[])) (head t)
55   | t -> t
56 ;;
57
58 let index ((univ,oldcache) as cache) key term =
59   match key with
60   | Cic.Meta _ -> cache
61   | _ -> 
62       prerr_endline("ADD: "^CicPp.ppterm key^" |-> "^CicPp.ppterm term);
63       (TI.index univ key term,oldcache)
64 ;;
65
66 let index_term_and_unfolded_term cache context t ty =
67   let key = head ty in
68   let cache = index cache key t in
69   try  
70     let key = head (unfold context ty) in
71     index cache key t
72   with ProofEngineTypes.Fail _ -> cache
73 ;;
74
75 let cache_add_library dbd proof gl cache = 
76   let univ = MetadataQuery.universe_of_goals ~dbd proof gl in
77   let terms = List.map CicUtil.term_of_uri univ in
78   let tyof t = fst(CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph)in
79   List.fold_left 
80     (fun acc term -> 
81       (* 
82       let key = head (tyof term) in
83       index acc key term
84       *)
85       index_term_and_unfolded_term acc [] term (tyof term))
86     cache terms
87 ;;
88 let cache_add_context context metasenv cache =  
89   let tail = function [] -> [] | h::tl -> tl in
90   let rc,_,_ = 
91     List.fold_left 
92     (fun (acc,i,ctx) ctxentry ->
93       match ctxentry with
94       | Some (_,Cic.Decl t) -> 
95           let ty = CicSubstitution.lift i t in
96           let elem = Cic.Rel i in
97           index_term_and_unfolded_term acc context elem ty, i+1, tail ctx
98           (* index acc key elem, i+1, tail ctx *)
99       | Some (_,Cic.Def (_,Some t)) ->
100           let ty = CicSubstitution.lift i t in
101           let elem = Cic.Rel i in
102           index_term_and_unfolded_term acc context elem ty, i+1, tail ctx
103       | Some (_,Cic.Def (t,None)) ->
104           let ctx = tail ctx in 
105           let ty = 
106             CicSubstitution.lift i
107               (fst (CicTypeChecker.type_of_aux' metasenv ctx t CicUniv.empty_ugraph))
108           in
109           let elem = Cic.Rel i in
110           index_term_and_unfolded_term acc context elem ty, i+1, tail ctx
111       |  _ -> acc,i+1,tail ctx)
112     (cache,1,context) context
113   in
114   rc
115 ;;
116
117 let cache_examine (_,oldcache) cache_key = 
118   try List.assoc cache_key oldcache with Not_found -> Notfound
119 ;;
120 let cache_replace (univ,oldcache) key v =
121   let oldcache = List.filter (fun (i,_) -> i <> key) oldcache in
122   univ, (key,v)::oldcache
123 ;;
124 let cache_remove (univ,oldcache) key =
125   let oldcache = List.filter (fun (i,_) -> i <> key) oldcache in
126   univ,oldcache
127 ;;
128 let cache_add_failure cache cache_key depth =
129   match cache_examine cache cache_key with
130   | Failed_in i when i > depth -> cache
131   | Notfound  
132   | Failed_in _ 
133   | UnderInspection -> cache_replace cache cache_key (Failed_in depth)
134   | Succeded t -> 
135       prerr_endline (CicPp.ppterm t);
136       assert false (* if succed it can't fail *)
137 ;;
138 let cache_add_success ((univ,_) as cache) cache_key proof =
139   TI.index univ cache_key proof,snd
140   (match cache_examine cache cache_key with
141   | Failed_in _ -> cache_replace cache cache_key (Succeded proof)
142   | UnderInspection -> cache_replace cache cache_key (Succeded proof)
143   | Succeded t -> (* we may decide to keep the smallest proof *) cache
144   | Notfound -> cache_replace cache cache_key (Succeded proof))
145 ;;
146 let cache_add_underinspection ((univ,oldcache) as cache) cache_key depth =
147   match cache_examine cache cache_key with
148   | Failed_in i when i < depth -> cache_replace cache cache_key UnderInspection
149   | Notfound -> univ,(cache_key,UnderInspection)::oldcache
150   | Failed_in _ 
151   | UnderInspection 
152   | Succeded _ -> assert false (* it must be a new goal *)
153 ;;
154 let cache_print context (_,oldcache) = 
155   let names = List.map (function None -> None | Some (x,_) -> Some x) context in
156   String.concat "\n" 
157     (HExtlib.filter_map 
158       (function 
159         | (k,Succeded _) -> Some (CicPp.pp k names)
160         | _ -> None)
161       oldcache)
162 ;;
163 let cache_remove_underinspection ((univ,oldcache) as cache) cache_key =
164   match cache_examine cache cache_key with
165   | Notfound 
166   | UnderInspection ->  cache_remove cache cache_key
167   | Failed_in _ -> assert false
168   | Succeded _ -> assert false (* it must be a new goal *)
169 ;;
170 let cache_size (_,oldcache) = 
171   List.length (List.filter (function (_,Succeded _) -> true | _ -> false) oldcache)
172 ;;
173 let cache_clean (univ,oldcache) = 
174   univ,List.filter (function (_,Succeded _) -> true | _ -> false) oldcache
175 ;;