doc-src/antiquote_setup.ML
author wenzelm
Thu, 26 Feb 2009 20:44:07 +0100
changeset 30120 aaa4667285c8
parent 30062 ec3fc818b82e
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
more robust handling of "|" within index;
     1 (*  Title:      Doc/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 structure AntiquoteSetup: sig end =
     8 struct
     9 
    10 structure O = ThyOutput;
    11 
    12 
    13 (* misc utils *)
    14 
    15 fun translate f = Symbol.explode #> map f #> implode;
    16 
    17 val clean_string = translate
    18   (fn "_" => "\\_"
    19     | "#" => "\\#"
    20     | "<" => "$<$"
    21     | ">" => "$>$"
    22     | "{" => "\\{"
    23     | "|" => "$\\mid$"
    24     | "}" => "\\}"
    25     | "\\<dash>" => "-"
    26     | c => c);
    27 
    28 fun clean_name "\\<dots>" = "dots"
    29   | clean_name ".." = "ddot"
    30   | clean_name "." = "dot"
    31   | clean_name "_" = "underscore"
    32   | clean_name "{" = "braceleft"
    33   | clean_name "}" = "braceright"
    34   | clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c);
    35 
    36 
    37 (* verbatim text *)
    38 
    39 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    40 
    41 val _ = O.add_commands
    42  [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
    43       split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
    44 
    45 
    46 (* ML text *)
    47 
    48 local
    49 
    50 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    51   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    52 
    53 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    54   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    55 
    56 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
    57   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
    58 
    59 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    60 
    61 fun ml_functor _ = "val _ = ();";  (*no check!*)
    62 
    63 fun index_ml kind ml src ctxt (txt1, txt2) =
    64   let
    65     val txt = if txt2 = "" then txt1 else
    66       if kind = "type" then txt1 ^ " = " ^ txt2
    67       else if kind = "exception" then txt1 ^ " of " ^ txt2
    68       else txt1 ^ ": " ^ txt2;
    69     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    70     val _ = writeln (ml (txt1, txt2));
    71     val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
    72     val kind' = if kind = "" then "ML" else "ML " ^ kind;
    73   in
    74     "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
    75     (txt'
    76     |> (if ! O.quotes then quote else I)
    77     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    78         else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    79   end;
    80 
    81 fun output_ml ctxt src =
    82   if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    83   else
    84     split_lines
    85     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
    86     #> space_implode "\\isasep\\isanewline%\n";
    87 
    88 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    89 
    90 in
    91 
    92 val _ = O.add_commands
    93  [("index_ML", ml_args (index_ml "" ml_val)),
    94   ("index_ML_type", ml_args (index_ml "type" ml_type)),
    95   ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
    96   ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
    97   ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
    98   ("ML_functor", O.args (Scan.lift Args.name) output_ml),
    99   ("ML_text", O.args (Scan.lift Args.name) output_ml)];
   100 
   101 end;
   102 
   103 
   104 (* theorems with names *)
   105 
   106 local
   107 
   108 fun output_named_thms src ctxt xs =
   109   map (apfst (ThyOutput.pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
   110   |> (if ! O.quotes then map (apfst Pretty.quote) else I)
   111   |> (if ! O.display then
   112     map (fn (p, name) =>
   113       Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
   114       "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   115     #> space_implode "\\par\\smallskip%\n"
   116     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   117   else
   118     map (fn (p, name) =>
   119       Output.output (Pretty.str_of p) ^
   120       "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   121     #> space_implode "\\par\\smallskip%\n"
   122     #> enclose "\\isa{" "}");
   123 
   124 in
   125 
   126 val _ = O.add_commands
   127  [("named_thms", O.args (Scan.repeat (Attrib.thm --
   128       Scan.lift (Args.parens Args.name))) output_named_thms)];
   129 
   130 end;
   131 
   132 
   133 (* theory files *)
   134 
   135 val _ = O.add_commands
   136  [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
   137       (ThyLoad.check_thy Path.current name; Pretty.str name))))];
   138 
   139 
   140 (* Isabelle entities (with index) *)
   141 
   142 local
   143 
   144 fun no_check _ _ = true;
   145 
   146 fun thy_check intern defined ctxt =
   147   let val thy = ProofContext.theory_of ctxt
   148   in defined thy o intern thy end;
   149 
   150 fun check_tool name =
   151   File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   152 
   153 val arg = enclose "{" "}" o clean_string;
   154 
   155 fun output_entity check markup index kind ctxt (logic, name) =
   156   let
   157     val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}";
   158     val hyper =
   159       enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   160       index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   161     val idx =
   162       (case index of
   163         NONE => ""
   164       | SOME is_def =>
   165           "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   166   in
   167     if check ctxt name then
   168       idx ^
   169       (Output.output name
   170         |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   171         |> (if ! O.quotes then quote else I)
   172         |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   173             else hyper o enclose "\\mbox{\\isa{" "}}"))
   174     else error ("Bad " ^ kind ^ " " ^ quote name)
   175   end;
   176 
   177 fun entity check markup index kind =
   178   O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   179     (K (output_entity check markup index kind));
   180 
   181 fun entity_antiqs check markup kind =
   182  [(kind, entity check markup NONE kind),
   183   (kind ^ "_def", entity check markup (SOME true) kind),
   184   (kind ^ "_ref", entity check markup (SOME false) kind)];
   185 
   186 in
   187 
   188 val _ = O.add_commands
   189  (entity_antiqs no_check "" "syntax" @
   190   entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @
   191   entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @
   192   entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @
   193   entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
   194   entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
   195   entity_antiqs no_check "" "fact" @
   196   entity_antiqs no_check "" "variable" @
   197   entity_antiqs no_check "" "case" @
   198   entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @
   199   entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @
   200   entity_antiqs no_check "" "inference" @
   201   entity_antiqs no_check "isatt" "executable" @
   202   entity_antiqs (K check_tool) "isatt" "tool" @
   203   entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @
   204   entity_antiqs (K ThyInfo.known_thy) "" "theory");
   205 
   206 end;
   207 
   208 end;