]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralClassify.ml
d21c14601f5108502d27de3c34f1511e3b89c3ab
[helm.git] / components / acic_procedural / proceduralClassify.ml
1 (* Copyright (C) 2003-2005, 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 module C   = Cic
27 module D   = Deannotate
28 module I   = CicInspect
29 module PEH = ProofEngineHelpers
30
31 type conclusion = (int * int) option
32
33 (* debugging ****************************************************************)
34
35 let string_of_entry inverse =
36    if I.S.mem 0 inverse then "C" else
37    if I.S.is_empty inverse then "I" else "P"
38
39 let to_string (classes, rc) =
40    let linearize = String.concat " " (List.map string_of_entry classes) in
41    match rc with
42       | None        -> linearize
43       | Some (i, j) -> Printf.sprintf "%s %u %u" linearize i j
44
45 let out_table b =
46    let map i (_, inverse) =
47       let map i tl = Printf.sprintf "%2u" i :: tl in 
48       let iset = String.concat " " (I.S.fold map inverse []) in
49       Printf.eprintf "%2u|%s\n" i iset
50    in
51    Array.iteri map b;
52    prerr_newline ()
53    
54 (****************************************************************************)
55
56 let id x = x
57
58 let classify_conclusion = function
59    | _, C.Rel i                -> Some (i, 0)
60    | _, C.Appl (C.Rel i :: tl) -> Some (i, List.length tl)
61    | _                         -> None
62
63 let classify c t =
64 try   
65    let vs, h = PEH.split_with_whd (c, t) in
66    let rc = classify_conclusion (List.hd vs) in
67    let map (b, h) (_, v) = (I.get_rels_from_premise h v, I.S.empty) :: b, succ h in
68    let l, h = List.fold_left map ([], 0) vs in
69    let b = Array.of_list (List.rev l) in
70    let mk_closure b h =
71       let map j = if j < h then I.S.union (fst b.(j)) else id in 
72       for i = pred h downto 0 do
73          let direct, unused = b.(i) in
74          b.(i) <- I.S.fold map direct direct, unused 
75       done; b
76    in
77    let b = mk_closure b h in
78    let rec mk_inverse i direct =
79       if I.S.is_empty direct then () else
80       let j = I.S.choose direct in
81       if j < h then
82          let unused, inverse = b.(j) in 
83          b.(j) <- unused, I.S.add i inverse
84        else ();
85        mk_inverse i (I.S.remove j direct)
86    in
87    let map i (direct, _) = mk_inverse i direct in
88    Array.iteri map b;
89 (*   out_table b; *)
90    List.rev_map snd (List.tl (Array.to_list b)), rc
91 with Invalid_argument _ -> failwith "Classify.classify"