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