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