]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicLibrary.ml
e2d800157dd4d57c4f44852dd6735b6dfc6ebd0e
[helm.git] / helm / software / components / ng_kernel / nCicLibrary.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 exception ObjectNotFound of string Lazy.t
15
16 let storage = ref [];;
17
18 let aliases = ref [];;
19
20 let resolve name =
21  try
22   HExtlib.filter_map
23    (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
24  with
25   Not_found -> raise (ObjectNotFound (lazy name))
26 ;;
27
28 let aliases_of uri =
29  try
30   HExtlib.filter_map
31    (fun (uri',_,nref) -> if NUri.eq uri' uri then Some nref else None) !aliases
32  with
33   Not_found -> raise (ObjectNotFound (lazy (NUri.string_of_uri uri)))
34 ;;
35
36 let add_obj u obj =
37  storage := (u,obj)::!storage;
38   let _,height,_,_,obj = obj in
39   let references =
40    match obj with
41       NCic.Constant (_,name,None,_,_) ->
42        [u,name,NReference.reference_of_spec u NReference.Decl]
43     | NCic.Constant (_,name,Some _,_,_) ->
44        [u,name,NReference.reference_of_spec u (NReference.Def height)]
45     | NCic.Fixpoint (is_ind,fl,_) ->
46        HExtlib.list_mapi
47         (fun (_,name,recno,_,_) i ->
48           if is_ind then
49            u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
50           else
51            u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
52     | NCic.Inductive (inductive,leftno,il,_) ->
53        List.flatten
54         (HExtlib.list_mapi
55          (fun (_,iname,_,cl) i ->
56            HExtlib.list_mapi
57             (fun (_,cname,_) j->
58               u,cname,
59                NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
60             ) cl @
61            [u,iname,
62              NReference.reference_of_spec u
63               (NReference.Ind (inductive,i,leftno))]
64          ) il)
65   in
66    aliases := references @ !aliases
67 ;;
68
69 let cache = NUri.UriHash.create 313;;
70
71 let get_obj u =
72  try List.assq u !storage
73  with Not_found ->
74   try NUri.UriHash.find cache u
75   with Not_found ->
76     (* in the final implementation should get it from disk *)
77     let ouri = NCic2OCic.ouri_of_nuri u in
78     try
79       let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
80       let l = OCic2NCic.convert_obj ouri o in
81       List.iter (fun (u,_,_,_,_ as o) -> 
82   (*       prerr_endline ("+"^NUri.string_of_uri u);  *)
83         NUri.UriHash.add cache u o) l;
84       HExtlib.list_last l
85     with CicEnvironment.Object_not_found u -> 
86       raise (ObjectNotFound 
87                (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
88 ;;
89
90 let clear_cache () = NUri.UriHash.clear cache;;