8569
|
1 |
(* Title: HOL/ex/Multiquote.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
9297
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
8569
|
5 |
|
|
6 |
Multiple nested quotations and anti-quotations -- basically a
|
|
7 |
generalized version of de-Bruijn representation.
|
|
8 |
*)
|
|
9 |
|
9297
|
10 |
theory Multiquote = Main:
|
8569
|
11 |
|
|
12 |
syntax
|
8578
|
13 |
"_quote" :: "'b => ('a => 'b)" (".'(_')." [0] 1000)
|
9297
|
14 |
"_antiquote" :: "('a => 'b) => 'b" ("`_" [1000] 1000)
|
8569
|
15 |
|
|
16 |
parse_translation {*
|
|
17 |
let
|
|
18 |
fun antiquote_tr i (Const ("_antiquote", _) $ (t as Const ("_antiquote", _) $ _)) =
|
|
19 |
skip_antiquote_tr i t
|
|
20 |
| antiquote_tr i (Const ("_antiquote", _) $ t) =
|
|
21 |
antiquote_tr i t $ Bound i
|
|
22 |
| antiquote_tr i (t $ u) = antiquote_tr i t $ antiquote_tr i u
|
|
23 |
| antiquote_tr i (Abs (x, T, t)) = Abs (x, T, antiquote_tr (i + 1) t)
|
|
24 |
| antiquote_tr _ a = a
|
|
25 |
and skip_antiquote_tr i ((c as Const ("_antiquote", _)) $ t) =
|
|
26 |
c $ skip_antiquote_tr i t
|
|
27 |
| skip_antiquote_tr i t = antiquote_tr i t;
|
|
28 |
|
8578
|
29 |
fun quote_tr [t] = Abs ("s", dummyT, antiquote_tr 0 (Term.incr_boundvars 1 t))
|
8569
|
30 |
| quote_tr ts = raise TERM ("quote_tr", ts);
|
|
31 |
in [("_quote", quote_tr)] end
|
9297
|
32 |
*}
|
8569
|
33 |
|
9297
|
34 |
text {* basic examples *}
|
|
35 |
term ".(a + b + c)."
|
|
36 |
term ".(a + b + c + `x + `y + 1)."
|
|
37 |
term ".(`(f w) + `x)."
|
|
38 |
term ".(f `x `y z)."
|
8569
|
39 |
|
9297
|
40 |
text {* advanced examples *}
|
|
41 |
term ".(.(` `x + `y).)."
|
|
42 |
term ".(.(` `x + `y). o `f)."
|
|
43 |
term ".(`(f o `g))."
|
|
44 |
term ".(.( ` `(f o `g) ).)."
|
8569
|
45 |
|
9297
|
46 |
end
|