author | wenzelm |
Sat, 05 Jan 2019 15:15:21 +0100 | |
changeset 69595 | ec135235fbcc |
parent 69592 | a80d8ec6c998 |
child 70565 | d0b75c59beca |
permissions | -rw-r--r-- |
56205 | 1 |
(* Title: Pure/ML/ml_antiquotations.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Miscellaneous ML antiquotations. |
|
5 |
*) |
|
6 |
||
7 |
structure ML_Antiquotations: sig end = |
|
8 |
struct |
|
9 |
||
58632 | 10 |
(* ML support *) |
11 |
||
12 |
val _ = Theory.setup |
|
67147 | 13 |
(ML_Antiquotation.inline \<^binding>\<open>undefined\<close> |
61597
53e32a9b66b8
added @{undefined} with somewhat undefined symbol;
wenzelm
parents:
61596
diff
changeset
|
14 |
(Scan.succeed "(raise General.Match)") #> |
53e32a9b66b8
added @{undefined} with somewhat undefined symbol;
wenzelm
parents:
61596
diff
changeset
|
15 |
|
67147 | 16 |
ML_Antiquotation.inline \<^binding>\<open>assert\<close> |
58632 | 17 |
(Scan.succeed "(fn b => if b then () else raise General.Fail \"Assertion failed\")") #> |
18 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
19 |
ML_Antiquotation.declaration_embedded \<^binding>\<open>print\<close> |
63120
629a4c5e953e
embedded content may be delimited via cartouches;
wenzelm
parents:
62900
diff
changeset
|
20 |
(Scan.lift (Scan.optional Args.embedded "Output.writeln")) |
58632 | 21 |
(fn src => fn output => fn ctxt => |
22 |
let |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
23 |
val struct_name = ML_Context.struct_name ctxt; |
58632 | 24 |
val (_, pos) = Token.name_of_src src; |
59112 | 25 |
val (a, ctxt') = ML_Context.variant "output" ctxt; |
58632 | 26 |
val env = |
27 |
"val " ^ a ^ ": string -> unit =\n\ |
|
28 |
\ (" ^ output ^ ") o (fn s => s ^ Position.here (" ^ |
|
29 |
ML_Syntax.print_position pos ^ "));\n"; |
|
30 |
val body = |
|
62900
c641bf9402fd
simplified default print_depth: context is usually available, in contrast to 0d295e339f52;
wenzelm
parents:
62899
diff
changeset
|
31 |
"(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
|
32 |
in (K (env, body), ctxt') end) #> |
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
33 |
|
67147 | 34 |
ML_Antiquotation.value \<^binding>\<open>rat\<close> |
63204
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
35 |
(Scan.lift (Scan.optional (Args.$$$ "~" >> K ~1) 1 -- Parse.nat -- |
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
36 |
Scan.optional (Args.$$$ "/" |-- Parse.nat) 1) >> (fn ((sign, a), b) => |
921a5be54132
support rat numerals via special antiquotation syntax;
wenzelm
parents:
63120
diff
changeset
|
37 |
"Rat.make " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int (sign * a, b)))) |
58632 | 38 |
|
39 |
||
40 |
(* formal entities *) |
|
41 |
||
56205 | 42 |
val _ = Theory.setup |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
43 |
(ML_Antiquotation.value_embedded \<^binding>\<open>system_option\<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
|
44 |
(Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (name, pos)) => |
67208 | 45 |
(Completion.check_option (Options.default ()) ctxt (name, pos) |> ML_Syntax.print_string))) #> |
56205 | 46 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
47 |
ML_Antiquotation.value_embedded \<^binding>\<open>theory\<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
|
48 |
(Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (name, pos)) => |
68482 | 49 |
(Theory.check {long = false} ctxt (name, pos); |
50 |
"Context.get_theory {long = false} (Proof_Context.theory_of ML_context) " ^ |
|
51 |
ML_Syntax.print_string name)) |
|
56205 | 52 |
|| Scan.succeed "Proof_Context.theory_of ML_context") #> |
53 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
54 |
ML_Antiquotation.value_embedded \<^binding>\<open>theory_context\<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
|
55 |
(Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (name, pos)) => |
68482 | 56 |
(Theory.check {long = false} ctxt (name, pos); |
56205 | 57 |
"Proof_Context.get_global (Proof_Context.theory_of ML_context) " ^ |
58 |
ML_Syntax.print_string name))) #> |
|
59 |
||
67147 | 60 |
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
|
61 |
(Args.context >> (fn ctxt => ML_Context.struct_name ctxt ^ ".ML_context")) #> |
56205 | 62 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
63 |
ML_Antiquotation.inline_embedded \<^binding>\<open>typ\<close> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
64 |
(Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ)) #> |
56205 | 65 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
66 |
ML_Antiquotation.inline_embedded \<^binding>\<open>term\<close> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
67 |
(Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term)) #> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
68 |
|
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
69 |
ML_Antiquotation.inline_embedded \<^binding>\<open>prop\<close> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
70 |
(Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term)) #> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
71 |
|
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
72 |
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
|
73 |
"Thm.ctyp_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))) #> |
56205 | 74 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
75 |
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
|
76 |
"Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #> |
56205 | 77 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
78 |
ML_Antiquotation.value_embedded \<^binding>\<open>cprop\<close> (Args.prop >> (fn t => |
62075 | 79 |
"Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #> |
80 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
81 |
ML_Antiquotation.inline_embedded \<^binding>\<open>method\<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
|
82 |
(Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (name, pos)) => |
62075 | 83 |
ML_Syntax.print_string (Method.check_name ctxt (name, pos))))); |
56205 | 84 |
|
85 |
||
63553
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
86 |
(* locales *) |
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
87 |
|
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
88 |
val _ = Theory.setup |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
89 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>locale\<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
|
90 |
(Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (name, pos)) => |
63553
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
91 |
Locale.check (Proof_Context.theory_of ctxt) (name, pos) |
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
92 |
|> ML_Syntax.print_string))); |
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
93 |
|
4a72b37ac4b8
text antiquotation for locales (similar to classes)
haftmann
parents:
63204
diff
changeset
|
94 |
|
56205 | 95 |
(* type classes *) |
96 |
||
63120
629a4c5e953e
embedded content may be delimited via cartouches;
wenzelm
parents:
62900
diff
changeset
|
97 |
fun class syn = Args.context -- Scan.lift Args.embedded_inner_syntax >> (fn (ctxt, s) => |
56205 | 98 |
Proof_Context.read_class ctxt s |
99 |
|> syn ? Lexicon.mark_class |
|
100 |
|> ML_Syntax.print_string); |
|
101 |
||
102 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
103 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>class\<close> (class false) #> |
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
104 |
ML_Antiquotation.inline_embedded \<^binding>\<open>class_syntax\<close> (class true) #> |
56205 | 105 |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
106 |
ML_Antiquotation.inline_embedded \<^binding>\<open>sort\<close> |
63120
629a4c5e953e
embedded content may be delimited via cartouches;
wenzelm
parents:
62900
diff
changeset
|
107 |
(Args.context -- Scan.lift Args.embedded_inner_syntax >> (fn (ctxt, s) => |
56205 | 108 |
ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))))); |
109 |
||
110 |
||
111 |
(* type constructors *) |
|
112 |
||
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
|
113 |
fun type_name kind check = Args.context -- Scan.lift Args.embedded_token |
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
|
114 |
>> (fn (ctxt, tok) => |
56205 | 115 |
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
|
116 |
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
|
117 |
val (_, pos) = Input.source_content (Token.input_of tok); |
56205 | 118 |
val Type (c, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s; |
119 |
val decl = Type.the_decl (Proof_Context.tsig_of ctxt) (c, pos); |
|
120 |
val res = |
|
121 |
(case try check (c, decl) of |
|
122 |
SOME res => res |
|
123 |
| NONE => error ("Not a " ^ kind ^ ": " ^ quote c ^ Position.here pos)); |
|
124 |
in ML_Syntax.print_string res end); |
|
125 |
||
126 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
127 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>type_name\<close> |
56205 | 128 |
(type_name "logical type" (fn (c, Type.LogicalType _) => c)) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
129 |
ML_Antiquotation.inline_embedded \<^binding>\<open>type_abbrev\<close> |
56205 | 130 |
(type_name "type abbreviation" (fn (c, Type.Abbreviation _) => c)) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
131 |
ML_Antiquotation.inline_embedded \<^binding>\<open>nonterminal\<close> |
56205 | 132 |
(type_name "nonterminal" (fn (c, Type.Nonterminal) => c)) #> |
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
133 |
ML_Antiquotation.inline_embedded \<^binding>\<open>type_syntax\<close> |
56205 | 134 |
(type_name "type" (fn (c, _) => Lexicon.mark_type c))); |
135 |
||
136 |
||
137 |
(* constants *) |
|
138 |
||
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
|
139 |
fun const_name check = Args.context -- Scan.lift Args.embedded_token |
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
|
140 |
>> (fn (ctxt, tok) => |
56205 | 141 |
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
|
142 |
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
|
143 |
val (_, pos) = Input.source_content (Token.input_of tok); |
56205 | 144 |
val Const (c, _) = Proof_Context.read_const {proper = true, strict = false} ctxt s; |
145 |
val res = check (Proof_Context.consts_of ctxt, c) |
|
146 |
handle TYPE (msg, _, _) => error (msg ^ Position.here pos); |
|
147 |
in ML_Syntax.print_string res end); |
|
148 |
||
149 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
150 |
(ML_Antiquotation.inline_embedded \<^binding>\<open>const_name\<close> |
56205 | 151 |
(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
|
152 |
ML_Antiquotation.inline_embedded \<^binding>\<open>const_abbrev\<close> |
56205 | 153 |
(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
|
154 |
ML_Antiquotation.inline_embedded \<^binding>\<open>const_syntax\<close> |
56205 | 155 |
(const_name (fn (_, c) => Lexicon.mark_const c)) #> |
156 |
||
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
157 |
ML_Antiquotation.inline_embedded \<^binding>\<open>syntax_const\<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
|
158 |
(Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (c, pos)) => |
56205 | 159 |
if is_some (Syntax.lookup_const (Proof_Context.syn_of ctxt) c) |
160 |
then ML_Syntax.print_string c |
|
161 |
else error ("Unknown syntax const: " ^ quote c ^ Position.here pos))) #> |
|
162 |
||
69595 | 163 |
ML_Antiquotation.inline_embedded \<^binding>\<open>const\<close> |
63120
629a4c5e953e
embedded content may be delimited via cartouches;
wenzelm
parents:
62900
diff
changeset
|
164 |
(Args.context -- Scan.lift (Parse.position Args.embedded_inner_syntax) -- Scan.optional |
56205 | 165 |
(Scan.lift (Args.$$$ "(") |-- Parse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) [] |
56251 | 166 |
>> (fn ((ctxt, (raw_c, pos)), Ts) => |
56205 | 167 |
let |
168 |
val Const (c, _) = |
|
169 |
Proof_Context.read_const {proper = true, strict = true} ctxt raw_c; |
|
170 |
val consts = Proof_Context.consts_of ctxt; |
|
171 |
val n = length (Consts.typargs consts (c, Consts.type_scheme consts c)); |
|
172 |
val _ = length Ts <> n andalso |
|
173 |
error ("Constant requires " ^ string_of_int n ^ " type argument(s): " ^ |
|
56251 | 174 |
quote c ^ enclose "(" ")" (commas (replicate n "_")) ^ Position.here pos); |
56205 | 175 |
val const = Const (c, Consts.instance consts (c, Ts)); |
176 |
in ML_Syntax.atomic (ML_Syntax.print_term const) end))); |
|
177 |
||
178 |
||
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
179 |
(* basic combinators *) |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
180 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
181 |
local |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
182 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
186 |
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
|
187 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
199 |
in |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
200 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
201 |
val _ = Theory.setup |
67147 | 202 |
(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
|
203 |
(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
|
204 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
205 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
206 |
\ 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
|
207 |
\ | 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
|
208 |
\ | 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
|
209 |
" in map f end")) #> |
67147 | 210 |
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
|
211 |
(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
|
212 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
213 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
214 |
\ 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
|
215 |
\ | 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
|
216 |
\ | 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
|
217 |
" in fold f end")) #> |
67147 | 218 |
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
|
219 |
(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
|
220 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
221 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
222 |
\ 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
|
223 |
\ | 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
|
224 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
225 |
\ 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
|
226 |
\ 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
|
227 |
\ 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
|
228 |
\ | 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
|
229 |
" in fold_map f end")) #> |
67147 | 230 |
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
|
231 |
(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
|
232 |
"fn list =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
233 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
234 |
\ 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
|
235 |
\ | 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
|
236 |
\ 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
|
237 |
\ in " ^ cons_tuple n ^ "end\n\ |
59057
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
238 |
\ in split_list list end")) #> |
67147 | 239 |
ML_Antiquotation.value \<^binding>\<open>apply\<close> |
59057
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
240 |
(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
|
241 |
(fn (n, opt_index) => |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
242 |
let |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
243 |
val cond = |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
244 |
(case opt_index of |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
245 |
NONE => K true |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
246 |
| SOME (index, index_pos) => |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
247 |
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
|
248 |
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
|
249 |
in |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
250 |
"fn f => fn " ^ tuple_vars "x" n ^ " => " ^ |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
251 |
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
|
252 |
end))); |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
253 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
254 |
end; |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
255 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
256 |
|
56205 | 257 |
(* outer syntax *) |
258 |
||
259 |
val _ = Theory.setup |
|
69592
a80d8ec6c998
support for isabelle update -u control_cartouches;
wenzelm
parents:
69349
diff
changeset
|
260 |
(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
|
261 |
(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
|
262 |
Scan.lift (Parse.embedded_position || Parse.position (Parse.keyword_with (K true))) |
64594 | 263 |
>> (fn (ctxt, (name, pos)) => |
264 |
if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then |
|
265 |
(Context_Position.report ctxt pos (Token.keyword_markup (true, Markup.keyword2) name); |
|
266 |
"Parse.$$$ " ^ ML_Syntax.print_string name) |
|
267 |
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
|
268 |
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
|
269 |
(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
|
270 |
(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
|
271 |
SOME markup => |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
272 |
(Context_Position.reports ctxt [(pos, markup), (pos, Markup.keyword1)]; |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
273 |
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
|
274 |
| NONE => error ("Bad outer syntax command " ^ quote name ^ Position.here pos))))); |
56205 | 275 |
|
276 |
end; |