doc-src/antiquote_setup.ML
author wenzelm
Sun Mar 08 15:01:10 2009 +0100 (2009-03-08)
changeset 30358 f7fea73b97a6
parent 30242 aea5d7fa7ef5
child 30365 790129514c2d
permissions -rw-r--r--
index_ML: removed spurious writeln introduced in 41ce4f5c97c9 -- it merely produces unreadable LaTeX sources;
     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 _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
    71     val kind' = if kind = "" then "ML" else "ML " ^ kind;
    72   in
    73     "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
    74     (txt'
    75     |> (if ! O.quotes then quote else I)
    76     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    77         else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    78   end;
    79 
    80 fun output_ml ctxt src =
    81   if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    82   else
    83     split_lines
    84     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
    85     #> space_implode "\\isasep\\isanewline%\n";
    86 
    87 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    88 
    89 in
    90 
    91 val _ = O.add_commands
    92  [("index_ML", ml_args (index_ml "" ml_val)),
    93   ("index_ML_type", ml_args (index_ml "type" ml_type)),
    94   ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
    95   ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
    96   ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
    97   ("ML_functor", O.args (Scan.lift Args.name) output_ml),
    98   ("ML_text", O.args (Scan.lift Args.name) output_ml)];
    99 
   100 end;
   101 
   102 
   103 (* theorems with names *)
   104 
   105 local
   106 
   107 fun output_named_thms src ctxt xs =
   108   map (apfst (ThyOutput.pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
   109   |> (if ! O.quotes then map (apfst Pretty.quote) else I)
   110   |> (if ! O.display then
   111     map (fn (p, name) =>
   112       Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
   113       "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   114     #> space_implode "\\par\\smallskip%\n"
   115     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   116   else
   117     map (fn (p, name) =>
   118       Output.output (Pretty.str_of p) ^
   119       "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   120     #> space_implode "\\par\\smallskip%\n"
   121     #> enclose "\\isa{" "}");
   122 
   123 in
   124 
   125 val _ = O.add_commands
   126  [("named_thms", O.args (Scan.repeat (Attrib.thm --
   127       Scan.lift (Args.parens Args.name))) output_named_thms)];
   128 
   129 end;
   130 
   131 
   132 (* theory files *)
   133 
   134 val _ = O.add_commands
   135  [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
   136       (ThyLoad.check_thy Path.current name; Pretty.str name))))];
   137 
   138 
   139 (* Isabelle entities (with index) *)
   140 
   141 local
   142 
   143 fun no_check _ _ = true;
   144 
   145 fun thy_check intern defined ctxt =
   146   let val thy = ProofContext.theory_of ctxt
   147   in defined thy o intern thy end;
   148 
   149 fun check_tool name =
   150   File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   151 
   152 val arg = enclose "{" "}" o clean_string;
   153 
   154 fun output_entity check markup index kind ctxt (logic, name) =
   155   let
   156     val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}";
   157     val hyper =
   158       enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   159       index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   160     val idx =
   161       (case index of
   162         NONE => ""
   163       | SOME is_def =>
   164           "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   165   in
   166     if check ctxt name then
   167       idx ^
   168       (Output.output name
   169         |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   170         |> (if ! O.quotes then quote else I)
   171         |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   172             else hyper o enclose "\\mbox{\\isa{" "}}"))
   173     else error ("Bad " ^ kind ^ " " ^ quote name)
   174   end;
   175 
   176 fun entity check markup index kind =
   177   O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   178     (K (output_entity check markup index kind));
   179 
   180 fun entity_antiqs check markup kind =
   181  [(kind, entity check markup NONE kind),
   182   (kind ^ "_def", entity check markup (SOME true) kind),
   183   (kind ^ "_ref", entity check markup (SOME false) kind)];
   184 
   185 in
   186 
   187 val _ = O.add_commands
   188  (entity_antiqs no_check "" "syntax" @
   189   entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @
   190   entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @
   191   entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @
   192   entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
   193   entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
   194   entity_antiqs no_check "" "fact" @
   195   entity_antiqs no_check "" "variable" @
   196   entity_antiqs no_check "" "case" @
   197   entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @
   198   entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @
   199   entity_antiqs no_check "" "inference" @
   200   entity_antiqs no_check "isatt" "executable" @
   201   entity_antiqs (K check_tool) "isatt" "tool" @
   202   entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @
   203   entity_antiqs (K ThyInfo.known_thy) "" "theory");
   204 
   205 end;
   206 
   207 end;