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 [])
115 let get_name = function
119 let add_xref id = function
120 | B.Text (attrs, t) -> B.Text (((Some "helm", "xref", id) :: attrs), t)
121 | _ -> assert false (* TODO, add_xref is meaningful for all boxes *)
123 let rec justification term2pres p =
124 if ((p.Con.proof_conclude.Con.conclude_method = "Exact") or
125 ((p.Con.proof_context = []) &
126 (p.Con.proof_apply_context = []) &
127 (p.Con.proof_conclude.Con.conclude_method = "Apply"))) then
129 make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args in
131 (B.b_kw "by")::B.b_space::
132 B.Text([],"(")::pres_args@[B.Text([],")")]), None
134 Some (B.b_toggle [B.b_kw "proof";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 make_concl ~attrs:[ Some "helm", "xref", p.Con.proof_id ]
166 B.b_toggle [ concl; 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
361 let justif1,justif2 =
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");
379 B.b_space; justif1])::
380 match justif2 with None -> [] | Some j -> [B.indent j])
381 else if conclude.Con.conclude_method = "Eq_chain" then
382 let justification p =
383 let j1,j2 = justification term2pres p in
384 j1 :: B.b_space :: (match j2 with Some j -> [j] | None -> [])
389 | (Con.ArgProof p)::(Con.Term t)::tl ->
390 B.H([],([B.b_kw "=";B.b_space;term2pres t;B.b_space]@justification p))::(aux tl)
394 match List.hd conclude.Con.conclude_args with
398 B.H([],[term2pres hd; B.b_space;
399 B.V ([],aux (List.tl conclude.Con.conclude_args))])
400 else if conclude.Con.conclude_method = "Apply" then
402 make_args_for_apply term2pres conclude.Con.conclude_args in
406 B.Text([],"(")::pres_args@[B.Text([],")")])
409 B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
410 (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
412 and args2pres l = List.map arg2pres l
416 Con.Aux n -> B.b_kw ("aux " ^ n)
417 | Con.Premise prem -> B.b_kw "premise"
418 | Con.Lemma lemma -> B.b_kw "lemma"
419 | Con.Term t -> term2pres t
420 | Con.ArgProof p -> proof2pres p
421 | Con.ArgMethod s -> B.b_kw "method"
424 let proof_conclusion =
425 (match conclude.Con.conclude_conclusion with
426 None -> B.b_kw "No conclusion???"
427 | Some t -> term2pres t) in
428 let arg,args_for_cases =
429 (match conclude.Con.conclude_args with
430 Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
432 | _ -> assert false) in
436 Con.Aux n -> B.b_kw "an aux???"
437 | Con.Premise prem ->
438 (match prem.Con.premise_binder with
439 None -> B.b_kw "the previous result"
440 | Some n -> B.Object ([], P.Mi([],n)))
441 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
444 | Con.ArgProof p -> B.b_kw "a proof???"
445 | Con.ArgMethod s -> B.b_kw "a method???")
447 (make_concl "we proceed by cases on" case_arg) in
449 (make_concl "to prove" proof_conclusion) in
450 B.V ([], case_on::to_prove::(make_cases args_for_cases))
452 and byinduction conclude =
453 let proof_conclusion =
454 (match conclude.Con.conclude_conclusion with
455 None -> B.b_kw "No conclusion???"
456 | Some t -> term2pres t) in
457 let inductive_arg,args_for_cases =
458 (match conclude.Con.conclude_args with
460 let l1,l2 = split (int_of_string n) tl in
461 let last_pos = (List.length l2)-1 in
462 List.nth l2 last_pos,l1
463 | _ -> assert false) in
466 (match inductive_arg with
467 Con.Aux n -> B.b_kw "an aux???"
468 | Con.Premise prem ->
469 (match prem.Con.premise_binder with
470 None -> B.b_kw "the previous result"
471 | Some n -> B.Object ([], P.Mi([],n)))
472 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
475 | Con.ArgProof p -> B.b_kw "a proof???"
476 | Con.ArgMethod s -> B.b_kw "a method???") in
477 (make_concl "we proceed by induction on" arg) in
479 (make_concl "to prove" proof_conclusion) in
480 B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
482 and make_cases l = List.map make_case l
488 (match p.Con.proof_name with
489 None -> B.b_kw "no name for case!!"
490 | Some n -> B.Object ([], P.Mi([],n))) in
494 `Hypothesis h -> h.Con.dec_inductive
495 | _ -> false) p.Con.proof_context in
504 (match h.Con.dec_name with
508 B.Object ([], P.Mi ([],name));
510 (term2pres h.Con.dec_type)]
511 | _ -> [B.Text ([],"???")]) in
515 (B.b_kw "Case"::B.b_space::name::pattern_aux)@
517 B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
519 (match p.Con.proof_conclude.Con.conclude_conclusion with
520 None -> B.b_kw "No conclusion!!!"
521 | Some t -> term2pres t) in
522 let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
523 let induction_hypothesis =
527 let text = B.indent (B.b_kw "by induction hypothesis we know") in
532 (match h.Con.dec_name with
537 B.Object ([], P.Mi ([],name));
540 term2pres h.Con.dec_type]))
541 | _ -> assert false in
542 let hyps = List.map make_hyp indhyps in
545 acontext2pres_old p.Con.proof_apply_context true in *)
546 let body = conclude2pres p.Con.proof_conclude true false in
549 match p.Con.proof_apply_context with
550 [] -> p.Con.proof_conclude.Con.conclude_id
551 | {Con.proof_id = id}::_ -> id
553 B.Action([None,"type","toggle"],
554 [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
555 acontext2pres p.Con.proof_apply_context body true]) in
556 B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
559 and falseind conclude =
560 let proof_conclusion =
561 (match conclude.Con.conclude_conclusion with
562 None -> B.b_kw "No conclusion???"
563 | Some t -> term2pres t) in
565 (match conclude.Con.conclude_args with
566 [Con.Aux(n);_;case_arg] -> case_arg
569 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
573 Con.Aux n -> assert false
574 | Con.Premise prem ->
575 (match prem.Con.premise_binder with
576 None -> [B.b_kw "Contradiction, hence"]
578 [ B.Object ([],P.Mi([],n)); B.skip;
579 B.b_kw "is contradictory, hence"])
581 [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
582 B.b_kw "is contradictory, hence" ]
583 | _ -> assert false) in
584 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
585 make_row arg proof_conclusion
587 and andind conclude =
589 (match conclude.Con.conclude_args with
590 [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
593 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
597 Con.Aux n -> assert false
598 | Con.Premise prem ->
599 (match prem.Con.premise_binder with
601 | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
603 [(B.b_kw "by");B.skip;
604 B.Object([], P.Mi([],lemma.Con.lemma_name))]
605 | _ -> assert false) in
606 match proof.Con.proof_context with
607 `Hypothesis hyp1::`Hypothesis hyp2::tl ->
609 (match hyp.Con.dec_name with
615 B.Object ([], P.Mi([],get_name hyp1));
618 term2pres hyp1.Con.dec_type]) in
622 B.Object ([], P.Mi([],get_name hyp2));
625 term2pres hyp2.Con.dec_type]) in
626 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
627 let body = conclude2pres proof.Con.proof_conclude false true in
629 acontext2pres proof.Con.proof_apply_context body false in
632 [B.H ([],arg@[B.skip; B.b_kw "we have"]);
639 and exists conclude =
641 (match conclude.Con.conclude_args with
642 [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
645 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
647 match proof.Con.proof_context with
648 `Declaration decl::`Hypothesis hyp::tl
649 | `Hypothesis decl::`Hypothesis hyp::tl ->
651 (match decl.Con.dec_name with
658 B.Object ([], P.Mi([],get_name decl));
659 B.Text([],":"); term2pres decl.Con.dec_type]) in
662 [(B.b_kw "such that");
665 B.Object ([], P.Mi([],get_name hyp));
668 term2pres hyp.Con.dec_type]) in
669 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
670 let body = conclude2pres proof.Con.proof_conclude false true in
672 acontext2pres proof.Con.proof_apply_context body false in
687 let conjecture2pres term2pres (id, n, context, ty) =
689 (B.b_hv [Some "helm", "xref", id]
691 B.b_h [] [B.b_text [] "{...}"; B.b_space];
696 [ B.b_object (p_mi [] "_") ;
697 B.b_object (p_mo [] ":?") ;
698 B.b_object (p_mi [] "_")]
699 | Some (`Declaration d)
700 | Some (`Hypothesis d) ->
701 let { Content.dec_name =
702 dec_name ; Content.dec_type = ty } = d
712 | Some (`Definition d) ->
714 { Content.def_name = def_name ;
715 Content.def_term = bo } = d
718 [ B.b_object (p_mi []
722 B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
725 let proof_name = p.Content.proof_name in
727 [ B.b_object (p_mi []
728 (match proof_name with
731 B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
732 proof2pres term2pres p])
733 (List.rev context)) ] ::
735 [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
736 B.b_object (p_mi [] (string_of_int n)) ;
740 let metasenv2pres term2pres = function
743 (* Conjectures are in their own table to make *)
744 (* diffing the DOM trees easier. *)
746 ((B.b_kw ("Conjectures:" ^
747 (let _ = incr counter; in (string_of_int !counter)))) ::
748 (List.map (conjecture2pres term2pres) metasenv'))]
750 let params2pres params =
752 B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
753 (UriManager.name_of_uri uri)
755 let rec spatiate = function
758 | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
763 let params = spatiate (List.map param2pres p) in
765 B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
767 let recursion_kind2pres params kind =
770 | `Recursive _ -> "Recursive definition"
771 | `CoRecursive -> "CoRecursive definition"
772 | `Inductive _ -> "Inductive definition"
773 | `CoInductive _ -> "CoInductive definition"
775 B.b_h [] (B.b_kw kind :: params2pres params)
777 let inductive2pres term2pres ind =
778 let constructor2pres decl =
780 B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
782 term2pres decl.Content.dec_type
787 B.b_kw (ind.Content.inductive_name ^ " of arity");
789 term2pres ind.Content.inductive_type ]
790 :: List.map constructor2pres ind.Content.inductive_constructors)
792 let joint_def2pres term2pres def =
794 | `Inductive ind -> inductive2pres term2pres ind
795 | _ -> assert false (* ZACK or raise ToDo? *)
797 let content2pres term2pres (id,params,metasenv,obj) =
799 | `Def (Content.Const, thesis, `Proof p) ->
800 let name = get_name p.Content.proof_name in
802 [Some "helm","xref","id"]
803 ([ B.b_h [] (B.b_kw ("Proof " ^ name) :: params2pres params);
805 B.indent (term2pres thesis) ] @
806 metasenv2pres term2pres metasenv @
807 [proof2pres term2pres p])
808 | `Def (_, ty, `Definition body) ->
809 let name = get_name body.Content.def_name in
811 [Some "helm","xref","id"]
812 ([B.b_h [] (B.b_kw ("Definition " ^ name) :: params2pres params);
814 B.indent (term2pres ty)] @
815 metasenv2pres term2pres metasenv @
816 [B.b_kw "Body:"; term2pres body.Content.def_term])
817 | `Decl (_, `Declaration decl)
818 | `Decl (_, `Hypothesis decl) ->
819 let name = get_name decl.Content.dec_name in
821 [Some "helm","xref","id"]
822 ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
824 B.indent (term2pres decl.Content.dec_type)] @
825 metasenv2pres term2pres metasenv)
828 (recursion_kind2pres params joint.Content.joint_kind
829 :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
832 let content2pres ~ids_to_inner_sorts =
835 let ast, ids_to_uris =
836 TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
838 CicNotationPres.box_of_mpres
839 (CicNotationPres.render ids_to_uris
840 (TermContentPres.pp_ast ast)))