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