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