author | wenzelm |
Sat, 02 Sep 2000 21:51:14 +0200 | |
changeset 9805 | 10b617bdd028 |
parent 9748 | 67486cf2f8f6 |
child 9809 | 58e9d55a9f88 |
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 |
ID: $Id$ |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
8803 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
5 |
|
5878 | 6 |
Concrete argument syntax (of attributes and methods). |
5822
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 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
9 |
signature ARGS = |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
10 |
sig |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
11 |
type T |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
12 |
val val_of: T -> string |
5878 | 13 |
val pos_of: T -> Position.T |
14 |
val str_of: T -> string |
|
9748 | 15 |
val string_of: T -> string |
5878 | 16 |
val ident: string * Position.T -> T |
17 |
val string: string * Position.T -> T |
|
18 |
val keyword: string * Position.T -> T |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
19 |
val stopper: T * (T -> bool) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
20 |
val not_eof: T -> bool |
5878 | 21 |
val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b |
22 |
val !!! : (T list -> 'a) -> T list -> 'a |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
23 |
val $$$ : string -> T list -> string * T list |
8803 | 24 |
val colon: T list -> string * T list |
25 |
val parens: (T list -> 'a * T list) -> T list -> 'a * T list |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
26 |
val name: T list -> string * T list |
8687 | 27 |
val name_dummy: T list -> string option * T list |
5878 | 28 |
val nat: T list -> int * T list |
9538 | 29 |
val int: T list -> int * T list |
5878 | 30 |
val var: T list -> indexname * T list |
6447 | 31 |
val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list) |
32 |
val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list) |
|
33 |
val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list) |
|
34 |
val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list) |
|
9504 | 35 |
val global_typ_no_norm: theory * T list -> typ * (theory * T list) |
5878 | 36 |
val global_typ: theory * T list -> typ * (theory * T list) |
37 |
val global_term: theory * T list -> term * (theory * T list) |
|
38 |
val global_prop: theory * T list -> term * (theory * T list) |
|
9504 | 39 |
val local_typ_no_norm: Proof.context * T list -> typ * (Proof.context * T list) |
5878 | 40 |
val local_typ: Proof.context * T list -> typ * (Proof.context * T list) |
41 |
val local_term: Proof.context * T list -> term * (Proof.context * T list) |
|
42 |
val local_prop: Proof.context * T list -> term * (Proof.context * T list) |
|
7553 | 43 |
val bang_facts: Proof.context * T list -> thm list * (Proof.context * T list) |
8536 | 44 |
val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list) |
45 |
-> ((int -> tactic) -> tactic) * ('a * T list) |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
46 |
type src |
5878 | 47 |
val src: (string * T list) * Position.T -> src |
48 |
val dest_src: src -> (string * T list) * Position.T |
|
49 |
val attribs: T list -> src list * T list |
|
50 |
val opt_attribs: T list -> src list * T list |
|
8282 | 51 |
val syntax: string -> ('a * T list -> 'b * ('a * T list)) -> src -> 'a -> 'a * 'b |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
52 |
end; |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
53 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
54 |
structure Args: ARGS = |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
55 |
struct |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
56 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
57 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
58 |
(** datatype T **) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
59 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
60 |
datatype kind = Ident | String | Keyword | EOF; |
5878 | 61 |
datatype T = Arg of kind * (string * Position.T); |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
62 |
|
5878 | 63 |
fun val_of (Arg (_, (x, _))) = x; |
64 |
fun pos_of (Arg (_, (_, pos))) = pos; |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
65 |
|
5878 | 66 |
fun str_of (Arg (Ident, (x, _))) = enclose "'" "'" x |
67 |
| str_of (Arg (String, (x, _))) = quote x |
|
68 |
| str_of (Arg (Keyword, (x, _))) = x |
|
69 |
| str_of (Arg (EOF, _)) = "end-of-text"; |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
70 |
|
9748 | 71 |
fun string_of (Arg (Ident, (x, _))) = x |
72 |
| string_of (Arg (String, (x, _))) = quote x |
|
73 |
| string_of (Arg (Keyword, (x, _))) = x |
|
74 |
| string_of (Arg (EOF, _)) = ""; |
|
75 |
||
5878 | 76 |
fun arg kind x_pos = Arg (kind, x_pos); |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
77 |
val ident = arg Ident; |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
78 |
val string = arg String; |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
79 |
val keyword = arg Keyword; |
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 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
82 |
(* eof *) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
83 |
|
5878 | 84 |
val eof = arg EOF ("", Position.none); |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
85 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
86 |
fun is_eof (Arg (EOF, _)) = true |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
87 |
| is_eof _ = false; |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
88 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
89 |
val stopper = (eof, is_eof); |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
90 |
val not_eof = not o is_eof; |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
91 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
92 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
93 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
94 |
(** scanners **) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
95 |
|
5878 | 96 |
(* position *) |
97 |
||
98 |
fun position scan = (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap; |
|
99 |
||
100 |
||
101 |
(* cut *) |
|
102 |
||
103 |
fun !!! scan = |
|
104 |
let |
|
105 |
fun get_pos [] = " (past end-of-text!)" |
|
106 |
| get_pos (Arg (_, (_, pos)) :: _) = Position.str_of pos; |
|
107 |
||
108 |
fun err (args, None) = "Argument syntax error" ^ get_pos args |
|
109 |
| err (args, Some msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg; |
|
110 |
in Scan.!! err scan end; |
|
111 |
||
112 |
||
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
113 |
(* basic *) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
114 |
|
5878 | 115 |
fun $$$$ x = Scan.one (fn Arg (k, (y, _)) => (k = Ident orelse k = Keyword) andalso x = y); |
116 |
fun $$$ x = $$$$ x >> val_of; |
|
117 |
||
8803 | 118 |
val colon = $$$ ":"; |
119 |
fun parens scan = $$$ "(" |-- scan --| $$$ ")"; |
|
120 |
||
5878 | 121 |
val name = Scan.one (fn Arg (k, (x, _)) => k = Ident orelse k = String) >> val_of; |
8687 | 122 |
val name_dummy = $$$ "_" >> K None || name >> Some; |
5878 | 123 |
|
8233 | 124 |
val keyword_symid = |
125 |
Scan.one (fn Arg (k, (x, _)) => k = Keyword andalso OuterLex.is_sid x) >> val_of; |
|
126 |
||
5878 | 127 |
fun kind f = Scan.one (K true) :-- |
128 |
(fn Arg (Ident, (x, _)) => |
|
129 |
(case f x of Some y => Scan.succeed y | _ => Scan.fail) |
|
130 |
| _ => Scan.fail) >> #2; |
|
131 |
||
132 |
val nat = kind Syntax.read_nat; |
|
9538 | 133 |
val int = Scan.optional ($$$ "-" >> K ~1) 1 -- nat >> op *; |
5878 | 134 |
val var = kind (apsome #1 o try Term.dest_Var o Syntax.read_var); |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
135 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
136 |
|
5878 | 137 |
(* enumerations *) |
138 |
||
6447 | 139 |
fun enum1 sep scan = scan -- Scan.repeat (Scan.lift ($$$ sep) |-- scan) >> op ::; |
5878 | 140 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
141 |
||
6447 | 142 |
fun and_list1 scan = enum1 "and" scan; |
143 |
fun and_list scan = enum "and" scan; |
|
5878 | 144 |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
145 |
|
5878 | 146 |
(* terms and types *) |
147 |
||
148 |
fun gen_item read = Scan.depend (fn st => name >> (pair st o read st)); |
|
149 |
||
9504 | 150 |
val global_typ_no_norm = gen_item (ProofContext.read_typ_no_norm o ProofContext.init); |
5878 | 151 |
val global_typ = gen_item (ProofContext.read_typ o ProofContext.init); |
152 |
val global_term = gen_item (ProofContext.read_term o ProofContext.init); |
|
153 |
val global_prop = gen_item (ProofContext.read_prop o ProofContext.init); |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
154 |
|
9504 | 155 |
val local_typ_no_norm = gen_item ProofContext.read_typ_no_norm; |
5878 | 156 |
val local_typ = gen_item ProofContext.read_typ; |
157 |
val local_term = gen_item ProofContext.read_term; |
|
158 |
val local_prop = gen_item ProofContext.read_prop; |
|
159 |
||
160 |
||
7553 | 161 |
(* bang facts *) |
162 |
||
163 |
val bang_facts = Scan.depend (fn ctxt => |
|
164 |
($$$ "!" >> K (ProofContext.prems_of ctxt) || Scan.succeed []) >> pair ctxt); |
|
165 |
||
166 |
||
8536 | 167 |
(* goal specification *) |
168 |
||
169 |
(* range *) |
|
8233 | 170 |
|
8536 | 171 |
val from_to = |
172 |
nat -- ($$$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) || |
|
173 |
nat --| $$$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) || |
|
8549 | 174 |
nat >> (fn i => fn tac => tac i) || |
175 |
$$$$ "!" >> K ALLGOALS; |
|
8536 | 176 |
|
177 |
val goal = $$$$ "[" |-- !!! (from_to --| $$$$ "]"); |
|
178 |
fun goal_spec def = Scan.lift (Scan.optional goal def); |
|
8233 | 179 |
|
180 |
||
5878 | 181 |
(* args *) |
182 |
||
9126 | 183 |
val exclude = explode "()[],"; |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
184 |
|
5878 | 185 |
fun atom_arg blk = Scan.one (fn Arg (k, (x, _)) => |
186 |
k <> Keyword orelse not (x mem exclude) orelse blk andalso x = ","); |
|
187 |
||
188 |
fun paren_args l r scan = $$$$ l -- !!! (scan true -- $$$$ r) |
|
189 |
>> (fn (x, (ys, z)) => x :: ys @ [z]); |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
190 |
|
5878 | 191 |
fun args blk x = Scan.optional (args1 blk) [] x |
192 |
and args1 blk x = |
|
193 |
((Scan.repeat1 |
|
194 |
(Scan.repeat1 (atom_arg blk) || |
|
195 |
paren_args "(" ")" args || |
|
196 |
paren_args "[" "]" args)) >> flat) x; |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
197 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
198 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
199 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
200 |
(** type src **) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
201 |
|
5878 | 202 |
datatype src = Src of (string * T list) * Position.T; |
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
203 |
|
5878 | 204 |
val src = Src; |
205 |
fun dest_src (Src src) = src; |
|
206 |
||
207 |
fun err_in_src kind msg (Src ((s, args), pos)) = |
|
208 |
error (kind ^ " " ^ s ^ Position.str_of pos ^ ": " ^ msg ^ "\n " ^ |
|
209 |
space_implode " " (map str_of args)); |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
210 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
211 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
212 |
(* argument syntax *) |
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
213 |
|
8282 | 214 |
fun syntax kind scan (src as Src ((s, args), pos)) st = |
5878 | 215 |
(case handle_error (Scan.error (Scan.finite' stopper (Scan.option scan))) (st, args) of |
216 |
OK (Some x, (st', [])) => (st', x) |
|
217 |
| OK (_, (_, args')) => err_in_src kind "bad arguments" (Src ((s, args'), pos)) |
|
5911 | 218 |
| Error msg => err_in_src kind ("\n" ^ msg) src); |
5878 | 219 |
|
220 |
||
221 |
(* attribs *) |
|
222 |
||
6447 | 223 |
fun list1 scan = scan -- Scan.repeat ($$$ "," |-- scan) >> op ::; |
224 |
fun list scan = list1 scan || Scan.succeed []; |
|
225 |
||
8233 | 226 |
val attrib = position ((keyword_symid || name) -- !!! (args false)) >> src; |
5878 | 227 |
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]"); |
228 |
val opt_attribs = Scan.optional attribs []; |
|
5822
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
229 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
230 |
|
3f824514ad88
Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff
changeset
|
231 |
end; |