author | wenzelm |
Mon, 25 Oct 2021 19:52:12 +0200 | |
changeset 74580 | d114553793df |
parent 74579 | bf703bfc065c |
child 74581 | e5b38bb5a147 |
permissions | -rw-r--r-- |
74559 | 1 |
(* Title: Pure/ML/ml_antiquotations.ML |
56205 | 2 |
Author: Makarius |
3 |
||
74559 | 4 |
Miscellaneous ML antiquotations. |
56205 | 5 |
*) |
6 |
||
74559 | 7 |
signature ML_ANTIQUOTATIONS = |
74341 | 8 |
sig |
9 |
val make_judgment: Proof.context -> term -> term |
|
10 |
val dest_judgment: Proof.context -> term -> term |
|
74573
e2e2bc1f9570
avoid persistence of static context: instantiation arguments should provide proper dynamic context;
wenzelm
parents:
74571
diff
changeset
|
11 |
val make_ctyp: Proof.context -> typ -> ctyp |
e2e2bc1f9570
avoid persistence of static context: instantiation arguments should provide proper dynamic context;
wenzelm
parents:
74571
diff
changeset
|
12 |
val make_cterm: Proof.context -> term -> cterm |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
13 |
type insts = ((indexname * sort) * typ) list * ((indexname * typ) * term) list |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
14 |
type cinsts = ((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
15 |
val instantiate_typ: insts -> typ -> typ |
74579 | 16 |
val instantiate_ctyp: Position.T -> cinsts -> ctyp -> ctyp |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
17 |
val instantiate_term: insts -> term -> term |
74579 | 18 |
val instantiate_cterm: Position.T -> cinsts -> cterm -> cterm |
74341 | 19 |
end; |
20 |
||
74559 | 21 |
structure ML_Antiquotations: ML_ANTIQUOTATIONS = |
56205 | 22 |
struct |
23 |
||
58632 | 24 |
(* ML support *) |
25 |
||
26 |
val _ = Theory.setup |
|
67147 | 27 |
(ML_Antiquotation.inline \<^binding>\<open>undefined\<close> |
61597
53e32a9b66b8
added @{undefined} with somewhat undefined symbol;
wenzelm
parents:
61596
diff
changeset
|
28 |
(Scan.succeed "(raise General.Match)") #> |
53e32a9b66b8
added @{undefined} with somewhat undefined symbol;
wenzelm
parents:
61596
diff
changeset
|
29 |
|
67147 | 30 |
ML_Antiquotation.inline \<^binding>\<open>assert\<close> |
58632 | 31 |
(Scan.succeed "(fn b => if b then () else raise General.Fail \"Assertion failed\")") #> |
32 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
33 |
ML_Antiquotation.declaration_embedded \<^binding>\<open>print\<close> |
74563 | 34 |
(Scan.lift (Scan.optional Parse.embedded "Output.writeln")) |
58632 | 35 |
(fn src => fn output => fn ctxt => |
36 |
let |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
37 |
val struct_name = ML_Context.struct_name ctxt; |
58632 | 38 |
val (_, pos) = Token.name_of_src src; |
59112 | 39 |
val (a, ctxt') = ML_Context.variant "output" ctxt; |
58632 | 40 |
val env = |
41 |
"val " ^ a ^ ": string -> unit =\n\ |
|
42 |
\ (" ^ output ^ ") o (fn s => s ^ Position.here (" ^ |
|
43 |
ML_Syntax.print_position pos ^ "));\n"; |
|
44 |
val body = |
|
62900
c641bf9402fd
simplified default print_depth: context is usually available, in contrast to 0d295e339f52;
wenzelm
parents:
62899
diff
changeset
|
45 |
"(fn x => (" ^ struct_name ^ "." ^ a ^ " (" ^ ML_Pretty.make_string_fn ^ " x); x))"; |
63204
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
46 |
in (K (env, body), ctxt') end) #> |
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
47 |
|
67147 | 48 |
ML_Antiquotation.value \<^binding>\<open>rat\<close> |
63204
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
49 |
(Scan.lift (Scan.optional (Args.$$$ "~" >> K ~1) 1 -- Parse.nat -- |
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
50 |
Scan.optional (Args.$$$ "/" |-- Parse.nat) 1) >> (fn ((sign, a), b) => |
73586 | 51 |
"Rat.make " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int (sign * a, b))) #> |
52 |
||
53 |
ML_Antiquotation.conditional \<^binding>\<open>if_linux\<close> (fn _ => ML_System.platform_is_linux) #> |
|
54 |
ML_Antiquotation.conditional \<^binding>\<open>if_macos\<close> (fn _ => ML_System.platform_is_macos) #> |
|
55 |
ML_Antiquotation.conditional \<^binding>\<open>if_windows\<close> (fn _ => ML_System.platform_is_windows) #> |
|
56 |
ML_Antiquotation.conditional \<^binding>\<open>if_unix\<close> (fn _ => ML_System.platform_is_unix)); |
|
58632 | 57 |
|
58 |
||
59 |
(* formal entities *) |
|
60 |
||
56205 | 61 |
val _ = Theory.setup |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
62 |
(ML_Antiquotation.value_embedded \<^binding>\<open>system_option\<close> |
74563 | 63 |
(Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) => |
67208 | 64 |
(Completion.check_option (Options.default ()) ctxt (name, pos) |> ML_Syntax.print_string))) #> |
56205 | 65 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
66 |
ML_Antiquotation.value_embedded \<^binding>\<open>theory\<close> |
74563 | 67 |
(Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) => |
68482 | 68 |
(Theory.check {long = false} ctxt (name, pos); |
69 |
"Context.get_theory {long = false} (Proof_Context.theory_of ML_context) " ^ |
|
70 |
ML_Syntax.print_string name)) |
|
56205 | 71 |
|| Scan.succeed "Proof_Context.theory_of ML_context") #> |
72 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
73 |
ML_Antiquotation.value_embedded \<^binding>\<open>theory_context\<close> |
74563 | 74 |
(Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) => |
68482 | 75 |
(Theory.check {long = false} ctxt (name, pos); |
56205 | 76 |
"Proof_Context.get_global (Proof_Context.theory_of ML_context) " ^ |
77 |
ML_Syntax.print_string name))) #> |
|
78 |
||
67147 | 79 |
ML_Antiquotation.inline \<^binding>\<open>context\<close> |
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
80 |
(Args.context >> (fn ctxt => ML_Context.struct_name ctxt ^ ".ML_context")) #> |
56205 | 81 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
82 |
ML_Antiquotation.inline_embedded \<^binding>\<open>typ\<close> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
83 |
(Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ)) #> |
56205 | 84 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
85 |
ML_Antiquotation.inline_embedded \<^binding>\<open>term\<close> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
86 |
(Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term)) #> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
87 |
|
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
88 |
ML_Antiquotation.inline_embedded \<^binding>\<open>prop\<close> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
89 |
(Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term)) #> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
90 |
|
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
91 |
ML_Antiquotation.value_embedded \<^binding>\<open>ctyp\<close> (Args.typ >> (fn T => |
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59573
diff
changeset
|
92 |
"Thm.ctyp_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))) #> |
56205 | 93 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
94 |
ML_Antiquotation.value_embedded \<^binding>\<open>cterm\<close> (Args.term >> (fn t => |
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59573
diff
changeset
|
95 |
"Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #> |
56205 | 96 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
97 |
ML_Antiquotation.value_embedded \<^binding>\<open>cprop\<close> (Args.prop >> (fn t => |
62075 | 98 |
"Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #> |
99 |
||
70565 | 100 |
ML_Antiquotation.inline_embedded \<^binding>\<open>oracle_name\<close> |
74563 | 101 |
(Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) => |
73613 | 102 |
ML_Syntax.print_string (Thm.check_oracle ctxt (name, pos))))); |
63553
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
103 |
|
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
104 |
|
74290 | 105 |
(* schematic variables *) |
106 |
||
107 |
val schematic_input = |
|
74563 | 108 |
Args.context -- Scan.lift Parse.embedded_input >> (fn (ctxt, src) => |
74290 | 109 |
(Proof_Context.set_mode Proof_Context.mode_schematic ctxt, |
110 |
(Syntax.implode_input src, Input.pos_of src))); |
|
111 |
||
112 |
val _ = Theory.setup |
|
113 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>tvar\<close> |
|
114 |
(schematic_input >> (fn (ctxt, (s, pos)) => |
|
115 |
(case Syntax.read_typ ctxt s of |
|
116 |
TVar v => ML_Syntax.print_pair ML_Syntax.print_indexname ML_Syntax.print_sort v |
|
117 |
| _ => error ("Schematic type variable expected" ^ Position.here pos)))) #> |
|
118 |
ML_Antiquotation.inline_embedded \<^binding>\<open>var\<close> |
|
119 |
(schematic_input >> (fn (ctxt, (s, pos)) => |
|
120 |
(case Syntax.read_term ctxt s of |
|
121 |
Var v => ML_Syntax.print_pair ML_Syntax.print_indexname ML_Syntax.print_typ v |
|
122 |
| _ => error ("Schematic variable expected" ^ Position.here pos))))); |
|
123 |
||
124 |
||
56205 | 125 |
(* type classes *) |
126 |
||
74563 | 127 |
fun class syn = Args.context -- Scan.lift Parse.embedded_inner_syntax >> (fn (ctxt, s) => |
56205 | 128 |
Proof_Context.read_class ctxt s |
129 |
|> syn ? Lexicon.mark_class |
|
130 |
|> ML_Syntax.print_string); |
|
131 |
||
132 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
133 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>class\<close> (class false) #> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
134 |
ML_Antiquotation.inline_embedded \<^binding>\<open>class_syntax\<close> (class true) #> |
56205 | 135 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
136 |
ML_Antiquotation.inline_embedded \<^binding>\<open>sort\<close> |
74563 | 137 |
(Args.context -- Scan.lift Parse.embedded_inner_syntax >> (fn (ctxt, s) => |
56205 | 138 |
ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))))); |
139 |
||
140 |
||
141 |
(* type constructors *) |
|
142 |
||
74563 | 143 |
fun type_name kind check = Args.context -- Scan.lift (Parse.token Parse.embedded) |
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
144 |
>> (fn (ctxt, tok) => |
56205 | 145 |
let |
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
146 |
val s = Token.inner_syntax_of tok; |
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
147 |
val (_, pos) = Input.source_content (Token.input_of tok); |
56205 | 148 |
val Type (c, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s; |
149 |
val decl = Type.the_decl (Proof_Context.tsig_of ctxt) (c, pos); |
|
150 |
val res = |
|
151 |
(case try check (c, decl) of |
|
152 |
SOME res => res |
|
153 |
| NONE => error ("Not a " ^ kind ^ ": " ^ quote c ^ Position.here pos)); |
|
154 |
in ML_Syntax.print_string res end); |
|
155 |
||
156 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
157 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>type_name\<close> |
56205 | 158 |
(type_name "logical type" (fn (c, Type.LogicalType _) => c)) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
159 |
ML_Antiquotation.inline_embedded \<^binding>\<open>type_abbrev\<close> |
56205 | 160 |
(type_name "type abbreviation" (fn (c, Type.Abbreviation _) => c)) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
161 |
ML_Antiquotation.inline_embedded \<^binding>\<open>nonterminal\<close> |
56205 | 162 |
(type_name "nonterminal" (fn (c, Type.Nonterminal) => c)) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
163 |
ML_Antiquotation.inline_embedded \<^binding>\<open>type_syntax\<close> |
56205 | 164 |
(type_name "type" (fn (c, _) => Lexicon.mark_type c))); |
165 |
||
166 |
||
167 |
(* constants *) |
|
168 |
||
74563 | 169 |
fun const_name check = Args.context -- Scan.lift (Parse.token Parse.embedded) |
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
170 |
>> (fn (ctxt, tok) => |
56205 | 171 |
let |
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
172 |
val s = Token.inner_syntax_of tok; |
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
173 |
val (_, pos) = Input.source_content (Token.input_of tok); |
56205 | 174 |
val Const (c, _) = Proof_Context.read_const {proper = true, strict = false} ctxt s; |
175 |
val res = check (Proof_Context.consts_of ctxt, c) |
|
176 |
handle TYPE (msg, _, _) => error (msg ^ Position.here pos); |
|
177 |
in ML_Syntax.print_string res end); |
|
178 |
||
179 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
180 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>const_name\<close> |
56205 | 181 |
(const_name (fn (consts, c) => (Consts.the_const consts c; c))) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
182 |
ML_Antiquotation.inline_embedded \<^binding>\<open>const_abbrev\<close> |
56205 | 183 |
(const_name (fn (consts, c) => (Consts.the_abbreviation consts c; c))) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
184 |
ML_Antiquotation.inline_embedded \<^binding>\<open>const_syntax\<close> |
56205 | 185 |
(const_name (fn (_, c) => Lexicon.mark_const c)) #> |
186 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
187 |
ML_Antiquotation.inline_embedded \<^binding>\<open>syntax_const\<close> |
74563 | 188 |
(Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, arg) => |
74331 | 189 |
ML_Syntax.print_string (Proof_Context.check_syntax_const ctxt arg))) #> |
56205 | 190 |
|
69595 | 191 |
ML_Antiquotation.inline_embedded \<^binding>\<open>const\<close> |
74563 | 192 |
(Args.context -- Scan.lift (Parse.position Parse.embedded_inner_syntax) -- Scan.optional |
56205 | 193 |
(Scan.lift (Args.$$$ "(") |-- Parse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) [] |
56251 | 194 |
>> (fn ((ctxt, (raw_c, pos)), Ts) => |
56205 | 195 |
let |
196 |
val Const (c, _) = |
|
197 |
Proof_Context.read_const {proper = true, strict = true} ctxt raw_c; |
|
198 |
val consts = Proof_Context.consts_of ctxt; |
|
199 |
val n = length (Consts.typargs consts (c, Consts.type_scheme consts c)); |
|
200 |
val _ = length Ts <> n andalso |
|
201 |
error ("Constant requires " ^ string_of_int n ^ " type argument(s): " ^ |
|
56251 | 202 |
quote c ^ enclose "(" ")" (commas (replicate n "_")) ^ Position.here pos); |
56205 | 203 |
val const = Const (c, Consts.instance consts (c, Ts)); |
204 |
in ML_Syntax.atomic (ML_Syntax.print_term const) end))); |
|
205 |
||
206 |
||
74341 | 207 |
(* object-logic judgment *) |
208 |
||
74344
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
209 |
fun make_judgment ctxt = |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
210 |
let val const = Object_Logic.judgment_const ctxt |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
211 |
in fn t => Const const $ t end; |
74341 | 212 |
|
74344
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
213 |
fun dest_judgment ctxt = |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
214 |
let |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
215 |
val is_judgment = Object_Logic.is_judgment ctxt; |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
216 |
val drop_judgment = Object_Logic.drop_judgment ctxt; |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
217 |
in |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
218 |
fn t => |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
219 |
if is_judgment t then drop_judgment t |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
220 |
else raise TERM ("dest_judgment", [t]) |
1c2c0380d58b
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents:
74341
diff
changeset
|
221 |
end; |
74341 | 222 |
|
223 |
val _ = Theory.setup |
|
224 |
(ML_Antiquotation.value \<^binding>\<open>make_judgment\<close> |
|
74559 | 225 |
(Scan.succeed "ML_Antiquotations.make_judgment ML_context") #> |
74341 | 226 |
ML_Antiquotation.value \<^binding>\<open>dest_judgment\<close> |
74559 | 227 |
(Scan.succeed "ML_Antiquotations.dest_judgment ML_context")); |
74341 | 228 |
|
229 |
||
74580 | 230 |
(* type/term instantiations *) |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
231 |
|
74573
e2e2bc1f9570
avoid persistence of static context: instantiation arguments should provide proper dynamic context;
wenzelm
parents:
74571
diff
changeset
|
232 |
fun make_ctyp ctxt T = Thm.ctyp_of ctxt T |> Thm.trim_context_ctyp; |
e2e2bc1f9570
avoid persistence of static context: instantiation arguments should provide proper dynamic context;
wenzelm
parents:
74571
diff
changeset
|
233 |
fun make_cterm ctxt t = Thm.cterm_of ctxt t |> Thm.trim_context_cterm; |
e2e2bc1f9570
avoid persistence of static context: instantiation arguments should provide proper dynamic context;
wenzelm
parents:
74571
diff
changeset
|
234 |
|
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
235 |
type insts = ((indexname * sort) * typ) list * ((indexname * typ) * term) list |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
236 |
type cinsts = ((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
237 |
|
74579 | 238 |
fun instantiate_typ (insts: insts) = |
239 |
Term_Subst.instantiateT (TVars.make (#1 insts)); |
|
240 |
||
241 |
fun instantiate_ctyp pos (cinsts: cinsts) cT = |
|
242 |
Thm.instantiate_ctyp (TVars.make (#1 cinsts)) cT |
|
243 |
handle CTERM (msg, args) => Exn.reraise (CTERM (msg ^ Position.here pos, args)); |
|
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
244 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
245 |
fun instantiate_term (insts: insts) = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
246 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
247 |
val instT = TVars.make (#1 insts); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
248 |
val instantiateT = Term_Subst.instantiateT instT; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
249 |
val inst = Vars.make ((map o apfst o apsnd) instantiateT (#2 insts)); |
74577
d4829a7333e2
clarified instantiation: local beta reduction after substitution, as for Envir.expand_term_defs;
wenzelm
parents:
74573
diff
changeset
|
250 |
in Term_Subst.instantiate_beta (instT, inst) end; |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
251 |
|
74579 | 252 |
fun instantiate_cterm pos (cinsts: cinsts) ct = |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
253 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
254 |
val cinstT = TVars.make (#1 cinsts); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
255 |
val instantiateT = Term_Subst.instantiateT (TVars.map (K Thm.typ_of) cinstT); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
256 |
val cinst = Vars.make ((map o apfst o apsnd) instantiateT (#2 cinsts)); |
74579 | 257 |
in Thm.instantiate_beta_cterm (cinstT, cinst) ct end |
258 |
handle CTERM (msg, args) => Exn.reraise (CTERM (msg ^ Position.here pos, args)); |
|
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
259 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
260 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
261 |
local |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
262 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
263 |
fun make_keywords ctxt = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
264 |
Thy_Header.get_keywords' ctxt |
74567 | 265 |
|> Keyword.no_major_keywords |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
266 |
|> Keyword.add_major_keywords ["typ", "term", "prop", "ctyp", "cterm", "cprop"]; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
267 |
|
74570 | 268 |
val parse_inst_name = Parse.position (Parse.type_ident >> pair true || Parse.name >> pair false); |
269 |
||
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
270 |
val parse_inst = |
74570 | 271 |
(parse_inst_name -- (Parse.$$$ "=" |-- Parse.!!! Parse.embedded_ml) || |
272 |
Scan.ahead parse_inst_name -- Parse.embedded_ml) |
|
273 |
>> (fn (((b, a), pos), ml) => (b, ((a, pos), ml))); |
|
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
274 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
275 |
val parse_insts = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
276 |
Parse.and_list1 parse_inst >> (List.partition #1 #> apply2 (map #2)); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
277 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
278 |
val ml = ML_Lex.tokenize_no_range; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
279 |
val ml_range = ML_Lex.tokenize_range; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
280 |
fun ml_parens x = ml "(" @ x @ ml ")"; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
281 |
fun ml_bracks x = ml "[" @ x @ ml "]"; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
282 |
fun ml_commas xs = flat (separate (ml ", ") xs); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
283 |
val ml_list = ml_bracks o ml_commas; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
284 |
fun ml_pair (x, y) = ml_parens (ml_commas [x, y]); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
285 |
val ml_list_pair = ml_list o ListPair.map ml_pair; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
286 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
287 |
fun get_tfree envT (a, pos) = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
288 |
(case AList.lookup (op =) envT a of |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
289 |
SOME S => (a, S) |
74570 | 290 |
| NONE => error ("No occurrence of type variable " ^ quote a ^ Position.here pos)); |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
291 |
|
74571 | 292 |
fun check_free ctxt env (x, pos) = |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
293 |
(case AList.lookup (op =) env x of |
74571 | 294 |
SOME T => |
295 |
(Context_Position.reports ctxt (map (pair pos) (Syntax_Phases.markup_free ctxt x)); (x, T)) |
|
74570 | 296 |
| NONE => error ("No occurrence of variable " ^ quote x ^ Position.here pos)); |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
297 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
298 |
fun make_instT (a, pos) T = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
299 |
(case try (Term.dest_TVar o Logic.dest_type) T of |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
300 |
NONE => error ("Not a free type variable " ^ quote a ^ Position.here pos) |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
301 |
| SOME v => ml (ML_Syntax.print_pair ML_Syntax.print_indexname ML_Syntax.print_sort v)); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
302 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
303 |
fun make_inst (a, pos) t = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
304 |
(case try Term.dest_Var t of |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
305 |
NONE => error ("Not a free variable " ^ quote a ^ Position.here pos) |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
306 |
| SOME v => ml (ML_Syntax.print_pair ML_Syntax.print_indexname ML_Syntax.print_typ v)); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
307 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
308 |
fun prepare_insts ctxt1 ctxt0 (instT, inst) t = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
309 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
310 |
val envT = Term.add_tfrees t []; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
311 |
val env = Term.add_frees t []; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
312 |
val freesT = map (Logic.mk_type o TFree o get_tfree envT) instT; |
74571 | 313 |
val frees = map (Free o check_free ctxt1 env) inst; |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
314 |
val (t' :: varsT, vars) = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
315 |
Variable.export_terms ctxt1 ctxt0 (t :: freesT @ frees) |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
316 |
|> chop (1 + length freesT); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
317 |
val ml_insts = (map2 make_instT instT varsT, map2 make_inst inst vars); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
318 |
in (ml_insts, t') end; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
319 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
320 |
fun prepare_ml range (kind, ml1, ml2) ml_val (ml_instT, ml_inst) ctxt = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
321 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
322 |
val (ml_name, ctxt') = ML_Context.variant kind ctxt; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
323 |
val ml_env = ml ("val " ^ ml_name ^ " = ") @ ml ml1 @ ml_parens (ml ml_val) @ ml ";\n"; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
324 |
fun ml_body (ml_argsT, ml_args) = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
325 |
ml_parens (ml ml2 @ |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
326 |
ml_pair (ml_list_pair (ml_instT, ml_argsT), ml_list_pair (ml_inst, ml_args)) @ |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
327 |
ml_range range (ML_Context.struct_name ctxt ^ "." ^ ml_name)); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
328 |
in ((ml_env, ml_body), ctxt') end; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
329 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
330 |
fun prepare_type range (arg, s) insts ctxt = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
331 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
332 |
val T = Syntax.read_typ ctxt s; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
333 |
val t = Logic.mk_type T; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
334 |
val ctxt1 = Proof_Context.augment t ctxt; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
335 |
val (ml_insts, T') = prepare_insts ctxt1 ctxt insts t ||> Logic.dest_type; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
336 |
in prepare_ml range arg (ML_Syntax.print_typ T') ml_insts ctxt end; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
337 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
338 |
fun prepare_term read range (arg, (s, fixes)) insts ctxt = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
339 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
340 |
val ctxt' = #2 (Proof_Context.add_fixes_cmd fixes ctxt); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
341 |
val t = read ctxt' s; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
342 |
val ctxt1 = Proof_Context.augment t ctxt'; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
343 |
val (ml_insts, t') = prepare_insts ctxt1 ctxt insts t; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
344 |
in prepare_ml range arg (ML_Syntax.print_term t') ml_insts ctxt end; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
345 |
|
74579 | 346 |
val ml_here = ML_Syntax.atomic o ML_Syntax.print_position; |
347 |
||
348 |
fun typ_ml (kind, _: Position.T) = (kind, "", "ML_Antiquotations.instantiate_typ "); |
|
349 |
fun term_ml (kind, _: Position.T) = (kind, "", "ML_Antiquotations.instantiate_term "); |
|
350 |
fun ctyp_ml (kind, pos) = |
|
351 |
(kind, "ML_Antiquotations.make_ctyp ML_context", |
|
352 |
"ML_Antiquotations.instantiate_ctyp " ^ ml_here pos); |
|
353 |
fun cterm_ml (kind, pos) = |
|
354 |
(kind, "ML_Antiquotations.make_cterm ML_context", |
|
355 |
"ML_Antiquotations.instantiate_cterm " ^ ml_here pos); |
|
356 |
||
357 |
val command_name = Parse.position o Parse.command_name; |
|
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
358 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
359 |
fun parse_body range = |
74579 | 360 |
(command_name "typ" >> typ_ml || command_name "ctyp" >> ctyp_ml) -- |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
361 |
Parse.!!! Parse.typ >> prepare_type range || |
74579 | 362 |
(command_name "term" >> term_ml || command_name "cterm" >> cterm_ml) |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
363 |
-- Parse.!!! (Parse.term -- Parse.for_fixes) >> prepare_term Syntax.read_term range || |
74579 | 364 |
(command_name "prop" >> term_ml || command_name "cprop" >> cterm_ml) |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
365 |
-- Parse.!!! (Parse.term -- Parse.for_fixes) >> prepare_term Syntax.read_prop range; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
366 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
367 |
val _ = Theory.setup |
74570 | 368 |
(ML_Context.add_antiquotation \<^binding>\<open>instantiate\<close> true |
74566
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
369 |
(fn range => fn src => fn ctxt => |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
370 |
let |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
371 |
val (insts, prepare_val) = src |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
372 |
|> Parse.read_embedded_src ctxt (make_keywords ctxt) |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
373 |
((parse_insts --| Parse.$$$ "in") -- parse_body range); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
374 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
375 |
val (((ml_env, ml_body), decls), ctxt1) = ctxt |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
376 |
|> prepare_val (apply2 (map #1) insts) |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
377 |
||>> ML_Context.expand_antiquotes_list (op @ (apply2 (map #2) insts)); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
378 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
379 |
fun decl' ctxt' = |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
380 |
let val (ml_args_env, ml_args) = split_list (decls ctxt') |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
381 |
in (ml_env @ flat ml_args_env, ml_body (chop (length (#1 insts)) ml_args)) end; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
382 |
in (decl', ctxt1) end)); |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
383 |
|
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
384 |
in end; |
8e0f0317e266
ML antiquotations to instantiate types/terms/props;
wenzelm
parents:
74564
diff
changeset
|
385 |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
386 |
|
74580 | 387 |
(* type/term constructors *) |
388 |
||
74556 | 389 |
local |
390 |
||
391 |
val keywords = Keyword.add_minor_keywords ["for", "=>"] Keyword.empty_keywords; |
|
392 |
||
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
393 |
val parse_name = Parse.input Parse.name; |
74374
e585e5a906ba
more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents:
74344
diff
changeset
|
394 |
|
74562 | 395 |
val parse_args = Scan.repeat Parse.embedded_ml_underscore; |
74556 | 396 |
val parse_for_args = Scan.optional (Parse.$$$ "for" |-- Parse.!!! parse_args) []; |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
397 |
|
74317 | 398 |
fun parse_body b = |
74374
e585e5a906ba
more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents:
74344
diff
changeset
|
399 |
if b then Parse.$$$ "=>" |-- Parse.!!! Parse.embedded_input >> (ML_Lex.read_source #> single) |
74317 | 400 |
else Scan.succeed []; |
401 |
||
74374
e585e5a906ba
more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents:
74344
diff
changeset
|
402 |
fun is_dummy [Antiquote.Text tok] = ML_Lex.content_of tok = "_" |
e585e5a906ba
more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents:
74344
diff
changeset
|
403 |
| is_dummy _ = false; |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
404 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
405 |
val ml = ML_Lex.tokenize_no_range; |
74317 | 406 |
val ml_range = ML_Lex.tokenize_range; |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
407 |
val ml_dummy = ml "_"; |
74387
6edb71482de6
avoid overlapping PIDE markup (amending bb25ea271b15);
wenzelm
parents:
74378
diff
changeset
|
408 |
fun ml_enclose range x = ml "(" @ x @ ml_range range ")"; |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
409 |
fun ml_parens x = ml "(" @ x @ ml ")"; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
410 |
fun ml_bracks x = ml "[" @ x @ ml "]"; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
411 |
fun ml_commas xs = flat (separate (ml ", ") xs); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
412 |
val ml_list = ml_bracks o ml_commas; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
413 |
val ml_string = ml o ML_Syntax.print_string; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
414 |
fun ml_pair (x, y) = ml_parens (ml_commas [x, y]); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
415 |
|
74317 | 416 |
fun type_antiquotation binding {function} = |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
417 |
ML_Context.add_antiquotation binding true |
74317 | 418 |
(fn range => fn src => fn ctxt => |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
419 |
let |
74562 | 420 |
val ((s, type_args), fn_body) = src |
74564 | 421 |
|> Parse.read_embedded_src ctxt keywords (parse_name -- parse_args -- parse_body function); |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
422 |
val pos = Input.pos_of s; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
423 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
424 |
val Type (c, Ts) = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
425 |
Proof_Context.read_type_name {proper = true, strict = true} ctxt |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
426 |
(Syntax.implode_input s); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
427 |
val n = length Ts; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
428 |
val _ = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
429 |
length type_args = n orelse |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
430 |
error ("Type constructor " ^ quote (Proof_Context.markup_type ctxt c) ^ |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
431 |
" takes " ^ string_of_int n ^ " argument(s)" ^ Position.here pos); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
432 |
|
74562 | 433 |
val (decls1, ctxt1) = ML_Context.expand_antiquotes_list type_args ctxt; |
434 |
val (decls2, ctxt2) = ML_Context.expand_antiquotes_list fn_body ctxt1; |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
435 |
fun decl' ctxt' = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
436 |
let |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
437 |
val (ml_args_env, ml_args_body) = split_list (decls1 ctxt'); |
74317 | 438 |
val (ml_fn_env, ml_fn_body) = split_list (decls2 ctxt'); |
74378
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
439 |
val ml1 = |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
440 |
ml_enclose range (ml "Term.Type " @ ml_pair (ml_string c, ml_list ml_args_body)); |
74317 | 441 |
val ml2 = |
442 |
if function then |
|
74378
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
443 |
ml_enclose range |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
444 |
(ml_range range "fn " @ ml1 @ ml "=> " @ flat ml_fn_body @ |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
445 |
ml "| T => " @ ml_range range "raise" @ |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
446 |
ml " Term.TYPE (" @ ml_string ("Type_fn " ^ quote c) @ ml ", [T], [])") |
74317 | 447 |
else ml1; |
448 |
in (flat (ml_args_env @ ml_fn_env), ml2) end; |
|
449 |
in (decl', ctxt2) end); |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
450 |
|
74317 | 451 |
fun const_antiquotation binding {pattern, function} = |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
452 |
ML_Context.add_antiquotation binding true |
74317 | 453 |
(fn range => fn src => fn ctxt => |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
454 |
let |
74562 | 455 |
val (((s, type_args), term_args), fn_body) = src |
74564 | 456 |
|> Parse.read_embedded_src ctxt keywords |
74562 | 457 |
(parse_name -- parse_args -- parse_for_args -- parse_body function); |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
458 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
459 |
val Const (c, T) = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
460 |
Proof_Context.read_const {proper = true, strict = true} ctxt |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
461 |
(Syntax.implode_input s); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
462 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
463 |
val consts = Proof_Context.consts_of ctxt; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
464 |
val type_paths = Consts.type_arguments consts c; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
465 |
val type_params = map Term.dest_TVar (Consts.typargs consts (c, T)); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
466 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
467 |
val n = length type_params; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
468 |
val m = length (Term.binder_types T); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
469 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
470 |
fun err msg = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
471 |
error ("Constant " ^ quote (Proof_Context.markup_const ctxt c) ^ msg ^ |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
472 |
Position.here (Input.pos_of s)); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
473 |
val _ = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
474 |
length type_args <> n andalso err (" takes " ^ string_of_int n ^ " type argument(s)"); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
475 |
val _ = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
476 |
length term_args > m andalso Term.is_Type (Term.body_type T) andalso |
74377 | 477 |
err (" cannot have more than " ^ string_of_int m ^ " argument(s)"); |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
478 |
|
74562 | 479 |
val (decls1, ctxt1) = ML_Context.expand_antiquotes_list type_args ctxt; |
480 |
val (decls2, ctxt2) = ML_Context.expand_antiquotes_list term_args ctxt1; |
|
481 |
val (decls3, ctxt3) = ML_Context.expand_antiquotes_list fn_body ctxt2; |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
482 |
fun decl' ctxt' = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
483 |
let |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
484 |
val (ml_args_env1, ml_args_body1) = split_list (decls1 ctxt'); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
485 |
val (ml_args_env2, ml_args_body2) = split_list (decls2 ctxt'); |
74317 | 486 |
val (ml_fn_env, ml_fn_body) = split_list (decls3 ctxt'); |
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
487 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
488 |
val relevant = map is_dummy type_args ~~ type_paths; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
489 |
fun relevant_path is = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
490 |
not pattern orelse |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
491 |
let val p = rev is |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
492 |
in relevant |> exists (fn (u, q) => not u andalso is_prefix (op =) p q) end; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
493 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
494 |
val ml_typarg = the o AList.lookup (op =) (type_params ~~ ml_args_body1); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
495 |
fun ml_typ is (Type (d, Us)) = |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
496 |
if relevant_path is then |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
497 |
ml "Term.Type " @ |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
498 |
ml_pair (ml_string d, ml_list (map_index (fn (i, U) => ml_typ (i :: is) U) Us)) |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
499 |
else ml_dummy |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
500 |
| ml_typ is (TVar arg) = if relevant_path is then ml_typarg arg else ml_dummy |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
501 |
| ml_typ _ (TFree _) = raise Match; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
502 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
503 |
fun ml_app [] = ml "Term.Const " @ ml_pair (ml_string c, ml_typ [] T) |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
504 |
| ml_app (u :: us) = ml "Term.$ " @ ml_pair (ml_app us, u); |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
505 |
|
74317 | 506 |
val ml_env = flat (ml_args_env1 @ ml_args_env2 @ ml_fn_env); |
74378
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
507 |
val ml1 = ml_enclose range (ml_app (rev ml_args_body2)); |
74317 | 508 |
val ml2 = |
509 |
if function then |
|
74378
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
510 |
ml_enclose range |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
511 |
(ml_range range "fn " @ ml1 @ ml "=> " @ flat ml_fn_body @ |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
512 |
ml "| t => " @ ml_range range "raise" @ |
bb25ea271b15
clarified positions, notably for ML compiler errors;
wenzelm
parents:
74377
diff
changeset
|
513 |
ml " Term.TERM (" @ ml_string ("Const_fn " ^ quote c) @ ml ", [t])") |
74317 | 514 |
else ml1; |
515 |
in (ml_env, ml2) end; |
|
516 |
in (decl', ctxt3) end); |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
517 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
518 |
val _ = Theory.setup |
74317 | 519 |
(type_antiquotation \<^binding>\<open>Type\<close> {function = false} #> |
520 |
type_antiquotation \<^binding>\<open>Type_fn\<close> {function = true} #> |
|
521 |
const_antiquotation \<^binding>\<open>Const\<close> {pattern = false, function = false} #> |
|
522 |
const_antiquotation \<^binding>\<open>Const_\<close> {pattern = true, function = false} #> |
|
523 |
const_antiquotation \<^binding>\<open>Const_fn\<close> {pattern = true, function = true}); |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
524 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
525 |
in end; |
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
526 |
|
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74290
diff
changeset
|
527 |
|
73550
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
528 |
(* special forms *) |
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
529 |
|
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
530 |
val _ = Theory.setup |
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
531 |
(ML_Antiquotation.special_form \<^binding>\<open>try\<close> "() |> Basics.try" #> |
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
532 |
ML_Antiquotation.special_form \<^binding>\<open>can\<close> "() |> Basics.can"); |
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
533 |
|
2f6855142a8c
support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents:
70565
diff
changeset
|
534 |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
535 |
(* basic combinators *) |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
536 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
537 |
local |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
538 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
539 |
val parameter = Parse.position Parse.nat >> (fn (n, pos) => |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
540 |
if n > 1 then n else error ("Bad parameter: " ^ string_of_int n ^ Position.here pos)); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
541 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
542 |
fun indices n = map string_of_int (1 upto n); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
543 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
544 |
fun empty n = replicate_string n " []"; |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
545 |
fun dummy n = replicate_string n " _"; |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
546 |
fun vars x n = implode (map (fn a => " " ^ x ^ a) (indices n)); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
547 |
fun cons n = implode (map (fn a => " (x" ^ a ^ " :: xs" ^ a ^ ")") (indices n)); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
548 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
549 |
val tuple = enclose "(" ")" o commas; |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
550 |
fun tuple_empty n = tuple (replicate n "[]"); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
551 |
fun tuple_vars x n = tuple (map (fn a => x ^ a) (indices n)); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
552 |
fun tuple_cons n = "(" ^ tuple_vars "x" n ^ " :: xs)" |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
553 |
fun cons_tuple n = tuple (map (fn a => "x" ^ a ^ " :: xs" ^ a) (indices n)); |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
554 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
555 |
in |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
556 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
557 |
val _ = Theory.setup |
67147 | 558 |
(ML_Antiquotation.value \<^binding>\<open>map\<close> |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
559 |
(Scan.lift parameter >> (fn n => |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
560 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
561 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
562 |
\ fun map _" ^ empty n ^ " = []\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
563 |
\ | map f" ^ cons n ^ " = f" ^ vars "x" n ^ " :: map f" ^ vars "xs" n ^ "\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
564 |
\ | map _" ^ dummy n ^ " = raise ListPair.UnequalLengths\n" ^ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
565 |
" in map f end")) #> |
67147 | 566 |
ML_Antiquotation.value \<^binding>\<open>fold\<close> |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
567 |
(Scan.lift parameter >> (fn n => |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
568 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
569 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
570 |
\ fun fold _" ^ empty n ^ " a = a\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
571 |
\ | fold f" ^ cons n ^ " a = fold f" ^ vars "xs" n ^ " (f" ^ vars "x" n ^ " a)\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
572 |
\ | fold _" ^ dummy n ^ " _ = raise ListPair.UnequalLengths\n" ^ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
573 |
" in fold f end")) #> |
67147 | 574 |
ML_Antiquotation.value \<^binding>\<open>fold_map\<close> |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
575 |
(Scan.lift parameter >> (fn n => |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
576 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
577 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
578 |
\ fun fold_map _" ^ empty n ^ " a = ([], a)\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
579 |
\ | fold_map f" ^ cons n ^ " a =\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
580 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
581 |
\ val (x, a') = f" ^ vars "x" n ^ " a\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
582 |
\ val (xs, a'') = fold_map f" ^ vars "xs" n ^ " a'\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
583 |
\ in (x :: xs, a'') end\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
584 |
\ | fold_map _" ^ dummy n ^ " _ = raise ListPair.UnequalLengths\n" ^ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
585 |
" in fold_map f end")) #> |
67147 | 586 |
ML_Antiquotation.value \<^binding>\<open>split_list\<close> |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
587 |
(Scan.lift parameter >> (fn n => |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
588 |
"fn list =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
589 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
590 |
\ fun split_list [] =" ^ tuple_empty n ^ "\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
591 |
\ | split_list" ^ tuple_cons n ^ " =\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
592 |
\ let val" ^ tuple_vars "xs" n ^ " = split_list xs\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
593 |
\ in " ^ cons_tuple n ^ "end\n\ |
59057
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
594 |
\ in split_list list end")) #> |
67147 | 595 |
ML_Antiquotation.value \<^binding>\<open>apply\<close> |
59057
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
596 |
(Scan.lift (parameter -- Scan.option (Args.parens (Parse.position Parse.nat))) >> |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
597 |
(fn (n, opt_index) => |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
598 |
let |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
599 |
val cond = |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
600 |
(case opt_index of |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
601 |
NONE => K true |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
602 |
| SOME (index, index_pos) => |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
603 |
if 1 <= index andalso index <= n then equal (string_of_int index) |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
604 |
else error ("Bad index: " ^ string_of_int index ^ Position.here index_pos)); |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
605 |
in |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
606 |
"fn f => fn " ^ tuple_vars "x" n ^ " => " ^ |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
607 |
tuple (map (fn a => (if cond a then "f x" else "x") ^ a) (indices n)) |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
608 |
end))); |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
609 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
610 |
end; |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
611 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
612 |
|
56205 | 613 |
(* outer syntax *) |
614 |
||
615 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
616 |
(ML_Antiquotation.value_embedded \<^binding>\<open>keyword\<close> |
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
617 |
(Args.context -- |
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
618 |
Scan.lift (Parse.embedded_position || Parse.position (Parse.keyword_with (K true))) |
64594 | 619 |
>> (fn (ctxt, (name, pos)) => |
620 |
if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then |
|
621 |
(Context_Position.report ctxt pos (Token.keyword_markup (true, Markup.keyword2) name); |
|
622 |
"Parse.$$$ " ^ ML_Syntax.print_string name) |
|
623 |
else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos))) #> |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
624 |
ML_Antiquotation.value_embedded \<^binding>\<open>command_keyword\<close> |
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
68482
diff
changeset
|
625 |
(Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) => |
59934
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
626 |
(case Keyword.command_markup (Thy_Header.get_keywords' ctxt) name of |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
627 |
SOME markup => |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
628 |
(Context_Position.reports ctxt [(pos, markup), (pos, Markup.keyword1)]; |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
629 |
ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_position (name, pos)) |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
630 |
| NONE => error ("Bad outer syntax command " ^ quote name ^ Position.here pos))))); |
56205 | 631 |
|
632 |
end; |