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