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