]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_procedural/proceduralClassify.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / 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 UM  = UriManager
27 module C   = Cic
28 module D   = Deannotate
29 module I   = CicInspect
30 module PEH = ProofEngineHelpers
31
32 module H   = ProceduralHelpers
33
34 type dependences = (I.S.t * bool) list
35
36 type conclusion = (int * int * UM.uri * int) option
37
38 (* debugging ****************************************************************)
39
40 let string_of_entry synth (inverse, b) =
41    if I.overlaps synth inverse then begin if b then "CF" else "C" end else
42    if I.S.is_empty inverse then "I" else "P"
43
44 let to_string synth (classes, rc) =
45    let linearize = 
46       String.concat " " (List.map (string_of_entry synth) classes)
47    in
48    match rc with
49       | None              -> linearize
50       | Some (i, j, _, _) -> Printf.sprintf "%s %u %u" linearize i j
51
52 let out_table b =
53    let map i (_, inverse) =
54       let map i tl = Printf.sprintf "%2u" i :: tl in 
55       let iset = String.concat " " (I.S.fold map inverse []) in
56       Printf.eprintf "%2u|%s\n" i iset
57    in
58    Array.iteri map b;
59    prerr_newline ()
60
61 (* dummy dependences ********************************************************)
62
63 let make l =
64    let map _ = I.S.empty, false in
65    List.rev_map map l
66
67 (* classification ***********************************************************)
68
69 let classify_conclusion vs = 
70    let rec get_argsno = function
71       | c, C.Appl (t :: vs) -> 
72          let hd, argsno = get_argsno (c, t) in 
73          hd, argsno + List.length vs
74       | _, t                -> t, 0
75    in
76    let inside i = i > 1 && i <= List.length vs in
77    match vs with
78       | v0 :: v1 :: _ ->
79          let hd0, a0 = get_argsno v0 in
80          let hd1, a1 = get_argsno v1 in
81          begin match hd0, hd1 with
82             | C.Rel i, C.MutInd (u, n, _) when inside i -> Some (i, a0, u, n)
83             | _                                         -> None
84          end
85       | _             -> None
86  
87 let classify c t =
88 try   
89    let vs, h = PEH.split_with_whd (c, t) in
90    let rc = classify_conclusion vs in
91    let map (b, h) (c, v) = 
92       let _, argsno = PEH.split_with_whd (c, v) in
93       let isf = argsno > 0 (* || H.is_sort v *) in
94       let iu = H.is_unsafe h (List.hd vs) in
95       (I.get_rels_from_premise h v, I.S.empty, isf && iu) :: b, succ h
96    in
97    let l, h = List.fold_left map ([], 0) vs in
98    let b = Array.of_list (List.rev l) in
99    let mk_closure b h =
100       let map j = if j < h then I.S.union (H.fst3 b.(j)) else H.identity in 
101       for i = pred h downto 0 do
102          let direct, unused, fa = b.(i) in
103          b.(i) <- I.S.fold map direct direct, unused, fa 
104       done; b
105    in
106    let b = mk_closure b h in
107    let rec mk_inverse i direct =
108       if I.S.is_empty direct then () else
109       let j = I.S.choose direct in
110       if j < h then
111          let unused, inverse, fa = b.(j) in 
112          b.(j) <- unused, I.S.add i inverse, fa
113        else ();
114        mk_inverse i (I.S.remove j direct)
115    in
116    let map i (direct, _, _) = mk_inverse i direct in
117    Array.iteri map b;
118 (*   out_table b; *)
119    let extract (x, y, z) = y, z in
120    List.rev_map extract (List.tl (Array.to_list b)), rc
121 with Invalid_argument _ -> failwith "Classify.classify"
122
123 (* adjusting the inferrable arguments that do not occur in the goal *********)
124
125 let adjust c vs ?goal classes = 
126    let list_xmap2 map l1 l2 = 
127       let rec aux a = function
128          | hd1 :: tl1, hd2 :: tl2 -> aux (map hd1 hd2 :: a) (tl1,tl2)
129          | _, l2                  -> List.rev_append l2 a
130       in
131       List.rev (aux [] (l1, l2))
132    in
133    let map where what (i, b) = 
134       let what = H.cic what in
135       (i, b || not (H.occurs c ~what ~where))
136    in
137    match goal with
138       | None      -> classes
139       | Some goal -> list_xmap2 (map goal) vs classes