doc-src/antiquote_setup.ML
author wenzelm
Mon Apr 28 14:42:13 2008 +0200 (2008-04-28)
changeset 26756 6634a641b961
parent 26751 2b97ea3130c2
child 26768 844068d16ba0
permissions -rw-r--r--
proper command/keyword markup;
     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 markup 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 markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   146     |> (if ! O.quotes then quote else I)
   147     |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   148         else enclose "\\isa{" "}"));
   149 
   150 fun entity markup index kind =
   151   O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   152     (output_entity markup index kind);
   153     
   154 fun entity_antiqs markup kind =
   155  [(kind, entity markup NONE kind),
   156   (kind ^ "_def", entity markup (SOME true) kind), 
   157   (kind ^ "_ref", entity markup (SOME false) kind)];
   158 
   159 in
   160 
   161 val _ = O.add_commands
   162  (entity_antiqs "" "syntax" @
   163   entity_antiqs "isacommand" "command" @
   164   entity_antiqs "isakeyword" "keyword" @
   165   entity_antiqs "" "element" @
   166   entity_antiqs "" "method" @
   167   entity_antiqs "" "attribute" @
   168   entity_antiqs "" "fact" @
   169   entity_antiqs "" "variable" @
   170   entity_antiqs "" "case" @
   171   entity_antiqs "" "antiquotation");
   172 
   173 end;
   174 
   175 end;