]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_procedural/acic2Procedural.ml
68c88496ba91fbfe3e735a0264f2eb9be266b166
[helm.git] / helm / software / components / acic_procedural / acic2Procedural.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 L  = Librarian
28 module G  = GrafiteAst
29
30 module H  = ProceduralHelpers
31 module T  = ProceduralTypes
32 module P1 = Procedural1
33 module P2 = Procedural2
34 module X  = ProceduralTeX
35
36 let tex_formatter = ref None
37
38 (* object costruction *******************************************************)
39
40 let th_flavours = [`Theorem; `Lemma; `Remark; `Fact]
41
42 let def_flavours = [`Definition; `Variant]
43
44 let get_flavour sorts params context v attrs =
45    let rec aux = function
46       | []               -> 
47          if H.is_acic_proof sorts context v then List.hd th_flavours
48          else List.hd def_flavours
49       | `Flavour fl :: _ -> fl
50       | _ :: tl          -> aux tl
51    in
52    let flavour_map x y = match x, y with
53       | None, G.IPAs flavour -> Some flavour
54       | _                    -> x
55    in   
56    match List.fold_left flavour_map None params with
57       | Some fl -> fl
58       | None    -> aux attrs
59
60 let proc_obj ?(info="") proc_proof sorts params context = function
61    | C.AConstant (_, _, s, Some v, t, [], attrs)         ->
62       begin match get_flavour sorts params context v attrs with
63          | flavour when List.mem flavour th_flavours  ->
64             let ast = proc_proof v in
65             let steps, nodes = T.count_steps 0 ast, T.count_nodes 0 ast in
66             let text =
67                if List.mem G.IPComments params then 
68                   Printf.sprintf "%s\n%s%s: %u\n%s: %u\n%s"
69                   "COMMENTS" info "Tactics" steps "Final nodes" nodes "END"
70                else
71                   ""
72             in
73             T.Statement (flavour, Some s, t, None, "") :: ast @ [T.Qed text]
74          | flavour when List.mem flavour def_flavours ->
75             [T.Statement (flavour, Some s, t, Some v, "")]
76          | _                                  ->
77             failwith "not a theorem, definition, axiom or inductive type"
78       end
79    | C.AConstant (_, _, s, None, t, [], attrs)           ->
80       [T.Statement (`Axiom, Some s, t, None, "")]
81    | C.AInductiveDefinition (_, types, [], lpsno, attrs) ->
82       [T.Inductive (types, lpsno, "")] 
83    | _                                          ->
84       failwith "not a theorem, definition, axiom or inductive type"
85
86 (* interface functions ******************************************************)
87
88 let get_proc_proof ~ids_to_inner_sorts ~ids_to_inner_types params context =
89    let level_map x y = match x, y with
90       | None, G.IPLevel level -> Some level
91       | _                     -> x
92    in   
93    match List.fold_left level_map None params with
94       | None
95       | Some 2 ->   
96          P2.proc_proof 
97             (P2.init ~ids_to_inner_sorts ~ids_to_inner_types params context)
98       | Some 1 ->
99          P1.proc_proof 
100             (P1.init ~ids_to_inner_sorts ~ids_to_inner_types params context)
101       | Some n ->
102          failwith (
103             "Procedural reconstruction level not supported: " ^ 
104             string_of_int n
105          )
106
107 let procedural_of_acic_object ~ids_to_inner_sorts ~ids_to_inner_types 
108    ?info params anobj = 
109    let proc_proof = 
110       get_proc_proof ~ids_to_inner_sorts ~ids_to_inner_types params []
111    in 
112    L.time_stamp "P : LEVEL 2  ";
113    HLog.debug "Procedural: level 2 transformation";
114    let steps = proc_obj ?info proc_proof ids_to_inner_sorts params [] anobj in
115    let _ = match !tex_formatter with
116       | None     -> ()
117       | Some frm -> X.tex_of_steps frm ids_to_inner_sorts steps
118    in
119    L.time_stamp "P : RENDERING";
120    HLog.debug "Procedural: grafite rendering";
121    let r = List.rev (T.render_steps [] steps) in
122    L.time_stamp "P : DONE     "; r
123
124 let procedural_of_acic_term ~ids_to_inner_sorts ~ids_to_inner_types params
125    context annterm = 
126    let proc_proof =
127       get_proc_proof ~ids_to_inner_sorts ~ids_to_inner_types params context
128    in
129    HLog.debug "Procedural: level 2 transformation";
130    let steps = proc_proof annterm in
131    let _ = match !tex_formatter with
132       | None     -> ()
133       | Some frm -> X.tex_of_steps frm ids_to_inner_sorts steps
134    in
135    HLog.debug "Procedural: grafite rendering";
136    List.rev (T.render_steps [] steps)