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