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