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