]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_proof_checking/cicEnvironment.ml
Debugging stuff removed.
[helm.git] / helm / ocaml / cic_proof_checking / cicEnvironment.ml
index fbe5e4b266018e4e3018fa1f619673ee572eecfc..f63b1618c5e6f4db654571a08ea7ddf8829cd1a8 100644 (file)
 (*                                                                            *)
 (******************************************************************************)
 
-let raise e = print_endline "***" ; flush stdout ; print_endline (Printexc.to_string e) ; flush stdout ; raise e;;
+let cleanup_tmp = true;;
+
+let trust_obj = function uri -> true;;
 
-(*CSC: forse i due seguenti tipi sono da unificare? *)
-type cooked_obj =
-   Cooked of Cic.obj
- | Frozen of Cic.obj
- | Unchecked of Cic.obj
 type type_checked_obj =
    CheckedObj of Cic.obj     (* cooked obj *)
- | UncheckedObj of Cic.obj   (* uncooked obj *)
+ | UncheckedObj of Cic.obj   (* uncooked obj to proof-check *)
 ;;
 
-exception NoFunctionProvided;;
-
-let cook_obj = ref (fun obj uri -> raise NoFunctionProvided);;
-
-let set_cooking_function foo =
- cook_obj := foo
-;;
 
+exception AlreadyCooked of string;;
 exception CircularDependency of string;;
+exception CouldNotFreeze of string;;
 exception CouldNotUnfreeze of string;;
-exception Impossible;;
-exception UncookedObj;;
-
-module HashedType =
- struct
-  type t = UriManager.uri * int    (* uri, livello di cottura *)
-  let equal (u1,n1) (u2,n2) = UriManager.eq u1 u2 && n1 = n2
-  let hash = Hashtbl.hash
- end
-;;
 
-(* Hashtable that uses == instead of = for testing equality *)
-module HashTable = Hashtbl.Make(HashedType);;
+(* Cache that uses == instead of = for testing equality *)
+(* Invariant: an object is always in at most one of the *)
+(* following states: unchecked, frozen and cooked.      *)
+module Cache :
+  sig
+   val find_or_add_unchecked :
+    UriManager.uri -> get_object_to_add:(unit -> Cic.obj) -> Cic.obj
+   val unchecked_to_frozen : UriManager.uri -> unit
+   val frozen_to_cooked :
+    uri:UriManager.uri -> unit
+   val find_cooked : key:UriManager.uri -> Cic.obj
+   val add_cooked : key:UriManager.uri -> Cic.obj -> unit
 
-let hashtable = HashTable.create 271;;
+   val dump_to_channel : ?callback:(string -> unit) -> out_channel -> unit
+   val restore_from_channel : ?callback:(string -> unit) -> in_channel -> unit
+   val empty : unit -> unit
+  end 
+=
+  struct
+   module CacheOfCookedObjects :
+    sig
+     val mem  : UriManager.uri -> bool
+     val find : UriManager.uri -> Cic.obj
+     val add  : UriManager.uri -> Cic.obj -> unit
 
-(* n is the number of time that the object must be cooked *)
-let get_obj_and_type_checking_info uri n =
- try
-   HashTable.find hashtable (uri,n)
- with
-  Not_found -> 
-   try
-    match HashTable.find hashtable (uri,0) with
-        Cooked _
-      | Frozen _ -> raise Impossible
-      | Unchecked _ as t -> t
-   with
-    Not_found ->
-     let filename = Getter.getxml uri in
-      let (annobj,_) = CicParser.term_of_xml filename uri false in
-       let obj = Deannotate.deannotate_obj annobj in
-        let output = Unchecked obj in
-         HashTable.add hashtable (uri,0) output ;
-         output
-;;
+      (** (de)serialization of type checker cache *)
+     val dump_to_channel : ?callback:(string -> unit) -> out_channel -> unit
+     val restore_from_channel : ?callback:(string -> unit) -> in_channel -> unit
+     val empty : unit -> unit
+    end
+   =
+    struct
+     module HashedType =
+      struct
+       type t = UriManager.uri
+       let equal = UriManager.eq
+       let hash = Hashtbl.hash
+      end
+     ;;
+     module HT = Hashtbl.Make(HashedType);;
+     let hashtable = HT.create 1009;;
+     let mem uri =
+      try
+       HT.mem hashtable uri
+      with
+       Not_found -> false
+     ;;
+     let find uri = HT.find hashtable uri
+     ;;
+     let add uri obj =
+      HT.add hashtable uri obj
+     ;;
 
-(* DANGEROUS!!!                                *)
-(* USEFUL ONLY DURING THE FIXING OF THE FILES  *)
-(* change_obj uri (Some newobj)                *)
-(*  maps uri to newobj in cache.               *)
-(* change_obj uri None                         *)
-(*  maps uri to a freeze dummy-object.         *)
-let change_obj uri newobj =
- let newobj =
-  match newobj with
-     Some newobj' -> Unchecked newobj'
-   | None         -> Frozen (Cic.Variable ("frozen-dummy", None, Cic.Implicit))
- in
-  HashTable.remove hashtable (uri,0) ;
-  HashTable.add hashtable (uri,0) newobj
-;;
+      (* used to hash cons uris on restore to grant URI structure unicity *)
+     let restore_uris =
+       let module C = Cic in
+       let recons uri =
+         UriManager.uri_of_string (UriManager.string_of_uri uri)
+       in
+       let rec restore_in_term =
+         function
+            (C.Rel _) as t -> t
+          | C.Var (uri,exp_named_subst) ->
+             let uri' = recons uri in
+             let exp_named_subst' =
+              List.map
+               (function (uri,t) ->(recons uri,restore_in_term t)) exp_named_subst
+             in
+              C.Var (uri',exp_named_subst')
+          | C.Meta (i,l) ->
+             let l' =
+              List.map
+               (function
+                   None -> None
+                 | Some t -> Some (restore_in_term t)
+               ) l
+             in
+              C.Meta(i,l')
+          | C.Sort _ as t -> t
+          | C.Implicit as t -> t
+          | C.Cast (te,ty) -> C.Cast (restore_in_term te, restore_in_term ty)
+          | C.Prod (n,s,t) -> C.Prod (n, restore_in_term s, restore_in_term t)
+          | C.Lambda (n,s,t) -> C.Lambda (n, restore_in_term s, restore_in_term t)
+          | C.LetIn (n,s,t) -> C.LetIn (n, restore_in_term s, restore_in_term t)
+          | C.Appl l -> C.Appl (List.map restore_in_term l)
+          | C.Const (uri,exp_named_subst) ->
+             let uri' = recons uri in
+             let exp_named_subst' = 
+              List.map
+               (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
+             in
+              C.Const (uri',exp_named_subst')
+          | C.MutInd (uri,tyno,exp_named_subst) ->
+             let uri' = recons uri in
+             let exp_named_subst' = 
+              List.map
+               (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
+             in
+              C.MutInd (uri',tyno,exp_named_subst')
+          | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
+             let uri' = recons uri in
+             let exp_named_subst' = 
+              List.map
+               (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
+             in
+              C.MutConstruct (uri',tyno,consno,exp_named_subst')
+          | C.MutCase (uri,i,outty,t,pl) ->
+             C.MutCase (recons uri, i, restore_in_term outty, restore_in_term t,
+              List.map restore_in_term pl)
+          | C.Fix (i, fl) ->
+             let len = List.length fl in
+             let liftedfl =
+              List.map
+               (fun (name, i, ty, bo) ->
+                 (name, i, restore_in_term ty, restore_in_term bo))
+                fl
+             in
+              C.Fix (i, liftedfl)
+          | C.CoFix (i, fl) ->
+             let len = List.length fl in
+             let liftedfl =
+              List.map
+               (fun (name, ty, bo) -> (name, restore_in_term ty, restore_in_term bo))
+                fl
+             in
+              C.CoFix (i, liftedfl)
+       in
+       function
+          C.Constant (name,bo,ty,params) ->
+            let bo' =
+              match bo with
+                None -> None
+              | Some bo -> Some (restore_in_term bo)
+            in
+            let ty' = restore_in_term ty in
+            let params' = List.map recons params in
+            C.Constant (name, bo', ty', params')
+        | C.CurrentProof (name,conjs,bo,ty,params) ->
+            let conjs' =
+              List.map
+                (function (i,hyps,ty) ->
+                  (i,
+                  List.map (function
+                      None -> None
+                    | Some (name,C.Decl t) ->
+                        Some (name,C.Decl (restore_in_term t))
+                    | Some (name,C.Def (bo,ty)) ->
+                        let ty' =
+                          match ty with
+                            None -> None
+                          | Some ty'' -> Some (restore_in_term ty'')
+                        in
+                        Some (name,C.Def (restore_in_term bo, ty'))) hyps,
+                  restore_in_term ty))
+                conjs
+            in
+            let bo' = restore_in_term bo in
+            let ty' = restore_in_term ty in
+            let params' = List.map recons params in
+            C.CurrentProof (name, conjs', bo', ty', params')
+        | C.Variable (name,bo,ty,params) ->
+            let bo' =
+              match bo with
+                None -> None
+              | Some bo -> Some (restore_in_term bo)
+            in
+            let ty' = restore_in_term ty in
+            let params' = List.map recons params in
+            C.Variable (name, bo', ty', params')
+        | C.InductiveDefinition (tl,params,paramsno) ->
+            let params' = List.map recons params in
+            let tl' =
+              List.map (function (name, inductive, ty, constructors) ->
+                name,
+                inductive,
+                restore_in_term ty,
+                (List.map
+                  (function (name, ty) -> name, restore_in_term ty)
+                  constructors))
+                tl
+            in
+            C.InductiveDefinition (tl', params', paramsno)
 
-let is_annotation_uri uri =
- Str.string_match (Str.regexp ".*\.ann$") (UriManager.string_of_uri uri) 0
-;;
+     let dump_to_channel ?(callback = ignore) oc =
+       HT.iter (fun uri _ -> callback (UriManager.string_of_uri uri)) hashtable;
+       Marshal.to_channel oc hashtable [] ;;
+     let empty () = HT.clear hashtable ;;
+     let restore_from_channel ?(callback = ignore) ic =
+       let restored = Marshal.from_channel ic in
+       empty ();
+       HT.iter
+        (fun k v ->
+          callback (UriManager.string_of_uri k);
+          HT.add hashtable
+            (UriManager.uri_of_string (UriManager.string_of_uri k))
+            (restore_uris v))
+        restored
+     ;;
+
+    end
+   ;;
+   let frozen_list = ref [];;
+   let unchecked_list = ref [];;
 
-(* returns both the annotated and deannotated uncooked forms (plus the *)
-(* map from ids to annotation targets)                                 *)
-let get_annobj_and_type_checking_info uri =
- let filename = Getter.getxml uri in
-  match CicParser.term_of_xml filename uri true with
-     (_, None) -> raise Impossible
-   | (annobj, Some ids_to_targets) ->
-    (* If uri is the uri of an annotation, let's use the annotation file *)
-    if is_annotation_uri uri  then
-(* CSC: la roba annotata non dovrebbe piu' servire
-     AnnotationParser.annotate (Getter.get_ann uri) ids_to_targets ;
-*) assert false ;
+   let find_or_add_unchecked uri ~get_object_to_add =
     try
-      (annobj, ids_to_targets, HashTable.find hashtable (uri,0))
+     List.assq uri !unchecked_list
     with
-     Not_found -> 
-      let obj = Deannotate.deannotate_obj annobj in
-       let output = Unchecked obj in
-        HashTable.add hashtable (uri,0) output ;
-        (annobj, ids_to_targets, output)
-;;
-
+     Not_found ->
+      if List.mem_assq uri !frozen_list then
+       raise (CircularDependency (UriManager.string_of_uri uri))
+      else
+       if CacheOfCookedObjects.mem uri then
+        raise (AlreadyCooked (UriManager.string_of_uri uri))
+       else
+        (* OK, it is not already frozen nor cooked *)
+        let obj = get_object_to_add () in
+         unchecked_list := (uri,obj)::!unchecked_list ;
+         obj
+   ;;
+   let unchecked_to_frozen uri =
+    try
+     let obj = List.assq uri !unchecked_list in
+      unchecked_list := List.remove_assq uri !unchecked_list ;
+      frozen_list := (uri,obj)::!frozen_list
+    with
+     Not_found -> raise (CouldNotFreeze (UriManager.string_of_uri uri))
+   ;;
+   let frozen_to_cooked ~uri =
+    try
+     let obj = List.assq uri !frozen_list in
+      frozen_list := List.remove_assq uri !frozen_list ;
+       CacheOfCookedObjects.add uri obj
+    with
+     Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
+   ;;
+   let find_cooked ~key:uri = CacheOfCookedObjects.find uri;;
+   let add_cooked ~key:uri obj = CacheOfCookedObjects.add uri obj;;
 
-(* get_obj uri                                                                *)
-(* returns the cic object whose uri is uri. If the term is not just in cache, *)
-(* then it is parsed via CicParser.term_of_xml from the file whose name is    *)
-(* the result of Getter.getxml uri                                            *)
-let get_obj uri =
- match get_obj_and_type_checking_info uri 0 with
-    Unchecked obj -> obj
-  | Frozen    obj -> obj
-  | Cooked    obj -> obj
+   let dump_to_channel = CacheOfCookedObjects.dump_to_channel;;
+   let restore_from_channel = CacheOfCookedObjects.restore_from_channel;;
+   let empty = CacheOfCookedObjects.empty;;
+  end
 ;;
 
-(* get_annobj uri                                                             *)
-(* returns the cic object whose uri is uri either in annotated and            *)
-(* deannotated form. The term is put into the cache if it's not there yet.    *)
-let get_annobj uri =
- let (ann, ids_to_targets, deann) = get_annobj_and_type_checking_info uri in
-  let deannobj =
-   match deann with
-      Unchecked obj -> obj
-    | Frozen    _   -> raise (CircularDependency (UriManager.string_of_uri uri))
-    | Cooked    obj -> obj
-  in
-   (ann, ids_to_targets, deannobj)
+let dump_to_channel = Cache.dump_to_channel;;
+let restore_from_channel = Cache.restore_from_channel;;
+let empty = Cache.empty;;
+
+let find_or_add_unchecked_to_cache uri =
+ Cache.find_or_add_unchecked uri
+  ~get_object_to_add:
+   (function () ->
+     let filename = Getter.getxml uri in
+     let bodyfilename =
+      match UriManager.bodyuri_of_uri uri with
+         None -> None
+       | Some bodyuri ->
+          try
+           ignore (Getter.resolve bodyuri) ;
+           (* The body exists ==> it is not an axiom *)
+           Some (Getter.getxml bodyuri)
+          with
+           Getter.Unresolved ->
+            (* The body does not exist ==> we consider it an axiom *)
+            None
+     in
+      let obj = CicParser.obj_of_xml filename bodyfilename in
+       if cleanup_tmp then
+        begin
+         Unix.unlink filename ;
+         match bodyfilename with
+            Some f -> Unix.unlink f
+          | None -> ()
+        end ;
+       obj
+   )
 ;;
 
-(*CSC Commento falso *)
-(* get_obj uri                                                               *)
-(* returns the cooked cic object whose uri is uri. The term must be present  *)
-(* and cooked in cache                                                       *)
-let rec get_cooked_obj uri cookingsno =
- match get_obj_and_type_checking_info uri cookingsno with
-    Unchecked _
-  | Frozen    _ -> raise UncookedObj
-  | Cooked obj -> obj
+(* set_type_checking_info uri                               *)
+(* must be called once the type-checking of uri is finished *)
+(* The object whose uri is uri is unfreezed                 *)
+let set_type_checking_info uri =
+ Cache.frozen_to_cooked uri
 ;;
 
-(* is_type_checked uri                                              *)
+(* is_type_checked uri                                                *)
 (* CSC: commento falso ed obsoleto *)
-(* returns true if the term has been type-checked                   *)
-(* otherwise it returns false and freeze the term for type-checking *)
-(* set_type_checking_info must be called to unfreeze the term       *)
-let is_type_checked uri cookingsno =
- match get_obj_and_type_checking_info uri cookingsno with
-    Cooked obj -> CheckedObj obj
-  | Unchecked obj ->
-     HashTable.remove hashtable (uri,0) ;
-     HashTable.add hashtable (uri,0) (Frozen obj) ;
+(* returns a CheckedObj if the term has been type-checked             *)
+(* otherwise it freezes the term for type-checking and returns
+ it *)
+(* set_type_checking_info must be called to unfreeze the term         *)
+let is_type_checked ?(trust=true) uri =
+ try
+  CheckedObj (Cache.find_cooked uri)
+ with
+  Not_found ->
+   let obj = find_or_add_unchecked_to_cache uri in
+    Cache.unchecked_to_frozen uri ;
+    if trust && trust_obj uri then
+     begin
+      Logger.log (`Trusting uri) ;
+      set_type_checking_info uri ;
+      CheckedObj (Cache.find_cooked uri)
+     end
+    else
      UncheckedObj obj
-  | Frozen _ -> raise (CircularDependency (UriManager.string_of_uri uri))
 ;;
 
-(* set_type_checking_info uri                               *)
-(* must be called once the type-checking of uri is finished *)
-(* The object whose uri is uri is unfreezed                 *)
-let set_type_checking_info uri =
- match HashTable.find hashtable (uri,0) with
-    Frozen obj ->
-     (* let's cook the object at every level *)
-     HashTable.remove hashtable (uri,0) ;
-     let obj' = CicSubstitution.undebrujin_inductive_def uri obj in
-      HashTable.add hashtable (uri,0) (Cooked obj') ;
-      let cooked_objs = !cook_obj obj' uri in
-       let last_cooked_level = ref 0 in
-       let last_cooked_obj = ref obj' in
-        List.iter
-         (fun (n,cobj) ->
-           for i = !last_cooked_level + 1 to n do
-            HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
-           done ;
-           HashTable.add hashtable (uri,n + 1) (Cooked cobj) ;
-           last_cooked_level := n + 1 ;
-           last_cooked_obj := cobj
-         ) cooked_objs ;
-        for i = !last_cooked_level + 1 to UriManager.depth_of_uri uri + 1 do
-         HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
-        done
-  | _ -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
+(* get_cooked_obj ~trust uri *)
+(* returns the object if it is already type-checked or if it can be *)
+(* trusted (if [trust] = true and the trusting function accepts it) *)
+(* Otherwise it raises Not_found                                    *)
+let get_cooked_obj ?(trust=true) uri =
+ try
+  Cache.find_cooked uri
+ with Not_found ->
+  if trust && trust_obj uri then
+   begin
+    match is_type_checked uri with
+       CheckedObj obj -> obj
+     | _ -> assert false
+   end
+  else
+   begin
+    prerr_endline ("@@@ OOOOOOOPS: get_cooked_obj(" ^ UriManager.string_of_uri uri ^ ") raises Not_found since the object is not type-checked nor trusted.") ;
+    raise Not_found
+   end
+;;
+
+(* get_obj uri                                                                *)
+(* returns the cic object whose uri is uri. If the term is not just in cache, *)
+(* then it is parsed via CicParser.term_of_xml from the file whose name is    *)
+(* the result of Getter.getxml uri                                            *)
+let get_obj uri =
+ try
+  get_cooked_obj uri
+ with
+  Not_found ->
+   find_or_add_unchecked_to_cache uri
+;; 
+
+exception OnlyPutOfInductiveDefinitionsIsAllowed
+
+let put_inductive_definition uri obj =
+ match obj with
+    Cic.InductiveDefinition _ -> Cache.add_cooked uri obj
+  | _ -> raise OnlyPutOfInductiveDefinitionsIsAllowed
 ;;