doc-src/antiquote_setup.ML
author wenzelm
Mon, 09 Mar 2009 11:57:48 +0100
changeset 30387 910290f04692
parent 30369 937eaec692cb
child 30394 c11a1e65a2ed
permissions -rw-r--r--
adapted ThyOutput.antiquotation;
     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 (* 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 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 _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
    67     val kind' = if kind = "" then "ML" else "ML " ^ kind;
    68   in
    69     "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
    70     (txt'
    71     |> (if ! ThyOutput.quotes then quote else I)
    72     |> (if ! ThyOutput.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 ! ThyOutput.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 = ThyOutput.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    84 
    85 in
    86 
    87 val _ = ThyOutput.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", ThyOutput.args (Scan.lift Args.name) output_ml),
    94   ("ML_text", ThyOutput.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 ! ThyOutput.quotes then map (apfst Pretty.quote) else I)
   106   |> (if ! ThyOutput.display then
   107     map (fn (p, name) =>
   108       Output.output (Pretty.string_of (Pretty.indent (! ThyOutput.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 _ = ThyOutput.add_commands
   122  [("named_thms", ThyOutput.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 _ = ThyOutput.add_commands
   131  [("thy_file", ThyOutput.args (Scan.lift Args.name) (ThyOutput.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 = "{" ^ Long_Name.append kind (Long_Name.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 ! ThyOutput.quotes then quote else I)
   167         |> (if ! ThyOutput.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   ThyOutput.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 _ = ThyOutput.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;