]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/gTopLevel.ml
Reduction tactics in the scratch window implemented.
[helm.git] / helm / gTopLevel / gTopLevel.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 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 06/01/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 (* GLOBAL CONSTANTS *)
37
38 let helmns = Gdome.domString "http://www.cs.unibo.it/helm";;
39
40 let htmlheader =
41  "<html>" ^
42  " <body bgColor=\"white\">"
43 ;;
44
45 let htmlfooter =
46  " </body>" ^
47  "</html>"
48 ;;
49
50 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
51
52 let htmlheader_and_content = ref htmlheader;;
53
54 let current_cic_infos = ref None;;
55 let current_goal_infos = ref None;;
56 let current_scratch_infos = ref None;;
57
58
59 (* MISC FUNCTIONS *)
60
61 let domImpl = Gdome.domImplementation ();;
62
63 let parseStyle name =
64  let style =
65   domImpl#createDocumentFromURI
66 (*
67    ~uri:("http://phd.cs.unibo.it:8081/getxslt?uri=" ^ name) ?mode:None
68 *)
69    ~uri:("styles/" ^ name) ()
70  in
71   Gdome_xslt.processStylesheet style
72 ;;
73
74 let d_c = parseStyle "drop_coercions.xsl";;
75 let tc1 = parseStyle "objtheorycontent.xsl";;
76 let hc2 = parseStyle "content_to_html.xsl";;
77 let l   = parseStyle "link.xsl";;
78
79 let c1 = parseStyle "rootcontent.xsl";;
80 let g  = parseStyle "genmmlid.xsl";;
81 let c2 = parseStyle "annotatedpres.xsl";;
82
83
84 let getterURL = Configuration.getter_url;;
85 let processorURL = Configuration.processor_url;;
86 (*
87 let processorURL = "http://phd.cs.unibo.it:8080/helm/servelt/uwobo/";;
88 let getterURL = "http://phd.cs.unibo.it:8081/";;
89 let processorURL = "http://localhost:8080/helm/servelt/uwobo/";;
90 let getterURL = "http://localhost:8081/";;
91 *)
92
93 let mml_styles = [d_c ; c1 ; g ; c2 ; l];;
94 let mml_args =
95  ["processorURL", "'" ^ processorURL ^ "'" ;
96   "getterURL", "'" ^ getterURL ^ "'" ;
97   "draw_graphURL", "'http%3A//phd.cs.unibo.it%3A8083/'" ;
98   "uri_set_queueURL", "'http%3A//phd.cs.unibo.it%3A8084/'" ;
99   "UNICODEvsSYMBOL", "'symbol'" ;
100   "doctype-public", "'-//W3C//DTD%20XHTML%201.0%20Transitional//EN'" ;
101   "encoding", "'iso-8859-1'" ;
102   "media-type", "'text/html'" ;
103   "keys", "'d_c%2CC1%2CG%2CC2%2CL'" ;
104   "interfaceURL", "'http%3A//phd.cs.unibo.it/helm/html/cic/index.html'" ;
105   "naturalLanguage", "'yes'" ;
106   "annotations", "'no'" ;
107   "explodeall", "'true()'" ;
108   "topurl", "'http://phd.cs.unibo.it/helm'" ;
109   "CICURI", "'cic:/Coq/Init/Datatypes/bool_ind.con'" ]
110 ;;
111
112 let sequent_styles = [d_c ; c1 ; g ; c2 ; l];;
113 let sequent_args =
114  ["processorURL", "'" ^ processorURL ^ "'" ;
115   "getterURL", "'" ^ getterURL ^ "'" ;
116   "draw_graphURL", "'http%3A//phd.cs.unibo.it%3A8083/'" ;
117   "uri_set_queueURL", "'http%3A//phd.cs.unibo.it%3A8084/'" ;
118   "UNICODEvsSYMBOL", "'symbol'" ;
119   "doctype-public", "'-//W3C//DTD%20XHTML%201.0%20Transitional//EN'" ;
120   "encoding", "'iso-8859-1'" ;
121   "media-type", "'text/html'" ;
122   "keys", "'d_c%2CC1%2CG%2CC2%2CL'" ;
123   "interfaceURL", "'http%3A//phd.cs.unibo.it/helm/html/cic/index.html'" ;
124   "naturalLanguage", "'no'" ;
125   "annotations", "'no'" ;
126   "explodeall", "'true()'" ;
127   "topurl", "'http://phd.cs.unibo.it/helm'" ;
128   "CICURI", "'cic:/Coq/Init/Datatypes/bool_ind.con'" ]
129 ;;
130
131 let parse_file filename =
132  let inch = open_in filename in
133   let rec read_lines () =
134    try
135     let line = input_line inch in
136      line ^ read_lines ()
137    with
138     End_of_file -> ""
139   in
140    read_lines ()
141 ;;
142
143 let applyStylesheets input styles args =
144  List.fold_left (fun i style -> Gdome_xslt.applyStylesheet i style args)
145   input styles
146 ;;
147
148 let mml_of_cic_object uri annobj ids_to_inner_sorts ids_to_inner_types =
149  let xml =
150   Cic2Xml.print_object uri ids_to_inner_sorts annobj 
151  in
152  let xmlinnertypes =
153   Cic2Xml.print_inner_types uri ids_to_inner_sorts
154    ids_to_inner_types
155  in
156   let input = Xml2Gdome.document_of_xml domImpl xml in
157 (*CSC: We save the innertypes to disk so that we can retrieve them in the  *)
158 (*CSC: stylesheet. This DOES NOT work when UWOBO and/or the getter are not *)
159 (*CSC: local.                                                              *)
160    Xml.pp xmlinnertypes (Some "/public/sacerdot/innertypes") ;
161    let output = applyStylesheets input mml_styles mml_args in
162     output
163 ;;
164
165
166 (* CALLBACKS *)
167
168 let refresh_proof (output : GMathView.math_view) =
169  let uri,currentproof =
170   match !ProofEngine.proof with
171      None -> assert false
172    | Some (uri,metasenv,bo,ty) ->
173       uri,(Cic.CurrentProof (UriManager.name_of_uri uri, metasenv, bo, ty))
174  in
175  let
176   (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types)
177  =
178   Cic2acic.acic_object_of_cic_object currentproof
179  in
180   let mml = mml_of_cic_object uri acic ids_to_inner_sorts ids_to_inner_types in
181    output#load_tree mml ;
182    current_cic_infos := Some (ids_to_terms,ids_to_father_ids)
183 ;;
184
185 let refresh_sequent (proofw : GMathView.math_view) =
186  match !ProofEngine.goal with
187     None -> proofw#unload
188   | Some (_,currentsequent) ->
189      let metasenv =
190       match !ProofEngine.proof with
191          None -> assert false
192        | Some (_,metasenv,_,_) -> metasenv
193      in
194       let sequent_gdome,ids_to_terms,ids_to_father_ids =
195        SequentPp.XmlPp.print_sequent metasenv currentsequent
196       in
197        let sequent_doc =
198         Xml2Gdome.document_of_xml domImpl sequent_gdome
199        in
200         let sequent_mml =
201          applyStylesheets sequent_doc sequent_styles sequent_args
202         in
203          proofw#load_tree ~dom:sequent_mml ;
204          current_goal_infos := Some (ids_to_terms,ids_to_father_ids)
205 (*
206 ignore(domImpl#saveDocumentToFile ~doc:sequent_doc
207  ~name:"/public/sacerdot/guruguru1" ~indent:true ()) ;
208 ignore(domImpl#saveDocumentToFile ~doc:sequent_mml
209  ~name:"/public/sacerdot/guruguru2" ~indent:true ())
210 *)
211 ;;
212
213 let mml_of_cic_term term =
214  let context =
215   match !ProofEngine.goal with
216      None -> []
217    | Some (_,(context,_)) -> context
218  in
219   let metasenv =
220    match !ProofEngine.proof with
221       None -> []
222     | Some (_,metasenv,_,_) -> metasenv
223   in
224    let sequent_gdome,ids_to_terms,ids_to_father_ids =
225     SequentPp.XmlPp.print_sequent metasenv (context,term)
226    in
227     let sequent_doc =
228      Xml2Gdome.document_of_xml domImpl sequent_gdome
229     in
230      let res =
231       applyStylesheets sequent_doc sequent_styles sequent_args ;
232      in
233       current_scratch_infos := Some (term,ids_to_terms,ids_to_father_ids) ;
234       res
235 ;;
236
237 let output_html outputhtml msg =
238  htmlheader_and_content := !htmlheader_and_content ^ msg ;
239  outputhtml#source (!htmlheader_and_content ^ htmlfooter) ;
240  outputhtml#set_topline (-1)
241 ;;
242
243 (***********************)
244 (*       TACTICS       *)
245 (***********************)
246
247 let call_tactic tactic rendering_window () =
248  let proofw = (rendering_window#proofw : GMathView.math_view) in
249  let output = (rendering_window#output : GMathView.math_view) in
250  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
251  let savedproof = !ProofEngine.proof in
252  let savedgoal  = !ProofEngine.goal in
253   begin
254    try
255     tactic () ;
256     refresh_sequent proofw ;
257     refresh_proof output
258    with
259     e ->
260      output_html outputhtml
261       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
262      ProofEngine.proof := savedproof ;
263      ProofEngine.goal := savedgoal ;
264   end
265 ;;
266
267 let call_tactic_with_input tactic rendering_window () =
268  let proofw = (rendering_window#proofw : GMathView.math_view) in
269  let output = (rendering_window#output : GMathView.math_view) in
270  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
271  let inputt = (rendering_window#inputt : GEdit.text) in
272  let savedproof = !ProofEngine.proof in
273  let savedgoal  = !ProofEngine.goal in
274 (*CSC: Gran cut&paste da sotto... *)
275   let inputlen = inputt#length in
276   let input = inputt#get_chars 0 inputlen ^ "\n" in
277    let lexbuf = Lexing.from_string input in
278    let curi =
279     match !ProofEngine.proof with
280        None -> assert false
281      | Some (curi,_,_,_) -> curi
282    in
283    let context =
284     List.map
285      (function (_,n,_) -> n)
286      (match !ProofEngine.goal with
287          None -> assert false
288        | Some (_,(ctx,_)) -> ctx
289      )
290    in
291     try
292      while true do
293       match
294        CicTextualParserContext.main curi context CicTextualLexer.token lexbuf
295       with
296          None -> ()
297        | Some expr ->
298           tactic expr ;
299           refresh_sequent proofw ;
300           refresh_proof output
301      done
302     with
303        CicTextualParser0.Eof ->
304         inputt#delete_text 0 inputlen
305      | e ->
306         output_html outputhtml
307          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>");
308         ProofEngine.proof := savedproof ;
309         ProofEngine.goal := savedgoal
310 ;;
311
312 let call_tactic_with_goal_input tactic rendering_window () =
313  let module L = LogicalOperations in
314  let module G = Gdome in
315   let proofw = (rendering_window#proofw : GMathView.math_view) in
316   let output = (rendering_window#output : GMathView.math_view) in
317   let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
318   let savedproof = !ProofEngine.proof in
319   let savedgoal  = !ProofEngine.goal in
320    match proofw#get_selection with
321      Some node ->
322       let xpath =
323        ((node : Gdome.element)#getAttributeNS
324          ~namespaceURI:helmns
325          ~localName:(G.domString "xref"))#to_string
326       in
327        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
328        else
329         begin
330          try
331           match !current_goal_infos with
332              Some (ids_to_terms, ids_to_father_ids) ->
333               let id = xpath in
334                tactic (Hashtbl.find ids_to_terms id) ;
335                refresh_sequent rendering_window#proofw ;
336                refresh_proof rendering_window#output
337            | None -> assert false (* "ERROR: No current term!!!" *)
338          with
339           e ->
340            output_html outputhtml
341             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
342         end
343    | None ->
344       output_html outputhtml
345        ("<h1 color=\"red\">No term selected</h1>")
346 ;;
347
348 let call_tactic_with_input_and_goal_input tactic rendering_window () =
349  let module L = LogicalOperations in
350  let module G = Gdome in
351   let proofw = (rendering_window#proofw : GMathView.math_view) in
352   let output = (rendering_window#output : GMathView.math_view) in
353   let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
354   let inputt = (rendering_window#inputt : GEdit.text) in
355   let savedproof = !ProofEngine.proof in
356   let savedgoal  = !ProofEngine.goal in
357    match proofw#get_selection with
358      Some node ->
359       let xpath =
360        ((node : Gdome.element)#getAttributeNS
361          ~namespaceURI:helmns
362          ~localName:(G.domString "xref"))#to_string
363       in
364        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
365        else
366         begin
367          try
368           match !current_goal_infos with
369              Some (ids_to_terms, ids_to_father_ids) ->
370               let id = xpath in
371                (* Let's parse the input *)
372                let inputlen = inputt#length in
373                let input = inputt#get_chars 0 inputlen ^ "\n" in
374                 let lexbuf = Lexing.from_string input in
375                 let curi =
376                  match !ProofEngine.proof with
377                     None -> assert false
378                   | Some (curi,_,_,_) -> curi
379                 in
380                 let context =
381                  List.map
382                   (function (_,n,_) -> n)
383                   (match !ProofEngine.goal with
384                       None -> assert false
385                     | Some (_,(ctx,_)) -> ctx
386                   )
387                 in
388                  begin
389                   try
390                    while true do
391                     match
392                      CicTextualParserContext.main curi context
393                       CicTextualLexer.token lexbuf
394                     with
395                        None -> ()
396                      | Some expr ->
397                         tactic ~goal_input:(Hashtbl.find ids_to_terms id)
398                          ~input:expr ;
399                         refresh_sequent proofw ;
400                         refresh_proof output
401                    done
402                   with
403                      CicTextualParser0.Eof ->
404                       inputt#delete_text 0 inputlen
405                  end
406            | None -> assert false (* "ERROR: No current term!!!" *)
407          with
408           e ->
409            output_html outputhtml
410             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
411         end
412    | None ->
413       output_html outputhtml
414        ("<h1 color=\"red\">No term selected</h1>")
415 ;;
416
417 let call_tactic_with_goal_input_in_scratch tactic scratch_window () =
418  let module L = LogicalOperations in
419  let module G = Gdome in
420   let mmlwidget = (scratch_window#mmlwidget : GMathView.math_view) in
421   let outputhtml = (scratch_window#outputhtml : GHtml.xmhtml) in
422   let savedproof = !ProofEngine.proof in
423   let savedgoal  = !ProofEngine.goal in
424    match mmlwidget#get_selection with
425      Some node ->
426       let xpath =
427        ((node : Gdome.element)#getAttributeNS
428          ~namespaceURI:helmns
429          ~localName:(G.domString "xref"))#to_string
430       in
431        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
432        else
433         begin
434          try
435           match !current_scratch_infos with
436              (* term is the whole goal in the scratch_area *)
437              Some (term,ids_to_terms, ids_to_father_ids) ->
438               let id = xpath in
439                let expr = tactic term (Hashtbl.find ids_to_terms id) in
440                 let mml = mml_of_cic_term expr in
441                  scratch_window#show () ;
442                  scratch_window#mmlwidget#load_tree ~dom:mml
443            | None -> assert false (* "ERROR: No current term!!!" *)
444          with
445           e ->
446            output_html outputhtml
447             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
448         end
449    | None ->
450       output_html outputhtml
451        ("<h1 color=\"red\">No term selected</h1>")
452 ;;
453
454 let intros rendering_window = call_tactic ProofEngine.intros rendering_window;;
455 let exact rendering_window =
456  call_tactic_with_input ProofEngine.exact rendering_window
457 ;;
458 let apply rendering_window =
459  call_tactic_with_input ProofEngine.apply rendering_window
460 ;;
461 let elimintros rendering_window =
462  call_tactic_with_input ProofEngine.elim_intros rendering_window
463 ;;
464 let whd rendering_window =
465  call_tactic_with_goal_input ProofEngine.whd rendering_window
466 ;;
467 let reduce rendering_window =
468  call_tactic_with_goal_input ProofEngine.reduce rendering_window
469 ;;
470 let simpl rendering_window =
471  call_tactic_with_goal_input ProofEngine.simpl rendering_window
472 ;;
473 let fold rendering_window =
474  call_tactic_with_input ProofEngine.fold rendering_window
475 ;;
476 let cut rendering_window =
477  call_tactic_with_input ProofEngine.cut rendering_window
478 ;;
479 let change rendering_window =
480  call_tactic_with_input_and_goal_input ProofEngine.change rendering_window
481 ;;
482
483
484 let whd_in_scratch scratch_window =
485  call_tactic_with_goal_input_in_scratch ProofEngine.whd_in_scratch
486   scratch_window
487 ;;
488 let reduce_in_scratch scratch_window =
489  call_tactic_with_goal_input_in_scratch ProofEngine.reduce_in_scratch
490   scratch_window
491 ;;
492 let simpl_in_scratch scratch_window =
493  call_tactic_with_goal_input_in_scratch ProofEngine.simpl_in_scratch
494   scratch_window
495 ;;
496
497
498
499 (**********************)
500 (*   END OF TACTICS   *)
501 (**********************)
502
503 exception OpenConjecturesStillThere;;
504 exception WrongProof;;
505
506 let save rendering_window () =
507  match !ProofEngine.proof with
508     None -> assert false
509   | Some (uri,[],bo,ty) ->
510      if CicReduction.are_convertible (CicTypeChecker.type_of_aux' [] [] bo) ty then
511       begin
512        (*CSC: Wrong: [] is just plainly wrong *)
513        let proof = Cic.Definition (UriManager.name_of_uri uri,bo,ty,[]) in
514         let
515          (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
516           ids_to_inner_types)
517         =
518          Cic2acic.acic_object_of_cic_object proof
519         in
520          let mml =
521           mml_of_cic_object uri acic ids_to_inner_sorts ids_to_inner_types
522          in
523           (rendering_window#output : GMathView.math_view)#load_tree mml ;
524           current_cic_infos := Some (ids_to_terms,ids_to_father_ids)
525       end
526      else
527       raise WrongProof
528   | _ -> raise OpenConjecturesStillThere
529 ;;
530
531 let proveit rendering_window () =
532  let module L = LogicalOperations in
533  let module G = Gdome in
534  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
535   match rendering_window#output#get_selection with
536     Some node ->
537      let xpath =
538       ((node : Gdome.element)#getAttributeNS
539       (*CSC: OCAML DIVERGE
540       ((element : G.element)#getAttributeNS
541       *)
542         ~namespaceURI:helmns
543         ~localName:(G.domString "xref"))#to_string
544      in
545       if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
546       else
547        begin
548         try
549          match !current_cic_infos with
550             Some (ids_to_terms, ids_to_father_ids) ->
551              let id = xpath in
552               if L.to_sequent id ids_to_terms ids_to_father_ids then
553                refresh_proof rendering_window#output ;
554               refresh_sequent rendering_window#proofw
555           | None -> assert false (* "ERROR: No current term!!!" *)
556         with
557          e ->
558           output_html outputhtml
559            ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
560        end
561   | None -> assert false (* "ERROR: No selection!!!" *)
562 ;;
563
564 exception NotADefinition;;
565
566 let open_ rendering_window () =
567  let inputt = (rendering_window#inputt : GEdit.text) in
568  let oldinputt = (rendering_window#oldinputt : GEdit.text) in
569  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
570  let output = (rendering_window#output : GMathView.math_view) in
571  let proofw = (rendering_window#proofw : GMathView.math_view) in
572   let inputlen = inputt#length in
573   let input = inputt#get_chars 0 inputlen ^ "\n" in
574    try
575     let uri = UriManager.uri_of_string ("cic:" ^ input) in
576      CicTypeChecker.typecheck uri ;
577      let metasenv,bo,ty =
578       match CicEnvironment.get_cooked_obj uri 0 with
579          Cic.Definition (_,bo,ty,_) -> [],bo,ty
580        | Cic.CurrentProof (_,metasenv,bo,ty) -> metasenv,bo,ty
581        | Cic.Axiom _
582        | Cic.Variable _
583        | Cic.InductiveDefinition _ -> raise NotADefinition
584      in
585       ProofEngine.proof :=
586        Some (uri, metasenv, bo, ty) ;
587       ProofEngine.goal := None ;
588       inputt#delete_text 0 inputlen ;
589       ignore(oldinputt#insert_text input oldinputt#length) ;
590       refresh_sequent proofw ;
591       refresh_proof output ;
592    with
593     e ->
594      output_html outputhtml
595       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
596 ;;
597
598 let state rendering_window () =
599  let inputt = (rendering_window#inputt : GEdit.text) in
600  let oldinputt = (rendering_window#oldinputt : GEdit.text) in
601  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
602  let output = (rendering_window#output : GMathView.math_view) in
603  let proofw = (rendering_window#proofw : GMathView.math_view) in
604   let inputlen = inputt#length in
605   let input = inputt#get_chars 0 inputlen ^ "\n" in
606    (* Do something interesting *)
607    let lexbuf = Lexing.from_string input in
608     try
609      while true do
610       (* Execute the actions *)
611       match CicTextualParser.main CicTextualLexer.token lexbuf with
612          None -> ()
613        | Some expr ->
614           try
615            let _  = CicTypeChecker.type_of_aux' [] [] expr in
616             ProofEngine.proof :=
617              Some (UriManager.uri_of_string "cic:/dummy.con",
618                     [1,expr], Cic.Meta 1, expr) ;
619             ProofEngine.goal := Some (1,([],expr)) ;
620             refresh_sequent proofw ;
621             refresh_proof output ;
622           with
623            e ->
624             print_endline ("? " ^ CicPp.ppterm expr) ;
625             raise e
626      done
627     with
628        CicTextualParser0.Eof ->
629         inputt#delete_text 0 inputlen ;
630         ignore(oldinputt#insert_text input oldinputt#length)
631      | e ->
632         output_html outputhtml
633          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
634 ;;
635
636 let check rendering_window scratch_window () =
637  let inputt = (rendering_window#inputt : GEdit.text) in
638  let oldinputt = (rendering_window#oldinputt : GEdit.text) in
639  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
640  let output = (rendering_window#output : GMathView.math_view) in
641  let proofw = (rendering_window#proofw : GMathView.math_view) in
642   let inputlen = inputt#length in
643   let input = inputt#get_chars 0 inputlen ^ "\n" in
644   let curi,metasenv =
645    match !ProofEngine.proof with
646       None -> UriManager.uri_of_string "cic:/dummy.con", []
647     | Some (curi,metasenv,_,_) -> curi,metasenv
648   in
649   let ciccontext,names_context =
650    let context =
651     match !ProofEngine.goal with
652        None -> []
653      | Some (_,(ctx,_)) -> ctx
654    in
655     ProofEngine.cic_context_of_context context,
656      List.map (function (_,n,_) -> n) context
657   in
658    (* Do something interesting *)
659    let lexbuf = Lexing.from_string input in
660     try
661      while true do
662       (* Execute the actions *)
663       match
664        CicTextualParserContext.main curi names_context CicTextualLexer.token
665         lexbuf
666       with
667          None -> ()
668        | Some expr ->
669           try
670            let ty  = CicTypeChecker.type_of_aux' metasenv ciccontext expr in
671             let mml = mml_of_cic_term (Cic.Cast (expr,ty)) in
672              scratch_window#show () ;
673              scratch_window#mmlwidget#load_tree ~dom:mml
674           with
675            e ->
676             print_endline ("? " ^ CicPp.ppterm expr) ;
677             raise e
678      done
679     with
680        CicTextualParser0.Eof ->
681         inputt#delete_text 0 inputlen ;
682         ignore(oldinputt#insert_text input oldinputt#length)
683      | e ->
684        output_html outputhtml
685         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
686 ;;
687
688 let choose_selection
689      (mmlwidget : GMathView.math_view) (element : Gdome.element option)
690 =
691  let module G = Gdome in
692   let rec aux element =
693    if element#hasAttributeNS
694        ~namespaceURI:helmns
695        ~localName:(G.domString "xref")
696    then
697      mmlwidget#set_selection (Some element)
698    else
699       match element#get_parentNode with
700          None -> assert false
701        (*CSC: OCAML DIVERGES!
702        | Some p -> aux (new G.element_of_node p)
703        *)
704        | Some p -> aux (new Gdome.element_of_node p)
705   in
706    match element with
707      Some x -> aux x
708    | None   -> mmlwidget#set_selection None
709 ;;
710
711 (* STUFF TO BUILD THE GTK INTERFACE *)
712
713 (* Stuff for the widget settings *)
714
715 let export_to_postscript (output : GMathView.math_view) () =
716  output#export_to_postscript ~filename:"output.ps" ();
717 ;;
718
719 let activate_t1 (output : GMathView.math_view) button_set_anti_aliasing
720  button_set_kerning button_set_transparency button_export_to_postscript
721  button_t1 ()
722 =
723  let is_set = button_t1#active in
724   output#set_font_manager_type
725    (if is_set then `font_manager_t1 else `font_manager_gtk) ;
726   if is_set then
727    begin
728     button_set_anti_aliasing#misc#set_sensitive true ;
729     button_set_kerning#misc#set_sensitive true ;
730     button_set_transparency#misc#set_sensitive true ;
731     button_export_to_postscript#misc#set_sensitive true ;
732    end
733   else
734    begin
735     button_set_anti_aliasing#misc#set_sensitive false ;
736     button_set_kerning#misc#set_sensitive false ;
737     button_set_transparency#misc#set_sensitive false ;
738     button_export_to_postscript#misc#set_sensitive false ;
739    end
740 ;;
741
742 let set_anti_aliasing output button_set_anti_aliasing () =
743  output#set_anti_aliasing button_set_anti_aliasing#active
744 ;;
745
746 let set_kerning output button_set_kerning () =
747  output#set_kerning button_set_kerning#active
748 ;;
749
750 let set_transparency output button_set_transparency () =
751  output#set_transparency button_set_transparency#active
752 ;;
753
754 let changefont output font_size_spinb () =
755  output#set_font_size font_size_spinb#value_as_int
756 ;;
757
758 let set_log_verbosity output log_verbosity_spinb () =
759  output#set_log_verbosity log_verbosity_spinb#value_as_int
760 ;;
761
762 class settings_window (output : GMathView.math_view) sw
763  button_export_to_postscript selection_changed_callback
764 =
765  let settings_window = GWindow.window ~title:"GtkMathView settings" () in
766  let vbox =
767   GPack.vbox ~packing:settings_window#add () in
768  let table =
769   GPack.table
770    ~rows:1 ~columns:3 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
771    ~border_width:5 ~packing:vbox#add () in
772  let button_t1 =
773   GButton.toggle_button ~label:"activate t1 fonts"
774    ~packing:(table#attach ~left:0 ~top:0) () in
775  let button_set_anti_aliasing =
776   GButton.toggle_button ~label:"set_anti_aliasing"
777    ~packing:(table#attach ~left:0 ~top:1) () in
778  let button_set_kerning =
779   GButton.toggle_button ~label:"set_kerning"
780    ~packing:(table#attach ~left:1 ~top:1) () in
781  let button_set_transparency =
782   GButton.toggle_button ~label:"set_transparency"
783    ~packing:(table#attach ~left:2 ~top:1) () in
784  let table =
785   GPack.table
786    ~rows:2 ~columns:2 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
787    ~border_width:5 ~packing:vbox#add () in
788  let font_size_label =
789   GMisc.label ~text:"font size:"
790    ~packing:(table#attach ~left:0 ~top:0 ~expand:`NONE) () in
791  let font_size_spinb =
792   let sadj =
793    GData.adjustment ~value:14.0 ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
794   in
795    GEdit.spin_button 
796     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:0 ~fill:`NONE) () in
797  let log_verbosity_label =
798   GMisc.label ~text:"log verbosity:"
799    ~packing:(table#attach ~left:0 ~top:1) () in
800  let log_verbosity_spinb =
801   let sadj =
802    GData.adjustment ~value:0.0 ~lower:0.0 ~upper:3.0 ~step_incr:1.0 ()
803   in
804    GEdit.spin_button 
805     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:1) () in
806  let hbox =
807   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
808  let closeb =
809   GButton.button ~label:"Close"
810    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
811 object(self)
812  method show = settings_window#show
813  initializer
814   button_set_anti_aliasing#misc#set_sensitive false ;
815   button_set_kerning#misc#set_sensitive false ;
816   button_set_transparency#misc#set_sensitive false ;
817   (* Signals connection *)
818   ignore(button_t1#connect#clicked
819    (activate_t1 output button_set_anti_aliasing button_set_kerning
820     button_set_transparency button_export_to_postscript button_t1)) ;
821   ignore(font_size_spinb#connect#changed (changefont output font_size_spinb)) ;
822   ignore(button_set_anti_aliasing#connect#toggled
823    (set_anti_aliasing output button_set_anti_aliasing));
824   ignore(button_set_kerning#connect#toggled
825    (set_kerning output button_set_kerning)) ;
826   ignore(button_set_transparency#connect#toggled
827    (set_transparency output button_set_transparency)) ;
828   ignore(log_verbosity_spinb#connect#changed
829    (set_log_verbosity output log_verbosity_spinb)) ;
830   ignore(closeb#connect#clicked settings_window#misc#hide)
831 end;;
832
833 (* Scratch window *)
834
835 class scratch_window outputhtml =
836  let window =
837   GWindow.window ~title:"MathML viewer" ~border_width:2 () in
838  let vbox =
839   GPack.vbox ~packing:window#add () in
840  let hbox =
841   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
842  let whdb =
843   GButton.button ~label:"Whd"
844    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
845  let reduceb =
846   GButton.button ~label:"Reduce"
847    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
848  let simplb =
849   GButton.button ~label:"Simpl"
850    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
851  let scrolled_window =
852   GBin.scrolled_window ~border_width:10
853    ~packing:(vbox#pack ~expand:true ~padding:5) () in
854  let mmlwidget =
855   GMathView.math_view
856    ~packing:(scrolled_window#add) ~width:400 ~height:280 () in
857 object(self)
858  method outputhtml = outputhtml
859  method mmlwidget = mmlwidget
860  method show () = window#misc#hide () ; window#show ()
861  initializer
862   ignore(mmlwidget#connect#selection_changed (choose_selection mmlwidget)) ;
863   ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true )) ;
864   ignore(whdb#connect#clicked (whd_in_scratch self)) ;
865   ignore(reduceb#connect#clicked (reduce_in_scratch self)) ;
866   ignore(simplb#connect#clicked (simpl_in_scratch self))
867 end;;
868
869 (* Main window *)
870
871 class rendering_window output proofw (label : GMisc.label) =
872  let window =
873   GWindow.window ~title:"MathML viewer" ~border_width:2 () in
874  let hbox0 =
875   GPack.hbox ~packing:window#add () in
876  let vbox =
877   GPack.vbox ~packing:(hbox0#pack ~expand:false ~fill:false ~padding:5) () in
878  let _ = vbox#pack ~expand:false ~fill:false ~padding:5 label#coerce in
879  let scrolled_window0 =
880   GBin.scrolled_window ~border_width:10
881    ~packing:(vbox#pack ~expand:true ~padding:5) () in
882  let _ = scrolled_window0#add output#coerce in
883  let hbox1 =
884   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
885  let settingsb =
886   GButton.button ~label:"Settings"
887    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
888  let button_export_to_postscript =
889   GButton.button ~label:"export_to_postscript"
890   ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
891  let saveb =
892   GButton.button ~label:"Save"
893    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
894  let closeb =
895   GButton.button ~label:"Close"
896    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
897  let hbox2 =
898   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
899  let proveitb =
900   GButton.button ~label:"Prove It"
901    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
902  let oldinputt = GEdit.text ~editable:false ~width:400 ~height:180
903    ~packing:(vbox#pack ~padding:5) () in
904  let hbox4 =
905   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
906  let stateb =
907   GButton.button ~label:"State"
908    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
909  let openb =
910   GButton.button ~label:"Open"
911    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
912  let checkb =
913   GButton.button ~label:"Check"
914    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
915  let inputt = GEdit.text ~editable:true ~width:400 ~height: 100
916    ~packing:(vbox#pack ~padding:5) () in
917  let vbox1 =
918   GPack.vbox ~packing:(hbox0#pack ~expand:false ~fill:false ~padding:5) () in
919  let scrolled_window1 =
920   GBin.scrolled_window ~border_width:10
921    ~packing:(vbox1#pack ~expand:true ~padding:5) () in
922  let _ = scrolled_window1#add proofw#coerce in
923  let hbox3 =
924   GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
925  let exactb =
926   GButton.button ~label:"Exact"
927    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
928  let introsb =
929   GButton.button ~label:"Intros"
930    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
931  let applyb =
932   GButton.button ~label:"Apply"
933    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
934  let elimintrosb =
935   GButton.button ~label:"ElimIntros"
936    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
937  let whdb =
938   GButton.button ~label:"Whd"
939    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
940  let reduceb =
941   GButton.button ~label:"Reduce"
942    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
943  let simplb =
944   GButton.button ~label:"Simpl"
945    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
946  let foldb =
947   GButton.button ~label:"Fold"
948    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
949  let cutb =
950   GButton.button ~label:"Cut"
951    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
952  let changeb =
953   GButton.button ~label:"Change"
954    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
955  let outputhtml =
956   GHtml.xmhtml
957    ~source:"<html><body bgColor=\"white\"></body></html>"
958    ~width:400 ~height: 200
959    ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5)
960    ~show:true () in
961  let scratch_window = new scratch_window outputhtml in
962 object(self)
963  method outputhtml = outputhtml
964  method oldinputt = oldinputt
965  method inputt = inputt
966  method output = (output : GMathView.math_view)
967  method proofw = (proofw : GMathView.math_view)
968  method show = window#show
969  initializer
970   button_export_to_postscript#misc#set_sensitive false ;
971
972   (* signal handlers here *)
973   ignore(output#connect#selection_changed
974    (function elem -> proofw#unload ; choose_selection output elem)) ;
975   ignore(proofw#connect#selection_changed (choose_selection proofw)) ;
976   ignore(closeb#connect#clicked (fun _ -> GMain.Main.quit ())) ;
977   let settings_window = new settings_window output scrolled_window0
978    button_export_to_postscript (choose_selection output) in
979   ignore(settingsb#connect#clicked settings_window#show) ;
980   ignore(button_export_to_postscript#connect#clicked (export_to_postscript output)) ;
981   ignore(saveb#connect#clicked (save self)) ;
982   ignore(proveitb#connect#clicked (proveit self)) ;
983   ignore(window#event#connect#delete (fun _ -> GMain.Main.quit () ; true )) ;
984   ignore(stateb#connect#clicked (state self)) ;
985   ignore(openb#connect#clicked (open_ self)) ;
986   ignore(checkb#connect#clicked (check self scratch_window)) ;
987   ignore(exactb#connect#clicked (exact self)) ;
988   ignore(applyb#connect#clicked (apply self)) ;
989   ignore(elimintrosb#connect#clicked (elimintros self)) ;
990   ignore(whdb#connect#clicked (whd self)) ;
991   ignore(reduceb#connect#clicked (reduce self)) ;
992   ignore(simplb#connect#clicked (simpl self)) ;
993   ignore(foldb#connect#clicked (fold self)) ;
994   ignore(cutb#connect#clicked (cut self)) ;
995   ignore(changeb#connect#clicked (change self)) ;
996   ignore(introsb#connect#clicked (intros self)) ;
997   Logger.log_callback :=
998    (Logger.log_to_html ~print_and_flush:(output_html outputhtml))
999 end;;
1000
1001 (* MAIN *)
1002
1003 let initialize_everything () =
1004  let module U = Unix in
1005   let output = GMathView.math_view ~width:400 ~height:280 ()
1006   and proofw = GMathView.math_view ~width:400 ~height:275 ()
1007   and label = GMisc.label ~text:"gTopLevel" () in
1008     let rendering_window =
1009      new rendering_window output proofw label
1010     in
1011      rendering_window#show () ;
1012      GMain.Main.main ()
1013 ;;
1014
1015 let _ =
1016  CicCooking.init () ;
1017  ignore (GtkMain.Main.init ()) ;
1018  initialize_everything ()
1019 ;;