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