doc-src/more_antiquote.ML
author haftmann
Wed, 15 Sep 2010 15:11:40 +0200
changeset 39399 267235a14938
parent 39305 d4fa19eb0822
child 39478 8866d068791a
permissions -rw-r--r--
static nbe conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
     1
(*  Title:      doc-src/more_antiquote.ML
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     3
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     4
More antiquotations.
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     5
*)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     6
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     7
signature MORE_ANTIQUOTE =
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
     8
sig
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
     9
  val typewriter: string -> string
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    10
end;
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    11
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    12
structure More_Antiquote : MORE_ANTIQUOTE =
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    13
struct
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    14
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
    15
(* printing typewriter lines *)
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    16
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
    17
fun typewriter s =
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    18
  let
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    19
    val parse = Scan.repeat
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    20
      (Scan.this_string "\n" |-- Scan.succeed "\\\\\n\\hspace*{0pt}"
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
    21
        || (Scan.this_string " "
28921
e60a41c21768 consider TeX spacing conventions for punctuation marks
haftmann
parents: 28727
diff changeset
    22
          || Scan.this_string "."
e60a41c21768 consider TeX spacing conventions for punctuation marks
haftmann
parents: 28727
diff changeset
    23
          || Scan.this_string ","
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    24
          || Scan.this_string ":"
28921
e60a41c21768 consider TeX spacing conventions for punctuation marks
haftmann
parents: 28727
diff changeset
    25
          || Scan.this_string ";"
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    26
          || Scan.this_string "\"" |-- Scan.succeed "{\\char34}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    27
          || Scan.this_string "#" |-- Scan.succeed "{\\char35}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    28
          || Scan.this_string "$" |-- Scan.succeed "{\\char36}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    29
          || Scan.this_string "%" |-- Scan.succeed "{\\char37}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    30
          || Scan.this_string "&" |-- Scan.succeed "{\\char38}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    31
          || Scan.this_string "\\" |-- Scan.succeed "{\\char92}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    32
          || Scan.this_string "^" |-- Scan.succeed "{\\char94}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    33
          || Scan.this_string "_" |-- Scan.succeed "{\\char95}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    34
          || Scan.this_string "{" |-- Scan.succeed "{\\char123}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    35
          || Scan.this_string "}" |-- Scan.succeed "{\\char125}"
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    36
          || Scan.this_string "~" |-- Scan.succeed "{\\char126}")
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    37
          -- Scan.repeat (Scan.this_string " ")
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    38
          >> (fn (x, xs) => x ^ replicate_string (length xs) "~")
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    39
        || Scan.one Symbol.not_eof);
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    40
    fun is_newline s = (s = "\n");
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    41
    val cs = explode s |> take_prefix is_newline |> snd
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    42
      |> take_suffix is_newline |> fst;
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    43
    val (texts, []) =  Scan.finite Symbol.stopper parse cs
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    44
  in if null texts
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    45
    then ""
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
    46
    else implode ("\\isatypewriter%\n\\noindent%\n\\hspace*{0pt}" :: texts)
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28679
diff changeset
    47
  end
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    48
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    49
29397
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    50
(* code theorem antiquotation *)
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    51
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    52
local
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    53
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    54
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    55
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    56
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    57
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    58
fun no_vars ctxt thm =
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    59
  let
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    60
    val ctxt' = Variable.set_body false ctxt;
31794
71af1fd6a5e4 renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
wenzelm
parents: 31156
diff changeset
    61
    val ((_, [thm]), _) = Variable.import true [thm] ctxt';
29397
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    62
  in thm end;
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    63
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    64
fun pretty_code_thm src ctxt raw_const =
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    65
  let
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    66
    val thy = ProofContext.theory_of ctxt;
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31143
diff changeset
    67
    val const = Code.check_const thy raw_const;
34896
a22b09addd78 adjusted to changes in code equation administration
haftmann
parents: 34072
diff changeset
    68
    val (_, eqngr) = Code_Preproc.obtain thy [const] [];
29874
0b92f68124e8 display code theorems with HOL equality
haftmann
parents: 29619
diff changeset
    69
    fun holize thm = @{thm meta_eq_to_obj_eq} OF [thm];
34896
a22b09addd78 adjusted to changes in code equation administration
haftmann
parents: 34072
diff changeset
    70
    val thms = Code_Preproc.cert eqngr const
35246
bcbb5ba7dbbc adjusted to changes in cs b987b803616d
haftmann
parents: 34896
diff changeset
    71
      |> Code.equations_of_cert thy
34896
a22b09addd78 adjusted to changes in code equation administration
haftmann
parents: 34072
diff changeset
    72
      |> snd
35246
bcbb5ba7dbbc adjusted to changes in cs b987b803616d
haftmann
parents: 34896
diff changeset
    73
      |> map_filter (fn (_, (some_thm, proper)) => if proper then some_thm else NONE)
29874
0b92f68124e8 display code theorems with HOL equality
haftmann
parents: 29619
diff changeset
    74
      |> map (holize o no_vars ctxt o AxClass.overload thy);
38767
d8da44a8dd25 proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents: 37216
diff changeset
    75
  in Thy_Output.output ctxt (Thy_Output.maybe_pretty_source pretty_thm ctxt src thms) end;
29397
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    76
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    77
in
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    78
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36745
diff changeset
    79
val _ = Thy_Output.antiquotation "code_thms" Args.term
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
    80
  (fn {source, context, ...} => pretty_code_thm source context);
29397
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    81
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    82
end;
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    83
aab26a65e80f dded code_thm antiquotation
haftmann
parents: 28921
diff changeset
    84
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    85
(* code antiquotation *)
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    86
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    87
local
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    88
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    89
  val parse_const_terms = Scan.repeat1 Args.term
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31143
diff changeset
    90
    >> (fn ts => fn thy => map (Code.check_const thy) ts);
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    91
  val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
29619
82054da94a74 be more liberal with selected code statements
haftmann
parents: 29397
diff changeset
    92
    >> (fn mk_cs => fn thy => fn naming => map_filter (Code_Thingol.lookup_const naming) (mk_cs thy));
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    93
  val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
29619
82054da94a74 be more liberal with selected code statements
haftmann
parents: 29397
diff changeset
    94
    >> (fn tycos => fn thy => fn naming => map_filter (Code_Thingol.lookup_tyco naming o Sign.intern_type thy) tycos);
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    95
  val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
29619
82054da94a74 be more liberal with selected code statements
haftmann
parents: 29397
diff changeset
    96
    >> (fn classes => fn thy => fn naming => map_filter (Code_Thingol.lookup_class naming o Sign.intern_class thy) classes);
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
    97
  val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name))
29619
82054da94a74 be more liberal with selected code statements
haftmann
parents: 29397
diff changeset
    98
    >> (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);
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
    99
  val parse_names = parse_consts || parse_types || parse_classes || parse_instances;
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
   100
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
   101
in
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
   102
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36745
diff changeset
   103
val _ = Thy_Output.antiquotation "code_stmts"
38932
515059ca8022 allow explicit parameter for code width
haftmann
parents: 38929
diff changeset
   104
  (parse_const_terms -- Scan.repeat parse_names
515059ca8022 allow explicit parameter for code width
haftmann
parents: 38929
diff changeset
   105
    -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
515059ca8022 allow explicit parameter for code width
haftmann
parents: 38929
diff changeset
   106
  (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target, some_width)) =>
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
   107
    let val thy = ProofContext.theory_of ctxt in
38929
d9ac9dee764d distinguish code production and code presentation
haftmann
parents: 38921
diff changeset
   108
      Code_Target.present_code thy (mk_cs thy)
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
   109
        (fn naming => maps (fn f => f thy naming) mk_stmtss)
38932
515059ca8022 allow explicit parameter for code width
haftmann
parents: 38929
diff changeset
   110
        target some_width "Example" []
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
   111
      |> typewriter
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
   112
    end);
28440
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
   113
0b9ddfa6458e added more_antiquote.ML
haftmann
parents:
diff changeset
   114
end;
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
   115
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30202
diff changeset
   116
end;