doc-src/antiquote_setup.ML
author wenzelm
Sat, 26 Apr 2008 13:20:16 +0200
changeset 26751 2b97ea3130c2
parent 26742 5a86bc79431c
child 26756 6634a641b961
permissions -rw-r--r--
added setup for Isar entities;
tuned;
     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 clean_string =
    17   translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c);
    18 
    19 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
    20 
    21 fun tweak_line s =
    22   if ! O.display then s else Symbol.strip_blanks s;
    23 
    24 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
    25 
    26 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
    27 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
    28 
    29 
    30 (* verbatim text *)
    31 
    32 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    33 
    34 val _ = O.add_commands
    35  [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
    36       split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
    37 
    38 
    39 (* ML text *)
    40 
    41 local
    42 
    43 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    44   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    45 
    46 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    47   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    48 
    49 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
    50   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
    51 
    52 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    53 
    54 fun ml_functor _ = "val _ = ();";  (*no check!*)
    55 
    56 fun index_ml kind ml src ctxt (txt1, txt2) =
    57   let
    58     val txt = if txt2 = "" then txt1 else
    59       if kind = "type" then txt1 ^ " = " ^ txt2
    60       else if kind = "exception" then txt1 ^ " of " ^ txt2
    61       else txt1 ^ ": " ^ txt2;
    62     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    63     val _ = writeln (ml (txt1, txt2));
    64     val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
    65   in
    66     "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
    67     ((if ! O.source then str_of_source src else txt')
    68     |> (if ! O.quotes then quote else I)
    69     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    70         else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    71   end;
    72 
    73 fun output_ml ctxt src =
    74   if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    75   else
    76     split_lines
    77     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
    78     #> space_implode "\\isasep\\isanewline%\n";
    79 
    80 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    81 
    82 in
    83 
    84 val _ = O.add_commands
    85  [("index_ML", ml_args (index_ml "" ml_val)),
    86   ("index_ML_type", ml_args (index_ml "type" ml_type)),
    87   ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
    88   ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
    89   ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
    90   ("ML_functor", O.args (Scan.lift Args.name) output_ml),
    91   ("ML_text", O.args (Scan.lift Args.name) output_ml)];
    92 
    93 end;
    94 
    95 
    96 (* theorems with names *)
    97 
    98 local
    99 
   100 fun output_named_thms src ctxt xs =
   101   map (apfst (pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
   102   |> (if ! O.quotes then map (apfst Pretty.quote) else I)
   103   |> (if ! O.display then
   104     map (fn (p, name) =>
   105       Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
   106       "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
   107     #> space_implode "\\par\\smallskip%\n"
   108     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   109   else
   110     map (fn (p, name) =>
   111       Output.output (Pretty.str_of p) ^
   112       "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
   113     #> space_implode "\\par\\smallskip%\n"
   114     #> enclose "\\isa{" "}");
   115 
   116 in
   117 
   118 val _ = O.add_commands
   119  [("named_thms", O.args (Scan.repeat (Attrib.thm --
   120       Scan.lift (Args.parens Args.name))) output_named_thms)];
   121 
   122 end;
   123 
   124 
   125 (* theory files *)
   126 
   127 val _ = O.add_commands
   128  [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
   129       (ThyLoad.check_thy Path.current name; Pretty.str name))))];
   130 
   131 
   132 (* Isar entities (with index) *)
   133 
   134 local
   135 
   136 val arg = enclose "{" "}" o clean_string;
   137 
   138 fun output_entity index kind src ctxt (logic, name) =
   139   (case index of
   140     NONE => ""
   141   | SOME is_def =>
   142       "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name)
   143   ^
   144   (Output.output (if ! O.source then str_of_source src else name)
   145     |> (if ! O.quotes then quote else I)
   146     |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   147         else enclose "\\isa{" "}"));
   148 
   149 fun entity index kind =
   150   O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   151     (output_entity index kind);
   152     
   153 fun entity_antiqs kind =
   154  [(kind, entity NONE kind),
   155   (kind ^ "_def", entity (SOME true) kind), 
   156   (kind ^ "_ref", entity (SOME false) kind)];
   157 
   158 in
   159 
   160 val _ = O.add_commands
   161  (entity_antiqs "syntax" @
   162   entity_antiqs "command" @
   163   entity_antiqs "keyword" @
   164   entity_antiqs "element" @
   165   entity_antiqs "method" @
   166   entity_antiqs "attribute" @
   167   entity_antiqs "fact" @
   168   entity_antiqs "term" @
   169   entity_antiqs "case" @
   170   entity_antiqs "antiquotation");
   171 
   172 end;
   173 
   174 end;