1 (* Copyright (C) 2003-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (***************************************************************************)
30 (* Andrea Asperti <asperti@cs.unibo.it> *)
33 (***************************************************************************)
37 module P = Mpresentation
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)
53 split (n-1) (List.tl l) in
56 let get_xref = function
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
64 RenderingAttrs.spacing_attributes `BoxML
65 @ RenderingAttrs.indent_attributes `BoxML
67 let make_row items concl =
68 B.b_hv hv_attrs (items @ [ concl ])
71 B.b_v attrs [B.b_h [] items; B.b_indent concl]
73 B.b_h attrs (items@[B.b_space; concl]) *)
75 let make_concl ?(attrs=[]) verb concl =
76 B.b_hv (hv_attrs @ attrs) [ B.b_kw verb; concl ]
79 B.b_v attrs [ B.b_kw verb; B.b_indent concl]
81 B.b_h attrs [ B.b_kw verb; B.b_space; concl ] *)
83 let make_args_for_apply term2pres args =
84 let make_arg_for_apply is_first arg row =
87 Con.Aux n -> assert false
90 (match prem.Con.premise_binder with
93 (B.b_object (P.Mi ([], name)))::row
96 Some "helm", "xref", lemma.Con.lemma_id;
97 Some "xlink", "href", lemma.Con.lemma_uri ]
99 (B.b_object (P.Mi(lemma_attrs,lemma.Con.lemma_name)))::row
103 else (B.b_object (P.Mi([],"_")))::row
106 (B.b_object (P.Mi([],"_")))::row
108 if is_first then res else B.skip::res
112 make_arg_for_apply true hd
113 (List.fold_right (make_arg_for_apply false) tl [])
116 let get_name = function
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 *)
124 let rec justification term2pres p =
125 if ((p.Con.proof_conclude.Con.conclude_method = "Exact") or
126 ((p.Con.proof_context = []) &
127 (p.Con.proof_apply_context = []) &
128 (p.Con.proof_conclude.Con.conclude_method = "Apply"))) then
130 make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args in
132 (B.b_kw "by")::B.b_space::
133 B.Text([],"(")::pres_args@[B.Text([],")")])
134 else proof2pres term2pres p
136 and proof2pres term2pres p =
137 let rec proof2pres p =
142 | `Hypothesis _ -> true
144 ((List.filter is_decl p.Con.proof_context) != []) in
145 let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
147 (match p.Con.proof_conclude.Con.conclude_conclusion with
149 | Some t -> Some (term2pres t)) in
152 conclude2pres p.Con.proof_conclude indent omit_conclusion in
154 acontext2pres p.Con.proof_apply_context presconclude indent in
155 context2pres p.Con.proof_context presacontext in
156 match p.Con.proof_name with
164 ([None,"type","toggle"],
165 [(make_concl ~attrs:[Some "helm", "xref", p.Con.proof_id]
166 "proof of" ac); body])
169 [B.Text ([],"(" ^ name ^ ")");
172 and context2pres c continuation =
173 (* we generate a subtable for each context element, for selection
175 The table generated by the head-element does not have an xref;
176 the whole context-proof is already selectable *)
182 (fun ce continuation ->
183 let xref = get_xref ce in
184 B.V([Some "helm", "xref", xref ],
185 [B.H([Some "helm", "xref", "ce_"^xref],
186 [ce2pres_in_proof_context_element ce]);
187 continuation])) tl continuation in
188 let hd_xref= get_xref hd in
190 [B.H([Some "helm", "xref", "ce_"^hd_xref],
191 [ce2pres_in_proof_context_element hd]);
194 and ce2pres_in_joint_context_element = function
195 | `Inductive _ -> assert false (* TODO *)
196 | (`Declaration _) as x -> ce2pres x
197 | (`Hypothesis _) as x -> ce2pres x
198 | (`Proof _) as x -> ce2pres x
199 | (`Definition _) as x -> ce2pres x
201 and ce2pres_in_proof_context_element = function
203 B.H ([],(List.map ce2pres_in_joint_context_element ho.Content.joint_defs))
204 | (`Declaration _) as x -> ce2pres x
205 | (`Hypothesis _) as x -> ce2pres x
206 | (`Proof _) as x -> ce2pres x
207 | (`Definition _) as x -> ce2pres x
212 (match d.Con.dec_name with
214 let ty = term2pres d.Con.dec_type in
218 B.Object ([], P.Mi([],s));
222 prerr_endline "NO NAME!!"; assert false)
224 (match h.Con.dec_name with
226 let ty = term2pres h.Con.dec_type in
231 B.Object ([], P.Mi ([],s));
236 prerr_endline "NO NAME!!"; assert false)
240 (match d.Con.def_name with
242 let term = term2pres d.Con.def_term in
244 [ B.b_kw "Let"; B.b_space;
245 B.Object ([], P.Mi([],s));
249 prerr_endline "NO NAME!!"; assert false)
251 and acontext2pres ac continuation indent =
253 (fun p continuation ->
256 B.indent (proof2pres p)
259 B.V([Some "helm","xref",p.Con.proof_id],
260 [B.H([Some "helm","xref","ace_"^p.Con.proof_id],[hd]);
261 continuation])) ac continuation
263 and conclude2pres conclude indent omit_conclusion =
265 match conclude.Con.conclude_conclusion with
267 not omit_conclusion or
268 (* CSC: I ignore the omit_conclusion flag in this case. *)
269 (* CSC: Is this the correct behaviour? In the stylesheets *)
270 (* CSC: we simply generated nothing (i.e. the output type *)
271 (* CSC: of the function should become an option. *)
272 conclude.Con.conclude_method = "BU_Conversion" ->
273 let concl = (term2pres t) in
274 if conclude.Con.conclude_method = "BU_Conversion" then
275 make_concl "that is equivalent to" concl
276 else if conclude.Con.conclude_method = "FalseInd" then
277 (* false ind is in charge to add the conclusion *)
280 let conclude_body = conclude_aux conclude in
282 if conclude.Con.conclude_method = "TD_Conversion" then
283 make_concl "that is equivalent to" concl
284 else make_concl "we conclude" concl in
285 B.V ([], [conclude_body; ann_concl])
286 | _ -> conclude_aux conclude in
288 B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
291 B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
293 and conclude_aux conclude =
294 if conclude.Con.conclude_method = "TD_Conversion" then
296 (match conclude.Con.conclude_conclusion with
297 None -> B.Text([],"NO EXPECTED!!!")
298 | Some c -> term2pres c) in
300 (match conclude.Con.conclude_args with
301 [Con.ArgProof p] -> p
302 | _ -> assert false) in
304 (match subproof.Con.proof_conclude.Con.conclude_conclusion with
305 None -> B.Text([],"NO SYNTH!!!")
306 | Some c -> (term2pres c)) in
309 [make_concl "we must prove" expected;
310 make_concl "or equivalently" synth;
311 proof2pres subproof])
312 else if conclude.Con.conclude_method = "BU_Conversion" then
314 else if conclude.Con.conclude_method = "Exact" then
316 (match conclude.Con.conclude_args with
317 [Con.Term t] -> term2pres t
319 (match p.Con.premise_binder with
320 | None -> assert false; (* unnamed hypothesis ??? *)
321 | Some s -> B.Text([],s))
322 | err -> assert false) in
323 (match conclude.Con.conclude_conclusion with
325 B.b_h [] [B.b_kw "Consider"; B.b_space; arg]
326 | Some c -> let conclusion = term2pres c in
328 [arg; B.b_space; B.b_kw "proves"]
331 else if conclude.Con.conclude_method = "Intros+LetTac" then
332 (match conclude.Con.conclude_args with
333 [Con.ArgProof p] -> proof2pres p
337 (match conclude.Con.conclude_conclusion with
338 None -> B.Text([],"NO Conclusion!!!")
339 | Some c -> term2pres c) in
340 (match conclude.Con.conclude_args with
343 ([None,"align","baseline 1"; None,"equalrows","false";
344 None,"columnalign","left"],
345 [B.H([],[B.Object([],proof2pres p)]);
347 (make_concl "we proved 1" conclusion))])]);
350 else if (conclude.Con.conclude_method = "Case") then
352 else if (conclude.Con.conclude_method = "ByInduction") then
354 else if (conclude.Con.conclude_method = "Exists") then
356 else if (conclude.Con.conclude_method = "AndInd") then
358 else if (conclude.Con.conclude_method = "FalseInd") then
360 else if (conclude.Con.conclude_method = "Rewrite") then
362 (match (List.nth conclude.Con.conclude_args 6) with
363 Con.ArgProof p -> justification term2pres p
364 | _ -> assert false) in
366 (match List.nth conclude.Con.conclude_args 2 with
367 Con.Term t -> term2pres t
368 | _ -> assert false) in
370 (match List.nth conclude.Con.conclude_args 5 with
371 Con.Term t -> term2pres t
372 | _ -> assert false) in
377 B.b_space; (B.b_kw "with");
380 else if conclude.Con.conclude_method = "Apply" then
382 make_args_for_apply term2pres conclude.Con.conclude_args in
386 B.Text([],"(")::pres_args@[B.Text([],")")])
389 B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
390 (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
392 and args2pres l = List.map arg2pres l
396 Con.Aux n -> B.b_kw ("aux " ^ n)
397 | Con.Premise prem -> B.b_kw "premise"
398 | Con.Lemma lemma -> B.b_kw "lemma"
399 | Con.Term t -> term2pres t
400 | Con.ArgProof p -> proof2pres p
401 | Con.ArgMethod s -> B.b_kw "method"
404 let proof_conclusion =
405 (match conclude.Con.conclude_conclusion with
406 None -> B.b_kw "No conclusion???"
407 | Some t -> term2pres t) in
408 let arg,args_for_cases =
409 (match conclude.Con.conclude_args with
410 Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
412 | _ -> assert false) in
416 Con.Aux n -> B.b_kw "an aux???"
417 | Con.Premise prem ->
418 (match prem.Con.premise_binder with
419 None -> B.b_kw "the previous result"
420 | Some n -> B.Object ([], P.Mi([],n)))
421 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
424 | Con.ArgProof p -> B.b_kw "a proof???"
425 | Con.ArgMethod s -> B.b_kw "a method???")
427 (make_concl "we proceed by cases on" case_arg) in
429 (make_concl "to prove" proof_conclusion) in
430 B.V ([], case_on::to_prove::(make_cases args_for_cases))
432 and byinduction conclude =
433 let proof_conclusion =
434 (match conclude.Con.conclude_conclusion with
435 None -> B.b_kw "No conclusion???"
436 | Some t -> term2pres t) in
437 let inductive_arg,args_for_cases =
438 (match conclude.Con.conclude_args with
440 let l1,l2 = split (int_of_string n) tl in
441 let last_pos = (List.length l2)-1 in
442 List.nth l2 last_pos,l1
443 | _ -> assert false) in
446 (match inductive_arg with
447 Con.Aux n -> B.b_kw "an aux???"
448 | Con.Premise prem ->
449 (match prem.Con.premise_binder with
450 None -> B.b_kw "the previous result"
451 | Some n -> B.Object ([], P.Mi([],n)))
452 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
455 | Con.ArgProof p -> B.b_kw "a proof???"
456 | Con.ArgMethod s -> B.b_kw "a method???") in
457 (make_concl "we proceed by induction on" arg) in
459 (make_concl "to prove" proof_conclusion) in
460 B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
462 and make_cases l = List.map make_case l
468 (match p.Con.proof_name with
469 None -> B.b_kw "no name for case!!"
470 | Some n -> B.Object ([], P.Mi([],n))) in
474 `Hypothesis h -> h.Con.dec_inductive
475 | _ -> false) p.Con.proof_context in
484 (match h.Con.dec_name with
488 B.Object ([], P.Mi ([],name));
490 (term2pres h.Con.dec_type)]
491 | _ -> [B.Text ([],"???")]) in
495 (B.b_kw "Case"::B.b_space::name::pattern_aux)@
497 B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
499 (match p.Con.proof_conclude.Con.conclude_conclusion with
500 None -> B.b_kw "No conclusion!!!"
501 | Some t -> term2pres t) in
502 let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
503 let induction_hypothesis =
507 let text = B.indent (B.b_kw "by induction hypothesis we know") in
512 (match h.Con.dec_name with
517 B.Object ([], P.Mi ([],name));
520 term2pres h.Con.dec_type]))
521 | _ -> assert false in
522 let hyps = List.map make_hyp indhyps in
525 acontext2pres_old p.Con.proof_apply_context true in *)
526 let body = conclude2pres p.Con.proof_conclude true false in
529 match p.Con.proof_apply_context with
530 [] -> p.Con.proof_conclude.Con.conclude_id
531 | {Con.proof_id = id}::_ -> id
533 B.Action([None,"type","toggle"],
534 [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
535 acontext2pres p.Con.proof_apply_context body true]) in
536 B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
539 and falseind conclude =
540 let proof_conclusion =
541 (match conclude.Con.conclude_conclusion with
542 None -> B.b_kw "No conclusion???"
543 | Some t -> term2pres t) in
545 (match conclude.Con.conclude_args with
546 [Con.Aux(n);_;case_arg] -> case_arg
549 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
553 Con.Aux n -> assert false
554 | Con.Premise prem ->
555 (match prem.Con.premise_binder with
556 None -> [B.b_kw "Contradiction, hence"]
558 [ B.Object ([],P.Mi([],n)); B.skip;
559 B.b_kw "is contradictory, hence"])
561 [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
562 B.b_kw "is contradictory, hence" ]
563 | _ -> assert false) in
564 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
565 make_row arg proof_conclusion
567 and andind conclude =
569 (match conclude.Con.conclude_args with
570 [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
573 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
577 Con.Aux n -> assert false
578 | Con.Premise prem ->
579 (match prem.Con.premise_binder with
581 | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
583 [(B.b_kw "by");B.skip;
584 B.Object([], P.Mi([],lemma.Con.lemma_name))]
585 | _ -> assert false) in
586 match proof.Con.proof_context with
587 `Hypothesis hyp1::`Hypothesis hyp2::tl ->
589 (match hyp.Con.dec_name with
595 B.Object ([], P.Mi([],get_name hyp1));
598 term2pres hyp1.Con.dec_type]) in
602 B.Object ([], P.Mi([],get_name hyp2));
605 term2pres hyp2.Con.dec_type]) in
606 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
607 let body = conclude2pres proof.Con.proof_conclude false true in
609 acontext2pres proof.Con.proof_apply_context body false in
612 [B.H ([],arg@[B.skip; B.b_kw "we have"]);
619 and exists conclude =
621 (match conclude.Con.conclude_args with
622 [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
625 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
627 match proof.Con.proof_context with
628 `Declaration decl::`Hypothesis hyp::tl
629 | `Hypothesis decl::`Hypothesis hyp::tl ->
631 (match decl.Con.dec_name with
638 B.Object ([], P.Mi([],get_name decl));
639 B.Text([],":"); term2pres decl.Con.dec_type]) in
642 [(B.b_kw "such that");
645 B.Object ([], P.Mi([],get_name hyp));
648 term2pres hyp.Con.dec_type]) in
649 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
650 let body = conclude2pres proof.Con.proof_conclude false true in
652 acontext2pres proof.Con.proof_apply_context body false in
667 let conjecture2pres term2pres (id, n, context, ty) =
668 (B.b_h [Some "helm", "xref", id]
673 [ B.b_object (p_mi [] "_") ;
674 B.b_object (p_mo [] ":?") ;
675 B.b_object (p_mi [] "_")]
676 | Some (`Declaration d)
677 | Some (`Hypothesis d) ->
678 let { Content.dec_name =
679 dec_name ; Content.dec_type = ty } = d
689 | Some (`Definition d) ->
691 { Content.def_name = def_name ;
692 Content.def_term = bo } = d
695 [ B.b_object (p_mi []
699 B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
702 let proof_name = p.Content.proof_name in
704 [ B.b_object (p_mi []
705 (match proof_name with
708 B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
709 proof2pres term2pres p])
710 (List.rev context)) @
711 [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
712 B.b_object (p_mi [] (string_of_int n)) ;
716 let metasenv2pres term2pres = function
719 (* Conjectures are in their own table to make *)
720 (* diffing the DOM trees easier. *)
722 ((B.b_kw ("Conjectures:" ^
723 (let _ = incr counter; in (string_of_int !counter)))) ::
724 (List.map (conjecture2pres term2pres) metasenv'))]
726 let params2pres params =
728 B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
729 (UriManager.name_of_uri uri)
731 let rec spatiate = function
734 | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
739 let params = spatiate (List.map param2pres p) in
741 B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
743 let recursion_kind2pres params kind =
746 | `Recursive _ -> "Recursive definition"
747 | `CoRecursive -> "CoRecursive definition"
748 | `Inductive _ -> "Inductive definition"
749 | `CoInductive _ -> "CoInductive definition"
751 B.b_h [] (B.b_kw kind :: params2pres params)
753 let inductive2pres term2pres ind =
754 let constructor2pres decl =
756 B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
758 term2pres decl.Content.dec_type
763 B.b_kw (ind.Content.inductive_name ^ " of arity");
765 term2pres ind.Content.inductive_type ]
766 :: List.map constructor2pres ind.Content.inductive_constructors)
768 let joint_def2pres term2pres def =
770 | `Inductive ind -> inductive2pres term2pres ind
771 | _ -> assert false (* ZACK or raise ToDo? *)
773 let content2pres term2pres (id,params,metasenv,obj) =
775 | `Def (Content.Const, thesis, `Proof p) ->
776 let name = get_name p.Content.proof_name in
778 [Some "helm","xref","id"]
779 ([ B.b_h [] (B.b_kw ("Proof " ^ name) :: params2pres params);
781 B.indent (term2pres thesis) ] @
782 metasenv2pres term2pres metasenv @
783 [proof2pres term2pres p])
784 | `Def (_, ty, `Definition body) ->
785 let name = get_name body.Content.def_name in
787 [Some "helm","xref","id"]
788 ([B.b_h [] (B.b_kw ("Definition " ^ name) :: params2pres params);
790 B.indent (term2pres ty)] @
791 metasenv2pres term2pres metasenv @
792 [B.b_kw "Body:"; term2pres body.Content.def_term])
793 | `Decl (_, `Declaration decl)
794 | `Decl (_, `Hypothesis decl) ->
795 let name = get_name decl.Content.dec_name in
797 [Some "helm","xref","id"]
798 ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
800 B.indent (term2pres decl.Content.dec_type)] @
801 metasenv2pres term2pres metasenv)
804 (recursion_kind2pres params joint.Content.joint_kind
805 :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
808 let content2pres ~ids_to_inner_sorts =
811 let ast, ids_to_uris =
812 TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
814 CicNotationPres.box_of_mpres
815 (CicNotationPres.render ids_to_uris
816 (TermContentPres.pp_ast ast)))