]> matita.cs.unibo.it Git - helm.git/blob - components/library/coercGraph.ml
use the graphviz pretty printer to generate graphviz markup for the coercions graph
[helm.git] / components / 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 (* $Id$ *)
27
28 open Printf;;
29
30 type coercion_search_result = 
31   | SomeCoercion of Cic.term list
32   | NoCoercion
33   | NotMetaClosed
34   | NotHandled of string Lazy.t
35
36 let debug = false
37 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
38
39 (* searches a coercion fron src to tgt in the !coercions list *)
40 let look_for_coercion src tgt =
41   let arity_of con =
42     try
43       let ty,_ = CicTypeChecker.type_of_aux' [] [] con CicUniv.empty_ugraph in
44       let rec count_pi = function
45         | Cic.Prod (_,_,t) -> 1 + count_pi t
46         | _ -> 0
47       in
48       count_pi ty
49     with Invalid_argument _ -> assert false (* all coercions have an uri *)
50   in
51   let rec mk_implicits = function
52     | 0 -> [] 
53     | n -> Cic.Implicit None :: mk_implicits (n-1)
54   in
55   try 
56     let l = 
57       CoercDb.find_coercion 
58         (fun (s,t) -> CoercDb.eq_carr s src && CoercDb.eq_carr t tgt) in
59     let uri =
60      match l with
61      | [] -> 
62          debug_print 
63            (lazy 
64              (sprintf ":-( coercion non trovata da %s a %s" 
65                (CoercDb.name_of_carr src) 
66                (CoercDb.name_of_carr tgt)));
67          None
68      | _::_ -> 
69          debug_print (lazy (
70            sprintf ":-) TROVATE %d coercion(s) da %s a %s" 
71              (List.length l)
72              (CoercDb.name_of_carr src) 
73              (CoercDb.name_of_carr tgt)));
74          Some l
75     in
76      (match uri with
77          None -> NoCoercion
78        | Some ul ->
79           let cl = List.map CicUtil.term_of_uri ul in
80           let arityl = List.map arity_of cl in
81           let argsl = List.map (fun arity -> mk_implicits (arity - 1)) arityl in
82           let newtl =
83             List.map2 
84               (fun args c -> 
85                 match args with
86                 |  [] -> c
87                 | _ -> Cic.Appl (c::args))
88               argsl cl
89           in
90            SomeCoercion newtl)
91   with
92     | CoercDb.EqCarrNotImplemented s -> NotHandled s
93     | CoercDb.EqCarrOnNonMetaClosed -> NotMetaClosed
94 ;;
95
96 let look_for_coercion src tgt = 
97   let src_uri = CoercDb.coerc_carr_of_term src in
98   let tgt_uri = CoercDb.coerc_carr_of_term tgt in
99   look_for_coercion src_uri tgt_uri
100
101 let is_a_coercion t = 
102   try
103     let uri = CicUtil.uri_of_term t in
104     CoercDb.is_a_coercion uri
105   with Invalid_argument _ -> false
106
107 let source_of t = 
108   try
109     let uri = CicUtil.uri_of_term t in
110     CoercDb.term_of_carr (fst (CoercDb.get_carr uri))
111   with Invalid_argument _ -> assert false (* t must be a coercion *)
112   
113 let target_of t =
114   try
115     let uri = CicUtil.uri_of_term t in
116     CoercDb.term_of_carr (snd (CoercDb.get_carr uri))
117   with Invalid_argument _ -> assert false (* t must be a coercion *)
118     
119 let generate_dot_file () =
120   let module Pp = GraphvizPp.Dot in
121   let buf = Buffer.create 10240 in
122   let fmt = Format.formatter_of_buffer buf in
123   Pp.header fmt;
124   Pp.node "node" ~attrs:["fontsize", "9"; "width", ".4"; "height", ".4"] fmt;
125   Pp.node "edge" ~attrs:["fontsize", "10"] fmt;
126   let l = CoercDb.to_list () in
127   let pp_description carr =
128     match CoercDb.uri_of_carr carr with
129     | None -> ()
130     | Some uri ->
131         Pp.node (CoercDb.name_of_carr carr)
132           ~attrs:["href", UriManager.string_of_uri uri] fmt in
133   List.iter
134     (fun (src, tgt, cl) ->
135       let src_name = CoercDb.name_of_carr src in
136       let tgt_name = CoercDb.name_of_carr tgt in
137       pp_description src;
138       pp_description tgt;
139       List.iter
140         (fun c ->
141           Pp.edge src_name tgt_name
142             ~attrs:[ "label", UriManager.name_of_uri c;
143               "href", UriManager.string_of_uri c ]
144             fmt)
145         cl)
146     l;
147   Pp.trailer fmt;
148   Buffer.contents buf
149
150 (* EOF *)