doc-src/more_antiquote.ML
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 37216 3165bc303f66
child 39042 d8da44a8dd25
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
wenzelm@30394
     1
(*  Title:      doc-src/more_antiquote.ML
haftmann@28440
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@28440
     3
haftmann@28440
     4
More antiquotations.
haftmann@28440
     5
*)
haftmann@28440
     6
haftmann@28440
     7
signature MORE_ANTIQUOTE =
haftmann@28440
     8
sig
haftmann@28727
     9
  val typewriter: string -> string
haftmann@28440
    10
end;
haftmann@28440
    11
haftmann@28440
    12
structure More_Antiquote : MORE_ANTIQUOTE =
haftmann@28440
    13
struct
haftmann@28440
    14
haftmann@28727
    15
(* printing typewriter lines *)
haftmann@28440
    16
haftmann@28727
    17
fun typewriter s =
haftmann@28714
    18
  let
haftmann@28714
    19
    val parse = Scan.repeat
haftmann@28714
    20
      (Scan.this_string "\n" |-- Scan.succeed "\\\\\n\\hspace*{0pt}"
wenzelm@30394
    21
        || (Scan.this_string " "
haftmann@28921
    22
          || Scan.this_string "."
haftmann@28921
    23
          || Scan.this_string ","
haftmann@28714
    24
          || Scan.this_string ":"
haftmann@28921
    25
          || Scan.this_string ";"
haftmann@28714
    26
          || Scan.this_string "\"" |-- Scan.succeed "{\\char34}"
haftmann@28714
    27
          || Scan.this_string "#" |-- Scan.succeed "{\\char35}"
haftmann@28714
    28
          || Scan.this_string "$" |-- Scan.succeed "{\\char36}"
haftmann@28714
    29
          || Scan.this_string "%" |-- Scan.succeed "{\\char37}"
haftmann@28714
    30
          || Scan.this_string "&" |-- Scan.succeed "{\\char38}"
haftmann@28714
    31
          || Scan.this_string "\\" |-- Scan.succeed "{\\char92}"
haftmann@28714
    32
          || Scan.this_string "^" |-- Scan.succeed "{\\char94}"
haftmann@28714
    33
          || Scan.this_string "_" |-- Scan.succeed "{\\char95}"
haftmann@28714
    34
          || Scan.this_string "{" |-- Scan.succeed "{\\char123}"
haftmann@28714
    35
          || Scan.this_string "}" |-- Scan.succeed "{\\char125}"
haftmann@28714
    36
          || Scan.this_string "~" |-- Scan.succeed "{\\char126}")
haftmann@28714
    37
          -- Scan.repeat (Scan.this_string " ")
haftmann@28714
    38
          >> (fn (x, xs) => x ^ replicate_string (length xs) "~")
haftmann@28714
    39
        || Scan.one Symbol.not_eof);
haftmann@28714
    40
    fun is_newline s = (s = "\n");
haftmann@28714
    41
    val cs = explode s |> take_prefix is_newline |> snd
haftmann@28714
    42
      |> take_suffix is_newline |> fst;
haftmann@28714
    43
    val (texts, []) =  Scan.finite Symbol.stopper parse cs
haftmann@28714
    44
  in if null texts
haftmann@28714
    45
    then ""
haftmann@28727
    46
    else implode ("\\isatypewriter%\n\\noindent%\n\\hspace*{0pt}" :: texts)
haftmann@28714
    47
  end
haftmann@28440
    48
haftmann@28440
    49
haftmann@29394
    50
(* class and type constructor antiquotation *)
haftmann@28440
    51
haftmann@28440
    52
local
haftmann@28440
    53
wenzelm@36754
    54
val pr_text = enclose "\\isa{" "}" o Pretty.output NONE o Pretty.str;
haftmann@28634
    55
wenzelm@35786
    56
fun pr_class ctxt = ProofContext.read_class ctxt
wenzelm@35786
    57
  #> Type.extern_class (ProofContext.tsig_of ctxt)
haftmann@28634
    58
  #> pr_text;
haftmann@28634
    59
wenzelm@35786
    60
fun pr_type ctxt = ProofContext.read_type_name_proper ctxt true
wenzelm@35786
    61
  #> (#1 o Term.dest_Type)
wenzelm@35786
    62
  #> Type.extern_type (ProofContext.tsig_of ctxt)
haftmann@28634
    63
  #> pr_text;
haftmann@28440
    64
haftmann@28440
    65
in
haftmann@28440
    66
wenzelm@37216
    67
val _ = Thy_Output.antiquotation "class" (Scan.lift Args.name) (pr_class o #context);
wenzelm@37216
    68
val _ = Thy_Output.antiquotation "type" (Scan.lift Args.name) (pr_type o #context);
haftmann@28440
    69
haftmann@28440
    70
end;
haftmann@28440
    71
haftmann@28440
    72
haftmann@29394
    73
(* code theorem antiquotation *)
haftmann@29394
    74
haftmann@29394
    75
local
haftmann@29394
    76
haftmann@29394
    77
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
haftmann@29394
    78
haftmann@29394
    79
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
haftmann@29394
    80
haftmann@29394
    81
fun no_vars ctxt thm =
haftmann@29394
    82
  let
haftmann@29394
    83
    val ctxt' = Variable.set_body false ctxt;
wenzelm@31794
    84
    val ((_, [thm]), _) = Variable.import true [thm] ctxt';
haftmann@29394
    85
  in thm end;
haftmann@29394
    86
haftmann@29394
    87
fun pretty_code_thm src ctxt raw_const =
haftmann@29394
    88
  let
haftmann@29394
    89
    val thy = ProofContext.theory_of ctxt;
haftmann@31156
    90
    val const = Code.check_const thy raw_const;
haftmann@34896
    91
    val (_, eqngr) = Code_Preproc.obtain thy [const] [];
haftmann@29811
    92
    fun holize thm = @{thm meta_eq_to_obj_eq} OF [thm];
haftmann@34896
    93
    val thms = Code_Preproc.cert eqngr const
haftmann@35246
    94
      |> Code.equations_of_cert thy
haftmann@34896
    95
      |> snd
haftmann@35246
    96
      |> map_filter (fn (_, (some_thm, proper)) => if proper then some_thm else NONE)
haftmann@29811
    97
      |> map (holize o no_vars ctxt o AxClass.overload thy);
wenzelm@37216
    98
  in Thy_Output.output (Thy_Output.maybe_pretty_source (pretty_thm ctxt) src thms) end;
haftmann@29394
    99
haftmann@29394
   100
in
haftmann@29394
   101
wenzelm@37216
   102
val _ = Thy_Output.antiquotation "code_thms" Args.term
wenzelm@30394
   103
  (fn {source, context, ...} => pretty_code_thm source context);
haftmann@29394
   104
haftmann@29394
   105
end;
haftmann@29394
   106
haftmann@29394
   107
haftmann@28440
   108
(* code antiquotation *)
haftmann@28440
   109
haftmann@28440
   110
local
haftmann@28440
   111
haftmann@28440
   112
  val parse_const_terms = Scan.repeat1 Args.term
haftmann@31156
   113
    >> (fn ts => fn thy => map (Code.check_const thy) ts);
haftmann@28440
   114
  val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
haftmann@29619
   115
    >> (fn mk_cs => fn thy => fn naming => map_filter (Code_Thingol.lookup_const naming) (mk_cs thy));
haftmann@28440
   116
  val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
haftmann@29619
   117
    >> (fn tycos => fn thy => fn naming => map_filter (Code_Thingol.lookup_tyco naming o Sign.intern_type thy) tycos);
haftmann@28440
   118
  val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
haftmann@29619
   119
    >> (fn classes => fn thy => fn naming => map_filter (Code_Thingol.lookup_class naming o Sign.intern_class thy) classes);
haftmann@28440
   120
  val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@29619
   121
    >> (fn insts => fn thy => fn naming => map_filter (Code_Thingol.lookup_instance naming o apsnd (Sign.intern_type thy) o apfst (Sign.intern_class thy) o swap) insts);
wenzelm@30394
   122
  val parse_names = parse_consts || parse_types || parse_classes || parse_instances;
haftmann@28440
   123
haftmann@28440
   124
in
haftmann@28440
   125
wenzelm@37216
   126
val _ = Thy_Output.antiquotation "code_stmts"
wenzelm@30394
   127
  (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name))
wenzelm@30394
   128
  (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), target) =>
wenzelm@30394
   129
    let val thy = ProofContext.theory_of ctxt in
wenzelm@30394
   130
      Code_Target.code_of thy
haftmann@34072
   131
        target NONE "Example" (mk_cs thy)
wenzelm@30394
   132
        (fn naming => maps (fn f => f thy naming) mk_stmtss)
wenzelm@30394
   133
      |> typewriter
wenzelm@30394
   134
    end);
haftmann@28440
   135
haftmann@28440
   136
end;
wenzelm@30394
   137
wenzelm@30394
   138
end;