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