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