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