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