]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicUnifHint.ml
fix convertibility in case of application test_eq_only is =true for
[helm.git] / helm / software / components / ng_refiner / nCicUnifHint.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: nCicRefiner.mli 9227 2008-11-21 16:00:06Z tassi $ *)
13
14 module COT : Set.OrderedType with type t = NCic.term = 
15   struct
16         type t = NCic.term
17         let compare = Pervasives.compare
18   end
19
20 module HintSet = Set.Make(COT)
21
22 module DB = 
23   Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
24
25 type db = DB.t
26
27 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
28 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
29
30 let index_hint hdb context t1 t2 =
31   assert (
32     (match t1 with
33     | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
34     &&
35     (match t2 with
36     | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
37   );
38   let pair' = pair t1 t2 in
39   let data = 
40     List.fold_left 
41      (fun t (n,e) -> 
42         match e with
43         | NCic.Decl ty -> NCic.Prod (n,ty,t)
44         | _ -> assert false) 
45       pair' context in
46   let src = 
47     List.fold_left 
48      (fun t (_,e) -> 
49         match e with
50         | NCic.Decl _ -> 
51             NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t
52         | _ -> assert false) 
53      pair' context in
54   prerr_endline ("INDEXING: " ^ 
55     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
56     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
57   DB.index hdb src data
58 ;;
59
60 let empty_db = DB.empty ;;
61
62 let user_db = ref empty_db ;;
63
64 let add_user_provided_hint t =
65   let u = UriManager.uri_of_string "cic:/foo/bar.con" in
66   let c, a, b = 
67     let rec aux ctx = function
68       | NCic.Appl l ->
69           (match List.rev l with
70           | b::a::_ -> ctx, a, b
71           | _ -> assert false)
72       | NCic.Prod (n,s,t) ->
73           aux ((n, NCic.Decl s) :: ctx) t
74       | _ -> assert false
75     in
76       aux [] (fst (OCic2NCic.convert_term u t))
77   in
78   user_db := index_hint !user_db c a b 
79 ;;
80
81 let db () = 
82   let combine f cmp l1 l2 =
83    List.flatten
84      (List.map
85        (fun u1 -> 
86           HExtlib.filter_map 
87             (fun u2 -> if cmp u1 u2 then None else Some (f u1 u2)) l2)
88        l1)
89   in
90   let mk_hint (u1,_,_) (u2,_,_) = 
91     let l = OCic2NCic.convert_obj u1 
92       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
93     let r = OCic2NCic.convert_obj u2 
94       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
95     match List.hd l,List.hd r with
96     | (_,_,_,_,NCic.Constant (_,_,Some l,_,_)), 
97       (_,_,_,_,NCic.Constant (_,_,Some r,_,_)) ->
98         let rec aux ctx t1 t2 =
99           match t1, t2 with
100           | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
101               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
102               then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
103               else None
104           | b1,b2 -> 
105               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2 
106               then begin
107 (*
108                 prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
109                   ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
110                   ~subst:[] ~context:ctx b2);
111 *)
112                 Some (ctx,b1,b2)
113               end else None
114         in
115           aux [] l r
116     | _ -> None
117   in
118   let hints = 
119     List.fold_left 
120       (fun acc (_,_,l) -> 
121           acc @ 
122           if List.length l > 1 then 
123            combine mk_hint (fun (u1,_,_) (u2,_,_) -> UriManager.eq u1 u2) l l
124           else [])
125       [] (CoercDb.to_list ())
126   in
127   List.fold_left 
128     (fun db -> function 
129      | None -> db 
130      | Some (ctx,b1,b2) -> index_hint db ctx b1 b2)
131     !user_db hints
132 ;;
133
134
135 let look_for_hint hdb metasenv subst context t1 t2 =
136 (*
137   prerr_endline ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t1 t2));
138   prerr_endline ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t2 t1));
139   DB.iter hdb
140    (fun p ds ->
141       prerr_endline ("ENTRY: " ^
142       NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
143       String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
144       ~context:[]) (HintSet.elements ds))));
145 *)
146   let candidates1 = DB.retrieve_unifiables hdb (pair t1 t2) in
147   let candidates2 = DB.retrieve_unifiables hdb (pair t2 t1) in
148   let candidates1 = 
149     List.map (fun ty -> 
150        true,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
151     (HintSet.elements candidates1) 
152   in
153   let candidates2 = 
154     List.map (fun ty -> 
155        false,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
156     (HintSet.elements candidates2) 
157   in
158   List.map
159    (function 
160     | (true,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t1, t2
161     | (false,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t2, t1
162     | _ -> assert false)
163    (candidates1 @ candidates2)
164 ;;