doc-src/isac/jrocnik/Inverse_Z_Transform/antiquote_setup.ML
author Jan Rocnik <jan.rocnik@student.tugraz.at>
Thu, 08 Sep 2011 23:17:35 +0200
branchdecompose-isar
changeset 42252 e633bb41ea42
permissions -rwxr-xr-x
setted up envoirement for latex includement (tuned)
jan@42252
     1
(*  Title:      doc-src/antiquote_setup.ML
jan@42252
     2
    Author:     Makarius
jan@42252
     3
jan@42252
     4
Auxiliary antiquotations for the Isabelle manuals.
jan@42252
     5
*)
jan@42252
     6
jan@42252
     7
structure Antiquote_Setup: sig end =
jan@42252
     8
struct
jan@42252
     9
jan@42252
    10
(* misc utils *)
jan@42252
    11
jan@42252
    12
fun translate f = Symbol.explode #> map f #> implode;
jan@42252
    13
jan@42252
    14
val clean_string = translate
jan@42252
    15
  (fn "_" => "\\_"
jan@42252
    16
    | "#" => "\\#"
jan@42252
    17
    | "<" => "$<$"
jan@42252
    18
    | ">" => "$>$"
jan@42252
    19
    | "{" => "\\{"
jan@42252
    20
    | "|" => "$\\mid$"
jan@42252
    21
    | "}" => "\\}"
jan@42252
    22
    | "\<dash>" => "-"
jan@42252
    23
    | c => c);
jan@42252
    24
jan@42252
    25
fun clean_name "\<dots>" = "dots"
jan@42252
    26
  | clean_name ".." = "ddot"
jan@42252
    27
  | clean_name "." = "dot"
jan@42252
    28
  | clean_name "_" = "underscore"
jan@42252
    29
  | clean_name "{" = "braceleft"
jan@42252
    30
  | clean_name "}" = "braceright"
jan@42252
    31
  | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | c => c);
jan@42252
    32
jan@42252
    33
jan@42252
    34
(* verbatim text *)
jan@42252
    35
jan@42252
    36
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
jan@42252
    37
jan@42252
    38
val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name)
jan@42252
    39
  (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
jan@42252
    40
jan@42252
    41
jan@42252
    42
(* ML text *)
jan@42252
    43
jan@42252
    44
local
jan@42252
    45
jan@42252
    46
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
jan@42252
    47
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
jan@42252
    48
jan@42252
    49
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
jan@42252
    50
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
jan@42252
    51
jan@42252
    52
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
jan@42252
    53
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
jan@42252
    54
jan@42252
    55
fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
jan@42252
    56
jan@42252
    57
fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
jan@42252
    58
jan@42252
    59
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
jan@42252
    60
jan@42252
    61
fun ml_name txt =
jan@42252
    62
  (case filter is_name (ML_Lex.tokenize txt) of
jan@42252
    63
    toks as [_] => ML_Lex.flatten toks
jan@42252
    64
  | _ => error ("Single ML name expected in input: " ^ quote txt));
jan@42252
    65
jan@42252
    66
fun index_ml name kind ml = Thy_Output.antiquotation name
jan@42252
    67
  (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
jan@42252
    68
  (fn {context = ctxt, ...} => fn (txt1, txt2) =>
jan@42252
    69
    let
jan@42252
    70
      val txt =
jan@42252
    71
        if txt2 = "" then txt1
jan@42252
    72
        else if kind = "type" then txt1 ^ " = " ^ txt2
jan@42252
    73
        else if kind = "exception" then txt1 ^ " of " ^ txt2
jan@42252
    74
        else if Syntax.is_identifier (Long_Name.base_name (ml_name txt1)) then txt1 ^ ": " ^ txt2
jan@42252
    75
        else txt1 ^ " : " ^ txt2;
jan@42252
    76
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
jan@42252
    77
      val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
jan@42252
    78
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
jan@42252
    79
    in
jan@42252
    80
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
jan@42252
    81
      (txt'
jan@42252
    82
      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
jan@42252
    83
      |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
jan@42252
    84
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
jan@42252
    85
    end);
jan@42252
    86
jan@42252
    87
in
jan@42252
    88
jan@42252
    89
val _ = index_ml "index_ML" "" ml_val;
jan@42252
    90
val _ = index_ml "index_ML_type" "type" ml_type;
jan@42252
    91
val _ = index_ml "index_ML_exn" "exception" ml_exn;
jan@42252
    92
val _ = index_ml "index_ML_structure" "structure" ml_structure;
jan@42252
    93
val _ = index_ml "index_ML_functor" "functor" ml_functor;
jan@42252
    94
jan@42252
    95
end;
jan@42252
    96
jan@42252
    97
jan@42252
    98
(* named theorems *)
jan@42252
    99
jan@42252
   100
val _ = Thy_Output.antiquotation "named_thms"
jan@42252
   101
  (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
jan@42252
   102
  (fn {context = ctxt, ...} =>
jan@42252
   103
    map (apfst (Thy_Output.pretty_thm ctxt))
jan@42252
   104
    #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
jan@42252
   105
    #> (if Config.get ctxt Thy_Output.display
jan@42252
   106
        then
jan@42252
   107
          map (fn (p, name) =>
jan@42252
   108
            Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
jan@42252
   109
            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
jan@42252
   110
          #> space_implode "\\par\\smallskip%\n"
jan@42252
   111
          #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
jan@42252
   112
        else
jan@42252
   113
          map (fn (p, name) =>
jan@42252
   114
            Output.output (Pretty.str_of p) ^
jan@42252
   115
            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
jan@42252
   116
          #> space_implode "\\par\\smallskip%\n"
jan@42252
   117
          #> enclose "\\isa{" "}"));
jan@42252
   118
jan@42252
   119
jan@42252
   120
(* theory file *)
jan@42252
   121
jan@42252
   122
val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name)
jan@42252
   123
  (fn {context = ctxt, ...} =>
jan@42252
   124
    fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
jan@42252
   125
jan@42252
   126
jan@42252
   127
(* Isabelle/Isar entities (with index) *)
jan@42252
   128
jan@42252
   129
local
jan@42252
   130
jan@42252
   131
fun no_check _ _ = true;
jan@42252
   132
jan@42252
   133
fun thy_check intern defined ctxt =
jan@42252
   134
  let val thy = ProofContext.theory_of ctxt
jan@42252
   135
  in defined thy o intern thy end;
jan@42252
   136
jan@42252
   137
fun check_tool name =
jan@42252
   138
  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
jan@42252
   139
jan@42252
   140
val arg = enclose "{" "}" o clean_string;
jan@42252
   141
jan@42252
   142
fun entity check markup kind index =
jan@42252
   143
  Thy_Output.antiquotation
jan@42252
   144
    (translate (fn " " => "_" | c => c) kind ^
jan@42252
   145
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
jan@42252
   146
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
jan@42252
   147
    (fn {context = ctxt, ...} => fn (logic, name) =>
jan@42252
   148
      let
jan@42252
   149
        val hyper_name =
jan@42252
   150
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
jan@42252
   151
        val hyper =
jan@42252
   152
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
jan@42252
   153
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
jan@42252
   154
        val idx =
jan@42252
   155
          (case index of
jan@42252
   156
            NONE => ""
jan@42252
   157
          | SOME is_def =>
jan@42252
   158
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
jan@42252
   159
      in
jan@42252
   160
        if check ctxt name then
jan@42252
   161
          idx ^
jan@42252
   162
          (Output.output name
jan@42252
   163
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
jan@42252
   164
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
jan@42252
   165
            |> (if Config.get ctxt Thy_Output.display
jan@42252
   166
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
jan@42252
   167
                else hyper o enclose "\\mbox{\\isa{" "}}"))
jan@42252
   168
        else error ("Bad " ^ kind ^ " " ^ quote name)
jan@42252
   169
      end);
jan@42252
   170
jan@42252
   171
fun entity_antiqs check markup kind =
jan@42252
   172
 ((entity check markup kind NONE);
jan@42252
   173
  (entity check markup kind (SOME true));
jan@42252
   174
  (entity check markup kind (SOME false)));
jan@42252
   175
jan@42252
   176
in
jan@42252
   177
jan@42252
   178
val _ = entity_antiqs no_check "" "syntax";
jan@42252
   179
val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command";
jan@42252
   180
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword";
jan@42252
   181
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element";
jan@42252
   182
val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
jan@42252
   183
val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
jan@42252
   184
val _ = entity_antiqs no_check "" "fact";
jan@42252
   185
val _ = entity_antiqs no_check "" "variable";
jan@42252
   186
val _ = entity_antiqs no_check "" "case";
jan@42252
   187
val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation";
jan@42252
   188
val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option";
jan@42252
   189
val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
jan@42252
   190
val _ = entity_antiqs no_check "" "inference";
jan@42252
   191
val _ = entity_antiqs no_check "isatt" "executable";
jan@42252
   192
val _ = entity_antiqs (K check_tool) "isatt" "tool";
jan@42252
   193
val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory";
jan@42252
   194
val _ = entity_antiqs no_check "" "ML antiquotation";  (* FIXME proper check *)
jan@42252
   195
jan@42252
   196
end;
jan@42252
   197
jan@42252
   198
end;