| author | wenzelm | 
| Tue, 30 Mar 2010 00:47:52 +0200 | |
| changeset 36016 | 4f5c7a19ebe0 | 
| parent 35767 | 086504a943af | 
| child 36505 | 79c1d2bbe5a9 | 
| permissions | -rw-r--r-- | 
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
1  | 
(* Title: Pure/Isar/args.ML  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
2  | 
Author: Markus Wenzel, TU Muenchen  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
3  | 
|
| 35013 | 4  | 
Parsing with implicit value assignment. Concrete argument syntax of  | 
| 27811 | 5  | 
attributes, methods etc.  | 
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
6  | 
*)  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
7  | 
|
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
8  | 
signature ARGS =  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
9  | 
sig  | 
| 30513 | 10  | 
type token = OuterLex.token  | 
| 15703 | 11  | 
type src  | 
| 30513 | 12  | 
val src: (string * token list) * Position.T -> src  | 
13  | 
val dest_src: src -> (string * token list) * Position.T  | 
|
| 21030 | 14  | 
val pretty_src: Proof.context -> src -> Pretty.T  | 
| 15703 | 15  | 
val map_name: (string -> string) -> src -> src  | 
| 21480 | 16  | 
val morph_values: morphism -> src -> src  | 
| 15703 | 17  | 
val assignable: src -> src  | 
18  | 
val closure: src -> src  | 
|
| 30513 | 19  | 
val context: Proof.context context_parser  | 
20  | 
val theory: theory context_parser  | 
|
21  | 
val $$$ : string -> string parser  | 
|
22  | 
val add: string parser  | 
|
23  | 
val del: string parser  | 
|
24  | 
val colon: string parser  | 
|
25  | 
val query: string parser  | 
|
26  | 
val bang: string parser  | 
|
27  | 
val query_colon: string parser  | 
|
28  | 
val bang_colon: string parser  | 
|
29  | 
  val parens: ('a parser) -> 'a parser
 | 
|
30  | 
  val bracks: ('a parser) -> 'a parser
 | 
|
31  | 
val mode: string -> bool context_parser  | 
|
32  | 
val maybe: 'a parser -> 'a option parser  | 
|
33  | 
val name_source: string parser  | 
|
| 30573 | 34  | 
val name_source_position: (Symbol_Pos.text * Position.T) parser  | 
| 30513 | 35  | 
val name: string parser  | 
36  | 
val binding: binding parser  | 
|
37  | 
val alt_name: string parser  | 
|
38  | 
val symbol: string parser  | 
|
39  | 
val liberal_name: string parser  | 
|
40  | 
val var: indexname parser  | 
|
41  | 
val internal_text: string parser  | 
|
42  | 
val internal_typ: typ parser  | 
|
43  | 
val internal_term: term parser  | 
|
44  | 
val internal_fact: thm list parser  | 
|
45  | 
val internal_attribute: (morphism -> attribute) parser  | 
|
46  | 
val named_text: (string -> string) -> string parser  | 
|
47  | 
val named_typ: (string -> typ) -> typ parser  | 
|
48  | 
val named_term: (string -> term) -> term parser  | 
|
49  | 
val named_fact: (string -> thm list) -> thm list parser  | 
|
50  | 
val named_attribute: (string -> morphism -> attribute) -> (morphism -> attribute) parser  | 
|
51  | 
val typ_abbrev: typ context_parser  | 
|
52  | 
val typ: typ context_parser  | 
|
53  | 
val term: term context_parser  | 
|
54  | 
val term_abbrev: term context_parser  | 
|
55  | 
val prop: term context_parser  | 
|
| 
35360
 
df2b2168e43a
clarified ProofContext.read_type_name/Args.type_name wrt strict logical constructors;
 
wenzelm 
parents: 
35013 
diff
changeset
 | 
56  | 
val type_name: bool -> string context_parser  | 
| 
35399
 
3881972fcfca
clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
 
wenzelm 
parents: 
35360 
diff
changeset
 | 
57  | 
val const: bool -> string context_parser  | 
| 
 
3881972fcfca
clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
 
wenzelm 
parents: 
35360 
diff
changeset
 | 
58  | 
val const_proper: bool -> string context_parser  | 
| 30514 | 59  | 
val goal_spec: ((int -> tactic) -> tactic) context_parser  | 
| 30513 | 60  | 
val parse: token list parser  | 
61  | 
val parse1: (string -> bool) -> token list parser  | 
|
62  | 
val attribs: (string -> string) -> src list parser  | 
|
63  | 
val opt_attribs: (string -> string) -> src list parser  | 
|
64  | 
val thm_name: (string -> string) -> string -> (binding * src list) parser  | 
|
65  | 
val opt_thm_name: (string -> string) -> string -> (binding * src list) parser  | 
|
66  | 
val syntax: string -> 'a context_parser -> src -> Context.generic -> 'a * Context.generic  | 
|
67  | 
val context_syntax: string -> 'a context_parser -> src -> Proof.context -> 'a * Proof.context  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
68  | 
end;  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
69  | 
|
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
70  | 
structure Args: ARGS =  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
71  | 
struct  | 
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
72  | 
|
| 27811 | 73  | 
structure T = OuterLex;  | 
74  | 
structure P = OuterParse;  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
75  | 
|
| 30513 | 76  | 
type token = T.token;  | 
77  | 
||
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
78  | 
|
| 
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
79  | 
|
| 15703 | 80  | 
(** datatype src **)  | 
81  | 
||
| 30513 | 82  | 
datatype src = Src of (string * token list) * Position.T;  | 
| 15703 | 83  | 
|
84  | 
val src = Src;  | 
|
85  | 
fun dest_src (Src src) = src;  | 
|
86  | 
||
| 21030 | 87  | 
fun pretty_src ctxt src =  | 
88  | 
let  | 
|
| 
32091
 
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
 
wenzelm 
parents: 
30573 
diff
changeset
 | 
89  | 
val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;  | 
| 27811 | 90  | 
fun prt arg =  | 
91  | 
(case T.get_value arg of  | 
|
92  | 
SOME (T.Text s) => Pretty.str (quote s)  | 
|
93  | 
| SOME (T.Typ T) => Syntax.pretty_typ ctxt T  | 
|
94  | 
| SOME (T.Term t) => Syntax.pretty_term ctxt t  | 
|
95  | 
      | SOME (T.Fact ths) => Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
 | 
|
96  | 
| _ => Pretty.str (T.unparse arg));  | 
|
| 21030 | 97  | 
val (s, args) = #1 (dest_src src);  | 
98  | 
in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;  | 
|
99  | 
||
| 15703 | 100  | 
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);  | 
101  | 
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);  | 
|
102  | 
||
103  | 
||
104  | 
(* values *)  | 
|
105  | 
||
| 27811 | 106  | 
fun morph_values phi = map_args (T.map_value  | 
107  | 
(fn T.Text s => T.Text s  | 
|
108  | 
| T.Typ T => T.Typ (Morphism.typ phi T)  | 
|
109  | 
| T.Term t => T.Term (Morphism.term phi t)  | 
|
110  | 
| T.Fact ths => T.Fact (Morphism.fact phi ths)  | 
|
111  | 
| T.Attribute att => T.Attribute (Morphism.transform phi att)));  | 
|
| 15703 | 112  | 
|
| 27811 | 113  | 
val assignable = map_args T.assignable;  | 
114  | 
val closure = map_args T.closure;  | 
|
| 15703 | 115  | 
|
116  | 
||
117  | 
||
| 27811 | 118  | 
(** argument scanners **)  | 
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
119  | 
|
| 27371 | 120  | 
(* context *)  | 
121  | 
||
122  | 
fun context x = (Scan.state >> Context.proof_of) x;  | 
|
123  | 
fun theory x = (Scan.state >> Context.theory_of) x;  | 
|
124  | 
||
125  | 
||
| 27811 | 126  | 
(* basic *)  | 
| 5878 | 127  | 
|
| 27811 | 128  | 
fun token atom = Scan.ahead P.not_eof --| atom;  | 
| 5878 | 129  | 
|
| 27811 | 130  | 
val ident = token  | 
131  | 
(P.short_ident || P.long_ident || P.sym_ident || P.term_var ||  | 
|
132  | 
P.type_ident || P.type_var || P.number);  | 
|
| 5878 | 133  | 
|
| 27811 | 134  | 
val string = token (P.string || P.verbatim);  | 
135  | 
val alt_string = token P.alt_string;  | 
|
136  | 
val symbolic = token P.keyword_ident_or_symbolic;  | 
|
137  | 
||
138  | 
fun $$$ x = (ident >> T.content_of || P.keyword)  | 
|
139  | 
:|-- (fn y => if x = y then Scan.succeed x else Scan.fail);  | 
|
| 5878 | 140  | 
|
141  | 
||
| 27811 | 142  | 
val named = ident || string;  | 
| 5878 | 143  | 
|
| 10035 | 144  | 
val add = $$$ "add";  | 
145  | 
val del = $$$ "del";  | 
|
| 8803 | 146  | 
val colon = $$$ ":";  | 
| 10035 | 147  | 
val query = $$$ "?";  | 
148  | 
val bang = $$$ "!";  | 
|
| 20111 | 149  | 
val query_colon = $$$ "?" ^^ $$$ ":";  | 
150  | 
val bang_colon = $$$ "!" ^^ $$$ ":";  | 
|
| 10035 | 151  | 
|
| 8803 | 152  | 
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
 | 
| 10150 | 153  | 
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";  | 
| 15703 | 154  | 
fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);  | 
155  | 
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;  | 
|
| 5878 | 156  | 
|
| 
27882
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27819 
diff
changeset
 | 
157  | 
val name_source = named >> T.source_of;  | 
| 
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27819 
diff
changeset
 | 
158  | 
val name_source_position = named >> T.source_position_of;  | 
| 
 
eaa9fef9f4c1
Args.name_source(_position) for proper position information;
 
wenzelm 
parents: 
27819 
diff
changeset
 | 
159  | 
|
| 27811 | 160  | 
val name = named >> T.content_of;  | 
| 
30223
 
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
161  | 
val binding = P.position name >> Binding.make;  | 
| 27811 | 162  | 
val alt_name = alt_string >> T.content_of;  | 
163  | 
val symbol = symbolic >> T.content_of;  | 
|
| 17064 | 164  | 
val liberal_name = symbol || name;  | 
| 8233 | 165  | 
|
| 27811 | 166  | 
val var = (ident >> T.content_of) :|-- (fn x =>  | 
167  | 
(case Lexicon.read_variable x of SOME v => Scan.succeed v | NONE => Scan.fail));  | 
|
| 5878 | 168  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
169  | 
|
| 15703 | 170  | 
(* values *)  | 
171  | 
||
| 27811 | 172  | 
fun value dest = Scan.some (fn arg =>  | 
173  | 
(case T.get_value arg of SOME v => (SOME (dest v) handle Match => NONE) | NONE => NONE));  | 
|
| 15703 | 174  | 
|
175  | 
fun evaluate mk eval arg =  | 
|
| 27819 | 176  | 
let val x = eval arg in (T.assign (SOME (mk x)) arg; x) end;  | 
| 15703 | 177  | 
|
| 27811 | 178  | 
val internal_text = value (fn T.Text s => s);  | 
179  | 
val internal_typ = value (fn T.Typ T => T);  | 
|
180  | 
val internal_term = value (fn T.Term t => t);  | 
|
181  | 
val internal_fact = value (fn T.Fact ths => ths);  | 
|
182  | 
val internal_attribute = value (fn T.Attribute att => att);  | 
|
| 15703 | 183  | 
|
| 27819 | 184  | 
fun named_text intern = internal_text || named >> evaluate T.Text (intern o T.content_of);  | 
185  | 
fun named_typ readT = internal_typ || named >> evaluate T.Typ (readT o T.source_of);  | 
|
186  | 
fun named_term read = internal_term || named >> evaluate T.Term (read o T.source_of);  | 
|
187  | 
fun named_fact get = internal_fact || named >> evaluate T.Fact (get o T.content_of) ||  | 
|
188  | 
alt_string >> evaluate T.Fact (get o T.source_of);  | 
|
189  | 
fun named_attribute att = internal_attribute || named >> evaluate T.Attribute (att o T.content_of);  | 
|
| 15703 | 190  | 
|
191  | 
||
| 5878 | 192  | 
(* terms and types *)  | 
193  | 
||
| 18635 | 194  | 
val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);  | 
| 25331 | 195  | 
val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);  | 
| 
24508
 
c8b82fec6447
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
 
wenzelm 
parents: 
24244 
diff
changeset
 | 
196  | 
val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);  | 
| 21724 | 197  | 
val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);  | 
| 
24508
 
c8b82fec6447
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
 
wenzelm 
parents: 
24244 
diff
changeset
 | 
198  | 
val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);  | 
| 18635 | 199  | 
|
| 5878 | 200  | 
|
| 15703 | 201  | 
(* type and constant names *)  | 
202  | 
||
| 
35360
 
df2b2168e43a
clarified ProofContext.read_type_name/Args.type_name wrt strict logical constructors;
 
wenzelm 
parents: 
35013 
diff
changeset
 | 
203  | 
fun type_name strict =  | 
| 
 
df2b2168e43a
clarified ProofContext.read_type_name/Args.type_name wrt strict logical constructors;
 
wenzelm 
parents: 
35013 
diff
changeset
 | 
204  | 
Scan.peek (fn ctxt => named_typ (ProofContext.read_type_name (Context.proof_of ctxt) strict))  | 
| 18998 | 205  | 
>> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");  | 
| 15703 | 206  | 
|
| 
35399
 
3881972fcfca
clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
 
wenzelm 
parents: 
35360 
diff
changeset
 | 
207  | 
fun const strict =  | 
| 
 
3881972fcfca
clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
 
wenzelm 
parents: 
35360 
diff
changeset
 | 
208  | 
Scan.peek (fn ctxt => named_term (ProofContext.read_const (Context.proof_of ctxt) strict))  | 
| 18998 | 209  | 
>> (fn Const (c, _) => c | Free (x, _) => x | _ => "");  | 
| 7553 | 210  | 
|
| 
35399
 
3881972fcfca
clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
 
wenzelm 
parents: 
35360 
diff
changeset
 | 
211  | 
fun const_proper strict =  | 
| 
 
3881972fcfca
clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
 
wenzelm 
parents: 
35360 
diff
changeset
 | 
212  | 
Scan.peek (fn ctxt => named_term (ProofContext.read_const_proper (Context.proof_of ctxt) strict))  | 
| 25343 | 213  | 
>> (fn Const (c, _) => c | _ => "");  | 
214  | 
||
| 15703 | 215  | 
|
| 27811 | 216  | 
(* improper method arguments *)  | 
| 15703 | 217  | 
|
| 8536 | 218  | 
val from_to =  | 
| 27811 | 219  | 
P.nat -- ($$$ "-" |-- P.nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||  | 
220  | 
P.nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||  | 
|
221  | 
P.nat >> (fn i => fn tac => tac i) ||  | 
|
| 15703 | 222  | 
$$$ "!" >> K ALLGOALS;  | 
| 8536 | 223  | 
|
| 27811 | 224  | 
val goal = $$$ "[" |-- P.!!! (from_to --| $$$ "]");  | 
| 30514 | 225  | 
fun goal_spec x = Scan.lift (Scan.optional goal (fn tac => tac 1)) x;  | 
| 8233 | 226  | 
|
227  | 
||
| 27811 | 228  | 
(* arguments within outer syntax *)  | 
| 5878 | 229  | 
|
| 27382 | 230  | 
fun parse_args is_symid =  | 
231  | 
let  | 
|
| 27811 | 232  | 
val keyword_symid = token (P.keyword_with is_symid);  | 
233  | 
fun atom blk = P.group "argument"  | 
|
234  | 
(ident || keyword_symid || string || alt_string ||  | 
|
235  | 
(if blk then token (P.$$$ ",") else Scan.fail));  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
236  | 
|
| 27382 | 237  | 
fun args blk x = Scan.optional (args1 blk) [] x  | 
238  | 
and args1 blk x =  | 
|
239  | 
((Scan.repeat1  | 
|
240  | 
(Scan.repeat1 (atom blk) ||  | 
|
241  | 
          argsp "(" ")" ||
 | 
|
242  | 
argsp "[" "]")) >> flat) x  | 
|
| 27811 | 243  | 
and argsp l r x = (token (P.$$$ l) ::: P.!!! (args true @@@ (token (P.$$$ r) >> single))) x;  | 
| 27382 | 244  | 
in (args, args1) end;  | 
| 15703 | 245  | 
|
| 27811 | 246  | 
val parse = #1 (parse_args T.ident_or_symbolic) false;  | 
247  | 
fun parse1 is_symid = #2 (parse_args is_symid) false;  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
248  | 
|
| 27811 | 249  | 
|
250  | 
(* attributes *)  | 
|
| 27382 | 251  | 
|
| 15703 | 252  | 
fun attribs intern =  | 
253  | 
let  | 
|
| 27819 | 254  | 
val attrib_name = internal_text || (symbolic || named)  | 
255  | 
>> evaluate T.Text (intern o T.content_of);  | 
|
| 27811 | 256  | 
val attrib = P.position (attrib_name -- P.!!! parse) >> src;  | 
257  | 
in $$$ "[" |-- P.!!! (P.list attrib --| $$$ "]") end;  | 
|
| 15703 | 258  | 
|
259  | 
fun opt_attribs intern = Scan.optional (attribs intern) [];  | 
|
260  | 
||
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
261  | 
|
| 27377 | 262  | 
(* theorem specifications *)  | 
263  | 
||
| 28078 | 264  | 
fun thm_name intern s = binding -- opt_attribs intern --| $$$ s;  | 
| 27811 | 265  | 
|
| 27377 | 266  | 
fun opt_thm_name intern s =  | 
| 28078 | 267  | 
Scan.optional  | 
| 28965 | 268  | 
((binding -- opt_attribs intern || attribs intern >> pair Binding.empty) --| $$$ s)  | 
269  | 
(Binding.empty, []);  | 
|
| 27377 | 270  | 
|
271  | 
||
| 27382 | 272  | 
|
273  | 
(** syntax wrapper **)  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
274  | 
|
| 32784 | 275  | 
fun syntax kind scan (Src ((s, args), pos)) st =  | 
| 27811 | 276  | 
(case Scan.error (Scan.finite' T.stopper (Scan.option scan)) (st, args) of  | 
| 21879 | 277  | 
(SOME x, (st', [])) => (x, st')  | 
| 15703 | 278  | 
| (_, (_, args')) =>  | 
279  | 
error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n " ^  | 
|
| 27811 | 280  | 
space_implode " " (map T.unparse args')));  | 
| 15703 | 281  | 
|
| 21879 | 282  | 
fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;  | 
| 18998 | 283  | 
|
| 
5822
 
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
284  | 
end;  |