doc-src/antiquote_setup.ML
author wenzelm
Mon Mar 09 20:34:11 2009 +0100 (2009-03-09)
changeset 30394 c11a1e65a2ed
parent 30382 910290f04692
child 30396 841ce0fcbe14
permissions -rw-r--r--
moved @{ML_functor} and @{ML_text} to Pure;
adapted to simplified ThyOutput.antiquotation interface;
misc tuning;
     1 (*  Title:      doc-src/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 (* misc utils *)
    11 
    12 fun translate f = Symbol.explode #> map f #> implode;
    13 
    14 val clean_string = translate
    15   (fn "_" => "\\_"
    16     | "#" => "\\#"
    17     | "<" => "$<$"
    18     | ">" => "$>$"
    19     | "{" => "\\{"
    20     | "|" => "$\\mid$"
    21     | "}" => "\\}"
    22     | "\\<dash>" => "-"
    23     | c => c);
    24 
    25 fun clean_name "\\<dots>" = "dots"
    26   | clean_name ".." = "ddot"
    27   | clean_name "." = "dot"
    28   | clean_name "_" = "underscore"
    29   | clean_name "{" = "braceleft"
    30   | clean_name "}" = "braceright"
    31   | clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c);
    32 
    33 
    34 (* verbatim text *)
    35 
    36 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    37 
    38 val _ = ThyOutput.antiquotation "verbatim" (Scan.lift Args.name)
    39   (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    40 
    41 
    42 (* ML text *)
    43 
    44 local
    45 
    46 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    47   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    48 
    49 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    50   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    51 
    52 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
    53   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
    54 
    55 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    56 
    57 fun ml_functor _ = "";  (*no check!*)
    58 
    59 fun index_ml name kind ml = ThyOutput.antiquotation name
    60   (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    61   (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    62     let
    63       val txt =
    64         if txt2 = "" then txt1
    65         else if kind = "type" then txt1 ^ " = " ^ txt2
    66         else if kind = "exception" then txt1 ^ " of " ^ txt2
    67         else txt1 ^ ": " ^ txt2;
    68       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    69       val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
    70       val kind' = if kind = "" then "ML" else "ML " ^ kind;
    71     in
    72       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
    73       (txt'
    74       |> (if ! ThyOutput.quotes then quote else I)
    75       |> (if ! ThyOutput.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    76           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    77     end);
    78 
    79 in
    80 
    81 val _ = index_ml "index_ML" "" ml_val;
    82 val _ = index_ml "index_ML_type" "type" ml_type;
    83 val _ = index_ml "index_ML_exn" "exception" ml_exn;
    84 val _ = index_ml "index_ML_structure" "structure" ml_structure;
    85 val _ = index_ml "index_ML_functor" "functor" ml_functor;
    86 
    87 end;
    88 
    89 
    90 (* named theorems *)
    91 
    92 val _ = ThyOutput.antiquotation "named_thms"
    93   (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
    94   (fn {context = ctxt, ...} =>
    95     map (apfst (ThyOutput.pretty_thm ctxt))
    96     #> (if ! ThyOutput.quotes then map (apfst Pretty.quote) else I)
    97     #> (if ! ThyOutput.display
    98         then
    99           map (fn (p, name) =>
   100             Output.output (Pretty.string_of (Pretty.indent (! ThyOutput.indent) p)) ^
   101             "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   102           #> space_implode "\\par\\smallskip%\n"
   103           #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   104         else
   105           map (fn (p, name) =>
   106             Output.output (Pretty.str_of p) ^
   107             "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   108           #> space_implode "\\par\\smallskip%\n"
   109           #> enclose "\\isa{" "}"));
   110 
   111 
   112 (* theory file *)
   113 
   114 val _ = ThyOutput.antiquotation "thy_file" (Scan.lift Args.name)
   115   (fn _ => fn name => (ThyLoad.check_thy Path.current name; ThyOutput.output [Pretty.str name]));
   116 
   117 
   118 (* Isabelle/Isar entities (with index) *)
   119 
   120 local
   121 
   122 fun no_check _ _ = true;
   123 
   124 fun thy_check intern defined ctxt =
   125   let val thy = ProofContext.theory_of ctxt
   126   in defined thy o intern thy end;
   127 
   128 fun check_tool name =
   129   File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   130 
   131 val arg = enclose "{" "}" o clean_string;
   132 
   133 fun entity check markup kind index =
   134   ThyOutput.antiquotation
   135     (case index of NONE => kind | SOME true => kind ^ "_def" | SOME false => kind ^ "_ref")
   136     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   137     (fn {context = ctxt, ...} => fn (logic, name) =>
   138       let
   139         val hyper_name =
   140           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   141         val hyper =
   142           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   143           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   144         val idx =
   145           (case index of
   146             NONE => ""
   147           | SOME is_def =>
   148               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   149       in
   150         if check ctxt name then
   151           idx ^
   152           (Output.output name
   153             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   154             |> (if ! ThyOutput.quotes then quote else I)
   155             |> (if ! ThyOutput.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   156                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   157         else error ("Bad " ^ kind ^ " " ^ quote name)
   158       end);
   159 
   160 fun entity_antiqs check markup kind =
   161  [(entity check markup kind NONE),
   162   (entity check markup kind (SOME true)),
   163   (entity check markup kind (SOME false))];
   164 
   165 in
   166 
   167 val _ = entity_antiqs no_check "" "syntax";
   168 val _ = entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command";
   169 val _ = entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword";
   170 val _ = entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element";
   171 val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
   172 val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
   173 val _ = entity_antiqs no_check "" "fact";
   174 val _ = entity_antiqs no_check "" "variable";
   175 val _ = entity_antiqs no_check "" "case";
   176 val _ = entity_antiqs (K ThyOutput.defined_command) "" "antiquotation";
   177 val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
   178 val _ = entity_antiqs no_check "" "inference";
   179 val _ = entity_antiqs no_check "isatt" "executable";
   180 val _ = entity_antiqs (K check_tool) "isatt" "tool";
   181 val _ = entity_antiqs (K (File.exists o Path.explode)) "isatt" "file";
   182 val _ = entity_antiqs (K ThyInfo.known_thy) "" "theory";
   183 
   184 end;
   185 
   186 end;