doc-src/more_antiquote.ML
author haftmann
Wed, 01 Oct 2008 08:42:42 +0200
changeset 28440 0b9ddfa6458e
child 28461 640b7f8f9cad
permissions -rw-r--r--
added more_antiquote.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     1
(*  Title:      Doc/more_antiquote.ML
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     2
    ID:         $Id$
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     4
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     5
More antiquotations.
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     6
*)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     7
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     8
signature MORE_ANTIQUOTE =
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     9
sig
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    10
  val verbatim_lines: string list -> string
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    11
end;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    12
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    13
structure More_Antiquote : MORE_ANTIQUOTE =
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    14
struct
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    15
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    16
structure O = ThyOutput;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    17
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    18
(* printing verbatim lines *)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    19
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    20
val verbatim_line = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    21
val verbatim_lines = rev
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    22
  #> dropwhile (fn s => s = "")
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    23
  #> rev
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    24
  #> map verbatim_line #> space_implode "\\newline%\n"
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    25
  #> prefix "\\isaverbatim%\n\\noindent%\n"
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    26
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    27
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    28
(* class antiquotation *)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    29
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    30
local
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    31
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    32
fun pr_class ctxt = enclose "\\isa{" "}" o Sign.extern_class (ProofContext.theory_of ctxt)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    33
  o Sign.read_class (ProofContext.theory_of ctxt);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    34
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    35
in
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    36
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    37
val _ = O.add_commands
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    38
  [("class", ThyOutput.args (Scan.lift Args.name) (K pr_class))]
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    39
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    40
end;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    41
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    42
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    43
(* code antiquotation *)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    44
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    45
local
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    46
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    47
  val parse_const_terms = Scan.repeat1 Args.term
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    48
    >> (fn ts => fn thy => map (Code_Unit.check_const thy) ts);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    49
  val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    50
    >> (fn mk_cs => fn thy => map (Code_Name.const thy) (mk_cs thy));
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    51
  val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    52
    >> (fn tycos => fn thy => map (Code_Name.tyco thy o Sign.intern_type thy) tycos);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    53
  val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    54
    >> (fn classes => fn thy => map (Code_Name.class thy o Sign.intern_class thy) classes);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    55
  val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name))
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    56
    >> (fn insts => fn thy => map (Code_Name.instance thy o apsnd (Sign.intern_type thy) o apfst (Sign.intern_class thy) o swap) insts);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    57
  val parse_names = parse_consts || parse_types || parse_classes || parse_instances; 
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    58
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    59
  fun code_stmts src ctxt ((mk_cs, mk_stmtss), target) =
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    60
    Code_Target.code_of (ProofContext.theory_of ctxt)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    61
      target "Example" (mk_cs (ProofContext.theory_of ctxt))
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    62
        (maps (fn f => f (ProofContext.theory_of ctxt)) mk_stmtss)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    63
    |> split_lines
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    64
    |> verbatim_lines;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    65
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    66
in
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    67
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    68
val _ = O.add_commands
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    69
  [("code_stmts", O.args
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    70
      (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name))
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    71
        code_stmts)]
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    72
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    73
end
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    74
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    75
end;