doc-src/antiquote_setup.ML
author huffman
Mon May 14 17:45:42 2007 +0200 (2007-05-14)
changeset 22967 0651b224528a
parent 22289 41ce4f5c97c9
child 23651 6e0b8b6012c9
permissions -rw-r--r--
added general sum-squares lemmas
     1 (*  Title:      antiquote_setup.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Auxiliary antiquotations for Isabelle manuals.
     6 *)
     7 
     8 local
     9 
    10 structure O = ThyOutput;
    11 
    12 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
    13 
    14 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    15   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    16 
    17 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    18   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    19 
    20 fun ml_exc (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
    21   | ml_exc (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
    22 
    23 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    24 
    25 fun ml_functor _ = "val _ = ();";  (*no check!*)
    26 
    27 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    28 
    29 fun index_ml kind ml src ctxt (txt1, txt2) =
    30   let
    31     val txt = if txt2 = "" then txt1 else
    32       if kind = "type"
    33         then txt1 ^ " = " ^ txt2
    34       else if kind = "exception"
    35         then txt1 ^ " of " ^ txt2
    36       else txt1 ^ ": " ^ txt2;
    37     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    38     val _ = writeln (ml (txt1, txt2));
    39     val _ = ML_Context.use_mltext (ml (txt1, txt2)) false (SOME (Context.Proof ctxt));
    40   in
    41     "\\indexml" ^ kind ^ enclose "{" "}"
    42       (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^
    43     ((if ! O.source then str_of_source src else txt')
    44     |> (if ! O.quotes then quote else I)
    45     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    46     else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    47   end;
    48 
    49 fun output_verbatim _ _ = split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n";
    50 
    51 fun arguments x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    52 
    53 fun pretty_thy_file name = (ThyLoad.check_thy Path.current name false; Pretty.str name);
    54 
    55 in
    56 
    57 val _ = O.add_commands
    58  [("index_ML", arguments (index_ml "" ml_val)),
    59   ("index_ML_type", arguments (index_ml "type" ml_type)),
    60   ("index_ML_exc", arguments (index_ml "exception" ml_exc)),
    61   ("index_ML_structure", arguments (index_ml "structure" ml_structure)),
    62   ("index_ML_functor", arguments (index_ml "functor" ml_functor)),
    63   ("ML_functor", O.args (Scan.lift Args.name) output_verbatim),
    64   ("verbatim", O.args (Scan.lift Args.name) output_verbatim),
    65   ("thy_file", O.args (Scan.lift Args.name) (O.output (K pretty_thy_file)))];
    66 
    67 end;