]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicLibrary.ml
1) unification hint now takes NG terms (as it should have been from the very
[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 LibraryOutOfSync of string Lazy.t
15
16 type timestamp =
17  (NUri.uri * NCic.obj) list *
18  (NUri.uri * string * NReference.reference) list *
19  NCic.obj NUri.UriMap.t;;
20
21 let time0 = [],[],NUri.UriMap.empty;;
22 let storage = ref [];;
23 let aliases = ref [];;
24 let cache = ref NUri.UriMap.empty;;
25
26 let time_travel (sto,ali,cac) = storage := sto; aliases := ali; cache := cac;;
27
28 let path_of_baseuri baseuri =
29  let uri = NUri.string_of_uri baseuri in
30  let path = String.sub uri 4 (String.length uri - 4) in
31  let path = Helm_registry.get "matita.basedir" ^ path in
32  let dirname = Filename.dirname path in
33   HExtlib.mkdir dirname;
34   path ^ ".ng"
35 ;;
36
37 let magic = 0;;
38
39 let require0 ~baseuri =
40  let ch = open_in (path_of_baseuri baseuri) in
41  let mmagic,dump = Marshal.from_channel ch in
42   close_in ch;
43   if mmagic <> magic then
44    raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
45   else
46    dump
47 ;;
48
49 let serialize ~baseuri dump =
50  let ch = open_out (path_of_baseuri baseuri) in
51  Marshal.to_channel ch (magic,dump) [Marshal.Closures];
52  close_out ch;
53  List.iter
54   (fun (uri,obj) ->
55     let ch = open_out (path_of_baseuri uri) in
56     Marshal.to_channel ch (magic,obj) [];
57     close_out ch
58   ) !storage;
59  time_travel time0
60 ;;
61
62 let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
63
64 let rec refresh_uri_in_term =
65  function
66     NCic.Const (NReference.Ref (u,spec)) ->
67      NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
68   | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
69 ;;
70
71 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
72  assert (metasenv = []);
73  assert (subst = []);
74  uri,height,metasenv,subst,
75   NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
76 ;;
77
78 module type Serializer =
79  sig
80   type status
81   type obj
82   val register:
83    string ->
84     ('a -> refresh_uri_in_term:(NCic.term -> NCic.term) -> status -> status) ->
85     ('a -> obj)
86   val serialize: baseuri:NUri.uri -> obj list -> unit
87   val require: baseuri:NUri.uri -> status -> status
88  end
89
90 module Serializer(S: sig type status end) =
91  struct
92   type status = S.status
93   type obj = string * Obj.t
94
95   let require1 = ref (fun _ -> assert false (* unknown data*))
96   let already_registered = ref []
97
98   let register tag require =
99    assert (not (List.mem tag !already_registered));
100    already_registered := tag :: !already_registered;
101    require1 :=
102     (fun (tag',data) as x ->
103      if tag=tag' then
104       require (Obj.magic data) ~refresh_uri_in_term
105      else
106       !require1 x);
107    (fun x -> tag,Obj.repr x)
108
109   let serialize = serialize
110
111   let require ~baseuri status =
112    let dump = require0 ~baseuri in
113     List.fold_right !require1 dump status
114 end
115
116 let decompile ~baseuri =
117  Unix.unlink (path_of_baseuri baseuri)
118  (* WE ARE NOT REMOVING ALL THE OBJECTS YET! *)
119 ;;
120
121 let fetch_obj uri =
122  let obj = require0 ~baseuri:uri in
123   refresh_uri_in_obj obj
124 ;;
125
126 let resolve name =
127  try
128   HExtlib.filter_map
129    (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
130  with
131   Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
132 ;;
133
134 let aliases_of uri =
135  try
136   HExtlib.filter_map
137    (fun (uri',_,nref) -> if NUri.eq uri' uri then Some nref else None) !aliases
138  with
139   Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
140 ;;
141
142 let add_obj u obj =
143  storage := (u,obj)::!storage;
144   let _,height,_,_,obj = obj in
145   let references =
146    match obj with
147       NCic.Constant (_,name,None,_,_) ->
148        [u,name,NReference.reference_of_spec u NReference.Decl]
149     | NCic.Constant (_,name,Some _,_,_) ->
150        [u,name,NReference.reference_of_spec u (NReference.Def height)]
151     | NCic.Fixpoint (is_ind,fl,_) ->
152        HExtlib.list_mapi
153         (fun (_,name,recno,_,_) i ->
154           if is_ind then
155            u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
156           else
157            u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
158     | NCic.Inductive (inductive,leftno,il,_) ->
159        List.flatten
160         (HExtlib.list_mapi
161          (fun (_,iname,_,cl) i ->
162            HExtlib.list_mapi
163             (fun (_,cname,_) j->
164               u,cname,
165                NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
166             ) cl @
167            [u,iname,
168              NReference.reference_of_spec u
169               (NReference.Ind (inductive,i,leftno))]
170          ) il)
171   in
172   aliases := references @ !aliases;
173   !storage,!aliases,!cache
174 ;;
175
176 let get_obj u =
177  try List.assq u !storage
178  with Not_found ->
179   try fetch_obj u
180   with Sys_error _ ->
181    try NUri.UriMap.find u !cache
182    with Not_found ->
183     let ouri = NCic2OCic.ouri_of_nuri u in
184     try
185       let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
186       let l = OCic2NCic.convert_obj ouri o in
187       List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
188       HExtlib.list_last l
189     with CicEnvironment.Object_not_found u -> 
190       raise (NCicEnvironment.ObjectNotFound 
191                (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
192 ;;
193
194 let clear_cache () = cache := NUri.UriMap.empty;;
195
196 NCicEnvironment.set_get_obj get_obj;;
197 NCicPp.set_get_obj get_obj;;