]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicCoercion.ml
huge commit regarding the grafite_status:
[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 index_old_db odb db =
41   List.fold_left 
42     (fun db (_,tgt,clist) -> 
43        List.fold_left 
44          (fun db (uri,_,arg) ->
45            try
46             let c=fst (OCic2NCic.convert_term uri (CicUtil.term_of_uri uri)) in
47             let arity = match tgt with | CoercDb.Fun i -> i | _ -> 0 in
48             let src, tgt = 
49               let cty = NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] c in
50               let scty, metasenv,_ = 
51                 NCicMetaSubst.saturate ~delta:max_int [] [] [] cty (arity+1) 
52               in
53               match scty with
54               | NCic.Prod (_, src, tgt) -> 
55                  let tgt =
56                    NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) tgt
57                  in
58 (*
59             prerr_endline (Printf.sprintf "indicizzo %s (%d) : %s ===> %s" 
60               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] scty) (arity+1)
61               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] src)
62               (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] tgt));
63 *)
64                 src, tgt
65               | t -> 
66                   prerr_endline (
67                     NCicPp.ppterm ~metasenv ~subst:[] ~context:[] t);
68                   assert false
69             in
70             index_coercion db c src tgt arity arg
71            with 
72            | NCicEnvironment.BadDependency _ 
73            | NCicTypeChecker.TypeCheckerFailure _ -> db)
74          db clist)
75     db (CoercDb.to_list odb)
76 ;;
77
78 let empty_db = (DB.empty,DB.empty) ;;
79
80
81 let look_for_coercion (db_src,db_tgt) metasenv subst context infty expty =
82   match infty, expty with
83   | (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)), 
84     (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)) -> [] 
85   | _ ->
86 (*
87     prerr_endline ("LOOK FOR COERCIONS: " ^ 
88       NCicPp.ppterm ~metasenv ~subst ~context infty ^ "  |===> " ^
89       NCicPp.ppterm ~metasenv ~subst ~context expty);
90 *)
91     let set_src = DB.retrieve_unifiables db_src infty in
92     let set_tgt = DB.retrieve_unifiables db_tgt expty in
93     let candidates = CoercionSet.inter set_src set_tgt in
94 (*
95     prerr_endline ("CANDIDATES: " ^ 
96       String.concat "," (List.map (fun (t,_,_) ->
97         NCicPp.ppterm ~metasenv ~subst ~context t) 
98       (CoercionSet.elements candidates)));
99 *)
100     List.map
101       (fun (t,arity,arg) ->
102           let ty = NCicTypeChecker.typeof ~metasenv:[] ~subst:[] [] t in
103           let ty, metasenv, args = 
104            NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty arity
105           in
106   (*       prerr_endline (
107             NCicPp.ppterm ~metasenv ~subst:[] ~context:[] ty ^ " --- " ^ 
108             NCicPp.ppterm ~metasenv ~subst ~context
109             (NCicUntrusted.mk_appl t args) ^ " --- " ^ 
110               string_of_int (List.length args) ^ " == " ^ string_of_int arg); *)
111           metasenv, NCicUntrusted.mk_appl t args, ty, List.nth args arg)
112       (CoercionSet.elements candidates)
113 ;;