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
61 let make_row ?(attrs=[]) items concl =
64 B.b_v attrs [B.b_h [] items; B.b_indent concl]
66 B.b_h attrs (items@[B.b_space; concl])
68 let make_concl ?(attrs=[]) verb concl =
71 B.b_v attrs [ B.b_kw verb; B.b_indent concl]
73 B.b_h attrs [ B.b_kw verb; B.b_space; concl ]
75 let make_args_for_apply term2pres args =
76 let make_arg_for_apply is_first arg row =
79 Con.Aux n -> assert false
82 (match prem.Con.premise_binder with
85 (B.b_object (P.Mi ([], name)))::row
88 Some "helm", "xref", lemma.Con.lemma_id;
89 Some "xlink", "href", lemma.Con.lemma_uri ]
91 (B.b_object (P.Mi(lemma_attrs,lemma.Con.lemma_name)))::row
95 else (B.b_object (P.Mi([],"_")))::row
98 (B.b_object (P.Mi([],"_")))::row
100 if is_first then res else B.skip::res
104 make_arg_for_apply true hd
105 (List.fold_right (make_arg_for_apply false) tl [])
108 let get_name = function
112 let add_xref id = function
113 | B.Text (attrs, t) -> B.Text (((Some "helm", "xref", id) :: attrs), t)
114 | _ -> assert false (* TODO, add_xref is meaningful for all boxes *)
116 let rec justification term2pres p =
117 if ((p.Con.proof_conclude.Con.conclude_method = "Exact") or
118 ((p.Con.proof_context = []) &
119 (p.Con.proof_apply_context = []) &
120 (p.Con.proof_conclude.Con.conclude_method = "Apply"))) then
122 make_args_for_apply term2pres p.Con.proof_conclude.Con.conclude_args in
124 (B.b_kw "by")::B.b_space::
125 B.Text([],"(")::pres_args@[B.Text([],")")])
126 else proof2pres term2pres p
128 and proof2pres term2pres p =
129 let rec proof2pres p =
134 | `Hypothesis _ -> true
136 ((List.filter is_decl p.Con.proof_context) != []) in
137 let omit_conclusion = (not indent) && (p.Con.proof_context != []) in
139 (match p.Con.proof_conclude.Con.conclude_conclusion with
141 | Some t -> Some (term2pres t)) in
144 conclude2pres p.Con.proof_conclude indent omit_conclusion in
146 acontext2pres p.Con.proof_apply_context presconclude indent in
147 context2pres p.Con.proof_context presacontext in
148 match p.Con.proof_name with
156 ([None,"type","toggle"],
157 [(make_concl ~attrs:[Some "helm", "xref", p.Con.proof_id]
158 "proof of" ac); body])
161 [B.Text ([],"(" ^ name ^ ")");
164 and context2pres c continuation =
165 (* we generate a subtable for each context element, for selection
167 The table generated by the head-element does not have an xref;
168 the whole context-proof is already selectable *)
174 (fun ce continuation ->
175 let xref = get_xref ce in
176 B.V([Some "helm", "xref", xref ],
177 [B.H([Some "helm", "xref", "ce_"^xref],
178 [ce2pres_in_proof_context_element ce]);
179 continuation])) tl continuation in
180 let hd_xref= get_xref hd in
182 [B.H([Some "helm", "xref", "ce_"^hd_xref],
183 [ce2pres_in_proof_context_element hd]);
186 and ce2pres_in_joint_context_element = function
187 | `Inductive _ -> assert false (* TODO *)
188 | (`Declaration _) as x -> ce2pres x
189 | (`Hypothesis _) as x -> ce2pres x
190 | (`Proof _) as x -> ce2pres x
191 | (`Definition _) as x -> ce2pres x
193 and ce2pres_in_proof_context_element = function
195 B.H ([],(List.map ce2pres_in_joint_context_element ho.Content.joint_defs))
196 | (`Declaration _) as x -> ce2pres x
197 | (`Hypothesis _) as x -> ce2pres x
198 | (`Proof _) as x -> ce2pres x
199 | (`Definition _) as x -> ce2pres x
204 (match d.Con.dec_name with
206 let ty = term2pres d.Con.dec_type in
210 B.Object ([], P.Mi([],s));
214 prerr_endline "NO NAME!!"; assert false)
216 (match h.Con.dec_name with
218 let ty = term2pres h.Con.dec_type in
223 B.Object ([], P.Mi ([],s));
228 prerr_endline "NO NAME!!"; assert false)
232 (match d.Con.def_name with
234 let term = term2pres d.Con.def_term in
236 [ B.b_kw "Let"; B.b_space;
237 B.Object ([], P.Mi([],s));
241 prerr_endline "NO NAME!!"; assert false)
243 and acontext2pres ac continuation indent =
245 (fun p continuation ->
248 B.indent (proof2pres p)
251 B.V([Some "helm","xref",p.Con.proof_id],
252 [B.H([Some "helm","xref","ace_"^p.Con.proof_id],[hd]);
253 continuation])) ac continuation
255 and conclude2pres conclude indent omit_conclusion =
257 match conclude.Con.conclude_conclusion with
259 not omit_conclusion or
260 (* CSC: I ignore the omit_conclusion flag in this case. *)
261 (* CSC: Is this the correct behaviour? In the stylesheets *)
262 (* CSC: we simply generated nothing (i.e. the output type *)
263 (* CSC: of the function should become an option. *)
264 conclude.Con.conclude_method = "BU_Conversion" ->
265 let concl = (term2pres t) in
266 if conclude.Con.conclude_method = "BU_Conversion" then
267 make_concl "that is equivalent to" concl
268 else if conclude.Con.conclude_method = "FalseInd" then
269 (* false ind is in charge to add the conclusion *)
272 let conclude_body = conclude_aux conclude in
274 if conclude.Con.conclude_method = "TD_Conversion" then
275 make_concl "that is equivalent to" concl
276 else make_concl "we conclude" concl in
277 B.V ([], [conclude_body; ann_concl])
278 | _ -> conclude_aux conclude in
280 B.indent (B.H ([Some "helm", "xref", conclude.Con.conclude_id],
283 B.H ([Some "helm", "xref", conclude.Con.conclude_id],[tconclude_body])
285 and conclude_aux conclude =
286 if conclude.Con.conclude_method = "TD_Conversion" then
288 (match conclude.Con.conclude_conclusion with
289 None -> B.Text([],"NO EXPECTED!!!")
290 | Some c -> term2pres c) in
292 (match conclude.Con.conclude_args with
293 [Con.ArgProof p] -> p
294 | _ -> assert false) in
296 (match subproof.Con.proof_conclude.Con.conclude_conclusion with
297 None -> B.Text([],"NO SYNTH!!!")
298 | Some c -> (term2pres c)) in
301 [make_concl "we must prove" expected;
302 make_concl "or equivalently" synth;
303 proof2pres subproof])
304 else if conclude.Con.conclude_method = "BU_Conversion" then
306 else if conclude.Con.conclude_method = "Exact" then
308 (match conclude.Con.conclude_args with
309 [Con.Term t] -> term2pres t
311 (match p.Con.premise_binder with
312 | None -> assert false; (* unnamed hypothesis ??? *)
313 | Some s -> B.Text([],s))
314 | err -> assert false) in
315 (match conclude.Con.conclude_conclusion with
317 B.b_h [] [B.b_kw "Consider"; B.b_space; arg]
318 | Some c -> let conclusion = term2pres c in
320 [arg; B.b_space; B.b_kw "proves"]
323 else if conclude.Con.conclude_method = "Intros+LetTac" then
324 (match conclude.Con.conclude_args with
325 [Con.ArgProof p] -> proof2pres p
329 (match conclude.Con.conclude_conclusion with
330 None -> B.Text([],"NO Conclusion!!!")
331 | Some c -> term2pres c) in
332 (match conclude.Con.conclude_args with
335 ([None,"align","baseline 1"; None,"equalrows","false";
336 None,"columnalign","left"],
337 [B.H([],[B.Object([],proof2pres p)]);
339 (make_concl "we proved 1" conclusion))])]);
342 else if (conclude.Con.conclude_method = "Case") then
344 else if (conclude.Con.conclude_method = "ByInduction") then
346 else if (conclude.Con.conclude_method = "Exists") then
348 else if (conclude.Con.conclude_method = "AndInd") then
350 else if (conclude.Con.conclude_method = "FalseInd") then
352 else if (conclude.Con.conclude_method = "Rewrite") then
354 (match (List.nth conclude.Con.conclude_args 6) with
355 Con.ArgProof p -> justification term2pres p
356 | _ -> assert false) in
358 (match List.nth conclude.Con.conclude_args 2 with
359 Con.Term t -> term2pres t
360 | _ -> assert false) in
362 (match List.nth conclude.Con.conclude_args 5 with
363 Con.Term t -> term2pres t
364 | _ -> assert false) in
369 B.b_space; (B.b_kw "with");
372 else if conclude.Con.conclude_method = "Apply" then
374 make_args_for_apply term2pres conclude.Con.conclude_args in
378 B.Text([],"(")::pres_args@[B.Text([],")")])
381 B.b_kw ("Apply method" ^ conclude.Con.conclude_method ^ " to");
382 (B.indent (B.V ([], args2pres conclude.Con.conclude_args)))])
384 and args2pres l = List.map arg2pres l
388 Con.Aux n -> B.b_kw ("aux " ^ n)
389 | Con.Premise prem -> B.b_kw "premise"
390 | Con.Lemma lemma -> B.b_kw "lemma"
391 | Con.Term t -> term2pres t
392 | Con.ArgProof p -> proof2pres p
393 | Con.ArgMethod s -> B.b_kw "method"
396 let proof_conclusion =
397 (match conclude.Con.conclude_conclusion with
398 None -> B.b_kw "No conclusion???"
399 | Some t -> term2pres t) in
400 let arg,args_for_cases =
401 (match conclude.Con.conclude_args with
402 Con.Aux(_)::Con.Aux(_)::Con.Term(_)::arg::tl ->
404 | _ -> assert false) in
408 Con.Aux n -> B.b_kw "an aux???"
409 | Con.Premise prem ->
410 (match prem.Con.premise_binder with
411 None -> B.b_kw "the previous result"
412 | Some n -> B.Object ([], P.Mi([],n)))
413 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
416 | Con.ArgProof p -> B.b_kw "a proof???"
417 | Con.ArgMethod s -> B.b_kw "a method???")
419 (make_concl "we proceed by cases on" case_arg) in
421 (make_concl "to prove" proof_conclusion) in
422 B.V ([], case_on::to_prove::(make_cases args_for_cases))
424 and byinduction conclude =
425 let proof_conclusion =
426 (match conclude.Con.conclude_conclusion with
427 None -> B.b_kw "No conclusion???"
428 | Some t -> term2pres t) in
429 let inductive_arg,args_for_cases =
430 (match conclude.Con.conclude_args with
432 let l1,l2 = split (int_of_string n) tl in
433 let last_pos = (List.length l2)-1 in
434 List.nth l2 last_pos,l1
435 | _ -> assert false) in
438 (match inductive_arg with
439 Con.Aux n -> B.b_kw "an aux???"
440 | Con.Premise prem ->
441 (match prem.Con.premise_binder with
442 None -> B.b_kw "the previous result"
443 | Some n -> B.Object ([], P.Mi([],n)))
444 | Con.Lemma lemma -> B.Object ([], P.Mi([],lemma.Con.lemma_name))
447 | Con.ArgProof p -> B.b_kw "a proof???"
448 | Con.ArgMethod s -> B.b_kw "a method???") in
449 (make_concl "we proceed by induction on" arg) in
451 (make_concl "to prove" proof_conclusion) in
452 B.V ([], induction_on::to_prove:: (make_cases args_for_cases))
454 and make_cases l = List.map make_case l
460 (match p.Con.proof_name with
461 None -> B.b_kw "no name for case!!"
462 | Some n -> B.Object ([], P.Mi([],n))) in
466 `Hypothesis h -> h.Con.dec_inductive
467 | _ -> false) p.Con.proof_context in
476 (match h.Con.dec_name with
480 B.Object ([], P.Mi ([],name));
482 (term2pres h.Con.dec_type)]
483 | _ -> [B.Text ([],"???")]) in
487 (B.b_kw "Case"::B.b_space::name::pattern_aux)@
489 B.Text([], Utf8Macro.unicode_of_tex "\\Rightarrow")]) in
491 (match p.Con.proof_conclude.Con.conclude_conclusion with
492 None -> B.b_kw "No conclusion!!!"
493 | Some t -> term2pres t) in
494 let asubconcl = B.indent (make_concl "the thesis becomes" subconcl) in
495 let induction_hypothesis =
499 let text = B.indent (B.b_kw "by induction hypothesis we know") in
504 (match h.Con.dec_name with
509 B.Object ([], P.Mi ([],name));
512 term2pres h.Con.dec_type]))
513 | _ -> assert false in
514 let hyps = List.map make_hyp indhyps in
517 acontext2pres_old p.Con.proof_apply_context true in *)
518 let body = conclude2pres p.Con.proof_conclude true false in
521 match p.Con.proof_apply_context with
522 [] -> p.Con.proof_conclude.Con.conclude_id
523 | {Con.proof_id = id}::_ -> id
525 B.Action([None,"type","toggle"],
526 [ B.indent (add_xref acontext_id (B.b_kw "Proof"));
527 acontext2pres p.Con.proof_apply_context body true]) in
528 B.V ([], pattern::asubconcl::induction_hypothesis@[presacontext])
531 and falseind conclude =
532 let proof_conclusion =
533 (match conclude.Con.conclude_conclusion with
534 None -> B.b_kw "No conclusion???"
535 | Some t -> term2pres t) in
537 (match conclude.Con.conclude_args with
538 [Con.Aux(n);_;case_arg] -> case_arg
541 List.map (ContentPp.parg 0) conclude.Con.conclude_args;
545 Con.Aux n -> assert false
546 | Con.Premise prem ->
547 (match prem.Con.premise_binder with
548 None -> [B.b_kw "Contradiction, hence"]
550 [ B.Object ([],P.Mi([],n)); B.skip;
551 B.b_kw "is contradictory, hence"])
553 [ B.Object ([], P.Mi([],lemma.Con.lemma_name)); B.skip;
554 B.b_kw "is contradictory, hence" ]
555 | _ -> assert false) in
556 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
557 make_row arg proof_conclusion
559 and andind 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);_;Con.ArgProof proof;case_arg] -> proof,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
577 | Some n -> [(B.b_kw "by"); B.b_space; B.Object([], P.Mi([],n))])
579 [(B.b_kw "by");B.skip;
580 B.Object([], P.Mi([],lemma.Con.lemma_name))]
581 | _ -> assert false) in
582 match proof.Con.proof_context with
583 `Hypothesis hyp1::`Hypothesis hyp2::tl ->
585 (match hyp.Con.dec_name with
591 B.Object ([], P.Mi([],get_name hyp1));
594 term2pres hyp1.Con.dec_type]) in
598 B.Object ([], P.Mi([],get_name hyp2));
601 term2pres hyp2.Con.dec_type]) in
602 (* let body = proof2pres {proof with Con.proof_context = tl} in *)
603 let body = conclude2pres proof.Con.proof_conclude false true in
605 acontext2pres proof.Con.proof_apply_context body false in
608 [B.H ([],arg@[B.skip; B.b_kw "we have"]);
615 and exists conclude =
616 let proof_conclusion =
617 (match conclude.Con.conclude_conclusion with
618 None -> B.b_kw "No conclusion???"
619 | Some t -> term2pres t) in
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 CicNotationRew.ast_of_acic ids_to_inner_sorts annterm
814 CicNotationPres.box_of_mpres
815 (CicNotationPres.render ids_to_uris
816 (CicNotationRew.pp_ast ast)))