]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralClassify.ml
6c1e482c2ff4d86aee5ee01cb988e50ece825630
[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 dependence = I.S.t * bool
32
33 type conclusion = (int * int) option
34
35 (* debugging ****************************************************************)
36
37 let string_of_entry (inverse, b) =
38    if I.S.mem 0 inverse then begin if b then "CF" else "C" end else
39    if I.S.is_empty inverse then "I" else "P"
40
41 let to_string (classes, rc) =
42    let linearize = String.concat " " (List.map string_of_entry classes) in
43    match rc with
44       | None        -> linearize
45       | Some (i, j) -> Printf.sprintf "%s %u %u" linearize i j
46
47 let out_table b =
48    let map i (_, inverse) =
49       let map i tl = Printf.sprintf "%2u" i :: tl in 
50       let iset = String.concat " " (I.S.fold map inverse []) in
51       Printf.eprintf "%2u|%s\n" i iset
52    in
53    Array.iteri map b;
54    prerr_newline ()
55    
56 (****************************************************************************)
57
58 let identity x = x
59
60 let fst3 (x, _, _) = x
61
62 let classify_conclusion vs = 
63    let rec get_argsno = function
64       | c, C.Appl (t :: vs) -> 
65          let hd, argsno = get_argsno (c, t) in 
66          hd, argsno + List.length vs
67       | _, t                -> t, 0
68    in
69    let inside i = i > 1 && i <= List.length vs in
70    match vs with
71       | v0 :: v1 :: _ ->
72          let hd0, argsno0 = get_argsno v0 in
73          let hd1, argsno1 = get_argsno v1 in
74          begin match hd0, hd1 with
75             | C.Rel i, C.MutInd _ when inside i -> Some (i, argsno0)
76             | _                                 -> None
77          end
78       | _             -> None
79  
80 let classify c t =
81 try   
82    let vs, h = PEH.split_with_whd (c, t) in
83    let rc = classify_conclusion vs in
84    let map (b, h) (c, v) = 
85       let _, argsno = PEH.split_with_whd (c, v) in
86       (I.get_rels_from_premise h v, I.S.empty, argsno > 0) :: b, succ h
87    in
88    let l, h = List.fold_left map ([], 0) vs in
89    let b = Array.of_list (List.rev l) in
90    let mk_closure b h =
91       let map j = if j < h then I.S.union (fst3 b.(j)) else identity in 
92       for i = pred h downto 0 do
93          let direct, unused, fa = b.(i) in
94          b.(i) <- I.S.fold map direct direct, unused, fa 
95       done; b
96    in
97    let b = mk_closure b h in
98    let rec mk_inverse i direct =
99       if I.S.is_empty direct then () else
100       let j = I.S.choose direct in
101       if j < h then
102          let unused, inverse, fa = b.(j) in 
103          b.(j) <- unused, I.S.add i inverse, fa
104        else ();
105        mk_inverse i (I.S.remove j direct)
106    in
107    let map i (direct, _, _) = mk_inverse i direct in
108    Array.iteri map b;
109 (*   out_table b; *)
110    let extract (x, y, z) = y, z in
111    List.rev_map extract (List.tl (Array.to_list b)), rc
112 with Invalid_argument _ -> failwith "Classify.classify"