| author | wenzelm | 
| Wed, 31 Dec 2008 00:01:07 +0100 | |
| changeset 29262 | 3ee4656b9e0c | 
| parent 27882 | eaa9fef9f4c1 | 
| child 29606 | fedb8be05f24 | 
| permissions | -rw-r--r-- | 
| 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  | 
|
| 27379 | 10  | 
val macro: string ->  | 
11  | 
(Context.generic * Args.T list -> Proof.context * (Context.generic * Args.T list)) -> unit  | 
|
| 27340 | 12  | 
val variant: string -> Proof.context -> string * Proof.context  | 
13  | 
val inline: string ->  | 
|
14  | 
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit  | 
|
15  | 
val declaration: string -> string ->  | 
|
16  | 
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit  | 
|
17  | 
val value: string ->  | 
|
18  | 
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit  | 
|
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  | 
||
| 27379 | 44  | 
fun macro name scan = ML_Context.add_antiq name  | 
| 27868 | 45  | 
(fn _ => scan :|-- (fn ctxt => Scan.depend (fn _ => Scan.succeed  | 
| 27379 | 46  | 
    (Context.Proof ctxt, fn {background, ...} => (K ("", ""), background)))));
 | 
47  | 
||
| 27340 | 48  | 
fun inline name scan = ML_Context.add_antiq name  | 
| 27868 | 49  | 
  (fn _ => scan >> (fn s => fn {struct_name, background} => (K ("", s), background)));
 | 
| 27340 | 50  | 
|
51  | 
fun declaration kind name scan = ML_Context.add_antiq name  | 
|
| 27868 | 52  | 
  (fn _ => scan >> (fn s => fn {struct_name, background} =>
 | 
| 27340 | 53  | 
let  | 
54  | 
val (a, background') = variant name background;  | 
|
55  | 
val env = kind ^ " " ^ a ^ " = " ^ s ^ ";\n";  | 
|
56  | 
val body = struct_name ^ "." ^ a;  | 
|
57  | 
in (K (env, body), background') end));  | 
|
58  | 
||
59  | 
val value = declaration "val";  | 
|
60  | 
||
61  | 
||
62  | 
||
63  | 
(** concrete antiquotations **)  | 
|
64  | 
||
| 
27809
 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 
wenzelm 
parents: 
27390 
diff
changeset
 | 
65  | 
structure P = OuterParse;  | 
| 
 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 
wenzelm 
parents: 
27390 
diff
changeset
 | 
66  | 
|
| 
 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 
wenzelm 
parents: 
27390 
diff
changeset
 | 
67  | 
|
| 27340 | 68  | 
(* misc *)  | 
69  | 
||
70  | 
val _ = value "theory"  | 
|
71  | 
(Scan.lift Args.name >> (fn name => "ThyInfo.get_theory " ^ ML_Syntax.print_string name)  | 
|
72  | 
|| Scan.succeed "ML_Context.the_global_context ()");  | 
|
73  | 
||
74  | 
val _ = value "theory_ref"  | 
|
75  | 
(Scan.lift Args.name >> (fn name =>  | 
|
76  | 
"Theory.check_thy (ThyInfo.theory " ^ ML_Syntax.print_string name ^ ")")  | 
|
77  | 
|| Scan.succeed "Theory.check_thy (ML_Context.the_global_context ())");  | 
|
78  | 
||
79  | 
val _ = value "context" (Scan.succeed "ML_Context.the_local_context ()");  | 
|
80  | 
||
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27868 
diff
changeset
 | 
81  | 
val _ = inline "sort" (Args.context -- Scan.lift Args.name_source >> (fn (ctxt, s) =>  | 
| 27340 | 82  | 
ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))));  | 
83  | 
||
84  | 
val _ = inline "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));  | 
|
85  | 
val _ = inline "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));  | 
|
86  | 
val _ = inline "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));  | 
|
87  | 
||
| 27379 | 88  | 
val _ = macro "let" (Args.context --  | 
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27868 
diff
changeset
 | 
89  | 
Scan.lift (P.and_list1 (P.and_list1 Args.name_source -- (Args.$$$ "=" |-- Args.name_source)))  | 
| 27379 | 90  | 
>> (fn (ctxt, args) => #2 (ProofContext.match_bind true args ctxt)));  | 
91  | 
||
92  | 
val _ = macro "note" (Args.context :|-- (fn ctxt =>  | 
|
| 
27809
 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 
wenzelm 
parents: 
27390 
diff
changeset
 | 
93  | 
P.and_list1' (Scan.lift (Args.opt_thm_name I "=") -- Attrib.thms >> (fn ((a, srcs), ths) =>  | 
| 27379 | 94  | 
((a, map (Attrib.attribute (ProofContext.theory_of ctxt)) srcs), [(ths, [])])))  | 
95  | 
>> (fn args => #2 (ProofContext.note_thmss_i "" args ctxt))));  | 
|
96  | 
||
| 27340 | 97  | 
val _ = value "ctyp" (Args.typ >> (fn T =>  | 
98  | 
"Thm.ctyp_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T)));  | 
|
99  | 
||
100  | 
val _ = value "cterm" (Args.term >> (fn t =>  | 
|
101  | 
"Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));  | 
|
102  | 
||
103  | 
val _ = value "cprop" (Args.prop >> (fn t =>  | 
|
104  | 
"Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));  | 
|
105  | 
||
106  | 
val _ = value "cpat"  | 
|
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27868 
diff
changeset
 | 
107  | 
(Args.context -- Scan.lift Args.name_source >> uncurry ProofContext.read_term_pattern >> (fn t =>  | 
| 27340 | 108  | 
"Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));  | 
109  | 
||
110  | 
||
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27868 
diff
changeset
 | 
111  | 
fun type_ syn = (Args.context -- Scan.lift Args.name_source >> (fn (ctxt, c) =>  | 
| 27340 | 112  | 
#1 (Term.dest_Type (ProofContext.read_tyname ctxt c))  | 
113  | 
|> syn ? Sign.base_name  | 
|
114  | 
|> ML_Syntax.print_string));  | 
|
115  | 
||
116  | 
val _ = inline "type_name" (type_ false);  | 
|
117  | 
val _ = inline "type_syntax" (type_ true);  | 
|
118  | 
||
119  | 
||
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27868 
diff
changeset
 | 
120  | 
fun const syn = Args.context -- Scan.lift Args.name_source >> (fn (ctxt, c) =>  | 
| 27340 | 121  | 
#1 (Term.dest_Const (ProofContext.read_const_proper ctxt c))  | 
122  | 
|> syn ? ProofContext.const_syntax_name ctxt  | 
|
123  | 
|> ML_Syntax.print_string);  | 
|
124  | 
||
125  | 
val _ = inline "const_name" (const false);  | 
|
126  | 
val _ = inline "const_syntax" (const true);  | 
|
127  | 
||
128  | 
val _ = inline "const"  | 
|
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27868 
diff
changeset
 | 
129  | 
(Args.context -- Scan.lift Args.name_source -- Scan.optional  | 
| 
27809
 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 
wenzelm 
parents: 
27390 
diff
changeset
 | 
130  | 
      (Scan.lift (Args.$$$ "(") |-- OuterParse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) []
 | 
| 27340 | 131  | 
>> (fn ((ctxt, c), Ts) =>  | 
132  | 
let val (c, _) = Term.dest_Const (ProofContext.read_const_proper ctxt c)  | 
|
133  | 
in ML_Syntax.atomic (ML_Syntax.print_term (ProofContext.mk_const ctxt (c, Ts))) end));  | 
|
134  | 
||
135  | 
end;  | 
|
136  |