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