27340
|
1 |
(* Title: Pure/ML/ml_antiquote.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Common ML antiquotations.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature ML_ANTIQUOTE =
|
|
9 |
sig
|
|
10 |
val variant: string -> Proof.context -> string * Proof.context
|
|
11 |
val inline: string ->
|
|
12 |
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
|
|
13 |
val declaration: string -> string ->
|
|
14 |
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
|
|
15 |
val value: string ->
|
|
16 |
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
|
|
17 |
val the_thms: Proof.context -> int -> thm list
|
|
18 |
val the_thm: Proof.context -> int -> thm
|
|
19 |
end;
|
|
20 |
|
|
21 |
structure ML_Antiquote: ML_ANTIQUOTE =
|
|
22 |
struct
|
|
23 |
|
|
24 |
(** generic tools **)
|
|
25 |
|
|
26 |
(* ML names *)
|
|
27 |
|
|
28 |
structure NamesData = ProofDataFun
|
|
29 |
(
|
|
30 |
type T = Name.context;
|
|
31 |
fun init _ = ML_Syntax.reserved;
|
|
32 |
);
|
|
33 |
|
|
34 |
fun variant a ctxt =
|
|
35 |
let
|
|
36 |
val names = NamesData.get ctxt;
|
|
37 |
val ([b], names') = Name.variants [a] names;
|
|
38 |
val ctxt' = NamesData.put names' ctxt;
|
|
39 |
in (b, ctxt') end;
|
|
40 |
|
|
41 |
|
|
42 |
(* specific antiquotations *)
|
|
43 |
|
|
44 |
fun inline name scan = ML_Context.add_antiq name
|
|
45 |
(scan >> (fn s => fn {struct_name, background} => ((K ("", s)), background)));
|
|
46 |
|
|
47 |
fun declaration kind name scan = ML_Context.add_antiq name
|
|
48 |
(scan >> (fn s => fn {struct_name, background} =>
|
|
49 |
let
|
|
50 |
val (a, background') = variant name background;
|
|
51 |
val env = kind ^ " " ^ a ^ " = " ^ s ^ ";\n";
|
|
52 |
val body = struct_name ^ "." ^ a;
|
|
53 |
in (K (env, body), background') end));
|
|
54 |
|
|
55 |
val value = declaration "val";
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
(** concrete antiquotations **)
|
|
60 |
|
|
61 |
fun context x = (Scan.state >> Context.proof_of) x;
|
|
62 |
|
|
63 |
|
|
64 |
(* misc *)
|
|
65 |
|
|
66 |
val _ = value "theory"
|
|
67 |
(Scan.lift Args.name >> (fn name => "ThyInfo.get_theory " ^ ML_Syntax.print_string name)
|
|
68 |
|| Scan.succeed "ML_Context.the_global_context ()");
|
|
69 |
|
|
70 |
val _ = value "theory_ref"
|
|
71 |
(Scan.lift Args.name >> (fn name =>
|
|
72 |
"Theory.check_thy (ThyInfo.theory " ^ ML_Syntax.print_string name ^ ")")
|
|
73 |
|| Scan.succeed "Theory.check_thy (ML_Context.the_global_context ())");
|
|
74 |
|
|
75 |
val _ = value "context" (Scan.succeed "ML_Context.the_local_context ()");
|
|
76 |
|
|
77 |
val _ = inline "sort" (context -- Scan.lift Args.name >> (fn (ctxt, s) =>
|
|
78 |
ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))));
|
|
79 |
|
|
80 |
val _ = inline "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));
|
|
81 |
val _ = inline "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));
|
|
82 |
val _ = inline "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));
|
|
83 |
|
|
84 |
val _ = value "ctyp" (Args.typ >> (fn T =>
|
|
85 |
"Thm.ctyp_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T)));
|
|
86 |
|
|
87 |
val _ = value "cterm" (Args.term >> (fn t =>
|
|
88 |
"Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));
|
|
89 |
|
|
90 |
val _ = value "cprop" (Args.prop >> (fn t =>
|
|
91 |
"Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));
|
|
92 |
|
|
93 |
val _ = value "cpat"
|
|
94 |
(context -- Scan.lift Args.name >> uncurry ProofContext.read_term_pattern >> (fn t =>
|
|
95 |
"Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));
|
|
96 |
|
|
97 |
|
|
98 |
fun type_ syn = (context -- Scan.lift Args.name >> (fn (ctxt, c) =>
|
|
99 |
#1 (Term.dest_Type (ProofContext.read_tyname ctxt c))
|
|
100 |
|> syn ? Sign.base_name
|
|
101 |
|> ML_Syntax.print_string));
|
|
102 |
|
|
103 |
val _ = inline "type_name" (type_ false);
|
|
104 |
val _ = inline "type_syntax" (type_ true);
|
|
105 |
|
|
106 |
|
|
107 |
fun const syn = context -- Scan.lift Args.name >> (fn (ctxt, c) =>
|
|
108 |
#1 (Term.dest_Const (ProofContext.read_const_proper ctxt c))
|
|
109 |
|> syn ? ProofContext.const_syntax_name ctxt
|
|
110 |
|> ML_Syntax.print_string);
|
|
111 |
|
|
112 |
val _ = inline "const_name" (const false);
|
|
113 |
val _ = inline "const_syntax" (const true);
|
|
114 |
|
|
115 |
val _ = inline "const"
|
|
116 |
(context -- Scan.lift Args.name --
|
|
117 |
Scan.optional (Scan.lift (Args.$$$ "(") |-- Args.enum1 "," Args.typ --| Scan.lift (Args.$$$ ")")) []
|
|
118 |
>> (fn ((ctxt, c), Ts) =>
|
|
119 |
let val (c, _) = Term.dest_Const (ProofContext.read_const_proper ctxt c)
|
|
120 |
in ML_Syntax.atomic (ML_Syntax.print_term (ProofContext.mk_const ctxt (c, Ts))) end));
|
|
121 |
|
|
122 |
|
|
123 |
(* abstract fact values *)
|
|
124 |
|
|
125 |
structure AuxFacts = ProofDataFun
|
|
126 |
(
|
|
127 |
type T = thm list Inttab.table;
|
|
128 |
fun init _ = Inttab.empty;
|
|
129 |
);
|
|
130 |
|
|
131 |
fun the_thms ctxt name = the (Inttab.lookup (AuxFacts.get ctxt) name);
|
|
132 |
fun the_thm ctxt name = the_single (the_thms ctxt name);
|
|
133 |
|
|
134 |
fun put_thms ths ctxt =
|
|
135 |
let val i = serial ()
|
|
136 |
in (i, AuxFacts.map (Inttab.update (i, ths)) ctxt) end;
|
|
137 |
|
|
138 |
|
|
139 |
fun thm_antiq kind scan = ML_Context.add_antiq kind
|
|
140 |
(scan >> (fn ths => fn {struct_name, background} =>
|
|
141 |
let
|
|
142 |
val ((a, i), background') = background |> variant kind ||>> put_thms ths;
|
|
143 |
val env = "val " ^ a ^ " = ML_Antiquote.the_" ^ kind ^
|
|
144 |
" (ML_Context.the_local_context ()) " ^ string_of_int i ^ ";\n";
|
|
145 |
val body = struct_name ^ "." ^ a;
|
|
146 |
in (K (env, body), background') end));
|
|
147 |
|
|
148 |
val _ = thm_antiq "thm" (Attrib.thm >> single);
|
|
149 |
val _ = thm_antiq "thms" Attrib.thms;
|
|
150 |
|
|
151 |
end;
|
|
152 |
|