doc-src/antiquote_setup.ML
author wenzelm
Thu, 15 May 2008 20:02:40 +0200
changeset 26910 aa6357b39212
parent 26903 0542898ab667
child 27353 71c4dd53d4cb
permissions -rw-r--r--
tuned clean_name (underscore);
     1 (*  Title:      Doc/antiquote_setup.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Auxiliary antiquotations for the Isabelle manuals.
     6 *)
     7 
     8 structure AntiquoteSetup: sig end =
     9 struct
    10 
    11 structure O = ThyOutput;
    12 
    13 
    14 (* misc utils *)
    15 
    16 val clean_string = translate_string
    17   (fn "_" => "\\_"
    18     | "<" => "$<$"
    19     | ">" => "$>$"
    20     | "#" => "\\#"
    21     | "{" => "\\{"
    22     | "}" => "\\}"
    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_string (fn "_" => "-" | c => c);
    32 
    33 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
    34 
    35 fun tweak_line s =
    36   if ! O.display then s else Symbol.strip_blanks s;
    37 
    38 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
    39 
    40 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
    41 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
    42 
    43 
    44 (* verbatim text *)
    45 
    46 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    47 
    48 val _ = O.add_commands
    49  [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
    50       split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
    51 
    52 
    53 (* ML text *)
    54 
    55 local
    56 
    57 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    58   | ml_val (txt1, txt2) = "fn _ => (" ^ 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 DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    67 
    68 fun ml_functor _ = "val _ = ();";  (*no check!*)
    69 
    70 fun index_ml kind ml src ctxt (txt1, txt2) =
    71   let
    72     val txt = if txt2 = "" then txt1 else
    73       if kind = "type" then txt1 ^ " = " ^ txt2
    74       else if kind = "exception" then txt1 ^ " of " ^ txt2
    75       else txt1 ^ ": " ^ txt2;
    76     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    77     val _ = writeln (ml (txt1, txt2));
    78     val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
    79   in
    80     "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
    81     ((if ! O.source then str_of_source src else txt')
    82     |> (if ! O.quotes then quote else I)
    83     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    84         else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    85   end;
    86 
    87 fun output_ml ctxt src =
    88   if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    89   else
    90     split_lines
    91     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
    92     #> space_implode "\\isasep\\isanewline%\n";
    93 
    94 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    95 
    96 in
    97 
    98 val _ = O.add_commands
    99  [("index_ML", ml_args (index_ml "" ml_val)),
   100   ("index_ML_type", ml_args (index_ml "type" ml_type)),
   101   ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
   102   ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
   103   ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
   104   ("ML_functor", O.args (Scan.lift Args.name) output_ml),
   105   ("ML_text", O.args (Scan.lift Args.name) output_ml)];
   106 
   107 end;
   108 
   109 
   110 (* theorems with names *)
   111 
   112 local
   113 
   114 fun output_named_thms src ctxt xs =
   115   map (apfst (pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
   116   |> (if ! O.quotes then map (apfst Pretty.quote) else I)
   117   |> (if ! O.display then
   118     map (fn (p, name) =>
   119       Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
   120       "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
   121     #> space_implode "\\par\\smallskip%\n"
   122     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   123   else
   124     map (fn (p, name) =>
   125       Output.output (Pretty.str_of p) ^
   126       "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
   127     #> space_implode "\\par\\smallskip%\n"
   128     #> enclose "\\isa{" "}");
   129 
   130 in
   131 
   132 val _ = O.add_commands
   133  [("named_thms", O.args (Scan.repeat (Attrib.thm --
   134       Scan.lift (Args.parens Args.name))) output_named_thms)];
   135 
   136 end;
   137 
   138 
   139 (* theory files *)
   140 
   141 val _ = O.add_commands
   142  [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
   143       (ThyLoad.check_thy Path.current name; Pretty.str name))))];
   144 
   145 
   146 (* Isar entities (with index) *)
   147 
   148 local
   149 
   150 fun no_check _ _ = true;
   151 
   152 fun thy_check intern defined ctxt =
   153   let val thy = ProofContext.theory_of ctxt
   154   in defined thy o intern thy end;
   155 
   156 val arg = enclose "{" "}" o clean_string;
   157 
   158 fun output_entity check markup index kind ctxt (logic, name) =
   159   let
   160     val hyper_name = "{" ^ NameSpace.append kind (NameSpace.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 ! O.quotes then quote else I)
   175         |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   176             else hyper o enclose "\\mbox{\\isa{" "}}"))
   177     else error ("Undefined " ^ kind ^ " " ^ quote name)
   178   end;
   179 
   180 fun entity check markup index kind =
   181   O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   182     (K (output_entity check markup index kind));
   183 
   184 fun entity_antiqs check markup kind =
   185  [(kind, entity check markup NONE kind),
   186   (kind ^ "_def", entity check markup (SOME true) kind),
   187   (kind ^ "_ref", entity check markup (SOME false) kind)];
   188 
   189 in
   190 
   191 val _ = O.add_commands
   192  (entity_antiqs no_check "" "syntax" @
   193   entity_antiqs (K (is_some o OuterSyntax.command_keyword)) "isacommand" "command" @
   194   entity_antiqs (K OuterSyntax.is_keyword) "isakeyword" "keyword" @
   195   entity_antiqs (K OuterSyntax.is_keyword) "isakeyword" "element" @
   196   entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
   197   entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
   198   entity_antiqs no_check "" "fact" @
   199   entity_antiqs no_check "" "variable" @
   200   entity_antiqs no_check "" "case" @
   201   entity_antiqs (K ThyOutput.defined_command) "" "antiquotation");
   202 
   203 end;
   204 
   205 end;