doc-src/more_antiquote.ML
author haftmann
Fri Oct 24 10:41:13 2008 +0200 (2008-10-24)
changeset 28679 d7384e8e99b3
parent 28634 764ef122a164
child 28714 1992553cccfe
permissions -rw-r--r--
explicit namings for generated code
     1 (*  Title:      Doc/more_antiquote.ML
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 More antiquotations.
     6 *)
     7 
     8 signature MORE_ANTIQUOTE =
     9 sig
    10   val verbatim_lines: string list -> string
    11 end;
    12 
    13 structure More_Antiquote : MORE_ANTIQUOTE =
    14 struct
    15 
    16 structure O = ThyOutput;
    17 
    18 (* printing verbatim lines *)
    19 
    20 val verbatim_line = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    21 val verbatim_lines = rev
    22   #> dropwhile (fn s => s = "")
    23   #> rev
    24   #> map verbatim_line #> space_implode "\\newline%\n"
    25   #> prefix "\\isaverbatim%\n\\noindent%\n"
    26 
    27 
    28 (* class antiquotation *)
    29 
    30 local
    31 
    32 val pr_text = enclose "\\isa{" "}" o Pretty.output o Pretty.str;
    33 
    34 fun pr_class ctxt = Sign.read_class (ProofContext.theory_of ctxt)
    35   #> Sign.extern_class (ProofContext.theory_of ctxt)
    36   #> pr_text;
    37 
    38 fun pr_type ctxt = Sign.intern_type (ProofContext.theory_of ctxt)
    39   #> tap (Sign.arity_number (ProofContext.theory_of ctxt))
    40   #> Sign.extern_type (ProofContext.theory_of ctxt)
    41   #> pr_text;
    42 
    43 in
    44 
    45 val _ = O.add_commands
    46   [("class", ThyOutput.args (Scan.lift Args.name) (K pr_class)),
    47     ("type", ThyOutput.args (Scan.lift Args.name) (K pr_type))]
    48 
    49 end;
    50 
    51 
    52 (* code antiquotation *)
    53 
    54 local
    55 
    56   val parse_const_terms = Scan.repeat1 Args.term
    57     >> (fn ts => fn thy => map (Code_Unit.check_const thy) ts);
    58   val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
    59     >> (fn mk_cs => fn thy => fn naming => map (the o Code_Thingol.lookup_const naming) (mk_cs thy));
    60   val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
    61     >> (fn tycos => fn thy => fn naming => map (the o Code_Thingol.lookup_tyco naming o Sign.intern_type thy) tycos);
    62   val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
    63     >> (fn classes => fn thy => fn naming => map (the o Code_Thingol.lookup_class naming o Sign.intern_class thy) classes);
    64   val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name))
    65     >> (fn insts => fn thy => fn naming => map (the o Code_Thingol.lookup_instance naming o apsnd (Sign.intern_type thy) o apfst (Sign.intern_class thy) o swap) insts);
    66   val parse_names = parse_consts || parse_types || parse_classes || parse_instances; 
    67 
    68   fun code_stmts src ctxt ((mk_cs, mk_stmtss), target) =
    69     Code_Target.code_of (ProofContext.theory_of ctxt)
    70       target "Example" (mk_cs (ProofContext.theory_of ctxt))
    71         (fn naming => maps (fn f => f (ProofContext.theory_of ctxt) naming) mk_stmtss)
    72     |> split_lines
    73     |> verbatim_lines;
    74 
    75 in
    76 
    77 val _ = O.add_commands
    78   [("code_stmts", O.args
    79       (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name))
    80         code_stmts)]
    81 
    82 end
    83 
    84 end;