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