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