]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicCoercion.ml
fd697265a2464e4c9255eb6f844fd796b2fe6bac
[helm.git] / helm / software / components / ng_refiner / nCicCoercion.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 let debug s = prerr_endline (Lazy.force s);;
15 let debug _ = ();;
16
17 module COT : Set.OrderedType 
18 with type t = string * NCic.term * int * int  * NCic.term *
19 NCic.term = 
20   struct
21      type t = string * NCic.term * int * int * NCic.term * NCic.term
22      let compare = Pervasives.compare
23   end
24
25 module CoercionSet = Set.Make(COT)
26
27 module DB = 
28   Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(CoercionSet)
29
30 type db = DB.t * DB.t
31
32 let empty_db = DB.empty,DB.empty
33
34 class type g_status =
35  object
36   inherit NCicUnifHint.g_status
37   method coerc_db: db
38  end
39
40 class status =
41  object
42   inherit NCicUnifHint.status
43   val db = empty_db
44   method coerc_db = db
45   method set_coerc_db v = {< db = v >}
46   method set_coercion_status
47    : 'status. #g_status as 'status -> 'self
48    = fun o -> {< db = o#coerc_db >}#set_unifhint_status o
49  end
50
51 let index_coercion status name c src tgt arity arg =
52   let db_src,db_tgt = status#coerc_db in
53   let data = (name,c,arity,arg,src,tgt) in
54   debug (lazy ("INDEX:" ^ 
55     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " ===> " ^
56     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] tgt ^ "  :=  " ^
57     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] c ^ " " ^ 
58     string_of_int arg ^ " " ^ string_of_int arity));
59   let db_src = DB.index db_src src data in
60   let db_tgt = DB.index db_tgt tgt data in
61   status#set_coerc_db (db_src, db_tgt)
62 ;;
63
64 let index_old_db odb (status : #status) =
65   List.fold_left 
66     (fun status (_,tgt,clist) -> 
67        List.fold_left 
68          (fun status (uri,_,arg) ->
69            try
70             let c=fst (OCic2NCic.convert_term uri (CicUtil.term_of_uri uri)) in
71             let arity = match tgt with | CoercDb.Fun i -> i | _ -> 0 in
72             let src, tgt = 
73               let cty = NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] c in
74               let scty, metasenv,_ = 
75                 NCicMetaSubst.saturate ~delta:max_int [] [] [] cty (arity+1) 
76               in
77               match scty with
78               | NCic.Prod (_, src, tgt) -> 
79                  let tgt =
80                    NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) tgt
81                  in
82 (*
83             debug (lazy (Printf.sprintf "indicizzo %s (%d)) : %s ===> %s" 
84               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] scty) (arity+1)
85               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] src)
86               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] tgt));
87 *)
88                 src, tgt
89               | t -> 
90                   debug (lazy (
91                     NCicPp.ppterm ~metasenv ~subst:[] ~context:[] t));
92                   assert false
93             in
94             index_coercion status "foo" c src tgt arity arg
95            with 
96            | NCicEnvironment.BadDependency _ 
97            | NCicTypeChecker.TypeCheckerFailure _ -> status)
98          status clist)
99     status (CoercDb.to_list odb)
100 ;;
101
102 let look_for_coercion status metasenv subst context infty expty =
103  let db_src,db_tgt = status#coerc_db in
104   match 
105     NCicUntrusted.apply_subst subst context infty, 
106     NCicUntrusted.apply_subst subst context expty 
107   with
108   | (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)), 
109     (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)) -> [] 
110   | infty, expty ->
111
112     debug (lazy ("LOOK FOR COERCIONS: " ^ 
113       NCicPp.ppterm ~metasenv ~subst ~context infty ^ "  |===> " ^
114       NCicPp.ppterm ~metasenv ~subst ~context expty));
115
116     let src_class = infty :: NCicUnifHint.eq_class_of status infty in
117     let tgt_class = expty :: NCicUnifHint.eq_class_of status expty in
118
119     let set_src = 
120       List.fold_left 
121         (fun set infty -> 
122           CoercionSet.union (DB.retrieve_unifiables db_src infty) set)
123         CoercionSet.empty src_class
124     in
125     let set_tgt = 
126       List.fold_left 
127         (fun set expty -> 
128           CoercionSet.union (DB.retrieve_unifiables db_tgt expty) set)
129         CoercionSet.empty tgt_class
130     in
131
132     debug (lazy ("CANDIDATES SRC: " ^ 
133       String.concat "," (List.map (fun (name,t,_,_,_,_) ->
134         name ^ " :: " ^ NCicPp.ppterm ~metasenv ~subst ~context t) 
135       (CoercionSet.elements set_src))));
136     debug (lazy ("CANDIDATES TGT: " ^ 
137       String.concat "," (List.map (fun (name,t,_,_,_,_) ->
138         name ^ " :: " ^ NCicPp.ppterm ~metasenv ~subst ~context t) 
139       (CoercionSet.elements set_tgt))));
140
141     let candidates = CoercionSet.inter set_src set_tgt in
142
143     debug (lazy ("CANDIDATES: " ^ 
144       String.concat "," (List.map (fun (name,t,_,_,_,_) ->
145         name ^ " :: " ^ NCicPp.ppterm ~metasenv ~subst ~context t) 
146       (CoercionSet.elements candidates))));
147
148     List.map
149       (fun (name,t,arity,arg,_,_) ->
150           let ty =
151             try NCicTypeChecker.typeof ~metasenv:[] ~subst:[] [] t 
152             with NCicTypeChecker.TypeCheckerFailure s ->
153              prerr_endline ("illtyped coercion: "^Lazy.force s);
154              prerr_endline (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
155              assert false
156           in
157           let ty, metasenv, args = 
158            NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty arity
159           in
160
161           debug (lazy (
162             NCicPp.ppterm ~metasenv ~subst:[] ~context:[] ty ^ " --- " ^ 
163             NCicPp.ppterm ~metasenv ~subst ~context
164             (NCicUntrusted.mk_appl t args) ^ " --- " ^ 
165               string_of_int (List.length args) ^ " == " ^ string_of_int arg)); 
166              
167           name,metasenv, NCicUntrusted.mk_appl t args, ty, List.nth args arg)
168       (CoercionSet.elements candidates)
169 ;;
170
171 (* CSC: very inefficient implementation!
172    Enrico, can we use a discrimination tree here? *)
173 let match_coercion status ~metasenv ~subst ~context t =
174  let db =
175   DB.fold (fst (status#coerc_db)) (fun _ v l -> (CoercionSet.elements v)@l) []
176  in
177     (HExtlib.list_findopt
178       (fun (_,p,arity,cpos,_,_) _ ->
179         try
180          let t =
181           match p,t with
182              NCic.Appl lp, NCic.Appl lt ->
183               (match fst (HExtlib.split_nth (List.length lp) lt) with
184                   [t] -> t
185                 | l -> NCic.Appl l)
186            | _,NCic.Appl (he::_) -> he
187            | _,_ -> t
188          in
189          let b = NCicReduction.alpha_eq metasenv subst context p t in
190          if not b then None else
191          let ty = NCicTypeChecker.typeof ~metasenv:[] ~subst:[] [] p in
192          let pis = 
193            let rec aux = function NCic.Prod (_,_,t) -> 1+aux t | _ -> 0 in
194            aux ty
195          in
196          Some (p,pis - arity - cpos - 1,cpos)
197         with
198          Failure _ -> None (* raised by split_nth *)
199       ) db)
200 ;;
201
202 let generate_dot_file status fmt =
203   let module Pp = GraphvizPp.Dot in
204   let src_db, _ = status#coerc_db in
205   let edges = ref [] in
206   DB.iter src_db (fun _ dataset -> 
207      edges := !edges @ 
208       List.map
209         (fun (name,t,a,g,sk,dk) -> 
210           debug(lazy (let p = NCicPp.ppterm ~metasenv:[] ~context:[]
211                 ~subst:[] in p t ^ " ::: " ^ p sk ^ " |--> " ^ p dk));
212           let eq_s= sk::NCicUnifHint.eq_class_of status sk in
213           let eq_t= dk::NCicUnifHint.eq_class_of status dk in
214           (name,t,a,g),eq_s,eq_t
215           )
216         (CoercionSet.elements dataset);
217     );
218   let nodes = 
219     HExtlib.list_uniq
220      (List.sort compare 
221        (List.flatten
222          (List.map
223            (fun (_,a,b) ->
224              [a;b]
225             )
226            !edges)))
227   in
228   let names = ref [] in
229   let id = ref 0 in
230   let mangle l =
231     try List.assoc l !names
232     with Not_found ->
233       incr id;
234       names := (l,"node"^string_of_int!id) :: !names;
235       List.assoc l !names
236   in
237   List.iter 
238     (fun cl -> 
239       Pp.node (mangle cl) 
240       ~attrs:["label",String.concat "\\n"
241         (List.map (fun t->
242           NCicPp.ppterm ~metasenv:[] ~subst:[]
243            ~context:[] t ~margin:max_int
244         ) cl)]
245       fmt)
246     nodes;
247   List.iter 
248     (fun ((name,_,_,_),src,tgt) ->
249        Pp.edge (mangle src) (mangle tgt)
250        ~attrs:["label", name] fmt)
251     !edges;
252 ;;