doc-src/more_antiquote.ML
author haftmann
Mon, 02 Mar 2009 16:58:39 +0100
changeset 30202 2775062fd3a9
parent 29811 0b92f68124e8
child 30394 c11a1e65a2ed
permissions -rw-r--r--
reduced confusion code_funcgr vs. code_wellsorted
haftmann@28440
     1
(*  Title:      Doc/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@28440
    15
structure O = ThyOutput;
haftmann@28440
    16
haftmann@28727
    17
(* printing typewriter lines *)
haftmann@28440
    18
haftmann@28727
    19
fun typewriter s =
haftmann@28714
    20
  let
haftmann@28714
    21
    val parse = Scan.repeat
haftmann@28714
    22
      (Scan.this_string "\n" |-- Scan.succeed "\\\\\n\\hspace*{0pt}"
haftmann@28714
    23
        || (Scan.this_string " " 
haftmann@28921
    24
          || Scan.this_string "."
haftmann@28921
    25
          || Scan.this_string ","
haftmann@28714
    26
          || Scan.this_string ":"
haftmann@28921
    27
          || Scan.this_string ";"
haftmann@28714
    28
          || Scan.this_string "\"" |-- Scan.succeed "{\\char34}"
haftmann@28714
    29
          || Scan.this_string "#" |-- Scan.succeed "{\\char35}"
haftmann@28714
    30
          || Scan.this_string "$" |-- Scan.succeed "{\\char36}"
haftmann@28714
    31
          || Scan.this_string "%" |-- Scan.succeed "{\\char37}"
haftmann@28714
    32
          || Scan.this_string "&" |-- Scan.succeed "{\\char38}"
haftmann@28714
    33
          || Scan.this_string "\\" |-- Scan.succeed "{\\char92}"
haftmann@28714
    34
          || Scan.this_string "^" |-- Scan.succeed "{\\char94}"
haftmann@28714
    35
          || Scan.this_string "_" |-- Scan.succeed "{\\char95}"
haftmann@28714
    36
          || Scan.this_string "{" |-- Scan.succeed "{\\char123}"
haftmann@28714
    37
          || Scan.this_string "}" |-- Scan.succeed "{\\char125}"
haftmann@28714
    38
          || Scan.this_string "~" |-- Scan.succeed "{\\char126}")
haftmann@28714
    39
          -- Scan.repeat (Scan.this_string " ")
haftmann@28714
    40
          >> (fn (x, xs) => x ^ replicate_string (length xs) "~")
haftmann@28714
    41
        || Scan.one Symbol.not_eof);
haftmann@28714
    42
    fun is_newline s = (s = "\n");
haftmann@28714
    43
    val cs = explode s |> take_prefix is_newline |> snd
haftmann@28714
    44
      |> take_suffix is_newline |> fst;
haftmann@28714
    45
    val (texts, []) =  Scan.finite Symbol.stopper parse cs
haftmann@28714
    46
  in if null texts
haftmann@28714
    47
    then ""
haftmann@28727
    48
    else implode ("\\isatypewriter%\n\\noindent%\n\\hspace*{0pt}" :: texts)
haftmann@28714
    49
  end
haftmann@28440
    50
haftmann@28440
    51
haftmann@29394
    52
(* class and type constructor antiquotation *)
haftmann@28440
    53
haftmann@28440
    54
local
haftmann@28440
    55
haftmann@28634
    56
val pr_text = enclose "\\isa{" "}" o Pretty.output o Pretty.str;
haftmann@28634
    57
haftmann@28634
    58
fun pr_class ctxt = Sign.read_class (ProofContext.theory_of ctxt)
haftmann@28634
    59
  #> Sign.extern_class (ProofContext.theory_of ctxt)
haftmann@28634
    60
  #> pr_text;
haftmann@28634
    61
haftmann@28634
    62
fun pr_type ctxt = Sign.intern_type (ProofContext.theory_of ctxt)
haftmann@28634
    63
  #> tap (Sign.arity_number (ProofContext.theory_of ctxt))
haftmann@28634
    64
  #> Sign.extern_type (ProofContext.theory_of ctxt)
haftmann@28634
    65
  #> pr_text;
haftmann@28440
    66
haftmann@28440
    67
in
haftmann@28440
    68
haftmann@28440
    69
val _ = O.add_commands
haftmann@28634
    70
  [("class", ThyOutput.args (Scan.lift Args.name) (K pr_class)),
haftmann@28634
    71
    ("type", ThyOutput.args (Scan.lift Args.name) (K pr_type))]
haftmann@28440
    72
haftmann@28440
    73
end;
haftmann@28440
    74
haftmann@28440
    75
haftmann@29394
    76
(* code theorem antiquotation *)
haftmann@29394
    77
haftmann@29394
    78
local
haftmann@29394
    79
haftmann@29394
    80
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
haftmann@29394
    81
haftmann@29394
    82
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
haftmann@29394
    83
haftmann@29394
    84
fun no_vars ctxt thm =
haftmann@29394
    85
  let
haftmann@29394
    86
    val ctxt' = Variable.set_body false ctxt;
haftmann@29394
    87
    val ((_, [thm]), _) = Variable.import_thms true [thm] ctxt';
haftmann@29394
    88
  in thm end;
haftmann@29394
    89
haftmann@29394
    90
fun pretty_code_thm src ctxt raw_const =
haftmann@29394
    91
  let
haftmann@29394
    92
    val thy = ProofContext.theory_of ctxt;
haftmann@29394
    93
    val const = Code_Unit.check_const thy raw_const;
haftmann@30202
    94
    val (_, funcgr) = Code_Wellsorted.make thy [const];
haftmann@29811
    95
    fun holize thm = @{thm meta_eq_to_obj_eq} OF [thm];
haftmann@30202
    96
    val thms = Code_Wellsorted.eqns funcgr const
haftmann@29394
    97
      |> map_filter (fn (thm, linear) => if linear then SOME thm else NONE)
haftmann@29811
    98
      |> map (holize o no_vars ctxt o AxClass.overload thy);
haftmann@29394
    99
  in ThyOutput.output_list pretty_thm src ctxt thms end;
haftmann@29394
   100
haftmann@29394
   101
in
haftmann@29394
   102
haftmann@29394
   103
val _ = O.add_commands
haftmann@29394
   104
  [("code_thms", ThyOutput.args Args.term pretty_code_thm)];
haftmann@29394
   105
haftmann@29394
   106
end;
haftmann@29394
   107
haftmann@29394
   108
haftmann@28440
   109
(* code antiquotation *)
haftmann@28440
   110
haftmann@28440
   111
local
haftmann@28440
   112
haftmann@28440
   113
  val parse_const_terms = Scan.repeat1 Args.term
haftmann@28440
   114
    >> (fn ts => fn thy => map (Code_Unit.check_const thy) ts);
haftmann@28440
   115
  val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
haftmann@29619
   116
    >> (fn mk_cs => fn thy => fn naming => map_filter (Code_Thingol.lookup_const naming) (mk_cs thy));
haftmann@28440
   117
  val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
haftmann@29619
   118
    >> (fn tycos => fn thy => fn naming => map_filter (Code_Thingol.lookup_tyco naming o Sign.intern_type thy) tycos);
haftmann@28440
   119
  val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
haftmann@29619
   120
    >> (fn classes => fn thy => fn naming => map_filter (Code_Thingol.lookup_class naming o Sign.intern_class thy) classes);
haftmann@28440
   121
  val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@29619
   122
    >> (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);
haftmann@28440
   123
  val parse_names = parse_consts || parse_types || parse_classes || parse_instances; 
haftmann@28440
   124
haftmann@28440
   125
  fun code_stmts src ctxt ((mk_cs, mk_stmtss), target) =
haftmann@28440
   126
    Code_Target.code_of (ProofContext.theory_of ctxt)
haftmann@28440
   127
      target "Example" (mk_cs (ProofContext.theory_of ctxt))
haftmann@28679
   128
        (fn naming => maps (fn f => f (ProofContext.theory_of ctxt) naming) mk_stmtss)
haftmann@28727
   129
    |> typewriter;
haftmann@28440
   130
haftmann@28440
   131
in
haftmann@28440
   132
haftmann@28440
   133
val _ = O.add_commands
haftmann@28440
   134
  [("code_stmts", O.args
haftmann@28440
   135
      (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name))
haftmann@28440
   136
        code_stmts)]
haftmann@28440
   137
haftmann@28440
   138
end
haftmann@28440
   139
haftmann@28440
   140
end;