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