]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/content2pres.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / ocaml / cic_transformations / content2pres.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 (*                Andrea Asperti <asperti@cs.unibo.it>                     *)
31 (*                              17/06/2003                                 *)
32 (*                                                                         *)
33 (***************************************************************************)
34
35 let rec split n l =
36   if n = 0 then [],l
37   else let l1,l2 = 
38     split (n-1) (List.tl l) in
39     (List.hd l)::l1,l2
40 ;;
41   
42
43 let is_big_general countterm p =
44   let maxsize = Cexpr2pres.maxsize in
45   let module Con = Content in
46   let rec countp current_size p =
47     if current_size > maxsize then current_size
48     else 
49       let c1 = (countcontext current_size p.Con.proof_context) in
50       if c1 > maxsize then c1
51     else 
52       let c2 = (countapplycontext c1 p.Con.proof_apply_context) in
53       if c2 > maxsize then c2
54     else 
55       countconclude c2 p.Con.proof_conclude
56
57   and 
58     countcontext current_size c =
59       List.fold_left countcontextitem current_size c
60   and
61     countcontextitem current_size e =
62       if current_size > maxsize then maxsize
63       else 
64         (match e with
65           `Declaration d -> 
66             (match d.Con.dec_name with
67                Some s -> current_size + 4 + (String.length s)
68              | None -> prerr_endline "NO NAME!!"; assert false)
69         | `Hypothesis h ->
70             (match h.Con.dec_name with
71                 Some s -> current_size + 4 + (String.length s)
72               | None -> prerr_endline "NO NAME!!"; assert false) 
73         | `Proof p -> countp current_size p
74         | `Definition d -> 
75             (match d.Con.def_name with
76                 Some s -> 
77                   let c1 = (current_size + 4 + (String.length s)) in
78                   (countterm c1 d.Con.def_term)
79               | None -> 
80                   prerr_endline "NO NAME!!"; assert false) 
81         | `Joint ho -> maxsize + 1) (* we assume is big *)
82   and 
83     countapplycontext current_size ac =
84       List.fold_left countp current_size ac
85   and 
86     countconclude current_size co =
87       if current_size > maxsize then current_size
88       else
89         let c1 = countargs current_size co.Con.conclude_args in
90         if c1 > maxsize then c1 
91       else 
92         (match co.Con.conclude_conclusion with
93            Some concl ->  countterm c1 concl
94         | None -> c1)
95   and 
96     countargs current_size args =
97       List.fold_left countarg current_size args
98   and
99     countarg current_size arg =
100       if current_size > maxsize then current_size
101       else 
102         (match arg with 
103            Con.Aux _ -> current_size
104          | Con.Premise prem -> 
105              (match prem.Con.premise_binder with
106                 Some s -> current_size + (String.length s)
107               | None -> current_size + 7) 
108          | Con.Lemma lemma -> 
109              current_size + (String.length lemma.Con.lemma_name)
110          | Con.Term t -> countterm current_size t
111          | Con.ArgProof p -> countp current_size p
112          | Con.ArgMethod s -> (maxsize + 1)) in
113   let size = (countp 0 p) in
114   (size > maxsize)
115 ;;
116
117 let is_big = is_big_general (Cexpr2pres.countterm)
118 ;;
119
120 let get_xref =
121     let module Con = Content in
122       function
123         `Declaration d  
124       | `Hypothesis d -> d.Con.dec_id
125       | `Proof p -> p.Con.proof_id
126       | `Definition d -> d.Con.def_id
127       | `Joint jo -> jo.Con.joint_id
128 ;;
129
130 let make_row ?(attrs=[]) items concl =
131   let module P = Mpresentation in
132     (match concl with 
133        P.Mtable _ -> (* big! *)
134          P.Mtable (attrs@[None,"align","baseline 1"; None,"equalrows","false";
135           None,"columnalign","left"],
136            [P.Mtr([],[P.Mtd ([],P.Mrow([],items))]);
137             P.Mtr ([],[P.Mtd ([],P.indented concl)])])
138      | _ ->  (* small *)
139        P.Mrow(attrs,items@[P.Mspace([None,"width","0.1cm"]);concl]))
140 ;;
141
142 let make_concl ?(attrs=[]) verb concl =
143   let module P = Mpresentation in
144     (match concl with 
145        P.Mtable _ -> (* big! *)
146          P.Mtable (attrs@[None,"align","baseline 1"; None,"equalrows","false";
147           None,"columnalign","left"],
148            [P.Mtr([],[P.Mtd ([],P.Mtext([None,"mathcolor","Red"],verb))]);
149             P.Mtr ([],[P.Mtd ([],P.indented concl)])])
150      | _ ->  (* small *)
151        P.Mrow(attrs,
152         [P.Mtext([None,"mathcolor","Red"],verb); 
153          P.Mspace([None,"width","0.1cm"]);
154          concl]))
155 ;;
156
157 let make_args_for_apply term2pres args =
158  let module Con = Content in
159  let module P = Mpresentation in
160  let make_arg_for_apply is_first arg row = 
161   let res =
162    match arg with 
163       Con.Aux n -> assert false
164     | Con.Premise prem -> 
165         let name = 
166           (match prem.Con.premise_binder with
167              None -> "previous"
168            | Some s -> s) in
169         P.Mi([],name)::row
170     | Con.Lemma lemma -> 
171          P.Mi([],lemma.Con.lemma_name)::row 
172     | Con.Term t -> 
173         if is_first then
174           (term2pres t)::row
175         else P.Mi([],"_")::row
176     | Con.ArgProof _ 
177     | Con.ArgMethod _ -> 
178        P.Mi([],"_")::row
179   in
180    if is_first then res else P.smallskip::res
181  in
182   match args with 
183     hd::tl -> 
184       make_arg_for_apply true hd 
185         (List.fold_right (make_arg_for_apply false) tl [])
186   | _ -> assert false
187 ;;
188
189 let rec justification term2pres p = 
190   let module Con = Content in
191   let module P = Mpresentation in
192   if ((p.Con.proof_conclude.Con.conclude_method = "Exact") or
193      ((p.Con.proof_context = []) &
194       (p.Con.proof_apply_context = []) &
195       (p.Con.proof_conclude.Con.conclude_method = "Apply"))) then
196     let pres_args = 
197       make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args in
198     P.Mrow([],
199       P.Mtext([None,"mathcolor","Red"],"by")::P.Mspace([None,"width","0.1cm"])::
200       P.Mo([],"(")::pres_args@[P.Mo([],")")]) 
201   else proof2pres term2pres p 
202      
203 and proof2pres term2pres p =
204   let rec proof2pres p =
205     let module Con = Content in
206     let module P = Mpresentation in
207       let indent = 
208         let is_decl e = 
209           (match e with 
210              `Declaration _
211            | `Hypothesis _ -> true
212            | _ -> false) in
213         ((List.filter is_decl p.Con.proof_context) != []) in 
214       let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
215       let concl = 
216         (match p.Con.proof_conclude.Con.conclude_conclusion with
217            None -> None
218          | Some t -> Some (term2pres t)) in
219       let body =
220           let presconclude = 
221             conclude2pres p.Con.proof_conclude indent omit_conclusion in
222           let presacontext = 
223             acontext2pres p.Con.proof_apply_context presconclude indent in
224           context2pres p.Con.proof_context presacontext in
225       match p.Con.proof_name with
226         None -> body
227       | Some name ->
228           let action = 
229            match concl with
230               None -> body
231 (*
232                P.Maction
233                  ([None,"actiontype","toggle" ; None,"selection","1"],
234                   [P.Mtext [] "proof" ; body])
235 *)
236             | Some ac ->
237                P.Maction
238                  ([None,"actiontype","toggle" ; None,"selection","1"],
239                   [(make_concl "proof of" ac); body])
240           in
241           P.Mtable ([None,"align","baseline 1"; None,"equalrows","false";
242               None,"columnalign","left"],
243             [P.Mtr ([],[P.Mtd ([],P.Mfenced([],[P.Mtext ([],name)]))]);
244              P.Mtr ([],[P.Mtd ([], P.indented action)])])
245 (*
246           P.Mtable ([None,"align","baseline 1"; None,"equalrows","false";
247               None,"columnalign","left";Some "helm", "xref", p.Con.proof_id],
248             [P.Mtr ([],[P.Mtd ([],P.Mfenced([],[P.Mtext ([],name)]))]);
249              P.Mtr ([],[P.Mtd ([], P.indented action)])]) *)
250
251   and context2pres c continuation =
252     (* we generate a subtable for each context element, for selection
253        purposes 
254        The table generated by the head-element does not have an xref;
255        the whole context-proof is already selectable *)
256     let module P = Mpresentation in
257     match c with
258       [] -> continuation
259     | hd::tl -> 
260         let continuation' =
261           List.fold_right
262             (fun ce continuation ->
263               let xref = get_xref ce in
264               P.Mtable([None,"align","baseline 1"; None,"equalrows","false";
265                None,"columnalign","left"; Some "helm", "xref", xref ],
266                 [P.Mtr([Some "helm", "xref", "ce_"^xref],[P.Mtd ([],ce2pres ce)]);
267                  P.Mtr([],[P.Mtd ([], continuation)])])) tl continuation in
268          let hd_xref= get_xref hd in
269          P.Mtable([None,"align","baseline 1"; None,"equalrows","false";
270            None,"columnalign","left"],
271              [P.Mtr([Some "helm", "xref", "ce_"^hd_xref],
272                [P.Mtd ([],ce2pres hd)]);
273              P.Mtr([],[P.Mtd ([], continuation')])])
274          
275   and ce2pres =
276     let module P = Mpresentation in
277     let module Con = Content in
278       function
279         `Declaration d -> 
280           (match d.Con.dec_name with
281               Some s ->
282                 let ty = term2pres d.Con.dec_type in
283                 P.Mrow ([],
284                   [P.Mtext([None,"mathcolor","Red"],"Assume");
285                    P.Mspace([None,"width","0.1cm"]);
286                    P.Mi([],s);
287                    P.Mtext([],":");
288                    ty])
289             | None -> 
290                 prerr_endline "NO NAME!!"; assert false)
291       | `Hypothesis h ->
292           (match h.Con.dec_name with
293               Some s ->
294                 let ty = term2pres h.Con.dec_type in
295                 P.Mrow ([],
296                   [P.Mtext([None,"mathcolor","Red"],"Suppose");
297                    P.Mspace([None,"width","0.1cm"]);
298                    P.Mo([],"(");
299                    P.Mi ([],s);
300                    P.Mo([],")");
301                    P.Mspace([None,"width","0.1cm"]);
302                    ty])
303             | None -> 
304                 prerr_endline "NO NAME!!"; assert false) 
305       | `Proof p -> 
306            (match  p.Con.proof_name with
307               Some "w" -> prerr_endline ("processing w");
308             | _ -> ());
309            proof2pres p 
310       | `Definition d -> 
311            (match d.Con.def_name with
312               Some s ->
313                 let term = term2pres d.Con.def_term in
314                 P.Mrow ([],
315                   [P.Mtext([],"Let ");
316                    P.Mi([],s);
317                    P.Mtext([]," = ");
318                    term])
319             | None -> 
320                 prerr_endline "NO NAME!!"; assert false) 
321       | `Joint ho -> 
322             P.Mtext ([],"jointdef")
323
324   and acontext2pres ac continuation indent =
325     let module Con = Content in
326     let module P = Mpresentation in
327     List.fold_right
328       (fun p continuation ->
329          let hd = 
330            if indent then
331              P.indented (proof2pres p)
332            else 
333              proof2pres p in
334          P.Mtable([None,"align","baseline 1"; None,"equalrows","false";
335           None,"columnalign","left"; Some "helm","xref",p.Con.proof_id],
336            [P.Mtr([Some "helm","xref","ace_"^p.Con.proof_id],[P.Mtd ([],hd)]);
337             P.Mtr([],[P.Mtd ([], continuation)])])) ac continuation 
338
339   and conclude2pres conclude indent omit_conclusion =
340     let module Con = Content in
341     let module P = Mpresentation in
342     let tconclude_body = 
343       match conclude.Con.conclude_conclusion with
344         Some t when not omit_conclusion ->
345           let concl = (term2pres t) in 
346           if conclude.Con.conclude_method = "BU_Conversion" then
347             make_concl "that is equivalent to" concl
348           else  
349             let conclude_body = conclude_aux conclude in
350             let ann_concl = 
351               if conclude.Con.conclude_method = "TD_Conversion" then
352                  make_concl "that is equivalent to" concl 
353               else make_concl "we conclude" concl in
354             P.Mtable ([None,"align","baseline 1"; None,"equalrows","false";
355               None,"columnalign","left"],
356                 [P.Mtr ([],[P.Mtd ([],conclude_body)]);
357                  P.Mtr ([],[P.Mtd ([],ann_concl)])])
358       | _ -> conclude_aux conclude in
359     if indent then 
360       P.indented (P.Mrow ([Some "helm", "xref", conclude.Con.conclude_id],
361                     [tconclude_body]))
362     else 
363       P.Mrow ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
364
365
366   and conclude_aux conclude =
367     let module Con = Content in
368     let module P = Mpresentation in
369     if conclude.Con.conclude_method = "TD_Conversion" then
370       let expected = 
371         (match conclude.Con.conclude_conclusion with 
372            None -> P.Mtext([],"NO EXPECTED!!!")
373          | Some c -> term2pres c) in
374       let subproof = 
375         (match conclude.Con.conclude_args with
376           [Con.ArgProof p] -> p
377          | _ -> assert false) in
378       let synth = 
379         (match subproof.Con.proof_conclude.Con.conclude_conclusion with
380            None -> P.Mtext([],"NO SYNTH!!!")
381          | Some c -> (term2pres c)) in
382       P.Mtable 
383         ([None,"align","baseline 1"; None,"equalrows","false"; None,"columnalign","left"],
384         [P.Mtr([],[P.Mtd([],make_concl "we must prove" expected)]);
385          P.Mtr([],[P.Mtd([],make_concl "or equivalently" synth)]);
386          P.Mtr([],[P.Mtd([],proof2pres subproof)])])
387     else if conclude.Con.conclude_method = "BU_Conversion" then
388       assert false
389     else if conclude.Con.conclude_method = "Exact" then
390       let arg = 
391         (match conclude.Con.conclude_args with 
392            [Con.Term t] -> term2pres t
393          | _ -> assert false) in
394       (match conclude.Con.conclude_conclusion with 
395          None ->
396           P.Mrow []
397            [P.Mtext [None, "mathcolor", "red"] "Consider" ; P.smallskip; arg]
398        | Some c -> let conclusion = term2pres c in
399           make_row 
400             [arg; P.Mspace([None,"width","0.1cm"]);P.Mtext([],"proves")]
401             conclusion
402        )
403     else if conclude.Con.conclude_method = "Intros+LetTac" then
404       (match conclude.Con.conclude_args with
405          [Con.ArgProof p] -> proof2pres p
406        | _ -> assert false)
407 (* OLD CODE 
408       let conclusion = 
409       (match conclude.Con.conclude_conclusion with 
410          None -> P.Mtext([],"NO Conclusion!!!")
411        | Some c -> term2pres c) in
412       (match conclude.Con.conclude_args with
413          [Con.ArgProof p] -> 
414            P.Mtable 
415             ([None,"align","baseline 1"; None,"equalrows","false";
416               None,"columnalign","left"],
417               [P.Mtr([],[P.Mtd([],proof2pres p)]);
418                P.Mtr([],[P.Mtd([],
419                 (make_concl "we proved 1" conclusion))])]);
420        | _ -> assert false)
421 *)
422     else if (conclude.Con.conclude_method = "ByInduction") then
423       byinduction conclude
424     else if (conclude.Con.conclude_method = "Exists") then
425       exists conclude
426     else if (conclude.Con.conclude_method = "AndInd") then
427       andind conclude
428     else if (conclude.Con.conclude_method = "Rewrite") then
429       let justif = 
430         (match (List.nth conclude.Con.conclude_args 6) with
431            Con.ArgProof p -> justification term2pres p
432          | _ -> assert false) in
433       let term1 = 
434         (match List.nth conclude.Con.conclude_args 2 with
435            Con.Term t -> term2pres t
436          | _ -> assert false) in 
437       let term2 = 
438         (match List.nth conclude.Con.conclude_args 5 with
439            Con.Term t -> term2pres t
440          | _ -> assert false) in
441       P.Mtable ([None,"align","baseline 1";None,"equalrows","false";
442         None,"columnalign","left"], 
443          [P.Mtr ([],[P.Mtd ([],P.Mrow([],[
444           P.Mtext([None,"mathcolor","Red"],"rewrite");
445           P.Mspace([None,"width","0.1cm"]);term1;
446           P.Mspace([None,"width","0.1cm"]);
447           P.Mtext([None,"mathcolor","Red"],"with");
448           P.Mspace([None,"width","0.1cm"]);term2]))]);
449           P.Mtr ([],[P.Mtd ([],P.indented justif)])]);
450 (* OLD CODE   
451       let conclusion = 
452         (match conclude.Con.conclude_conclusion with 
453            None -> P.Mtext([],"NO Conclusion!!!")
454          | Some c -> term2pres c) in
455       P.Mtable ([None,"align","baseline 1";None,"equalrows","false";
456             None,"columnalign","left"],
457              [P.Mtr ([],[P.Mtd ([],P.Mrow([],[
458                P.Mtext([None,"mathcolor","Red"],"rewrite");
459                P.Mspace([None,"width","0.1cm"]);term1;
460                P.Mspace([None,"width","0.1cm"]);
461                P.Mtext([None,"mathcolor","Red"],"with");
462                P.Mspace([None,"width","0.1cm"]);term2]))]);
463               P.Mtr ([],[P.Mtd ([],P.indented justif)]);
464               P.Mtr ([],[P.Mtd ([],make_concl "we proved 2" conclusion)])]) *)
465     else if conclude.Con.conclude_method = "Apply" then
466       let pres_args = 
467         make_args_for_apply term2pres conclude.Con.conclude_args in
468       P.Mrow([],
469         P.Mtext([None,"mathcolor","Red"],"by")::
470         P.Mspace([None,"width","0.1cm"])::
471         P.Mo([],"(")::pres_args@[P.Mo([],")")])
472 (* OLD CODE 
473       let by = 
474          P.Mrow([],
475            P.Mtext([None,"mathcolor","Red"],"by")::P.Mspace([None,"width","0.1cm"])::
476            P.Mo([],"(")::pres_args@[P.Mo([],")")]) in 
477       match conclude.Con.conclude_conclusion with
478         None -> P.Mrow([],[P.Mtext([],"QUA");by])
479       | Some t ->
480          let concl = (term2pres t) in
481          let ann_concl = make_concl "we proved 3" concl in
482          P.Mtable ([None,"align","baseline 1"; None,"equalrows","false";
483             None,"columnalign","left"; 
484             Some "helm", "xref", conclude.Con.conclude_id],
485              [P.Mtr ([],[P.Mtd ([],by)]);
486               P.Mtr ([],[P.Mtd ([],ann_concl)])]) *)
487     else 
488       P.Mtable 
489         ([None,"align","baseline 1"; None,"equalrows","false"; None,"columnalign","left"],
490          [P.Mtr ([],[P.Mtd ([],P.Mtext([],"Apply method" ^ conclude.Con.conclude_method ^ " to"))]);
491           P.Mtr ([],
492            [P.Mtd ([], 
493              (P.indented 
494                (P.Mtable 
495                  ([None,"align","baseline 1"; None,"equalrows","false";
496                    None,"columnalign","left"],
497                   args2pres conclude.Con.conclude_args))))])]) 
498 (* OLD CODE 
499      match conclude.Con.conclude_conclusion with
500        None -> body
501      | Some t ->
502          let concl = (term2pres t) in
503          let ann_concl = make_concl "we proved 4" concl in
504          P.Mtable ([None,"align","baseline 1"; None,"equalrows","false";
505             None,"columnalign","left"],
506              [P.Mtr ([],[P.Mtd ([],body)]);
507               P.Mtr ([],[P.Mtd ([],ann_concl)])]) *)
508
509   and args2pres l =
510     let module P = Mpresentation in
511     List.map 
512      (function a -> P.Mtr ([], [P.Mtd ([], arg2pres a)])) l
513
514   and arg2pres =
515     let module P = Mpresentation in
516     let module Con = Content in
517     function
518         Con.Aux n -> 
519           P.Mtext ([],"aux " ^ n)
520       | Con.Premise prem -> 
521           P.Mtext ([],"premise")
522       | Con.Lemma lemma ->
523           P.Mtext ([],"lemma")
524       | Con.Term t -> 
525           term2pres t
526       | Con.ArgProof p ->
527         proof2pres p 
528       | Con.ArgMethod s -> 
529          P.Mtext ([],"method") 
530  
531    and byinduction conclude =
532      let module P = Mpresentation in
533      let module Con = Content in
534      let proof_conclusion = 
535        (match conclude.Con.conclude_conclusion with
536           None -> P.Mtext([],"No conclusion???")
537         | Some t -> term2pres t) in
538      let inductive_arg,args_for_cases = 
539        (match conclude.Con.conclude_args with
540            Con.Aux(n)::_::tl ->
541              let l1,l2 = split (int_of_string n) tl in
542              let last_pos = (List.length l2)-1 in
543              List.nth l2 last_pos,l1
544          | _ -> assert false) in
545      let induction_on =
546        let arg = 
547          (match inductive_arg with
548             Con.Aux n -> 
549               P.Mtext ([],"an aux???")
550            | Con.Premise prem ->
551               (match prem.Con.premise_binder with
552                  None -> P.Mtext ([],"the previous result")
553                | Some n -> P.Mi([],n))
554            | Con.Lemma lemma -> P.Mi([],lemma.Con.lemma_name)
555            | Con.Term t -> 
556                term2pres t
557            | Con.ArgProof p ->
558                P.Mtext ([],"a proof???")
559            | Con.ArgMethod s -> 
560                P.Mtext ([],"a method???")) in
561         (make_concl "we proceede by induction on" arg) in
562      let to_prove =
563         (make_concl "to prove" proof_conclusion) in
564      P.Mtable 
565        ([None,"align","baseline 1"; None,"equalrows","false"; 
566          None,"columnalign","left"],
567           P.Mtr ([],[P.Mtd ([],induction_on)])::
568           P.Mtr ([],[P.Mtd ([],to_prove)])::
569           (make_cases args_for_cases))
570
571     and make_cases args_for_cases =
572     let module P = Mpresentation in
573     List.map 
574       (fun p -> P.Mtr ([],[P.Mtd ([],make_case p)])) args_for_cases
575
576     and make_case =  
577       let module P = Mpresentation in
578       let module Con = Content in
579       function 
580         Con.ArgProof p ->
581           let name =
582             (match p.Con.proof_name with
583                None -> P.Mtext([],"no name for case!!")
584              | Some n -> P.Mi([],n)) in
585           let indhyps,args =
586              List.partition 
587                (function
588                    `Hypothesis h -> h.Con.dec_inductive
589                  | _ -> false) p.Con.proof_context in
590           let pattern_aux =
591              List.fold_right
592                (fun e p -> 
593                   let dec  = 
594                     (match e with 
595                        `Declaration h 
596                      | `Hypothesis h -> 
597                          let name = 
598                            (match h.Con.dec_name with
599                               None -> "NO NAME???"
600                            | Some n ->n) in
601                          [P.Mspace([None,"width","0.1cm"]);
602                           P.Mi ([],name);
603                           P.Mtext([],":");
604                           (term2pres h.Con.dec_type)]
605                      | _ -> [P.Mtext ([],"???")]) in
606                   dec@p) args [] in
607           let pattern = 
608             P.Mtr ([],[P.Mtd ([],P.Mrow([],
609                P.Mtext([],"Case")::P.Mspace([None,"width","0.1cm"])::name::pattern_aux@
610                 [P.Mspace([None,"width","0.1cm"]);
611                  P.Mtext([],"->")]))]) in
612           let subconcl = 
613             (match p.Con.proof_conclude.Con.conclude_conclusion with
614                None -> P.Mtext([],"No conclusion!!!") 
615              | Some t -> term2pres t) in
616           let asubconcl =
617              P.Mtr([],[P.Mtd([],
618               P.indented (make_concl "the thesis becomes" subconcl))]) in
619           let induction_hypothesis = 
620             (match indhyps with
621               [] -> []
622             | _ -> 
623                let text =
624                  P.Mtr([],[P.Mtd([], P.indented 
625                  (P.Mtext([],"by induction hypothesis we know:")))]) in
626                let make_hyp =
627                  function 
628                    `Hypothesis h ->
629                      let name = 
630                        (match h.Con.dec_name with
631                           None -> "no name"
632                         | Some s -> s) in
633                      P.indented (P.Mrow ([],
634                        [P.Mo([],"(");
635                         P.Mi ([],name);
636                         P.Mo([],")");
637                         P.Mspace([None,"width","0.1cm"]);
638                         term2pres h.Con.dec_type]))
639                    | _ -> assert false in
640                let hyps = 
641                  List.map 
642                    (function ce -> P.Mtr ([], [P.Mtd ([], make_hyp ce)])) 
643                     indhyps in
644                text::hyps) in          
645           (* let acontext = 
646                acontext2pres_old p.Con.proof_apply_context true in *)
647           let body = conclude2pres p.Con.proof_conclude true false in
648           let presacontext = 
649             P.Maction([None,"actiontype","toggle" ; None,"selection","1"],
650               [P.indented (P.Mtext([None,"mathcolor","Red"],"Proof"));
651                acontext2pres p.Con.proof_apply_context body true]) in
652           P.Mtable ([None,"align","baseline 1"; None,"equalrows","false";
653              None,"columnalign","left"],
654              pattern::asubconcl::induction_hypothesis@
655               [P.Mtr([],[P.Mtd([],presacontext)])])
656        | _ -> assert false 
657
658      and andind conclude =
659        let module P = Mpresentation in
660        let module Con = Content in
661        let proof_conclusion = 
662          (match conclude.Con.conclude_conclusion with
663             None -> P.Mtext([],"No conclusion???")
664           | Some t -> term2pres t) in
665        let proof,case_arg = 
666          (match conclude.Con.conclude_args with
667              [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
668            | _ -> assert false;
669              (* 
670              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
671              assert false *)) in
672        let arg = 
673          (match case_arg with
674              Con.Aux n -> assert false
675            | Con.Premise prem ->
676               (match prem.Con.premise_binder with
677                  None -> []
678                | Some n -> [P.Mtext([],"by");P.smallskip;P.Mi([],n)])
679            | Con.Lemma lemma -> 
680                [P.Mtext([],"by");P.smallskip;P.Mi([],lemma.Con.lemma_name)]
681            | _ -> assert false) in
682        match proof.Con.proof_context with
683          `Hypothesis hyp1::`Hypothesis hyp2::tl ->
684             let get_name hyp =
685               (match hyp.Con.dec_name with
686                 None -> "_"
687               | Some s -> s) in
688             let preshyp1 = 
689               P.Mrow ([],
690                [P.Mtext([],"(");
691                 P.Mi([],get_name hyp1);
692                 P.Mtext([],")");
693                 P.smallskip;
694                 term2pres hyp1.Con.dec_type]) in
695             let preshyp2 = 
696               P.Mrow ([],
697                [P.Mtext([],"(");
698                 P.Mi([],get_name hyp2);
699                 P.Mtext([],")");
700                 P.smallskip;
701                 term2pres hyp2.Con.dec_type]) in
702             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
703             let body = conclude2pres proof.Con.proof_conclude false true in
704             let presacontext = 
705               acontext2pres proof.Con.proof_apply_context body false in
706             P.Mtable 
707               ([None,"align","baseline 1"; None,"equalrows","false"; 
708                 None,"columnalign","left"],
709                [P.Mtr ([],[P.Mtd ([],
710                  P.Mrow([],arg@[P.smallskip;P.Mtext([],"we have")]))]);
711                 P.Mtr ([],[P.Mtd ([],preshyp1)]);
712                 P.Mtr ([],[P.Mtd ([],P.Mtext([],"and"))]);
713                 P.Mtr ([],[P.Mtd ([],preshyp2)]);
714                 P.Mtr ([],[P.Mtd ([],presacontext)])]);
715          | _ -> assert false
716
717      and exists conclude =
718        let module P = Mpresentation in
719        let module Con = Content in
720        let proof_conclusion = 
721          (match conclude.Con.conclude_conclusion with
722             None -> P.Mtext([],"No conclusion???")
723           | Some t -> term2pres t) in
724        let proof = 
725          (match conclude.Con.conclude_args with
726              [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
727            | _ -> assert false;
728              (* 
729              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
730              assert false *)) in
731        match proof.Con.proof_context with
732            `Declaration decl::`Hypothesis hyp::tl
733          | `Hypothesis decl::`Hypothesis hyp::tl ->
734            let get_name decl =
735              (match decl.Con.dec_name with
736                 None -> "_"
737               | Some s -> s) in
738            let presdecl = 
739              P.Mrow ([],
740                [P.Mtext([None,"mathcolor","Red"],"let");
741                 P.smallskip;
742                 P.Mi([],get_name decl);
743                 P.Mtext([],":"); term2pres decl.Con.dec_type]) in
744            let suchthat =
745              P.Mrow ([],
746                [P.Mtext([None,"mathcolor","Red"],"such that");
747                 P.smallskip;
748                 P.Mtext([],"(");
749                 P.Mi([],get_name hyp);
750                 P.Mtext([],")");
751                 P.smallskip;
752                 term2pres hyp.Con.dec_type]) in
753             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
754             let body = conclude2pres proof.Con.proof_conclude false true in
755             let presacontext = 
756               acontext2pres proof.Con.proof_apply_context body false in
757             P.Mtable 
758               ([None,"align","baseline 1"; None,"equalrows","false"; 
759                 None,"columnalign","left"],
760                [P.Mtr ([],[P.Mtd ([],presdecl)]);
761                 P.Mtr ([],[P.Mtd ([],suchthat)]);
762                 P.Mtr ([],[P.Mtd ([],presacontext)])]);
763          | _ -> assert false in
764
765 proof2pres p
766 ;;
767
768 exception ToDo;;
769
770 let content2pres term2pres (id,params,metasenv,obj) =
771  let module K = Content in
772  let module P = Mpresentation in
773   match obj with
774      `Def (K.Const,thesis,`Proof p) ->
775        P.Mtable
776         [None,"align","baseline 1";
777          None,"equalrows","false";
778          None,"columnalign","left";
779          None,"helm:xref","id"]
780         ([P.Mtr []
781            [P.Mtd []
782             (P.Mrow []
783              [P.Mtext []
784                ("UNFINISHED PROOF" ^ id ^"(" ^
785                  String.concat " ; " (List.map UriManager.string_of_uri params)^
786                 ")")])] ;
787          P.Mtr []
788           [P.Mtd []
789             (P.Mrow []
790               [P.Mtext [] "THESIS:"])] ;
791          P.Mtr []
792           [P.Mtd []
793             (P.Mrow []
794               [P.Mphantom []
795                 (P.Mtext [] "__") ;
796               term2pres thesis])]] @
797          (match metasenv with
798              None -> []
799            | Some metasenv' ->
800               [P.Mtr []
801                 [P.Mtd []
802                   (* Conjectures are in their own table to make *)
803                   (* diffing the DOM trees easier.              *)
804                   (P.Mtable
805                     [None,"align","baseline 1";
806                      None,"equalrows","false";
807                      None,"columnalign","left"]
808                    ((P.Mtr []
809                       [P.Mtd []
810                        (P.Mrow []
811                          [P.Mtext [] "CONJECTURES:"])])::
812                     List.map
813                      (function
814                        (id,n,context,ty) ->
815                          P.Mtr []
816                           [P.Mtd []
817                            (P.Mrow [Some "helm", "xref", id]
818                              (List.map
819                                (function
820                                    (_,None) ->
821                                      P.Mrow []
822                                       [ P.Mi [] "_" ;
823                                         P.Mo [] ":?" ;
824                                         P.Mi [] "_"]
825                                  | (_,Some (`Declaration d))
826                                  | (_,Some (`Hypothesis d)) ->
827                                     let
828                                      { K.dec_name = dec_name ;
829                                        K.dec_type = ty } = d
830                                      in
831                                       P.Mrow []
832                                        [ P.Mi []
833                                           (match dec_name with
834                                               None -> "_"
835                                             | Some n -> n) ;
836                                          P.Mo [] ":" ;
837                                          term2pres ty]
838                                  | (_,Some (`Definition d)) ->
839                                     let
840                                      { K.def_name = def_name ;
841                                        K.def_term = bo } = d
842                                      in
843                                       P.Mrow []
844                                        [ P.Mi []
845                                           (match def_name with
846                                               None -> "_"
847                                             | Some n -> n) ;
848                                          P.Mo [] ":=" ;
849                                          term2pres bo]
850                                  | (_,Some (`Proof p)) ->
851                                     let proof_name = p.K.proof_name in
852                                      P.Mrow []
853                                       [ P.Mi []
854                                          (match proof_name with
855                                              None -> "_"
856                                            | Some n -> n) ;
857                                         P.Mo [] ":=" ;
858                                         proof2pres term2pres p]
859                                ) context @
860                              [ P.Mo [] "|-" ] @
861                              [ P.Mi [] (string_of_int n) ;
862                                P.Mo [] ":" ;
863                                term2pres ty ]
864                            ))
865                           ]
866                      ) metasenv'
867                   ))]]
868          )  @
869         [P.Mtr []
870           [P.Mtd []
871             (P.Mrow []
872               [proof2pres term2pres p])]])
873    | _ -> raise ToDo
874 ;;
875
876 let content2pres ~ids_to_inner_sorts =
877  content2pres 
878   (function p -> 
879    (Cexpr2pres.cexpr2pres_charcount 
880     (Content_expressions.acic2cexpr ids_to_inner_sorts p)))
881 ;;