]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/gTopLevel.ml
- rewritesimpl_tac added in fourierR.ml (wrong location)
[helm.git] / helm / gTopLevel / gTopLevel.ml
1 (* Copyright (C) 2000-2002, 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 let prooffile = "/home/tassi/miohelm/tmp/currentproof";;
51 (*CSC: the getter should handle the innertypes, not the FS *)
52 let innertypesfile = "/home/tassi/miohelm/tmp/innertypes";;
53
54 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
55
56 let htmlheader_and_content = ref htmlheader;;
57
58 let current_cic_infos = ref None;;
59 let current_goal_infos = ref None;;
60 let current_scratch_infos = ref None;;
61
62 (* COMMAND LINE OPTIONS *)
63
64 let usedb = ref true
65
66 let argspec =
67   [
68     "-nodb", Arg.Clear usedb, "disable use of MathQL DB"
69   ]
70 in
71 Arg.parse argspec ignore ""
72
73
74 (* MISC FUNCTIONS *)
75
76 let domImpl = Gdome.domImplementation ();;
77
78 let parseStyle name =
79  let style =
80   domImpl#createDocumentFromURI
81 (*
82    ~uri:("http://phd.cs.unibo.it:8081/getxslt?uri=" ^ name) ?mode:None
83 *)
84    ~uri:("styles/" ^ name) ()
85  in
86   Gdome_xslt.processStylesheet style
87 ;;
88
89 let d_c = parseStyle "drop_coercions.xsl";;
90 let tc1 = parseStyle "objtheorycontent.xsl";;
91 let hc2 = parseStyle "content_to_html.xsl";;
92 let l   = parseStyle "link.xsl";;
93
94 let c1 = parseStyle "rootcontent.xsl";;
95 let g  = parseStyle "genmmlid.xsl";;
96 let c2 = parseStyle "annotatedpres.xsl";;
97
98
99 let getterURL = Configuration.getter_url;;
100 let processorURL = Configuration.processor_url;;
101
102 let mml_styles = [d_c ; c1 ; g ; c2 ; l];;
103 let mml_args =
104  ["processorURL", "'" ^ processorURL ^ "'" ;
105   "getterURL", "'" ^ getterURL ^ "'" ;
106   "draw_graphURL", "'http%3A//phd.cs.unibo.it%3A8083/'" ;
107   "uri_set_queueURL", "'http%3A//phd.cs.unibo.it%3A8084/'" ;
108   "UNICODEvsSYMBOL", "'symbol'" ;
109   "doctype-public", "'-//W3C//DTD%20XHTML%201.0%20Transitional//EN'" ;
110   "encoding", "'iso-8859-1'" ;
111   "media-type", "'text/html'" ;
112   "keys", "'d_c%2CC1%2CG%2CC2%2CL'" ;
113   "interfaceURL", "'http%3A//phd.cs.unibo.it/helm/html/cic/index.html'" ;
114   "naturalLanguage", "'yes'" ;
115   "annotations", "'no'" ;
116   "explodeall", "'true()'" ;
117   "topurl", "'http://phd.cs.unibo.it/helm'" ;
118   "CICURI", "'cic:/Coq/Init/Datatypes/bool_ind.con'" ]
119 ;;
120
121 let sequent_styles = [d_c ; c1 ; g ; c2 ; l];;
122 let sequent_args =
123  ["processorURL", "'" ^ processorURL ^ "'" ;
124   "getterURL", "'" ^ getterURL ^ "'" ;
125   "draw_graphURL", "'http%3A//phd.cs.unibo.it%3A8083/'" ;
126   "uri_set_queueURL", "'http%3A//phd.cs.unibo.it%3A8084/'" ;
127   "UNICODEvsSYMBOL", "'symbol'" ;
128   "doctype-public", "'-//W3C//DTD%20XHTML%201.0%20Transitional//EN'" ;
129   "encoding", "'iso-8859-1'" ;
130   "media-type", "'text/html'" ;
131   "keys", "'d_c%2CC1%2CG%2CC2%2CL'" ;
132   "interfaceURL", "'http%3A//phd.cs.unibo.it/helm/html/cic/index.html'" ;
133   "naturalLanguage", "'no'" ;
134   "annotations", "'no'" ;
135   "explodeall", "'true()'" ;
136   "topurl", "'http://phd.cs.unibo.it/helm'" ;
137   "CICURI", "'cic:/Coq/Init/Datatypes/bool_ind.con'" ]
138 ;;
139
140 let parse_file filename =
141  let inch = open_in filename in
142   let rec read_lines () =
143    try
144     let line = input_line inch in
145      line ^ read_lines ()
146    with
147     End_of_file -> ""
148   in
149    read_lines ()
150 ;;
151
152 let applyStylesheets input styles args =
153  List.fold_left (fun i style -> Gdome_xslt.applyStylesheet i style args)
154   input styles
155 ;;
156
157 let mml_of_cic_object uri annobj ids_to_inner_sorts ids_to_inner_types =
158  let xml =
159   Cic2Xml.print_object uri ~ids_to_inner_sorts annobj 
160  in
161  let xmlinnertypes =
162   Cic2Xml.print_inner_types uri ~ids_to_inner_sorts
163    ~ids_to_inner_types
164  in
165   let input = Xml2Gdome.document_of_xml domImpl xml in
166 (*CSC: We save the innertypes to disk so that we can retrieve them in the  *)
167 (*CSC: stylesheet. This DOES NOT work when UWOBO and/or the getter are not *)
168 (*CSC: local.                                                              *)
169    Xml.pp xmlinnertypes (Some innertypesfile) ;
170    let output = applyStylesheets input mml_styles mml_args in
171     output
172 ;;
173
174
175 (* CALLBACKS *)
176
177 exception RefreshSequentException of exn;;
178 exception RefreshProofException of exn;;
179
180 let refresh_proof (output : GMathView.math_view) =
181  try
182   let uri,currentproof =
183    match !ProofEngine.proof with
184       None -> assert false
185     | Some (uri,metasenv,bo,ty) ->
186        uri,(Cic.CurrentProof (UriManager.name_of_uri uri, metasenv, bo, ty))
187   in
188    let
189     (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
190      ids_to_inner_types,ids_to_conjectures,ids_to_hypotheses)
191    =
192     Cic2acic.acic_object_of_cic_object currentproof
193    in
194     let mml =
195      mml_of_cic_object uri acic ids_to_inner_sorts ids_to_inner_types
196     in
197      output#load_tree mml ;
198      current_cic_infos :=
199       Some (ids_to_terms,ids_to_father_ids,ids_to_conjectures,ids_to_hypotheses)
200  with
201   e ->
202  match !ProofEngine.proof with
203     None -> assert false
204   | Some (uri,metasenv,bo,ty) ->
205 prerr_endline ("Offending proof: " ^ CicPp.ppobj (Cic.CurrentProof ("questa",metasenv,bo,ty))) ; flush stderr ;
206    raise (RefreshProofException e)
207 ;;
208
209 let refresh_sequent (proofw : GMathView.math_view) =
210  try
211   match !ProofEngine.goal with
212      None -> proofw#unload
213    | Some metano ->
214       let metasenv =
215        match !ProofEngine.proof with
216           None -> assert false
217         | Some (_,metasenv,_,_) -> metasenv
218       in
219       let currentsequent = List.find (function (m,_,_) -> m=metano) metasenv in
220        let sequent_gdome,ids_to_terms,ids_to_father_ids,ids_to_hypotheses =
221         SequentPp.XmlPp.print_sequent metasenv currentsequent
222        in
223         let sequent_doc =
224          Xml2Gdome.document_of_xml domImpl sequent_gdome
225         in
226          let sequent_mml =
227           applyStylesheets sequent_doc sequent_styles sequent_args
228          in
229           proofw#load_tree ~dom:sequent_mml ;
230           current_goal_infos :=
231            Some (ids_to_terms,ids_to_father_ids,ids_to_hypotheses)
232  with
233   e ->
234 let metano =
235   match !ProofEngine.goal with
236      None -> assert false
237    | Some m -> m
238 in
239 let metasenv =
240  match !ProofEngine.proof with
241     None -> assert false
242   | Some (_,metasenv,_,_) -> metasenv
243 in
244 let currentsequent = List.find (function (m,_,_) -> m=metano) metasenv in
245 prerr_endline ("Offending sequent: " ^ SequentPp.TextualPp.print_sequent currentsequent) ; flush stderr ;
246    raise (RefreshSequentException e)
247 ;;
248
249 (*
250 ignore(domImpl#saveDocumentToFile ~doc:sequent_doc
251  ~name:"/home/galata/miohelm/guruguru1" ~indent:true ()) ;
252 *)
253
254 let mml_of_cic_term metano term =
255  let metasenv =
256   match !ProofEngine.proof with
257      None -> []
258    | Some (_,metasenv,_,_) -> metasenv
259  in
260  let context =
261   match !ProofEngine.goal with
262      None -> []
263    | Some metano ->
264       let (_,canonical_context,_) =
265        List.find (function (m,_,_) -> m=metano) metasenv
266       in
267        canonical_context
268  in
269    let sequent_gdome,ids_to_terms,ids_to_father_ids,ids_to_hypotheses =
270     SequentPp.XmlPp.print_sequent metasenv (metano,context,term)
271    in
272     let sequent_doc =
273      Xml2Gdome.document_of_xml domImpl sequent_gdome
274     in
275      let res =
276       applyStylesheets sequent_doc sequent_styles sequent_args ;
277      in
278       current_scratch_infos :=
279        Some (term,ids_to_terms,ids_to_father_ids,ids_to_hypotheses) ;
280       res
281 ;;
282
283 let output_html outputhtml msg =
284  htmlheader_and_content := !htmlheader_and_content ^ msg ;
285  outputhtml#source (!htmlheader_and_content ^ htmlfooter) ;
286  outputhtml#set_topline (-1)
287 ;;
288
289 (***********************)
290 (*       TACTICS       *)
291 (***********************)
292
293 let call_tactic tactic rendering_window () =
294  let proofw = (rendering_window#proofw : GMathView.math_view) in
295  let output = (rendering_window#output : GMathView.math_view) in
296  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
297  let savedproof = !ProofEngine.proof in
298  let savedgoal  = !ProofEngine.goal in
299   begin
300    try
301     tactic () ;
302     refresh_sequent proofw ;
303     refresh_proof output
304    with
305       RefreshSequentException e ->
306        output_html outputhtml
307         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
308          "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
309        ProofEngine.proof := savedproof ;
310        ProofEngine.goal := savedgoal ;
311        refresh_sequent proofw
312     | RefreshProofException e ->
313        output_html outputhtml
314         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
315          "proof: " ^ Printexc.to_string e ^ "</h1>") ;
316        ProofEngine.proof := savedproof ;
317        ProofEngine.goal := savedgoal ;
318        refresh_sequent proofw ;
319        refresh_proof output
320     | e ->
321        output_html outputhtml
322         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
323        ProofEngine.proof := savedproof ;
324        ProofEngine.goal := savedgoal ;
325   end
326 ;;
327
328 let call_tactic_with_input tactic rendering_window () =
329  let proofw = (rendering_window#proofw : GMathView.math_view) in
330  let output = (rendering_window#output : GMathView.math_view) in
331  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
332  let inputt = (rendering_window#inputt : GEdit.text) in
333  let savedproof = !ProofEngine.proof in
334  let savedgoal  = !ProofEngine.goal in
335 (*CSC: Gran cut&paste da sotto... *)
336   let inputlen = inputt#length in
337   let input = inputt#get_chars 0 inputlen ^ "\n" in
338    let lexbuf = Lexing.from_string input in
339    let curi =
340     match !ProofEngine.proof with
341        None -> assert false
342      | Some (curi,_,_,_) -> curi
343    in
344    let uri,metasenv,bo,ty =
345     match !ProofEngine.proof with
346        None -> assert false
347      | Some (uri,metasenv,bo,ty) -> uri,metasenv,bo,ty
348    in
349    let context =
350     List.map
351      (function
352          Some (n,_) -> Some n
353        | None -> None)
354      (match !ProofEngine.goal with
355          None -> assert false
356        | Some metano ->
357           let (_,canonical_context,_) =
358            List.find (function (m,_,_) -> m=metano) metasenv
359           in
360            canonical_context
361      )
362    in
363     try
364      while true do
365       match
366        CicTextualParserContext.main
367         curi context metasenv CicTextualLexer.token lexbuf
368       with
369          None -> ()
370        | Some (metasenv',expr) ->
371           ProofEngine.proof := Some (uri,metasenv',bo,ty) ;
372           tactic expr ;
373           refresh_sequent proofw ;
374           refresh_proof output
375      done
376     with
377        CicTextualParser0.Eof ->
378         inputt#delete_text 0 inputlen
379      | RefreshSequentException e ->
380         output_html outputhtml
381          ("<h1 color=\"red\">Exception raised during the refresh of the " ^
382           "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
383         ProofEngine.proof := savedproof ;
384         ProofEngine.goal := savedgoal ;
385         refresh_sequent proofw
386      | RefreshProofException e ->
387         output_html outputhtml
388          ("<h1 color=\"red\">Exception raised during the refresh of the " ^
389           "proof: " ^ Printexc.to_string e ^ "</h1>") ;
390         ProofEngine.proof := savedproof ;
391         ProofEngine.goal := savedgoal ;
392         refresh_sequent proofw ;
393         refresh_proof output
394      | e ->
395         output_html outputhtml
396          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
397         ProofEngine.proof := savedproof ;
398         ProofEngine.goal := savedgoal ;
399 ;;
400
401 let call_tactic_with_goal_input tactic rendering_window () =
402  let module L = LogicalOperations in
403  let module G = Gdome in
404   let proofw = (rendering_window#proofw : GMathView.math_view) in
405   let output = (rendering_window#output : GMathView.math_view) in
406   let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
407   let savedproof = !ProofEngine.proof in
408   let savedgoal  = !ProofEngine.goal in
409    match proofw#get_selection with
410      Some node ->
411       let xpath =
412        ((node : Gdome.element)#getAttributeNS
413          ~namespaceURI:helmns
414          ~localName:(G.domString "xref"))#to_string
415       in
416        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
417        else
418         begin
419          try
420           match !current_goal_infos with
421              Some (ids_to_terms, ids_to_father_ids,_) ->
422               let id = xpath in
423                tactic (Hashtbl.find ids_to_terms id) ;
424                refresh_sequent rendering_window#proofw ;
425                refresh_proof rendering_window#output
426            | None -> assert false (* "ERROR: No current term!!!" *)
427          with
428             RefreshSequentException e ->
429              output_html outputhtml
430               ("<h1 color=\"red\">Exception raised during the refresh of the " ^
431                "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
432              ProofEngine.proof := savedproof ;
433              ProofEngine.goal := savedgoal ;
434              refresh_sequent proofw
435           | RefreshProofException e ->
436              output_html outputhtml
437               ("<h1 color=\"red\">Exception raised during the refresh of the " ^
438                "proof: " ^ Printexc.to_string e ^ "</h1>") ;
439              ProofEngine.proof := savedproof ;
440              ProofEngine.goal := savedgoal ;
441              refresh_sequent proofw ;
442              refresh_proof output
443           | e ->
444              output_html outputhtml
445               ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
446              ProofEngine.proof := savedproof ;
447              ProofEngine.goal := savedgoal ;
448         end
449    | None ->
450       output_html outputhtml
451        ("<h1 color=\"red\">No term selected</h1>")
452 ;;
453
454 let call_tactic_with_input_and_goal_input tactic rendering_window () =
455  let module L = LogicalOperations in
456  let module G = Gdome in
457   let proofw = (rendering_window#proofw : GMathView.math_view) in
458   let output = (rendering_window#output : GMathView.math_view) in
459   let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
460   let inputt = (rendering_window#inputt : GEdit.text) in
461   let savedproof = !ProofEngine.proof in
462   let savedgoal  = !ProofEngine.goal in
463    match proofw#get_selection with
464      Some node ->
465       let xpath =
466        ((node : Gdome.element)#getAttributeNS
467          ~namespaceURI:helmns
468          ~localName:(G.domString "xref"))#to_string
469       in
470        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
471        else
472         begin
473          try
474           match !current_goal_infos with
475              Some (ids_to_terms, ids_to_father_ids,_) ->
476               let id = xpath in
477                (* Let's parse the input *)
478                let inputlen = inputt#length in
479                let input = inputt#get_chars 0 inputlen ^ "\n" in
480                 let lexbuf = Lexing.from_string input in
481                 let curi =
482                  match !ProofEngine.proof with
483                     None -> assert false
484                   | Some (curi,_,_,_) -> curi
485                 in
486                 let uri,metasenv,bo,ty =
487                  match !ProofEngine.proof with
488                     None -> assert false
489                   | Some (uri,metasenv,bo,ty) -> uri,metasenv,bo,ty
490                 in
491                 let context =
492                  List.map
493                   (function
494                       Some (n,_) -> Some n
495                     | None -> None)
496                   (match !ProofEngine.goal with
497                       None -> assert false
498                     | Some metano ->
499                        let (_,canonical_context,_) =
500                         List.find (function (m,_,_) -> m=metano) metasenv
501                        in
502                         canonical_context
503                   )
504                 in
505                  begin
506                   try
507                    while true do
508                     match
509                      CicTextualParserContext.main curi context metasenv
510                       CicTextualLexer.token lexbuf
511                     with
512                        None -> ()
513                      | Some (metasenv',expr) ->
514                         ProofEngine.proof := Some (uri,metasenv',bo,ty) ;
515                         tactic ~goal_input:(Hashtbl.find ids_to_terms id)
516                          ~input:expr ;
517                         refresh_sequent proofw ;
518                         refresh_proof output
519                    done
520                   with
521                      CicTextualParser0.Eof ->
522                       inputt#delete_text 0 inputlen
523                  end
524            | None -> assert false (* "ERROR: No current term!!!" *)
525          with
526             RefreshSequentException e ->
527              output_html outputhtml
528               ("<h1 color=\"red\">Exception raised during the refresh of the " ^
529                "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
530              ProofEngine.proof := savedproof ;
531              ProofEngine.goal := savedgoal ;
532              refresh_sequent proofw
533           | RefreshProofException e ->
534              output_html outputhtml
535               ("<h1 color=\"red\">Exception raised during the refresh of the " ^
536                "proof: " ^ Printexc.to_string e ^ "</h1>") ;
537              ProofEngine.proof := savedproof ;
538              ProofEngine.goal := savedgoal ;
539              refresh_sequent proofw ;
540              refresh_proof output
541           | e ->
542              output_html outputhtml
543               ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
544              ProofEngine.proof := savedproof ;
545              ProofEngine.goal := savedgoal ;
546         end
547    | None ->
548       output_html outputhtml
549        ("<h1 color=\"red\">No term selected</h1>")
550 ;;
551
552 let call_tactic_with_goal_input_in_scratch tactic scratch_window () =
553  let module L = LogicalOperations in
554  let module G = Gdome in
555   let mmlwidget = (scratch_window#mmlwidget : GMathView.math_view) in
556   let outputhtml = (scratch_window#outputhtml : GHtml.xmhtml) in
557   let savedproof = !ProofEngine.proof in
558   let savedgoal  = !ProofEngine.goal in
559    match mmlwidget#get_selection with
560      Some node ->
561       let xpath =
562        ((node : Gdome.element)#getAttributeNS
563          ~namespaceURI:helmns
564          ~localName:(G.domString "xref"))#to_string
565       in
566        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
567        else
568         begin
569          try
570           match !current_scratch_infos with
571              (* term is the whole goal in the scratch_area *)
572              Some (term,ids_to_terms, ids_to_father_ids,_) ->
573               let id = xpath in
574                let expr = tactic term (Hashtbl.find ids_to_terms id) in
575                 let mml = mml_of_cic_term 111 expr in
576                  scratch_window#show () ;
577                  scratch_window#mmlwidget#load_tree ~dom:mml
578            | None -> assert false (* "ERROR: No current term!!!" *)
579          with
580           e ->
581            output_html outputhtml
582             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
583         end
584    | None ->
585       output_html outputhtml
586        ("<h1 color=\"red\">No term selected</h1>")
587 ;;
588
589 let call_tactic_with_hypothesis_input tactic rendering_window () =
590  let module L = LogicalOperations in
591  let module G = Gdome in
592   let proofw = (rendering_window#proofw : GMathView.math_view) in
593   let output = (rendering_window#output : GMathView.math_view) in
594   let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
595   let savedproof = !ProofEngine.proof in
596   let savedgoal  = !ProofEngine.goal in
597    match proofw#get_selection with
598      Some node ->
599       let xpath =
600        ((node : Gdome.element)#getAttributeNS
601          ~namespaceURI:helmns
602          ~localName:(G.domString "xref"))#to_string
603       in
604        if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
605        else
606         begin
607          try
608           match !current_goal_infos with
609              Some (_,_,ids_to_hypotheses) ->
610               let id = xpath in
611                tactic (Hashtbl.find ids_to_hypotheses id) ;
612                refresh_sequent rendering_window#proofw ;
613                refresh_proof rendering_window#output
614            | None -> assert false (* "ERROR: No current term!!!" *)
615          with
616             RefreshSequentException e ->
617              output_html outputhtml
618               ("<h1 color=\"red\">Exception raised during the refresh of the " ^
619                "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
620              ProofEngine.proof := savedproof ;
621              ProofEngine.goal := savedgoal ;
622              refresh_sequent proofw
623           | RefreshProofException e ->
624              output_html outputhtml
625               ("<h1 color=\"red\">Exception raised during the refresh of the " ^
626                "proof: " ^ Printexc.to_string e ^ "</h1>") ;
627              ProofEngine.proof := savedproof ;
628              ProofEngine.goal := savedgoal ;
629              refresh_sequent proofw ;
630              refresh_proof output
631           | e ->
632              output_html outputhtml
633               ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
634              ProofEngine.proof := savedproof ;
635              ProofEngine.goal := savedgoal ;
636         end
637    | None ->
638       output_html outputhtml
639        ("<h1 color=\"red\">No term selected</h1>")
640 ;;
641
642
643 let intros rendering_window = call_tactic ProofEngine.intros rendering_window;;
644 let exact rendering_window =
645  call_tactic_with_input ProofEngine.exact rendering_window
646 ;;
647 let apply rendering_window =
648  call_tactic_with_input ProofEngine.apply rendering_window
649 ;;
650 let elimintrossimpl rendering_window =
651  call_tactic_with_input ProofEngine.elim_intros_simpl rendering_window
652 ;;
653 let elimtype rendering_window =
654  call_tactic_with_input ProofEngine.elim_type rendering_window
655 ;;
656 let whd rendering_window =
657  call_tactic_with_goal_input ProofEngine.whd rendering_window
658 ;;
659 let reduce rendering_window =
660  call_tactic_with_goal_input ProofEngine.reduce rendering_window
661 ;;
662 let simpl rendering_window =
663  call_tactic_with_goal_input ProofEngine.simpl rendering_window
664 ;;
665 let fold rendering_window =
666  call_tactic_with_input ProofEngine.fold rendering_window
667 ;;
668 let cut rendering_window =
669  call_tactic_with_input ProofEngine.cut rendering_window
670 ;;
671 let change rendering_window =
672  call_tactic_with_input_and_goal_input ProofEngine.change rendering_window
673 ;;
674 let letin rendering_window =
675  call_tactic_with_input ProofEngine.letin rendering_window
676 ;;
677 let ring rendering_window = call_tactic ProofEngine.ring rendering_window;;
678 let clearbody rendering_window =
679  call_tactic_with_hypothesis_input ProofEngine.clearbody rendering_window
680 ;;
681 let clear rendering_window =
682  call_tactic_with_hypothesis_input ProofEngine.clear rendering_window
683 ;;
684 let fourier rendering_window =
685  call_tactic ProofEngine.fourier rendering_window
686 ;;
687 let rewritesimpl rendering_window =
688  call_tactic_with_input ProofEngine.rewrite_simpl rendering_window
689 ;;
690
691
692
693 let whd_in_scratch scratch_window =
694  call_tactic_with_goal_input_in_scratch ProofEngine.whd_in_scratch
695   scratch_window
696 ;;
697 let reduce_in_scratch scratch_window =
698  call_tactic_with_goal_input_in_scratch ProofEngine.reduce_in_scratch
699   scratch_window
700 ;;
701 let simpl_in_scratch scratch_window =
702  call_tactic_with_goal_input_in_scratch ProofEngine.simpl_in_scratch
703   scratch_window
704 ;;
705
706
707
708 (**********************)
709 (*   END OF TACTICS   *)
710 (**********************)
711
712 exception OpenConjecturesStillThere;;
713 exception WrongProof;;
714
715 let qed rendering_window () =
716  match !ProofEngine.proof with
717     None -> assert false
718   | Some (uri,[],bo,ty) ->
719      if
720       CicReduction.are_convertible []
721        (CicTypeChecker.type_of_aux' [] [] bo) ty
722      then
723       begin
724        (*CSC: Wrong: [] is just plainly wrong *)
725        let proof = Cic.Definition (UriManager.name_of_uri uri,bo,ty,[]) in
726         let
727          (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
728           ids_to_inner_types,ids_to_conjectures,ids_to_hypotheses)
729         =
730          Cic2acic.acic_object_of_cic_object proof
731         in
732          let mml =
733           mml_of_cic_object uri acic ids_to_inner_sorts ids_to_inner_types
734          in
735           (rendering_window#output : GMathView.math_view)#load_tree mml ;
736           current_cic_infos :=
737            Some
738             (ids_to_terms,ids_to_father_ids,ids_to_conjectures,
739              ids_to_hypotheses)
740       end
741      else
742       raise WrongProof
743   | _ -> raise OpenConjecturesStillThere
744 ;;
745
746 (*????
747 let dtdname = "http://www.cs.unibo.it/helm/dtd/cic.dtd";;
748 *)
749 let dtdname = "/home/tassi/miohelm/helm/dtd/cic.dtd";;
750
751 let save rendering_window () =
752  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
753   match !ProofEngine.proof with
754      None -> assert false
755    | Some (uri, metasenv, bo, ty) ->
756       let currentproof =
757        Cic.CurrentProof (UriManager.name_of_uri uri,metasenv,bo,ty)
758       in
759        let (acurrentproof,_,_,ids_to_inner_sorts,_,_,_) =
760         Cic2acic.acic_object_of_cic_object currentproof
761        in
762         let xml = Cic2Xml.print_object uri ~ids_to_inner_sorts acurrentproof in
763          let xml' =
764           [< Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
765              Xml.xml_cdata
766               ("<!DOCTYPE CurrentProof SYSTEM \"" ^ dtdname ^ "\">\n\n") ;
767              xml
768           >]
769          in
770           Xml.pp ~quiet:true xml' (Some prooffile) ;
771           output_html outputhtml
772            ("<h1 color=\"Green\">Current proof saved to " ^
773             prooffile ^ "</h1>")
774 ;;
775
776 (* Used to typecheck the loaded proofs *)
777 let typecheck_loaded_proof metasenv bo ty =
778  let module T = CicTypeChecker in
779   (*CSC: bisogna controllare anche il metasenv!!! *)
780   ignore (T.type_of_aux' metasenv [] ty) ;
781   ignore (T.type_of_aux' metasenv [] bo)
782 ;;
783
784 let load rendering_window () =
785  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
786  let output = (rendering_window#output : GMathView.math_view) in
787  let proofw = (rendering_window#proofw : GMathView.math_view) in
788   try
789    let uri = UriManager.uri_of_string "cic:/dummy.con" in
790     match CicParser.obj_of_xml prooffile uri with
791        Cic.CurrentProof (_,metasenv,bo,ty) ->
792         typecheck_loaded_proof metasenv bo ty ;
793         ProofEngine.proof :=
794          Some (uri, metasenv, bo, ty) ;
795         ProofEngine.goal :=
796          (match metasenv with
797              [] -> None
798            | (metano,_,_)::_ -> Some metano
799          ) ;
800         refresh_proof output ;
801         refresh_sequent proofw ;
802          output_html outputhtml
803           ("<h1 color=\"Green\">Current proof loaded from " ^
804             prooffile ^ "</h1>")
805      | _ -> assert false
806   with
807      RefreshSequentException e ->
808       output_html outputhtml
809        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
810         "sequent: " ^ Printexc.to_string e ^ "</h1>")
811    | RefreshProofException e ->
812       output_html outputhtml
813        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
814         "proof: " ^ Printexc.to_string e ^ "</h1>")
815    | e ->
816       output_html outputhtml
817        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
818 ;;
819
820 let proveit rendering_window () =
821  let module L = LogicalOperations in
822  let module G = Gdome in
823  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
824   match rendering_window#output#get_selection with
825     Some node ->
826      let xpath =
827       ((node : Gdome.element)#getAttributeNS
828       (*CSC: OCAML DIVERGE
829       ((element : G.element)#getAttributeNS
830       *)
831         ~namespaceURI:helmns
832         ~localName:(G.domString "xref"))#to_string
833      in
834       if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
835       else
836        begin
837         try
838          match !current_cic_infos with
839             Some (ids_to_terms, ids_to_father_ids, _, _) ->
840              let id = xpath in
841               L.to_sequent id ids_to_terms ids_to_father_ids ;
842               refresh_proof rendering_window#output ;
843               refresh_sequent rendering_window#proofw
844           | None -> assert false (* "ERROR: No current term!!!" *)
845         with
846            RefreshSequentException e ->
847             output_html outputhtml
848              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
849               "sequent: " ^ Printexc.to_string e ^ "</h1>")
850          | RefreshProofException e ->
851             output_html outputhtml
852              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
853               "proof: " ^ Printexc.to_string e ^ "</h1>")
854          | e ->
855             output_html outputhtml
856              ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
857        end
858   | None -> assert false (* "ERROR: No selection!!!" *)
859 ;;
860
861 let focus rendering_window () =
862  let module L = LogicalOperations in
863  let module G = Gdome in
864  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
865   match rendering_window#output#get_selection with
866     Some node ->
867      let xpath =
868       ((node : Gdome.element)#getAttributeNS
869       (*CSC: OCAML DIVERGE
870       ((element : G.element)#getAttributeNS
871       *)
872         ~namespaceURI:helmns
873         ~localName:(G.domString "xref"))#to_string
874      in
875       if xpath = "" then assert false (* "ERROR: No xref found!!!" *)
876       else
877        begin
878         try
879          match !current_cic_infos with
880             Some (ids_to_terms, ids_to_father_ids, _, _) ->
881              let id = xpath in
882               L.focus id ids_to_terms ids_to_father_ids ;
883               refresh_sequent rendering_window#proofw
884           | None -> assert false (* "ERROR: No current term!!!" *)
885         with
886            RefreshSequentException e ->
887             output_html outputhtml
888              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
889               "sequent: " ^ Printexc.to_string e ^ "</h1>")
890          | RefreshProofException e ->
891             output_html outputhtml
892              ("<h1 color=\"red\">Exception raised during the refresh of the " ^
893               "proof: " ^ Printexc.to_string e ^ "</h1>")
894          | e ->
895             output_html outputhtml
896              ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
897        end
898   | None -> assert false (* "ERROR: No selection!!!" *)
899 ;;
900
901 exception NoPrevGoal;;
902 exception NoNextGoal;;
903
904 let prevgoal metasenv metano =
905  let rec aux =
906   function
907      [] -> assert false
908    | [(m,_,_)] -> raise NoPrevGoal
909    | (n,_,_)::(m,_,_)::_ when m=metano -> n
910    | _::tl -> aux tl
911  in
912   aux metasenv
913 ;;
914
915 let nextgoal metasenv metano =
916  let rec aux =
917   function
918      [] -> assert false
919    | [(m,_,_)] when m = metano -> raise NoNextGoal
920    | (m,_,_)::(n,_,_)::_ when m=metano -> n
921    | _::tl -> aux tl
922  in
923   aux metasenv
924 ;;
925
926 let prev_or_next_goal select_goal rendering_window () =
927  let module L = LogicalOperations in
928  let module G = Gdome in
929  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
930   let metasenv =
931    match !ProofEngine.proof with
932       None -> assert false
933     | Some (_,metasenv,_,_) -> metasenv
934   in
935   let metano =
936    match !ProofEngine.goal with
937       None -> assert false
938     | Some m -> m
939   in
940    try
941     ProofEngine.goal := Some (select_goal metasenv metano) ;
942     refresh_sequent rendering_window#proofw
943    with
944       RefreshSequentException e ->
945        output_html outputhtml
946         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
947          "sequent: " ^ Printexc.to_string e ^ "</h1>")
948     | e ->
949        output_html outputhtml
950         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
951 ;;
952
953 exception NotADefinition;;
954
955 let open_ rendering_window () =
956  let inputt = (rendering_window#inputt : GEdit.text) in
957  let oldinputt = (rendering_window#oldinputt : GEdit.text) in
958  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
959  let output = (rendering_window#output : GMathView.math_view) in
960  let proofw = (rendering_window#proofw : GMathView.math_view) in
961   let inputlen = inputt#length in
962   let input = inputt#get_chars 0 inputlen ^ "\n" in
963    try
964     let uri = UriManager.uri_of_string ("cic:" ^ input) in
965      CicTypeChecker.typecheck uri ;
966      let metasenv,bo,ty =
967       match CicEnvironment.get_cooked_obj uri 0 with
968          Cic.Definition (_,bo,ty,_) -> [],bo,ty
969        | Cic.CurrentProof (_,metasenv,bo,ty) -> metasenv,bo,ty
970        | Cic.Axiom _
971        | Cic.Variable _
972        | Cic.InductiveDefinition _ -> raise NotADefinition
973      in
974       ProofEngine.proof :=
975        Some (uri, metasenv, bo, ty) ;
976       ProofEngine.goal := None ;
977       refresh_sequent proofw ;
978       refresh_proof output ;
979       inputt#delete_text 0 inputlen ;
980       ignore(oldinputt#insert_text input oldinputt#length)
981    with
982       RefreshSequentException e ->
983        output_html outputhtml
984         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
985          "sequent: " ^ Printexc.to_string e ^ "</h1>")
986     | RefreshProofException e ->
987        output_html outputhtml
988         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
989          "proof: " ^ Printexc.to_string e ^ "</h1>")
990     | e ->
991        output_html outputhtml
992         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
993 ;;
994
995 let state rendering_window () =
996  let inputt = (rendering_window#inputt : GEdit.text) in
997  let oldinputt = (rendering_window#oldinputt : GEdit.text) in
998  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
999  let output = (rendering_window#output : GMathView.math_view) in
1000  let proofw = (rendering_window#proofw : GMathView.math_view) in
1001   let inputlen = inputt#length in
1002   let input = inputt#get_chars 0 inputlen ^ "\n" in
1003    (* Do something interesting *)
1004    let lexbuf = Lexing.from_string input in
1005     try
1006      while true do
1007       (* Execute the actions *)
1008       match CicTextualParser.main CicTextualLexer.token lexbuf with
1009          None -> ()
1010        | Some expr ->
1011           let _  = CicTypeChecker.type_of_aux' [] [] expr in
1012            ProofEngine.proof :=
1013             Some (UriManager.uri_of_string "cic:/dummy.con",
1014                    [1,[],expr], Cic.Meta (1,[]), expr) ;
1015            ProofEngine.goal := Some 1 ;
1016            refresh_sequent proofw ;
1017            refresh_proof output ;
1018      done
1019     with
1020        CicTextualParser0.Eof ->
1021         inputt#delete_text 0 inputlen ;
1022         ignore(oldinputt#insert_text input oldinputt#length)
1023      | RefreshSequentException e ->
1024         output_html outputhtml
1025          ("<h1 color=\"red\">Exception raised during the refresh of the " ^
1026           "sequent: " ^ Printexc.to_string e ^ "</h1>")
1027      | RefreshProofException e ->
1028         output_html outputhtml
1029          ("<h1 color=\"red\">Exception raised during the refresh of the " ^
1030           "proof: " ^ Printexc.to_string e ^ "</h1>")
1031      | e ->
1032         output_html outputhtml
1033          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1034 ;;
1035
1036 let check rendering_window scratch_window () =
1037  let inputt = (rendering_window#inputt : GEdit.text) in
1038  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
1039  let output = (rendering_window#output : GMathView.math_view) in
1040  let proofw = (rendering_window#proofw : GMathView.math_view) in
1041   let inputlen = inputt#length in
1042   let input = inputt#get_chars 0 inputlen ^ "\n" in
1043   let curi,metasenv =
1044    match !ProofEngine.proof with
1045       None -> UriManager.uri_of_string "cic:/dummy.con", []
1046     | Some (curi,metasenv,_,_) -> curi,metasenv
1047   in
1048   let context,names_context =
1049    let context =
1050     match !ProofEngine.goal with
1051        None -> []
1052      | Some metano ->
1053         let (_,canonical_context,_) =
1054          List.find (function (m,_,_) -> m=metano) metasenv
1055         in
1056          canonical_context
1057    in
1058     context,
1059     List.map
1060      (function
1061          Some (n,_) -> Some n
1062        | None -> None
1063      ) context
1064   in
1065    (* Do something interesting *)
1066    let lexbuf = Lexing.from_string input in
1067     try
1068      while true do
1069       (* Execute the actions *)
1070       match
1071        CicTextualParserContext.main curi names_context metasenv
1072         CicTextualLexer.token lexbuf
1073       with
1074          None -> ()
1075        | Some (metasenv',expr) ->
1076           try
1077            let ty  = CicTypeChecker.type_of_aux' metasenv' context expr in
1078             let mml = mml_of_cic_term 111 (Cic.Cast (expr,ty)) in
1079              scratch_window#show () ;
1080              scratch_window#mmlwidget#load_tree ~dom:mml
1081           with
1082            e ->
1083             print_endline ("? " ^ CicPp.ppterm expr) ;
1084             raise e
1085      done
1086     with
1087        CicTextualParser0.Eof -> ()
1088      | e ->
1089        output_html outputhtml
1090         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1091 ;;
1092
1093 exception NoObjectsLocated;;
1094
1095 let user_uri_choice uris =
1096  let uri =
1097   match uris with
1098      [] -> raise NoObjectsLocated
1099    | [uri] -> uri
1100    | uris ->
1101       let choice =
1102        GToolbox.question_box ~title:"Ambiguous result."
1103         ~buttons:uris ~default:1
1104         "Ambiguous result. Please, choose one."
1105       in
1106        List.nth uris (choice-1)
1107  in
1108   String.sub uri 4 (String.length uri - 4)
1109 ;;
1110
1111 let locate rendering_window () =
1112  let inputt = (rendering_window#inputt : GEdit.text) in
1113  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
1114   let inputlen = inputt#length in
1115   let input = inputt#get_chars 0 inputlen in
1116    try
1117     match Str.split (Str.regexp "[ \t]+") input with
1118        [] -> ()
1119      | head :: tail ->
1120         inputt#delete_text 0 inputlen ;
1121         let MathQL.MQRefs uris, html = MQueryGenerator.locate head in
1122          output_html outputhtml html ;
1123          let uri' = user_uri_choice uris in
1124           ignore ((inputt#insert_text uri') ~pos:0)
1125    with
1126     e ->
1127      output_html outputhtml
1128       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
1129 ;;
1130
1131 let backward rendering_window () =
1132  let outputhtml = (rendering_window#outputhtml : GHtml.xmhtml) in
1133  let inputt = (rendering_window#inputt : GEdit.text) in
1134   let inputlen = inputt#length in
1135   let input = inputt#get_chars 0 inputlen in
1136   let level = int_of_string input in
1137   let metasenv =
1138    match !ProofEngine.proof with
1139       None -> assert false
1140     | Some (_,metasenv,_,_) -> metasenv
1141   in
1142    try
1143     match !ProofEngine.goal with
1144        None -> ()
1145      | Some metano ->
1146         let (_, ey ,ty) = List.find (function (m,_,_) -> m=metano) metasenv in
1147          let MathQL.MQRefs uris, html =
1148           MQueryGenerator.backward metasenv ey ty level
1149          in
1150           output_html outputhtml html ;
1151           let uri' = user_uri_choice uris in
1152            inputt#delete_text 0 inputlen ;
1153            ignore ((inputt#insert_text uri') ~pos:0)
1154     with
1155      e -> 
1156       output_html outputhtml 
1157        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
1158 ;;
1159       
1160 let choose_selection
1161      (mmlwidget : GMathView.math_view) (element : Gdome.element option)
1162 =
1163  let module G = Gdome in
1164   let rec aux element =
1165    if element#hasAttributeNS
1166        ~namespaceURI:helmns
1167        ~localName:(G.domString "xref")
1168    then
1169      mmlwidget#set_selection (Some element)
1170    else
1171       match element#get_parentNode with
1172          None -> assert false
1173        (*CSC: OCAML DIVERGES!
1174        | Some p -> aux (new G.element_of_node p)
1175        *)
1176        | Some p -> aux (new Gdome.element_of_node p)
1177   in
1178    match element with
1179      Some x -> aux x
1180    | None   -> mmlwidget#set_selection None
1181 ;;
1182
1183 (* STUFF TO BUILD THE GTK INTERFACE *)
1184
1185 (* Stuff for the widget settings *)
1186
1187 let export_to_postscript (output : GMathView.math_view) () =
1188  output#export_to_postscript ~filename:"output.ps" ();
1189 ;;
1190
1191 let activate_t1 (output : GMathView.math_view) button_set_anti_aliasing
1192  button_set_kerning button_set_transparency button_export_to_postscript
1193  button_t1 ()
1194 =
1195  let is_set = button_t1#active in
1196   output#set_font_manager_type
1197    (if is_set then `font_manager_t1 else `font_manager_gtk) ;
1198   if is_set then
1199    begin
1200     button_set_anti_aliasing#misc#set_sensitive true ;
1201     button_set_kerning#misc#set_sensitive true ;
1202     button_set_transparency#misc#set_sensitive true ;
1203     button_export_to_postscript#misc#set_sensitive true ;
1204    end
1205   else
1206    begin
1207     button_set_anti_aliasing#misc#set_sensitive false ;
1208     button_set_kerning#misc#set_sensitive false ;
1209     button_set_transparency#misc#set_sensitive false ;
1210     button_export_to_postscript#misc#set_sensitive false ;
1211    end
1212 ;;
1213
1214 let set_anti_aliasing output button_set_anti_aliasing () =
1215  output#set_anti_aliasing button_set_anti_aliasing#active
1216 ;;
1217
1218 let set_kerning output button_set_kerning () =
1219  output#set_kerning button_set_kerning#active
1220 ;;
1221
1222 let set_transparency output button_set_transparency () =
1223  output#set_transparency button_set_transparency#active
1224 ;;
1225
1226 let changefont output font_size_spinb () =
1227  output#set_font_size font_size_spinb#value_as_int
1228 ;;
1229
1230 let set_log_verbosity output log_verbosity_spinb () =
1231  output#set_log_verbosity log_verbosity_spinb#value_as_int
1232 ;;
1233
1234 class settings_window (output : GMathView.math_view) sw
1235  button_export_to_postscript selection_changed_callback
1236 =
1237  let settings_window = GWindow.window ~title:"GtkMathView settings" () in
1238  let vbox =
1239   GPack.vbox ~packing:settings_window#add () in
1240  let table =
1241   GPack.table
1242    ~rows:1 ~columns:3 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
1243    ~border_width:5 ~packing:vbox#add () in
1244  let button_t1 =
1245   GButton.toggle_button ~label:"activate t1 fonts"
1246    ~packing:(table#attach ~left:0 ~top:0) () in
1247  let button_set_anti_aliasing =
1248   GButton.toggle_button ~label:"set_anti_aliasing"
1249    ~packing:(table#attach ~left:0 ~top:1) () in
1250  let button_set_kerning =
1251   GButton.toggle_button ~label:"set_kerning"
1252    ~packing:(table#attach ~left:1 ~top:1) () in
1253  let button_set_transparency =
1254   GButton.toggle_button ~label:"set_transparency"
1255    ~packing:(table#attach ~left:2 ~top:1) () in
1256  let table =
1257   GPack.table
1258    ~rows:2 ~columns:2 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
1259    ~border_width:5 ~packing:vbox#add () in
1260  let font_size_label =
1261   GMisc.label ~text:"font size:"
1262    ~packing:(table#attach ~left:0 ~top:0 ~expand:`NONE) () in
1263  let font_size_spinb =
1264   let sadj =
1265    GData.adjustment ~value:14.0 ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
1266   in
1267    GEdit.spin_button 
1268     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:0 ~fill:`NONE) () in
1269  let log_verbosity_label =
1270   GMisc.label ~text:"log verbosity:"
1271    ~packing:(table#attach ~left:0 ~top:1) () in
1272  let log_verbosity_spinb =
1273   let sadj =
1274    GData.adjustment ~value:0.0 ~lower:0.0 ~upper:3.0 ~step_incr:1.0 ()
1275   in
1276    GEdit.spin_button 
1277     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:1) () in
1278  let hbox =
1279   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1280  let closeb =
1281   GButton.button ~label:"Close"
1282    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1283 object(self)
1284  method show = settings_window#show
1285  initializer
1286   button_set_anti_aliasing#misc#set_sensitive false ;
1287   button_set_kerning#misc#set_sensitive false ;
1288   button_set_transparency#misc#set_sensitive false ;
1289   (* Signals connection *)
1290   ignore(button_t1#connect#clicked
1291    (activate_t1 output button_set_anti_aliasing button_set_kerning
1292     button_set_transparency button_export_to_postscript button_t1)) ;
1293   ignore(font_size_spinb#connect#changed (changefont output font_size_spinb)) ;
1294   ignore(button_set_anti_aliasing#connect#toggled
1295    (set_anti_aliasing output button_set_anti_aliasing));
1296   ignore(button_set_kerning#connect#toggled
1297    (set_kerning output button_set_kerning)) ;
1298   ignore(button_set_transparency#connect#toggled
1299    (set_transparency output button_set_transparency)) ;
1300   ignore(log_verbosity_spinb#connect#changed
1301    (set_log_verbosity output log_verbosity_spinb)) ;
1302   ignore(closeb#connect#clicked settings_window#misc#hide)
1303 end;;
1304
1305 (* Scratch window *)
1306
1307 class scratch_window outputhtml =
1308  let window =
1309   GWindow.window ~title:"MathML viewer" ~border_width:2 () in
1310  let vbox =
1311   GPack.vbox ~packing:window#add () in
1312  let hbox =
1313   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1314  let whdb =
1315   GButton.button ~label:"Whd"
1316    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1317  let reduceb =
1318   GButton.button ~label:"Reduce"
1319    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1320  let simplb =
1321   GButton.button ~label:"Simpl"
1322    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1323  let scrolled_window =
1324   GBin.scrolled_window ~border_width:10
1325    ~packing:(vbox#pack ~expand:true ~padding:5) () in
1326  let mmlwidget =
1327   GMathView.math_view
1328    ~packing:(scrolled_window#add) ~width:400 ~height:280 () in
1329 object(self)
1330  method outputhtml = outputhtml
1331  method mmlwidget = mmlwidget
1332  method show () = window#misc#hide () ; window#show ()
1333  initializer
1334   ignore(mmlwidget#connect#selection_changed (choose_selection mmlwidget)) ;
1335   ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true )) ;
1336   ignore(whdb#connect#clicked (whd_in_scratch self)) ;
1337   ignore(reduceb#connect#clicked (reduce_in_scratch self)) ;
1338   ignore(simplb#connect#clicked (simpl_in_scratch self))
1339 end;;
1340
1341 (* Main window *)
1342
1343 class rendering_window output proofw (label : GMisc.label) =
1344  let window =
1345   GWindow.window ~title:"MathML viewer" ~border_width:2 () in
1346  let hbox0 =
1347   GPack.hbox ~packing:window#add () in
1348  let vbox =
1349   GPack.vbox ~packing:(hbox0#pack ~expand:false ~fill:false ~padding:5) () in
1350  let _ = vbox#pack ~expand:false ~fill:false ~padding:5 label#coerce in
1351  let scrolled_window0 =
1352   GBin.scrolled_window ~border_width:10
1353    ~packing:(vbox#pack ~expand:true ~padding:5) () in
1354  let _ = scrolled_window0#add output#coerce in
1355  let hbox1 =
1356   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1357  let settingsb =
1358   GButton.button ~label:"Settings"
1359    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1360  let button_export_to_postscript =
1361   GButton.button ~label:"export_to_postscript"
1362   ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1363  let qedb =
1364   GButton.button ~label:"Qed"
1365    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1366  let saveb =
1367   GButton.button ~label:"Save"
1368    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1369  let loadb =
1370   GButton.button ~label:"Load"
1371    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1372  let closeb =
1373   GButton.button ~label:"Close"
1374    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1375  let hbox2 =
1376   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1377  let proveitb =
1378   GButton.button ~label:"Prove It"
1379    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
1380  let focusb =
1381   GButton.button ~label:"Focus"
1382    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
1383  let prevgoalb =
1384   GButton.button ~label:"<"
1385    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
1386  let nextgoalb =
1387   GButton.button ~label:">"
1388    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
1389  let oldinputt = GEdit.text ~editable:false ~width:400 ~height:180
1390    ~packing:(vbox#pack ~padding:5) () in
1391  let hbox4 =
1392   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1393  let stateb =
1394   GButton.button ~label:"State"
1395    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1396  let openb =
1397   GButton.button ~label:"Open"
1398    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1399  let checkb =
1400   GButton.button ~label:"Check"
1401    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1402  let locateb =
1403   GButton.button ~label:"Locate"
1404    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1405  let backwardb =
1406   GButton.button ~label:"Backward"
1407    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1408  let inputt = GEdit.text ~editable:true ~width:400 ~height: 100
1409    ~packing:(vbox#pack ~padding:5) () in
1410  let vbox1 =
1411   GPack.vbox ~packing:(hbox0#pack ~expand:false ~fill:false ~padding:5) () in
1412  let scrolled_window1 =
1413   GBin.scrolled_window ~border_width:10
1414    ~packing:(vbox1#pack ~expand:true ~padding:5) () in
1415  let _ = scrolled_window1#add proofw#coerce in
1416  let hbox3 =
1417   GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
1418  let exactb =
1419   GButton.button ~label:"Exact"
1420    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1421  let introsb =
1422   GButton.button ~label:"Intros"
1423    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1424  let applyb =
1425   GButton.button ~label:"Apply"
1426    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1427  let elimintrossimplb =
1428   GButton.button ~label:"ElimIntrosSimpl"
1429    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1430  let elimtypeb =
1431   GButton.button ~label:"ElimType"
1432    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1433  let whdb =
1434   GButton.button ~label:"Whd"
1435    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1436  let reduceb =
1437   GButton.button ~label:"Reduce"
1438    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1439  let simplb =
1440   GButton.button ~label:"Simpl"
1441    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1442  let foldb =
1443   GButton.button ~label:"Fold"
1444    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1445  let cutb =
1446   GButton.button ~label:"Cut"
1447    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1448  let changeb =
1449   GButton.button ~label:"Change"
1450    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1451  let hbox4 =
1452   GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
1453  let letinb =
1454   GButton.button ~label:"Let ... In"
1455    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1456  let ringb =
1457   GButton.button ~label:"Ring"
1458    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1459  let clearbodyb =
1460   GButton.button ~label:"ClearBody"
1461    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1462  let clearb =
1463   GButton.button ~label:"Clear"
1464    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1465  let fourierb =
1466   GButton.button ~label:"Fourier"
1467    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1468  let rewritesimplb =
1469   GButton.button ~label:"RewriteSimpl ->"
1470    ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
1471  let outputhtml =
1472   GHtml.xmhtml
1473    ~source:"<html><body bgColor=\"white\"></body></html>"
1474    ~width:400 ~height: 200
1475    ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5)
1476    ~show:true () in
1477  let scratch_window = new scratch_window outputhtml in
1478 object(self)
1479  method outputhtml = outputhtml
1480  method oldinputt = oldinputt
1481  method inputt = inputt
1482  method output = (output : GMathView.math_view)
1483  method proofw = (proofw : GMathView.math_view)
1484  method show = window#show
1485  initializer
1486   button_export_to_postscript#misc#set_sensitive false ;
1487
1488   (* signal handlers here *)
1489   ignore(output#connect#selection_changed
1490    (function elem -> proofw#unload ; choose_selection output elem)) ;
1491   ignore(proofw#connect#selection_changed (choose_selection proofw)) ;
1492   ignore(closeb#connect#clicked (fun _ -> GMain.Main.quit ())) ;
1493   let settings_window = new settings_window output scrolled_window0
1494    button_export_to_postscript (choose_selection output) in
1495   ignore(settingsb#connect#clicked settings_window#show) ;
1496   ignore(button_export_to_postscript#connect#clicked (export_to_postscript output)) ;
1497   ignore(qedb#connect#clicked (qed self)) ;
1498   ignore(saveb#connect#clicked (save self)) ;
1499   ignore(loadb#connect#clicked (load self)) ;
1500   ignore(proveitb#connect#clicked (proveit self)) ;
1501   ignore(focusb#connect#clicked (focus self)) ;
1502   ignore(prevgoalb#connect#clicked (prev_or_next_goal prevgoal self)) ;
1503   ignore(nextgoalb#connect#clicked (prev_or_next_goal nextgoal self)) ;
1504   ignore(window#event#connect#delete (fun _ -> GMain.Main.quit () ; true )) ;
1505   ignore(stateb#connect#clicked (state self)) ;
1506   ignore(openb#connect#clicked (open_ self)) ;
1507   ignore(checkb#connect#clicked (check self scratch_window)) ;
1508   ignore(locateb#connect#clicked (locate self)) ;
1509   ignore(backwardb#connect#clicked (backward self)) ;
1510   ignore(exactb#connect#clicked (exact self)) ;
1511   ignore(applyb#connect#clicked (apply self)) ;
1512   ignore(elimintrossimplb#connect#clicked (elimintrossimpl self)) ;
1513   ignore(elimtypeb#connect#clicked (elimtype self)) ;
1514   ignore(whdb#connect#clicked (whd self)) ;
1515   ignore(reduceb#connect#clicked (reduce self)) ;
1516   ignore(simplb#connect#clicked (simpl self)) ;
1517   ignore(foldb#connect#clicked (fold self)) ;
1518   ignore(cutb#connect#clicked (cut self)) ;
1519   ignore(changeb#connect#clicked (change self)) ;
1520   ignore(letinb#connect#clicked (letin self)) ;
1521   ignore(ringb#connect#clicked (ring self)) ;
1522   ignore(clearbodyb#connect#clicked (clearbody self)) ;
1523   ignore(clearb#connect#clicked (clear self)) ;
1524   ignore(fourierb#connect#clicked (fourier self)) ;
1525   ignore(rewritesimplb#connect#clicked (rewritesimpl self)) ;
1526   ignore(introsb#connect#clicked (intros self)) ;
1527   Logger.log_callback :=
1528    (Logger.log_to_html ~print_and_flush:(output_html outputhtml))
1529 end;;
1530
1531 (* MAIN *)
1532
1533 let rendering_window = ref None;;
1534
1535 let initialize_everything () =
1536  let module U = Unix in
1537   let output = GMathView.math_view ~width:350 ~height:280 ()
1538   and proofw = GMathView.math_view ~width:400 ~height:275 ()
1539   and label = GMisc.label ~text:"gTopLevel" () in
1540     let rendering_window' =
1541      new rendering_window output proofw label
1542     in
1543      rendering_window := Some rendering_window' ;
1544      rendering_window'#show () ;
1545      GMain.Main.main ()
1546 ;;
1547
1548 let _ =
1549  CicCooking.init () ;
1550  if !usedb then
1551   begin
1552    MQueryGenerator.init () ;
1553    CicTextualParser0.set_locate_object
1554     (function id ->
1555       let MathQL.MQRefs uris, html = MQueryGenerator.locate id in
1556        begin
1557         match !rendering_window with
1558            None -> assert false
1559          | Some rw -> output_html rw#outputhtml html ;
1560        end ;
1561        let uri = 
1562         match uris with
1563            [] ->
1564             (match
1565               (GToolbox.input_string ~title:"Unknown input"
1566                ("No URI matching \"" ^ id ^ "\" found. Please enter its URI"))
1567              with
1568                 None -> None
1569               | Some uri -> Some ("cic:" ^ uri)
1570             )
1571          | [uri] -> Some uri
1572          | _ ->
1573            let choice =
1574             GToolbox.question_box ~title:"Ambiguous input."
1575              ~buttons:uris ~default:1 "Ambiguous input. Please, choose one."
1576            in
1577             if choice > 0 then
1578              Some (List.nth uris (choice - 1))
1579             else
1580              (* No choice from the user *)
1581              None
1582        in
1583         match uri with
1584            Some uri' ->
1585             (* Constant *)
1586             if String.sub uri' (String.length uri' - 4) 4 = ".con" then
1587 (*CSC: what cooking number? Here I always use 0, which may be bugged *)
1588              Some (Cic.Const (UriManager.uri_of_string uri',0))
1589             else
1590              (try
1591                (* Inductive Type *)
1592                let uri'',typeno = CicTextualLexer.indtyuri_of_uri uri' in
1593 (*CSC: what cooking number? Here I always use 0, which may be bugged *)
1594                 Some (Cic.MutInd (uri'',0,typeno))
1595               with
1596                _ ->
1597                 (* Constructor of an Inductive Type *)
1598                 let uri'',typeno,consno =
1599                  CicTextualLexer.indconuri_of_uri uri'
1600                 in
1601 (*CSC: what cooking number? Here I always use 0, which may be bugged *)
1602                  Some (Cic.MutConstruct (uri'',0,typeno,consno))
1603              )
1604          | None -> None
1605     )
1606   end ;
1607  ignore (GtkMain.Main.init ()) ;
1608  initialize_everything () ;
1609  if !usedb then MQueryGenerator.close ();
1610 ;;