doc-src/more_antiquote.ML
author haftmann
Fri, 17 Oct 2008 10:14:12 +0200
changeset 28634 764ef122a164
parent 28461 640b7f8f9cad
child 28679 d7384e8e99b3
permissions -rw-r--r--
added type antiquotation
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
28634
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    32
val pr_text = enclose "\\isa{" "}" o Pretty.output o Pretty.str;
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    33
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    34
fun pr_class ctxt = Sign.read_class (ProofContext.theory_of ctxt)
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    35
  #> Sign.extern_class (ProofContext.theory_of ctxt)
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    36
  #> pr_text;
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    37
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    38
fun pr_type ctxt = Sign.intern_type (ProofContext.theory_of ctxt)
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    39
  #> tap (Sign.arity_number (ProofContext.theory_of ctxt))
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    40
  #> Sign.extern_type (ProofContext.theory_of ctxt)
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    41
  #> pr_text;
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    42
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    43
in
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    44
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    45
val _ = O.add_commands
28634
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    46
  [("class", ThyOutput.args (Scan.lift Args.name) (K pr_class)),
764ef122a164 added type antiquotation
haftmann
parents: 28461
diff changeset
    47
    ("type", ThyOutput.args (Scan.lift Args.name) (K pr_type))]
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    48
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    49
end;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    50
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    51
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    52
(* code antiquotation *)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    53
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    54
local
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    55
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    56
  val parse_const_terms = Scan.repeat1 Args.term
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    57
    >> (fn ts => fn thy => map (Code_Unit.check_const thy) ts);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    58
  val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    59
    >> (fn mk_cs => fn thy => map (Code_Name.const thy) (mk_cs thy));
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    60
  val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    61
    >> (fn tycos => fn thy => map (Code_Name.tyco thy o Sign.intern_type thy) tycos);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    62
  val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    63
    >> (fn classes => fn thy => map (Code_Name.class thy o Sign.intern_class thy) classes);
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    64
  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
    65
    >> (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
    66
  val parse_names = parse_consts || parse_types || parse_classes || parse_instances; 
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    67
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    68
  fun code_stmts src ctxt ((mk_cs, mk_stmtss), target) =
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    69
    Code_Target.code_of (ProofContext.theory_of ctxt)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    70
      target "Example" (mk_cs (ProofContext.theory_of ctxt))
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    71
        (maps (fn f => f (ProofContext.theory_of ctxt)) mk_stmtss)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    72
    |> split_lines
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    73
    |> verbatim_lines;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    74
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    75
in
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    76
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    77
val _ = O.add_commands
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    78
  [("code_stmts", O.args
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    79
      (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name))
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    80
        code_stmts)]
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    81
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    82
end
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    83
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    84
end;