]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/content2pres.ml
parameter sintax added to axiom statement
[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 ~for_rewriting_step ~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      (*(if for_rewriting_step then (B.b_kw "exact") else (B.b_kw "by"))::*)
140        (B.b_kw "by")::
141        B.b_space::
142        B.Text([],"(")::pres_args@[B.Text([],")")])], None 
143   else
144    [B.H([],
145     if for_rewriting_step then
146      [B.b_kw "proof"]
147     else
148      [B.b_kw "by"; B.b_space; B.b_kw "proof"]
149     )],
150     Some (B.b_toggle [B.b_kw "proof";B.indent (proof2pres true term2pres p)])
151      
152 and proof2pres ?skip_initial_lambdas is_top_down term2pres p =
153   let rec proof2pres ?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
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
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 
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           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 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 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 hd]);
230              continuation'])
231         
232   and ce2pres_in_joint_context_element = function
233     | `Inductive _ -> assert false (* TODO *)
234     | (`Declaration _) as x -> ce2pres x
235     | (`Hypothesis _) as x  -> ce2pres x
236     | (`Proof _) as x       -> ce2pres x
237     | (`Definition _) as x  -> ce2pres x
238   
239   and ce2pres_in_proof_context_element = function 
240     | `Joint ho -> 
241       B.H ([],(List.map ce2pres_in_joint_context_element ho.Content.joint_defs))
242     | (`Declaration _) as x -> ce2pres x 
243     | (`Hypothesis _) as x  -> ce2pres x 
244     | (`Proof _) as x       -> ce2pres x
245     | (`Definition _) as x  -> ce2pres x 
246   
247   and ce2pres =
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            proof2pres 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 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 = proof2pres 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 ?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 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 ?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 ?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 ?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          proof2pres 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                 proof2pres ?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 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 conclude
439     else if (conclude.Con.conclude_method = "ByInduction") then
440       byinduction conclude
441     else if (conclude.Con.conclude_method = "Exists") then
442       exists conclude
443     else if (conclude.Con.conclude_method = "AndInd") then
444       andind conclude
445     else if (conclude.Con.conclude_method = "FalseInd") then
446       falseind 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 conclude.Con.conclude_args)))])
535
536   and args2pres l = List.map arg2pres l
537
538   and arg2pres =
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 -> proof2pres true p false
545       | Con.ArgMethod s -> B.b_kw "method"
546  
547    and case 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 args_for_cases))
575
576    and byinduction 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 args_for_cases))
605
606     and make_cases l = List.map make_case l
607
608     and make_case =  
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 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
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 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 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 false proof.Con.proof_name proof.Con.proof_conclude
750               false true false in
751             let presacontext = 
752               acontext2pres 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 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 false proof.Con.proof_name proof.Con.proof_conclude
791               false true false in
792             let presacontext = 
793               acontext2pres 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     in
803     proof2pres
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 params2pres params =
885   let param2pres uri =
886     B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
887       (UriManager.name_of_uri uri)
888   in
889   let rec spatiate = function
890     | [] -> []
891     | hd :: [] -> [hd]
892     | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
893   in
894   match params with
895   | [] -> []
896   | p ->
897       let params = spatiate (List.map param2pres p) in
898       [B.b_space;
899        B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
900
901 let recursion_kind2pres params kind =
902   let kind =
903     match kind with
904     | `Recursive _ -> "Recursive definition"
905     | `CoRecursive -> "CoRecursive definition"
906     | `Inductive i -> 
907         "Inductive definition with "^string_of_int i^" fixed parameter(s)"
908     | `CoInductive i -> 
909         "Co-Inductive definition with "^string_of_int i^" fixed parameter(s)"
910   in
911   B.b_h [] (B.b_kw kind :: params2pres params)
912 ;;
913
914 let inductive2pres term2pres ind =
915   let constructor2pres decl =
916     B.b_h [] [
917       B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
918       B.b_space;
919       term2pres decl.Content.dec_type
920     ]
921   in
922   B.b_v []
923     (B.b_h [] [
924       B.b_kw (ind.Content.inductive_name ^ " of arity");
925       B.smallskip;
926       term2pres ind.Content.inductive_type ]
927     :: List.map constructor2pres ind.Content.inductive_constructors)
928
929 let definition2pres ?recno term2pres d =
930   let name = match d.Content.def_name with Some x -> x|_->assert false in
931   let rno = match recno with None -> -1 (* cofix *) | Some x -> x in
932   let ty = d.Content.def_type in
933   let module P = CicNotationPt in
934   let rec split_pi i t = 
935     if i <= 1 then 
936       match t with 
937       | P.Binder ((`Pi|`Forall),(var,_ as v),t) 
938       | P.AttributedTerm (_,P.Binder ((`Pi|`Forall),(var,_ as v),t)) -> 
939             [v], var, t 
940       | _ -> assert false
941     else
942       match t with
943       | P.Binder ((`Pi|`Forall), var ,ty) 
944       | P.AttributedTerm (_, P.Binder ((`Pi|`Forall), var ,ty)) ->
945            let l, r, t = split_pi (i-1) ty in
946            var :: l, r, t
947       | _ -> assert false
948   in 
949   let params, rec_param, ty = split_pi rno ty in
950   let body = d.Content.def_term in
951   let params = 
952     List.map 
953       (function 
954       | (name,Some ty) -> 
955           B.b_h [] [B.b_text [] "("; term2pres name; B.b_text [] " : ";
956                     B.b_space; term2pres ty; B.b_text [] ")"; B.b_space]
957       | (name,None) -> B.b_h [] [term2pres name;B.b_space]) 
958       params
959   in
960   B.b_hv [] 
961    [B.b_hov (RenderingAttrs.indent_attributes `BoxML)
962     ( [B.b_hov (RenderingAttrs.indent_attributes `BoxML) ([ B.b_space; B.b_text [] name ] @ 
963         [B.indent(B.b_hov (RenderingAttrs.indent_attributes `BoxML) (params))])] 
964       @ [B.b_h [] 
965          ((if rno <> -1 then 
966            [B.b_kw "on";B.b_space;term2pres rec_param] else [])
967          @ [ B.b_space; B.b_text [] ":";]) ]
968       @[ B.indent(term2pres ty)]); 
969    B.b_text [] ":=";
970    B.b_h [] [B.b_space;term2pres body] ]
971 ;;
972
973 let joint_def2pres ?recno term2pres def =
974   match def with
975   | `Inductive ind -> inductive2pres term2pres ind
976   | _ -> assert false
977 ;;
978
979 let njoint_def2pres ?recno term2pres def =
980   match def with
981   | `Inductive ind -> inductive2pres term2pres ind
982   | `Definition def -> definition2pres ?recno term2pres def 
983   | _ -> assert false
984 ;;
985
986 let njoint_def2pres term2pres joint_kind defs =
987   match joint_kind with
988   | `Recursive recnos ->
989       B.b_hv [] (B.b_kw "nlet rec " ::
990         List.flatten
991          (HExtlib.list_mapi (fun x i -> 
992             if i > 0 then [B.b_kw " and ";x] else [x])
993           (List.map2 (fun a b -> njoint_def2pres ~recno:a term2pres b) 
994             recnos defs)))
995   | `CoRecursive ->
996       B.b_hv [] (B.b_kw "nlet corec " ::
997         List.flatten
998          (HExtlib.list_mapi (fun x i -> 
999             if i > 0 then [B.b_kw " and ";x] else [x])
1000           (List.map (njoint_def2pres term2pres) defs)))
1001   | `Inductive _ -> 
1002       B.b_hv [] (B.b_kw "ninductive " ::
1003         List.flatten
1004          (HExtlib.list_mapi (fun x i -> 
1005             if i > 0 then [B.b_kw " and ";x] else [x])
1006           (List.map (njoint_def2pres term2pres) defs)))
1007   | `CoInductive _ -> 
1008       B.b_hv [] (B.b_kw "ncoinductive " ::
1009         List.flatten
1010          (HExtlib.list_mapi (fun x i -> 
1011             if i > 0 then [B.b_kw " and ";x] else [x])
1012           (List.map (njoint_def2pres term2pres) defs)))
1013 ;;
1014
1015 let content2pres0
1016   ?skip_initial_lambdas ?(skip_thm_and_qed=false)
1017   (term2pres : ?prec:int -> 'a)
1018   (id,params,metasenv,obj) 
1019 =
1020   match obj with
1021   | `Def (Content.Const, thesis, `Proof p) ->
1022       let name = get_name p.Content.proof_name in
1023       let proof = proof2pres true term2pres ?skip_initial_lambdas p in
1024       if skip_thm_and_qed then
1025         proof
1026       else
1027       B.b_v
1028         [Some "helm","xref","id"]
1029         ([ B.b_h [] (B.b_kw ("theorem " ^ name) :: 
1030           params2pres params @ [B.b_kw ":"]);
1031            B.H ([],[B.indent (term2pres thesis) ; B.b_kw "." ])] @
1032          metasenv2pres term2pres metasenv @
1033          [proof ; B.b_kw "qed."])
1034   | `Def (_, ty, `Definition body) ->
1035       let name = get_name body.Content.def_name in
1036       B.b_v
1037         [Some "helm","xref","id"]
1038         ([B.b_h []
1039            (B.b_kw ("definition " ^ name) :: params2pres params @ [B.b_kw ":"]);
1040           B.indent (term2pres ty)] @
1041           metasenv2pres term2pres metasenv @
1042           [B.b_kw ":=";
1043            B.indent (term2pres body.Content.def_term);
1044            B.b_kw "."])
1045   | `Decl (_, `Declaration decl)
1046   | `Decl (_, `Hypothesis decl) ->
1047       let name = get_name decl.Content.dec_name in
1048       B.b_v
1049         [Some "helm","xref","id"]
1050         ([B.b_h [] (B.b_kw ("axiom " ^ name) :: params2pres params);
1051           B.b_kw "Type:";
1052           B.indent (term2pres decl.Content.dec_type)] @
1053           metasenv2pres term2pres metasenv)
1054   | `Joint joint ->
1055       B.b_v []
1056         (recursion_kind2pres params joint.Content.joint_kind
1057         :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
1058   | _ -> raise ToDo
1059
1060 let content2pres 
1061   ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts 
1062 =
1063   content2pres0 ?skip_initial_lambdas ?skip_thm_and_qed
1064 (* FG: prec not optional in my patch *)
1065     (fun ?(prec=90) annterm ->
1066       let ast, ids_to_uris =
1067        TermAcicContent.ast_of_acic ~output_type:`Term ids_to_inner_sorts annterm
1068       in
1069        CicNotationPres.box_of_mpres
1070         (CicNotationPres.render
1071           ~lookup_uri:(CicNotationPres.lookup_uri ids_to_uris) ~prec
1072           (TermContentPres.pp_ast ast)))
1073
1074 let ncontent2pres0
1075   ?skip_initial_lambdas ?(skip_thm_and_qed=false)
1076   (term2pres : ?prec:int -> 'a)
1077   (id,params,metasenv,obj : CicNotationPt.term Content.cobj) 
1078 =
1079   match obj with
1080   | `Def (Content.Const, thesis, `Proof p) ->
1081       let name = get_name p.Content.proof_name in
1082       let proof = proof2pres true term2pres ?skip_initial_lambdas p in
1083       if skip_thm_and_qed then
1084         proof
1085       else
1086       B.b_v
1087         [Some "helm","xref","id"]
1088         ([ B.b_h [] (B.b_kw ("ntheorem " ^ name) :: 
1089           params2pres params @ [B.b_kw ":"]);
1090            B.H ([],[B.indent (term2pres thesis) ; B.b_kw "." ])] @
1091          metasenv2pres term2pres metasenv @
1092          [proof ; B.b_kw "qed."])
1093   | `Def (_, ty, `Definition body) ->
1094       let name = get_name body.Content.def_name in
1095       B.b_v
1096         [Some "helm","xref","id"]
1097         ([B.b_h []
1098            (B.b_kw ("ndefinition " ^ name) :: params2pres params @ [B.b_kw ":"]);
1099           B.indent (term2pres ty)] @
1100           metasenv2pres term2pres metasenv @
1101           [B.b_kw ":=";
1102            B.indent (term2pres body.Content.def_term);
1103            B.b_kw "."])
1104   | `Decl (_, `Declaration decl)
1105   | `Decl (_, `Hypothesis decl) ->
1106       let name = get_name decl.Content.dec_name in
1107       B.b_v
1108         [Some "helm","xref","id"]
1109         ([B.b_h [] (B.b_kw ("naxiom " ^ name) :: params2pres params);
1110           B.b_kw "Type:";
1111           B.indent (term2pres decl.Content.dec_type)] @
1112           metasenv2pres term2pres metasenv)
1113   | `Joint joint ->
1114       B.b_v [] 
1115         [njoint_def2pres term2pres 
1116           joint.Content.joint_kind joint.Content.joint_defs]
1117   | _ -> raise ToDo
1118
1119 let ncontent2pres ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_nrefs =
1120  let lookup_uri id =
1121   try
1122    let nref = Hashtbl.find ids_to_nrefs id in
1123     Some (NReference.string_of_reference nref)
1124   with Not_found -> None
1125  in
1126   ncontent2pres0 ?skip_initial_lambdas ?skip_thm_and_qed
1127     (fun ?(prec=90) ast ->
1128       CicNotationPres.box_of_mpres
1129        (CicNotationPres.render ~lookup_uri ~prec (TermContentPres.pp_ast ast)))