src/Doc/antiquote_setup.ML
author wenzelm
Sat Jun 15 21:01:07 2013 +0200 (2013-06-15)
changeset 52408 fa2dc6c6c94f
parent 50239 fb579401dc26
child 53044 be27b6be8027
permissions -rw-r--r--
updated operations on proof terms;
     1 (*  Title:      Doc/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 signature ANTIQUOTE_SETUP =
     8 sig
     9   val setup: theory -> theory
    10 end;
    11 
    12 structure Antiquote_Setup: ANTIQUOTE_SETUP =
    13 struct
    14 
    15 (* misc utils *)
    16 
    17 fun translate f = Symbol.explode #> map f #> implode;
    18 
    19 val clean_string = translate
    20   (fn "_" => "\\_"
    21     | "#" => "\\#"
    22     | "$" => "\\$"
    23     | "%" => "\\%"
    24     | "<" => "$<$"
    25     | ">" => "$>$"
    26     | "{" => "\\{"
    27     | "|" => "$\\mid$"
    28     | "}" => "\\}"
    29     | "\<hyphen>" => "-"
    30     | c => c);
    31 
    32 fun clean_name "\<dots>" = "dots"
    33   | clean_name ".." = "ddot"
    34   | clean_name "." = "dot"
    35   | clean_name "_" = "underscore"
    36   | clean_name "{" = "braceleft"
    37   | clean_name "}" = "braceright"
    38   | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
    39 
    40 
    41 (* verbatim text *)
    42 
    43 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    44 
    45 val verbatim_setup =
    46   Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
    47     (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    48 
    49 
    50 (* ML text *)
    51 
    52 local
    53 
    54 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    55   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    56 
    57 fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
    58   | ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
    59 
    60 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    61   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    62 
    63 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    64   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    65 
    66 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    67 
    68 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    69 
    70 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    71 
    72 fun ml_name txt =
    73   (case filter is_name (ML_Lex.tokenize txt) of
    74     toks as [_] => ML_Lex.flatten toks
    75   | _ => error ("Single ML name expected in input: " ^ quote txt));
    76 
    77 fun index_ml name kind ml = Thy_Output.antiquotation name
    78   (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    79   (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    80     let
    81       val txt =
    82         if txt2 = "" then txt1
    83         else if kind = "type" then txt1 ^ " = " ^ txt2
    84         else if kind = "exception" then txt1 ^ " of " ^ txt2
    85         else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
    86         then txt1 ^ ": " ^ txt2
    87         else txt1 ^ " : " ^ txt2;
    88       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    89       val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
    90       val kind' = if kind = "" then "ML" else "ML " ^ kind;
    91     in
    92       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
    93       (txt'
    94       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
    95       |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    96           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    97     end);
    98 
    99 in
   100 
   101 val index_ml_setup =
   102   index_ml @{binding index_ML} "" ml_val #>
   103   index_ml @{binding index_ML_op} "infix" ml_op #>
   104   index_ml @{binding index_ML_type} "type" ml_type #>
   105   index_ml @{binding index_ML_exn} "exception" ml_exn #>
   106   index_ml @{binding index_ML_structure} "structure" ml_structure #>
   107   index_ml @{binding index_ML_functor} "functor" ml_functor;
   108 
   109 end;
   110 
   111 
   112 (* named theorems *)
   113 
   114 val named_thms_setup =
   115   Thy_Output.antiquotation @{binding named_thms}
   116     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   117     (fn {context = ctxt, ...} =>
   118       map (apfst (Thy_Output.pretty_thm ctxt))
   119       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   120       #> (if Config.get ctxt Thy_Output.display
   121           then
   122             map (fn (p, name) =>
   123               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   124               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   125             #> space_implode "\\par\\smallskip%\n"
   126             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   127           else
   128             map (fn (p, name) =>
   129               Output.output (Pretty.str_of p) ^
   130               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   131             #> space_implode "\\par\\smallskip%\n"
   132             #> enclose "\\isa{" "}"));
   133 
   134 
   135 (* theory file *)
   136 
   137 val thy_file_setup =
   138   Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   139     (fn {context = ctxt, ...} =>
   140       fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   141 
   142 
   143 (* Isabelle/Isar entities (with index) *)
   144 
   145 local
   146 
   147 fun no_check _ _ = true;
   148 
   149 fun thy_check intern defined ctxt =
   150   let val thy = Proof_Context.theory_of ctxt
   151   in defined thy o intern thy end;
   152 
   153 fun check_tool name =
   154   let val tool_dirs = map Path.explode ["~~/lib/Tools", "~~/src/Tools/jEdit/lib/Tools"]
   155   in exists (fn dir => File.exists (Path.append dir (Path.basic name))) tool_dirs end;
   156 
   157 val arg = enclose "{" "}" o clean_string;
   158 
   159 fun entity check markup kind index =
   160   Thy_Output.antiquotation
   161     (Binding.name (translate (fn " " => "_" | c => c) kind ^
   162       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   163     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   164     (fn {context = ctxt, ...} => fn (logic, name) =>
   165       let
   166         val hyper_name =
   167           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   168         val hyper =
   169           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   170           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   171         val idx =
   172           (case index of
   173             NONE => ""
   174           | SOME is_def =>
   175               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   176       in
   177         if check ctxt name then
   178           idx ^
   179           (Output.output name
   180             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   181             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   182             |> (if Config.get ctxt Thy_Output.display
   183                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   184                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   185         else error ("Bad " ^ kind ^ " " ^ quote name)
   186       end);
   187 
   188 fun entity_antiqs check markup kind =
   189   entity check markup kind NONE #>
   190   entity check markup kind (SOME true) #>
   191   entity check markup kind (SOME false);
   192 
   193 in
   194 
   195 val entity_setup =
   196   entity_antiqs no_check "" "syntax" #>
   197   entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command" #>
   198   entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword" #>
   199   entity_antiqs (K Keyword.is_keyword) "isakeyword" "element" #>
   200   entity_antiqs (thy_check Method.intern Method.defined) "" "method" #>
   201   entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" #>
   202   entity_antiqs no_check "" "fact" #>
   203   entity_antiqs no_check "" "variable" #>
   204   entity_antiqs no_check "" "case" #>
   205   entity_antiqs (thy_check Thy_Output.intern_command Thy_Output.defined_command)
   206     "" "antiquotation" #>
   207   entity_antiqs (thy_check Thy_Output.intern_option Thy_Output.defined_option)
   208     "" "antiquotation option" #>
   209   entity_antiqs no_check "isatt" "setting" #>
   210   entity_antiqs no_check "isatt" "system option" #>
   211   entity_antiqs no_check "" "inference" #>
   212   entity_antiqs no_check "isatt" "executable" #>
   213   entity_antiqs (K check_tool) "isatool" "tool" #>
   214   entity_antiqs (thy_check ML_Context.intern_antiq ML_Context.defined_antiq)
   215     "" Markup.ML_antiquotationN;
   216 
   217 end;
   218 
   219 
   220 (* theory setup *)
   221 
   222 val setup =
   223   verbatim_setup #>
   224   index_ml_setup #>
   225   named_thms_setup #>
   226   thy_file_setup #>
   227   entity_setup;
   228 
   229 end;