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