(* Copyright (C) 2002, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://cs.unibo.it/helm/. *) module Codomain = struct type t = Cic.term let compare = Pervasives.compare end module S = Set.Make(Codomain) module TI = Discrimination_tree.DiscriminationTreeIndexing(S) type universe = TI.t (* (proof,ty) list *) type cache_key = Cic.term type cache_elem = | Failed_in of int | Succeded of Cic.term | UnderInspection | Notfound type cache = (universe * ((cache_key * cache_elem) list));; let cache_empty = (TI.empty,[]);; let get_candidates (univ,_) ty = S.elements (TI.retrieve_unifiables univ ty) ;; let rec unfold context = function | Cic.Prod(name,s,t) -> let t' = unfold ((Some (name,Cic.Decl s))::context) t in Cic.Prod(name,s,t') | t -> ProofEngineReduction.unfold context t let rec collapse_head_metas = function | Cic.Appl(a::l) -> let a' = collapse_head_metas a in (match a' with | Cic.Meta(n,m) -> Cic.Meta(n,m) | t -> let l' = List.map collapse_head_metas l in Cic.Appl(t::l')) | t -> t ;; let rec head t = let rec aux = function | Cic.Prod(_,_,t) -> CicSubstitution.subst (Cic.Meta (-1,[])) (aux t) | t -> t in collapse_head_metas (aux t) ;; let index (univ,oldcache) key term = prerr_endline("ADD: "^CicPp.ppterm key^" |-> "^CicPp.ppterm term); (TI.index univ key term,oldcache) ;; let index_term_and_unfolded_term cache context t ty = let key = head ty in let cache = index cache key t in try let key = head (unfold context ty) in index cache key t with ProofEngineTypes.Fail _ -> cache ;; let cache_add_list cache context terms_and_types = List.fold_left (fun acc (term,ty) -> index_term_and_unfolded_term acc context term ty) cache terms_and_types let cache_examine (_,oldcache) cache_key = try List.assoc cache_key oldcache with Not_found -> Notfound ;; let cache_replace (univ,oldcache) key v = let oldcache = List.filter (fun (i,_) -> i <> key) oldcache in univ, (key,v)::oldcache ;; let cache_remove (univ,oldcache) key = let oldcache = List.filter (fun (i,_) -> i <> key) oldcache in univ,oldcache ;; let cache_add_failure cache cache_key depth = match cache_examine cache cache_key with | Failed_in i when i > depth -> cache | Notfound | Failed_in _ | UnderInspection -> cache_replace cache cache_key (Failed_in depth) | Succeded t -> prerr_endline (CicPp.ppterm t); assert false (* if succed it can't fail *) ;; let cache_add_success ((univ,_) as cache) cache_key proof = TI.index univ cache_key proof,snd (match cache_examine cache cache_key with | Failed_in _ -> cache_replace cache cache_key (Succeded proof) | UnderInspection -> cache_replace cache cache_key (Succeded proof) | Succeded t -> (* we may decide to keep the smallest proof *) cache | Notfound -> cache_replace cache cache_key (Succeded proof)) ;; let cache_add_underinspection ((univ,oldcache) as cache) cache_key depth = match cache_examine cache cache_key with | Failed_in i when i < depth -> cache_replace cache cache_key UnderInspection | Notfound -> univ,(cache_key,UnderInspection)::oldcache | Failed_in _ | UnderInspection | Succeded _ -> assert false (* it must be a new goal *) ;; let cache_print context (_,oldcache) = let names = List.map (function None -> None | Some (x,_) -> Some x) context in String.concat "\n" (HExtlib.filter_map (function | (k,Succeded _) -> Some (CicPp.pp k names) | _ -> None) oldcache) ;; let cache_remove_underinspection ((univ,oldcache) as cache) cache_key = match cache_examine cache cache_key with | Notfound | UnderInspection -> cache_remove cache cache_key | Failed_in _ -> assert false | Succeded _ -> assert false (* it must be a new goal *) ;; let cache_size (_,oldcache) = List.length (List.filter (function (_,Succeded _) -> true | _ -> false) oldcache) ;; let cache_clean (univ,oldcache) = univ,List.filter (function (_,Succeded _) -> true | _ -> false) oldcache ;;