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