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