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 (***************************************************************************)
35 module P = Mpresentation
39 let p_mtr a b = Mpresentation.Mtr(a,b)
40 let p_mtd a b = Mpresentation.Mtd(a,b)
41 let p_mtable a b = Mpresentation.Mtable(a,b)
42 let p_mtext a b = Mpresentation.Mtext(a,b)
43 let p_mi a b = Mpresentation.Mi(a,b)
44 let p_mo a b = Mpresentation.Mo(a,b)
45 let p_mrow a b = Mpresentation.Mrow(a,b)
46 let p_mphantom a b = Mpresentation.Mphantom(a,b)
51 split (n-1) (List.tl l) in
54 let get_xref = function
56 | `Hypothesis d -> d.Con.dec_id
57 | `Proof p -> p.Con.proof_id
58 | `Definition d -> d.Con.def_id
59 | `Joint jo -> jo.Con.joint_id
62 RenderingAttrs.spacing_attributes `BoxML
63 @ RenderingAttrs.indent_attributes `BoxML
65 let make_row items concl =
66 B.b_hv hv_attrs (items @ [ concl ])
69 B.b_v attrs [B.b_h [] items; B.b_indent concl]
71 B.b_h attrs (items@[B.b_space; concl]) *)
73 let make_concl ?(attrs=[]) verb concl =
74 B.b_hv (hv_attrs @ attrs) [ B.b_kw verb; concl ]
77 B.b_v attrs [ B.b_kw verb; B.b_indent concl]
79 B.b_h attrs [ B.b_kw verb; B.b_space; concl ] *)
81 let make_args_for_apply term2pres args =
82 let make_arg_for_apply is_first arg row =
85 Con.Aux n -> assert false
88 (match prem.Con.premise_binder with
91 (B.b_object (P.Mi ([], name)))::row
94 Some "helm", "xref", lemma.Con.lemma_id;
95 Some "xlink", "href", lemma.Con.lemma_uri ]
97 (B.b_object (P.Mi(lemma_attrs,lemma.Con.lemma_name)))::row
101 else (B.b_object (P.Mi([],"_")))::row
104 (B.b_object (P.Mi([],"_")))::row
106 if is_first then res else B.skip::res
110 make_arg_for_apply true hd
111 (List.fold_right (make_arg_for_apply false) tl [])
114 let get_name = function
118 let add_xref id = function
119 | B.Text (attrs, t) -> B.Text (((Some "helm", "xref", id) :: attrs), t)
120 | _ -> assert false (* TODO, add_xref is meaningful for all boxes *)
122 let rec justification term2pres p =
123 if ((p.Con.proof_conclude.Con.conclude_method = "Exact") or
124 ((p.Con.proof_context = []) &
125 (p.Con.proof_apply_context = []) &
126 (p.Con.proof_conclude.Con.conclude_method = "Apply"))) then
128 make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args in
130 (B.b_kw "by")::B.b_space::
131 B.Text([],"(")::pres_args@[B.Text([],")")])
132 else proof2pres term2pres p
134 and proof2pres term2pres p =
135 let rec proof2pres p =
140 | `Hypothesis _ -> true
142 ((List.filter is_decl p.Con.proof_context) != []) in
143 let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
145 (match p.Con.proof_conclude.Con.conclude_conclusion with
147 | Some t -> Some (term2pres t)) in
150 conclude2pres p.Con.proof_conclude indent omit_conclusion in
152 acontext2pres p.Con.proof_apply_context presconclude indent in
153 context2pres p.Con.proof_context presacontext in
154 match p.Con.proof_name with
162 ([None,"type","toggle"],
163 [(make_concl ~attrs:[Some "helm", "xref", p.Con.proof_id]
164 "proof of" ac); body])
167 [B.Text ([],"(" ^ name ^ ")");
170 and context2pres c continuation =
171 (* we generate a subtable for each context element, for selection
173 The table generated by the head-element does not have an xref;
174 the whole context-proof is already selectable *)
180 (fun ce continuation ->
181 let xref = get_xref ce in
182 B.V([Some "helm", "xref", xref ],
183 [B.H([Some "helm", "xref", "ce_"^xref],
184 [ce2pres_in_proof_context_element ce]);
185 continuation])) tl continuation in
186 let hd_xref= get_xref hd in
188 [B.H([Some "helm", "xref", "ce_"^hd_xref],
189 [ce2pres_in_proof_context_element hd]);
192 and ce2pres_in_joint_context_element = function
193 | `Inductive _ -> assert false (* TODO *)
194 | (`Declaration _) as x -> ce2pres x
195 | (`Hypothesis _) as x -> ce2pres x
196 | (`Proof _) as x -> ce2pres x
197 | (`Definition _) as x -> ce2pres x
199 and ce2pres_in_proof_context_element = function
201 B.H ([],(List.map ce2pres_in_joint_context_element ho.Content.joint_defs))
202 | (`Declaration _) as x -> ce2pres x
203 | (`Hypothesis _) as x -> ce2pres x
204 | (`Proof _) as x -> ce2pres x
205 | (`Definition _) as x -> ce2pres x
210 (match d.Con.dec_name with
212 let ty = term2pres d.Con.dec_type in
216 B.Object ([], P.Mi([],s));
220 prerr_endline "NO NAME!!"; assert false)
222 (match h.Con.dec_name with
224 let ty = term2pres h.Con.dec_type in
229 B.Object ([], P.Mi ([],s));
234 prerr_endline "NO NAME!!"; assert false)
238 (match d.Con.def_name with
240 let term = term2pres d.Con.def_term in
242 [ B.b_kw "Let"; B.b_space;
243 B.Object ([], P.Mi([],s));
247 prerr_endline "NO NAME!!"; assert false)
249 and acontext2pres ac continuation indent =
251 (fun p continuation ->
254 B.indent (proof2pres p)
257 B.V([Some "helm","xref",p.Con.proof_id],
258 [B.H([Some "helm","xref","ace_"^p.Con.proof_id],[hd]);
259 continuation])) ac continuation
261 and conclude2pres conclude indent omit_conclusion =
263 match conclude.Con.conclude_conclusion with
265 not omit_conclusion or
266 (* CSC: I ignore the omit_conclusion flag in this case. *)
267 (* CSC: Is this the correct behaviour? In the stylesheets *)
268 (* CSC: we simply generated nothing (i.e. the output type *)
269 (* CSC: of the function should become an option. *)
270 conclude.Con.conclude_method = "BU_Conversion" ->
271 let concl = (term2pres t) in
272 if conclude.Con.conclude_method = "BU_Conversion" then
273 make_concl "that is equivalent to" concl
274 else if conclude.Con.conclude_method = "FalseInd" then
275 (* false ind is in charge to add the conclusion *)
278 let conclude_body = conclude_aux conclude in
280 if conclude.Con.conclude_method = "TD_Conversion" then
281 make_concl "that is equivalent to" concl
282 else make_concl "we conclude" concl in
283 B.V ([], [conclude_body; ann_concl])
284 | _ -> conclude_aux conclude in
286 B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
289 B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
291 and conclude_aux conclude =
292 if conclude.Con.conclude_method = "TD_Conversion" then
294 (match conclude.Con.conclude_conclusion with
295 None -> B.Text([],"NO EXPECTED!!!")
296 | Some c -> term2pres c) in
298 (match conclude.Con.conclude_args with
299 [Con.ArgProof p] -> p
300 | _ -> assert false) in
302 (match subproof.Con.proof_conclude.Con.conclude_conclusion with
303 None -> B.Text([],"NO SYNTH!!!")
304 | Some c -> (term2pres c)) in
307 [make_concl "we must prove" expected;
308 make_concl "or equivalently" synth;
309 proof2pres subproof])
310 else if conclude.Con.conclude_method = "BU_Conversion" then
312 else if conclude.Con.conclude_method = "Exact" then
314 (match conclude.Con.conclude_args with
315 [Con.Term t] -> term2pres t
317 (match p.Con.premise_binder with
318 | None -> assert false; (* unnamed hypothesis ??? *)
319 | Some s -> B.Text([],s))
320 | err -> assert false) in
321 (match conclude.Con.conclude_conclusion with
323 B.b_h [] [B.b_kw "Consider"; B.b_space; arg]
324 | Some c -> let conclusion = term2pres c in
326 [arg; B.b_space; B.b_kw "proves"]
329 else if conclude.Con.conclude_method = "Intros+LetTac" then
330 (match conclude.Con.conclude_args with
331 [Con.ArgProof p] -> proof2pres p
335 (match conclude.Con.conclude_conclusion with
336 None -> B.Text([],"NO Conclusion!!!")
337 | Some c -> term2pres c) in
338 (match conclude.Con.conclude_args with
341 ([None,"align","baseline 1"; None,"equalrows","false";
342 None,"columnalign","left"],
343 [B.H([],[B.Object([],proof2pres p)]);
345 (make_concl "we proved 1" conclusion))])]);
348 else if (conclude.Con.conclude_method = "Case") then
350 else if (conclude.Con.conclude_method = "ByInduction") then
352 else if (conclude.Con.conclude_method = "Exists") then
354 else if (conclude.Con.conclude_method = "AndInd") then
356 else if (conclude.Con.conclude_method = "FalseInd") then
358 else if (conclude.Con.conclude_method = "Rewrite") then
360 (match (List.nth conclude.Con.conclude_args 6) with
361 Con.ArgProof p -> justification term2pres p
362 | _ -> assert false) in
364 (match List.nth conclude.Con.conclude_args 2 with
365 Con.Term t -> term2pres t
366 | _ -> assert false) in
368 (match List.nth conclude.Con.conclude_args 5 with
369 Con.Term t -> term2pres t
370 | _ -> assert false) in
375 B.b_space; (B.b_kw "with");
378 else if conclude.Con.conclude_method = "Apply" then
380 make_args_for_apply term2pres conclude.Con.conclude_args in
384 B.Text([],"(")::pres_args@[B.Text([],")")])
387 B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
388 (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
390 and args2pres l = List.map arg2pres l
394 Con.Aux n -> B.b_kw ("aux " ^ n)
395 | Con.Premise prem -> B.b_kw "premise"
396 | Con.Lemma lemma -> B.b_kw "lemma"
397 | Con.Term t -> term2pres t
398 | Con.ArgProof p -> proof2pres p
399 | Con.ArgMethod s -> B.b_kw "method"
402 let proof_conclusion =
403 (match conclude.Con.conclude_conclusion with
404 None -> B.b_kw "No conclusion???"
405 | Some t -> term2pres t) in
406 let arg,args_for_cases =
407 (match conclude.Con.conclude_args with
408 Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
410 | _ -> assert false) in
414 Con.Aux n -> B.b_kw "an aux???"
415 | Con.Premise prem ->
416 (match prem.Con.premise_binder with
417 None -> B.b_kw "the previous result"
418 | Some n -> B.Object ([], P.Mi([],n)))
419 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
422 | Con.ArgProof p -> B.b_kw "a proof???"
423 | Con.ArgMethod s -> B.b_kw "a method???")
425 (make_concl "we proceed by cases on" case_arg) in
427 (make_concl "to prove" proof_conclusion) in
428 B.V ([], case_on::to_prove::(make_cases args_for_cases))
430 and byinduction conclude =
431 let proof_conclusion =
432 (match conclude.Con.conclude_conclusion with
433 None -> B.b_kw "No conclusion???"
434 | Some t -> term2pres t) in
435 let inductive_arg,args_for_cases =
436 (match conclude.Con.conclude_args with
438 let l1,l2 = split (int_of_string n) tl in
439 let last_pos = (List.length l2)-1 in
440 List.nth l2 last_pos,l1
441 | _ -> assert false) in
444 (match inductive_arg with
445 Con.Aux n -> B.b_kw "an aux???"
446 | Con.Premise prem ->
447 (match prem.Con.premise_binder with
448 None -> B.b_kw "the previous result"
449 | Some n -> B.Object ([], P.Mi([],n)))
450 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
453 | Con.ArgProof p -> B.b_kw "a proof???"
454 | Con.ArgMethod s -> B.b_kw "a method???") in
455 (make_concl "we proceed by induction on" arg) in
457 (make_concl "to prove" proof_conclusion) in
458 B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
460 and make_cases l = List.map make_case l
466 (match p.Con.proof_name with
467 None -> B.b_kw "no name for case!!"
468 | Some n -> B.Object ([], P.Mi([],n))) in
472 `Hypothesis h -> h.Con.dec_inductive
473 | _ -> false) p.Con.proof_context in
482 (match h.Con.dec_name with
486 B.Object ([], P.Mi ([],name));
488 (term2pres h.Con.dec_type)]
489 | _ -> [B.Text ([],"???")]) in
493 (B.b_kw "Case"::B.b_space::name::pattern_aux)@
495 B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
497 (match p.Con.proof_conclude.Con.conclude_conclusion with
498 None -> B.b_kw "No conclusion!!!"
499 | Some t -> term2pres t) in
500 let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
501 let induction_hypothesis =
505 let text = B.indent (B.b_kw "by induction hypothesis we know") in
510 (match h.Con.dec_name with
515 B.Object ([], P.Mi ([],name));
518 term2pres h.Con.dec_type]))
519 | _ -> assert false in
520 let hyps = List.map make_hyp indhyps in
523 acontext2pres_old p.Con.proof_apply_context true in *)
524 let body = conclude2pres p.Con.proof_conclude true false in
527 match p.Con.proof_apply_context with
528 [] -> p.Con.proof_conclude.Con.conclude_id
529 | {Con.proof_id = id}::_ -> id
531 B.Action([None,"type","toggle"],
532 [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
533 acontext2pres p.Con.proof_apply_context body true]) in
534 B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
537 and falseind conclude =
538 let proof_conclusion =
539 (match conclude.Con.conclude_conclusion with
540 None -> B.b_kw "No conclusion???"
541 | Some t -> term2pres t) in
543 (match conclude.Con.conclude_args with
544 [Con.Aux(n);_;case_arg] -> case_arg
547 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
551 Con.Aux n -> assert false
552 | Con.Premise prem ->
553 (match prem.Con.premise_binder with
554 None -> [B.b_kw "Contradiction, hence"]
556 [ B.Object ([],P.Mi([],n)); B.skip;
557 B.b_kw "is contradictory, hence"])
559 [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
560 B.b_kw "is contradictory, hence" ]
561 | _ -> assert false) in
562 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
563 make_row arg proof_conclusion
565 and andind conclude =
566 let proof_conclusion =
567 (match conclude.Con.conclude_conclusion with
568 None -> B.b_kw "No conclusion???"
569 | Some t -> term2pres t) in
571 (match conclude.Con.conclude_args with
572 [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
575 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
579 Con.Aux n -> assert false
580 | Con.Premise prem ->
581 (match prem.Con.premise_binder with
583 | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
585 [(B.b_kw "by");B.skip;
586 B.Object([], P.Mi([],lemma.Con.lemma_name))]
587 | _ -> assert false) in
588 match proof.Con.proof_context with
589 `Hypothesis hyp1::`Hypothesis hyp2::tl ->
591 (match hyp.Con.dec_name with
597 B.Object ([], P.Mi([],get_name hyp1));
600 term2pres hyp1.Con.dec_type]) in
604 B.Object ([], P.Mi([],get_name hyp2));
607 term2pres hyp2.Con.dec_type]) in
608 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
609 let body = conclude2pres proof.Con.proof_conclude false true in
611 acontext2pres proof.Con.proof_apply_context body false in
614 [B.H ([],arg@[B.skip; B.b_kw "we have"]);
621 and exists conclude =
622 let proof_conclusion =
623 (match conclude.Con.conclude_conclusion with
624 None -> B.b_kw "No conclusion???"
625 | Some t -> term2pres t) in
627 (match conclude.Con.conclude_args with
628 [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
631 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
633 match proof.Con.proof_context with
634 `Declaration decl::`Hypothesis hyp::tl
635 | `Hypothesis decl::`Hypothesis hyp::tl ->
637 (match decl.Con.dec_name with
644 B.Object ([], P.Mi([],get_name decl));
645 B.Text([],":"); term2pres decl.Con.dec_type]) in
648 [(B.b_kw "such that");
651 B.Object ([], P.Mi([],get_name hyp));
654 term2pres hyp.Con.dec_type]) in
655 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
656 let body = conclude2pres proof.Con.proof_conclude false true in
658 acontext2pres proof.Con.proof_apply_context body false in
673 let conjecture2pres term2pres (id, n, context, ty) =
674 (B.b_h [Some "helm", "xref", id]
679 [ B.b_object (p_mi [] "_") ;
680 B.b_object (p_mo [] ":?") ;
681 B.b_object (p_mi [] "_")]
682 | Some (`Declaration d)
683 | Some (`Hypothesis d) ->
684 let { Content.dec_name =
685 dec_name ; Content.dec_type = ty } = d
695 | Some (`Definition d) ->
697 { Content.def_name = def_name ;
698 Content.def_term = bo } = d
701 [ B.b_object (p_mi []
705 B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
708 let proof_name = p.Content.proof_name in
710 [ B.b_object (p_mi []
711 (match proof_name with
714 B.b_text [] (Utf8Macro.unicode_of_tex "\\Assign");
715 proof2pres term2pres p])
716 (List.rev context)) @
717 [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
718 B.b_object (p_mi [] (string_of_int n)) ;
722 let metasenv2pres term2pres = function
725 (* Conjectures are in their own table to make *)
726 (* diffing the DOM trees easier. *)
728 ((B.b_kw ("Conjectures:" ^
729 (let _ = incr counter; in (string_of_int !counter)))) ::
730 (List.map (conjecture2pres term2pres) metasenv'))]
732 let params2pres params =
734 B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
735 (UriManager.name_of_uri uri)
737 let rec spatiate = function
740 | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
745 let params = spatiate (List.map param2pres p) in
747 B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
749 let recursion_kind2pres params kind =
752 | `Recursive _ -> "Recursive definition"
753 | `CoRecursive -> "CoRecursive definition"
754 | `Inductive _ -> "Inductive definition"
755 | `CoInductive _ -> "CoInductive definition"
757 B.b_h [] (B.b_kw kind :: params2pres params)
759 let inductive2pres term2pres ind =
760 let constructor2pres decl =
762 B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
764 term2pres decl.Content.dec_type
769 B.b_kw (ind.Content.inductive_name ^ " of arity");
771 term2pres ind.Content.inductive_type ]
772 :: List.map constructor2pres ind.Content.inductive_constructors)
774 let joint_def2pres term2pres def =
776 | `Inductive ind -> inductive2pres term2pres ind
777 | _ -> assert false (* ZACK or raise ToDo? *)
779 let content2pres term2pres (id,params,metasenv,obj) =
781 | `Def (Content.Const, thesis, `Proof p) ->
782 let name = get_name p.Content.proof_name in
784 [Some "helm","xref","id"]
785 ([ B.b_h [] (B.b_kw ("Proof " ^ name) :: params2pres params);
787 B.indent (term2pres thesis) ] @
788 metasenv2pres term2pres metasenv @
789 [proof2pres term2pres p])
790 | `Def (_, ty, `Definition body) ->
791 let name = get_name body.Content.def_name in
793 [Some "helm","xref","id"]
794 ([B.b_h [] (B.b_kw ("Definition " ^ name) :: params2pres params);
796 B.indent (term2pres ty)] @
797 metasenv2pres term2pres metasenv @
798 [B.b_kw "Body:"; term2pres body.Content.def_term])
799 | `Decl (_, `Declaration decl)
800 | `Decl (_, `Hypothesis decl) ->
801 let name = get_name decl.Content.dec_name in
803 [Some "helm","xref","id"]
804 ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
806 B.indent (term2pres decl.Content.dec_type)] @
807 metasenv2pres term2pres metasenv)
810 (recursion_kind2pres params joint.Content.joint_kind
811 :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
814 let content2pres ~ids_to_inner_sorts =
817 let ast, ids_to_uris =
818 CicNotationRew.ast_of_acic ids_to_inner_sorts annterm
820 CicNotationPres.box_of_mpres
821 (CicNotationPres.render ids_to_uris
822 (CicNotationRew.pp_ast ast)))