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