]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicUnifHint.ml
- fixed hint generation, more hints are generated
[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 (*
55   prerr_endline ("INDEXING: " ^ 
56     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
57     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
58 *)
59   DB.index hdb src data
60 ;;
61
62 let empty_db = DB.empty ;;
63
64 let user_db = ref empty_db ;;
65
66 let add_user_provided_hint t =
67   let u = UriManager.uri_of_string "cic:/foo/bar.con" in
68   let c, a, b = 
69     let rec aux ctx = function
70       | NCic.Appl l ->
71           (match List.rev l with
72           | b::a::_ -> ctx, a, b
73           | _ -> assert false)
74       | NCic.Prod (n,s,t) ->
75           aux ((n, NCic.Decl s) :: ctx) t
76       | _ -> assert false
77     in
78       aux [] (fst (OCic2NCic.convert_term u t))
79   in
80   user_db := index_hint !user_db c a b 
81 ;;
82
83 let db () = 
84   let combine f l =
85    List.flatten
86      (let rec aux = function 
87       | u1 :: tl -> List.map (f u1) tl :: aux tl
88       | [] -> []
89      in aux l)
90   in
91   let mk_hint (u1,_,_) (u2,_,_) = 
92     let l = OCic2NCic.convert_obj u1 
93       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
94     let r = OCic2NCic.convert_obj u2 
95       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
96     match List.hd l,List.hd r with
97     | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)), 
98       (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
99         let rec aux ctx t1 t2 =
100           match t1, t2 with
101           | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
102               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
103               then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
104               else []
105           | b1,b2 -> 
106               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2 
107               then begin
108 (*
109                 prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
110                   ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
111                   ~subst:[] ~context:ctx b2);
112 *)
113               let rec mk_rels =  
114                  function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1) 
115               in 
116               let n1 = 
117                 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
118                  u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
119               in
120               let n2 = 
121                 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
122                  u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
123               in
124                 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
125               end else []
126         in
127           aux [] l r
128     | _ -> []
129   in
130   let hints = 
131     List.fold_left 
132       (fun acc (_,_,l) -> 
133           acc @ 
134           if List.length l > 1 then 
135            combine mk_hint l
136           else [])
137       [] (CoercDb.to_list ())
138   in
139   List.fold_left 
140     (fun db -> function 
141      | (ctx,b1,b2) -> index_hint db ctx b1 b2)
142     !user_db (List.flatten hints)
143 ;;
144
145
146 let look_for_hint hdb metasenv subst context t1 t2 =
147 (*
148   prerr_endline ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t1 t2));
149   prerr_endline ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t2 t1));
150   DB.iter hdb
151    (fun p ds ->
152       prerr_endline ("ENTRY: " ^
153       NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
154       String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
155       ~context:[]) (HintSet.elements ds))));
156 *)
157   let candidates1 = DB.retrieve_unifiables hdb (pair t1 t2) in
158   let candidates2 = DB.retrieve_unifiables hdb (pair t2 t1) in
159   let candidates1 = 
160     List.map (fun ty -> 
161        true,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
162     (HintSet.elements candidates1) 
163   in
164   let candidates2 = 
165     List.map (fun ty -> 
166        false,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
167     (HintSet.elements candidates2) 
168   in
169   let rc = 
170   List.map
171    (function 
172     | (true,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t1, t2
173     | (false,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t2, t1
174     | _ -> assert false)
175    (candidates1 @ candidates2)
176   in
177 (*
178     List.iter 
179       (fun (metasenv, t1, t2) ->
180           prerr_endline 
181             ("CAND: "^NCicPp.ppterm ~metasenv ~subst ~context t1 ^
182             " == "^NCicPp.ppterm ~metasenv ~subst ~context t2))
183       rc;
184 *)
185   rc
186 ;;