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