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