author | wenzelm |
Sat, 07 Nov 2015 00:28:42 +0100 | |
changeset 61596 | 8323b8e21fe9 |
parent 61154 | 82798c8bfa7f |
child 61597 | 53e32a9b66b8 |
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 |
|
61596 | 13 |
(ML_Antiquotation.value @{binding cartouche} |
14 |
(Args.context -- Scan.lift (Parse.position Args.cartouche_input) >> (fn (ctxt, (source, pos)) => |
|
15 |
(Context_Position.report ctxt pos Markup.ML_cartouche; |
|
16 |
"Input.source true " ^ ML_Syntax.print_string (Input.text_of source) ^ " " ^ |
|
17 |
ML_Syntax.atomic (ML_Syntax.print_range (Input.range_of source))))) #> |
|
18 |
||
19 |
ML_Antiquotation.inline @{binding assert} |
|
58632 | 20 |
(Scan.succeed "(fn b => if b then () else raise General.Fail \"Assertion failed\")") #> |
21 |
||
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
22 |
ML_Antiquotation.inline @{binding make_string} |
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
23 |
(Args.context >> (ml_make_string o ML_Context.struct_name)) #> |
58632 | 24 |
|
25 |
ML_Antiquotation.declaration @{binding print} |
|
26 |
(Scan.lift (Scan.optional Args.name "Output.writeln")) |
|
27 |
(fn src => fn output => fn ctxt => |
|
28 |
let |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
29 |
val struct_name = ML_Context.struct_name ctxt; |
58632 | 30 |
val (_, pos) = Token.name_of_src src; |
59112 | 31 |
val (a, ctxt') = ML_Context.variant "output" ctxt; |
58632 | 32 |
val env = |
33 |
"val " ^ a ^ ": string -> unit =\n\ |
|
34 |
\ (" ^ output ^ ") o (fn s => s ^ Position.here (" ^ |
|
35 |
ML_Syntax.print_position pos ^ "));\n"; |
|
36 |
val body = |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
37 |
"(fn x => (" ^ struct_name ^ "." ^ a ^ " (" ^ ml_make_string struct_name ^ " x); x))"; |
58632 | 38 |
in (K (env, body), ctxt') end)); |
39 |
||
40 |
||
41 |
(* formal entities *) |
|
42 |
||
56205 | 43 |
val _ = Theory.setup |
56467 | 44 |
(ML_Antiquotation.value @{binding system_option} |
56465 | 45 |
(Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (name, pos)) => |
59812 | 46 |
let |
47 |
val markup = |
|
48 |
Options.default_markup (name, pos) handle ERROR msg => |
|
49 |
let |
|
50 |
val completion = |
|
51 |
Completion.make (name, pos) (fn completed => |
|
52 |
Options.names (Options.default ()) |
|
53 |
|> filter completed |
|
54 |
|> map (fn a => (a, ("system_option", a)))); |
|
55 |
val report = Markup.markup_report (Completion.reported_text completion); |
|
59878 | 56 |
in error (msg ^ report) end; |
59812 | 57 |
val _ = Context_Position.report ctxt pos markup; |
58 |
in ML_Syntax.print_string name end)) #> |
|
56205 | 59 |
|
60 |
ML_Antiquotation.value @{binding theory} |
|
61 |
(Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (name, pos)) => |
|
60099 | 62 |
(Theory.check ctxt (name, pos); |
56205 | 63 |
"Context.get_theory (Proof_Context.theory_of ML_context) " ^ ML_Syntax.print_string name)) |
64 |
|| Scan.succeed "Proof_Context.theory_of ML_context") #> |
|
65 |
||
66 |
ML_Antiquotation.value @{binding theory_context} |
|
67 |
(Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (name, pos)) => |
|
60099 | 68 |
(Theory.check ctxt (name, pos); |
56205 | 69 |
"Proof_Context.get_global (Proof_Context.theory_of ML_context) " ^ |
70 |
ML_Syntax.print_string name))) #> |
|
71 |
||
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
72 |
ML_Antiquotation.inline @{binding context} |
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59112
diff
changeset
|
73 |
(Args.context >> (fn ctxt => ML_Context.struct_name ctxt ^ ".ML_context")) #> |
56205 | 74 |
|
75 |
ML_Antiquotation.inline @{binding typ} (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ)) #> |
|
76 |
ML_Antiquotation.inline @{binding term} (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term)) #> |
|
77 |
ML_Antiquotation.inline @{binding prop} (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term)) #> |
|
78 |
||
79 |
ML_Antiquotation.value @{binding ctyp} (Args.typ >> (fn T => |
|
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59573
diff
changeset
|
80 |
"Thm.ctyp_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))) #> |
56205 | 81 |
|
82 |
ML_Antiquotation.value @{binding cterm} (Args.term >> (fn t => |
|
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59573
diff
changeset
|
83 |
"Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #> |
56205 | 84 |
|
85 |
ML_Antiquotation.value @{binding cprop} (Args.prop >> (fn t => |
|
61154 | 86 |
"Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t)))); |
56205 | 87 |
|
88 |
||
89 |
(* type classes *) |
|
90 |
||
91 |
fun class syn = Args.context -- Scan.lift Args.name_inner_syntax >> (fn (ctxt, s) => |
|
92 |
Proof_Context.read_class ctxt s |
|
93 |
|> syn ? Lexicon.mark_class |
|
94 |
|> ML_Syntax.print_string); |
|
95 |
||
96 |
val _ = Theory.setup |
|
97 |
(ML_Antiquotation.inline @{binding class} (class false) #> |
|
98 |
ML_Antiquotation.inline @{binding class_syntax} (class true) #> |
|
99 |
||
100 |
ML_Antiquotation.inline @{binding sort} |
|
101 |
(Args.context -- Scan.lift Args.name_inner_syntax >> (fn (ctxt, s) => |
|
102 |
ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))))); |
|
103 |
||
104 |
||
105 |
(* type constructors *) |
|
106 |
||
107 |
fun type_name kind check = Args.context -- Scan.lift (Parse.position Args.name_inner_syntax) |
|
108 |
>> (fn (ctxt, (s, pos)) => |
|
109 |
let |
|
110 |
val Type (c, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s; |
|
111 |
val decl = Type.the_decl (Proof_Context.tsig_of ctxt) (c, pos); |
|
112 |
val res = |
|
113 |
(case try check (c, decl) of |
|
114 |
SOME res => res |
|
115 |
| NONE => error ("Not a " ^ kind ^ ": " ^ quote c ^ Position.here pos)); |
|
116 |
in ML_Syntax.print_string res end); |
|
117 |
||
118 |
val _ = Theory.setup |
|
119 |
(ML_Antiquotation.inline @{binding type_name} |
|
120 |
(type_name "logical type" (fn (c, Type.LogicalType _) => c)) #> |
|
121 |
ML_Antiquotation.inline @{binding type_abbrev} |
|
122 |
(type_name "type abbreviation" (fn (c, Type.Abbreviation _) => c)) #> |
|
123 |
ML_Antiquotation.inline @{binding nonterminal} |
|
124 |
(type_name "nonterminal" (fn (c, Type.Nonterminal) => c)) #> |
|
125 |
ML_Antiquotation.inline @{binding type_syntax} |
|
126 |
(type_name "type" (fn (c, _) => Lexicon.mark_type c))); |
|
127 |
||
128 |
||
129 |
(* constants *) |
|
130 |
||
131 |
fun const_name check = Args.context -- Scan.lift (Parse.position Args.name_inner_syntax) |
|
132 |
>> (fn (ctxt, (s, pos)) => |
|
133 |
let |
|
134 |
val Const (c, _) = Proof_Context.read_const {proper = true, strict = false} ctxt s; |
|
135 |
val res = check (Proof_Context.consts_of ctxt, c) |
|
136 |
handle TYPE (msg, _, _) => error (msg ^ Position.here pos); |
|
137 |
in ML_Syntax.print_string res end); |
|
138 |
||
139 |
val _ = Theory.setup |
|
140 |
(ML_Antiquotation.inline @{binding const_name} |
|
141 |
(const_name (fn (consts, c) => (Consts.the_const consts c; c))) #> |
|
142 |
ML_Antiquotation.inline @{binding const_abbrev} |
|
143 |
(const_name (fn (consts, c) => (Consts.the_abbreviation consts c; c))) #> |
|
144 |
ML_Antiquotation.inline @{binding const_syntax} |
|
145 |
(const_name (fn (_, c) => Lexicon.mark_const c)) #> |
|
146 |
||
147 |
ML_Antiquotation.inline @{binding syntax_const} |
|
148 |
(Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (c, pos)) => |
|
149 |
if is_some (Syntax.lookup_const (Proof_Context.syn_of ctxt) c) |
|
150 |
then ML_Syntax.print_string c |
|
151 |
else error ("Unknown syntax const: " ^ quote c ^ Position.here pos))) #> |
|
152 |
||
153 |
ML_Antiquotation.inline @{binding const} |
|
56251 | 154 |
(Args.context -- Scan.lift (Parse.position Args.name_inner_syntax) -- Scan.optional |
56205 | 155 |
(Scan.lift (Args.$$$ "(") |-- Parse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) [] |
56251 | 156 |
>> (fn ((ctxt, (raw_c, pos)), Ts) => |
56205 | 157 |
let |
158 |
val Const (c, _) = |
|
159 |
Proof_Context.read_const {proper = true, strict = true} ctxt raw_c; |
|
160 |
val consts = Proof_Context.consts_of ctxt; |
|
161 |
val n = length (Consts.typargs consts (c, Consts.type_scheme consts c)); |
|
162 |
val _ = length Ts <> n andalso |
|
163 |
error ("Constant requires " ^ string_of_int n ^ " type argument(s): " ^ |
|
56251 | 164 |
quote c ^ enclose "(" ")" (commas (replicate n "_")) ^ Position.here pos); |
56205 | 165 |
val const = Const (c, Consts.instance consts (c, Ts)); |
166 |
in ML_Syntax.atomic (ML_Syntax.print_term const) end))); |
|
167 |
||
168 |
||
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
169 |
(* basic combinators *) |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
170 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
171 |
local |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
172 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
173 |
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
|
174 |
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
|
175 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
176 |
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
|
177 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
189 |
in |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
190 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
191 |
val _ = Theory.setup |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
192 |
(ML_Antiquotation.value @{binding map} |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
193 |
(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
|
194 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
195 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
196 |
\ 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
|
197 |
\ | 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
|
198 |
\ | 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
|
199 |
" in map f end")) #> |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
200 |
ML_Antiquotation.value @{binding fold} |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
201 |
(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
|
202 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
203 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
204 |
\ 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
|
205 |
\ | 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
|
206 |
\ | 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
|
207 |
" in fold f end")) #> |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
208 |
ML_Antiquotation.value @{binding fold_map} |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
209 |
(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
|
210 |
"fn f =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
211 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
212 |
\ 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
|
213 |
\ | 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
|
214 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
215 |
\ 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
|
216 |
\ 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
|
217 |
\ 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
|
218 |
\ | 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
|
219 |
" in fold_map f end")) #> |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
220 |
ML_Antiquotation.value @{binding split_list} |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
221 |
(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
|
222 |
"fn list =>\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
223 |
\ let\n\ |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
224 |
\ 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
|
225 |
\ | 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
|
226 |
\ 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
|
227 |
\ in " ^ cons_tuple n ^ "end\n\ |
59057
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
228 |
\ in split_list list end")) #> |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
229 |
ML_Antiquotation.value @{binding apply} |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
230 |
(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
|
231 |
(fn (n, opt_index) => |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
232 |
let |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
233 |
val cond = |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
234 |
(case opt_index of |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
235 |
NONE => K true |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
236 |
| SOME (index, index_pos) => |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
in |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
240 |
"fn f => fn " ^ tuple_vars "x" n ^ " => " ^ |
5b649fb2f2e1
added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents:
58978
diff
changeset
|
241 |
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
|
242 |
end))); |
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
243 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
244 |
end; |
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
245 |
|
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58632
diff
changeset
|
246 |
|
56205 | 247 |
(* outer syntax *) |
248 |
||
249 |
val _ = Theory.setup |
|
250 |
(ML_Antiquotation.value @{binding keyword} |
|
58932 | 251 |
(Args.theory -- Scan.lift (Parse.position Parse.string) >> (fn (thy, (name, pos)) => |
252 |
if Keyword.is_keyword (Thy_Header.get_keywords thy) name then |
|
253 |
"Parse.$$$ " ^ ML_Syntax.print_string name |
|
254 |
else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos))) #> |
|
59936
b8ffc3dc9e24
@{command_spec} is superseded by @{command_keyword};
wenzelm
parents:
59934
diff
changeset
|
255 |
ML_Antiquotation.value @{binding command_keyword} |
59934
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
256 |
(Args.context -- Scan.lift (Parse.position Parse.name) >> (fn (ctxt, (name, pos)) => |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
257 |
(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
|
258 |
SOME markup => |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
259 |
(Context_Position.reports ctxt [(pos, markup), (pos, Markup.keyword1)]; |
b65c4370f831
more position information and PIDE markup for command keywords;
wenzelm
parents:
59878
diff
changeset
|
260 |
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
|
261 |
| NONE => error ("Bad outer syntax command " ^ quote name ^ Position.here pos))))); |
56205 | 262 |
|
263 |
end; |