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([],")")]), None
136 Some (B.b_toggle [B.b_kw "proof";proof2pres term2pres p])
138 and proof2pres term2pres p =
139 let rec proof2pres p =
144 | `Hypothesis _ -> true
146 ((List.filter is_decl p.Con.proof_context) != []) in
147 let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
149 (match p.Con.proof_conclude.Con.conclude_conclusion with
151 | Some t -> Some (term2pres t)) in
154 conclude2pres p.Con.proof_conclude indent omit_conclusion in
156 acontext2pres p.Con.proof_apply_context presconclude indent in
157 context2pres p.Con.proof_context presacontext in
158 match p.Con.proof_name with
166 make_concl ~attrs:[ Some "helm", "xref", p.Con.proof_id ]
168 B.b_toggle [ concl; body ]
171 [B.Text ([],"(" ^ name ^ ")");
174 and context2pres c continuation =
175 (* we generate a subtable for each context element, for selection
177 The table generated by the head-element does not have an xref;
178 the whole context-proof is already selectable *)
184 (fun ce continuation ->
185 let xref = get_xref ce in
186 B.V([Some "helm", "xref", xref ],
187 [B.H([Some "helm", "xref", "ce_"^xref],
188 [ce2pres_in_proof_context_element ce]);
189 continuation])) tl continuation in
190 let hd_xref= get_xref hd in
192 [B.H([Some "helm", "xref", "ce_"^hd_xref],
193 [ce2pres_in_proof_context_element hd]);
196 and ce2pres_in_joint_context_element = function
197 | `Inductive _ -> assert false (* TODO *)
198 | (`Declaration _) as x -> ce2pres x
199 | (`Hypothesis _) as x -> ce2pres x
200 | (`Proof _) as x -> ce2pres x
201 | (`Definition _) as x -> ce2pres x
203 and ce2pres_in_proof_context_element = function
205 B.H ([],(List.map ce2pres_in_joint_context_element ho.Content.joint_defs))
206 | (`Declaration _) as x -> ce2pres x
207 | (`Hypothesis _) as x -> ce2pres x
208 | (`Proof _) as x -> ce2pres x
209 | (`Definition _) as x -> ce2pres x
214 (match d.Con.dec_name with
216 let ty = term2pres d.Con.dec_type in
220 B.Object ([], P.Mi([],s));
224 prerr_endline "NO NAME!!"; assert false)
226 (match h.Con.dec_name with
228 let ty = term2pres h.Con.dec_type in
233 B.Object ([], P.Mi ([],s));
238 prerr_endline "NO NAME!!"; assert false)
242 (match d.Con.def_name with
244 let term = term2pres d.Con.def_term in
246 [ B.b_kw "Let"; B.b_space;
247 B.Object ([], P.Mi([],s));
251 prerr_endline "NO NAME!!"; assert false)
253 and acontext2pres ac continuation indent =
255 (fun p continuation ->
258 B.indent (proof2pres p)
261 B.V([Some "helm","xref",p.Con.proof_id],
262 [B.H([Some "helm","xref","ace_"^p.Con.proof_id],[hd]);
263 continuation])) ac continuation
265 and conclude2pres conclude indent omit_conclusion =
267 match conclude.Con.conclude_conclusion with
269 not omit_conclusion or
270 (* CSC: I ignore the omit_conclusion flag in this case. *)
271 (* CSC: Is this the correct behaviour? In the stylesheets *)
272 (* CSC: we simply generated nothing (i.e. the output type *)
273 (* CSC: of the function should become an option. *)
274 conclude.Con.conclude_method = "BU_Conversion" ->
275 let concl = (term2pres t) in
276 if conclude.Con.conclude_method = "BU_Conversion" then
277 make_concl "that is equivalent to" concl
278 else if conclude.Con.conclude_method = "FalseInd" then
279 (* false ind is in charge to add the conclusion *)
282 let conclude_body = conclude_aux conclude in
284 if conclude.Con.conclude_method = "TD_Conversion" then
285 make_concl "that is equivalent to" concl
286 else make_concl "we conclude" concl in
287 B.V ([], [conclude_body; ann_concl])
288 | _ -> conclude_aux conclude in
290 B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
293 B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
295 and conclude_aux conclude =
296 if conclude.Con.conclude_method = "TD_Conversion" then
298 (match conclude.Con.conclude_conclusion with
299 None -> B.Text([],"NO EXPECTED!!!")
300 | Some c -> term2pres c) in
302 (match conclude.Con.conclude_args with
303 [Con.ArgProof p] -> p
304 | _ -> assert false) in
306 (match subproof.Con.proof_conclude.Con.conclude_conclusion with
307 None -> B.Text([],"NO SYNTH!!!")
308 | Some c -> (term2pres c)) in
311 [make_concl "we must prove" expected;
312 make_concl "or equivalently" synth;
313 proof2pres subproof])
314 else if conclude.Con.conclude_method = "BU_Conversion" then
316 else if conclude.Con.conclude_method = "Exact" then
318 (match conclude.Con.conclude_args with
319 [Con.Term t] -> term2pres t
321 (match p.Con.premise_binder with
322 | None -> assert false; (* unnamed hypothesis ??? *)
323 | Some s -> B.Text([],s))
324 | err -> assert false) in
325 (match conclude.Con.conclude_conclusion with
327 B.b_h [] [B.b_kw "Consider"; B.b_space; arg]
328 | Some c -> let conclusion = term2pres c in
330 [arg; B.b_space; B.b_kw "proves"]
333 else if conclude.Con.conclude_method = "Intros+LetTac" then
334 (match conclude.Con.conclude_args with
335 [Con.ArgProof p] -> proof2pres p
339 (match conclude.Con.conclude_conclusion with
340 None -> B.Text([],"NO Conclusion!!!")
341 | Some c -> term2pres c) in
342 (match conclude.Con.conclude_args with
345 ([None,"align","baseline 1"; None,"equalrows","false";
346 None,"columnalign","left"],
347 [B.H([],[B.Object([],proof2pres p)]);
349 (make_concl "we proved 1" conclusion))])]);
352 else if (conclude.Con.conclude_method = "Case") then
354 else if (conclude.Con.conclude_method = "ByInduction") then
356 else if (conclude.Con.conclude_method = "Exists") then
358 else if (conclude.Con.conclude_method = "AndInd") then
360 else if (conclude.Con.conclude_method = "FalseInd") then
362 else if (conclude.Con.conclude_method = "Rewrite") then
363 let justif1, justif2 =
364 (match (List.nth conclude.Con.conclude_args 6) with
365 Con.ArgProof p -> justification term2pres p
366 | _ -> assert false) in
368 (match List.nth conclude.Con.conclude_args 2 with
369 Con.Term t -> term2pres t
370 | _ -> assert false) in
372 (match List.nth conclude.Con.conclude_args 5 with
373 Con.Term t -> term2pres t
374 | _ -> assert false) in
379 B.b_space; (B.b_kw "with");
381 B.b_space; justif1]) ::
382 match justif2 with None -> [] | Some j -> [B.indent j])
383 else if conclude.Con.conclude_method = "Apply" then
385 make_args_for_apply term2pres conclude.Con.conclude_args in
389 B.Text([],"(")::pres_args@[B.Text([],")")])
392 B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
393 (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
395 and args2pres l = List.map arg2pres l
399 Con.Aux n -> B.b_kw ("aux " ^ n)
400 | Con.Premise prem -> B.b_kw "premise"
401 | Con.Lemma lemma -> B.b_kw "lemma"
402 | Con.Term t -> term2pres t
403 | Con.ArgProof p -> proof2pres p
404 | Con.ArgMethod s -> B.b_kw "method"
407 let proof_conclusion =
408 (match conclude.Con.conclude_conclusion with
409 None -> B.b_kw "No conclusion???"
410 | Some t -> term2pres t) in
411 let arg,args_for_cases =
412 (match conclude.Con.conclude_args with
413 Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
415 | _ -> assert false) in
419 Con.Aux n -> B.b_kw "an aux???"
420 | Con.Premise prem ->
421 (match prem.Con.premise_binder with
422 None -> B.b_kw "the previous result"
423 | Some n -> B.Object ([], P.Mi([],n)))
424 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
427 | Con.ArgProof p -> B.b_kw "a proof???"
428 | Con.ArgMethod s -> B.b_kw "a method???")
430 (make_concl "we proceed by cases on" case_arg) in
432 (make_concl "to prove" proof_conclusion) in
433 B.V ([], case_on::to_prove::(make_cases args_for_cases))
435 and byinduction conclude =
436 let proof_conclusion =
437 (match conclude.Con.conclude_conclusion with
438 None -> B.b_kw "No conclusion???"
439 | Some t -> term2pres t) in
440 let inductive_arg,args_for_cases =
441 (match conclude.Con.conclude_args with
443 let l1,l2 = split (int_of_string n) tl in
444 let last_pos = (List.length l2)-1 in
445 List.nth l2 last_pos,l1
446 | _ -> assert false) in
449 (match inductive_arg with
450 Con.Aux n -> B.b_kw "an aux???"
451 | Con.Premise prem ->
452 (match prem.Con.premise_binder with
453 None -> B.b_kw "the previous result"
454 | Some n -> B.Object ([], P.Mi([],n)))
455 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
458 | Con.ArgProof p -> B.b_kw "a proof???"
459 | Con.ArgMethod s -> B.b_kw "a method???") in
460 (make_concl "we proceed by induction on" arg) in
462 (make_concl "to prove" proof_conclusion) in
463 B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
465 and make_cases l = List.map make_case l
471 (match p.Con.proof_name with
472 None -> B.b_kw "no name for case!!"
473 | Some n -> B.Object ([], P.Mi([],n))) in
477 `Hypothesis h -> h.Con.dec_inductive
478 | _ -> false) p.Con.proof_context in
487 (match h.Con.dec_name with
491 B.Object ([], P.Mi ([],name));
493 (term2pres h.Con.dec_type)]
494 | _ -> [B.Text ([],"???")]) in
498 (B.b_kw "Case"::B.b_space::name::pattern_aux)@
500 B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
502 (match p.Con.proof_conclude.Con.conclude_conclusion with
503 None -> B.b_kw "No conclusion!!!"
504 | Some t -> term2pres t) in
505 let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
506 let induction_hypothesis =
510 let text = B.indent (B.b_kw "by induction hypothesis we know") in
515 (match h.Con.dec_name with
520 B.Object ([], P.Mi ([],name));
523 term2pres h.Con.dec_type]))
524 | _ -> assert false in
525 let hyps = List.map make_hyp indhyps in
528 acontext2pres_old p.Con.proof_apply_context true in *)
529 let body = conclude2pres p.Con.proof_conclude true false in
532 match p.Con.proof_apply_context with
533 [] -> p.Con.proof_conclude.Con.conclude_id
534 | {Con.proof_id = id}::_ -> id
536 B.Action([None,"type","toggle"],
537 [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
538 acontext2pres p.Con.proof_apply_context body true]) in
539 B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
542 and falseind conclude =
543 let proof_conclusion =
544 (match conclude.Con.conclude_conclusion with
545 None -> B.b_kw "No conclusion???"
546 | Some t -> term2pres t) in
548 (match conclude.Con.conclude_args with
549 [Con.Aux(n);_;case_arg] -> case_arg
552 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
556 Con.Aux n -> assert false
557 | Con.Premise prem ->
558 (match prem.Con.premise_binder with
559 None -> [B.b_kw "Contradiction, hence"]
561 [ B.Object ([],P.Mi([],n)); B.skip;
562 B.b_kw "is contradictory, hence"])
564 [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
565 B.b_kw "is contradictory, hence" ]
566 | _ -> assert false) in
567 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
568 make_row arg proof_conclusion
570 and andind conclude =
572 (match conclude.Con.conclude_args with
573 [Con.Aux(n);_;Con.ArgProof proof;case_arg] -> proof,case_arg
576 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
580 Con.Aux n -> assert false
581 | Con.Premise prem ->
582 (match prem.Con.premise_binder with
584 | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
586 [(B.b_kw "by");B.skip;
587 B.Object([], P.Mi([],lemma.Con.lemma_name))]
588 | _ -> assert false) in
589 match proof.Con.proof_context with
590 `Hypothesis hyp1::`Hypothesis hyp2::tl ->
592 (match hyp.Con.dec_name with
598 B.Object ([], P.Mi([],get_name hyp1));
601 term2pres hyp1.Con.dec_type]) in
605 B.Object ([], P.Mi([],get_name hyp2));
608 term2pres hyp2.Con.dec_type]) in
609 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
610 let body = conclude2pres proof.Con.proof_conclude false true in
612 acontext2pres proof.Con.proof_apply_context body false in
615 [B.H ([],arg@[B.skip; B.b_kw "we have"]);
622 and exists conclude =
624 (match conclude.Con.conclude_args with
625 [Con.Aux(n);_;Con.ArgProof proof;_] -> proof
628 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
630 match proof.Con.proof_context with
631 `Declaration decl::`Hypothesis hyp::tl
632 | `Hypothesis decl::`Hypothesis hyp::tl ->
634 (match decl.Con.dec_name with
641 B.Object ([], P.Mi([],get_name decl));
642 B.Text([],":"); term2pres decl.Con.dec_type]) in
645 [(B.b_kw "such that");
648 B.Object ([], P.Mi([],get_name hyp));
651 term2pres hyp.Con.dec_type]) in
652 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
653 let body = conclude2pres proof.Con.proof_conclude false true in
655 acontext2pres proof.Con.proof_apply_context body false in
670 let conjecture2pres term2pres (id, n, context, ty) =
672 (B.b_hv [Some "helm", "xref", id]
674 B.b_h [] [B.b_text [] "{...}"; B.b_space];
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)) ] ::
718 [ B.b_text [] (Utf8Macro.unicode_of_tex "\\vdash");
719 B.b_object (p_mi [] (string_of_int n)) ;
723 let metasenv2pres term2pres = function
726 (* Conjectures are in their own table to make *)
727 (* diffing the DOM trees easier. *)
729 ((B.b_kw ("Conjectures:" ^
730 (let _ = incr counter; in (string_of_int !counter)))) ::
731 (List.map (conjecture2pres term2pres) metasenv'))]
733 let params2pres params =
735 B.b_text [Some "xlink", "href", UriManager.string_of_uri uri]
736 (UriManager.name_of_uri uri)
738 let rec spatiate = function
741 | hd :: tl -> hd :: B.b_text [] ", " :: spatiate tl
746 let params = spatiate (List.map param2pres p) in
748 B.b_h [] (B.b_text [] "[" :: params @ [ B.b_text [] "]" ])]
750 let recursion_kind2pres params kind =
753 | `Recursive _ -> "Recursive definition"
754 | `CoRecursive -> "CoRecursive definition"
755 | `Inductive _ -> "Inductive definition"
756 | `CoInductive _ -> "CoInductive definition"
758 B.b_h [] (B.b_kw kind :: params2pres params)
760 let inductive2pres term2pres ind =
761 let constructor2pres decl =
763 B.b_text [] ("| " ^ get_name decl.Content.dec_name ^ ":");
765 term2pres decl.Content.dec_type
770 B.b_kw (ind.Content.inductive_name ^ " of arity");
772 term2pres ind.Content.inductive_type ]
773 :: List.map constructor2pres ind.Content.inductive_constructors)
775 let joint_def2pres term2pres def =
777 | `Inductive ind -> inductive2pres term2pres ind
778 | _ -> assert false (* ZACK or raise ToDo? *)
780 let content2pres term2pres (id,params,metasenv,obj) =
782 | `Def (Content.Const, thesis, `Proof p) ->
783 let name = get_name p.Content.proof_name in
785 [Some "helm","xref","id"]
786 ([ B.b_h [] (B.b_kw ("Proof " ^ name) :: params2pres params);
788 B.indent (term2pres thesis) ] @
789 metasenv2pres term2pres metasenv @
790 [proof2pres term2pres p])
791 | `Def (_, ty, `Definition body) ->
792 let name = get_name body.Content.def_name in
794 [Some "helm","xref","id"]
795 ([B.b_h [] (B.b_kw ("Definition " ^ name) :: params2pres params);
797 B.indent (term2pres ty)] @
798 metasenv2pres term2pres metasenv @
799 [B.b_kw "Body:"; term2pres body.Content.def_term])
800 | `Decl (_, `Declaration decl)
801 | `Decl (_, `Hypothesis decl) ->
802 let name = get_name decl.Content.dec_name in
804 [Some "helm","xref","id"]
805 ([B.b_h [] (B.b_kw ("Axiom " ^ name) :: params2pres params);
807 B.indent (term2pres decl.Content.dec_type)] @
808 metasenv2pres term2pres metasenv)
811 (recursion_kind2pres params joint.Content.joint_kind
812 :: List.map (joint_def2pres term2pres) joint.Content.joint_defs)
815 let content2pres ~ids_to_inner_sorts =
818 let ast, ids_to_uris =
819 TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
821 CicNotationPres.box_of_mpres
822 (CicNotationPres.render ids_to_uris
823 (TermContentPres.pp_ast ast)))