]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicUnifHint.ml
c09e08d3c6b506c4f144d929ce8c273975bb79a5
[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 = int * NCic.term = 
15   struct
16         type t = int * 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 precedence =
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         | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
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         | NCic.Def _ -> 
53             NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t)
54      pair' context in
55 (*
56   prerr_endline ("INDEXING: " ^ 
57     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
58     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
59 *)
60   DB.index hdb src (precedence, data)
61 ;;
62
63 let empty_db = DB.empty ;;
64
65 let add_user_provided_hint db t precedence =
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) -> aux ((n, NCic.Decl s) :: ctx) t
73       | NCic.LetIn (n,ty,t,b) -> aux  ((n, NCic.Def (t,ty)) :: ctx) b
74       | _ -> assert false
75     in
76       aux [] t
77   in
78    index_hint db c a b precedence
79 ;;
80
81 let db () = 
82   let combine f l =
83    List.flatten
84      (let rec aux = function 
85       | u1 :: tl -> List.map (f u1) tl :: aux tl
86       | [] -> []
87      in aux l)
88   in
89   let mk_hint (u1,_,_) (u2,_,_) = 
90     let l = OCic2NCic.convert_obj u1 
91       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
92     let r = OCic2NCic.convert_obj u2 
93       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
94     match List.hd l,List.hd r with
95     | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)), 
96       (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
97         let rec aux ctx t1 t2 =
98           match t1, t2 with
99           | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
100               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
101               then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
102               else []
103           | b1,b2 -> 
104               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2 
105               then begin
106 (*
107                 prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
108                   ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
109                   ~subst:[] ~context:ctx b2);
110 *)
111               let rec mk_rels =  
112                  function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1) 
113               in 
114               let n1 = 
115                 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
116                  u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
117               in
118               let n2 = 
119                 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
120                  u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
121               in
122                 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
123               end else []
124         in
125           aux [] l r
126     | _ -> []
127   in
128   let hints = 
129     List.fold_left 
130       (fun acc (_,_,l) -> 
131           acc @ 
132           if List.length l > 1 then 
133            combine mk_hint l
134           else [])
135       [] (CoercDb.to_list (CoercDb.dump ()))
136   in
137   prerr_endline "MISTERO";
138   assert false (* ERA
139   List.fold_left 
140     (fun db -> function 
141      | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
142     !user_db (List.flatten hints)
143 *)
144 ;;
145
146 let saturate ?(delta=0) metasenv subst context ty goal_arity =
147  assert (goal_arity >= 0);
148   let rec aux metasenv = function
149    | NCic.Prod (name,s,t) as ty ->
150        let metasenv1, _, arg,_ = 
151           NCicMetaSubst.mk_meta ~name:name metasenv context (`WithType s) in
152        let t, metasenv1, args, pno = 
153            aux metasenv1 (NCicSubstitution.subst arg t) 
154        in
155        if pno + 1 = goal_arity then
156          ty, metasenv, [], goal_arity+1
157        else
158          t, metasenv1, arg::args, pno+1
159    | ty ->
160         match NCicReduction.whd ~subst context ty ~delta with
161         | NCic.Prod _ as ty -> aux metasenv ty
162         | _ -> ty, metasenv, [], 0 (* differs from the other impl in this line*)
163   in
164   let res, newmetasenv, arguments, _ = aux metasenv ty in
165   res, newmetasenv, arguments
166 ;;
167
168 let look_for_hint hdb metasenv subst context t1 t2 =
169 (*
170   prerr_endline ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t1 t2));
171   prerr_endline ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t2 t1));
172   DB.iter hdb
173    (fun p ds ->
174       prerr_endline ("ENTRY: " ^
175       NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
176       String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
177       ~context:[]) (HintSet.elements ds))));
178 *)
179   let candidates1 = DB.retrieve_unifiables hdb (pair t1 t2) in
180   let candidates2 = DB.retrieve_unifiables hdb (pair t2 t1) in
181   let candidates1 = 
182     List.map (fun (prec,ty) -> 
183        prec,true,saturate ~delta:max_int metasenv subst context ty 0) 
184     (HintSet.elements candidates1) 
185   in
186   let candidates2 = 
187     List.map (fun (prec,ty) -> 
188        prec,false,saturate ~delta:max_int metasenv subst context ty 0) 
189     (HintSet.elements candidates2) 
190   in
191   let rc = 
192     List.map
193       (fun (p,b,(t,m,_)) ->
194          let rec aux () (m,l as acc) = function
195            | NCic.Meta _ as t -> acc, t
196            | NCic.LetIn (name,ty,bo,t) ->
197                let m,_,i,_=
198                  NCicMetaSubst.mk_meta ~name m context (`WithType ty)in
199                let t = NCicSubstitution.subst i t in
200                aux () (m, (i,bo)::l) t
201            | t -> NCicUntrusted.map_term_fold_a (fun _ () -> ()) () aux acc t
202          in
203          let (m,l), t = aux () (m,[]) t in
204          p,b,(t,m,l))
205    (candidates1 @ candidates2)
206   in
207   let rc = 
208   List.map
209    (function 
210     | (prec,true,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t1,t2),l
211     | (prec,false,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t2,t1),l
212     | _ -> assert false)
213     rc
214   in
215   let rc = 
216     List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
217   in 
218   let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
219 (*
220     prerr_endline "Hints:";
221     List.iter 
222       (fun (metasenv, (t1, t2), premises) ->
223           prerr_endline 
224           ("\t" ^ String.concat ";  "
225                 (List.map (fun (a,b) -> 
226                    NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context a ^
227                    " =?= "^
228                    NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context b)
229                 premises) ^     
230               "  ==> "^
231               NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t1 ^
232               " = "^NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t2))
233       rc;
234 *)
235   rc
236 ;;