]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/library/coercGraph.ml
b3176d0931daf34b13930b84fe29d80731fc507f
[helm.git] / helm / ocaml / library / coercGraph.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 open Printf;;
27
28 type coercion_search_result = 
29   | SomeCoercion of Cic.term
30   | NoCoercion
31   | NotMetaClosed
32   | NotHandled of string Lazy.t
33
34 let debug = false
35 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
36
37 (* searches a coercion fron src to tgt in the !coercions list *)
38 let look_for_coercion src tgt =
39   try 
40     let l = 
41       CoercDb.find_coercion 
42         (fun (s,t) -> CoercDb.eq_carr s src && CoercDb.eq_carr t tgt) 
43     in
44     match l with
45     | [] -> 
46         debug_print 
47           (lazy 
48             (sprintf ":-( coercion non trovata da %s a %s" 
49               (CoercDb.name_of_carr src) 
50               (CoercDb.name_of_carr tgt)));
51         NoCoercion
52     | [u] -> 
53         debug_print (lazy (
54           sprintf ":-) TROVATA 1 coercion da %s a %s: %s" 
55             (CoercDb.name_of_carr src) 
56             (CoercDb.name_of_carr tgt)
57             (UriManager.name_of_uri u)));
58         SomeCoercion (CicUtil.term_of_uri u)
59     | u::_ -> 
60         debug_print (lazy (
61           sprintf ":-/ TROVATE %d coercion(s) da %s a %s, prendo la prima: %s" 
62             (List.length l)
63             (CoercDb.name_of_carr src) 
64             (CoercDb.name_of_carr tgt)
65             (UriManager.name_of_uri u)));
66         SomeCoercion (CicUtil.term_of_uri u)
67   with
68     | CoercDb.EqCarrNotImplemented s -> NotHandled s
69     | CoercDb.EqCarrOnNonMetaClosed -> NotMetaClosed
70 ;;
71
72 let look_for_coercion src tgt = 
73   let src_uri = CoercDb.coerc_carr_of_term src in
74   let tgt_uri = CoercDb.coerc_carr_of_term tgt in
75   look_for_coercion src_uri tgt_uri
76
77 let is_a_coercion t = 
78   try
79     let uri = CicUtil.uri_of_term t in
80     CoercDb.is_a_coercion uri
81   with Invalid_argument _ -> false
82
83 let source_of t = 
84   try
85     let uri = CicUtil.uri_of_term t in
86     CoercDb.term_of_carr (fst (CoercDb.get_carr uri))
87   with Invalid_argument _ -> assert false (* t must be a coercion *)
88   
89 let target_of t =
90   try
91     let uri = CicUtil.uri_of_term t in
92     CoercDb.term_of_carr (snd (CoercDb.get_carr uri))
93   with Invalid_argument _ -> assert false (* t must be a coercion *)
94     
95 (* EOF *)