]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/content2pres.ml
- renamed ocaml/ to components/
[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([],")")]) 
134   else 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 justif = 
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.indent justif])])
380     else if conclude.Con.conclude_method = "Apply" then
381       let pres_args = 
382         make_args_for_apply term2pres conclude.Con.conclude_args in
383       B.H([],
384         (B.b_kw "by")::
385         B.b_space::
386         B.Text([],"(")::pres_args@[B.Text([],")")])
387     else 
388       B.V ([], [
389         B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
390         (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
391
392   and args2pres l = List.map arg2pres l
393
394   and arg2pres =
395     function
396         Con.Aux n -> B.b_kw ("aux " ^ n)
397       | Con.Premise prem -> B.b_kw "premise"
398       | Con.Lemma lemma -> B.b_kw "lemma"
399       | Con.Term t -> term2pres t
400       | Con.ArgProof p -> proof2pres p 
401       | Con.ArgMethod s -> B.b_kw "method"
402  
403    and case conclude =
404      let proof_conclusion = 
405        (match conclude.Con.conclude_conclusion with
406           None -> B.b_kw "No conclusion???"
407         | Some t -> term2pres t) in
408      let arg,args_for_cases = 
409        (match conclude.Con.conclude_args with
410            Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
411              arg,tl
412          | _ -> assert false) in
413      let case_on =
414        let case_arg = 
415          (match arg with
416             Con.Aux n -> B.b_kw "an aux???"
417            | Con.Premise prem ->
418               (match prem.Con.premise_binder with
419                  None -> B.b_kw "the previous result"
420                | Some n -> B.Object ([], P.Mi([],n)))
421            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
422            | Con.Term t -> 
423                term2pres t
424            | Con.ArgProof p -> B.b_kw "a proof???"
425            | Con.ArgMethod s -> B.b_kw "a method???")
426       in
427         (make_concl "we proceed by cases on" case_arg) in
428      let to_prove =
429         (make_concl "to prove" proof_conclusion) in
430      B.V ([], case_on::to_prove::(make_cases args_for_cases))
431
432    and byinduction conclude =
433      let proof_conclusion = 
434        (match conclude.Con.conclude_conclusion with
435           None -> B.b_kw "No conclusion???"
436         | Some t -> term2pres t) in
437      let inductive_arg,args_for_cases = 
438        (match conclude.Con.conclude_args with
439            Con.Aux(n)::_::tl ->
440              let l1,l2 = split (int_of_string n) tl in
441              let last_pos = (List.length l2)-1 in
442              List.nth l2 last_pos,l1
443          | _ -> assert false) in
444      let induction_on =
445        let arg = 
446          (match inductive_arg with
447             Con.Aux n -> B.b_kw "an aux???"
448            | Con.Premise prem ->
449               (match prem.Con.premise_binder with
450                  None -> B.b_kw "the previous result"
451                | Some n -> B.Object ([], P.Mi([],n)))
452            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
453            | Con.Term t -> 
454                term2pres t
455            | Con.ArgProof p -> B.b_kw "a proof???"
456            | Con.ArgMethod s -> B.b_kw "a method???") in
457         (make_concl "we proceed by induction on" arg) in
458      let to_prove =
459         (make_concl "to prove" proof_conclusion) in
460      B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
461
462     and make_cases l = List.map make_case l
463
464     and make_case =  
465       function 
466         Con.ArgProof p ->
467           let name =
468             (match p.Con.proof_name with
469                None -> B.b_kw "no name for case!!"
470              | Some n -> B.Object ([], P.Mi([],n))) in
471           let indhyps,args =
472              List.partition 
473                (function
474                    `Hypothesis h -> h.Con.dec_inductive
475                  | _ -> false) p.Con.proof_context in
476           let pattern_aux =
477              List.fold_right
478                (fun e p -> 
479                   let dec  = 
480                     (match e with 
481                        `Declaration h 
482                      | `Hypothesis h -> 
483                          let name = 
484                            (match h.Con.dec_name with
485                               None -> "NO NAME???"
486                            | Some n ->n) in
487                          [B.b_space;
488                           B.Object ([], P.Mi ([],name));
489                           B.Text([],":");
490                           (term2pres h.Con.dec_type)]
491                      | _ -> [B.Text ([],"???")]) in
492                   dec@p) args [] in
493           let pattern = 
494             B.H ([],
495                (B.b_kw "Case"::B.b_space::name::pattern_aux)@
496                 [B.b_space;
497                  B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
498           let subconcl = 
499             (match p.Con.proof_conclude.Con.conclude_conclusion with
500                None -> B.b_kw "No conclusion!!!"
501              | Some t -> term2pres t) in
502           let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
503           let induction_hypothesis = 
504             (match indhyps with
505               [] -> []
506             | _ -> 
507                let text = B.indent (B.b_kw "by induction hypothesis we know") in
508                let make_hyp =
509                  function 
510                    `Hypothesis h ->
511                      let name = 
512                        (match h.Con.dec_name with
513                           None -> "no name"
514                         | Some s -> s) in
515                      B.indent (B.H ([],
516                        [B.Text([],"(");
517                         B.Object ([], P.Mi ([],name));
518                         B.Text([],")");
519                         B.b_space;
520                         term2pres h.Con.dec_type]))
521                    | _ -> assert false in
522                let hyps = List.map make_hyp indhyps in
523                text::hyps) in          
524           (* let acontext = 
525                acontext2pres_old p.Con.proof_apply_context true in *)
526           let body = conclude2pres p.Con.proof_conclude true false in
527           let presacontext = 
528            let acontext_id =
529             match p.Con.proof_apply_context with
530                [] -> p.Con.proof_conclude.Con.conclude_id
531              | {Con.proof_id = id}::_ -> id
532            in
533             B.Action([None,"type","toggle"],
534               [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
535                 acontext2pres p.Con.proof_apply_context body true]) in
536           B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
537        | _ -> assert false 
538
539      and falseind conclude =
540        let proof_conclusion = 
541          (match conclude.Con.conclude_conclusion with
542             None -> B.b_kw "No conclusion???"
543           | Some t -> term2pres t) in
544        let case_arg = 
545          (match conclude.Con.conclude_args with
546              [Con.Aux(n);_;case_arg] -> case_arg
547            | _ -> assert false;
548              (* 
549              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
550              assert false *)) in
551        let arg = 
552          (match case_arg with
553              Con.Aux n -> assert false
554            | Con.Premise prem ->
555               (match prem.Con.premise_binder with
556                  None -> [B.b_kw "Contradiction, hence"]
557                | Some n -> 
558                    [ B.Object ([],P.Mi([],n)); B.skip;
559                      B.b_kw "is contradictory, hence"])
560            | Con.Lemma lemma -> 
561                [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
562                  B.b_kw "is contradictory, hence" ]
563            | _ -> assert false) in
564             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
565        make_row arg proof_conclusion
566
567      and andind conclude =
568        let proof,case_arg = 
569          (match conclude.Con.conclude_args with
570              [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
571            | _ -> assert false;
572              (* 
573              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
574              assert false *)) in
575        let arg = 
576          (match case_arg with
577              Con.Aux n -> assert false
578            | Con.Premise prem ->
579               (match prem.Con.premise_binder with
580                  None -> []
581                | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
582            | Con.Lemma lemma -> 
583                [(B.b_kw "by");B.skip;
584                 B.Object([], P.Mi([],lemma.Con.lemma_name))]
585            | _ -> assert false) in
586        match proof.Con.proof_context with
587          `Hypothesis hyp1::`Hypothesis hyp2::tl ->
588             let get_name hyp =
589               (match hyp.Con.dec_name with
590                 None -> "_"
591               | Some s -> s) in
592             let preshyp1 = 
593               B.H ([],
594                [B.Text([],"(");
595                 B.Object ([], P.Mi([],get_name hyp1));
596                 B.Text([],")");
597                 B.skip;
598                 term2pres hyp1.Con.dec_type]) in
599             let preshyp2 = 
600               B.H ([],
601                [B.Text([],"(");
602                 B.Object ([], P.Mi([],get_name hyp2));
603                 B.Text([],")");
604                 B.skip;
605                 term2pres hyp2.Con.dec_type]) in
606             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
607             let body = conclude2pres proof.Con.proof_conclude false true in
608             let presacontext = 
609               acontext2pres proof.Con.proof_apply_context body false in
610             B.V 
611               ([],
612                [B.H ([],arg@[B.skip; B.b_kw "we have"]);
613                 preshyp1;
614                 B.b_kw "and";
615                 preshyp2;
616                 presacontext]);
617          | _ -> assert false
618
619      and exists conclude =
620        let proof = 
621          (match conclude.Con.conclude_args with
622              [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
623            | _ -> assert false;
624              (* 
625              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
626              assert false *)) in
627        match proof.Con.proof_context with
628            `Declaration decl::`Hypothesis hyp::tl
629          | `Hypothesis decl::`Hypothesis hyp::tl ->
630            let get_name decl =
631              (match decl.Con.dec_name with
632                 None -> "_"
633               | Some s -> s) in
634            let presdecl = 
635              B.H ([],
636                [(B.b_kw "let");
637                 B.skip;
638                 B.Object ([], P.Mi([],get_name decl));
639                 B.Text([],":"); term2pres decl.Con.dec_type]) in
640            let suchthat =
641              B.H ([],
642                [(B.b_kw "such that");
643                 B.skip;
644                 B.Text([],"(");
645                 B.Object ([], P.Mi([],get_name hyp));
646                 B.Text([],")");
647                 B.skip;
648                 term2pres hyp.Con.dec_type]) in
649             (* let body = proof2pres {proof with Con.proof_context = tl} in *)
650             let body = conclude2pres proof.Con.proof_conclude false true in
651             let presacontext = 
652               acontext2pres proof.Con.proof_apply_context body false in
653             B.V 
654               ([],
655                [presdecl;
656                 suchthat;
657                 presacontext]);
658          | _ -> assert false
659
660     in
661     proof2pres p
662
663 exception ToDo
664
665 let counter = ref 0
666
667 let conjecture2pres term2pres (id, n, context, ty) =
668  B.b_indent
669   (B.b_hv [Some "helm", "xref", id]
670      ((B.b_toggle [
671         B.b_h [] [B.b_text [] "{...}"; B.b_space];
672         B.b_hv [] (List.map
673           (function
674              | None ->
675                  B.b_h []
676                    [ B.b_object (p_mi [] "_") ;
677                      B.b_object (p_mo [] ":?") ;
678                      B.b_object (p_mi [] "_")]
679              | Some (`Declaration d)
680              | Some (`Hypothesis d) ->
681                  let { Content.dec_name =
682                      dec_name ; Content.dec_type = ty } = d
683                  in
684                    B.b_h []
685                      [ B.b_object
686                          (p_mi []
687                             (match dec_name with
688                                  None -> "_"
689                                | Some n -> n));
690                        B.b_text [] ":";
691                        term2pres ty ]
692              | Some (`Definition d) ->
693                  let
694                      { Content.def_name = def_name ;
695                        Content.def_term = bo } = d
696                  in
697                    B.b_h []
698                      [ B.b_object (p_mi []
699                                      (match def_name with
700                                           None -> "_"
701                                         | Some n -> n)) ;
702                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
703                        term2pres bo]
704              | Some (`Proof p) ->
705                  let proof_name = p.Content.proof_name in
706                    B.b_h []
707                      [ B.b_object (p_mi []
708                                      (match proof_name with
709                                           None -> "_"
710                                         | Some n -> n)) ;
711                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
712                        proof2pres term2pres p])
713           (List.rev context)) ] ::
714          [ B.b_h []
715            [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
716              B.b_object (p_mi [] (string_of_int n)) ;
717              B.b_text [] ":" ;
718              term2pres ty ]])))
719
720 let metasenv2pres term2pres = function
721   | None -> []
722   | Some metasenv' ->
723       (* Conjectures are in their own table to make *)
724       (* diffing the DOM trees easier.              *)
725       [B.b_v []
726         ((B.b_kw ("Conjectures:" ^
727             (let _ = incr counter; in (string_of_int !counter)))) ::
728          (List.map (conjecture2pres term2pres) metasenv'))]
729
730 let params2pres params =
731   let param2pres uri =
732     B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
733       (UriManager.name_of_uri uri)
734   in
735   let rec spatiate = function
736     | [] -> []
737     | hd :: [] -> [hd]
738     | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
739   in
740   match params with
741   | [] -> []
742   | p ->
743       let params = spatiate (List.map param2pres p) in
744       [B.b_space;
745        B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
746
747 let recursion_kind2pres params kind =
748   let kind =
749     match kind with
750     | `Recursive _ -> "Recursive definition"
751     | `CoRecursive -> "CoRecursive definition"
752     | `Inductive _ -> "Inductive definition"
753     | `CoInductive _ -> "CoInductive definition"
754   in
755   B.b_h [] (B.b_kw kind :: params2pres params)
756
757 let inductive2pres term2pres ind =
758   let constructor2pres decl =
759     B.b_h [] [
760       B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
761       B.b_space;
762       term2pres decl.Content.dec_type
763     ]
764   in
765   B.b_v []
766     (B.b_h [] [
767       B.b_kw (ind.Content.inductive_name ^ " of arity");
768       B.smallskip;
769       term2pres ind.Content.inductive_type ]
770     :: List.map constructor2pres ind.Content.inductive_constructors)
771
772 let joint_def2pres term2pres def =
773   match def with
774   | `Inductive ind -> inductive2pres term2pres ind
775   | _ -> assert false (* ZACK or raise ToDo? *)
776
777 let content2pres term2pres (id,params,metasenv,obj) =
778   match obj with
779   | `Def (Content.Const, thesis, `Proof p) ->
780       let name = get_name p.Content.proof_name in
781       B.b_v
782         [Some "helm","xref","id"]
783         ([ B.b_h [] (B.b_kw ("Proof " ^ name) :: params2pres params);
784            B.b_kw "Thesis:";
785            B.indent (term2pres thesis) ] @
786          metasenv2pres term2pres metasenv @
787          [proof2pres term2pres p])
788   | `Def (_, ty, `Definition body) ->
789       let name = get_name body.Content.def_name in
790       B.b_v
791         [Some "helm","xref","id"]
792         ([B.b_h [] (B.b_kw ("Definition " ^ name) :: params2pres params);
793           B.b_kw "Type:";
794           B.indent (term2pres ty)] @
795           metasenv2pres term2pres metasenv @
796           [B.b_kw "Body:"; term2pres body.Content.def_term])
797   | `Decl (_, `Declaration decl)
798   | `Decl (_, `Hypothesis decl) ->
799       let name = get_name decl.Content.dec_name in
800       B.b_v
801         [Some "helm","xref","id"]
802         ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
803           B.b_kw "Type:";
804           B.indent (term2pres decl.Content.dec_type)] @
805           metasenv2pres term2pres metasenv)
806   | `Joint joint ->
807       B.b_v []
808         (recursion_kind2pres params joint.Content.joint_kind
809         :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
810   | _ -> raise ToDo
811
812 let content2pres ~ids_to_inner_sorts =
813   content2pres
814     (fun annterm ->
815       let ast, ids_to_uris =
816         TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
817       in
818       CicNotationPres.box_of_mpres
819         (CicNotationPres.render ids_to_uris
820           (TermContentPres.pp_ast ast)))
821