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