]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicCoercion.ml
added empty_db, usefull to avoid translating all old coercions to obtain a new coerci...
[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 module COT : Set.OrderedType with type t = NCic.term * int * int = 
15   struct
16         type t = NCic.term * int * int
17         let compare = Pervasives.compare
18   end
19
20 module CoercionSet = Set.Make(COT)
21
22 module DB = 
23   Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(CoercionSet)
24
25 type db = DB.t * DB.t
26
27 let index_coercion (db_src,db_tgt) c src tgt arity arg =
28   let data = (c,arity,arg) in
29 (*
30   prerr_endline ("INDEX:" ^ 
31     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " ===> " ^
32     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] tgt ^ "  :=  " ^
33     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] c);
34 *)
35   let db_src = DB.index db_src src data in
36   let db_tgt = DB.index db_tgt tgt data in
37   db_src, db_tgt
38 ;;
39
40 let db () =
41   List.fold_left 
42     (fun db (_,tgt,clist) -> 
43        List.fold_left 
44          (fun db (uri,_,arg) ->
45             let c=fst (OCic2NCic.convert_term uri (CicUtil.term_of_uri uri)) in
46             let arity = match tgt with | CoercDb.Fun i -> i | _ -> 0 in
47             let src, tgt = 
48               let cty = NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] c in
49               let scty, metasenv,_ = 
50                 NCicMetaSubst.saturate ~delta:max_int [] [] [] cty (arity+1) 
51               in
52               match scty with
53               | NCic.Prod (_, src, tgt) -> 
54                  let tgt =
55                    NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) tgt
56                  in
57 (*
58             prerr_endline (Printf.sprintf "indicizzo %s (%d) : %s ===> %s" 
59               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] scty) (arity+1)
60               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] src)
61               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] tgt));
62 *)
63                 src, tgt
64               | t -> 
65                   prerr_endline (
66                     NCicPp.ppterm ~metasenv ~subst:[] ~context:[] t);
67                   assert false
68             in
69             index_coercion db c src tgt arity arg)
70          db clist)
71     (DB.empty,DB.empty) (CoercDb.to_list ())
72 ;;
73
74 let empty_db = (DB.empty,DB.empty) ;;
75
76
77 let look_for_coercion (db_src,db_tgt) metasenv subst context infty expty =
78   match infty, expty with
79   | (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)), 
80     (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)) -> [] 
81   | _ ->
82 (*
83     prerr_endline ("LOOK FOR COERCIONS: " ^ 
84       NCicPp.ppterm ~metasenv ~subst ~context infty ^ "  |===> " ^
85       NCicPp.ppterm ~metasenv ~subst ~context expty);
86 *)
87     let set_src = DB.retrieve_unifiables db_src infty in
88     let set_tgt = DB.retrieve_unifiables db_tgt expty in
89     let candidates = CoercionSet.inter set_src set_tgt in
90 (*
91     prerr_endline ("CANDIDATES: " ^ 
92       String.concat "," (List.map (fun (t,_,_) ->
93         NCicPp.ppterm ~metasenv ~subst ~context t) 
94       (CoercionSet.elements candidates)));
95 *)
96     List.map
97       (fun (t,arity,arg) ->
98           let ty = NCicTypeChecker.typeof ~metasenv:[] ~subst:[] [] t in
99           let ty, metasenv, args = 
100            NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty arity
101           in
102   (*       prerr_endline (
103             NCicPp.ppterm ~metasenv ~subst:[] ~context:[] ty ^ " --- " ^ 
104             NCicPp.ppterm ~metasenv ~subst ~context
105             (NCicUntrusted.mk_appl t args) ^ " --- " ^ 
106               string_of_int (List.length args) ^ " == " ^ string_of_int arg); *)
107           metasenv, NCicUntrusted.mk_appl t args, ty, List.nth args arg)
108       (CoercionSet.elements candidates)
109 ;;