]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/content2pres.ml
Minor change.
[helm.git] / helm / software / components / content_pres / content2pres.ml
1 (* Copyright (C) 2003-2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (***************************************************************************)
27 (*                                                                         *)
28 (*                            PROJECT HELM                                 *)
29 (*                                                                         *)
30 (*                Andrea Asperti <asperti@cs.unibo.it>                     *)
31 (*                              17/06/2003                                 *)
32 (*                                                                         *)
33 (***************************************************************************)
34
35 (* $Id$ *)
36
37 module P = Mpresentation
38 module B = Box
39 module Con = Content
40
41 let p_mtr a b = Mpresentation.Mtr(a,b)
42 let p_mtd a b = Mpresentation.Mtd(a,b)
43 let p_mtable a b = Mpresentation.Mtable(a,b)
44 let p_mtext a b = Mpresentation.Mtext(a,b)
45 let p_mi a b = Mpresentation.Mi(a,b)
46 let p_mo a b = Mpresentation.Mo(a,b)
47 let p_mrow a b = Mpresentation.Mrow(a,b)
48 let p_mphantom a b = Mpresentation.Mphantom(a,b)
49
50 let rec split n l =
51   if n = 0 then [],l
52   else let l1,l2 = 
53     split (n-1) (List.tl l) in
54     (List.hd l)::l1,l2
55   
56 let get_xref = function
57   | `Declaration d  
58   | `Hypothesis d -> d.Con.dec_id
59   | `Proof p -> p.Con.proof_id
60   | `Definition d -> d.Con.def_id
61   | `Joint jo -> jo.Con.joint_id
62
63 let hv_attrs =
64   RenderingAttrs.spacing_attributes `BoxML
65   @ RenderingAttrs.indent_attributes `BoxML
66
67 let make_row items concl =
68   B.b_hv hv_attrs (items @ [ concl ])
69 (*   match concl with 
70       B.V _ -> |+ big! +|
71         B.b_v attrs [B.b_h [] items; B.b_indent concl]
72     | _ ->  |+ small +|
73         B.b_h attrs (items@[B.b_space; concl]) *)
74
75 let make_concl ?(attrs=[]) verb concl =
76   B.b_hv (hv_attrs @ attrs) [ B.b_kw verb; concl ]
77 (*   match concl with 
78       B.V _ -> |+ big! +|
79         B.b_v attrs [ B.b_kw verb; B.b_indent concl]
80     | _ ->  |+ small +|
81         B.b_h attrs [ B.b_kw verb; B.b_space; concl ] *)
82
83 let make_args_for_apply term2pres args =
84  let make_arg_for_apply is_first arg row = 
85   let res =
86    match arg with 
87       Con.Aux n -> assert false
88     | Con.Premise prem -> 
89         let name = 
90           (match prem.Con.premise_binder with
91              None -> "previous"
92            | Some s -> s) in
93         (B.b_object (P.Mi ([], name)))::row
94     | Con.Lemma lemma -> 
95         let lemma_attrs = [
96           Some "helm", "xref", lemma.Con.lemma_id;
97           Some "xlink", "href", lemma.Con.lemma_uri ]
98         in
99         (B.b_object (P.Mi(lemma_attrs,lemma.Con.lemma_name)))::row 
100     | Con.Term t -> 
101         if is_first then
102           (term2pres t)::row
103         else (B.b_object (P.Mi([],"_")))::row
104     | Con.ArgProof _ 
105     | Con.ArgMethod _ -> 
106         (B.b_object (P.Mi([],"_")))::row
107   in
108    if is_first then res else B.skip::res
109  in
110   match args with 
111     hd::tl -> 
112       make_arg_for_apply true hd 
113         (List.fold_right (make_arg_for_apply false) tl [])
114   | _ -> assert false
115 let get_name = function
116   | Some s -> s
117   | None -> "_"
118
119 let add_xref id = function
120   | B.Text (attrs, t) -> B.Text (((Some "helm", "xref", id) :: attrs), t)
121   | _ -> assert false (* TODO, add_xref is meaningful for all boxes *)
122
123 let rec justification term2pres p = 
124   if ((p.Con.proof_conclude.Con.conclude_method = "Exact") or
125      ((p.Con.proof_context = []) &
126       (p.Con.proof_apply_context = []) &
127       (p.Con.proof_conclude.Con.conclude_method = "Apply"))) then
128     let pres_args = 
129       make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args in
130     B.H([],
131       (B.b_kw "by")::B.b_space::
132       B.Text([],"(")::pres_args@[B.Text([],")")]), None 
133   else (B.b_kw "by"),
134     Some (B.b_toggle [B.b_kw "proof";proof2pres term2pres p])
135      
136 and proof2pres term2pres p =
137   let rec proof2pres p =
138     let indent = 
139       let is_decl e = 
140         (match e with 
141            `Declaration _
142          | `Hypothesis _ -> true
143          | _ -> false) in
144       ((List.filter is_decl p.Con.proof_context) != []) in 
145     let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
146     let concl = 
147       (match p.Con.proof_conclude.Con.conclude_conclusion with
148          None -> None
149        | Some t -> Some (term2pres t)) in
150     let body =
151         let presconclude = 
152           conclude2pres p.Con.proof_conclude indent omit_conclusion in
153         let presacontext = 
154           acontext2pres p.Con.proof_apply_context presconclude indent in
155         context2pres p.Con.proof_context presacontext in
156     match p.Con.proof_name with
157       None -> body
158     | Some name ->
159         let action = 
160          match concl with
161             None -> body
162           | Some ac ->
163              let concl =
164                make_concl ~attrs:[ Some "helm", "xref", p.Con.proof_id ]
165                  "proof of" ac in
166              B.b_toggle [ concl; body ]
167         in
168         B.V ([],
169           [B.Text ([],"(" ^ name ^ ")");
170            B.indent action])
171
172   and context2pres c continuation =
173     (* we generate a subtable for each context element, for selection
174        purposes 
175        The table generated by the head-element does not have an xref;
176        the whole context-proof is already selectable *)
177     match c with
178       [] -> continuation
179     | hd::tl -> 
180         let continuation' =
181           List.fold_right
182             (fun ce continuation ->
183               let xref = get_xref ce in
184               B.V([Some "helm", "xref", xref ],
185                 [B.H([Some "helm", "xref", "ce_"^xref],
186                      [ce2pres_in_proof_context_element ce]);
187                  continuation])) tl continuation in
188          let hd_xref= get_xref hd in
189          B.V([],
190              [B.H([Some "helm", "xref", "ce_"^hd_xref],
191                [ce2pres_in_proof_context_element hd]);
192              continuation'])
193         
194   and ce2pres_in_joint_context_element = function
195     | `Inductive _ -> assert false (* TODO *)
196     | (`Declaration _) as x -> ce2pres x
197     | (`Hypothesis _) as x  -> ce2pres x
198     | (`Proof _) as x       -> ce2pres x
199     | (`Definition _) as x  -> ce2pres x
200   
201   and ce2pres_in_proof_context_element = function 
202     | `Joint ho -> 
203       B.H ([],(List.map ce2pres_in_joint_context_element ho.Content.joint_defs))
204     | (`Declaration _) as x -> ce2pres x
205     | (`Hypothesis _) as x  -> ce2pres x
206     | (`Proof _) as x       -> ce2pres x
207     | (`Definition _) as x  -> ce2pres x
208   
209   and ce2pres = 
210     function 
211         `Declaration d -> 
212           (match d.Con.dec_name with
213               Some s ->
214                 let ty = term2pres d.Con.dec_type in
215                 B.H ([],
216                   [(B.b_kw "Assume");
217                    B.b_space;
218                    B.Object ([], P.Mi([],s));
219                    B.Text([],":");
220                    ty])
221             | None -> 
222                 prerr_endline "NO NAME!!"; assert false)
223       | `Hypothesis h ->
224           (match h.Con.dec_name with
225               Some s ->
226                 let ty = term2pres h.Con.dec_type in
227                 B.H ([],
228                   [(B.b_kw "Suppose");
229                    B.b_space;
230                    B.Text([],"(");
231                    B.Object ([], P.Mi ([],s));
232                    B.Text([],")");
233                    B.b_space;
234                    ty])
235             | None -> 
236                 prerr_endline "NO NAME!!"; assert false) 
237       | `Proof p -> 
238            proof2pres p 
239       | `Definition d -> 
240            (match d.Con.def_name with
241               Some s ->
242                 let term = term2pres d.Con.def_term in
243                 B.H ([],
244                   [ B.b_kw "Let"; B.b_space;
245                     B.Object ([], P.Mi([],s));
246                     B.Text([]," = ");
247                     term])
248             | None -> 
249                 prerr_endline "NO NAME!!"; assert false) 
250
251   and acontext2pres ac continuation indent =
252     List.fold_right
253       (fun p continuation ->
254          let hd = 
255            if indent then
256              B.indent (proof2pres p)
257            else 
258              proof2pres p in
259          B.V([Some "helm","xref",p.Con.proof_id],
260            [B.H([Some "helm","xref","ace_"^p.Con.proof_id],[hd]);
261             continuation])) ac continuation 
262
263   and conclude2pres conclude indent omit_conclusion =
264     let tconclude_body = 
265       match conclude.Con.conclude_conclusion with
266         Some t when
267          not omit_conclusion or
268          (* CSC: I ignore the omit_conclusion flag in this case.   *)
269          (* CSC: Is this the correct behaviour? In the stylesheets *)
270          (* CSC: we simply generated nothing (i.e. the output type *)
271          (* CSC: of the function should become an option.          *)
272          conclude.Con.conclude_method = "BU_Conversion" ->
273           let concl = (term2pres t) in 
274           if conclude.Con.conclude_method = "BU_Conversion" then
275             make_concl "that is equivalent to" concl
276           else if conclude.Con.conclude_method = "FalseInd" then
277            (* false ind is in charge to add the conclusion *)
278            falseind conclude
279           else  
280             let conclude_body = conclude_aux conclude in
281             let ann_concl = 
282               if conclude.Con.conclude_method = "TD_Conversion" then
283                  make_concl "that is equivalent to" concl 
284               else make_concl "we conclude" concl in
285             B.V ([], [conclude_body; ann_concl])
286       | _ -> conclude_aux conclude in
287     if indent then 
288       B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
289                     [tconclude_body]))
290     else 
291       B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
292
293   and conclude_aux conclude =
294     if conclude.Con.conclude_method = "TD_Conversion" then
295       let expected = 
296         (match conclude.Con.conclude_conclusion with 
297            None -> B.Text([],"NO EXPECTED!!!")
298          | Some c -> term2pres c) in
299       let subproof = 
300         (match conclude.Con.conclude_args with
301           [Con.ArgProof p] -> p
302          | _ -> assert false) in
303       let synth = 
304         (match subproof.Con.proof_conclude.Con.conclude_conclusion with
305            None -> B.Text([],"NO SYNTH!!!")
306          | Some c -> (term2pres c)) in
307       B.V 
308         ([],
309         [make_concl "we must prove" expected;
310          make_concl "or equivalently" synth;
311          proof2pres subproof])
312     else if conclude.Con.conclude_method = "BU_Conversion" then
313       assert false
314     else if conclude.Con.conclude_method = "Exact" then
315       let arg = 
316         (match conclude.Con.conclude_args with 
317            [Con.Term t] -> term2pres t
318          | [Con.Premise p] -> 
319              (match p.Con.premise_binder with
320              | None -> assert false; (* unnamed hypothesis ??? *)
321              | Some s -> B.Text([],s))
322          | err -> assert false) in
323       (match conclude.Con.conclude_conclusion with 
324          None ->
325           B.b_h [] [B.b_kw "Consider"; B.b_space; arg]
326        | Some c -> let conclusion = term2pres c in
327           make_row 
328             [arg; B.b_space; B.b_kw "proves"]
329             conclusion
330        )
331     else if conclude.Con.conclude_method = "Intros+LetTac" then
332       (match conclude.Con.conclude_args with
333          [Con.ArgProof p] -> proof2pres p
334        | _ -> assert false)
335 (* OLD CODE 
336       let conclusion = 
337       (match conclude.Con.conclude_conclusion with 
338          None -> B.Text([],"NO Conclusion!!!")
339        | Some c -> term2pres c) in
340       (match conclude.Con.conclude_args with
341          [Con.ArgProof p] -> 
342            B.V 
343             ([None,"align","baseline 1"; None,"equalrows","false";
344               None,"columnalign","left"],
345               [B.H([],[B.Object([],proof2pres p)]);
346                B.H([],[B.Object([],
347                 (make_concl "we proved 1" conclusion))])]);
348        | _ -> assert false)
349 *)
350     else if (conclude.Con.conclude_method = "Case") then
351       case conclude
352     else if (conclude.Con.conclude_method = "ByInduction") then
353       byinduction conclude
354     else if (conclude.Con.conclude_method = "Exists") then
355       exists conclude
356     else if (conclude.Con.conclude_method = "AndInd") then
357       andind conclude
358     else if (conclude.Con.conclude_method = "FalseInd") then
359       falseind conclude
360     else if (conclude.Con.conclude_method = "Rewrite") then
361       let justif1,justif2 = 
362         (match (List.nth conclude.Con.conclude_args 6) with
363            Con.ArgProof p -> justification term2pres p
364          | _ -> assert false) in
365       let term1 = 
366         (match List.nth conclude.Con.conclude_args 2 with
367            Con.Term t -> term2pres t
368          | _ -> assert false) in 
369       let term2 = 
370         (match List.nth conclude.Con.conclude_args 5 with
371            Con.Term t -> term2pres t
372          | _ -> assert false) in
373       B.V ([], 
374          B.H ([],[
375           (B.b_kw "rewrite");
376           B.b_space; term1;
377           B.b_space; (B.b_kw "with");
378           B.b_space; term2;
379           B.b_space; justif1])::
380             match justif2 with None -> [] | Some j -> [B.indent j])
381     else if conclude.Con.conclude_method = "Eq_chain" then
382       let justification p =
383         let j1,j2 = justification term2pres p in
384           j1 :: B.b_space :: (match j2 with Some j -> [j] | None -> [])
385       in
386       let rec aux args =
387         match args with
388           | [] -> []
389           | (Con.ArgProof p)::(Con.Term t)::tl -> 
390               B.HOV(RenderingAttrs.indent_attributes `BoxML,([B.b_kw "=";B.b_space;term2pres t;B.b_space]@justification p))::(aux tl)
391           | _ -> assert false 
392       in
393       let hd = 
394         match List.hd conclude.Con.conclude_args with
395           | Con.Term t -> t 
396           | _ -> assert false 
397       in
398       B.HOV([],[term2pres hd; (* B.b_space; *)
399               B.V ([],aux (List.tl conclude.Con.conclude_args))])
400     else if conclude.Con.conclude_method = "Apply" then
401       let pres_args = 
402         make_args_for_apply term2pres conclude.Con.conclude_args in
403       B.H([],
404         (B.b_kw "by")::
405         B.b_space::
406         B.Text([],"(")::pres_args@[B.Text([],")")])
407     else 
408       B.V ([], [
409         B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
410         (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
411
412   and args2pres l = List.map arg2pres l
413
414   and arg2pres =
415     function
416         Con.Aux n -> B.b_kw ("aux " ^ n)
417       | Con.Premise prem -> B.b_kw "premise"
418       | Con.Lemma lemma -> B.b_kw "lemma"
419       | Con.Term t -> term2pres t
420       | Con.ArgProof p -> proof2pres p 
421       | Con.ArgMethod s -> B.b_kw "method"
422  
423    and case conclude =
424      let proof_conclusion = 
425        (match conclude.Con.conclude_conclusion with
426           None -> B.b_kw "No conclusion???"
427         | Some t -> term2pres t) in
428      let arg,args_for_cases = 
429        (match conclude.Con.conclude_args with
430            Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
431              arg,tl
432          | _ -> assert false) in
433      let case_on =
434        let case_arg = 
435          (match arg with
436             Con.Aux n -> B.b_kw "an aux???"
437            | Con.Premise prem ->
438               (match prem.Con.premise_binder with
439                  None -> B.b_kw "the previous result"
440                | Some n -> B.Object ([], P.Mi([],n)))
441            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
442            | Con.Term t -> 
443                term2pres t
444            | Con.ArgProof p -> B.b_kw "a proof???"
445            | Con.ArgMethod s -> B.b_kw "a method???")
446       in
447         (make_concl "we proceed by cases on" case_arg) in
448      let to_prove =
449         (make_concl "to prove" proof_conclusion) in
450      B.V ([], case_on::to_prove::(make_cases args_for_cases))
451
452    and byinduction conclude =
453      let proof_conclusion = 
454        (match conclude.Con.conclude_conclusion with
455           None -> B.b_kw "No conclusion???"
456         | Some t -> term2pres t) in
457      let inductive_arg,args_for_cases = 
458        (match conclude.Con.conclude_args with
459            Con.Aux(n)::_::tl ->
460              let l1,l2 = split (int_of_string n) tl in
461              let last_pos = (List.length l2)-1 in
462              List.nth l2 last_pos,l1
463          | _ -> assert false) in
464      let induction_on =
465        let arg = 
466          (match inductive_arg with
467             Con.Aux n -> B.b_kw "an aux???"
468            | Con.Premise prem ->
469               (match prem.Con.premise_binder with
470                  None -> B.b_kw "the previous result"
471                | Some n -> B.Object ([], P.Mi([],n)))
472            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
473            | Con.Term t -> 
474                term2pres t
475            | Con.ArgProof p -> B.b_kw "a proof???"
476            | Con.ArgMethod s -> B.b_kw "a method???") in
477         (make_concl "we proceed by induction on" arg) in
478      let to_prove =
479         (make_concl "to prove" proof_conclusion) in
480      B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
481
482     and make_cases l = List.map make_case l
483
484     and make_case =  
485       function 
486         Con.ArgProof p ->
487           let name =
488             (match p.Con.proof_name with
489                None -> B.b_kw "no name for case!!"
490              | Some n -> B.Object ([], P.Mi([],n))) in
491           let indhyps,args =
492              List.partition 
493                (function
494                    `Hypothesis h -> h.Con.dec_inductive
495                  | _ -> false) p.Con.proof_context in
496           let pattern_aux =
497              List.fold_right
498                (fun e p -> 
499                   let dec  = 
500                     (match e with 
501                        `Declaration h 
502                      | `Hypothesis h -> 
503                          let name = 
504                            (match h.Con.dec_name with
505                               None -> "NO NAME???"
506                            | Some n ->n) in
507                          [B.b_space;
508                           B.Object ([], P.Mi ([],name));
509                           B.Text([],":");
510                           (term2pres h.Con.dec_type)]
511                      | _ -> [B.Text ([],"???")]) in
512                   dec@p) args [] in
513           let pattern = 
514             B.H ([],
515                (B.b_kw "Case"::B.b_space::name::pattern_aux)@
516                 [B.b_space;
517                  B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
518           let subconcl = 
519             (match p.Con.proof_conclude.Con.conclude_conclusion with
520                None -> B.b_kw "No conclusion!!!"
521              | Some t -> term2pres t) in
522           let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
523           let induction_hypothesis = 
524             (match indhyps with
525               [] -> []
526             | _ -> 
527                let text = B.indent (B.b_kw "by induction hypothesis we know") in
528                let make_hyp =
529                  function 
530                    `Hypothesis h ->
531                      let name = 
532                        (match h.Con.dec_name with
533                           None -> "no name"
534                         | Some s -> s) in
535                      B.indent (B.H ([],
536                        [B.Text([],"(");
537                         B.Object ([], P.Mi ([],name));
538                         B.Text([],")");
539                         B.b_space;
540                         term2pres h.Con.dec_type]))
541                    | _ -> assert false in
542                let hyps = List.map make_hyp indhyps in
543                text::hyps) in          
544           (* let acontext = 
545                acontext2pres_old p.Con.proof_apply_context true in *)
546           let body = conclude2pres p.Con.proof_conclude true false in
547           let presacontext = 
548            let acontext_id =
549             match p.Con.proof_apply_context with
550                [] -> p.Con.proof_conclude.Con.conclude_id
551              | {Con.proof_id = id}::_ -> id
552            in
553             B.Action([None,"type","toggle"],
554               [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
555                 acontext2pres p.Con.proof_apply_context body true]) in
556           B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
557        | _ -> assert false 
558
559      and falseind conclude =
560        let proof_conclusion = 
561          (match conclude.Con.conclude_conclusion with
562             None -> B.b_kw "No conclusion???"
563           | Some t -> term2pres t) in
564        let case_arg = 
565          (match conclude.Con.conclude_args with
566              [Con.Aux(n);_;case_arg] -> case_arg
567            | _ -> assert false;
568              (* 
569              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
570              assert false *)) in
571        let arg = 
572          (match case_arg with
573              Con.Aux n -> assert false
574            | Con.Premise prem ->
575               (match prem.Con.premise_binder with
576                  None -> [B.b_kw "Contradiction, hence"]
577                | Some n -> 
578                    [ B.Object ([],P.Mi([],n)); B.skip;
579                      B.b_kw "is contradictory, hence"])
580            | Con.Lemma lemma -> 
581                [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
582                  B.b_kw "is contradictory, hence" ]
583            | _ -> assert false) in
584             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
585        make_row arg proof_conclusion
586
587      and andind conclude =
588        let proof,case_arg = 
589          (match conclude.Con.conclude_args with
590              [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
591            | _ -> assert false;
592              (* 
593              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
594              assert false *)) in
595        let arg = 
596          (match case_arg with
597              Con.Aux n -> assert false
598            | Con.Premise prem ->
599               (match prem.Con.premise_binder with
600                  None -> []
601                | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
602            | Con.Lemma lemma -> 
603                [(B.b_kw "by");B.skip;
604                 B.Object([], P.Mi([],lemma.Con.lemma_name))]
605            | _ -> assert false) in
606        match proof.Con.proof_context with
607          `Hypothesis hyp1::`Hypothesis hyp2::tl ->
608             let get_name hyp =
609               (match hyp.Con.dec_name with
610                 None -> "_"
611               | Some s -> s) in
612             let preshyp1 = 
613               B.H ([],
614                [B.Text([],"(");
615                 B.Object ([], P.Mi([],get_name hyp1));
616                 B.Text([],")");
617                 B.skip;
618                 term2pres hyp1.Con.dec_type]) in
619             let preshyp2 = 
620               B.H ([],
621                [B.Text([],"(");
622                 B.Object ([], P.Mi([],get_name hyp2));
623                 B.Text([],")");
624                 B.skip;
625                 term2pres hyp2.Con.dec_type]) in
626             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
627             let body = conclude2pres proof.Con.proof_conclude false true in
628             let presacontext = 
629               acontext2pres proof.Con.proof_apply_context body false in
630             B.V 
631               ([],
632                [B.H ([],arg@[B.skip; B.b_kw "we have"]);
633                 preshyp1;
634                 B.b_kw "and";
635                 preshyp2;
636                 presacontext]);
637          | _ -> assert false
638
639      and exists conclude =
640        let proof = 
641          (match conclude.Con.conclude_args with
642              [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
643            | _ -> assert false;
644              (* 
645              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
646              assert false *)) in
647        match proof.Con.proof_context with
648            `Declaration decl::`Hypothesis hyp::tl
649          | `Hypothesis decl::`Hypothesis hyp::tl ->
650            let get_name decl =
651              (match decl.Con.dec_name with
652                 None -> "_"
653               | Some s -> s) in
654            let presdecl = 
655              B.H ([],
656                [(B.b_kw "let");
657                 B.skip;
658                 B.Object ([], P.Mi([],get_name decl));
659                 B.Text([],":"); term2pres decl.Con.dec_type]) in
660            let suchthat =
661              B.H ([],
662                [(B.b_kw "such that");
663                 B.skip;
664                 B.Text([],"(");
665                 B.Object ([], P.Mi([],get_name hyp));
666                 B.Text([],")");
667                 B.skip;
668                 term2pres hyp.Con.dec_type]) in
669             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
670             let body = conclude2pres proof.Con.proof_conclude false true in
671             let presacontext = 
672               acontext2pres proof.Con.proof_apply_context body false in
673             B.V 
674               ([],
675                [presdecl;
676                 suchthat;
677                 presacontext]);
678          | _ -> assert false
679
680     in
681     proof2pres p
682
683 exception ToDo
684
685 let counter = ref 0
686
687 let conjecture2pres term2pres (id, n, context, ty) =
688  B.b_indent
689   (B.b_hv [Some "helm", "xref", id]
690      ((B.b_toggle [
691         B.b_h [] [B.b_text [] "{...}"; B.b_space];
692         B.b_hv [] (List.map
693           (function
694              | None ->
695                  B.b_h []
696                    [ B.b_object (p_mi [] "_") ;
697                      B.b_object (p_mo [] ":?") ;
698                      B.b_object (p_mi [] "_")]
699              | Some (`Declaration d)
700              | Some (`Hypothesis d) ->
701                  let { Content.dec_name =
702                      dec_name ; Content.dec_type = ty } = d
703                  in
704                    B.b_h []
705                      [ B.b_object
706                          (p_mi []
707                             (match dec_name with
708                                  None -> "_"
709                                | Some n -> n));
710                        B.b_text [] ":";
711                        term2pres ty ]
712              | Some (`Definition d) ->
713                  let
714                      { Content.def_name = def_name ;
715                        Content.def_term = bo } = d
716                  in
717                    B.b_h []
718                      [ B.b_object (p_mi []
719                                      (match def_name with
720                                           None -> "_"
721                                         | Some n -> n)) ;
722                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
723                        term2pres bo]
724              | Some (`Proof p) ->
725                  let proof_name = p.Content.proof_name in
726                    B.b_h []
727                      [ B.b_object (p_mi []
728                                      (match proof_name with
729                                           None -> "_"
730                                         | Some n -> n)) ;
731                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
732                        proof2pres term2pres p])
733           (List.rev context)) ] ::
734          [ B.b_h []
735            [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
736              B.b_object (p_mi [] (string_of_int n)) ;
737              B.b_text [] ":" ;
738              term2pres ty ]])))
739
740 let metasenv2pres term2pres = function
741   | None -> []
742   | Some metasenv' ->
743       (* Conjectures are in their own table to make *)
744       (* diffing the DOM trees easier.              *)
745       [B.b_v []
746         ((B.b_kw ("Conjectures:" ^
747             (let _ = incr counter; in (string_of_int !counter)))) ::
748          (List.map (conjecture2pres term2pres) metasenv'))]
749
750 let params2pres params =
751   let param2pres uri =
752     B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
753       (UriManager.name_of_uri uri)
754   in
755   let rec spatiate = function
756     | [] -> []
757     | hd :: [] -> [hd]
758     | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
759   in
760   match params with
761   | [] -> []
762   | p ->
763       let params = spatiate (List.map param2pres p) in
764       [B.b_space;
765        B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
766
767 let recursion_kind2pres params kind =
768   let kind =
769     match kind with
770     | `Recursive _ -> "Recursive definition"
771     | `CoRecursive -> "CoRecursive definition"
772     | `Inductive _ -> "Inductive definition"
773     | `CoInductive _ -> "CoInductive definition"
774   in
775   B.b_h [] (B.b_kw kind :: params2pres params)
776
777 let inductive2pres term2pres ind =
778   let constructor2pres decl =
779     B.b_h [] [
780       B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
781       B.b_space;
782       term2pres decl.Content.dec_type
783     ]
784   in
785   B.b_v []
786     (B.b_h [] [
787       B.b_kw (ind.Content.inductive_name ^ " of arity");
788       B.smallskip;
789       term2pres ind.Content.inductive_type ]
790     :: List.map constructor2pres ind.Content.inductive_constructors)
791
792 let joint_def2pres term2pres def =
793   match def with
794   | `Inductive ind -> inductive2pres term2pres ind
795   | _ -> assert false (* ZACK or raise ToDo? *)
796
797 let content2pres term2pres (id,params,metasenv,obj) =
798   match obj with
799   | `Def (Content.Const, thesis, `Proof p) ->
800       let name = get_name p.Content.proof_name in
801       B.b_v
802         [Some "helm","xref","id"]
803         ([ B.b_h [] (B.b_kw ("Proof " ^ name) :: params2pres params);
804            B.b_kw "Thesis:";
805            B.indent (term2pres thesis) ] @
806          metasenv2pres term2pres metasenv @
807          [proof2pres term2pres p])
808   | `Def (_, ty, `Definition body) ->
809       let name = get_name body.Content.def_name in
810       B.b_v
811         [Some "helm","xref","id"]
812         ([B.b_h [] (B.b_kw ("Definition " ^ name) :: params2pres params);
813           B.b_kw "Type:";
814           B.indent (term2pres ty)] @
815           metasenv2pres term2pres metasenv @
816           [B.b_kw "Body:"; term2pres body.Content.def_term])
817   | `Decl (_, `Declaration decl)
818   | `Decl (_, `Hypothesis decl) ->
819       let name = get_name decl.Content.dec_name in
820       B.b_v
821         [Some "helm","xref","id"]
822         ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
823           B.b_kw "Type:";
824           B.indent (term2pres decl.Content.dec_type)] @
825           metasenv2pres term2pres metasenv)
826   | `Joint joint ->
827       B.b_v []
828         (recursion_kind2pres params joint.Content.joint_kind
829         :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
830   | _ -> raise ToDo
831
832 let content2pres ~ids_to_inner_sorts =
833   content2pres
834     (fun annterm ->
835       let ast, ids_to_uris =
836         TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
837       in
838       CicNotationPres.box_of_mpres
839         (CicNotationPres.render ids_to_uris
840           (TermContentPres.pp_ast ast)))
841