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