]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/cic_disambiguation/disambiguate.ml
disambiguation now returns and takes in input the substitution
[helm.git] / helm / software / components / cic_disambiguation / disambiguate.ml
index 248baa195e8cc5c2a958d20d1293cd921ae8cb3c..45186669d09d482f7a55ebf5da9a73ed7ac1b0b8 100644 (file)
@@ -103,19 +103,19 @@ let descr_of_domain_item = function
  | Symbol (s, _) -> s
  | Num i -> string_of_int i
 
-type ('a,'m) test_result =
-  | Ok of 'a * 'm
+type ('term,'metasenv,'subst,'graph) test_result =
+  | Ok of 'term * 'metasenv * 'subst * 'graph
   | Ko of Stdpp.location option * string Lazy.t
   | Uncertain of Stdpp.location option * string Lazy.t
 
-let refine_term metasenv context uri term ugraph ~localization_tbl =
+let refine_term metasenv subst context uri term ugraph ~localization_tbl =
 (*   if benchmark then incr actual_refinements; *)
   assert (uri=None);
     debug_print (lazy (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppterm term)));
     try
       let term', _, metasenv',ugraph1 = 
        CicRefine.type_of_aux' metasenv context term ugraph ~localization_tbl in
-       (Ok (term', metasenv')),ugraph1
+      (Ok (term', metasenv',[],ugraph1))
     with
      exn ->
       let rec process_exn loc =
@@ -123,35 +123,38 @@ let refine_term metasenv context uri term ugraph ~localization_tbl =
           HExtlib.Localized (loc,exn) -> process_exn (Some loc) exn
         | CicRefine.Uncertain msg ->
             debug_print (lazy ("UNCERTAIN!!! [" ^ (Lazy.force msg) ^ "] " ^ CicPp.ppterm term)) ;
-            Uncertain (loc,msg),ugraph
+            Uncertain (loc,msg)
         | CicRefine.RefineFailure msg ->
             debug_print (lazy (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
               (CicPp.ppterm term) (Lazy.force msg)));
-            Ko (loc,msg),ugraph
+            Ko (loc,msg)
        | exn -> raise exn
       in
        process_exn None exn
 
-let refine_obj metasenv context uri obj ugraph ~localization_tbl =
- assert (context = []);
+let refine_obj metasenv subst context uri obj ugraph ~localization_tbl =
+   assert (context = []);
+   assert (metasenv = []);
+   assert (subst = []);
    debug_print (lazy (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppobj obj))) ;
    try
      let obj', metasenv,ugraph =
-      CicRefine.typecheck metasenv uri obj ~localization_tbl
+       CicRefine.typecheck metasenv uri obj ~localization_tbl
      in
-       (Ok (obj', metasenv)),ugraph
+     (Ok (obj', metasenv,[],ugraph))
    with
      exn ->
       let rec process_exn loc =
        function
           HExtlib.Localized (loc,exn) -> process_exn (Some loc) exn
         | CicRefine.Uncertain msg ->
-            debug_print (lazy ("UNCERTAIN!!! [" ^ (Lazy.force msg) ^ "] " ^ CicPp.ppobj obj)) ;
-            Uncertain (loc,msg),ugraph
+            debug_print (lazy ("UNCERTAIN!!! [" ^ 
+              (Lazy.force msg) ^ "] " ^ CicPp.ppobj obj)) ;
+            Uncertain (loc,msg)
         | CicRefine.RefineFailure msg ->
             debug_print (lazy (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
               (CicPp.ppobj obj) (Lazy.force msg))) ;
-            Ko (loc,msg),ugraph
+            Ko (loc,msg)
        | exn -> raise exn
       in
        process_exn None exn
@@ -934,10 +937,11 @@ sig
     dbd:HSql.dbd ->
     context:'context ->
     metasenv:'metasenv ->
+    subst:'subst ->
     initial_ugraph:'ugraph ->
     hint: ('metasenv -> 'raw_thing -> 'raw_thing) * 
-          (('refined_thing,'metasenv) test_result -> 'ugraph ->
-              ('refined_thing,'metasenv) test_result * 'ugraph) ->
+          (('refined_thing,'metasenv,'subst,'ugraph) test_result ->
+              ('refined_thing,'metasenv,'subst,'ugraph) test_result) ->
     aliases:DisambiguateTypes.codomain_item DisambiguateTypes.Environment.t ->
     universe:DisambiguateTypes.codomain_item list
              DisambiguateTypes.Environment.t option ->
@@ -950,28 +954,32 @@ sig
                         uri:'uri ->
                         is_path:bool -> 'ast_thing -> localization_tbl:'cichash -> 'raw_thing) ->
     refine_thing:('metasenv ->
+                  'subst ->
                   'context ->
                   'uri ->
                   'raw_thing ->
-                  'ugraph -> localization_tbl:'cichash -> ('refined_thing,
-                  'metasenv) test_result * 'ugraph) ->
+                  'ugraph -> localization_tbl:'cichash -> 
+                  ('refined_thing, 'metasenv,'subst,'ugraph) test_result) ->
     localization_tbl:'cichash ->
     string * int * 'ast_thing ->
     ((DisambiguateTypes.Environment.key * DisambiguateTypes.codomain_item)
-     list * 'metasenv * 'refined_thing * 'ugraph)
+     list * 'metasenv * 'subst * 'refined_thing * 'ugraph)
     list * bool
 
   val disambiguate_term :
     ?fresh_instances:bool ->
     dbd:HSql.dbd ->
     context:Cic.context ->
-    metasenv:Cic.metasenv -> ?goal:int ->
+    metasenv:Cic.metasenv -> 
+    subst:Cic.substitution ->
+    ?goal:int ->
     ?initial_ugraph:CicUniv.universe_graph -> 
     aliases:DisambiguateTypes.environment ->(* previous interpretation status *)
     universe:DisambiguateTypes.multiple_environment option ->
     CicNotationPt.term disambiguator_input ->
     ((DisambiguateTypes.domain_item * DisambiguateTypes.codomain_item) list *
      Cic.metasenv *                  (* new metasenv *)
+     Cic.substitution *
      Cic.term*
      CicUniv.universe_graph) list *  (* disambiguated term *)
     bool
@@ -985,6 +993,7 @@ sig
     CicNotationPt.term CicNotationPt.obj disambiguator_input ->
     ((DisambiguateTypes.domain_item * DisambiguateTypes.codomain_item) list *
      Cic.metasenv *                  (* new metasenv *)
+     Cic.substitution *
      Cic.obj *
      CicUniv.universe_graph) list *  (* disambiguated obj *)
     bool
@@ -1028,8 +1037,8 @@ module Make (C: Callbacks) =
 
 let refine_profiler = HExtlib.profile "disambiguate_thing.refine_thing"
 
-    let disambiguate_thing ~dbd ~context ~metasenv
-      ~initial_ugraph ~hint
+    let disambiguate_thing ~dbd ~context ~metasenv ~subst
+      ~initial_ugraph:base_univ ~hint
       ~aliases ~universe
       ~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing 
       ~localization_tbl
@@ -1129,31 +1138,32 @@ let refine_profiler = HExtlib.profile "disambiguate_thing.refine_thing"
           in
           let cic_thing = (fst hint) metasenv cic_thing in
 let foo () =
-          let k,ugraph1 =
-           refine_thing metasenv context uri cic_thing ugraph ~localization_tbl
+          let k =
+           refine_thing metasenv subst 
+             context uri cic_thing ugraph ~localization_tbl
           in
-          let k, ugraph1 = (snd hint) k ugraph1 in
-            (k , ugraph1 )
+          let k = (snd hint) k in
+            k
 in refine_profiler.HExtlib.profile foo ()
         with
-        | Try_again msg -> Uncertain (None,msg), ugraph
-        | Invalid_choice (loc,msg) -> Ko (loc,msg), ugraph
+        | Try_again msg -> Uncertain (None,msg)
+        | Invalid_choice (loc,msg) -> Ko (loc,msg)
       in
       (* (4) build all possible interpretations *)
       let (@@) (l1,l2,l3) (l1',l2',l3') = l1@l1', l2@l2', l3@l3' in
       (* aux returns triples Ok/Uncertain/Ko *)
       (* rem_dom is the concatenation of all the remainin domains *)
-      let rec aux aliases diff lookup_in_todo_dom todo_dom rem_dom base_univ =
+      let rec aux aliases diff lookup_in_todo_dom todo_dom rem_dom =
         debug_print (lazy ("ZZZ: " ^ string_of_domain todo_dom));
         match todo_dom with
         | [] ->
             assert (lookup_in_todo_dom = None);
             (match test_env aliases rem_dom base_univ with
-            | Ok (thing, metasenv),new_univ -> 
-               [ aliases, diff, metasenv, thing, new_univ ], [], []
-            | Ko (loc,msg),_ -> [],[],[aliases,diff,loc,msg,true]
-            | Uncertain (loc,msg),new_univ ->
-               [],[aliases,diff,loc,msg,new_univ],[])
+            | Ok (thing, metasenv,subst,new_univ) -> 
+               [ aliases, diff, metasenv, subst, thing, new_univ ], [], []
+            | Ko (loc,msg) -> [],[],[aliases,diff,loc,msg,true]
+            | Uncertain (loc,msg) ->
+               [],[aliases,diff,loc,msg],[])
         | Node (locs,item,inner_dom) :: remaining_dom ->
             debug_print (lazy (sprintf "CHOOSED ITEM: %s"
              (string_of_domain_item item)));
@@ -1213,68 +1223,69 @@ in refine_profiler.HExtlib.profile foo ()
                    ok_l,uncertain_l,mark_not_significant error_l
                   else
                    outcome in
-               let rec filter univ = function 
+               let rec filter = function 
                 | [] -> [],[],[]
                 | codomain_item :: tl ->
                     debug_print(lazy (sprintf "%s CHOSEN" (fst codomain_item)));
                     let new_env = Environment.add item codomain_item aliases in
                     let new_diff = (item,codomain_item)::diff in
                     (match
-                      test_env new_env (inner_dom@remaining_dom@rem_dom) univ
+                      test_env new_env 
+                        (inner_dom@remaining_dom@rem_dom) base_univ
                      with
-                    | Ok (thing, metasenv),new_univ ->
+                    | Ok (thing, metasenv,subst,new_univ) ->
                         let res = 
                           (match inner_dom with
                           | [] ->
-                             [new_env,new_diff,metasenv,thing,new_univ], [], []
+                             [new_env,new_diff,metasenv,subst,thing,new_univ],
+                             [], []
                           | _ ->
                              aux new_env new_diff None inner_dom
-                              (remaining_dom@rem_dom) new_univ
+                              (remaining_dom@rem_dom) 
                           ) 
                         in
-                         res @@ filter univ tl
-                    | Uncertain (loc,msg),new_univ ->
+                         res @@ filter tl
+                    | Uncertain (loc,msg) ->
                         let res =
                           (match inner_dom with
-                          | [] -> [],[new_env,new_diff,loc,msg,new_univ],[]
+                          | [] -> [],[new_env,new_diff,loc,msg],[]
                           | _ ->
                              aux new_env new_diff None inner_dom
-                              (remaining_dom@rem_dom) new_univ
+                              (remaining_dom@rem_dom) 
                           )
                         in
-                         res @@ filter univ tl
-                    | Ko (loc,msg),_ ->
+                         res @@ filter tl
+                    | Ko (loc,msg) ->
                         let res = [],[],[new_env,new_diff,loc,msg,true] in
-                         res @@ filter univ tl)
+                         res @@ filter tl)
                in
                 let ok_l,uncertain_l,error_l =
-                 classify_errors (filter base_univ choices)
+                 classify_errors (filter choices)
                 in
                  let res_after_ok_l =
                   List.fold_right
-                   (fun (env,diff,_,_,univ) res ->
-                     aux env diff None remaining_dom rem_dom univ @@ res
+                   (fun (env,diff,_,_,_,_) res ->
+                     aux env diff None remaining_dom rem_dom @@ res
                    ) ok_l ([],[],error_l)
                 in
                  List.fold_right
-                  (fun (env,diff,_,_,univ) res ->
-                    aux env diff None remaining_dom rem_dom univ @@ res
+                  (fun (env,diff,_,_) res ->
+                    aux env diff None remaining_dom rem_dom @@ res
                   ) uncertain_l res_after_ok_l
       in
-      let aux' aliases diff lookup_in_todo_dom todo_dom base_univ =
+      let aux' aliases diff lookup_in_todo_dom todo_dom =
        match test_env aliases todo_dom base_univ with
-        | Ok _,_
-        | Uncertain _,_ ->
-           aux aliases diff lookup_in_todo_dom todo_dom [] base_univ
-        | Ko (loc,msg),_ -> [],[],[aliases,diff,loc,msg,true] in
-      let base_univ = initial_ugraph in
+        | Ok _
+        | Uncertain _ ->
+           aux aliases diff lookup_in_todo_dom todo_dom [] 
+        | Ko (loc,msg) -> [],[],[aliases,diff,loc,msg,true] in
       try
         let res =
-         match aux' aliases [] None todo_dom base_univ with
+         match aux' aliases [] None todo_dom with
          | [],uncertain,errors ->
             let errors =
              List.map
-              (fun (env,diff,loc,msg,_) -> (env,diff,loc,msg,true)
+              (fun (env,diff,loc,msg) -> (env,diff,loc,msg,true)
               ) uncertain @ errors
             in
             let errors =
@@ -1296,15 +1307,15 @@ in refine_profiler.HExtlib.profile foo ()
               ) errors
             in
              raise (NoWellTypedInterpretation (0,errors))
-         | [_,diff,metasenv,t,ugraph],_,_ ->
+         | [_,diff,metasenv,subst,t,ugraph],_,_ ->
              debug_print (lazy "SINGLE INTERPRETATION");
-             [diff,metasenv,t,ugraph], false
+             [diff,metasenv,subst,t,ugraph], false
          | l,_,_ ->
              debug_print 
                (lazy (sprintf "MANY INTERPRETATIONS (%d)" (List.length l)));
              let choices =
                List.map
-                 (fun (env, _, _, _, _) ->
+                 (fun (env, _, _, _, _, _) ->
                    map_domain
                      (fun locs domain_item ->
                        let description =
@@ -1318,7 +1329,8 @@ in refine_profiler.HExtlib.profile foo ()
                C.interactive_interpretation_choice 
                  thing_txt thing_txt_prefix_len choices 
              in
-             (List.map (fun n->let _,d,m,t,u= List.nth l n in d,m,t,u) choosed),
+             (List.map (fun n->let _,d,m,s,t,u= List.nth l n in d,m,s,t,u)
+               choosed),
               true
         in
          res
@@ -1327,7 +1339,7 @@ in refine_profiler.HExtlib.profile foo ()
         failwith "Disambiguate: circular dependency"
 
     let disambiguate_term ?(fresh_instances=false) ~dbd ~context ~metasenv 
-      ?goal
+      ~subst ?goal
       ?(initial_ugraph = CicUniv.oblivion_ugraph) ~aliases ~universe 
       (text,prefix_len,term)
     =
@@ -1335,21 +1347,22 @@ in refine_profiler.HExtlib.profile foo ()
         if fresh_instances then CicNotationUtil.freshen_term term else term
       in
       let hint = match goal with
-        | None -> (fun _ x -> x), fun k u -> k, u
+        | None -> (fun _ x -> x), fun k -> k
         | Some i ->
             (fun metasenv t ->
               let _,c,ty = CicUtil.lookup_meta i metasenv in
               assert(c=context);
               Cic.Cast(t,ty)),
             function  
-            | Ok (t,m) -> fun ug ->
+            | Ok (t,m,s,ug) ->
                 (match t with
-                | Cic.Cast(t,_) -> Ok (t,m), ug
+                | Cic.Cast(t,_) -> Ok (t,m,s,ug)
                 | _ -> assert false) 
-            | k -> fun ug -> k, ug
+            | k -> k
       in
       let localization_tbl = Cic.CicHash.create 503 in
-      disambiguate_thing ~dbd ~context ~metasenv ~initial_ugraph ~aliases
+       disambiguate_thing ~dbd ~context ~metasenv ~subst
+        ~initial_ugraph ~aliases
         ~universe ~uri:None ~pp_thing:CicNotationPp.pp_term
         ~domain_of_thing:domain_of_term
         ~interpretate_thing:(interpretate_term (?create_dummy_ids:None))
@@ -1365,10 +1378,10 @@ in refine_profiler.HExtlib.profile foo ()
       in
       let hint = 
         (fun _ x -> x),
-        fun k u -> k, u
+        fun k -> k
       in
       let localization_tbl = Cic.CicHash.create 503 in
-      disambiguate_thing ~dbd ~context:[] ~metasenv:[] 
+      disambiguate_thing ~dbd ~context:[] ~metasenv:[] ~subst:[] 
         ~aliases ~universe ~uri
         ~pp_thing:(CicNotationPp.pp_obj CicNotationPp.pp_term) ~domain_of_thing:domain_of_obj
         ~initial_ugraph:CicUniv.empty_ugraph