]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/content2pres.ml
In order to generate executable declarative scripts, we are now splitting
[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
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                        [B.b_space; B.Text([],"(previous)")]
349                       else []
350                    else [B.b_kw "done"]
351                   ) @ if not omit_dot then [B.Text([],".")] else [])
352             in
353              B.V ([], prequel @ [conclude_body; ann_concl])
354       | _ -> conclude_aux ?skip_initial_lambdas_internal is_top_down conclude
355     in
356      if indent then 
357        B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
358                      [tconclude_body]))
359      else 
360        B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
361
362   and conclude_aux ?skip_initial_lambdas_internal is_top_down conclude =
363     if conclude.Con.conclude_method = "TD_Conversion" then
364       let expected = 
365         (match conclude.Con.conclude_conclusion with 
366            None -> B.Text([],"NO EXPECTED!!!")
367          | Some c -> term2pres c) in
368       let subproof = 
369         (match conclude.Con.conclude_args with
370           [Con.ArgProof p] -> p
371          | _ -> assert false) in
372       let synth = 
373         (match subproof.Con.proof_conclude.Con.conclude_conclusion with
374            None -> B.Text([],"NO SYNTH!!!")
375          | Some c -> (term2pres c)) in
376       B.V 
377         ([],
378         [make_concl "we need to  prove" expected;
379          B.H ([],[make_concl "or equivalently" synth; B.Text([],".")]);
380          proof2pres true subproof false])
381     else if conclude.Con.conclude_method = "BU_Conversion" then
382       assert false
383     else if conclude.Con.conclude_method = "Exact" then
384       let arg = 
385         (match conclude.Con.conclude_args with 
386            [Con.Term (b,t)] -> assert (not b);term2pres t
387          | [Con.Premise p] -> 
388              (match p.Con.premise_binder with
389              | None -> assert false; (* unnamed hypothesis ??? *)
390              | Some s -> B.Text([],s))
391          | err -> assert false) in
392       (match conclude.Con.conclude_conclusion with 
393          None ->
394           B.b_h [] [B.b_kw "by"; B.b_space; arg]
395        | Some c -> 
396           B.b_h [] [B.b_kw "by"; B.b_space; arg]
397        )
398     else if conclude.Con.conclude_method = "Intros+LetTac" then
399       (match conclude.Con.conclude_args with
400          [Con.ArgProof p] ->
401            (match conclude.Con.conclude_args with
402               [Con.ArgProof p] -> 
403                 proof2pres ?skip_initial_lambdas_internal true p false
404             | _ -> assert false)
405        | _ -> assert false)
406 (* OLD CODE 
407       let conclusion = 
408       (match conclude.Con.conclude_conclusion with 
409          None -> B.Text([],"NO Conclusion!!!")
410        | Some c -> term2pres c) in
411       (match conclude.Con.conclude_args with
412          [Con.ArgProof p] -> 
413            B.V 
414             ([None,"align","baseline 1"; None,"equalrows","false";
415               None,"columnalign","left"],
416               [B.H([],[B.Object([],proof2pres p false)]);
417                B.H([],[B.Object([],
418                 (make_concl "we proved 1" conclusion))])]);
419        | _ -> assert false)
420 *)
421     else if (conclude.Con.conclude_method = "Case") then
422       case conclude
423     else if (conclude.Con.conclude_method = "ByInduction") then
424       byinduction conclude
425     else if (conclude.Con.conclude_method = "Exists") then
426       exists conclude
427     else if (conclude.Con.conclude_method = "AndInd") then
428       andind conclude
429     else if (conclude.Con.conclude_method = "FalseInd") then
430       falseind conclude
431     else if conclude.Con.conclude_method = "RewriteLR"
432          || conclude.Con.conclude_method = "RewriteRL" then
433       let justif1,justif2 = 
434         (match (List.nth conclude.Con.conclude_args 6) with
435            Con.ArgProof p -> justification term2pres p
436          | _ -> assert false) in
437       let term1 = 
438         (match List.nth conclude.Con.conclude_args 2 with
439            Con.Term (_,t) -> term2pres t
440          | _ -> assert false) in 
441       let term2 = 
442         (match List.nth conclude.Con.conclude_args 5 with
443            Con.Term (_,t) -> term2pres t
444          | _ -> assert false) in
445 (*
446       B.V ([], 
447          B.H ([],[
448           (B.b_kw "rewrite");
449           B.b_space; term1;
450           B.b_space; (B.b_kw "with");
451           B.b_space; term2;
452           B.b_space; justif1])::
453             match justif2 with None -> [] | Some j -> [B.indent j])
454 *)
455     if  (conclude.Con.conclude_method = "RewriteLR" && is_top_down)
456      || (conclude.Con.conclude_method = "RewriteRL" && not is_top_down) then
457      B.V([], [justif1 ; B.H([],[B.b_kw "we proved (" ; term1 ; B.b_kw "=" ; term2; B.b_kw ") (equality)."]); B.b_kw "by _"])
458     else
459      B.V([], [justif1 ; B.H([],[B.b_kw "we proved (" ; term2 ; B.b_kw "=" ; term1; B.b_kw ") (equality)."]); B.b_kw "by _"])
460 (*CSC: bad idea
461  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])]) *)
462     else if conclude.Con.conclude_method = "Eq_chain" then
463       let justification p =
464 (*
465         if skip_initial_lambdas <> None (* cheating *) then
466           [B.b_kw "by _"]
467         else
468 *)
469           let j1,j2 = justification term2pres p in
470           j1 :: B.b_space :: (match j2 with Some j -> [j] | None -> [])
471       in
472       let rec aux args =
473         match args with
474           | [] -> []
475           | (Con.ArgProof p)::(Con.Term (_,t))::tl -> 
476               B.HOV(RenderingAttrs.indent_attributes `BoxML,([B.b_kw
477               "=";B.b_space;term2pres t;B.b_space]@justification p@
478               (if tl <> [] then [B.Text ([],".")] else [])))::(aux tl)
479           | _ -> assert false 
480       in
481       let hd = 
482         match List.hd conclude.Con.conclude_args with
483           | Con.Term (_,t) -> t 
484           | _ -> assert false 
485       in
486       B.HOV([],[B.b_kw "conclude";B.b_space;term2pres hd; (* B.b_space; *)
487               B.V ([],aux (List.tl conclude.Con.conclude_args))])
488     else if conclude.Con.conclude_method = "Apply" then
489       let pres_args = 
490         make_args_for_apply term2pres conclude.Con.conclude_args in
491       B.H([],
492         (B.b_kw "by")::
493         B.b_space::
494         B.Text([],"(")::pres_args@[B.Text([],")")])
495     else 
496       B.V ([], [
497         B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
498         (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
499
500   and args2pres l = List.map arg2pres l
501
502   and arg2pres =
503     function
504         Con.Aux n -> B.b_kw ("aux " ^ n)
505       | Con.Premise prem -> B.b_kw "premise"
506       | Con.Lemma lemma -> B.b_kw "lemma"
507       | Con.Term (_,t) -> term2pres t
508       | Con.ArgProof p -> proof2pres true p false
509       | Con.ArgMethod s -> B.b_kw "method"
510  
511    and case conclude =
512      let proof_conclusion = 
513        (match conclude.Con.conclude_conclusion with
514           None -> B.b_kw "No conclusion???"
515         | Some t -> term2pres t) in
516      let arg,args_for_cases = 
517        (match conclude.Con.conclude_args with
518            Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
519              arg,tl
520          | _ -> assert false) in
521      let case_on =
522        let case_arg = 
523          (match arg with
524             Con.Aux n -> B.b_kw "an aux???"
525            | Con.Premise prem ->
526               (match prem.Con.premise_binder with
527                  None -> B.b_kw "the previous result"
528                | Some n -> B.Object ([], P.Mi([],n)))
529            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
530            | Con.Term (_,t) -> 
531                term2pres t
532            | Con.ArgProof p -> B.b_kw "a proof???"
533            | Con.ArgMethod s -> B.b_kw "a method???")
534       in
535         (make_concl "we proceed by cases on" case_arg) in
536      let to_prove =
537         (make_concl "to prove" proof_conclusion) in
538      B.V ([], case_on::to_prove::(make_cases args_for_cases))
539
540    and byinduction conclude =
541      let proof_conclusion = 
542        (match conclude.Con.conclude_conclusion with
543           None -> B.b_kw "No conclusion???"
544         | Some t -> term2pres t) in
545      let inductive_arg,args_for_cases = 
546        (match conclude.Con.conclude_args with
547            Con.Aux(n)::_::tl ->
548              let l1,l2 = split (int_of_string n) tl in
549              let last_pos = (List.length l2)-1 in
550              List.nth l2 last_pos,l1
551          | _ -> assert false) in
552      let induction_on =
553        let arg = 
554          (match inductive_arg with
555             Con.Aux n -> B.b_kw "an aux???"
556            | Con.Premise prem ->
557               (match prem.Con.premise_binder with
558                  None -> B.b_kw "the previous result"
559                | Some n -> B.Object ([], P.Mi([],n)))
560            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
561            | Con.Term (_,t) -> 
562                term2pres t
563            | Con.ArgProof p -> B.b_kw "a proof???"
564            | Con.ArgMethod s -> B.b_kw "a method???") in
565         (make_concl "we proceed by induction on" arg) in
566      let to_prove =
567       B.H ([], [make_concl "to prove" proof_conclusion ; B.Text([],".")]) in
568      B.V ([], induction_on::to_prove::(make_cases args_for_cases))
569
570     and make_cases l = List.map make_case l
571
572     and make_case =  
573       function 
574         Con.ArgProof p ->
575           let name =
576             (match p.Con.proof_name with
577                None -> B.b_kw "no name for case!!"
578              | Some n -> B.Object ([], P.Mi([],n))) in
579           let indhyps,args =
580              List.partition 
581                (function
582                    `Hypothesis h -> h.Con.dec_inductive
583                  | _ -> false) p.Con.proof_context in
584           let pattern_aux =
585              List.fold_right
586                (fun e p -> 
587                   let dec  = 
588                     (match e with 
589                        `Declaration h 
590                      | `Hypothesis h -> 
591                          let name = get_name h.Con.dec_name in
592                          [B.b_space;
593                           B.Text([],"(");
594                           B.Object ([], P.Mi ([],name));
595                           B.Text([],":");
596                           (term2pres h.Con.dec_type);
597                           B.Text([],")")]
598                      | _ -> assert false (*[B.Text ([],"???")]*)) in
599                   dec@p) args [] in
600           let pattern = 
601             B.H ([],
602                (B.b_kw "case"::B.b_space::name::pattern_aux)@
603                 [B.b_space;
604                  B.Text([], ".")]) in
605           let subconcl = 
606             (match p.Con.proof_conclude.Con.conclude_conclusion with
607                None -> B.b_kw "No conclusion!!!"
608              | Some t -> term2pres t) in
609           let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
610           let induction_hypothesis = 
611             (match indhyps with
612               [] -> []
613             | _ -> 
614                let text = B.indent (B.b_kw "by induction hypothesis we know") in
615                let make_hyp =
616                  function 
617                    `Hypothesis h ->
618                      let name = 
619                        (match h.Con.dec_name with
620                           None -> "useless"
621                         | Some s -> s) in
622                      B.indent (B.H ([],
623                        [term2pres h.Con.dec_type;
624                         B.b_space;
625                         B.Text([],"(");
626                         B.Object ([], P.Mi ([],name));
627                         B.Text([],")");
628                         B.Text([],".")]))
629                    | _ -> assert false in
630                let hyps = List.map make_hyp indhyps in
631                text::hyps) in          
632           let body =
633            conclude2pres true p.Con.proof_name p.Con.proof_conclude true true false in
634           let presacontext = 
635            let acontext_id =
636             match p.Con.proof_apply_context with
637                [] -> p.Con.proof_conclude.Con.conclude_id
638              | {Con.proof_id = id}::_ -> id
639            in
640             B.Action([None,"type","toggle"],
641               [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
642                 acontext2pres
643                  (p.Con.proof_conclude.Con.conclude_method = "BU_Conversion")
644                  p.Con.proof_apply_context body true
645                  (p.Con.proof_conclude.Con.conclude_method = "BU_Conversion")
646               ]) in
647           B.V ([], pattern::induction_hypothesis@[B.H ([],[asubconcl;B.Text([],".")]);presacontext])
648        | _ -> assert false 
649
650      and falseind conclude =
651        let proof_conclusion = 
652          (match conclude.Con.conclude_conclusion with
653             None -> B.b_kw "No conclusion???"
654           | Some t -> term2pres t) in
655        let case_arg = 
656          (match conclude.Con.conclude_args with
657              [Con.Aux(n);_;case_arg] -> case_arg
658            | _ -> assert false;
659              (* 
660              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
661              assert false *)) in
662        let arg = 
663          (match case_arg with
664              Con.Aux n -> assert false
665            | Con.Premise prem ->
666               (match prem.Con.premise_binder with
667                  None -> [B.b_kw "Contradiction, hence"]
668                | Some n -> 
669                    [ B.Object ([],P.Mi([],n)); B.skip;
670                      B.b_kw "is contradictory, hence"])
671            | Con.Lemma lemma -> 
672                [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
673                  B.b_kw "is contradictory, hence" ]
674            | _ -> assert false) in
675        make_row arg proof_conclusion
676
677      and andind conclude =
678        let proof,case_arg = 
679          (match conclude.Con.conclude_args with
680              [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
681            | _ -> assert false;
682              (* 
683              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
684              assert false *)) in
685        let arg = 
686          (match case_arg with
687              Con.Aux n -> assert false
688            | Con.Premise prem ->
689               (match prem.Con.premise_binder with
690                  None -> []
691                | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
692            | Con.Lemma lemma -> 
693                [(B.b_kw "by");B.skip;
694                 B.Object([], P.Mi([],lemma.Con.lemma_name))]
695            | _ -> assert false) in
696        match proof.Con.proof_context with
697          `Hypothesis hyp1::`Hypothesis hyp2::tl ->
698             let preshyp1 = 
699               B.H ([],
700                [B.Text([],"(");
701                 B.Object ([], P.Mi([],get_name hyp1.Con.dec_name));
702                 B.Text([],")");
703                 B.skip;
704                 term2pres hyp1.Con.dec_type]) in
705             let preshyp2 = 
706               B.H ([],
707                [B.Text([],"(");
708                 B.Object ([], P.Mi([],get_name hyp2.Con.dec_name));
709                 B.Text([],")");
710                 B.skip;
711                 term2pres hyp2.Con.dec_type]) in
712             let body =
713              conclude2pres false proof.Con.proof_name proof.Con.proof_conclude
714               false true false in
715             let presacontext = 
716               acontext2pres false proof.Con.proof_apply_context body false false
717             in
718             B.V 
719               ([],
720                [B.H ([],arg@[B.skip; B.b_kw "we have"]);
721                 preshyp1;
722                 B.b_kw "and";
723                 preshyp2;
724                 presacontext]);
725          | _ -> assert false
726
727      and exists conclude =
728        let proof = 
729          (match conclude.Con.conclude_args with
730              [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
731            | _ -> assert false;
732              (* 
733              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
734              assert false *)) in
735        match proof.Con.proof_context with
736            `Declaration decl::`Hypothesis hyp::tl
737          | `Hypothesis decl::`Hypothesis hyp::tl ->
738            let presdecl = 
739              B.H ([],
740                [(B.b_kw "let");
741                 B.skip;
742                 B.Object ([], P.Mi([],get_name decl.Con.dec_name));
743                 B.Text([],":"); term2pres decl.Con.dec_type]) in
744            let suchthat =
745              B.H ([],
746                [(B.b_kw "such that");
747                 B.skip;
748                 B.Text([],"(");
749                 B.Object ([], P.Mi([],get_name hyp.Con.dec_name));
750                 B.Text([],")");
751                 B.skip;
752                 term2pres hyp.Con.dec_type]) in
753             let body =
754              conclude2pres false proof.Con.proof_name proof.Con.proof_conclude
755               false true false in
756             let presacontext = 
757               acontext2pres false proof.Con.proof_apply_context body false false
758             in
759             B.V 
760               ([],
761                [presdecl;
762                 suchthat;
763                 presacontext]);
764          | _ -> assert false
765
766     in
767     proof2pres
768      ?skip_initial_lambdas_internal:
769        (match skip_initial_lambdas with
770            None -> Some (`Later 0) (* we already printed theorem: *)
771          | Some n -> Some (`Later n))
772      is_top_down p false
773
774 exception ToDo
775
776 let counter = ref 0
777
778 let conjecture2pres term2pres (id, n, context, ty) =
779  B.b_indent
780   (B.b_hv [Some "helm", "xref", id]
781      ((B.b_toggle [
782         B.b_h [] [B.b_text [] "{...}"; B.b_space];
783         B.b_hv [] (List.map
784           (function
785              | None ->
786                  B.b_h []
787                    [ B.b_object (p_mi [] "_") ;
788                      B.b_object (p_mo [] ":?") ;
789                      B.b_object (p_mi [] "_")]
790              | Some (`Declaration d)
791              | Some (`Hypothesis d) ->
792                  let { Content.dec_name =
793                      dec_name ; Content.dec_type = ty } = d
794                  in
795                    B.b_h []
796                      [ B.b_object
797                          (p_mi []
798                             (match dec_name with
799                                  None -> "_"
800                                | Some n -> n));
801                        B.b_text [] ":";
802                        term2pres ty ]
803              | Some (`Definition d) ->
804                  let
805                      { Content.def_name = def_name ;
806                        Content.def_term = bo } = d
807                  in
808                    B.b_h []
809                      [ B.b_object (p_mi []
810                                      (match def_name with
811                                           None -> "_"
812                                         | Some n -> n)) ;
813                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
814                        term2pres bo]
815              | Some (`Proof p) ->
816                  let proof_name = p.Content.proof_name in
817                    B.b_h []
818                      [ B.b_object (p_mi []
819                                      (match proof_name with
820                                           None -> "_"
821                                         | Some n -> n)) ;
822                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
823                        proof2pres true term2pres p])
824           (List.rev context)) ] ::
825          [ B.b_h []
826            [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
827              B.b_object (p_mi [] (string_of_int n)) ;
828              B.b_text [] ":" ;
829              term2pres ty ]])))
830
831 let metasenv2pres term2pres = function
832   | None -> []
833   | Some metasenv' ->
834       (* Conjectures are in their own table to make *)
835       (* diffing the DOM trees easier.              *)
836       [B.b_v []
837         ((B.b_kw ("Conjectures:" ^
838             (let _ = incr counter; in (string_of_int !counter)))) ::
839          (List.map (conjecture2pres term2pres) metasenv'))]
840
841 let params2pres params =
842   let param2pres uri =
843     B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
844       (UriManager.name_of_uri uri)
845   in
846   let rec spatiate = function
847     | [] -> []
848     | hd :: [] -> [hd]
849     | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
850   in
851   match params with
852   | [] -> []
853   | p ->
854       let params = spatiate (List.map param2pres p) in
855       [B.b_space;
856        B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
857
858 let recursion_kind2pres params kind =
859   let kind =
860     match kind with
861     | `Recursive _ -> "Recursive definition"
862     | `CoRecursive -> "CoRecursive definition"
863     | `Inductive _ -> "Inductive definition"
864     | `CoInductive _ -> "CoInductive definition"
865   in
866   B.b_h [] (B.b_kw kind :: params2pres params)
867
868 let inductive2pres term2pres ind =
869   let constructor2pres decl =
870     B.b_h [] [
871       B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
872       B.b_space;
873       term2pres decl.Content.dec_type
874     ]
875   in
876   B.b_v []
877     (B.b_h [] [
878       B.b_kw (ind.Content.inductive_name ^ " of arity");
879       B.smallskip;
880       term2pres ind.Content.inductive_type ]
881     :: List.map constructor2pres ind.Content.inductive_constructors)
882
883 let joint_def2pres term2pres def =
884   match def with
885   | `Inductive ind -> inductive2pres term2pres ind
886   | _ -> assert false (* ZACK or raise ToDo? *)
887
888 let content2pres 
889   ?skip_initial_lambdas ?(skip_thm_and_qed=false) term2pres 
890   (id,params,metasenv,obj) 
891 =
892   match obj with
893   | `Def (Content.Const, thesis, `Proof p) ->
894       let name = get_name p.Content.proof_name in
895       let proof = proof2pres true term2pres ?skip_initial_lambdas p in
896       if skip_thm_and_qed then
897         proof
898       else
899       B.b_v
900         [Some "helm","xref","id"]
901         ([ B.b_h [] (B.b_kw ("theorem " ^ name) :: 
902           params2pres params @ [B.b_kw ":"]);
903            B.H ([],[B.indent (term2pres thesis) ; B.b_kw "." ])] @
904          metasenv2pres term2pres metasenv @
905          [proof ; B.b_kw "qed."])
906   | `Def (_, ty, `Definition body) ->
907       let name = get_name body.Content.def_name in
908       B.b_v
909         [Some "helm","xref","id"]
910         ([B.b_h []
911            (B.b_kw ("definition " ^ name) :: params2pres params @ [B.b_kw ":"]);
912           B.indent (term2pres ty)] @
913           metasenv2pres term2pres metasenv @
914           [B.b_kw ":=";
915            B.indent (term2pres body.Content.def_term);
916            B.b_kw "."])
917   | `Decl (_, `Declaration decl)
918   | `Decl (_, `Hypothesis decl) ->
919       let name = get_name decl.Content.dec_name in
920       B.b_v
921         [Some "helm","xref","id"]
922         ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
923           B.b_kw "Type:";
924           B.indent (term2pres decl.Content.dec_type)] @
925           metasenv2pres term2pres metasenv)
926   | `Joint joint ->
927       B.b_v []
928         (recursion_kind2pres params joint.Content.joint_kind
929         :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
930   | _ -> raise ToDo
931
932 let content2pres 
933   ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts 
934 =
935   content2pres ?skip_initial_lambdas ?skip_thm_and_qed
936     (fun ?(prec=90) annterm ->
937       let ast, ids_to_uris =
938         TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
939       in
940        CicNotationPres.box_of_mpres
941         (CicNotationPres.render ids_to_uris ~prec
942           (TermContentPres.pp_ast ast)))