doc-src/antiquote_setup.ML
author wenzelm
Wed, 23 Apr 2008 15:04:14 +0200
changeset 26742 5a86bc79431c
parent 26710 f79aa228c582
child 26751 2b97ea3130c2
permissions -rw-r--r--
misc cleanup;
     1 (*  Title:      Doc/antiquote_setup.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Auxiliary antiquotations for the Isabelle manuals.
     6 *)
     7 
     8 structure AntiquoteSetup: sig end =
     9 struct
    10 
    11 structure O = ThyOutput;
    12 
    13 
    14 (* misc utils *)
    15 
    16 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
    17 
    18 fun tweak_line s =
    19   if ! O.display then s else Symbol.strip_blanks s;
    20 
    21 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
    22 
    23 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
    24 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
    25 
    26 
    27 (* verbatim text *)
    28 
    29 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    30 
    31 val _ = O.add_commands
    32  [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
    33       split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
    34 
    35 
    36 (* ML text *)
    37 
    38 local
    39 
    40 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    41   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    42 
    43 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    44   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    45 
    46 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
    47   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
    48 
    49 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    50 
    51 fun ml_functor _ = "val _ = ();";  (*no check!*)
    52 
    53 fun index_ml kind ml src ctxt (txt1, txt2) =
    54   let
    55     val txt = if txt2 = "" then txt1 else
    56       if kind = "type" then txt1 ^ " = " ^ txt2
    57       else if kind = "exception" then txt1 ^ " of " ^ txt2
    58       else txt1 ^ ": " ^ txt2;
    59     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    60     val _ = writeln (ml (txt1, txt2));
    61     val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
    62   in
    63     "\\indexml" ^ kind ^ enclose "{" "}"
    64       (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^
    65     ((if ! O.source then str_of_source src else txt')
    66     |> (if ! O.quotes then quote else I)
    67     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    68     else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    69   end;
    70 
    71 fun output_ml ctxt src =
    72   if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    73   else
    74     split_lines
    75     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
    76     #> space_implode "\\isasep\\isanewline%\n";
    77 
    78 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    79 
    80 in
    81 
    82 val _ = O.add_commands
    83  [("index_ML", ml_args (index_ml "" ml_val)),
    84   ("index_ML_type", ml_args (index_ml "type" ml_type)),
    85   ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
    86   ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
    87   ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
    88   ("ML_functor", O.args (Scan.lift Args.name) output_ml),
    89   ("ML_text", O.args (Scan.lift Args.name) output_ml)];
    90 
    91 end;
    92 
    93 
    94 (* theorems with names *)
    95 
    96 local
    97 
    98 fun output_named_thms src ctxt xs =
    99   map (apfst (pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
   100   |> (if ! O.quotes then map (apfst Pretty.quote) else I)
   101   |> (if ! O.display then
   102     map (fn (p, name) =>
   103       Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
   104       "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
   105     #> space_implode "\\par\\smallskip%\n"
   106     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   107   else
   108     map (fn (p, name) =>
   109       Output.output (Pretty.str_of p) ^
   110       "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
   111     #> space_implode "\\par\\smallskip%\n"
   112     #> enclose "\\isa{" "}");
   113 
   114 in
   115 
   116 val _ = O.add_commands
   117  [("named_thms", O.args (Scan.repeat (Attrib.thm --
   118       Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")"))) output_named_thms)];
   119 
   120 end;
   121 
   122 
   123 (* theory files *)
   124 
   125 val _ = O.add_commands
   126  [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
   127       (ThyLoad.check_thy Path.current name; Pretty.str name))))];
   128 
   129 end;