]> matita.cs.unibo.it Git - helm.git/blob - matita/components/content_pres/content2pres.ml
Most warnings turned into errors and avoided
[helm.git] / matita / 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 let b_ink a = Box.Ink a
50
51 let rec split n l =
52   if n = 0 then [],l
53   else let l1,l2 = 
54     split (n-1) (List.tl l) in
55     (List.hd l)::l1,l2
56   
57 let get_xref = function
58   | `Declaration d  
59   | `Hypothesis d -> d.Con.dec_id
60   | `Proof p -> p.Con.proof_id
61   | `Definition d -> d.Con.def_id
62   | `Joint jo -> jo.Con.joint_id
63
64 let hv_attrs =
65   RenderingAttrs.spacing_attributes `BoxML
66   @ RenderingAttrs.indent_attributes `BoxML
67
68 let make_row items concl =
69   B.b_hv hv_attrs (items @ [ concl ])
70 (*   match concl with 
71       B.V _ -> |+ big! +|
72         B.b_v attrs [B.b_h [] items; B.b_indent concl]
73     | _ ->  |+ small +|
74         B.b_h attrs (items@[B.b_space; concl]) *)
75
76 let make_concl ?(attrs=[]) verb concl =
77   B.b_hv (hv_attrs @ attrs) [ B.b_kw verb; concl ]
78 (*   match concl with 
79       B.V _ -> |+ big! +|
80         B.b_v attrs [ B.b_kw verb; B.b_indent concl]
81     | _ ->  |+ small +|
82         B.b_h attrs [ B.b_kw verb; B.b_space; concl ] *)
83
84 let make_args_for_apply term2pres args =
85  let make_arg_for_apply is_first arg row = 
86   let res =
87    match arg with 
88       Con.Aux _n -> assert false
89     | Con.Premise prem -> 
90         let name = 
91           (match prem.Con.premise_binder with
92              None -> "previous"
93            | Some s -> s) in
94         (B.b_object (P.Mi ([], name)))::row
95     | Con.Lemma lemma -> 
96         let lemma_attrs = [
97           Some "helm", "xref", lemma.Con.lemma_id;
98           Some "xlink", "href", lemma.Con.lemma_uri ]
99         in
100         (B.b_object (P.Mi(lemma_attrs,lemma.Con.lemma_name)))::row 
101     | Con.Term (b,t) -> 
102         if is_first || (not b) then
103           (term2pres t)::row
104         else (B.b_object (P.Mi([],"?")))::row
105     | Con.ArgProof _ 
106     | Con.ArgMethod _ -> 
107         (B.b_object (P.Mi([],"?")))::row
108   in
109    if is_first then res else B.skip::res
110  in
111   match args with 
112     hd::tl -> 
113       make_arg_for_apply true hd 
114         (List.fold_right (make_arg_for_apply false) tl [])
115   | _ -> assert false
116
117 let get_name ?(default="_") = function
118   | Some s -> s
119   | None -> default
120
121 let add_xref id = function
122   | B.Text (attrs, t) -> B.Text (((Some "helm", "xref", id) :: attrs), t)
123   | _ -> assert false (* TODO, add_xref is meaningful for all boxes *)
124
125 let rec justification ~for_rewriting_step ~ignore_atoms term2pres p = 
126   if p.Con.proof_conclude.Con.conclude_method = "Exact" &&
127      ignore_atoms
128   then
129    [], None
130   else if
131    (p.Con.proof_conclude.Con.conclude_method = "Exact" && not ignore_atoms) ||
132    (p.Con.proof_context = [] &&
133     p.Con.proof_apply_context = [] &&
134     p.Con.proof_conclude.Con.conclude_method = "Apply")
135   then
136     let pres_args = 
137       make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args
138     in
139      [B.H([],
140      (*(if for_rewriting_step then (B.b_kw "exact") else (B.b_kw "by"))::*)
141        (B.b_kw "by")::
142        B.b_space::
143        B.Text([],"(")::pres_args@[B.Text([],")")])], None 
144   else
145    [B.H([],
146     if for_rewriting_step then
147      [B.b_kw "proof"]
148     else
149      [B.b_kw "by"; B.b_space; B.b_kw "proof"]
150     )],
151     Some (B.b_toggle [B.b_kw "proof";B.indent (proof2pres true term2pres p)])
152      
153 and proof2pres0 term2pres ?skip_initial_lambdas_internal is_top_down p in_bu_conversion =
154     let indent = 
155       let is_decl e = 
156         (match e with 
157            `Declaration _
158          | `Hypothesis _ -> true
159          | _ -> false) in
160       ((List.filter is_decl p.Con.proof_context) != []) in 
161     let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
162     let concl = 
163       (match p.Con.proof_conclude.Con.conclude_conclusion with
164          None -> None
165        | Some t -> Some (term2pres t)) in
166     let body =
167         let presconclude = 
168           conclude2pres term2pres
169           ?skip_initial_lambdas_internal:
170             (match skip_initial_lambdas_internal with
171                 Some (`Later s) -> Some (`Now s)
172               | _ -> None)
173           is_top_down p.Con.proof_name p.Con.proof_conclude indent
174           omit_conclusion in_bu_conversion in
175         let presacontext = 
176           acontext2pres term2pres
177            (if p.Con.proof_conclude.Con.conclude_method = "BU_Conversion" then
178              is_top_down
179             else
180              false)
181            p.Con.proof_apply_context
182            presconclude indent
183            (p.Con.proof_conclude.Con.conclude_method = "BU_Conversion")
184         in
185         context2pres term2pres
186          (match skip_initial_lambdas_internal with
187              Some (`Now n) -> snd (HExtlib.split_nth n p.Con.proof_context)
188            | _ -> p.Con.proof_context)
189           ~continuation:presacontext
190     in
191 (*
192 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
193 *)
194     match p.Con.proof_name with
195       None -> body
196     | Some name ->
197         let action = 
198          match concl with
199             None -> body
200           | Some ac ->
201              let concl =
202                make_concl ~attrs:[ Some "helm", "xref", p.Con.proof_id ]
203                  "proof of" ac in
204              B.b_toggle [ B.H ([], [concl; B.skip ; B.Text([],"(");
205                       B.Object ([], P.Mi ([],name));
206                       B.Text([],")") ]) ; body ]
207         in
208          B.indent action
209
210 and context2pres term2pres c ~continuation =
211     (* we generate a subtable for each context element, for selection
212        purposes 
213        The table generated by the head-element does not have an xref;
214        the whole context-proof is already selectable *)
215     match c with
216       [] -> continuation
217     | hd::tl -> 
218         let continuation' =
219           List.fold_right
220             (fun ce continuation ->
221               let xref = get_xref ce in
222               B.V([Some "helm", "xref", xref ],
223                 [B.H([Some "helm", "xref", "ce_"^xref],
224                      [ce2pres_in_proof_context_element term2pres ce]);
225                  continuation])) tl continuation in
226          let hd_xref= get_xref hd in
227          B.V([],
228              [B.H([Some "helm", "xref", "ce_"^hd_xref],
229                [ce2pres_in_proof_context_element term2pres hd]);
230              continuation'])
231         
232 and ce2pres_in_joint_context_element term2pres = function
233     | `Inductive _ -> assert false (* TODO *)
234     | (`Declaration _)
235     | (`Hypothesis _)
236     | (`Proof _)
237     | (`Definition _) as x  -> ce2pres term2pres x
238   
239 and ce2pres_in_proof_context_element term2pres = function 
240     | `Joint ho -> 
241       B.H ([],(List.map (ce2pres_in_joint_context_element term2pres) ho.Content.joint_defs))
242     | (`Declaration _)
243     | (`Hypothesis _)
244     | (`Proof _)
245     | (`Definition _) as x  -> ce2pres term2pres x 
246   
247 and ce2pres term2pres =
248     function 
249         `Declaration d -> 
250          let ty = term2pres d.Con.dec_type in
251          B.H ([],
252            [(B.b_kw "assume");
253             B.b_space;
254             B.Object ([], P.Mi([],get_name d.Con.dec_name));
255             B.Text([],":");
256             ty;
257             B.Text([],".")])
258       | `Hypothesis h ->
259           let ty = term2pres h.Con.dec_type in
260           B.H ([],
261             [(B.b_kw "suppose");
262              B.b_space;
263              ty;
264              B.b_space;
265              B.Text([],"(");
266              B.Object ([], P.Mi ([],get_name h.Con.dec_name));
267              B.Text([],")");
268              B.Text([],".")])
269       | `Proof p -> 
270            proof2pres0 term2pres false p false
271       | `Definition d -> 
272           let term = term2pres d.Con.def_term in
273           B.H ([],
274             [ B.b_kw "let"; B.b_space;
275               B.Object ([], P.Mi([],get_name d.Con.def_name));
276               B.Text([],Utf8Macro.unicode_of_tex "\\def");
277               term])
278
279 and acontext2pres term2pres is_top_down ac continuation indent in_bu_conversion =
280    let rec aux =
281     function
282        [] -> continuation
283      | p::tl ->
284         let continuation = aux tl in
285         (* Applicative context get flattened and the "body" of a BU_Conversion
286            is put in the applicative context. Thus two different situations
287            are possible:
288             {method = "BU_Conversion"; applicative_context=[p1; ...; pn]}
289             {method = xxx; applicative_context =
290               [ p1; ...; pn; {method="BU_Conversion"} ; p_{n+1}; ... ; pm ]}
291            In both situations only pn must be processed in in_bu_conversion
292            mode
293         *)
294         let in_bu_conversion =
295          match tl with
296             [] -> in_bu_conversion
297           | p::_ -> p.Con.proof_conclude.Con.conclude_method = "BU_Conversion"
298         in
299         let hd = proof2pres0 term2pres is_top_down p in_bu_conversion in
300         let hd = if indent then B.indent hd else hd in
301          B.V([Some "helm","xref",p.Con.proof_id],
302           [B.H([Some "helm","xref","ace_"^p.Con.proof_id],[hd]);
303            continuation])
304    in aux ac
305
306 and conclude2pres term2pres ?skip_initial_lambdas_internal is_top_down name conclude indent omit_conclusion in_bu_conversion =
307     let tconclude_body = 
308       match conclude.Con.conclude_conclusion with
309         Some t (*when not omit_conclusion or
310          (* CSC: I ignore the omit_conclusion flag in this case.   *)
311          (* CSC: Is this the correct behaviour? In the stylesheets *)
312          (* CSC: we simply generated nothing (i.e. the output type *)
313          (* CSC: of the function should become an option.          *)
314          conclude.Con.conclude_method = "BU_Conversion" *) ->
315           let concl = term2pres t in 
316           if conclude.Con.conclude_method = "BU_Conversion" then
317             B.b_hv []
318              (make_concl "that is equivalent to" concl ::
319                      if is_top_down then [B.b_space ; B.b_kw "done";
320                      B.Text([],".")] else [B.Text([],".")])
321           else if conclude.Con.conclude_method = "FalseInd" then
322            (* false ind is in charge to add the conclusion *)
323            falseind term2pres conclude
324           else  
325             let prequel =
326               if
327                (not is_top_down) &&
328                 conclude.Con.conclude_method = "Intros+LetTac"
329               then
330                 let name = get_name name in
331                  [B.V ([],
332                  [ B.H([],
333                     let expected = 
334                       (match conclude.Con.conclude_conclusion with 
335                          None -> B.Text([],"NO EXPECTED!!!")
336                        | Some c -> term2pres c)
337                     in
338                      [make_concl "we need to prove" expected;
339                       B.skip;
340                       B.Text([],"(");
341                       B.Object ([], P.Mi ([],name));
342                       B.Text([],")");
343                       B.Text ([],".")
344                      ])])]
345               else
346                [] in
347             let conclude_body = 
348               conclude_aux term2pres ?skip_initial_lambdas_internal is_top_down conclude in
349             let ann_concl = 
350               if  conclude.Con.conclude_method = "Intros+LetTac"
351                || conclude.Con.conclude_method = "ByInduction"
352                || conclude.Con.conclude_method = "TD_Conversion"
353                || conclude.Con.conclude_method = "Eq_chain"
354               then
355                B.Text([],"")
356               else if omit_conclusion then 
357                 B.H([], [B.b_kw "done" ; B.Text([],".") ])
358               else
359                 B.b_hv []
360                  ((if not is_top_down || in_bu_conversion then
361                     (make_concl "we proved" concl) ::
362                       if not is_top_down then
363                        let name = get_name ~default:"previous" name in
364                         [B.b_space; B.Text([],"(" ^ name ^ ")")]
365                       else []
366                    else [B.b_kw "done"]
367                   ) @ if not in_bu_conversion then [B.Text([],".")] else [])
368             in
369              B.V ([], prequel @ [conclude_body; ann_concl])
370       | _ -> conclude_aux term2pres ?skip_initial_lambdas_internal is_top_down conclude
371     in
372      if indent then 
373        B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
374                      [tconclude_body]))
375      else 
376        B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
377
378 and conclude_aux term2pres ?skip_initial_lambdas_internal is_top_down conclude =
379     if conclude.Con.conclude_method = "TD_Conversion" then
380       let expected = 
381         (match conclude.Con.conclude_conclusion with 
382            None -> B.Text([],"NO EXPECTED!!!")
383          | Some c -> term2pres c) in
384       let subproof = 
385         (match conclude.Con.conclude_args with
386           [Con.ArgProof p] -> p
387          | _ -> assert false) in
388       let synth = 
389         (match subproof.Con.proof_conclude.Con.conclude_conclusion with
390            None -> B.Text([],"NO SYNTH!!!")
391          | Some c -> (term2pres c)) in
392       B.V 
393         ([],
394         [make_concl "we need to  prove" expected;
395          B.H ([],[make_concl "or equivalently" synth; B.Text([],".")]);
396          proof2pres0 term2pres true subproof false])
397     else if conclude.Con.conclude_method = "BU_Conversion" then
398       assert false
399     else if conclude.Con.conclude_method = "Exact" then
400       let arg = 
401         (match conclude.Con.conclude_args with 
402            [Con.Term (b,t)] -> assert (not b);term2pres t
403          | [Con.Premise p] -> 
404              (match p.Con.premise_binder with
405              | None -> assert false; (* unnamed hypothesis ??? *)
406              | Some s -> B.Text([],s))
407          | _err -> assert false) in
408       (match conclude.Con.conclude_conclusion with 
409          None ->
410           B.b_h [] [B.b_kw "by"; B.b_space; arg]
411        | Some _c -> 
412           B.b_h [] [B.b_kw "by"; B.b_space; arg]
413        )
414     else if conclude.Con.conclude_method = "Intros+LetTac" then
415       (match conclude.Con.conclude_args with
416          [Con.ArgProof _p] ->
417            (match conclude.Con.conclude_args with
418               [Con.ArgProof p] -> 
419                 proof2pres0 term2pres ?skip_initial_lambdas_internal true p false
420             | _ -> assert false)
421        | _ -> assert false)
422 (* OLD CODE 
423       let conclusion = 
424       (match conclude.Con.conclude_conclusion with 
425          None -> B.Text([],"NO Conclusion!!!")
426        | Some c -> term2pres c) in
427       (match conclude.Con.conclude_args with
428          [Con.ArgProof p] -> 
429            B.V 
430             ([None,"align","baseline 1"; None,"equalrows","false";
431               None,"columnalign","left"],
432               [B.H([],[B.Object([],proof2pres p term2pres false)]);
433                B.H([],[B.Object([],
434                 (make_concl "we proved 1" conclusion))])]);
435        | _ -> assert false)
436 *)
437     else if (conclude.Con.conclude_method = "Case") then
438       case term2pres conclude
439     else if (conclude.Con.conclude_method = "ByInduction") then
440       byinduction term2pres conclude
441     else if (conclude.Con.conclude_method = "Exists") then
442       exists term2pres conclude
443     else if (conclude.Con.conclude_method = "AndInd") then
444       andind term2pres conclude
445     else if (conclude.Con.conclude_method = "FalseInd") then
446       falseind term2pres conclude
447     else if conclude.Con.conclude_method = "RewriteLR"
448          || conclude.Con.conclude_method = "RewriteRL" then
449       let justif1,justif2 = 
450         (match (List.nth conclude.Con.conclude_args 6) with
451            Con.ArgProof p ->
452             justification ~for_rewriting_step:true ~ignore_atoms:true
453              term2pres p
454          | _ -> assert false) in
455       let justif =
456        match justif2 with
457           None -> justif1
458         | Some j -> [j]
459       in
460       let index_term1, index_term2 =
461        if conclude.Con.conclude_method = "RewriteLR" then 2,5 else 5,2
462       in
463       let term1 = 
464         (match List.nth conclude.Con.conclude_args index_term1 with
465            Con.Term (_,t) -> term2pres t
466          | _ -> assert false) in 
467       let term2 = 
468         (match List.nth conclude.Con.conclude_args index_term2 with
469            Con.Term (_,t) -> term2pres t
470          | _ -> assert false) in
471       let justif =
472        match justif with
473           [] -> []
474         | _ ->
475          justif @
476           [B.V([],
477             [B.b_kw "we proved (" ;
478              term1 ;
479              B.b_kw "=" ;
480              term2; B.b_kw ") (equality)."])]
481       in
482 (*
483       B.V ([], 
484          B.H ([],[
485           (B.b_kw "rewrite");
486           B.b_space; term1;
487           B.b_space; (B.b_kw "with");
488           B.b_space; term2;
489           B.b_space; justif1])::
490             match justif2 with None -> [] | Some j -> [B.indent j])
491 *)
492       B.V([], justif @ [B.b_kw "by _"])
493     else if conclude.Con.conclude_method = "Eq_chain" then
494       let justification p =
495        let j1,j2 =
496         justification ~for_rewriting_step:true ~ignore_atoms:false term2pres p
497        in
498         j1, match j2 with Some j -> [j] | None -> []
499       in
500       let rec aux args =
501         match args with
502           | [] -> []
503           | (Con.ArgProof p)::(Con.Term (_,t))::tl -> 
504               let justif1,justif2 = justification p in
505                B.HOV(RenderingAttrs.indent_attributes `BoxML,([B.b_kw
506                "=";B.b_space;term2pres t;B.b_space]@justif1@
507                (if tl <> [] then [B.Text ([],".")] else [B.b_space; B.b_kw "done" ; B.Text([],".")])@
508                justif2))::(aux tl)
509           | _ -> assert false 
510       in
511       let hd = 
512         match List.hd conclude.Con.conclude_args with
513           | Con.Term (_,t) -> t 
514           | _ -> assert false 
515       in
516        if is_top_down then
517         B.HOV([],
518          [B.b_kw "conclude";B.b_space;term2pres hd;
519           B.V ([],aux (List.tl conclude.Con.conclude_args))])
520        else
521         B.HOV([],
522          [B.b_kw "obtain";B.b_space;B.b_kw "FIXMEXX"; B.b_space;term2pres hd;
523           B.V ([],aux (List.tl conclude.Con.conclude_args))])
524     else if conclude.Con.conclude_method = "Apply" then
525       let pres_args = 
526         make_args_for_apply term2pres conclude.Con.conclude_args in
527       B.H([],
528         (B.b_kw "by")::
529         B.b_space::
530         B.Text([],"(")::pres_args@[B.Text([],")")])
531     else 
532       B.V ([], [
533         B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
534         (B.indent (B.V ([], args2pres term2pres conclude.Con.conclude_args)))])
535
536 and args2pres term2pres l = List.map (arg2pres term2pres) l
537
538 and arg2pres term2pres =
539     function
540         Con.Aux n -> B.b_kw ("aux " ^ n)
541       | Con.Premise _prem -> B.b_kw "premise"
542       | Con.Lemma _lemma -> B.b_kw "lemma"
543       | Con.Term (_,t) -> term2pres t
544       | Con.ArgProof p -> proof2pres0 term2pres true p false
545       | Con.ArgMethod _s -> B.b_kw "method"
546  
547 and case term2pres conclude =
548      let proof_conclusion = 
549        (match conclude.Con.conclude_conclusion with
550           None -> B.b_kw "No conclusion???"
551         | Some t -> term2pres t) in
552      let arg,args_for_cases = 
553        (match conclude.Con.conclude_args with
554            Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
555              arg,tl
556          | _ -> assert false) in
557      let case_on =
558        let case_arg = 
559          (match arg with
560             Con.Aux _n -> B.b_kw "an aux???"
561            | Con.Premise prem ->
562               (match prem.Con.premise_binder with
563                  None -> B.b_kw "previous"
564                | Some n -> B.Object ([], P.Mi([],n)))
565            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
566            | Con.Term (_,t) -> 
567                term2pres t
568            | Con.ArgProof _p -> B.b_kw "a proof???"
569            | Con.ArgMethod _s -> B.b_kw "a method???")
570       in
571         (make_concl "we proceed by cases on" case_arg) in
572      let to_prove =
573         (make_concl "to prove" proof_conclusion) in
574      B.V ([], case_on::to_prove::(make_cases term2pres args_for_cases))
575
576 and byinduction term2pres conclude =
577      let proof_conclusion = 
578        (match conclude.Con.conclude_conclusion with
579           None -> B.b_kw "No conclusion???"
580         | Some t -> term2pres t) in
581      let inductive_arg,args_for_cases = 
582        (match conclude.Con.conclude_args with
583            Con.Aux(n)::_::tl ->
584              let l1,l2 = split (int_of_string n) tl in
585              let last_pos = (List.length l2)-1 in
586              List.nth l2 last_pos,l1
587          | _ -> assert false) in
588      let induction_on =
589        let arg = 
590          (match inductive_arg with
591             Con.Aux _n -> B.b_kw "an aux???"
592            | Con.Premise prem ->
593               (match prem.Con.premise_binder with
594                  None -> B.b_kw "previous"
595                | Some n -> B.Object ([], P.Mi([],n)))
596            | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
597            | Con.Term (_,t) -> 
598                term2pres t
599            | Con.ArgProof _p -> B.b_kw "a proof???"
600            | Con.ArgMethod _s -> B.b_kw "a method???") in
601         (make_concl "we proceed by induction on" arg) in
602      let to_prove =
603       B.H ([], [make_concl "to prove" proof_conclusion ; B.Text([],".")]) in
604      B.V ([], induction_on::to_prove::(make_cases term2pres args_for_cases))
605
606 and make_cases term2pres l = List.map (make_case term2pres) l
607
608 and make_case term2pres =
609       function 
610         Con.ArgProof p ->
611           let name =
612             (match p.Con.proof_name with
613                None -> B.b_kw "no name for case!!"
614              | Some n -> B.Object ([], P.Mi([],n))) in
615           let indhyps,args =
616              List.partition 
617                (function
618                    `Hypothesis h -> h.Con.dec_inductive
619                  | _ -> false) p.Con.proof_context in
620           let pattern_aux =
621              List.fold_right
622                (fun e p -> 
623                   let dec  = 
624                     (match e with 
625                        `Declaration h 
626                      | `Hypothesis h -> 
627                          let name = get_name h.Con.dec_name in
628                          [B.b_space;
629                           B.Text([],"(");
630                           B.Object ([], P.Mi ([],name));
631                           B.Text([],":");
632                           (term2pres h.Con.dec_type);
633                           B.Text([],")")]
634                      | _ -> assert false (*[B.Text ([],"???")]*)) in
635                   dec@p) args [] in
636           let pattern = 
637             B.H ([],
638                (B.b_kw "case"::B.b_space::name::pattern_aux)@
639                 [B.b_space;
640                  B.Text([], ".")]) in
641           let subconcl = 
642             (match p.Con.proof_conclude.Con.conclude_conclusion with
643                None -> B.b_kw "No conclusion!!!"
644              | Some t -> term2pres t) in
645           let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
646           let induction_hypothesis = 
647             (match indhyps with
648               [] -> []
649             | _ -> 
650                let text = B.indent (B.b_kw "by induction hypothesis we know") in
651                let make_hyp =
652                  function 
653                    `Hypothesis h ->
654                      let name = 
655                        (match h.Con.dec_name with
656                           None -> "useless"
657                         | Some s -> s) in
658                      B.indent (B.H ([],
659                        [term2pres h.Con.dec_type;
660                         B.b_space;
661                         B.Text([],"(");
662                         B.Object ([], P.Mi ([],name));
663                         B.Text([],")");
664                         B.Text([],".")]))
665                    | _ -> assert false in
666                let hyps = List.map make_hyp indhyps in
667                text::hyps) in          
668           let body =
669            conclude2pres term2pres true p.Con.proof_name p.Con.proof_conclude true true false in
670           let presacontext = 
671            let acontext_id =
672             match p.Con.proof_apply_context with
673                [] -> p.Con.proof_conclude.Con.conclude_id
674              | {Con.proof_id = id}::_ -> id
675            in
676             B.Action([None,"type","toggle"],
677               [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
678                 acontext2pres term2pres
679                  (p.Con.proof_conclude.Con.conclude_method = "BU_Conversion")
680                  p.Con.proof_apply_context body true
681                  (p.Con.proof_conclude.Con.conclude_method = "BU_Conversion")
682               ]) in
683           B.V ([], pattern::induction_hypothesis@[B.H ([],[asubconcl;B.Text([],".")]);presacontext])
684        | _ -> assert false 
685
686 and falseind term2pres conclude =
687        let proof_conclusion = 
688          (match conclude.Con.conclude_conclusion with
689             None -> B.b_kw "No conclusion???"
690           | Some t -> term2pres t) in
691        let case_arg = 
692          (match conclude.Con.conclude_args with
693              [Con.Aux(_n);_;case_arg] -> case_arg
694            | _ -> assert false;
695              (* 
696              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
697              assert false *)) in
698        let arg = 
699          (match case_arg with
700              Con.Aux _n -> assert false
701            | Con.Premise prem ->
702               (match prem.Con.premise_binder with
703                  None -> [B.b_kw "Contradiction, hence"]
704                | Some n -> 
705                    [ B.Object ([],P.Mi([],n)); B.skip;
706                      B.b_kw "is contradictory, hence"])
707            | Con.Lemma lemma -> 
708                [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
709                  B.b_kw "is contradictory, hence" ]
710            | _ -> assert false) in
711        make_row arg proof_conclusion
712
713 and andind term2pres conclude =
714        let proof,case_arg = 
715          (match conclude.Con.conclude_args with
716              [Con.Aux(_n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
717            | _ -> assert false;
718              (* 
719              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
720              assert false *)) in
721        let arg = 
722          (match case_arg with
723              Con.Aux _n -> assert false
724            | Con.Premise prem ->
725               (match prem.Con.premise_binder with
726                  None -> []
727                | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
728            | Con.Lemma lemma -> 
729                [(B.b_kw "by");B.skip;
730                 B.Object([], P.Mi([],lemma.Con.lemma_name))]
731            | _ -> assert false) in
732        match proof.Con.proof_context with
733          `Hypothesis hyp1::`Hypothesis hyp2::_tl ->
734             let preshyp1 = 
735               B.H ([],
736                [B.Text([],"(");
737                 B.Object ([], P.Mi([],get_name hyp1.Con.dec_name));
738                 B.Text([],")");
739                 B.skip;
740                 term2pres hyp1.Con.dec_type]) in
741             let preshyp2 = 
742               B.H ([],
743                [B.Text([],"(");
744                 B.Object ([], P.Mi([],get_name hyp2.Con.dec_name));
745                 B.Text([],")");
746                 B.skip;
747                 term2pres hyp2.Con.dec_type]) in
748             let body =
749              conclude2pres term2pres false proof.Con.proof_name proof.Con.proof_conclude
750               false true false in
751             let presacontext = 
752               acontext2pres term2pres false proof.Con.proof_apply_context body false false
753             in
754             B.V 
755               ([],
756                [B.H ([],arg@[B.skip; B.b_kw "we have"]);
757                 preshyp1;
758                 B.b_kw "and";
759                 preshyp2;
760                 presacontext]);
761          | _ -> assert false
762
763 and exists term2pres conclude =
764        let proof = 
765          (match conclude.Con.conclude_args with
766              [Con.Aux(_n);_;Con.ArgProof proof;_] -> proof
767            | _ -> assert false;
768              (* 
769              List.map (ContentPp.parg 0) conclude.Con.conclude_args;
770              assert false *)) in
771        match proof.Con.proof_context with
772            `Declaration decl::`Hypothesis hyp::_tl
773          | `Hypothesis decl::`Hypothesis hyp::_tl ->
774            let presdecl = 
775              B.H ([],
776                [(B.b_kw "let");
777                 B.skip;
778                 B.Object ([], P.Mi([],get_name decl.Con.dec_name));
779                 B.Text([],":"); term2pres decl.Con.dec_type]) in
780            let suchthat =
781              B.H ([],
782                [(B.b_kw "such that");
783                 B.skip;
784                 B.Text([],"(");
785                 B.Object ([], P.Mi([],get_name hyp.Con.dec_name));
786                 B.Text([],")");
787                 B.skip;
788                 term2pres hyp.Con.dec_type]) in
789             let body =
790              conclude2pres term2pres false proof.Con.proof_name proof.Con.proof_conclude
791               false true false in
792             let presacontext = 
793               acontext2pres term2pres false proof.Con.proof_apply_context body false false
794             in
795             B.V 
796               ([],
797                [presdecl;
798                 suchthat;
799                 presacontext]);
800          | _ -> assert false
801
802 and proof2pres ?skip_initial_lambdas is_top_down term2pres p =
803   proof2pres0 term2pres
804    ?skip_initial_lambdas_internal:
805      (match skip_initial_lambdas with
806          None -> Some (`Later 0) (* we already printed theorem: *)
807        | Some n -> Some (`Later n))
808    is_top_down p false
809
810 exception ToDo
811
812 let counter = ref 0
813
814 let conjecture2pres term2pres (id, n, context, ty) =
815  B.b_indent
816   (B.b_hv [Some "helm", "xref", id]
817      ((B.b_toggle [
818         B.b_h [] [B.b_text [] "{...}"; B.b_space];
819         B.b_hv [] (HExtlib.list_concat ~sep:[B.b_text [] ";"; B.b_space] 
820          (List.map (fun x -> [x])
821          (List.map
822           (function
823              | None ->
824                  B.b_h []
825                    [ B.b_object (p_mi [] "_") ;
826                      B.b_object (p_mo [] ":?") ;
827                      B.b_object (p_mi [] "_")]
828              | Some (`Declaration d)
829              | Some (`Hypothesis d) ->
830                  let { Content.dec_name =
831                      dec_name ; Content.dec_type = ty } = d
832                  in
833                    B.b_h []
834                      [ B.b_object
835                          (p_mi []
836                             (match dec_name with
837                                  None -> "_"
838                                | Some n -> n));
839                        B.b_text [] ":"; B.b_space; 
840                        term2pres ty ]
841              | Some (`Definition d) ->
842                  let
843                      { Content.def_name = def_name ;
844                        Content.def_term = bo } = d
845                  in
846                    B.b_h []
847                      [ B.b_object (p_mi []
848                                      (match def_name with
849                                           None -> "_"
850                                         | Some n -> n)) ;
851                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
852                        B.b_space;
853                        term2pres bo]
854              | Some (`Proof p) ->
855                  let proof_name = p.Content.proof_name in
856                    B.b_h []
857                      [ B.b_object (p_mi []
858                                      (match proof_name with
859                                           None -> "_"
860                                         | Some n -> n)) ;
861                        B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
862                        B.b_space;
863                        proof2pres true term2pres p])
864           (List.rev context)))) ] ::
865          [ B.b_h []
866            [ B.b_space; 
867              B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
868              B.b_space;
869              B.b_object (p_mi [] (string_of_int n)) ;
870              B.b_text [] ":" ;
871              B.b_space;
872              term2pres ty ]])))
873
874 let metasenv2pres term2pres = function
875   | None -> []
876   | Some metasenv' ->
877       (* Conjectures are in their own table to make *)
878       (* diffing the DOM trees easier.              *)
879       [B.b_v []
880         ((B.b_kw ("Conjectures:" ^
881             (let _ = incr counter; in (string_of_int !counter)))) ::
882          (List.map (conjecture2pres term2pres) metasenv'))]
883
884 let inductive2pres term2pres ind =
885   let constructor2pres decl =
886     B.b_h [] [
887       B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
888       B.b_space;
889       term2pres decl.Content.dec_type
890     ]
891   in
892   B.b_v []
893     (B.b_h [] [
894       B.b_kw (ind.Content.inductive_name ^ " of arity");
895       B.smallskip;
896       term2pres ind.Content.inductive_type ]
897     :: List.map constructor2pres ind.Content.inductive_constructors)
898
899 let definition2pres ?recno term2pres d =
900   let name = match d.Content.def_name with Some x -> x|_->assert false in
901   let rno = match recno with None -> -1 (* cofix *) | Some x -> x in
902   let ty = d.Content.def_type in
903   let module P = NotationPt in
904   let rec split_pi i t =
905     (* dummy case for corecursive defs *)
906     if i = ~-1 then [], P.UserInput, t
907     else if i <= 1 then 
908       match t with 
909       | P.Binder ((`Pi|`Forall),(var,_ as v),t) 
910       | P.AttributedTerm (_,P.Binder ((`Pi|`Forall),(var,_ as v),t)) -> 
911             [v], var, t 
912       | _ -> assert false
913     else
914       match t with
915       | P.Binder ((`Pi|`Forall), var ,ty) 
916       | P.AttributedTerm (_, P.Binder ((`Pi|`Forall), var ,ty)) ->
917            let l, r, t = split_pi (i-1) ty in
918            var :: l, r, t
919       | _ -> assert false
920   in 
921   let params, rec_param, ty = split_pi rno ty in
922   let body = d.Content.def_term in
923   let params = 
924     List.map 
925       (function 
926       | (name,Some ty) -> 
927           B.b_h [] [B.b_text [] "("; term2pres name; B.b_text [] " : ";
928                     B.b_space; term2pres ty; B.b_text [] ")"; B.b_space]
929       | (name,None) -> B.b_h [] [term2pres name;B.b_space]) 
930       params
931   in
932   B.b_hv [] 
933    [B.b_hov (RenderingAttrs.indent_attributes `BoxML)
934     ( [B.b_hov (RenderingAttrs.indent_attributes `BoxML) ([ B.b_space; B.b_text [] name ] @ 
935         if params <> [] then
936            [B.indent(B.b_hov (RenderingAttrs.indent_attributes `BoxML) (params))]
937         else [])] 
938       @ [B.b_h [] 
939          ((if rno <> -1 then 
940            [B.b_kw "on";B.b_space;term2pres rec_param] else [])
941          @ [ B.b_space; B.b_text [] ":";]) ]
942       @[ B.indent(term2pres ty)]); 
943    B.b_text [] ":=";
944    B.b_h [] [B.b_space;term2pres body] ]
945 ;;
946
947 let njoint_def2pres ?recno term2pres def =
948   match def with
949   | `Inductive ind -> inductive2pres term2pres ind
950   | `Definition def -> definition2pres ?recno term2pres def 
951   | _ -> assert false
952 ;;
953
954 let njoint_def2pres term2pres joint_kind defs =
955   match joint_kind with
956   | `Recursive recnos ->
957       B.b_hv [] (B.b_kw "nlet rec " ::
958         List.flatten
959          (HExtlib.list_mapi (fun x i -> 
960             if i > 0 then [B.b_kw " and ";x] else [x])
961           (List.map2 (fun a b -> njoint_def2pres ~recno:a term2pres b) 
962             recnos defs)))
963   | `CoRecursive ->
964       B.b_hv [] (B.b_kw "nlet corec " ::
965         List.flatten
966          (HExtlib.list_mapi (fun x i -> 
967             if i > 0 then [B.b_kw " and ";x] else [x])
968           (List.map (njoint_def2pres term2pres) defs)))
969   | `Inductive _ -> 
970       B.b_hv [] (B.b_kw "ninductive " ::
971         List.flatten
972          (HExtlib.list_mapi (fun x i -> 
973             if i > 0 then [B.b_kw " and ";x] else [x])
974           (List.map (njoint_def2pres term2pres) defs)))
975   | `CoInductive _ -> 
976       B.b_hv [] (B.b_kw "ncoinductive " ::
977         List.flatten
978          (HExtlib.list_mapi (fun x i -> 
979             if i > 0 then [B.b_kw " and ";x] else [x])
980           (List.map (njoint_def2pres term2pres) defs)))
981 ;;
982
983 let nobj2pres0
984   ?skip_initial_lambdas ?(skip_thm_and_qed=false) term2pres 
985   (_id,metasenv,obj : NotationPt.term Content.cobj) 
986 =
987   match obj with
988   | `Def (Content.Const, thesis, `Proof p) ->
989       let name = get_name p.Content.proof_name in
990       let proof = proof2pres true ?skip_initial_lambdas term2pres p in
991       if skip_thm_and_qed then
992         proof
993       else
994       B.b_v
995         [Some "helm","xref","id"]
996         ([ B.b_h [] (B.b_kw ("ntheorem " ^ name) :: 
997           [B.b_kw ":"]);
998            B.H ([],[B.indent (term2pres thesis) ; B.b_kw "." ])] @
999          metasenv2pres term2pres metasenv @
1000          [proof ; B.b_kw "qed."])
1001   | `Def (_, ty, `Definition body) ->
1002       let name = get_name body.Content.def_name in
1003       B.b_v
1004         [Some "helm","xref","id"]
1005         ([B.b_h []
1006            (B.b_kw ("ndefinition " ^ name) :: [B.b_kw ":"]);
1007           B.indent (term2pres ty)] @
1008           metasenv2pres term2pres metasenv @
1009           [B.b_kw ":=";
1010            B.indent (term2pres body.Content.def_term);
1011            B.b_kw "."])
1012   | `Decl (_, `Declaration decl)
1013   | `Decl (_, `Hypothesis decl) ->
1014       let name = get_name decl.Content.dec_name in
1015       B.b_v
1016         [Some "helm","xref","id"]
1017         ([B.b_h [] (B.b_kw ("naxiom " ^ name) :: []);
1018           B.b_kw "Type:";
1019           B.indent (term2pres decl.Content.dec_type)] @
1020           metasenv2pres term2pres metasenv)
1021   | `Joint joint ->
1022       B.b_v [] 
1023         [njoint_def2pres term2pres 
1024           joint.Content.joint_kind joint.Content.joint_defs]
1025   | _ -> raise ToDo
1026
1027 let nterm2pres status ~ids_to_nrefs =
1028  let lookup_uri id =
1029   try
1030    let nref = Hashtbl.find ids_to_nrefs id in
1031     Some (NReference.string_of_reference nref)
1032   with Not_found -> None
1033  in
1034   fun ?(prec=90) ast ->
1035    CicNotationPres.box_of_mpres
1036     (CicNotationPres.render status ~lookup_uri ~prec
1037       (TermContentPres.pp_ast status ast))
1038
1039 let nobj2pres status ~ids_to_nrefs =
1040  nobj2pres0 ?skip_initial_lambdas:None ?skip_thm_and_qed:None
1041   (nterm2pres status ~ids_to_nrefs)
1042
1043 let nconjlist2pres0 term2pres context = 
1044  let rec aux accum =
1045   function 
1046     [] -> accum 
1047   | None::tl -> aux accum tl
1048   | (Some (`Declaration d))::tl ->
1049       let
1050         { Con.dec_name = dec_name ;
1051           Con.dec_id = dec_id ;
1052           Con.dec_type = ty } = d in
1053       let r = 
1054         Box.b_hv [Some "helm", "xref", dec_id] 
1055           [ Box.b_h []
1056             [ Box.b_object (p_mi []
1057                (match dec_name with
1058                   None -> "_"
1059                | Some n -> n)) ;
1060               Box.b_space; Box.b_text [] ":"; ];
1061               Box.indent (term2pres ty)] in
1062       aux (r::accum) tl
1063   | (Some (`Definition d))::tl ->
1064       let
1065         { Con.def_name = def_name ;
1066           Con.def_id = def_id ;
1067           Con.def_term = bo } = d in
1068       let r = 
1069         Box.b_hov [Some "helm", "xref", def_id]
1070            [ Box.b_object (p_mi []
1071              (match def_name with
1072                 None -> "_"
1073               | Some n -> n)) ; Box.b_space ;
1074              Box.b_text [] (Utf8Macro.unicode_of_tex "\\def") ;
1075              Box.indent (term2pres bo)] in
1076       aux (r::accum) tl
1077    | _::_ -> assert false
1078  in
1079   if context <> [] then [Box.b_v [] (aux [] context)] else []
1080
1081 let sequent2pres0 term2pres (_,_,context,ty) =
1082  let pres_context = nconjlist2pres0 term2pres context in
1083  let pres_goal = term2pres ty in 
1084  (Box.b_h [] [
1085    Box.b_space; 
1086    (Box.b_v []
1087       (Box.b_space ::
1088        pres_context @ [
1089        b_ink [None,"width","4cm"; None,"height","2px"]; (* sequent line *)
1090        Box.b_space; 
1091        pres_goal]))])
1092
1093 let ncontext2pres status ~ids_to_nrefs ctx =
1094  let ctx = HExtlib.filter_map (fun x -> x) ctx in
1095  context2pres (nterm2pres status ~ids_to_nrefs) ~continuation:Box.smallskip
1096   (ctx:>NotationPt.term Con.in_proof_context_element list)
1097
1098 let nsequent2pres status ~ids_to_nrefs =
1099  sequent2pres0 (nterm2pres status ~ids_to_nrefs)